| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Allows store owners to create links to add products to carts and send
|
| 7 |
* customers on to checkout.
|
| 8 |
*
|
| 9 |
* Development sponsored by the Ubercart project. http://www.ubercart.org
|
| 10 |
*/
|
| 11 |
|
| 12 |
|
| 13 |
/*******************************************************************************
|
| 14 |
* Hook Functions (Drupal)
|
| 15 |
******************************************************************************/
|
| 16 |
|
| 17 |
/**
|
| 18 |
* Implementation of hook_menu().
|
| 19 |
*/
|
| 20 |
function uc_cart_links_menu($may_cache) {
|
| 21 |
if ($may_cache) {
|
| 22 |
$items[] = array(
|
| 23 |
'path' => 'admin/store/settings/cart_links',
|
| 24 |
'title' => t('Cart links settings'),
|
| 25 |
'description' => t('Configure and craft special product add to cart links.'),
|
| 26 |
'callback' => 'drupal_get_form',
|
| 27 |
'callback arguments' => array('uc_cart_links_settings_form'),
|
| 28 |
'access' => user_access('administer cart links'),
|
| 29 |
'type' => MENU_NORMAL_ITEM,
|
| 30 |
);
|
| 31 |
$items[] = array(
|
| 32 |
'path' => 'admin/store/reports/cart_links',
|
| 33 |
'title' => t('Cart links clicks'),
|
| 34 |
'description' => t('Track clicks through cart links.'),
|
| 35 |
'callback' => 'uc_cart_links_report',
|
| 36 |
'access' => user_access('view cart links report'),
|
| 37 |
'type' => MENU_NORMAL_ITEM,
|
| 38 |
);
|
| 39 |
$items[] = array(
|
| 40 |
'path' => 'admin/store/help/cart_links',
|
| 41 |
'title' => t('Creating cart links'),
|
| 42 |
'description' => t('Learn how to create cart links for your products.'),
|
| 43 |
'callback' => 'uc_cart_links_creation_help',
|
| 44 |
'access' => user_access('administer store'),
|
| 45 |
'type' => MENU_NORMAL_ITEM,
|
| 46 |
);
|
| 47 |
}
|
| 48 |
else {
|
| 49 |
$items[] = array(
|
| 50 |
'path' => 'cart/add/'. arg(2),
|
| 51 |
'title' => t('Add to cart'),
|
| 52 |
'callback' => 'uc_cart_links_process',
|
| 53 |
'callback arguments' => array(arg(2)),
|
| 54 |
'access' => user_access('access content'),
|
| 55 |
'type' => MENU_CALLBACK,
|
| 56 |
);
|
| 57 |
|
| 58 |
}
|
| 59 |
|
| 60 |
return $items;
|
| 61 |
}
|
| 62 |
|
| 63 |
/**
|
| 64 |
* Implementation of hook_perm().
|
| 65 |
*/
|
| 66 |
function uc_cart_links_perm() {
|
| 67 |
return array('administer cart links', 'view cart links report');
|
| 68 |
}
|
| 69 |
|
| 70 |
/**
|
| 71 |
* Implementation of hook_add_to_cart().
|
| 72 |
*/
|
| 73 |
function uc_cart_links_add_to_cart($nid, $qty, $data) {
|
| 74 |
if (user_access('administer cart links') &&
|
| 75 |
variable_get('uc_cart_links_add_show', TRUE)) {
|
| 76 |
$cart_link = 'p'. $nid .'_q'. $qty;
|
| 77 |
if (!empty($data['attributes'])) {
|
| 78 |
foreach ($data['attributes'] as $attribute => $option) {
|
| 79 |
$cart_link .= '_a'. $attribute .'o'. $option;
|
| 80 |
}
|
| 81 |
}
|
| 82 |
drupal_set_message(t('Cart link product action: @cart_link', array('@cart_link' => $cart_link)));
|
| 83 |
}
|
| 84 |
}
|
| 85 |
|
| 86 |
|
| 87 |
/*******************************************************************************
|
| 88 |
* Callback Functions, Forms, and Tables
|
| 89 |
******************************************************************************/
|
| 90 |
|
| 91 |
function uc_cart_links_settings_form() {
|
| 92 |
$form['instructions'] = array(
|
| 93 |
'#value' => '<div>'. t('<a href="!url">View the help page</a> to learn how to create cart links.', array('!url' => url('admin/store/help/cart_links'))) .'</div>',
|
| 94 |
);
|
| 95 |
|
| 96 |
$form['uc_cart_links_add_show'] = array(
|
| 97 |
'#type' => 'checkbox',
|
| 98 |
'#title' => t('Display the cart link product action when you add a product to your cart.'),
|
| 99 |
'#default_value' => variable_get('uc_cart_links_add_show', TRUE),
|
| 100 |
);
|
| 101 |
$form['uc_cart_links_track'] = array(
|
| 102 |
'#type' => 'checkbox',
|
| 103 |
'#title' => t('Track clicks through cart links that specify tracking IDs.'),
|
| 104 |
'#default_value' => variable_get('uc_cart_links_track', TRUE),
|
| 105 |
);
|
| 106 |
$form['uc_cart_links_messages'] = array(
|
| 107 |
'#type' => 'textarea',
|
| 108 |
'#title' => t('Cart links messages'),
|
| 109 |
'#description' => t('Enter in messages available to the cart links API for display through a link. Separate messages with a line break.<br />Messages should have a numeric key and text value. Example: 1337|Message text.'),
|
| 110 |
'#default_value' => variable_get('uc_cart_links_messages', ''),
|
| 111 |
);
|
| 112 |
$form['uc_cart_links_empty'] = array(
|
| 113 |
'#type' => 'checkbox',
|
| 114 |
'#title' => t('Allow cart links to empty customer carts.'),
|
| 115 |
'#default_value' => variable_get('uc_cart_links_empty', TRUE),
|
| 116 |
);
|
| 117 |
$form['uc_cart_links_restrictions'] = array(
|
| 118 |
'#type' => 'textarea',
|
| 119 |
'#title' => t('Cart links restrictions'),
|
| 120 |
'#description' => t('To restrict what cart links may be used on your site, enter valid cart links in this textbox. Separate links with a line break. Leave blank to permit any cart link.'),
|
| 121 |
'#default_value' => variable_get('uc_cart_links_restrictions', ''),
|
| 122 |
);
|
| 123 |
$form['uc_cart_links_invalid_page'] = array(
|
| 124 |
'#type' => 'textfield',
|
| 125 |
'#title' => t('Invalid link redirect page'),
|
| 126 |
'#description' => t('Enter the URL to redirect to when an invalid cart link is used.'),
|
| 127 |
'#default_value' => variable_get('uc_cart_links_invalid_page', ''),
|
| 128 |
'#size' => 32,
|
| 129 |
'#field_prefix' => url(NULL, NULL, NULL, TRUE) . (variable_get('clean_url', 0) ? '' : '?q='),
|
| 130 |
);
|
| 131 |
|
| 132 |
return system_settings_form($form);
|
| 133 |
}
|
| 134 |
|
| 135 |
// Displays the cart links report.
|
| 136 |
function uc_cart_links_report() {
|
| 137 |
$header = array(
|
| 138 |
array('data' => t('ID'), 'field' => 'cart_link_id'),
|
| 139 |
array('data' => t('Clicks'), 'field' => 'clicks'),
|
| 140 |
array('data' => t('Last click'), 'field' => 'last_click', 'sort' => 'desc'),
|
| 141 |
);
|
| 142 |
|
| 143 |
$rows = array();
|
| 144 |
$result = pager_query("SELECT * FROM {uc_cart_link_clicks}". tablesort_sql($header), 25, 1);
|
| 145 |
while ($data = db_fetch_object($result)) {
|
| 146 |
$rows[] = array(
|
| 147 |
check_plain($data->cart_link_id),
|
| 148 |
$data->clicks,
|
| 149 |
format_date($data->last_click, 'short'),
|
| 150 |
);
|
| 151 |
}
|
| 152 |
|
| 153 |
if (empty($rows)) {
|
| 154 |
$rows[] = array(
|
| 155 |
array('data' => t('No cart links have been tracked yet.'), 'colspan' => 3),
|
| 156 |
);
|
| 157 |
}
|
| 158 |
|
| 159 |
return theme('table', $header, $rows) . theme('pager', array(), 25, 1);
|
| 160 |
}
|
| 161 |
|
| 162 |
// Processes a cart link to fiddle with the cart and redirect the user.
|
| 163 |
function uc_cart_links_process($arg1) {
|
| 164 |
$messages = array();
|
| 165 |
|
| 166 |
// Fail if the link is restricted.
|
| 167 |
$data = variable_get('uc_cart_links_restrictions', '');
|
| 168 |
if (!empty($data)) {
|
| 169 |
$restrictions = explode("\n", variable_get('uc_cart_links_restrictions', ''));
|
| 170 |
if (!empty($restrictions) && !in_array($arg1, $restrictions)) {
|
| 171 |
$url = variable_get('uc_cart_links_invalid_page', '');
|
| 172 |
if (empty($url)) {
|
| 173 |
$url = '<front>';
|
| 174 |
}
|
| 175 |
unset($_REQUEST['destination']);
|
| 176 |
drupal_goto($url);
|
| 177 |
}
|
| 178 |
}
|
| 179 |
|
| 180 |
// Split apart the cart link on the -.
|
| 181 |
$actions = explode('-', strtolower($arg1));
|
| 182 |
|
| 183 |
foreach ($actions as $action) {
|
| 184 |
switch (substr($action, 0, 1)) {
|
| 185 |
// Set the ID of the cart link.
|
| 186 |
case 'i':
|
| 187 |
$id = substr($action, 1);
|
| 188 |
break;
|
| 189 |
|
| 190 |
// Add a product to the cart.
|
| 191 |
case 'p':
|
| 192 |
// Set the default product variables.
|
| 193 |
$p = array('nid' => 0, 'qty' => 1, 'data' => array());
|
| 194 |
$msg = TRUE;
|
| 195 |
|
| 196 |
// Parse the product action to adjust the product variables.
|
| 197 |
$parts = explode('_', $action);
|
| 198 |
foreach ($parts as $part) {
|
| 199 |
switch (substr($part, 0, 1)) {
|
| 200 |
// Set the product node ID: p23
|
| 201 |
case 'p':
|
| 202 |
$p['nid'] = intval(substr($part, 1));
|
| 203 |
break;
|
| 204 |
// Set the quantity to add to cart: q2
|
| 205 |
case 'q':
|
| 206 |
$p['qty'] = intval(substr($part, 1));
|
| 207 |
break;
|
| 208 |
// Set an attribute/option for the product: a3o6
|
| 209 |
case 'a':
|
| 210 |
$attribute = intval(substr($part, 1, strpos($part, 'o') - 1));
|
| 211 |
$option = intval(substr($part, strpos($part, 'o') + 1));
|
| 212 |
$p['attributes'][$attribute] = (string) $option;
|
| 213 |
break;
|
| 214 |
// Suppress the add to cart message: m0
|
| 215 |
case 'm':
|
| 216 |
$msg = intval(substr($part, 1)) == 1 ? TRUE : FALSE;
|
| 217 |
break;
|
| 218 |
}
|
| 219 |
}
|
| 220 |
|
| 221 |
// Add the item to the cart, suppressing the default redirect.
|
| 222 |
if ($p['nid'] > 0 && $p['qty'] > 0) {
|
| 223 |
uc_cart_add_item($p['nid'], $p['qty'], $p['data'] + module_invoke_all('add_to_cart_data', $p), NULL, $msg, FALSE);
|
| 224 |
}
|
| 225 |
break;
|
| 226 |
|
| 227 |
// Empty the shopping cart.
|
| 228 |
case 'e':
|
| 229 |
if (variable_get('uc_cart_links_empty', TRUE)) {
|
| 230 |
uc_cart_empty(uc_cart_get_id());
|
| 231 |
}
|
| 232 |
break;
|
| 233 |
|
| 234 |
// Display a pre-configured message.
|
| 235 |
case 'm':
|
| 236 |
// Load the messages if they haven't been loaded yet.
|
| 237 |
if (empty($messages)) {
|
| 238 |
$data = explode("\n", variable_get('uc_cart_links_messages', ''));
|
| 239 |
foreach ($data as $message) {
|
| 240 |
$mdata = explode('|', $message);
|
| 241 |
$messages[$mdata[0]] = $mdata[1];
|
| 242 |
}
|
| 243 |
}
|
| 244 |
|
| 245 |
// Parse the message key and display it if it exists.
|
| 246 |
$mkey = intval(substr($action, 1));
|
| 247 |
if (!empty($messages[$mkey])) {
|
| 248 |
drupal_set_message($messages[$mkey]);
|
| 249 |
}
|
| 250 |
break;
|
| 251 |
}
|
| 252 |
}
|
| 253 |
|
| 254 |
if (variable_get('uc_cart_links_track', TRUE)) {
|
| 255 |
$exists = db_result(db_query("SELECT clicks FROM {uc_cart_link_clicks} WHERE cart_link_id = '%s'", $id));
|
| 256 |
if (intval($exists) == 0) {
|
| 257 |
db_query("INSERT INTO {uc_cart_link_clicks} (cart_link_id, clicks, last_click) VALUES ('%s', 1, %d)", $id, time());
|
| 258 |
}
|
| 259 |
else {
|
| 260 |
db_query("UPDATE {uc_cart_link_clicks} SET clicks = clicks + 1, last_click = %d WHERE cart_link_id = '%s'", time(), $id);
|
| 261 |
}
|
| 262 |
}
|
| 263 |
|
| 264 |
drupal_goto();
|
| 265 |
}
|
| 266 |
|
| 267 |
function uc_cart_links_creation_help() {
|
| 268 |
$items = array(
|
| 269 |
t('The cart link should be /cart/add/cart_link_content.'),
|
| 270 |
t('Chain together as many actions as you want with dashes.'),
|
| 271 |
t('Do not put any spaces or use dashes in any action arguments.'),
|
| 272 |
t('Use the table below to learn about actions and their arguments.'),
|
| 273 |
t('Arguments come directly after their action letters.'),
|
| 274 |
t('Specify the redirection by adding ?destination=url where url is the page to go to.'),
|
| 275 |
);
|
| 276 |
$header = array(t('Action'), t('Description'), t('Argument'));
|
| 277 |
$rows = array(
|
| 278 |
array('i', t('Sets the ID of the cart link.'), t('A custom text ID for the link.')),
|
| 279 |
array('e', t("Empties the customer's cart."), t('None.')),
|
| 280 |
array('m', t('Displays a preset message to the customer.'), t('A message ID.')),
|
| 281 |
array('p', t('Adds a product to the cart.'), t('A product string using the rules below...')),
|
| 282 |
);
|
| 283 |
$items2 = array(
|
| 284 |
t("You must at least specify a node ID immediately after the 'p'."),
|
| 285 |
t('Add additional specifications separated by underscores.'),
|
| 286 |
t('Specify the quantity with q followed by the number to add.'),
|
| 287 |
t('Specify attributes/options using a#o#, replacing # with the ID of the attribute and option.'),
|
| 288 |
t('Turn off the add to cart message with m0.'),
|
| 289 |
);
|
| 290 |
|
| 291 |
$output = '<div>'. t('There is currently no user interface for creating cart links, but this section includes some basic instructions.<br />Cart links are simple to form using a few actions and arguments with the following rules:')
|
| 292 |
.'<p>'. theme('item_list', $items) .'</p><p>'. theme('table', $header, $rows) .'</p><p>'. theme('item_list', $items2) .'</p><p>'. t('<b>Example:</b> /cart/add/e-p1_q5-imonday_special?destination=cart<br /><br />This example will empty the cart, add 5 of product 1 to the cart, track clicks with the ID "monday_special", and redirect the user to the cart. To use this on your site, simply create an HTML link to the URL you create:')
|
| 293 |
.'</p><p><a href="http://www.example.com/cart/add/e-p1_q5-imonday_special?destination=cart">'. t('Link text.') .'</a></p></div>';
|
| 294 |
|
| 295 |
return $output;
|
| 296 |
}
|