You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1084 lines
60 KiB
Plaintext
1084 lines
60 KiB
Plaintext
<?php
|
|
|
|
function fulcrm_shoppingcart_permission() {
|
|
return array( 'administer fulcrm shoppingcart' => array( 'title' => t('Administer fulcrm shopping cart'),
|
|
'description' => t('Perform main installation/administration tasks for fulcrm shopping cart.'),
|
|
),
|
|
);
|
|
}
|
|
|
|
function hook_fulcrm_shoppingcart_alter_person_add( $person ) {
|
|
}
|
|
|
|
function fulcrm_shoppingcart_fulcrm_shoppingcart_alter_person_add( $person ) {
|
|
return $person;
|
|
}
|
|
|
|
function hook_fulcrm_shoppingcart_person_add( $person, $redirect_destination ) {
|
|
}
|
|
|
|
function hook_fulcrm_shoppingcart_person_add_guest( $person, $redirect_destination ) {
|
|
}
|
|
|
|
function hook_fulcrm_shoppingcart_pre_add_to_cart( $shoppingcart, $person, $product ) {
|
|
}
|
|
|
|
function hook_fulcrm_shoppingcart_post_add_to_cart( $shoppingcart, $shoppingitem ) {
|
|
}
|
|
|
|
function hook_fulcrm_shoppingcart_pre_remove_from_cart( $shoppingcart, $shoppingitem ) {
|
|
}
|
|
|
|
function hook_fulcrm_shoppingcart_post_remove_from_cart( $shoppingcart, $product ) {
|
|
}
|
|
|
|
function hook_fulcrm_shoppingcart_pre_cart_to_transaction( $shoppingcart ) {
|
|
}
|
|
|
|
function hook_fulcrm_shoppingcart_post_cart_to_transaction( $transaction ) {
|
|
}
|
|
|
|
function hook_fulcrm_shoppingcart_transaction_success( $transaction ) {
|
|
}
|
|
|
|
function hook_fulcrm_shoppingcart_transaction_failure( $transaction ) {
|
|
}
|
|
|
|
function fulcrm_shoppingcart_menu() {
|
|
/*
|
|
$items[ 'admin/config/services/fulcrm' ] = array( 'page callback' => 'system_admin_menu_block_page',
|
|
'file' => 'system.admin.inc',
|
|
'file path' => drupal_get_path('module', 'system'),
|
|
'title' => 'fulcrm',
|
|
'description' => 'Configuration integration with fulcrm.org.',
|
|
'position' => 'right',
|
|
'access callback' => 'user_access',
|
|
'access arguments' => array('access administration pages'),
|
|
);
|
|
*/
|
|
$items[ 'admin/config/services/fulcrm/shoppingcart' ] = array( 'page callback' => 'fulcrm_shoppingcart_admin',
|
|
'file' => 'fulcrm_shoppingcart.admin.inc',
|
|
'title' => 'fulcrm Shopping Cart',
|
|
'description' => 'Configure Shopping Cart for fulcrm.org.',
|
|
'access callback' => 'user_access',
|
|
'access arguments' => array('administer fulcrm shoppingcart'),
|
|
);
|
|
|
|
$items[ 'fulcrm/buy/%/%' ] = array( 'page callback' => 'fulcrm_shoppingcart_buy',
|
|
'page arguments' => array(2,3),
|
|
'type' => MENU_CALLBACK,
|
|
'access callback' => 'user_access',
|
|
'access arguments' => array('access content'),
|
|
);
|
|
$items[ 'fulcrm/cart/remove/%/%' ] = array( 'page callback' => 'fulcrm_shoppingcart_remove',
|
|
'page arguments' => array(3,4),
|
|
'type' => MENU_CALLBACK,
|
|
'access callback' => 'user_access',
|
|
'access arguments' => array('access content'),
|
|
);
|
|
$items[ 'fulcrm/cart' ] = array( 'page callback' => 'fulcrm_shoppingcart_cart',
|
|
'type' => MENU_CALLBACK,
|
|
'access callback' => 'user_access',
|
|
'access arguments' => array('access content'),
|
|
);
|
|
$items[ 'fulcrm/checkout' ] = array( 'page callback' => 'fulcrm_shoppingcart_checkout',
|
|
'type' => MENU_CALLBACK,
|
|
'access callback' => 'user_access',
|
|
'access arguments' => array('access content'),
|
|
);
|
|
$items[ 'fulcrm/checkout/coupon/%/%' ] = array( 'page callback' => 'fulcrm_shoppingcart_coupon',
|
|
'page arguments' => array(3,4),
|
|
'type' => MENU_CALLBACK,
|
|
'access callback' => 'user_access',
|
|
'access arguments' => array('access content'),
|
|
);
|
|
$items[ 'fulcrm/checkout/success/%/%' ] = array( 'page callback' => 'fulcrm_shoppingcart_checkout_success',
|
|
'page arguments' => array(3,4),
|
|
'type' => MENU_CALLBACK,
|
|
'access callback' => 'user_access',
|
|
'access arguments' => array('access content'),
|
|
);
|
|
$items[ 'fulcrm/checkout/failure/%/%' ] = array( 'page callback' => 'fulcrm_shoppingcart_checkout_failure',
|
|
'page arguments' => array(3,4),
|
|
'type' => MENU_CALLBACK,
|
|
'access callback' => 'user_access',
|
|
'access arguments' => array('access content'),
|
|
);
|
|
$items[ 'fulcrm/cart/person/add/%/%' ] = array( 'page callback' => 'fulcrm_shoppingcart_person_add',
|
|
'page arguments' => array(4,5),
|
|
'type' => MENU_CALLBACK,
|
|
'access callback' => 'user_access',
|
|
'access arguments' => array('access content'),
|
|
);
|
|
|
|
return $items;
|
|
}
|
|
|
|
function fulcrm_shoppingcart_theme( $existing, $type, $theme, $path ) {
|
|
return array( 'fulcrm_shoppingcart_cart' => array( 'variables' => array( 'shoppingcart' => null ), // data returned from fulcrm API
|
|
'template' => 'fulcrm_shoppingcart_cart',
|
|
),
|
|
'fulcrm_shoppingcart_cart_items' => array( 'variables' => array( 'shoppingitems' => null ), // data returned from fulcrm API
|
|
'template' => 'fulcrm_shoppingcart_cart_items',
|
|
),
|
|
'fulcrm_shoppingcart_cart_item' => array( 'variables' => array( 'shoppingitem' => null ), // data returned from fulcrm API
|
|
'template' => 'fulcrm_shoppingcart_cart_item',
|
|
),
|
|
'fulcrm_shoppingcart_transaction_checkout' => array( 'variables' => array( 'transaction' => null, 'use_coupon_url' => null ), // data returned from fulcrm API
|
|
'template' => 'fulcrm_shoppingcart_transaction_checkout',
|
|
),
|
|
'fulcrm_shoppingcart_transaction_items' => array( 'variables' => array( 'items' => null ), // data returned from fulcrm API
|
|
'template' => 'fulcrm_shoppingcart_transaction_items',
|
|
),
|
|
'fulcrm_shoppingcart_transaction_item' => array( 'variables' => array( 'item' => null ), // data returned from fulcrm API
|
|
'template' => 'fulcrm_shoppingcart_transaction_item',
|
|
),
|
|
'fulcrm_shoppingcart_transaction_payment' => array( 'variables' => array( 'item' => null ), // data returned from fulcrm API
|
|
'template' => 'fulcrm_shoppingcart_transaction_payment',
|
|
),
|
|
'fulcrm_shoppingcart_transaction_complete' => array( 'variables' => array( 'transaction' => null ), // data returned from fulcrm API
|
|
'template' => 'fulcrm_shoppingcart_transaction_complete',
|
|
),
|
|
'fulcrm_shoppingcart_transaction_incomplete' => array( 'variables' => array( 'transaction' => null ), // data returned from fulcrm API
|
|
'template' => 'fulcrm_shoppingcart_transaction_incomplete',
|
|
),
|
|
'fulcrm_shoppingcart_transaction_mailshots' => array( 'variables' => array( 'transaction' => null ), // data returned from fulcrm API
|
|
'template' => 'fulcrm_shoppingcart_transaction_mailshots',
|
|
),
|
|
'fulcrm_shoppingcart_transaction_mailshot' => array( 'variables' => array( 'mailshot' => null ), // data returned from fulcrm API
|
|
'template' => 'fulcrm_shoppingcart_transaction_mailshot',
|
|
),
|
|
'fulcrm_shoppingcart_buy_eventbookingtype' => array( 'variables' => array( 'user_login_form' => null, 'fulcrm_shoppingcart_buy_eventbookingtype_form' => null ), // data returned from fulcrm API
|
|
'template' => 'fulcrm_shoppingcart_buy_eventbookingtype',
|
|
),
|
|
);
|
|
}
|
|
|
|
function fulcrm_shoppingcart_invalidate_cache( $cart_id = NULL ) {
|
|
if ( is_null( $cart_id ) ) {
|
|
if ( array_key_exists( 'fulcrm_shoppingcart_id', $_SESSION ) )
|
|
$cart_id = $_SESSION[ 'fulcrm_shoppingcart_id' ];
|
|
}
|
|
if ( $cart_id === '*' ) {
|
|
cache_clear_all( $cid = 'fulcrm_shoppingcart:shoppingcart:', $bin = 'cache', $wildcard = TRUE );
|
|
} else if ( !is_null( $cart_id ) ) {
|
|
$cid = 'fulcrm_shoppingcart:shoppingcart:' . $cart_id;
|
|
cache_clear_all( $cid = $cid, $bin = 'cache' );
|
|
}
|
|
}
|
|
|
|
function fulcrm_shoppingcart_get_session_cart( $create = true ) {
|
|
global $user;
|
|
|
|
drupal_session_start();
|
|
|
|
if ( array_key_exists( 'fulcrm_shoppingcart_id', $_SESSION ) ) {
|
|
$cid = 'fulcrm_shoppingcart:shoppingcart:' . $_SESSION[ 'fulcrm_shoppingcart_id' ];
|
|
|
|
if ( $cached = cache_get( $cid, 'cache' ) ) {
|
|
return $cached->data;
|
|
}
|
|
|
|
$api_data = fulcrm_apiv2_GET( 'shoppingcart/' . $_SESSION[ 'fulcrm_shoppingcart_id' ] . '/',
|
|
$query = array( 'expand' => implode( ',', array( 'd',
|
|
'shoppingitems',
|
|
'shoppingitems.d',
|
|
'shoppingitems.content_object',
|
|
'shoppingitems.product' ) ) ) );
|
|
if ( $api_data[ 'success' ] ) {
|
|
if ( ( $api_data[ 'data' ][ 'completed' ] === null ) && ( $api_data[ 'data' ][ 'abandoned' ] === null ) ) {
|
|
cache_set( $cid, $api_data[ 'data' ], $bin = 'cache', $expire = time() + 300 );
|
|
return $api_data[ 'data' ];
|
|
} else {
|
|
unset( $_SESSION[ 'fulcrm_shoppingcart_id' ] );
|
|
}
|
|
} else {
|
|
if ( $api_data[ 'code' ] === 404 ) {
|
|
unset( $_SESSION[ 'fulcrm_shoppingcart_id' ] );
|
|
}
|
|
}
|
|
}
|
|
|
|
if ( $create ) {
|
|
$person_url = null;
|
|
if ( !user_is_anonymous() ) {
|
|
$person_id = fulcrm_webhook_get_pk_for_entity_type( 'user', $user->uid, 'person' );
|
|
|
|
if ( $person_id )
|
|
$person_url = fulcrm_apiv2_make_url( 'person', $person_id );
|
|
}
|
|
|
|
$cart_data = array( 'person' => $person_url, 'd' => array( 'fulcrm_testing_data' => 'this is a test' ) );
|
|
|
|
$api_data = fulcrm_apiv2_POST( 'shoppingcart/', $cart_data, $query = array( 'expand' => 'd' ) );
|
|
if ( $api_data[ 'success' ] ) {
|
|
$_SESSION[ 'fulcrm_shoppingcart_id' ] = $api_data[ 'data' ][ 'id' ];
|
|
$cid = 'fulcrm_shoppingcart:shoppingcart:' . $api_data[ 'data' ][ 'id' ];
|
|
cache_set( $cid, $api_data[ 'data' ], $bin = 'cache', $expire = time() + 300 );
|
|
return $api_data[ 'data' ];
|
|
}
|
|
}
|
|
}
|
|
|
|
function fulcrm_shoppingcart_session_cart_count_items() {
|
|
if ( $cart_data = fulcrm_shoppingcart_get_session_cart(false) )
|
|
return count( $cart_data[ 'shoppingitems' ] );
|
|
return 0;
|
|
}
|
|
|
|
function fulcrm_shoppingcart_coupon_url( $transaction_id ) {
|
|
$hmac = fulcrm_crypto_object_id_hmac( 'transaction', $transaction_id );
|
|
return url( 'fulcrm/checkout/coupon/' . $transaction_id . '/' . $hmac );
|
|
}
|
|
|
|
function fulcrm_shoppingcart_buy_url( $product_id ) {
|
|
$hmac = fulcrm_crypto_object_id_hmac( 'product', $product_id );
|
|
return url( 'fulcrm/buy/' . $product_id . '/' . $hmac );
|
|
}
|
|
|
|
function fulcrm_shoppingcart_remove_url( $shoppingitem_id ) {
|
|
$hmac = fulcrm_crypto_object_id_hmac( 'shoppingitem', $shoppingitem_id );
|
|
return url( 'fulcrm/cart/remove/' . $shoppingitem_id . '/' . $hmac );
|
|
}
|
|
|
|
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_d[ 'uuid' ] = fulcrm_uuid_uuid4();
|
|
return $person_d;
|
|
}
|
|
|
|
function fulcrm_shoppingcart_person_add_form_submit( $form, &$form_state ) {
|
|
$person_d = _fulcrm_shoppingcart_person_add_form_submit_to_data( $form, $form_state );
|
|
$person_d = module_invoke_all( 'fulcrm_shoppingcart_alter_person_add', $person_d );
|
|
|
|
$uuid = $person_d[ 'uuid' ];
|
|
unset( $person_d[ 'uuid' ] );
|
|
$person_data = array( 'd' => $person_d, 'uuid' => $uuid, 'name' => 'unnamed person' );
|
|
|
|
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' ];
|
|
}
|
|
|
|
$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('<front>') ) );
|
|
}
|
|
}
|
|
|
|
function fulcrm_shoppingcart_person_add_guest_form_submit( $form, &$form_state ) {
|
|
$person_d = _fulcrm_shoppingcart_person_add_form_submit_to_data( $form, $form_state );
|
|
$person_d = module_invoke_all( 'fulcrm_shoppingcart_alter_person_add', $person_d );
|
|
|
|
$cart = fulcrm_shoppingcart_get_session_cart();
|
|
if ( $cart[ 'url' ] ) {
|
|
$cart[ 'd' ][ 'fulcrm' ][ 'person' ][ $person_d[ 'uuid' ] ] = $person_d;
|
|
if ( !array_key_exists( 'transaction', $cart[ 'd' ][ 'fulcrm' ] ) ||
|
|
!array_key_exists( 'person', $cart[ 'd' ][ 'fulcrm' ][ 'transaction' ] ) ||
|
|
is_null( $cart[ 'd' ][ 'fulcrm' ][ 'transaction' ][ 'person' ] ) ) {
|
|
$cart[ 'd' ][ 'fulcrm' ][ 'transaction' ][ 'person' ] = $person_d[ 'uuid' ];
|
|
}
|
|
|
|
$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('<front>') ) );
|
|
|
|
$form_state[ 'fulcrm_shoppingcart' ][ 'person' ] = $person_d;
|
|
$form_state[ 'fulcrm_shoppingcart' ][ 'shoppingcart' ] = $api_data[ 'data' ];
|
|
} 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' );
|
|
}
|
|
}
|
|
|
|
function fulcrm_shoppingcart_person_add_form( $form, &$form_state, $collection, $destination ) {
|
|
$form_state[ 'fulcrm_shoppingcart' ][ 'collection' ] = $collection;
|
|
$form[ 'title_event' ] = array( '#type' => 'markup',
|
|
'#markup' => '<h1>Add a Person</h1>',
|
|
);
|
|
|
|
$form[ 'person' ] = fulcrm_collection_to_form( 'person', $collection, array() );
|
|
$form[ 'destination' ] = array( '#type' => 'hidden',
|
|
'#default_value' => $destination,
|
|
);
|
|
|
|
$form[ 'actions' ] = array( '#type' => 'actions' );
|
|
$form[ 'actions' ][ 'submit' ] = array( '#type' => 'submit',
|
|
'#value' => t('Add Person'),
|
|
'#submit' => array( 'fulcrm_shoppingcart_person_add_form_submit' ),
|
|
);
|
|
|
|
return $form;
|
|
}
|
|
|
|
function fulcrm_shoppingcart_person_add( $collection_id, $hmac ) {
|
|
if ( fulcrm_crypto_check_object_id_hmac( 'collection', $collection_id, $hmac ) ) {
|
|
|
|
$collection_data = fulcrm_apiv2_GET( 'ddatacollection/' . $collection_id . '/' );
|
|
if ( $collection_data[ 'success' ] )
|
|
return drupal_get_form( 'fulcrm_shoppingcart_person_add_form', $collection_data[ 'data' ], array_key_exists( 'destination', $_GET ) ? $_GET[ 'destination' ] : variable_get( 'fulcrm_shoppingcart_proceed_url', url('<front>') ) );
|
|
}
|
|
}
|
|
|
|
function _fulcrm_shoppingcart_buy_eventbookingtype_create_shoppingitem( &$form_state, $cart, $person_uuid, $person_name, $product_id, $made_choices = array() ) {
|
|
$all_choices = array();
|
|
|
|
$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 ( in_array( $eventpart_id, $made_choice ) ) {
|
|
$eventslotbookings[ $eventpart_id ] = $made_choice[ $eventpart_id ];
|
|
} else {
|
|
$eventslotbookings[ $eventpart_id ] = $eventslot_id;
|
|
|
|
if ( !array_key_exists( $eventpart_id, $eventslotbookings ) )
|
|
$all_choices[ $eventpart_id ] = array();
|
|
$all_choices[ $eventpart_id ][] = $eventslot_id;
|
|
}
|
|
}
|
|
} else {
|
|
drupal_set_message( 'XXX no eventbookingtypeslots to choose from?', 'warning' );
|
|
}
|
|
|
|
$additional_choices = array();
|
|
foreach ( $all_choices as $k => $v ) {
|
|
if ( count( $v ) > 1 )
|
|
$additional_choices[ $k ] = $v;
|
|
}
|
|
|
|
if ( $person_uuid && empty( $additional_choices ) ) {
|
|
$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, $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' );
|
|
}
|
|
}
|
|
return $additional_choices;
|
|
}
|
|
|
|
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_uuid = NULL;
|
|
if ( array_key_exists( 'uuid', $form_state[ 'fulcrm_shoppingcart' ][ 'person' ] ) )
|
|
$person_uuid = $form_state[ 'fulcrm_shoppingcart' ][ 'person' ][ 'uuid' ];
|
|
|
|
$cart = fulcrm_shoppingcart_get_session_cart();
|
|
|
|
if ( $cart ) {
|
|
if ( !$person_uuid ) {
|
|
if ( array_key_exists( 'd', $cart ) &&
|
|
array_key_exists( 'fulcrm', $cart[ 'd' ] ) &&
|
|
array_key_exists( 'transaction', $cart[ 'd' ][ 'fulcrm' ] ) &&
|
|
array_key_exists( 'person', $cart[ 'd' ][ 'fulcrm' ][ 'transaction' ] )
|
|
) {
|
|
$person_uuid = $cart[ 'd' ][ 'fulcrm' ][ 'transaction' ][ 'person' ];
|
|
}
|
|
}
|
|
if ( $person_uuid ) {
|
|
$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( 'uuid' => $person_uuid );
|
|
$patch = TRUE;
|
|
}
|
|
if ( !array_key_exists( 'transaction', $cart[ 'd' ][ 'fulcrm' ] ) ||
|
|
!array_key_exists( 'person', $cart[ 'd' ][ 'fulcrm' ][ 'transaction' ] ) ||
|
|
is_null( $cart[ 'd' ][ 'fulcrm' ][ 'transaction' ][ 'person' ] ) ) {
|
|
$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' ) );
|
|
if ( $api_data[ 'success' ] ) {
|
|
$cart = $api_data[ 'data' ];
|
|
} else {
|
|
drupal_set_message( t('There was a problem adding your item to the shopping cart.'), 'error' );
|
|
}
|
|
fulcrm_shoppingcart_invalidate_cache();
|
|
}
|
|
}
|
|
|
|
if ( array_key_exists( 'ticket_for_persons', $form_state[ 'values' ] ) ) {
|
|
foreach ( $form_state[ 'values' ][ 'ticket_for_persons' ] as $ticket_for_person ) {
|
|
$add_person_name = 'unnamed person';
|
|
if ( $ticket_for_person === 'myself' ) {
|
|
$add_person_uuid = $form_state[ 'fulcrm_shoppingcart' ][ 'person' ][ 'uuid' ];
|
|
$add_person_name = $form_state[ 'fulcrm_shoppingcart' ][ 'person' ][ 'name' ];
|
|
} else {
|
|
$add_person_uuid = fulcrm_crypto_get_object_id_form_value( 'person.uuid', $ticket_for_person );
|
|
if ( $add_person_uuid )
|
|
$add_person_name = $form_state[ 'fulcrm_shoppingcart' ][ 'persons' ][ $add_person_uuid ][ 'name' ];
|
|
}
|
|
|
|
$ac = _fulcrm_shoppingcart_buy_eventbookingtype_create_shoppingitem( $form_state, $cart,
|
|
$add_person_uuid, $add_person_name, $product_id );
|
|
}
|
|
} else {
|
|
$person = $cart[ 'd' ][ 'fulcrm' ][ 'person' ][ $person_uuid ];
|
|
$ac = _fulcrm_shoppingcart_buy_eventbookingtype_create_shoppingitem( $form_state, $cart,
|
|
$person_uuid, $person[ 'name' ], $product_id );
|
|
}
|
|
|
|
if ( !empty( $ac ) ) {
|
|
drupal_set_message( t('Choices to make...'), 'warning' );
|
|
}
|
|
|
|
} else {
|
|
drupal_set_message( t('There was a problem adding your item to the shopping cart.'), 'error' );
|
|
}
|
|
}
|
|
|
|
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 );
|
|
} else {
|
|
drupal_set_message( t('There was a problem adding your item to the shopping cart.'), 'error' );
|
|
}
|
|
}
|
|
|
|
function fulcrm_shoppingcart_event_has_capacity( $event_id ) {
|
|
$capacity = null;
|
|
$count = null;
|
|
$api_data = fulcrm_apiv2_GET( 'event/' . $event_id . '/' );
|
|
if ( $api_data[ 'success' ] )
|
|
$capacity = $api_data[ 'data' ][ 'capacity' ];
|
|
|
|
$api_data = fulcrm_apiv2_GET( 'eventbooking/', $query = array( 'event' => $event_id,
|
|
'confirmed__isnull' => 'False',
|
|
'cancelled__isnull' => 'True',
|
|
) );
|
|
if ( $api_data[ 'success' ] )
|
|
$count = $api_data[ 'data' ][ 'count' ];
|
|
|
|
if ( !is_null( $capacity ) && !is_null( $count ) ) {
|
|
if ( $count >= $capacity )
|
|
return FALSE;
|
|
}
|
|
return TRUE;
|
|
}
|
|
|
|
function fulcrm_shoppingcart_eventslot_has_capacity( $eventslot_id ) {
|
|
$capacity = null;
|
|
$count = null;
|
|
$api_data = fulcrm_apiv2_GET( 'eventslot/' . $event_id . '/',
|
|
$query = array( 'expand' => 'eventpart,eventpart.event' ) );
|
|
if ( $api_data[ 'success' ] ) {
|
|
$capacities = array( $api_data[ 'data' ][ 'eventpart' ][ 'event' ][ 'capacity' ],
|
|
$api_data[ 'data' ][ 'eventpart' ][ 'capacity' ],
|
|
$api_data[ 'data' ][ 'capacity' ] );
|
|
foreach ( $capacities as $cap ) {
|
|
if ( !is_null( $cap ) )
|
|
$capacity = $cap;
|
|
}
|
|
}
|
|
|
|
$api_data = fulcrm_apiv2_GET( 'eventslotbooking/', $query = array( 'eventslot' => $eventslot_id,
|
|
'eventbooking__confirmed__isnull' => 'False',
|
|
'eventbooking__cancelled__isnull' => 'True',
|
|
) );
|
|
if ( $api_data[ 'success' ] )
|
|
$count = $api_data[ 'data' ][ 'count' ];
|
|
|
|
if ( !is_null( $capacity ) && !is_null( $count ) ) {
|
|
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;
|
|
$form_state[ 'fulcrm_shoppingcart' ][ 'product' ] = $product;
|
|
|
|
$form[ 'title_event' ] = array( '#type' => 'markup',
|
|
'#markup' => '<h1>' . check_plain( $eventbookingtype[ 'event' ][ 'name' ] ) . '</h1>',
|
|
);
|
|
$form[ 'title_eventbookingtype' ] = array( '#type' => 'markup',
|
|
'#markup' => '<h2>' . check_plain( $eventbookingtype[ 'name' ] ) . '</h2>',
|
|
);
|
|
$form[ 'title_product' ] = array( '#type' => 'markup',
|
|
'#markup' => '<h3>' . check_plain( $product[ 'name' ] ) . '</h3>',
|
|
);
|
|
|
|
drupal_set_title( check_plain( $eventbookingtype[ 'event' ][ 'name' ] ) . ', ' .
|
|
check_plain( $eventbookingtype[ 'name' ] ) . ', ' .
|
|
check_plain( $product[ 'name' ] ) );
|
|
|
|
// $form[ 'person' ] = fulcrm_collection_to_form( 'person', $eventbookingtype[ 'person_ddatacollection' ], $person[ 'd' ] );
|
|
|
|
if ( array_key_exists( 'url', $person ) ) {
|
|
if ( array_key_exists( 'name', $person ) )
|
|
$persons = array( 'myself' => $person[ 'name' ] . ' (myself)' );
|
|
else
|
|
$persons = array( 'myself' => '(myself)' );
|
|
} else {
|
|
$persons = array();
|
|
}
|
|
|
|
if ( array_key_exists( 'related_from_person2persons', $person ) ) {
|
|
foreach ( $person[ 'related_from_person2persons' ] as $p2p ) {
|
|
$form_state[ 'fulcrm_shoppingcart' ][ 'persons' ][ $p2p[ 'related_from' ][ 'uuid' ] ] = $p2p[ 'related_from' ];
|
|
$persons[ fulcrm_crypto_object_id_form_value( 'person.uuid', $p2p[ 'related_from' ][ 'uuid' ] ) ] = $p2p[ 'related_from' ][ 'name' ] . ' (your ' . $p2p[ 'relationship' ][ 'name' ] . ')';
|
|
}
|
|
}
|
|
if ( array_key_exists( 'related_to_person2persons', $person ) ) {
|
|
foreach ( $person[ 'related_to_person2persons' ] as $p2p ) {
|
|
$form_state[ 'fulcrm_shoppingcart' ][ 'persons' ][ $p2p[ 'related_to' ][ 'uuid' ] ] = $p2p[ 'related_to' ];
|
|
$persons[ fulcrm_crypto_object_id_form_value( 'person.uuid', $p2p[ 'related_to' ][ 'uuid' ] ) ] = $p2p[ 'related_to' ][ 'name' ] . ' (your ' . $p2p[ 'relationship' ][ 'name' ] . ')';
|
|
}
|
|
}
|
|
|
|
if ( $persons ) {
|
|
$form[ 'ticket_for_persons' ] = array( '#type' => 'checkboxes',
|
|
'#title' => 'Who is coming to the event? (<a href="' . url( 'fulcrm/cart/person/add/' . $eventbookingtype[ 'person_ddatacollection' ][ 'id' ] . '/' . fulcrm_crypto_object_id_hmac( 'collection', $eventbookingtype[ 'person_ddatacollection' ][ 'id' ] ), $options = array( 'query' => array( 'destination' => fulcrm_shoppingcart_buy_url( $product[ 'id' ] ) ) ) ) . '">add a new person to this list</a>)',
|
|
'#description' => 'Tick the box next to each person for whom you want to buy a ticket.',
|
|
'#options' => $persons,
|
|
);
|
|
} else {
|
|
if ( $eventbookingtype[ 'person_ddatacollection' ] ) {
|
|
$collection_id = $eventbookingtype[ 'person_ddatacollection' ][ 'id' ];
|
|
$collection_data = fulcrm_apiv2_GET( 'ddatacollection/' . $collection_id . '/' );
|
|
if ( $collection_data[ 'success' ] ) {
|
|
$form[ 'new_person' ] = fulcrm_collection_to_form( 'person', $collection_data[ 'data' ], array() );
|
|
}
|
|
}
|
|
$form[ '#submit' ] = 'fulcrm_shoppingcart_buy_eventbookingtype_person_add_guest_form_submit';
|
|
}
|
|
|
|
$form[ 'actions' ] = array( '#type' => 'actions' );
|
|
$form[ 'actions' ][ 'submit' ] = array( '#type' => 'submit',
|
|
'#value' => t('Add to Cart'),
|
|
'#submit' => array( 'fulcrm_shoppingcart_buy_eventbookingtype_form_submit' ),
|
|
);
|
|
|
|
return $form;
|
|
}
|
|
|
|
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_person_add_guest_form_submit' );
|
|
return $form;
|
|
}
|
|
|
|
function fulcrm_shoppingcart_buy_eventbookingtype_login_form_submit( $form, &$form_state ) {
|
|
user_login_submit( $form, $form_state );
|
|
$form_state[ 'redirect' ] = fulcrm_shoppingcart_buy_url( $form_state[ 'fulcrm_shoppingcart' ][ 'redirect_buy_product_id' ] );
|
|
}
|
|
|
|
function fulcrm_shoppingcart_buy_eventbookingtype_login_form( $form, &$form_state, $product_id ) {
|
|
$form = user_login( $form, $form_state );
|
|
$form_state[ 'fulcrm_shoppingcart' ][ 'redirect_buy_product_id' ] = $product_id;
|
|
return $form;
|
|
}
|
|
|
|
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' ];
|
|
|
|
if ( is_null( $person_id ) ) {
|
|
switch ( variable_get( 'fulcrm_shoppingcart_experience', 'force' ) ) {
|
|
case 'guest':
|
|
case 'user':
|
|
if ( user_is_anonymous() ) {
|
|
$user_login_form = drupal_get_form( 'fulcrm_shoppingcart_buy_eventbookingtype_login_form', $product_id = $product[ 'id' ] );
|
|
|
|
$fulcrm_shoppingcart_buy_eventbookingtype_form = drupal_get_form( 'fulcrm_shoppingcart_buy_eventbookingtype_guest_form', $person, $eventbookingtype, $product );
|
|
|
|
return theme( 'fulcrm_shoppingcart_buy_eventbookingtype', array( 'fulcrm_shoppingcart_buy_eventbookingtype_form' => $fulcrm_shoppingcart_buy_eventbookingtype_form, 'user_login_form' => $user_login_form ) );
|
|
}
|
|
break;
|
|
case 'force':
|
|
if ( user_is_anonymous() ) {
|
|
drupal_set_message( t('<strong>Sorry</strong>, but you need to be logged in to continue shopping.'), 'error' ); // XXX change to be on form
|
|
$form = user_login( $form, $form_state );
|
|
$form_state[ 'fulcrm_shoppingcart' ][ 'redirect_buy_product_id' ] = $product[ 'id' ];
|
|
$form[ '#submit' ][] = 'fulcrm_shoppingcart_buy_eventbookingtype_login_form_submit';
|
|
return drupal_render( $form );
|
|
} else {
|
|
drupal_set_message( t('Sorry! There was a problem looking up your record, and this means we cannot complete this order.'), 'error' );
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
$form = drupal_get_form( 'fulcrm_shoppingcart_buy_eventbookingtype_form', $person, $eventbookingtype, $product );
|
|
return drupal_render( $form );
|
|
}
|
|
|
|
function fulcrm_shoppingcart_buy_generic_product_form_submit( $form, &$form_state ) {
|
|
$product_id = $form_state[ 'fulcrm_shoppingcart' ][ 'product' ][ 'id' ];
|
|
|
|
$person_uuid = NULL;
|
|
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' ];
|
|
|
|
$cart = fulcrm_shoppingcart_get_session_cart();
|
|
|
|
if ( is_array( $cart ) ) {
|
|
if ( $person_uuid ) {
|
|
$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( 'uuid' => $person_uuid );
|
|
$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' ] );
|
|
|
|
$item = array( 'shoppingcart' => $cart[ 'url' ],
|
|
'product' => fulcrm_apiv2_make_url( 'product', $product_id ),
|
|
'quantity' => 1.0,
|
|
'd' => array( 'name' => $shoppingitem_name,
|
|
),
|
|
);
|
|
|
|
$api_data = fulcrm_apiv2_POST( 'shoppingitem/', $item, $query = array( 'expand' => 'd' ) );
|
|
fulcrm_shoppingcart_invalidate_cache();
|
|
if ( $api_data[ 'success' ] ) {
|
|
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' );
|
|
}
|
|
} else {
|
|
drupal_set_message( t('There was a problem adding your item to the shopping cart.'), 'error' );
|
|
}
|
|
}
|
|
|
|
function fulcrm_shoppingcart_buy_generic_product_form( $form, &$form_state, $person, $product ) {
|
|
$form_state[ 'fulcrm_shoppingcart' ][ 'person' ] = $person;
|
|
$form_state[ 'fulcrm_shoppingcart' ][ 'product' ] = $product;
|
|
|
|
$form = array();
|
|
|
|
$form[ 'title_product' ] = array( '#type' => 'markup',
|
|
'#markup' => '<h3>' . check_plain( $product[ 'name' ] ) . '</h3>',
|
|
);
|
|
|
|
drupal_set_title( check_plain( $product[ 'data' ][ 'name' ] ) );
|
|
|
|
$form[ 'actions' ] = array( '#type' => 'actions' );
|
|
$form[ 'actions' ][ 'save' ] = array( '#type' => 'submit',
|
|
'#value' => t('Add to Cart'),
|
|
'#submit' => array( 'fulcrm_shoppingcart_buy_generic_product_form_submit' ),
|
|
);
|
|
|
|
return $form;
|
|
}
|
|
|
|
function fulcrm_shoppingcart_buy( $product_id, $hmac ) {
|
|
global $user;
|
|
|
|
if ( fulcrm_crypto_check_object_id_hmac( 'product', $product_id, $hmac ) ) {
|
|
$product_data = fulcrm_apiv2_GET( 'product/' . $product_id . '/',
|
|
$query = array( 'expand' => implode( ',', array( 'content_object',
|
|
'selector_object',
|
|
'person_ddatacollection',
|
|
'organisation_ddatacollection',
|
|
'thing_ddatacollection',
|
|
'transaction_ddatacollection',
|
|
'lineitem_ddatacollection',
|
|
) ) ) );
|
|
if ( !$product_data[ 'success' ] ) {
|
|
return 'error while fetching information about this product!';
|
|
}
|
|
|
|
if ( user_is_logged_in() ) {
|
|
$person_id = fulcrm_webhook_get_pk_for_entity_type( 'user', $user->uid, 'person' );
|
|
} else {
|
|
$person_id = null;
|
|
}
|
|
|
|
if ( $person_id ) {
|
|
$person_data = fulcrm_apiv2_GET( 'person/' . $person_id . '/',
|
|
$query = array( 'expand' => implode( ',', array( 'd',
|
|
'related_from_person2persons',
|
|
'related_from_person2persons.related_from',
|
|
'related_from_person2persons.related_from.d',
|
|
'related_from_person2persons.relationship',
|
|
'related_to_person2persons',
|
|
'related_to_person2persons.related_to',
|
|
'related_to_person2persons.related_to.d',
|
|
'related_to_person2persons.relationship',
|
|
) ) ) );
|
|
if ( !$person_data[ 'success' ] ) {
|
|
return 'error while fetching your details!';
|
|
}
|
|
} else {
|
|
$person_data = array( 'data' => array( 'uuid' => NULL, // initialise as NULL so first item added takes over
|
|
'name' => 'website visitor',
|
|
'd' => array( 'name' => 'website visitor',
|
|
),
|
|
) );
|
|
}
|
|
|
|
$cart = fulcrm_shoppingcart_get_session_cart( $create = true );
|
|
if ( !$cart ) {
|
|
return 'error while fetching the contents of your shopping cart!';
|
|
}
|
|
|
|
$hook_results = module_invoke_all( 'fulcrm_shoppingcart_pre_add_to_cart', $cart, $person_data[ 'data' ], $product_data[ 'data' ] );
|
|
foreach ( $hook_results as $hook_result )
|
|
return $hook_result;
|
|
|
|
switch ( $product_data[ 'data' ][ 'content_object' ][ 'url' ] ? fulcrm_apiv2_url_to_type( $product_data[ 'data' ][ 'content_object' ][ 'url' ] ) : null ) {
|
|
case 'eventbookingtype':
|
|
$eventbookingtype_data = fulcrm_apiv2_GET( $product_data[ 'data' ][ 'content_object' ][ 'url' ],
|
|
$query = array( 'expand' => implode( ',', array( 'event',
|
|
'person_ddatacollection',
|
|
'eventbooking_ddatacollection',
|
|
'eventbookingtypeslots',
|
|
'eventbookingtypeslots.eventslot' ) ) ) );
|
|
if ( !$eventbookingtype_data[ 'success' ] ) {
|
|
return 'error while fetching information about your booking!';
|
|
}
|
|
|
|
return fulcrm_shoppingcart_buy_eventbookingtype( $person_data[ 'data' ], $eventbookingtype_data[ 'data' ], $product_data[ 'data' ] );
|
|
break;
|
|
case null:
|
|
return drupal_get_form( 'fulcrm_shoppingcart_buy_generic_product_form', $person_data[ 'data' ], $product_data[ 'data' ] );
|
|
default:
|
|
return 'cannot handle these products yet';
|
|
}
|
|
return 'ok!';
|
|
} else {
|
|
drupal_not_found();
|
|
}
|
|
}
|
|
|
|
function fulcrm_shoppingcart_remove_shoppingitem_confirm_submit( $form, &$form_state ) {
|
|
$api_data = fulcrm_apiv2_DELETE( $form_state[ 'fulcrm_shoppingcart' ][ 'shoppingitem' ][ 'url' ] );
|
|
fulcrm_shoppingcart_invalidate_cache();
|
|
if ( $api_data[ 'success' ] ) {
|
|
drupal_set_message( t('Removed "%item" from cart.', array( '%item' => ( array_key_exists( 'name', $form_state[ 'fulcrm_shoppingcart' ][ 'shoppingitem' ][ 'd' ] ) ? $form_state[ 'fulcrm_shoppingcart' ][ 'shoppingitem' ][ 'd' ][ 'name' ] : $form_state[ 'fulcrm_shoppingcart' ][ 'shoppingitem' ][ 'product' ][ 'name' ] ) ) ), 'status' );
|
|
|
|
$cart = fulcrm_shoppingcart_get_session_cart();
|
|
if ( !$cart ) {
|
|
return 'error while fetching information about your shopping cart!';
|
|
}
|
|
$hook_results = module_invoke_all( 'fulcrm_shoppingcart_post_remove_from_cart', $cart, $form_state[ 'fulcrm_shoppingcart' ][ 'shoppingitem' ][ 'product' ] );
|
|
foreach ( $hook_results as $hook_result )
|
|
return $hook_result;
|
|
}
|
|
$form_state[ 'redirect' ] = 'fulcrm/cart';
|
|
}
|
|
|
|
function fulcrm_shoppingcart_remove_shoppingitem_confirm( $form, &$form_state, $shoppingitem ) {
|
|
$form_state[ 'fulcrm_shoppingcart' ][ 'shoppingitem' ] = $shoppingitem;
|
|
|
|
$cart = fulcrm_shoppingcart_get_session_cart();
|
|
if ( !$cart ) {
|
|
return 'error while fetching information about your shopping cart!';
|
|
}
|
|
|
|
$hook_results = module_invoke_all( 'fulcrm_shoppingcart_pre_remove_from_cart', $cart, $shoppingitem );
|
|
foreach ( $hook_results as $hook_result )
|
|
return $hook_result;
|
|
|
|
return confirm_form( $form,
|
|
t('Remove from cart?'),
|
|
'fulcrm/cart',
|
|
t('Are you sure you want to remove "%item" from your shopping cart?', array( '%item' => ( array_key_exists( 'name', $shoppingitem[ 'd' ] ) ? $shoppingitem[ 'd' ][ 'name' ] : $shoppingitem[ 'product' ][ 'name' ] ) ) ),
|
|
t('Remove'),
|
|
t('Cancel')
|
|
);
|
|
}
|
|
|
|
function fulcrm_shoppingcart_remove( $shoppingitem_id, $hmac ) {
|
|
if ( fulcrm_crypto_check_object_id_hmac( 'shoppingitem', $shoppingitem_id, $hmac ) ) {
|
|
$shoppingitem_data = fulcrm_apiv2_GET( 'shoppingitem/' . $shoppingitem_id . '/',
|
|
$query = array( 'expand' => implode( ',', array( 'd',
|
|
'product',
|
|
'content_object',
|
|
'shoppingcart',
|
|
) ) ) );
|
|
|
|
if ( $shoppingitem_data[ 'success' ] ) {
|
|
if ( $shoppingitem_data[ 'data' ][ 'shoppingcart' ][ 'completed' ] ) {
|
|
drupal_set_message( t('Shopping cart has been ordered. You can\'t remove things now!'), 'error' );
|
|
drupal_goto( '<front>' );
|
|
return;
|
|
}
|
|
|
|
if ( $shoppingitem_data[ 'data' ][ 'shoppingcart' ][ 'abandoned' ] ) {
|
|
drupal_set_message( t('Sorry, your shopping cart has timed out. Please restart your purchase.'), 'error' );
|
|
drupal_goto( variable_get( 'fulcrm_shoppingcart_proceed_url', '<front>' ) );
|
|
return;
|
|
}
|
|
|
|
return drupal_get_form( 'fulcrm_shoppingcart_remove_shoppingitem_confirm', $shoppingitem_data[ 'data' ] );
|
|
} else {
|
|
drupal_set_message( t('Cannot remove item: it\'s already gone?'), 'error' );;
|
|
drupal_goto( 'fulcrm/cart' );
|
|
}
|
|
}
|
|
}
|
|
|
|
function fulcrm_shoppingcart_cart() {
|
|
$cart = fulcrm_shoppingcart_get_session_cart( false );
|
|
return theme( 'fulcrm_shoppingcart_cart', array( 'cart' => $cart ) );
|
|
}
|
|
|
|
function fulcrm_shoppingcart_coupon_form_submit( $form, &$form_state ) {
|
|
$transaction_id = $form_state[ 'fulcrm_shoppingcart' ][ 'transaction_id' ];
|
|
$coupon_code = $form_state[ 'values' ][ 'coupon_code' ];
|
|
if ( $coupon_code && $transaction_id ) {
|
|
$paymentitem_data = fulcrm_apiv2_POST( 'transaction/' . $transaction_id . '/coupon/',
|
|
array(),
|
|
array( 'coupon_code' => $coupon_code,
|
|
'expand' => implode( ',', array( 'd' ) ) ) );
|
|
|
|
if ( $paymentitem_data[ 'success' ] ) {
|
|
if ( count( $paymentitem_data[ 'data' ] ) ) {
|
|
drupal_set_message( t('Coupon code applied!'), 'info' );
|
|
} else {
|
|
drupal_set_message( t('That is not a valid coupon code.'), 'warning' );
|
|
}
|
|
} else {
|
|
drupal_set_message( t('There was a problem redeeming your coupon.'), 'error' );
|
|
}
|
|
} else {
|
|
drupal_set_message( t('That is not a valid coupon code.'), 'error' );
|
|
}
|
|
|
|
drupal_goto( 'fulcrm/checkout' );
|
|
}
|
|
|
|
function fulcrm_shoppingcart_coupon_form( $form, &$form_state, $transaction_id ) {
|
|
$form_state[ 'fulcrm_shoppingcart' ][ 'transaction_id' ] = $transaction_id;
|
|
|
|
$form = array();
|
|
|
|
$form[ 'title_coupon' ] = array( '#type' => 'markup',
|
|
'#markup' => '<h1>' . t('Redeem Coupon') . '</h1>',
|
|
);
|
|
|
|
$form[ 'coupon_code' ] = array( '#type' => 'textfield',
|
|
'#title' => t('Coupon Code'),
|
|
'#description' => t('Please carefully type the coupon code here. Coupons can only be used once, and are non-refundable.'),
|
|
);
|
|
|
|
$form[ 'actions' ] = array( '#type' => 'actions' );
|
|
$form[ 'actions' ][ 'save' ] = array( '#type' => 'submit',
|
|
'#value' => t('Use Coupon'),
|
|
'#submit' => array( 'fulcrm_shoppingcart_coupon_form_submit' ),
|
|
);
|
|
|
|
return $form;
|
|
}
|
|
|
|
function fulcrm_shoppingcart_coupon( $transaction_id, $hmac ) {
|
|
if ( fulcrm_crypto_check_object_id_hmac( 'transaction', $transaction_id, $hmac ) ) {
|
|
return drupal_get_form( 'fulcrm_shoppingcart_coupon_form', $transaction_id );
|
|
} else {
|
|
drupal_goto( 'fulcrm/checkout' );
|
|
}
|
|
}
|
|
|
|
function fulcrm_shoppingcart_checkout() {
|
|
$cart = fulcrm_shoppingcart_get_session_cart( false );
|
|
if ( $cart && array_key_exists( 'url', $cart ) ) {
|
|
$hook_results = module_invoke_all( 'fulcrm_shoppingcart_pre_cart_to_transaction', $cart );
|
|
foreach ( $hook_results as $hook_result )
|
|
return $hook_result;
|
|
|
|
$transaction_data = fulcrm_apiv2_POST( $cart[ 'url' ] . 'transaction/', array(), array( 'expand' => implode( ',', array( 'd' ) ) ) );
|
|
fulcrm_shoppingcart_invalidate_cache();
|
|
if ( $transaction_data[ 'success' ] ) {
|
|
$hmac = fulcrm_crypto_object_id_hmac( 'transaction', $transaction_data[ 'data' ][ 'id' ] );
|
|
|
|
$transaction_push = array( 'd' => array( 'fulcrm_fund_transaction_pay_failure_url' => url( 'fulcrm/checkout/failure/' . $transaction_data[ 'data' ][ 'id' ]. '/' . $hmac, array( 'absolute' => TRUE ) ),
|
|
'fulcrm_fund_transaction_complete_success_url' => url( 'fulcrm/checkout/success/' . $transaction_data[ 'data' ][ 'id' ]. '/' . $hmac, array( 'absolute' => TRUE ) ),
|
|
'fulcrm_fund_transaction_complete_failure_url' => url( 'fulcrm/checkout/failure/' . $transaction_data[ 'data' ][ 'id' ]. '/' . $hmac, array( 'absolute' => TRUE ) ),
|
|
),
|
|
);
|
|
|
|
$transaction_data2 = fulcrm_apiv2_PATCH( $transaction_data[ 'data' ][ 'url' ],
|
|
$transaction_push, $query = array( 'expand' => implode( ',', array( 'd',
|
|
'lineitems',
|
|
'paymentitems',
|
|
) ) ) );
|
|
fulcrm_shoppingcart_invalidate_cache();
|
|
if ( $transaction_data2[ 'success' ] ) {
|
|
$hook_results = module_invoke_all( 'fulcrm_shoppingcart_post_cart_to_transaction', $transaction_data2[ 'data' ] );
|
|
foreach ( $hook_results as $hook_result )
|
|
return $hook_result;
|
|
return theme( 'fulcrm_shoppingcart_transaction_checkout', array( 'transaction' => $transaction_data2[ 'data' ],
|
|
'use_coupon_url' => fulcrm_shoppingcart_coupon_url( $transaction_data[ 'data' ][ 'id' ] ),
|
|
) );
|
|
}
|
|
return 'problem 3';
|
|
}
|
|
return 'problem 2';
|
|
}
|
|
return 'problem 1';
|
|
}
|
|
|
|
function fulcrm_shoppingcart_checkout_success( $transaction_id, $hmac ) {
|
|
if ( fulcrm_crypto_check_object_id_hmac( 'transaction', $transaction_id, $hmac ) ) {
|
|
fulcrm_shoppingcart_invalidate_cache();
|
|
$transaction_data = fulcrm_apiv2_GET( 'transaction/' . $transaction_id . '/',
|
|
$query = array( 'expand' => implode( ',', array( 'd',
|
|
'lineitems',
|
|
'lineitems.d',
|
|
'lineitems.content_object',
|
|
'mailshots',
|
|
'mailshots.campaign',
|
|
'paymentitems',
|
|
'paymentitems.d',
|
|
) ) ) );
|
|
if ( $transaction_data[ 'success' ] ) {
|
|
if ( $transaction_data[ 'data' ][ 'completed' ] ) {
|
|
$hook_results = module_invoke_all( 'fulcrm_shoppingcart_transaction_success', $transaction_data[ 'data' ] );
|
|
foreach ( $hook_results as $hook_result )
|
|
return $hook_result;
|
|
drupal_set_message( t('Thank you for your order.'), 'status' );
|
|
} else if ( $transaction_data[ 'data' ][ 'submitted' ] ) {
|
|
drupal_set_message( t('Payment has not yet been processed.'), 'warning' );
|
|
} else {
|
|
drupal_set_message( t('This transaction has not yet completed.'), 'warning' );
|
|
}
|
|
return theme( 'fulcrm_shoppingcart_transaction_complete', array( 'transaction' => $transaction_data[ 'data' ] ) );
|
|
} else {
|
|
drupal_set_message( t('Although your transaction appears to have completed successfully, there has been a problem fetching the full transaction data.'), 'error' );
|
|
}
|
|
}
|
|
}
|
|
|
|
function fulcrm_shoppingcart_checkout_failure( $transaction_id, $hmac ) {
|
|
if ( fulcrm_crypto_check_object_id_hmac( 'transaction', $transaction_id, $hmac ) ) {
|
|
fulcrm_shoppingcart_invalidate_cache();
|
|
$transaction_data = fulcrm_apiv2_GET( 'transaction/' . $transaction_id . '/',
|
|
$query = array( 'expand' => implode( ',', array( 'd',
|
|
'lineitems',
|
|
'lineitems.d',
|
|
'lineitems.content_object',
|
|
'mailshots',
|
|
'mailshots.campaign',
|
|
'paymentitems',
|
|
'paymentitems.d',
|
|
) ) ) );
|
|
if ( $transaction_data[ 'success' ] ) {
|
|
$hook_results = module_invoke_all( 'fulcrm_shoppingcart_transaction_failure', $transaction_data[ 'data' ] );
|
|
} else {
|
|
$hook_results = module_invoke_all( 'fulcrm_shoppingcart_transaction_failure', NULL );
|
|
}
|
|
foreach ( $hook_results as $hook_result )
|
|
return $hook_result;
|
|
return theme( 'fulcrm_shoppingcart_transaction_incomplete', array( 'transaction' => $transaction_data[ 'data' ] ) );
|
|
}
|
|
}
|
|
|
|
function fulcrm_shoppingcart_user_logout( $account ) {
|
|
if ( array_key_exists( 'fulcrm_shoppingcart_id', $_SESSION ) ) {
|
|
unset( $_SESSION[ 'fulcrm_shoppingcart_id' ] );
|
|
}
|
|
}
|
|
|
|
function fulcrm_shoppingcart_create_for_fulcrm_shoppingcart( &$vars ) {
|
|
}
|
|
|
|
function fulcrm_shoppingcart_update_for_fulcrm_shoppingcart( &$vars ) {
|
|
if ( fulcrm_apiv2_url_to_type( $data[ 'url' ] ) === 'shoppingcart' ) {
|
|
$cart_id = fulcrm_apiv2_url_to_pk( $data[ 'url' ] );
|
|
if ( $cart_id )
|
|
fulcrm_shoppingcart_invalidate_cache( $cart_id );
|
|
}
|
|
}
|
|
|
|
function fulcrm_shoppingcart_delete_for_fulcrm_shoppingcart( &$vars ) {
|
|
if ( fulcrm_apiv2_url_to_type( $data[ 'url' ] ) === 'shoppingcart' ) {
|
|
$cart_id = fulcrm_apiv2_url_to_pk( $data[ 'url' ] );
|
|
if ( $cart_id )
|
|
fulcrm_shoppingcart_invalidate_cache( $cart_id );
|
|
}
|
|
}
|
|
|
|
function fulcrm_shoppingcart_create_for_fulcrm_shoppingitem( &$vars ) {
|
|
}
|
|
|
|
function fulcrm_shoppingcart_update_for_fulcrm_shoppingitem( &$vars ) {
|
|
if ( fulcrm_apiv2_url_to_type( $data[ 'shoppingcart' ] ) === 'shoppingcart' ) {
|
|
$cart_id = fulcrm_apiv2_url_to_pk( $data[ 'shoppingcart' ] );
|
|
if ( $cart_id )
|
|
fulcrm_shoppingcart_invalidate_cache( $cart_id );
|
|
}
|
|
}
|
|
|
|
function fulcrm_shoppingcart_delete_for_fulcrm_shoppingitem( &$vars ) {
|
|
fulcrm_shoppingcart_invalidate_cache( '*' );
|
|
}
|
|
|
|
function fulcrm_shoppingcart_create_for_fulcrm_transaction( &$vars ) {
|
|
if ( fulcrm_apiv2_url_to_type( $data[ 'content_object' ] ) === 'shoppingcart' ) {
|
|
$cart_id = fulcrm_apiv2_url_to_pk( $data[ 'content_object' ] );
|
|
if ( $cart_id )
|
|
fulcrm_shoppingcart_invalidate_cache( $cart_id );
|
|
}
|
|
}
|