diff --git a/fulcrm_shoppingcart.module b/fulcrm_shoppingcart.module index cc72224..d8502d2 100644 --- a/fulcrm_shoppingcart.module +++ b/fulcrm_shoppingcart.module @@ -176,7 +176,8 @@ function fulcrm_shoppingcart_get_session_cart( $create = true ) { } $api_data = fulcrm_apiv2_GET( 'shoppingcart/' . $_SESSION[ 'fulcrm_shoppingcart_id' ] . '/', - $query = array( 'expand' => implode( ',', array( 'shoppingitems', + $query = array( 'expand' => implode( ',', array( 'd', + 'shoppingitems', 'shoppingitems.d', 'shoppingitems.content_object', 'shoppingitems.product' ) ) ) ); @@ -236,7 +237,7 @@ function fulcrm_shoppingcart_remove_url( $shoppingitem_id ) { return url( 'fulcrm/cart/remove/' . $shoppingitem_id . '/' . $hmac ); } -function fulcrm_shoppingcart_person_add_form_submit( $form, &$form_state ) { +function _fulcrm_shoppingcart_person_add_form_submit_to_data( $form, &$form_state ) { $person_d = fulcrm_collection_form_values_to_d( 'person', $form_state[ 'values' ] ); $person_data = array( 'd' => $person_d ); @@ -248,7 +249,10 @@ function fulcrm_shoppingcart_person_add_form_submit( $form, &$form_state ) { } $person_data[ 'uuid' ] = fulcrm_uuid_uuid4(); +} +function fulcrm_shoppingcart_person_add_form_submit( $form, &$form_state ) { + $person_data = _fulcrm_shoppingcart_person_add_form_submit_to_data( $form, $form_state ); $person_data = module_invoke_all( 'fulcrm_shoppingcart_alter_person_add', $person_data ); $api_data = fulcrm_apiv2_POST( 'person/', $person_data, $query = array( 'expand' => 'd' ) ); @@ -258,23 +262,33 @@ function fulcrm_shoppingcart_person_add_form_submit( $form, &$form_state ) { } function fulcrm_shoppingcart_person_add_guest_form_submit( $form, &$form_state ) { - $person_d = fulcrm_collection_form_values_to_d( 'person', $form_state[ 'values' ] ); - - $person_data = array( 'd' => $person_d ); - if ( array_key_exists( 'name', $person_d ) ) { - if ( is_array( $person_d[ 'name' ] ) ) - $person_data[ 'name' ] = $person_d[ 'name' ][ 0 ]; - elseif ( is_string( $person_d[ 'name' ] ) ) - $person_data[ 'name' ] = $person_d[ 'name' ]; - } + $person_data = _fulcrm_shoppingcart_person_add_form_submit_to_data( $form, $form_state ); + $person_data = module_invoke_all( 'fulcrm_shoppingcart_alter_person_add', $person_data ); - $person_data[ 'uuid' ] = fulcrm_uuid_uuid4(); + $cart = fulcrm_shoppingcart_get_session_cart(); + if ( $cart[ 'url' ] ) { + $cart[ 'd' ][ 'fulcrm' ][ 'person' ][ $person_data[ 'uuid' ] ] = $person_data; + if ( !array_key_exists( 'transaction', $cart[ 'd' ][ 'fulcrm' ] ) || + !array_key_exists( 'person', $cart[ 'd' ][ 'fulcrm' ][ 'transaction' ] ) ) { + $cart[ 'd' ][ 'fulcrm' ][ 'transaction' ][ 'person' ] = $person_data[ 'uuid' ]; + } - $person_data = module_invoke_all( 'fulcrm_shoppingcart_alter_person_add_guest', $person_data ); + $api_data = fulcrm_apiv2_PATCH( $cart[ 'url' ], array( 'd' => $cart[ 'd' ] ), + $query = array( 'expand' => implode( ',', array( 'd', + 'shoppingitems', + 'shoppingitems.d', + 'shoppingitems.content_object', + 'shoppingitems.product' ) ) ) ); + if ( $api_data[ 'success' ] ) { + module_invoke_all( 'fulcrm_shoppingcart_person_add', $api_data[ 'data' ], array_key_exists( 'destination', $_GET ) ? $_GET[ 'destination' ] : variable_get( 'fulcrm_shoppingcart_proceed_url', url('') ) ); - $api_data = fulcrm_apiv2_POST( 'person/', $person_data, $query = array( 'expand' => 'd' ) ); - if ( $api_data[ 'success' ] ) { - module_invoke_all( 'fulcrm_shoppingcart_person_add', $api_data[ 'data' ], array_key_exists( 'destination', $_GET ) ? $_GET[ 'destination' ] : variable_get( 'fulcrm_shoppingcart_proceed_url', url('') ) ); + $form_state[ 'fulcrm_collection' ][ 'person' ] = $person_data; + $form_state[ 'fulcrm_collection' ][ 'shoppingcart' ] = $api_data[ 'cart' ]; + } else { + drupal_set_message( t('There was a problem adding your item to the shopping cart.'), 'error' ); + } + } else { + drupal_set_message( t('There was a problem adding your item to the shopping cart.'), 'error' ); } } @@ -307,16 +321,62 @@ function fulcrm_shoppingcart_person_add( $collection_id, $hmac ) { } } +function _fulcrm_shoppingcart_buy_eventbookingtype_create_shoppingitem( &$form_state, $person_uuid, $person_name ) { + $shoppingitem_name = ( $form_state[ 'fulcrm_shoppingcart' ][ 'eventbookingtype' ][ 'event' ][ 'name' ] . "\r\n" . + $form_state[ 'fulcrm_shoppingcart' ][ 'eventbookingtype' ][ 'name' ] . ', ' . $person_name . "\r\n" . + $form_state[ 'fulcrm_shoppingcart' ][ 'product' ][ 'name' ] ); + + $eventslotbookings = array(); + if ( array_key_exists( 'eventbookingtypeslots', $form_state[ 'fulcrm_shoppingcart' ][ 'eventbookingtype' ] ) ) { + foreach ( $form_state[ 'fulcrm_shoppingcart' ][ 'eventbookingtype' ][ 'eventbookingtypeslots' ] as $eventbookingtypeslots ) { + $eventpart_id = fulcrm_apiv2_url_to_pk( $eventbookingtypeslots[ 'eventslot' ][ 'eventpart' ] ); + $eventslot_id = fulcrm_apiv2_url_to_pk( $eventbookingtypeslots[ 'eventslot' ][ 'url' ] ); + + if ( array_key_exists( $eventpart_id, $eventslotbookings ) ) + drupal_set_message( 'XXX cannot yet cope with multiple choice event slot bookings; defaulting to first.', 'warning' ); + + $eventslotbookings[ $eventpart_id ] = $eventslot_id; + } + } else { + drupal_set_message( 'XXX no eventbookingtypeslots to choose from?', 'warning' ); + } + + if ( $person_uuid ) { + $item = array( 'shoppingcart' => $cart[ 'url' ], + 'product' => fulcrm_apiv2_make_url( 'product', $product_id ), + 'quantity' => 1.0, + 'd' => array( 'fulcrm' => array( 'eventbooking' => array( 'eventbooking' => array(), + 'eventslotbookings' => $eventslotbookings, + 'person' => $person_uuid, + ), + ), + 'name' => $shoppingitem_name, + ), + ); + + $api_data = fulcrm_apiv2_POST( 'shoppingitem/', $item, $query = array( 'expand' => 'd' ) ); + fulcrm_shoppingcart_invalidate_cache(); + if ( $api_data[ 'success' ] ) { + $hook_results = module_invoke_all( 'fulcrm_shoppingcart_post_add_to_cart', $cart, $person_data[ 'data' ], $api_data[ 'data' ] ); + foreach ( $hook_results as $hook_result ) + return $hook_result; + + drupal_set_message( t('Added "%item" to shopping cart.', array( '%item' => $item[ 'd' ][ 'name' ] ) ), 'status' ); + $form_state[ 'redirect' ] = 'fulcrm/cart'; + } else { + drupal_set_message( t('There was a problem adding your item to the shopping cart.'), 'error' ); + } + } + +} + function fulcrm_shoppingcart_buy_eventbookingtype_form_submit( $form, &$form_state ) { // $person_d = fulcrm_collection_form_values_to_d( 'person', $form_state[ 'values' ] ); $product_id = $form_state[ 'fulcrm_shoppingcart' ][ 'product' ][ 'id' ]; $eventbookingtype_id = $form_state[ 'fulcrm_shoppingcart' ][ 'eventbookingtype' ][ 'id' ]; - $person_id = NULL; $person_uuid = NULL; - if ( array_key_exists( 'id', $form_state[ 'fulcrm_shoppingcart' ] ) ) - $person_id = $form_state[ 'fulcrm_shoppingcart' ][ 'person' ][ 'id' ]; if ( array_key_exists( 'uuid', $form_state[ 'fulcrm_shoppingcart' ] ) ) $person_uuid = $form_state[ 'fulcrm_shoppingcart' ][ 'person' ][ 'uuid' ]; @@ -327,73 +387,44 @@ function fulcrm_shoppingcart_buy_eventbookingtype_form_submit( $form, &$form_sta if ( is_array( $cart ) ) { if ( $person_uuid ) { - $api_data = fulcrm_apiv2_PATCH( $cart[ 'url' ], - array( 'd' => array( 'fulcrm' => array( 'transaction' => array( 'person' => $person_uuid, - ), - 'person' => array( $person_uuid => array() ), - ), - ), - ), - $query = array( 'expand' => 'd' ) ); - } - - foreach ( $form_state[ 'values' ][ 'ticket_for_persons' ] as $ticket_for_person ) { - $person_name = 'unnamed person'; - if ( $ticket_for_person === 'myself' ) { - $person_uuid = $form_state[ 'fulcrm_shoppingcart' ][ 'person' ][ 'uuid' ]; - $person_name = $form_state[ 'fulcrm_shoppingcart' ][ 'person' ][ 'name' ]; - } else { - $person_uuid = fulcrm_crypto_get_object_id_form_value( 'person.uuid', $ticket_for_person ); - if ( $person_uuid ) - $person_name = $form_state[ 'fulcrm_shoppingcart' ][ 'persons' ][ $person_uuid ][ 'name' ]; + if ( !array_key_exists( 'fulcrm', $cart[ 'd' ] ) || + !array_key_exists( 'person', $cart[ 'd' ][ 'fulcrm' ] ) || + !array_key_exists( $person_uuid, $cart[ 'd' ][ 'fulcrm' ][ 'person' ] ) ) { + $cart[ 'd' ][ 'fulcrm' ][ 'person' ][ $person_uuid ] = array(); + $patch = TRUE; } - - $shoppingitem_name = ( $form_state[ 'fulcrm_shoppingcart' ][ 'eventbookingtype' ][ 'event' ][ 'name' ] . "\r\n" . - $form_state[ 'fulcrm_shoppingcart' ][ 'eventbookingtype' ][ 'name' ] . ', ' . $person_name . "\r\n" . - $form_state[ 'fulcrm_shoppingcart' ][ 'product' ][ 'name' ] ); - - $eventslotbookings = array(); - if ( array_key_exists( 'eventbookingtypeslots', $form_state[ 'fulcrm_shoppingcart' ][ 'eventbookingtype' ] ) ) { - foreach ( $form_state[ 'fulcrm_shoppingcart' ][ 'eventbookingtype' ][ 'eventbookingtypeslots' ] as $eventbookingtypeslots ) { - $eventpart_id = fulcrm_apiv2_url_to_pk( $eventbookingtypeslots[ 'eventslot' ][ 'eventpart' ] ); - $eventslot_id = fulcrm_apiv2_url_to_pk( $eventbookingtypeslots[ 'eventslot' ][ 'url' ] ); - - if ( array_key_exists( $eventpart_id, $eventslotbookings ) ) - drupal_set_message( 'XXX cannot yet cope with multiple choice event slot bookings; defaulting to first.', 'warning' ); - - $eventslotbookings[ $eventpart_id ] = $eventslot_id; - } - } else { - drupal_set_message( 'XXX no eventbookingtypeslots to choose from?', 'warning' ); + if ( !array_key_exists( 'transaction', $cart[ 'd' ][ 'fulcrm' ] ) || + !array_key_exists( 'person', $cart[ 'd' ][ 'fulcrm' ][ 'transaction' ] ) ) { + $cart[ 'd' ][ 'fulcrm' ][ 'transaction' ][ 'person' ] = $person_uuid; + $patch = TRUE; } - - if ( $person_uuid ) { - $item = array( 'shoppingcart' => $cart[ 'url' ], - 'product' => fulcrm_apiv2_make_url( 'product', $product_id ), - 'quantity' => 1.0, - 'd' => array( 'fulcrm' => array( 'eventbooking' => array( 'eventbooking' => array(), - 'eventslotbookings' => $eventslotbookings, - 'person' => $person_uuid, - ), - ), - 'name' => $shoppingitem_name, - ), - ); - - $api_data = fulcrm_apiv2_POST( 'shoppingitem/', $item, $query = array( 'expand' => 'd' ) ); + if ( $patch ) { + $api_data = fulcrm_apiv2_PATCH( $cart[ 'url' ], array( 'd' => $cart[ 'd' ] ), + $query = array( 'expand' => 'd' ) ); fulcrm_shoppingcart_invalidate_cache(); - if ( $api_data[ 'success' ] ) { - $hook_results = module_invoke_all( 'fulcrm_shoppingcart_post_add_to_cart', $cart, $person_data[ 'data' ], $api_data[ 'data' ] ); - foreach ( $hook_results as $hook_result ) - return $hook_result; + } + } - drupal_set_message( t('Added "%item" to shopping cart.', array( '%item' => $item[ 'd' ][ 'name' ] ) ), 'status' ); - $form_state[ 'redirect' ] = 'fulcrm/cart'; + if ( array_key_exists( 'ticket_for_persons', $form_state[ 'values' ] ) ) { + foreach ( $form_state[ 'values' ][ 'ticket_for_persons' ] as $ticket_for_person ) { + $person_name = 'unnamed person'; + if ( $ticket_for_person === 'myself' ) { + $person_uuid = $form_state[ 'fulcrm_shoppingcart' ][ 'person' ][ 'uuid' ]; + $person_name = $form_state[ 'fulcrm_shoppingcart' ][ 'person' ][ 'name' ]; } else { - drupal_set_message( t('There was a problem adding your item to the shopping cart.'), 'error' ); + $person_uuid = fulcrm_crypto_get_object_id_form_value( 'person.uuid', $ticket_for_person ); + if ( $person_uuid ) + $person_name = $form_state[ 'fulcrm_shoppingcart' ][ 'persons' ][ $person_uuid ][ 'name' ]; } + + _fulcrm_shoppingcart_buy_eventbookingtype_create_shoppingitem( $form_state, $person_uuid, $person_name ); } + } else { + _fulcrm_shoppingcart_buy_eventbookingtype_create_shoppingitem( $form_state, + $form_state[ 'fulcrm_shoppingcart' ][ 'person' ][ 'uuid' ], + $form_state[ 'fulcrm_shoppingcart' ][ 'person' ][ 'name' ] ); } + } else { drupal_set_message( t('There was a problem adding your item to the shopping cart.'), 'error' ); } @@ -401,15 +432,15 @@ function fulcrm_shoppingcart_buy_eventbookingtype_form_submit( $form, &$form_sta $form_state[ 'fulcrm_shoppingcart' ][ 'shoppingcart' ] = $cart; } -function fulcrm_shoppingcart_buy_eventbookingtype_guest_add_person_form_submit( $form, &$form_state ) { - die( print_r( $form_state[ 'values' ], 1 ) ); +function fulcrm_shoppingcart_buy_eventbookingtype_person_add_guest_form_submit( $form, &$form_state ) { + fulcrm_shoppingcart_person_add_guest_form_submit( $form, $form_state ); + if ( array_key_exists( 'person', $form_state[ 'fulcrm_shoppingcart' ] ) ) { + fulcrm_shoppingcart_buy_eventbookingtype_form_submit( $form, $form_state ); + // XXXXXX + } } -function fulcrm_shoppingcart_buy_eventbookingtype_form( $form, &$form_state, $person, $eventbookingtype, $product ) { - $form = array(); - - $event_id = $eventbookingtype[ 'event' ][ 'id' ]; - +function fulcrm_shoppingcart_event_has_capacity( $event_id ) { $capacity = null; $count = null; $api_data = fulcrm_apiv2_GET( 'event/' . $event_id . '/' ); @@ -424,11 +455,14 @@ function fulcrm_shoppingcart_buy_eventbookingtype_form( $form, &$form_state, $pe $count = $api_data[ 'data' ][ 'count' ]; if ( !is_null( $capacity ) && !is_null( $count ) ) { - if ( $count >= $capacity ) { - drupal_set_message( t('Sorry, this event must be sold out. There are no more tickets available.'), 'error' ); - return $form; - } + if ( $count >= $capacity ) + return FALSE; } + return TRUE; +} + +function fulcrm_shoppingcart_buy_eventbookingtype_form( $form, &$form_state, $person, $eventbookingtype, $product ) { + $form = array(); $form_state[ 'fulcrm_shoppingcart' ][ 'person' ] = $person; $form_state[ 'fulcrm_shoppingcart' ][ 'eventbookingtype' ] = $eventbookingtype; @@ -446,7 +480,7 @@ function fulcrm_shoppingcart_buy_eventbookingtype_form( $form, &$form_state, $pe drupal_set_title( check_plain( $eventbookingtype[ 'event' ][ 'name' ] ) . ', ' . check_plain( $eventbookingtype[ 'name' ] ) . ', ' . - check_plain( $product_data[ 'data' ][ 'name' ] ) ); + check_plain( $product[ 'name' ] ) ); // $form[ 'person' ] = fulcrm_collection_to_form( 'person', $eventbookingtype[ 'person_ddatacollection' ], $person[ 'd' ] ); @@ -486,7 +520,7 @@ function fulcrm_shoppingcart_buy_eventbookingtype_form( $form, &$form_state, $pe $form[ 'new_person' ] = fulcrm_collection_to_form( 'person', $collection_data[ 'data' ], array() ); } } - $form[ '#submit' ] = 'fulcrm_shoppingcart_buy_eventbookingtype_guest_add_person_form_submit'; + $form[ '#submit' ] = 'fulcrm_shoppingcart_buy_eventbookingtype_person_add_guest_form_submit'; } $form[ 'actions' ] = array( '#type' => 'actions' ); @@ -498,13 +532,9 @@ function fulcrm_shoppingcart_buy_eventbookingtype_form( $form, &$form_state, $pe return $form; } -function fulcrm_shoppingcart_buy_eventbookingtype_guest_form_submit( $form, &$form_state ) { - die( print_r( $form_state, 1 ) ); -} - function fulcrm_shoppingcart_buy_eventbookingtype_guest_form( $form, &$form_state, $person, $eventbookingtype, $product ) { $form = fulcrm_shoppingcart_buy_eventbookingtype_form( $form, $form_state, $person, $eventbookingtype, $product ); - $form[ 'actions' ][ 'submit' ][ '#submit' ] = array( 'fulcrm_shoppingcart_buy_eventbookingtype_guest_form_submit' ); + $form[ 'actions' ][ 'submit' ][ '#submit' ] = array( 'fulcrm_shoppingcart_buy_eventbookingtype_person_add_guest_form_submit' ); return $form; } @@ -520,6 +550,12 @@ function fulcrm_shoppingcart_buy_eventbookingtype_login_form( $form, &$form_stat } function fulcrm_shoppingcart_buy_eventbookingtype( $person, $eventbookingtype, $product ) { + $event_id = $eventbookingtype[ 'event' ][ 'id' ]; + if ( !fulcrm_shoppingcart_event_has_capacity( $event_id ) ) { + drupal_set_message( t('Sorry, this event must be sold out. There are no more tickets available.'), 'error' ); + return ''; + } + $person_id = NULL; if ( array_key_exists( 'id', $person ) ) $person_id = $person[ 'id' ]; @@ -557,11 +593,9 @@ function fulcrm_shoppingcart_buy_eventbookingtype( $person, $eventbookingtype, $ function fulcrm_shoppingcart_buy_generic_product_form_submit( $form, &$form_state ) { $product_id = $form_state[ 'fulcrm_shoppingcart' ][ 'product' ][ 'id' ]; - $person_id = NULL; $person_uuid = NULL; - if ( array_key_exists( 'id', $form_state[ 'fulcrm_shoppingcart' ] ) ) - $person_id = $form_state[ 'fulcrm_shoppingcart' ][ 'person' ][ 'id' ]; - if ( array_key_exists( 'uuid', $form_state[ 'fulcrm_shoppingcart' ] ) ) + if ( array_key_exists( 'person', $form_state[ 'fulcrm_shoppingcart' ] ) && + array_key_exists( 'uuid', $form_state[ 'fulcrm_shoppingcart' ][ 'person' ] ) ) $person_uuid = $form_state[ 'fulcrm_shoppingcart' ][ 'person' ][ 'uuid' ]; if ( array_key_exists( 'shoppingcart', $form_state[ 'fulcrm_shoppingcart' ] ) ) @@ -571,15 +605,23 @@ function fulcrm_shoppingcart_buy_generic_product_form_submit( $form, &$form_stat if ( is_array( $cart ) ) { if ( $person_uuid ) { - $api_data = fulcrm_apiv2_PATCH( $cart[ 'url' ], - array( 'd' => array( 'fulcrm' => array( 'transaction' => array( 'person' => $person_uuid, - ), - 'person' => array( $person_uuid => array() ), - ), - ), - ), - $query = array( 'expand' => 'd' ) ); - fulcrm_shoppingcart_invalidate_cache(); + $patch = FALSE; + if ( !array_key_exists( 'fulcrm', $cart[ 'd' ] ) || + !array_key_exists( 'person', $cart[ 'd' ][ 'fulcrm' ] ) || + !array_key_exists( $person_uuid, $cart[ 'd' ][ 'fulcrm' ][ 'person' ] ) ) { + $cart[ 'd' ][ 'fulcrm' ][ 'person' ][ $person_uuid ] = array(); + $patch = TRUE; + } + if ( !array_key_exists( 'transaction', $cart[ 'd' ][ 'fulcrm' ] ) || + !array_key_exists( 'person', $cart[ 'd' ][ 'fulcrm' ][ 'transaction' ] ) ) { + $cart[ 'd' ][ 'fulcrm' ][ 'transaction' ][ 'person' ] = $person_uuid; + $patch = TRUE; + } + if ( $patch ) { + $api_data = fulcrm_apiv2_PATCH( $cart[ 'url' ], array( 'd' => $cart[ 'd' ] ), + $query = array( 'expand' => 'd' ) ); + fulcrm_shoppingcart_invalidate_cache(); + } } $shoppingitem_name = ( $form_state[ 'fulcrm_shoppingcart' ][ 'product' ][ 'name' ] ); diff --git a/fulcrm_shoppingcart_buy_eventbookingtype.tpl.php b/fulcrm_shoppingcart_buy_eventbookingtype.tpl.php index 3d61058..ce0ef83 100644 --- a/fulcrm_shoppingcart_buy_eventbookingtype.tpl.php +++ b/fulcrm_shoppingcart_buy_eventbookingtype.tpl.php @@ -1,3 +1,5 @@ +

+