View source
<?php
define('UC_AJAX_CART_ADD_CALLBACK', 'uc_ajax_cart/add/item');
define('UC_AJAX_CART_REMOVE_CALLBACK', 'uc_ajax_cart/remove/item');
define('UC_AJAX_CART_SHOW_CALLBACK', 'uc_ajax_cart/show');
define('UC_AJAX_CART_LINK_CALLBACK', 'uc_ajax_cart/addlink');
define('UC_AJAX_CART_UPDATE_CALLBACK', 'uc_ajax_cart/update');
define('UC_AJAX_CART_SHOW_VIEW_CALLBACK', 'uc_ajax_cart/show-cart-view');
define('UC_AJAX_CART_DEFAULT_TIMEOUT', 3000);
define('UC_AJAX_CART_DEFAULT_EFFECT', 1);
define('UC_AJAX_CART_DEFAULT_PANE_EFFECT_DURATION', 200);
function uc_ajax_cart_permission() {
return array(
'show uncached cart' => array(
'title' => t('show uncached cart'),
'description' => t('TODO Add a description for \'show uncached cart\''),
),
'use cart' => array(
'title' => t('use cart'),
'description' => t('TODO Add a description for \'use cart\''),
),
);
}
function uc_ajax_cart_uc_cart_item($op, $product) {
switch ($op) {
case 'remove':
if ((!isset($_SESSION['do_complete']) || !$_SESSION['do_complete']) && !(isset($_REQUEST['q']) && $_REQUEST['q'] == 'cart' && $_REQUEST['op'] != 'Update cart')) {
$message = variable_get('ajax_cart_message_remove', '!product successfully removed from cart.');
drupal_set_message(t($message, array(
'!product' => $product->title,
)));
}
break;
}
}
function uc_ajax_cart_menu() {
$items['admin/store/settings/uc_ajax_cart'] = array(
'title' => 'UC Ajax cart settings',
'description' => 'Configure the uc ajax cart settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'uc_ajax_cart_settings_overview',
),
'access arguments' => array(
'administer store',
),
'file' => 'uc_ajax_cart.admin.inc',
);
$items[UC_AJAX_CART_ADD_CALLBACK] = array(
'page callback' => 'uc_ajax_cart_callback',
'page arguments' => array(
'add',
),
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
);
$items[UC_AJAX_CART_REMOVE_CALLBACK] = array(
'page callback' => 'uc_ajax_cart_callback',
'page arguments' => array(
'remove',
),
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
);
$items[UC_AJAX_CART_SHOW_CALLBACK] = array(
'page callback' => 'uc_ajax_cart_callback',
'page arguments' => array(
'show',
),
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
);
$items[UC_AJAX_CART_LINK_CALLBACK] = array(
'page callback' => 'uc_ajax_cart_callback',
'page arguments' => array(
'link',
),
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
'file' => 'uc_ajax_cart.pages.inc',
);
$items[UC_AJAX_CART_UPDATE_CALLBACK] = array(
'page callback' => 'uc_ajax_cart_callback',
'page arguments' => array(
'update',
),
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
);
$items[UC_AJAX_CART_SHOW_VIEW_CALLBACK] = array(
'page callback' => 'uc_ajax_cart_callback',
'page arguments' => array(
'show-cart-view',
),
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
);
return $items;
}
function uc_ajax_cart_callback($op, $nid = NULL, $data = NULL) {
$cacheEnabled = variable_get('cache', 0);
if ($cacheEnabled > 0 && !$user->uid) {
$page = request_uri();
global $base_root;
cache_clear_all($base_root . 'uc_ajax_cart/show', 'cache_page');
cache_clear_all($base_root . 'uc_ajax_cart/remove', 'cache_page');
cache_clear_all($base_root . 'uc_ajax_cart/show-cart-view', 'cache_page');
cache_clear_all($base_root . 'uc_ajax_cart/update', 'cache_page');
cache_clear_all($base_root . 'uc_ajax_cart/link', 'cache_page');
}
switch ($op) {
case 'remove':
uc_ajax_cart_remove_item($_GET['nid'], unserialize(base64_decode($_GET['data'])));
print theme('status_messages');
break;
case 'add':
uc_ajax_cart_add_item($_POST);
print theme('status_messages');
break;
case 'show':
uc_ajax_cart_show_cart();
break;
case 'show-cart-view':
include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'uc_cart') . '/uc_cart.pages.inc';
$outputs = uc_cart_view();
$output = drupal_render($outputs);
print $output;
break;
case 'update':
$formValues = array(
'values' => $_POST,
);
$cart = new UcAjaxCart();
$items = $cart
->getCartContents();
drupal_form_submit('uc_cart_view_form', $formValues, $items);
print theme('status_messages');
break;
case 'link':
if (module_exists('uc_cart_links')) {
$link = array_pop(explode('/', $_GET['href']));
uc_ajax_cart_links_process($link);
}
else {
drupal_set_message(t('Sorry link is not valid'), 'error');
}
print theme('status_messages');
break;
}
}
function uc_ajax_cart_show_cart() {
$cartContent = uc_cart_get_contents();
if (count($cartContent) > 0) {
print theme('uc_ajax_cart_block_content', array(
'items' => $cartContent,
));
}
else {
print theme('uc_ajax_cart_block_content_empty', array(
'items' => $cartContent,
));
}
}
function uc_ajax_cart_remove_item($nid, $data) {
$cart = new UcAjaxCart();
try {
$cart
->removeItemFromCart($nid, $data);
} catch (NoProductException $ex) {
drupal_set_message(t('No valid product'));
} catch (CartException $ex) {
foreach ($ex
->getMessages() as $message) {
drupal_set_message($message, 'warning');
}
}
}
function uc_ajax_cart_add_item($data) {
if (!isset($data['form_id'])) {
return;
}
if (!isset($data['qty'])) {
$data['qty'] = 1;
}
$formID = explode('_', $data['form_id']);
$nodeID = array_pop($formID);
$formID = implode('_', $formID);
$product = node_load($nodeID);
$formValues = array(
'values' => $data,
);
drupal_form_submit($formID, $formValues, $product);
return;
}
function uc_ajax_cart_theme() {
return array(
'uc_ajax_cart_block' => array(
'variables' => array(
'title' => NULL,
'collapsible' => FALSE,
),
'file' => 'uc_ajax_cart.theme.inc',
),
'uc_ajax_cart_block_header' => array(
'variables' => array(
'text' => NULL,
'hasItems' => NULL,
),
'file' => 'uc_ajax_cart.theme.inc',
),
'uc_ajax_cart_block_content' => array(
'variables' => array(
'items' => NULL,
),
'template' => 'templates/uc_ajax_cart_block_content',
'file' => 'uc_ajax_cart.theme.inc',
),
'uc_ajax_cart_block_content_empty' => array(
'variables' => array(
'items' => NULL,
),
'template' => 'templates/uc_ajax_cart_block_content_empty',
),
'uc_ajax_cart_cart_links' => array(
'file' => 'uc_ajax_cart.theme.inc',
),
'uc_ajax_cart_messages' => array(
'variables' => array(
'messages' => NULL,
),
'template' => 'templates/uc_ajax_cart_messages',
),
'uc_ajax_cart_block_content_cached' => array(
'variables' => array(
'total' => NULL,
'collapsible' => FALSE,
),
'file' => 'uc_ajax_cart.theme.inc',
),
);
}
function uc_ajax_cart_preprocess_uc_ajax_cart_block_content(&$vars) {
$content = $vars['items'];
$items = array();
$context = array(
'revision' => 'themed',
'type' => 'price',
);
$total = 0;
$total_items = 0;
foreach ($content as $item) {
$display_item = module_invoke($item->module, 'uc_cart_display', $item);
if (!empty($display_item)) {
$attributes = isset($item->data['attributes']) ? $item->data['attributes'] : NULL;
$total += $display_item['#total'];
$total_items += $display_item['qty']['#default_value'];
$items[] = array(
'nid' => $display_item['nid']['#value'],
'qty' => t('@qty×', array(
'@qty' => $display_item['qty']['#default_value'],
)),
'title' => $display_item['title']['#markup'],
'descr' => isset($display_item['description']['#markup']) ? $display_item['description']['#markup'] : FALSE,
'link' => url('node/' . $item->nid),
'item' => $item,
'image' => uc_product_get_picture($display_item['nid']['#value'], 'cart'),
'node' => node_load($item->nid),
'total' => $display_item['#total'],
'remove_link' => l(t('Remove product'), UC_AJAX_CART_REMOVE_CALLBACK, array(
'attributes' => array(
'onclick' => "ajaxCartBlockUIRemove(this.href); return false;",
'class' => array(
'remove-cart-link',
'remove-cart-link-' . $display_item['nid']['#value'],
),
),
'query' => array(
'nid' => $display_item['nid']['#value'],
'destination' => $_GET['q'],
'data' => base64_encode($display_item['data']['#value']),
'action' => 'remove',
),
)),
);
}
}
$vars['total'] = $total;
$vars['items'] = $items;
$vars['item_count'] = $total_items;
$vars['cart_links'] = theme('uc_ajax_cart_cart_links');
$vars['items_text'] = format_plural($total_items, '<span class="num-items">@count</span> Item', '<span class="num-items">@count</span> Items');
}
function uc_ajax_cart_block_info() {
if (TRUE) {
$blocks['delta-0'] = array(
'info' => t('Ajax shopping cart'),
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
}
function uc_ajax_cart_block_view($delta) {
if (TRUE) {
$cartContent = uc_cart_get_contents();
$hasItems = FALSE;
if (count($cartContent) > 0) {
$hasItems = TRUE;
}
$content['subject'] = theme('uc_ajax_cart_block_header', array(
'text' => $hasItems,
));
global $user;
$cacheEnabled = variable_get('cache', 0);
if (!$user->uid && $cacheEnabled > 0) {
$loadOnPageView = variable_get('uc_ajax_cart_cart_cached_load', 0);
if ($loadOnPageView != 0) {
$content['content'] = '<div id="ajaxCartUpdate" class="load-on-view"><p>' . t('Loading cart') . '</p></div>';
}
else {
$content['content'] = '<div id="ajaxCartUpdate">' . theme('uc_ajax_cart_block_content_cached') . '</div>';
}
}
else {
$load_on_page_view_class = variable_get('uc_ajax_cart_cart_cached_load', 0) ? ' class="load-on-view"' : '';
if ($hasItems == TRUE) {
$content['content'] = '<div id="ajaxCartUpdate" ' . $load_on_page_view_class . '>' . theme('uc_ajax_cart_block_content', array(
'items' => $cartContent,
)) . '</div>';
}
else {
$content['content'] = '<div id="ajaxCartUpdate" ' . $load_on_page_view_class . '>' . theme('uc_ajax_cart_block_content_empty') . '</div>';
}
}
return $content;
}
}
function uc_ajax_cart_block_configure($delta) {
if (TRUE) {
$form['text'] = array(
'#title' => t('Ajax cart settings'),
'#type' => 'fieldset',
'#description' => t('Advanced settings for ajax cart are !here', array(
'!here' => l(t('here'), 'admin/store/settings/uc_ajax_cart'),
)),
);
return $form;
}
}
function uc_ajax_cart_init() {
static $loaded;
if (isset($loaded) && $loaded === TRUE) {
return;
}
drupal_add_library('system', 'jquery.form');
drupal_add_js(drupal_get_path('module', 'uc_ajax_cart') . '/js/jquery.blockui.js');
drupal_add_js(drupal_get_path('module', 'uc_ajax_cart') . '/js/uc_ajax_cart.js');
drupal_add_js(drupal_get_path('module', 'uc_ajax_cart') . '/js/jquery.cookie.js');
drupal_add_css(drupal_get_path('module', 'uc_ajax_cart') . '/css/uc_ajax_cart.css');
$settings = array(
'CALLBACK' => url(UC_AJAX_CART_ADD_CALLBACK),
'SHOW_CALLBACK' => url(UC_AJAX_CART_SHOW_CALLBACK),
'ADD_TITLE' => t('Please wait'),
'ADD_MESSAGE' => t('Add product to cart'),
'REMOVE_TITLE' => t('Please wait'),
'REMOVE_MESSAGE' => t('Remove product from cart'),
'CART_LINK_CALLBACK' => url(UC_AJAX_CART_LINK_CALLBACK),
'BLOCK_UI' => (int) variable_get('ajax_cart_message_effect', UC_AJAX_CART_DEFAULT_EFFECT),
'TIMEOUT' => (int) variable_get('ajax_cart_message_timeout', UC_AJAX_CART_DEFAULT_TIMEOUT),
'UPDATE_CALLBACK' => url(UC_AJAX_CART_UPDATE_CALLBACK),
'UPDATE_MESSAGE' => t('Update cart'),
'UPDATE' => variable_get('uc_ajax_cart_closed_update', 0),
'CART_OPERATION' => t('Cart'),
'CART_VIEW_ON' => variable_get('uc_ajax_cart_cart_view', 0),
'SHOW_VIEW_CALLBACK' => url(UC_AJAX_CART_SHOW_VIEW_CALLBACK),
'TRACK_CLOSED_STATE' => variable_get('uc_ajax_cart_track_closed_state', 0),
'INITIAL_CLOSED_STATE' => variable_get('uc_ajax_cart_closed_default', FALSE),
'CART_PANE_EFFECT' => variable_get('uc_ajax_cart_pane_effect', 0),
'CART_PANE_EFFECT_DURATION' => (int) variable_get('uc_ajax_cart_pane_effect_duration', UC_AJAX_CART_DEFAULT_PANE_EFFECT_DURATION),
'HIDE_CART_OPERATIONS' => variable_get('uc_ajax_cart_hide_cart_op_msgs', FALSE),
'COLLAPSIBLE_CART' => variable_get('uc_ajax_cart_closed', 0),
);
if (($include_exclude = variable_get('uc_ajax_cart_include_exclude', 0)) && ($ajaxify_class = variable_get('uc_ajax_cart_ajaxify_class', ''))) {
$settings['AJAXIFY_CLASS_EXCLUDES'] = $include_exclude - 1;
$settings['AJAXIFY_CLASS'] = $ajaxify_class;
}
drupal_add_js(array(
'uc_ajax_cart' => $settings,
), array(
'type' => 'setting',
'scope' => 'footer',
'group' => JS_DEFAULT,
));
$loaded = TRUE;
}
function uc_ajax_cart_form_uc_cart_links_settings_form_alter(&$form) {
$form['instructions']['#value'] .= '<div><strong>' . t('Ajax Cart driven cart links') . '</strong><div>' . t('For use with Ajax Cart add class <em>ajax-cart-link</em> to links.') . '</div></div>';
}
function uc_ajax_cart_alter_cart_form(&$form, $formState, $formId) {
if (preg_match('/^uc_catalog_buy_it_now_form_/', $formId)) {
$ucAjaxCart = new UcAjaxCart();
$product = node_load($form['nid']['#value']);
if ($product->type == 'product_kit') {
}
else {
if ($ucAjaxCart
->hasAttributes($product)) {
$form['actions']['submit']['#value'] = t('Please choose an option');
return;
}
}
}
if (!isset($form['#attributes']) || !is_array($form['#attributes'])) {
$form['#attributes'] = array();
}
$form['product-nid'] = array(
'#type' => 'hidden',
'#value' => $form['nid']['#value'],
);
if (!isset($form['#attributes']['class'])) {
$form['#attributes']['class'] = array();
}
$form['#attributes']['class'][] = 'ajax-cart-submit-form';
if (!isset($form['actions']['submit']['#attributes']['class'])) {
$form['actions']['submit']['#attributes']['class'] = array();
}
$form['actions']['submit']['#attributes']['class'][] = 'ajax-cart-submit-form-button';
}
function uc_ajax_cart_preprocess_block(&$vars, $hook) {
if ($hook == 'block' && $vars['block']->module == 'uc_ajax_cart' && !empty($vars['block']->title) && $vars['block']->title != '<none>') {
$vars['block']->subject = _theme_uc_ajax_cart_block_header($vars['block']->title);
}
}
function uc_ajax_cart_form_alter(&$form, $form_state, $form_id) {
if (preg_match('/^uc_product_add_to_cart_form_/', $form_id) || preg_match('/^uc_catalog_buy_it_now_form_/', $form_id) || preg_match('/^uc_product_kit_add_to_cart_form/', $form_id)) {
uc_ajax_cart_alter_cart_form($form, $form_state, $form_id);
}
elseif ('uc_cart_view_form' == $form_id) {
if (variable_get('uc_ajax_cart_ajaxify_cart_page', 1)) {
drupal_add_js(array(
'uc_ajax_cart' => array(
'AJAXIFY_CART_PAGE' => TRUE,
),
), array(
'type' => 'setting',
'scope' => 'footer',
'group' => JS_DEFAULT,
));
if (!isset($form['actions']['update']['#attributes']['class'])) {
$form['actions']['update']['#attributes']['class'] = array();
}
if (variable_get('uc_ajax_cart_hide_update_cart_bt', FALSE)) {
$form['actions']['update']['#attributes']['class'][] = 'hidden-update-bt';
}
$form['actions']['update']['#attributes']['class'][] = 'ajax-cart-submit-form-button';
}
}
}
function uc_ajax_cart_theme_registry_alter(&$theme_registry) {
if (!empty($theme_registry['uc_empty_cart'])) {
$theme_registry['uc_empty_cart']['function'] = 'uc_ajax_cart_uc_empty_cart';
}
}
function uc_ajax_cart_uc_empty_cart() {
return '<p id="cart-form-pane">' . t('There are no products in your shopping cart.') . '</p>';
}