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

173 lines
9.3 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' ) ) ) );
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_hmac( $product_id ) {
return drupal_hmac_base64( 'fulcrm:' . strval( $product_id ) . ':product_id', session_id() . drupal_get_hash_salt() );
}
function fulcrm_shoppingcart_buy_url( $product_id ) {
$hmac = _fulcrm_shoppingcart_buy_hmac( $product_id );
return url( 'fulcrm/buy/' . $product_id . '/' . $hmac );
}
function fulcrm_shoppingcart_buy_eventbookingtype_form( $form, &$form_state, $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[ 'eventbookingtype' ] = array( '#type' => 'markup',
'#markup' => '<pre>' . print_r( $eventbookingtype, 1 ) . '</pre>' );
$form[ 'product' ] = array( '#type' => 'markup',
'#markup' => '<pre>' . print_r( $product, 1 ) . '</pre>' );
return $form;
}
function fulcrm_shoppingcart_buy( $product_id, $hmac ) {
$real_hmac = _fulcrm_shoppingcart_buy_hmac( $product_id );
if ( hash_equals( $real_hmac, $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' ] ) {
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', $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 );
}