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.

1465 lines
80 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_block_info() {
$blocks[ 'donate' ] = array( 'info' => t('Make a Donation'),
'cache' => DRUPAL_CACHE_GLOBAL,
);
return $blocks;
}
function fulcrm_shoppingcart_block_view( $delta = '' ) {
$block = array();
if ( $delta == 'donate' ) {
if ( variable_get( 'fulcrm_shoppingcart_donations', false ) ) {
$solicited_product_id = variable_get( 'fulcrm_shoppingcart_donation_solicited_product', 0 );
if ( $solicited_product_id > 0 ) {
$person = array();
$product_data = fulcrm_apiv2_GET( 'product/' . $solicited_product_id . '/',
$query = array( 'expand' => implode( ',', array( 'content_object',
'selector_object',
'person_ddatacollection',
'organisation_ddatacollection',
'thing_ddatacollection',
'transaction_ddatacollection',
'lineitem_ddatacollection',
) ) ) );
$block[ 'subject' ] = t('Make a Donation');
if ( $product_data[ 'success' ] ) {
$block[ 'content' ] = drupal_get_form( 'fulcrm_shoppingcart_buy_donation_form', $person, $product_data[ 'data' ] );
} else {
$block[ 'content' ] = 'There was a problem loading the product to make a donation.';
}
}
}
}
return $block;
}
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/donate' ] = array( 'page callback' => 'fulcrm_shoppingcart_donate',
'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',
),
'fulcrm_shoppingcart_buy_donation_page' => array( 'variables' => array( 'product' => null, 'fulcrm_shoppingcart_buy_donation_form' => null ), // data returned from fulcrm API
'template' => 'fulcrm_shoppingcart_buy_donation_page',
),
);
}
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_donation_create_shoppingitem( &$form_state, &$cart, $donation_for_shoppingitem ) {
if ( variable_get( 'fulcrm_shoppingcart_donations', false ) ) {
$donation_amount = floatval( $form_state[ 'values' ][ 'donation_amount' ] );
if ( $donation_amount < 0.0 )
drupal_set_message(t('A negative donation would rather defeat the point!'), 'error', FALSE);
if ( $donation_amount <= 0.0 )
return;
$cart_product_id = variable_get( 'fulcrm_shoppingcart_donation_cart_product', 0 );
if ( $cart_product_id > 0 ) {
$name = 'Donation of ' . sprintf( '%.2f', $donation_amount ) . ' for "' . $donation_for_shoppingitem[ 'd' ][ 'name' ] . '"';
$item = array( 'shoppingcart' => $cart[ 'url' ],
'product' => fulcrm_apiv2_make_url( 'product', $cart_product_id ),
'quantity' => 1.0,
'item' => 'donation',
'item_net' => $donation_amount,
'content_object' => $donation_for_shoppingitem[ 'url' ],
'd' => array( 'name' => $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' );
} else {
drupal_set_message( t('There was a problem adding your donation to the shopping cart.'), 'error' );
}
}
}
}
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' ] );
$eventslotbookings[ $eventpart_id ] = $made_choices[ $eventpart_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,
),
);
if ( $form_state[ 'fulcrm_shoppingcart' ][ 'eventbookingtype' ][ 'eventbooking_ddatacollection' ] ) {
$item[ 'd' ][ 'fulcrm' ][ 'eventbooking' ][ '_drupal_gather' ][ '_structure' ] = $form_state[ 'fulcrm_shoppingcart' ][ 'eventbookingtype' ][ 'eventbooking_ddatacollection' ][ '_structure' ];
$item[ 'd' ][ 'fulcrm' ][ 'eventbooking' ][ '_drupal_gather' ][ 's' ] = $form_state[ 'fulcrm_shoppingcart' ][ 'eventbookingtype' ][ 'eventbooking_ddatacollection' ][ 's' ];
}
$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';
_fulcrm_shoppingcart_buy_donation_create_shoppingitem( $form_state, $cart, $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('Cannot find your record\'s UUID. This order might have problems...'), '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 = $form_state[ 'fulcrm_shoppingcart' ][ 'eventbookingtype' ];
$eventbookingtype_id = $eventbookingtype[ 'id' ];
$event = $eventbookingtype[ 'event' ];
$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();
}
}
$made_choices = array();
foreach ( $event[ 'eventparts' ] as $eventpart ) {
$key = 'eventpart:' . $eventpart[ 'id' ];
if ( array_key_exists( $key, $form_state[ 'values' ] ) ) {
$choice = fulcrm_crypto_get_object_id_form_value( 'eventslot.id', $form_state[ 'values' ][ $key ] );
if ( $choice )
$made_choices[ $eventpart[ 'id' ] ] = $choice;
}
}
if ( array_key_exists( 'ticket_for_persons', $form_state[ 'values' ] ) ) {
$ticket_for_persons = $form_state[ 'values' ][ 'ticket_for_persons' ];
if ( !is_array( $ticket_for_persons ) )
$ticket_for_persons = array( $ticket_for_persons => 1 );
foreach ( $ticket_for_persons as $ticket_for_person => $add ) {
if ( $add ) {
$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' ];
}
if ( $add_person_uuid )
_fulcrm_shoppingcart_buy_eventbookingtype_create_shoppingitem( $form_state, $cart,
$add_person_uuid, $add_person_name, $product_id,
$made_choices = $made_choices );
}
}
} else {
if ( $person_uuid ) {
$person = $cart[ 'd' ][ 'fulcrm' ][ 'person' ][ $person_uuid ];
_fulcrm_shoppingcart_buy_eventbookingtype_create_shoppingitem( $form_state, $cart,
$person_uuid, $person[ 'name' ], $product_id,
$made_choices = $made_choices );
}
}
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_buy_donation_product_update_form( &$form, &$form_state, $donation_product_id ) {
if ( ( $donation_product_id > 0 ) && variable_get( 'fulcrm_shoppingcart_donations', false ) ) {
$product_data = fulcrm_apiv2_GET( 'product/' . $donation_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' ] ) {
$product = $product_data[ 'data' ] ;
$form[ 'donation' ] = array( '#type' => 'fieldset',
'#title' => 'Donation',
);
$form[ 'donation' ][ 'donation_amount' ] = array( '#type' => 'textfield',
'#title' => 'Amount (' . $product[ '_price' ][ 'currency' ] . ')',
'#default_value' => sprintf( "%.2f", $product[ '_price' ][ 'item_net' ] ),
);
}
}
}
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 {
$form[ 'ticket_for_persons' ] = array( '#options' => array() );
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';
}
if ( count( $form[ 'ticket_for_persons' ][ '#options' ] ) == 1 ) {
foreach ( $form[ 'ticket_for_persons' ][ '#options' ] as $k => $v )
$form[ 'ticket_for_persons' ][ '#default_value' ] = array( $k => $k );
}
$form[ 'eventslots' ] = array();
$eventslots = array();
$eventpart = array();
foreach ( $eventbookingtype[ 'event' ][ 'eventparts' ] as $eventpart ) {
$eventparts[ $eventpart[ 'id' ] ] = $eventpart;
foreach ( $eventpart[ 'eventslots' ] as $eventslot ) {
$eventslots[ $eventslot[ 'id' ] ] = $eventslot;
}
}
foreach ( $eventbookingtype[ 'eventbookingtypeslots' ] as $ebts ) {
$eventslot = $ebts[ 'eventslot' ];
$eventpart_id = fulcrm_apiv2_url_to_pk( $eventslot[ 'eventpart' ] );
$eventpart_key = 'eventpart:' . $eventpart_id;
if ( !array_key_exists( $eventpart_key, $form[ 'eventslots' ] ) )
$form[ 'eventslots' ][ $eventpart_key ] = array( '#type' => 'radios',
'#title' => $eventparts[ $eventpart_id ][ 'name' ],
'#options' => array(),
'#required' => TRUE,
);
$eventslot_id = fulcrm_crypto_object_id_form_value( 'eventslot.id', $eventslot[ 'id' ] );
$remaining_capacity = $eventslot[ '_remaining_capacity' ];
if ( is_null( $remaining_capacity ) || ( $remaining_capacity > 0 ) )
$form[ 'eventslots' ][ $eventpart_key ][ '#options' ][ $eventslot_id ] = $eventslot[ 'name' ];
}
$choices = false;
$zero_options = false;
foreach ( $form[ 'eventslots' ] as $eventpart_id => $eventpart_form ) {
if ( count( $eventpart_form[ '#options' ] ) == 0 ) {
$zero_options = true;
} else if ( count( $eventpart_form[ '#options' ] ) == 1 ) {
foreach ( $eventpart_form[ '#options' ] as $option_key => $option_value )
$form[ 'eventslots' ][ $eventpart_id ][ '#default_value' ] = $option_key;
} else {
$choices = true;
}
}
if ( $zero_options ) {
drupal_set_message(t('Unfortunately it looks like some of the choices for this type of ticket are completely sold out.'), 'error', FALSE);
}
if ( $choices ) {
$form[ 'ticket_for_persons' ][ '#type' ] = 'radios';
if ( count( $form[ 'ticket_for_persons' ][ '#options' ] ) == 1 ) {
foreach ( $form[ 'ticket_for_persons' ][ '#options' ] as $option_key => $option_value )
$form[ 'ticket_for_persons' ][ '#default_value' ] = $option_key;
}
}
if ( !$choices && ( variable_get( 'fulcrm_shoppingcart_event_itinerary', 'full' ) === 'elide' ) ) {
foreach ( $form[ 'eventslots' ] as $eventpart_id => $eventpart_form ) {
$form[ 'eventslots' ][ $eventpart_id ][ '#value' ] = $form[ 'eventslots' ][ $eventpart_id ][ '#default_value' ];
$form[ 'eventslots' ][ $eventpart_id ][ '#type' ] = 'hidden';
}
$form[ 'eventslots' ][ '#type' ] = 'container';
} else {
$form[ 'eventslots' ][ '#title' ] = 'Itinerary';
$form[ 'eventslots' ][ '#type' ] = 'fieldset';
}
_fulcrm_shoppingcart_buy_donation_product_update_form( $form, $form_state, variable_get( 'fulcrm_shoppingcart_donation_cart_product', 0 ) );
$form[ 'actions' ] = array( '#type' => 'actions' );
$form[ 'actions' ][ 'submit' ] = array( '#type' => 'submit',
'#value' => t('Add to Cart'),
'#submit' => array( 'fulcrm_shoppingcart_buy_eventbookingtype_form_submit' ),
);
if ( $zero_options ) {
$form[ 'actions' ][ 'submit' ][ '#disabled' ] = true;
$form[ 'actions' ][ 'submit' ][ '#value' ] = t('Sold Out');
}
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_donation_form_submit( $form, &$form_state ) {
$product_id = $form_state[ 'fulcrm_shoppingcart' ][ 'product' ][ 'id' ];
$donation_amount = 0.0;
if ( array_key_exists( 'donation_amount', $form_state[ 'values' ] ) )
$donation_amount = floatval( $form_state[ 'values' ][ 'donation_amount' ] );
if ( $donation_amount > 0.0 ) {
$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,
'item' => 'donation',
'item_net' => $donation_amount,
'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 your donation to your shopping cart.', array( '%item' => $item[ 'd' ][ 'name' ] ) ), 'status' );
$form_state[ 'redirect' ] = 'fulcrm/cart';
} else {
drupal_set_message( t('There was a problem adding your donation to your shopping cart.'), 'error' );
}
} else {
drupal_set_message( t('There was a problem adding your donation to your shopping cart.'), 'error' );
}
} else {
drupal_set_message( t('No donation added.'), 'warning' );
}
}
function fulcrm_shoppingcart_buy_donation_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>',
);
$form[ 'donation_amount' ] = array( '#type' => 'textfield',
'#title' => 'Amount (' . $product[ '_price' ][ 'currency' ] . ')',
'#default_value' => sprintf( "%.2f", $product[ '_price' ][ 'item_net' ] ),
);
drupal_set_title( check_plain( $product[ 'name' ] ) );
$form[ 'actions' ] = array( '#type' => 'actions' );
$form[ 'actions' ][ 'save' ] = array( '#type' => 'submit',
'#value' => t('Add to Cart'),
'#submit' => array( 'fulcrm_shoppingcart_buy_donation_form_submit' ),
);
return $form;
}
function fulcrm_shoppingcart_buy_donation( $person, $product ) {
$person_id = NULL;
if ( array_key_exists( 'id', $person ) )
$person_id = $person[ 'id' ];
$fulcrm_shoppingcart_buy_donation_form = drupal_get_form( 'fulcrm_shoppingcart_buy_donation_form', $person, $product );
return theme( 'fulcrm_shoppingcart_buy_donation_page', array( 'product' => $product, 'fulcrm_shoppingcart_buy_donation_form' => $fulcrm_shoppingcart_buy_donation_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' ] ) );
_fulcrm_shoppingcart_buy_donation_product_update_form( $form, $form_state, variable_get( 'fulcrm_shoppingcart_donation_cart_product', 0 ) );
$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 ) {
global $user;
$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;
if ( variable_get( 'fulcrm_shoppingcart_donations', false ) ) {
$checkout_product_id = variable_get( 'fulcrm_shoppingcart_donation_checkout_product', 0 );
if ( ( $checkout_product_id > 0 ) && ( $product_data[ 'data' ][ 'id' ] == $checkout_product_id ) )
return fulcrm_shoppingcart_buy_donation( $person_data[ 'data' ], $product_data[ 'data' ] );
$solicited_product_id = variable_get( 'fulcrm_shoppingcart_donation_solicited_product', 0 );
if ( ( $solicited_product_id > 0 ) && ( $product_data[ 'data' ][ 'id' ] == $solicited_product_id ) )
return fulcrm_shoppingcart_buy_donation( $person_data[ 'data' ], $product_data[ 'data' ] );
$cart_product_id = variable_get( 'fulcrm_shoppingcart_donation_cart_product', 0 );
if ( ( $cart_product_id > 0 ) && ( $product_data[ 'data' ][ 'id' ] == $cart_product_id ) )
return fulcrm_shoppingcart_buy_donation( $person_data[ 'data' ], $product_data[ 'data' ] );
}
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',
'event',
'event.eventparts',
'event.eventparts.eventslots' ) ) ) );
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!';
}
function fulcrm_shoppingcart_buy( $product_id, $hmac ) {
global $user;
if ( fulcrm_crypto_check_object_id_hmac( 'product', $product_id, $hmac ) ) {
return _fulcrm_shoppingcart_buy( $product_id );
} 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_donate() {
if ( variable_get( 'fulcrm_shoppingcart_donations', false ) ) {
$product_id = variable_get( 'fulcrm_shoppingcart_donation_solicited_product', null );
if ( $product_id )
return _fulcrm_shoppingcart_buy( $product_id );
}
return drupal_goto( 'fulcrm/checkout' );
}
function _fulcrm_shoppingcart_checkout_2( &$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';
}
function fulcrm_shoppingcart_pre_cart_step_form_submit( $form, &$form_state ) {
$cart = $form_state[ 'fulcrm_shoppingcart' ][ 'cart' ];
foreach ( $cart[ 'shoppingitems' ] as $index => $item ) {
if ( array_key_exists( 'eventbooking', $item[ 'd' ][ 'fulcrm' ] ) ) {
if ( array_key_exists( '_drupal_gather', $item[ 'd' ][ 'fulcrm' ][ 'eventbooking' ] ) ) {
$item_no = 'shoppingitem' . $item[ 'id' ];
$item_d = fulcrm_collection_form_values_to_d( $item_no, $form_state[ 'values' ] );
foreach ( $item_d as $k => $v ) {
$form_state[ 'fulcrm_shoppingcart' ][ 'cart' ][ 'shoppingitems' ][ $index ][ 'd' ][ 'fulcrm' ][ 'eventbooking' ][ 'eventbooking' ][ $k ] = $v;
}
$api_data = fulcrm_apiv2_PATCH( $item[ 'url' ], array( 'd' => $form_state[ 'fulcrm_shoppingcart' ][ 'cart' ][ 'shoppingitems' ][ $index ][ 'd' ] ),
$query = array( 'expand' => implode( ',', array( 'd' ) ) ) );
}
}
}
fulcrm_shoppingcart_invalidate_cache();
return _fulcrm_shoppingcart_checkout_2( $cart );
}
function fulcrm_shoppingcart_pre_cart_step_form( $form, &$form_state, $cart ) {
drupal_set_title( 'Additional Information Required' );
$form_state[ 'fulcrm_shoppingcart' ][ 'cart' ] = $cart;
// die( print_r( $cart,1 ) );
$form[ 'title_event' ] = array( '#type' => 'markup',
'#markup' => '<h1>Additional Information Required</h1>',
);
foreach ( $cart[ 'shoppingitems' ] as $index => $item ) {
if ( array_key_exists( 'eventbooking', $item[ 'd' ][ 'fulcrm' ] ) ) {
if ( array_key_exists( '_drupal_gather', $item[ 'd' ][ 'fulcrm' ][ 'eventbooking' ] ) ) {
$item_no = 'shoppingitem' . $item[ 'id' ];
$form[ $item_no ] = array( '#type' => 'fieldset',
'#title' => $item[ 'd' ][ 'name' ],
);
$form[ $item_no ][ 'd' ] = fulcrm_collection_to_form( $item_no,
$item[ 'd' ][ 'fulcrm' ][ 'eventbooking' ][ '_drupal_gather' ],
$item[ 'd' ][ 'fulcrm' ][ 'eventbooking' ][ '_drupal_gather' ] );
}
}
}
$form[ 'actions' ] = array( '#type' => 'actions' );
$form[ 'actions' ][ 'submit' ] = array( '#type' => 'submit',
'#value' => t('Submit'),
'#submit' => array( 'fulcrm_shoppingcart_pre_cart_step_form_submit' ),
);
return $form;
}
function fulcrm_shoppingcart_pre_cart_step( &$cart ) {
return drupal_get_form( 'fulcrm_shoppingcart_pre_cart_step_form', $cart );
}
function fulcrm_shoppingcart_has_pre_cart_step( &$cart ) {
foreach ( $cart[ 'shoppingitems' ] as $k => $item ) {
if ( array_key_exists( 'eventbooking', $item[ 'd' ][ 'fulcrm' ] ) ) {
if ( array_key_exists( '_drupal_gather', $item[ 'd' ][ 'fulcrm' ][ 'eventbooking' ] ) ) {
if ( array_key_exists( 'eventbooking', $item[ 'd' ][ 'fulcrm' ][ 'eventbooking' ] ) ) {
if ( empty( $item[ 'd' ][ 'fulcrm' ][ 'eventbooking' ][ 'eventbooking' ] ) ) {
return true;
}
} else {
return true;
}
}
}
}
return false;
}
function fulcrm_shoppingcart_checkout() {
$cart = fulcrm_shoppingcart_get_session_cart( false );
if ( $cart && array_key_exists( 'url', $cart ) ) {
if ( fulcrm_shoppingcart_has_pre_cart_step( $cart ) ) {
return fulcrm_shoppingcart_pre_cart_step( $cart );
}
return _fulcrm_shoppingcart_checkout_2( $cart );
}
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 ) ) {
// XXX cancel transaction if one exists?
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 );
}
}