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.
fulcrm_shoppingcart/fulcrm_shoppingcart.module

284 lines
17 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 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' ] = 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'),
);
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',
),
);
}
function fulcrm_shoppingcart_get_session_cart( $create = true ) {
global $user;
drupal_session_start();
if ( array_key_exists( 'fulcrm_shoppingcart_id', $_SESSION ) ) {
$api_data = fulcrm_apiv2_GET( 'shoppingcart/' . $_SESSION[ 'fulcrm_shoppingcart_id' ] . '/',
$query = array( 'expand' => implode( ',', array( 'shoppingitems',
'shoppingitems.content_object',
'shoppingitems.product' ) ) ) );
if ( $api_data[ 'success' ] ) {
if ( ( $api_data[ 'data' ][ 'completed' ] === null ) && ( $api_data[ 'data' ][ 'abandoned' ] === null ) ) {
return $api_data[ 'data' ];
}
}
}
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 );
if ( $api_data[ 'success' ] ) {
$_SESSION[ 'fulcrm_shoppingcart_id' ] = $api_data[ 'data' ][ 'id' ];
return $api_data[ 'data' ];
}
}
}
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_buy_eventbookingtype_form_submit( $form, &$form_state ) {
$product_id = fulcrm_crypto_get_object_id_form_value( 'product', $form_state[ 'values' ][ 'product_id_hmac' ] );
$eventbookingtype_id = fulcrm_crypto_get_object_id_form_value( 'eventbookingtype', $form_state[ 'values' ][ 'eventbookingtype_id_hmac' ] );
$person_id = NULL;
$person_uuid = NULL;
if ( array_key_exists( 'person_id_hmac', $form_state[ 'values' ] ) )
$person_id = fulcrm_crypto_get_object_id_form_value( 'person', $form_state[ 'values' ][ 'person_id_hmac' ] );
if ( array_key_exists( 'person_uuid_hmac', $form_state[ 'values' ] ) )
$person_uuid = fulcrm_crypto_get_object_id_form_value( 'person.uuid', $form_state[ 'values' ][ 'person_uuid_hmac' ] );
$cart = fulcrm_shoppingcart_get_session_cart();
if ( is_array( $cart ) ) {
if ( $person_uuid ) {
$api_data = fulcrm_apiv2_PATCH( $cart[ 'url' ], array( 'd' => array( 'fulcrm' => array( 'transaction' => array( 'person' => $person_uuid,
),
),
),
) );
}
// $cart[ 'd' ][ 'fulcrm' ][ 'person' ] = array( $uuid => array() );
// $cart[ 'd' ][ 'fulcrm' ][ 'transaction' ][ '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( 'foo' => 'bar',
),
// 'eventslotbookings' => array( $part => $slot )
// 'person' => $uuid,
),
),
),
);
$api_data = fulcrm_apiv2_POST( 'shoppingitem/', $item );
if ( $api_data[ 'success' ] ) {
drupal_set_message( t('Added to shopping cart.'), '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_eventbookingtype_form( $form, &$form_state, $person, $eventbookingtype, $product ) {
$form = array();
$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>',
);
$form[ 'product_id_hmac' ] = array( '#type' => 'hidden',
'#value' => fulcrm_crypto_object_id_form_value( 'product', $product[ 'id' ] ) );
$form[ 'eventbookingtype_id_hmac' ] = array( '#type' => 'hidden',
'#value' => fulcrm_crypto_object_id_form_value( 'eventbookingtype', $eventbookingtype[ 'id' ] ) );
if ( array_key_exists( 'id', $person ) ) {
$form[ 'person_id_hmac' ] = array( '#type' => 'hidden',
'#value' => fulcrm_crypto_object_id_form_value( 'person', $person[ 'id' ] ) );
$form[ 'person_uuid_hmac' ] = array( '#type' => 'hidden',
'#value' => fulcrm_crypto_object_id_form_value( 'person.uuid', $person[ 'uuid' ] ) );
}
if ( array_key_exists( 'id', $person ) )
$persons = array( fulcrm_crypto_object_id_hmac( 'person', $person[ 'id' ] ) => 'myself' );
else
$persons = array( 'myself' => 'myself' );
if ( array_key_exists( 'related_from_person2persons', $person ) ) {
foreach ( $person[ 'related_from_person2persons' ] as $p2p ) {
$persons[ fulcrm_crypto_object_id_hmac( 'person', $p2p[ 'related_from' ][ 'id' ] ) ] = $p2p[ 'related_from' ][ 'name' ] . ' (your ' . $p2p[ 'relationship' ][ 'name' ] . ')';
}
}
if ( array_key_exists( 'related_to_person2persons', $person ) ) {
foreach ( $person[ 'related_to_person2persons' ] as $p2p ) {
$persons[ fulcrm_crypto_object_id_hmac( 'person', $p2p[ 'related_to' ][ 'id' ] ) ] = $p2p[ 'related_to' ][ 'name' ] . ' (your ' . $p2p[ 'relationship' ][ 'name' ] . ')';
}
}
$form[ 'ticket_for_persons' ] = array( '#type' => 'checkboxes',
'#title' => 'Who is coming to the event?',
'#description' => 'Tick the box next to each person for whom you want to buy a ticket.',
'#options' => $persons,
);
$form[ 'tickets_for_others' ] = array( '#type' => 'select',
'#title' => 'How many tickets would you like for other people?',
'#description' => 'Choose how many tickets you are getting for other people. You\'ll be asked their names soon.',
'#options' => array( 0 => 'just the named people above',
1 => '1 more',
2 => '2 more',
3 => '3 more',
4 => '4 more',
),
);
$form[ 'actions' ] = array( '#type' => 'actions' );
$form[ 'actions' ][ 'save' ] = array( '#type' => 'submit',
'#value' => t('Add to Basket'),
'#submit' => array( 'fulcrm_shoppingcart_buy_eventbookingtype_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 ( 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',
) ) ) );
} else {
$person_data = array( 'data' => array() );
}
if ( $product_data[ 'success' ] ) {
switch ( fulcrm_apiv2_url_to_type( $product_data[ 'data' ][ 'content_object' ][ 'url' ] ) ) {
case 'eventbookingtype':
$eventbookingtype_data = fulcrm_apiv2_GET( $product_data[ 'data' ][ 'content_object' ][ 'url' ],
$query = array( 'expand' => implode( ',', array( 'event' ) ) ) );
if ( $eventbookingtype_data[ 'success' ] ) {
return drupal_get_form( 'fulcrm_shoppingcart_buy_eventbookingtype_form', $person_data[ 'data' ], $eventbookingtype_data[ 'data' ], $product_data[ 'data' ] );
}
break;
default:
return 'cannot handle these products yet';
}
return 'ok!';
} else {
return 'error adding item to shopping basket';
}
} else {
drupal_not_found();
}
}
function fulcrm_shoppingcart_cart() {
$cart = fulcrm_shoppingcart_get_session_cart( false );
return theme( 'fulcrm_shoppingcart_cart', array( 'cart' => $cart ) );
}
function fulcrm_shoppingcart_checkout() {
$cart = fulcrm_shoppingcart_get_session_cart( false );
return print_r( $cart, 1 );
}