You are here

function uc_cart_links_process in Ubercart 5

Same name and namespace in other branches
  1. 6.2 uc_cart_links/uc_cart_links.pages.inc \uc_cart_links_process()
  2. 7.3 uc_cart_links/uc_cart_links.pages.inc \uc_cart_links_process()
1 string reference to 'uc_cart_links_process'
uc_cart_links_menu in uc_cart_links/uc_cart_links.module
Implementation of hook_menu().

File

uc_cart_links/uc_cart_links.module, line 157
Allows store owners to create links to add products to carts and send customers on to checkout.

Code

function uc_cart_links_process($arg1) {
  $messages = array();

  // Fail if the link is restricted.
  $data = variable_get('uc_cart_links_restrictions', '');
  if (!empty($data)) {
    $restrictions = explode("\n", variable_get('uc_cart_links_restrictions', ''));
    if (!empty($restrictions) && !in_array($arg1, $restrictions)) {
      $url = variable_get('uc_cart_links_invalid_page', '');
      if (empty($url)) {
        $url = '<front>';
      }
      unset($_REQUEST['destination']);
      drupal_goto($url);
    }
  }

  // Split apart the cart link on the -.
  $actions = explode('-', strtolower($arg1));
  foreach ($actions as $action) {
    switch (substr($action, 0, 1)) {

      // Set the ID of the cart link.
      case 'i':
        $id = substr($action, 1);
        break;

      // Add a product to the cart.
      case 'p':

        // Set the default product variables.
        $p = array(
          'nid' => 0,
          'qty' => 1,
          'data' => array(),
        );
        $msg = TRUE;

        // Parse the product action to adjust the product variables.
        $parts = explode('_', $action);
        foreach ($parts as $part) {
          switch (substr($part, 0, 1)) {

            // Set the product node ID: p23
            case 'p':
              $p['nid'] = intval(substr($part, 1));
              break;

            // Set the quantity to add to cart: q2
            case 'q':
              $p['qty'] = intval(substr($part, 1));
              break;

            // Set an attribute/option for the product: a3o6
            case 'a':
              $attribute = intval(substr($part, 1, strpos($part, 'o') - 1));
              $option = intval(substr($part, strpos($part, 'o') + 1));
              $p['attributes'][$attribute] = (string) $option;
              break;

            // Suppress the add to cart message: m0
            case 'm':
              $msg = intval(substr($part, 1)) == 1 ? TRUE : FALSE;
              break;
          }
        }

        // Add the item to the cart, suppressing the default redirect.
        if ($p['nid'] > 0 && $p['qty'] > 0) {
          $node = node_load(array(
            'nid' => $p['nid'],
          ));
          if ($node->status) {
            uc_cart_add_item($p['nid'], $p['qty'], $p['data'] + module_invoke_all('add_to_cart_data', $p), NULL, $msg, FALSE);
          }
          else {
            watchdog('uc_cart_link', t('Cart link on %url tried to add an unpublished product to the cart.', array(
              '%url' => uc_referer_uri(),
            )), WATCHDOG_ERROR);
          }
        }
        break;

      // Display a pre-configured message.
      case 'm':

        // Load the messages if they haven't been loaded yet.
        if (empty($messages)) {
          $data = explode("\n", variable_get('uc_cart_links_messages', ''));
          foreach ($data as $message) {
            $mdata = explode('|', $message);
            $messages[$mdata[0]] = $mdata[1];
          }
        }

        // Parse the message key and display it if it exists.
        $mkey = intval(substr($action, 1));
        if (!empty($messages[$mkey])) {
          drupal_set_message($messages[$mkey]);
        }
        break;
    }
  }
  if (variable_get('uc_cart_links_track', TRUE)) {
    $exists = db_result(db_query("SELECT clicks FROM {uc_cart_link_clicks} WHERE cart_link_id = '%s'", $id));
    if (intval($exists) == 0) {
      db_query("INSERT INTO {uc_cart_link_clicks} (cart_link_id, clicks, last_click) VALUES ('%s', 1, %d)", $id, time());
    }
    else {
      db_query("UPDATE {uc_cart_link_clicks} SET clicks = clicks + 1, last_click = %d WHERE cart_link_id = '%s'", time(), $id);
    }
  }
  drupal_goto();
}