You are here

uc_wishlist.module in UC Wish List 6

Same filename and directory in other branches
  1. 8 uc_wishlist.module
  2. 7 uc_wishlist.module

Allows users to create public shopping/wish lists.

Original development by Joe Turgeon [http://arithmetric.com] sponsored by Sundays Energy [http://www.sundaysenergy.com].

D6 migration and support by Commerce Guys [http://www.commerceguys.com].

File

uc_wishlist.module
View source
<?php

/**
 * @file
 * Allows users to create public shopping/wish lists.
 *
 * Original development by Joe Turgeon [http://arithmetric.com] sponsored by
 *   Sundays Energy [http://www.sundaysenergy.com].
 *
 * D6 migration and support by Commerce Guys [http://www.commerceguys.com].
 */

/*******************************************************************************
 * Hook Functions (Drupal)
 ******************************************************************************/

/**
 * Implementation of hook_menu().
 */
function uc_wishlist_menu() {
  $items = array();
  $items['admin/store/customers/wishlist'] = array(
    'title' => 'Wish lists',
    'description' => 'Manage user wish lists.',
    'page callback' => 'uc_wishlist_admin',
    'access arguments' => array(
      'administer wish lists',
    ),
    'file' => 'uc_wishlist.admin.inc',
  );
  $items['admin/store/customers/wishlist/%uc_wishlist/delete'] = array(
    'title' => 'Delete a wish list',
    'description' => 'Confirm the deletion of a wish list.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'uc_wishlist_admin_delete_form',
      4,
    ),
    'access arguments' => array(
      'administer wish lists',
    ),
    'file' => 'uc_wishlist.admin.inc',
  );
  $items['admin/store/settings/wishlist'] = array(
    'title' => 'Wish list settings',
    'description' => 'Configure the wish list settings.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'uc_wishlist_admin_settings_form',
    ),
    'access arguments' => array(
      'administer wish lists',
    ),
    'file' => 'uc_wishlist.admin.inc',
  );
  $items['wishlist'] = array(
    'title' => 'Wish list',
    'description' => 'View or modify the contents of your wish list.',
    'page callback' => 'uc_wishlist_display',
    'access arguments' => array(
      'access wish lists',
    ),
    'file' => 'uc_wishlist.pages.inc',
  );
  $items['wishlist/search'] = array(
    'title' => 'Find a wish list',
    'description' => 'Search for a wish list.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'uc_wishlist_search_form',
    ),
    'access arguments' => array(
      'access wish lists',
    ),
    'file' => 'uc_wishlist.pages.inc',
  );
  $items['wishlist/search/%'] = array(
    'title' => 'Find a wish list',
    'description' => 'Search for a wish list.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'uc_wishlist_search_form',
      2,
    ),
    'access arguments' => array(
      'access wish lists',
    ),
    'file' => 'uc_wishlist.pages.inc',
  );

  // TODO: Convert this to use the autoloaded %uc_wishlist.
  $items['wishlist/%'] = array(
    'title' => 'Wish list',
    'description' => 'View or modify the contents of your wish list.',
    'page callback' => 'uc_wishlist_display',
    'page arguments' => array(
      1,
    ),
    'access arguments' => array(
      'access wish lists',
    ),
    'file' => 'uc_wishlist.pages.inc',
  );
  $items['user/%user/wishlist'] = array(
    'title' => 'Wish list',
    'description' => 'View or modify the contents of your wish list.',
    'page callback' => 'uc_wishlist_display',
    'page arguments' => array(
      1,
      'user',
    ),
    'access callback' => 'user_view_access',
    'access arguments' => array(
      1,
    ),
    'file' => 'uc_wishlist.pages.inc',
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}

/**
 * Implementation of hook_perm().
 */
function uc_wishlist_perm() {
  return array(
    'administer wish lists',
    'access wish lists',
  );
}

/**
 * Implementation of hook_cron().
 */
function uc_wishlist_cron() {

  // TODO: Add clean up of anonymous wish lists.
}

/**
 * Implementation of hook_theme().
 */
function uc_wishlist_theme() {
  return array(
    'uc_wishlist_block_title' => array(
      'arguments' => array(
        'wishlist_image' => NULL,
        'uc_wishlist_path' => FALSE,
        'arrow_up_image' => FALSE,
      ),
    ),
    'uc_wishlist_block_content' => array(
      'arguments' => array(),
    ),
    'uc_wishlist_view_form' => array(
      'arguments' => array(
        'form' => NULL,
      ),
    ),
  );
}

/**
 * Implementation of hook_search().
 *
 * Provides wishlist/search page to list and search for users with wish lists.
 * TODO: separate hook_search from custom search page
 */
function uc_wishlist_search($op = 'search', $keys = NULL) {
  global $user;
  switch ($op) {
    case 'name':
      return t('Wish lists');
    case 'search':
      $links = array();

      // Check for user, wish list title, or address matches.
      $result = db_query("SELECT w.wid, w.title FROM {uc_wishlists} AS w JOIN {users} AS u ON w.uid = u.uid WHERE u.name LIKE '%%%s%%' OR w.title LIKE '%%%s%%' OR w.address LIKE '%%firstname%%%s%%addr1%%' ORDER BY w.title", $keys, $keys, $keys);
      while ($wishlist = db_fetch_object($result)) {
        $results[] = array(
          'link' => url('wishlist/' . $wishlist->wid),
          'type' => t('Wish list'),
          'title' => filter_xss($wishlist->title, array()),
        );
      }
      return $results;
  }
}

/**
 * Implementation of hook_block().
 */
function uc_wishlist_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {

    // Return a list of blocks enabled by this module.
    case 'list':
      $blocks[1]['info'] = t('Find a wish list');
      return $blocks;
    case 'view':
      if ($delta == 1) {
        $block['subject'] = t('Find a wish list');
        $block['content'] = drupal_get_form('uc_wishlist_block_search_form');
      }
      return $block;
  }
}

/**
 * Implementation of hook_exit().
 *
 * Code from CacheExclude - http://drupal.org/project/cacheexclude
 */
function uc_wishlist_exit() {
  global $base_root;
  $pages = array(
    'wishlist',
    'wishlist/search',
  );
  $this_page = request_uri();
  foreach ($pages as $page) {
    if ($page && strstr($this_page, $page) !== FALSE) {
      cache_clear_all($base_root . $this_page, 'cache_page');
      return;
    }
  }
}

/**
 * Implementation of hook_user().
 *
 * When a user logs in, any products on the wish list associated with the
 * current session are moved to the newly authenticated user's wish list
 * (or if that does not exist, the anonymous wish list is assigned as the
 * user's wish list).
 *
 * Also, provide a link to the user's wish list from the user's profile page.
 */
function uc_wishlist_user($op, &$edit, &$user, $category = NULL) {
  switch ($op) {
    case 'login':
      $oldwid = db_result(db_query("SELECT wid FROM {uc_wishlists} WHERE uid = '%s'", session_id()));
      $newwid = uc_wishlist_get_wid();
      if ($oldwid && $newwid) {
        $res = db_query("SELECT * FROM {uc_wishlist_products} WHERE wid = %d", $oldwid);
        while ($obj = db_fetch_object($res)) {

          // NOTE: the delete must precede uc_wishlist_add_item because
          // it may update the old record but we expect it to insert
          // a new record
          db_query("DELETE FROM {uc_wishlist_products} WHERE wpid = %d", $obj->wpid);
          uc_wishlist_add_item($obj->nid, $obj->qty, unserialize($obj->data), $newwid);
        }
        db_query("DELETE FROM {uc_wishlists} WHERE wid = %d", $oldwid);
      }
      elseif ($oldwid && !$newwid) {
        db_query("UPDATE {uc_wishlists} SET uid = %d WHERE wid = %d", $user->uid, $oldwid);
      }
      break;
    case 'view':
      $res = db_query("SELECT * FROM {uc_wishlists} WHERE uid = %d", $user->uid);
      $items = array();
      while ($obj = db_fetch_object($res)) {
        $items['wishlist_' . $obj->wid] = array(
          'value' => l($obj->title, "wishlist/{$obj->wid}"),
        );
      }
      if (count($items) > 0) {
        return array(
          t('Wish lists') => $items,
        );
      }
      break;
    case 'delete':

      // Find and delete any wish lists associated with the user being deleted.
      $result = db_query("SELECT wid FROM {uc_wishlists} WHERE uid = %d", $user->uid);
      while ($row = db_fetch_object($result)) {
        uc_wishlist_delete($row->wid);
      }
      break;
  }
}

/**
 * Implementation of hook_form_alter().
 *
 * Alter uc_product_add_to_cart_form instances to include 'add to wish list'
 * button. Form submission is routed through uc_wishlist_add_to_wishlist
 * to differentiate between 'add to cart' and 'add to wish list'. If this
 * module is added to Ubercart core, then this can be simplified.
 *
 * Alter uc_cart_checkout_form to include by default the delivery address
 * of a wish list's owner, if items come from a single wish list.
 */
function uc_wishlist_form_alter(&$form, &$form_state, $form_id) {
  if (strpos($form_id, 'uc_product') === 0 && strpos($form_id, '_add_to_cart_form_') !== FALSE) {
    global $user;

    //Checking if user has access to wish list
    if (user_access('access wish lists', $user)) {

      // Add the wish list button to the add to cart form.
      $form['wishlist'] = array(
        '#type' => 'submit',
        '#value' => t('Add to wish list'),
        '#submit' => array(
          'uc_wishlist_add_to_wishlist_submit',
        ),
      );
    }
  }
  if (strpos($form_id, 'uc_product_kit_add_to_cart_form_') === 0) {

    // When adding a product kit, disable its default submit handler.
    // Otherwise, it will also be added to the cart.
    $new_submit = $form['#submit'];
    $submit_position = array_search('uc_product_kit_add_to_cart_form_submit', $new_submit);
    if ($submit_position !== FALSE) {
      foreach ($form['#submit'] as $key => $function) {
        if ($key === $submit_position) {
          unset($new_submit[$key]);
        }
      }

      // Re-index array.
      $form['#submit'] = array_values($new_submit);
    }
  }
  if ($form_id == 'uc_cart_checkout_form' && !isset($_SESSION['cart_order'])) {
    $items = unserialize($form['cart_contents']['#value']);
    $wids = array();
    foreach ($items as $item) {
      if ($item->data['wid']) {
        $wids[] = $item->data['wid'];
      }
    }
    $wids = array_unique($wids);
    if (count($wids) > 1) {
      drupal_set_message(t('This order contains items from multiple wish lists. It is not possible to automatically address this order for its recipient.'));
    }
    else {
      if (count($wids) == 1) {
        $wishlist = uc_wishlist_load($wids[0]);
        if (!empty($wishlist->address->firstname) && !empty($wishlist->address->lastname) && !empty($wishlist->address->addr1) && !empty($wishlist->address->postcode)) {
          $form['panes']['delivery']['delivery_first_name']['#default_value'] = $wishlist->address->firstname;
          $form['panes']['delivery']['delivery_last_name']['#default_value'] = $wishlist->address->lastname;
          $form['panes']['delivery']['delivery_company']['#default_value'] = $wishlist->address->company;
          $form['panes']['delivery']['delivery_street1']['#default_value'] = $wishlist->address->addr1;
          $form['panes']['delivery']['delivery_street2']['#default_value'] = $wishlist->address->addr2;
          $form['panes']['delivery']['delivery_city']['#default_value'] = $wishlist->address->city;
          $form['panes']['delivery']['delivery_country']['#default_value'] = $wishlist->address->country;
          $form['panes']['delivery']['delivery_zone']['#default_value'] = $wishlist->address->zone;
          $form['panes']['delivery']['delivery_postal_code']['#default_value'] = $wishlist->address->postcode;
          $form['panes']['delivery']['delivery_phone']['#default_value'] = $wishlist->address->phone;
          drupal_set_message(t('This order contains items from a wish list. The delivery address has been automatically set to the preferred address from the wish list. You may change this address.'));
        }
      }
    }
  }
}

/**
 * uc_wishlist_add_to_wishlist
 *
 * See comment for uc_wishlist_form_alter: This function handles submission
 * of uc_product_add_to_cart_form and differentiates between 'add to cart'
 * and 'add to wish list' functions.
 */
function uc_wishlist_add_to_wishlist_submit($form, $form_state) {

  // Add the item to the user's wish list.
  uc_wishlist_add_item($form_state['values']['nid'], $form_state['values']['qty'], module_invoke_all('add_to_cart_data', $form_state['values']));
}

/*******************************************************************************
 * Hook Functions (TAPIr)
 ******************************************************************************/

/**
 * Implementation of hook_table_settings().
 */
function uc_wishlist_table_settings() {
  $tables[] = array(
    'id' => 'uc_wishlist_view_table',
    'description' => t("Display information on products in a customer's wish list."),
    'path' => 'admin/store/settings/tables',
    'access' => 'administer store',
    'preview' => FALSE,
  );
  return $tables;
}

/*******************************************************************************
 * Hook Functions (Ubercart)
 ******************************************************************************/

/**
 * Implements Ubercart hook_cart_item
 *
 * When an item is loaded in the shopping cart, this adds a note to the
 * title to show from which wish list this item came. This will not display
 * in the cart (since those titles come from hook_cart_display), but will
 * show on the checkout screen and be recorded in the order.
 */
function uc_wishlist_cart_item($op, &$item) {
  switch ($op) {
    case 'load':
      if ($item->data && $item->data['wid'] && $item->data['wpid']) {
        $wishlist = uc_wishlist_load($item->data['wid']);
        if ($wishlist && !empty($wishlist->title)) {
          $item->title .= t(' [from !wishlist]', array(
            '!wishlist' => filter_xss($wishlist->title, array()),
          ));
        }
      }
      break;
  }
}

/**
 * Implements Ubercart hook_order.
 *
 * When the status of an order is changed to pending, then the order is
 * scanned for items from wish lists (that contain wid and wpid data fields).
 * If items are found, then their records in uc_wishlist_products are
 * updated with the additional purchases.
 */
function uc_wishlist_order($op, &$arg1, $arg2) {
  if ($op == 'update') {
    if (uc_order_status_data($arg1->order_status, 'state') != 'in_checkout' || uc_order_status_data($arg2, 'state') == 'in_checkout' || uc_order_status_data($arg2, 'state') == 'canceled') {
      return;
    }
    $o = $arg1;
    foreach ($o->products as $key => $item) {
      if ($item->data['wid'] && $item->data['wpid'] && !isset($item->data['wset'])) {
        $wid = $item->data['wid'];
        $wpid = $item->data['wpid'];
        $w = uc_wishlist_load($wid);
        $witem = uc_wishlist_get_product($wpid);
        $purchase = array(
          'order_id' => $item->order_id,
          'order_product_id' => $item->order_product_id,
          'uid' => $o->uid,
          'date' => time(),
        );
        for ($i = 0; $i < $item->qty; $i++) {
          $witem->purchase[] = $purchase;
        }
        uc_wishlist_update_purchase($witem);
      }
    }
  }
}

/*******************************************************************************
 * Callback Functions, Forms, and Tables
 ******************************************************************************/

/**
 * Update information about a specific item in current wish list.
 */
function uc_product_update_wishlist_item($nid, $data = array(), $qty, $wid = NULL, $wpid = NULL) {
  if (!$nid) {
    return NULL;
  }
  $wid = $wid ? $wid : uc_wishlist_get_wid();
  if ($qty < 1) {
    $wpid = $wpid ? $wpid : $data['wpid'];
    uc_wishlist_remove_item($wpid);
  }
  else {
    db_query("UPDATE {uc_wishlist_products} SET qty = %d, changed = %d WHERE wpid = %d", $qty, time(), $wpid);
  }
}

/*******************************************************************************
 * Module and Helper Functions
 ******************************************************************************/

// Get either an authenticated user's uid or an anonymous user's unique ID.
function uc_wishlist_get_uid() {
  global $user;

  // Get either an authenticated user's uid or an anonymous user's unique ID.
  if ($user->uid) {
    $uid = $user->uid;
  }
  else {
    if (!isset($_SESSION['uc_wishlist_uid']) || empty($_SESSION['uc_wishlist_uid'])) {
      $_SESSION['uc_wishlist_uid'] = md5(uniqid(rand(), TRUE));
    }
    $uid = $_SESSION['uc_wishlist_uid'];
  }
  return $uid;
}

// Return the wish list ID of the specified user (defaults to current user)
function uc_wishlist_get_wid($uid = NULL) {

  // Find the wish list matching the authenticated or anonymous user ID.
  // TODO: Handle multiple wishlists?
  if (empty($uid)) {
    $uid = uc_wishlist_get_uid();
  }
  return db_result(db_query("SELECT wid FROM {uc_wishlists} WHERE uid = '%s'", $uid));
}

/**
 * Load a wish list object from the database.
 *
 * @param $wid
 *   The ID of the wish list to load; if NULL, looks for the current user's
 *     wish list.
 * @return
 *   A wish list object.
 */
function uc_wishlist_load($wid = NULL) {
  if (empty($wid)) {
    $wid = uc_wishlist_get_wid();
  }
  if (!$wid || !is_numeric($wid)) {
    return FALSE;
  }
  $result = db_query("SELECT * FROM {uc_wishlists} WHERE wid = %d", $wid);
  if ($wishlist = db_fetch_object($result)) {
    $wishlist->address = unserialize($wishlist->address);
    return $wishlist;
  }
  return FALSE;
}

/**
 * Get the items in a specified wish list.
 */
function uc_wishlist_get_contents($wid = NULL) {
  $wid = $wid ? $wid : uc_wishlist_get_wid();
  if (!$wid || !is_numeric($wid)) {
    return FALSE;
  }
  $items = array();
  $res = db_query('SELECT w.*, n.title, n.vid FROM {node} n INNER JOIN {uc_wishlist_products} w ON n.nid = w.nid WHERE w.wid = %d', $wid);
  while ($item = db_fetch_object($res)) {
    for ($i = 0; $i < count($items); $i++) {
      if ($items[$i]->nid == $item->nid && $items[$i]->data == $item->data) {
        $items[$i]->qty += $item->qty;
        continue 2;
      }
    }
    $product = node_load($item->nid);
    $item->model = $product->model;
    $item->cost = $product->cost;
    $item->price = $product->sell_price;
    $item->weight = $product->weight;
    $item->weight_units = $product->weight_units;
    $item->shippable = $product->shippable;
    $item->data = unserialize($item->data);
    $item->module = $item->data['module'];
    $item->purchase = unserialize($item->purchase);
    $item->options = array();

    // add wishlist id data
    $item->data['wid'] = $item->wid;
    $item->data['wpid'] = $item->wpid;

    // Invoke hook_cart_item() with $op = 'modify' in enabled modules.
    // An especially important hook is uc_attribute_cart_item which
    // updates the item given the attributes.
    foreach (module_implements('cart_item') as $module) {
      if ($module == 'uc_wishlist') {
        continue;
      }
      $func = $module . '_cart_item';
      $func('load', $item);
    }
    $items[] = $item;
  }
  return $items;
}
function uc_wishlist_get_product($wpid) {
  if (!$wpid) {
    return FALSE;
  }
  $item = FALSE;
  $res = db_query("SELECT w.*, n.title, n.vid FROM {node} n INNER JOIN {uc_wishlist_products} w ON n.nid = w.nid WHERE w.wpid = %d", $wpid);
  if ($item = db_fetch_object($res)) {
    $product = node_load($item->nid);
    $item->model = $product->model;
    $item->cost = $product->cost;
    $item->price = $product->sell_price;
    $item->weight = $product->weight;
    $item->weight_units = $product->weight_units;
    $item->shippable = $product->shippable;
    $item->data = unserialize($item->data);
    $item->module = $item->data['module'];
    $item->purchase = unserialize($item->purchase);
    $item->options = array();

    // add wishlist id data
    $item->data['wid'] = $item->wid;
    $item->data['wpid'] = $item->wpid;

    // Invoke hook_cart_item() with $op = 'modify' in enabled modules.
    // An especially important hook is uc_attribute_cart_item which
    // updates the item given the attributes.
    foreach (module_implements('cart_item') as $module) {
      if ($module == 'uc_wishlist') {
        continue;
      }
      $func = $module . '_cart_item';
      $func('load', $item);
    }
  }
  return $item;
}

/*
 * uc_wishlist_update_purchase is used from uc_wishlist_order to
 * update the data on the purchases of a wish list item.
 *
 */
function uc_wishlist_update_purchase($witem) {
  if ($witem->wpid && is_array($witem->purchase)) {
    $rv = db_query("UPDATE {uc_wishlist_products} SET purchase = '%s' WHERE wpid = %d", serialize($witem->purchase), $witem->wpid);
  }
}

/**
 * Creates a new wishlist for the current authenticated or anonymous user.
 */
function uc_wishlist_create_wishlist($title = NULL) {
  global $user;

  // Abort if user is not logged in and anonymous wish lists are not allowed.
  if (!$user->uid && !variable_get('uc_wishlist_allow_anonymous', FALSE)) {
    drupal_set_message(t('You must be logged in to create a wish list. Please <a href="!login_url">login</a> or <a href="!register_url">register</a>.', array(
      '!login_url' => url('user', array(
        'query' => drupal_get_destination(),
      )),
      '!register_url' => url('user/register', array(
        'query' => drupal_get_destination(),
      )),
    )), 'error');
    return FALSE;
  }

  // Get the current user ID for the wish list.
  $uid = uc_wishlist_get_uid();
  if (empty($title)) {
    $title = variable_get('uc_wishlist_default_title', "%user's wish list");
  }
  if (strpos($title, '%user') !== FALSE) {
    $uname = $user->name ? $user->name : variable_get('anonymous', t('Anonymous'));
    $title = str_replace('%user', $uname, $title);
  }
  $expiration = time() + variable_get('uc_wishlist_default_length', 2592000);
  $result = db_query("INSERT INTO {uc_wishlists} (uid, title, expiration, description) VALUES ('%s', '%s', %d, '%s')", $uid, $title, $expiration, '');
  if ($result) {
    return db_last_insert_id('uc_wishlists', 'wid');
  }
  return FALSE;
}
function uc_wishlist_update_wishlist($wid, $title, $date, $address, $description) {

  // TODO: Combine with uc_wishlist_create_wishlist?
  $addrstr = serialize($address);
  db_query("UPDATE {uc_wishlists} SET title = '%s', expiration = %d, address = '%s', description = '%s' WHERE wid = %d", $title, $date, $addrstr, $description, $wid);
}

/**
 * Adds an item to a user's wish list.
 */
function uc_wishlist_add_item($nid, $qty = 1, $data = NULL, $wid = NULL, $msg = TRUE, $check_redirect = TRUE) {
  $wid = $wid ? $wid : uc_wishlist_get_wid();
  $created = FALSE;
  if (!$wid || $wid === NULL) {
    $wid = uc_wishlist_create_wishlist();
    if (!$wid) {
      drupal_set_message(t('Could not create wish list. Adding item failed.'), 'error');
      return FALSE;
    }
    $created = TRUE;
  }
  $node = node_load($nid);
  if (is_null($data)) {
    $data = array(
      'module' => 'uc_product',
    );
  }
  $supported_node_types = array_merge(array_keys(uc_product_node_info()), uc_product_kit_product_types());
  if (!in_array($node->type, $supported_node_types)) {
    drupal_set_message(t('!title is not a product. Unable to add to wish list.', array(
      '!title' => $node->title,
    )), 'error');
    return;
  }
  $result = uc_wishlist_invoke_hook_add_to_cart($nid, $qty, $data);
  if (is_array($result) && !empty($result)) {
    foreach ($result as $row) {
      if ($row['success'] === FALSE && (!isset($row['silent']) || $row['silent'] === FALSE)) {
        $message = empty($row['message']) ? t('Sorry, that item is not available for purchase at this time.') : $row['message'];
        drupal_set_message($message, 'error');
        return;
      }
    }
  }
  $item = db_fetch_object(db_query("SELECT * FROM {uc_wishlist_products} WHERE wid = %d AND nid = %d AND data = '%s'", $wid, $nid, serialize($data)));

  // If the item isn't in the cart yet, add it.
  if (is_null($item) || $item === FALSE) {
    db_query("INSERT INTO {uc_wishlist_products} (wid, nid, qty, changed, data, purchase) VALUES (%d, %d, %d, %d, '%s', '')", $wid, $nid, $qty, time(), serialize($data));
    if ($msg) {
      drupal_set_message(t('<b>@product-title</b> added to <a href="!url">your wish list</a>.', array(
        '@product-title' => $node->title,
        '!url' => url('wishlist'),
      )));
    }
  }
  else {

    // Update the item instead.
    $qty += $item->qty;
    $wpid = $item->wpid;
    uc_product_update_wishlist_item($nid, $data, min($qty, 999999), $wid, $wpid);
    if ($msg) {
      drupal_set_message(t('Your <a href="!url">wish list</a> has been updated.', array(
        '!url' => url('wishlist'),
      )));
    }
  }
  if ($check_redirect) {
    if (isset($_GET['destination'])) {
      drupal_goto();
    }
  }
  if ($created) {
    drupal_goto('wishlist');
  }
}

/**
 * Remove an item from the wish list
 */
function uc_wishlist_remove_item($wpid) {
  db_query("DELETE FROM {uc_wishlist_products} WHERE wpid = %d", $wpid);
}

/**
 * Updates the quantity of all the items in a cart object
 */
function uc_wishlist_update_item_object($wform) {
  if (is_object($wform)) {
    if ($wform->wid) {
      $wid = $wform->wid;
    }
    else {
      $wid = uc_wishlist_get_wid();
    }
    foreach ($wform->items as $item) {
      $data = unserialize($item['data']);
      $wpid = $data['wpid'];
      if ($item['remove']) {
        module_invoke($item['module'], 'update_wishlist_item', $item['nid'], $data, 0, $wid, $wpid);
      }
      else {
        module_invoke($item['module'], 'update_wishlist_item', $item['nid'], $data, $item['wantqty'], $wid, $wpid);
      }
    }
  }
}
function uc_wishlist_add_items_to_cart($form_values) {
  if (is_object($form_values)) {
    if ($form_values->wid) {
      $wid = $form_values->wid;
    }
    else {
      $wid = uc_wishlist_get_wid();
    }
    foreach ($form_values->items as $item) {
      if ($item['qty'] > 0) {

        //TODO: add attribute information 'gift to user' that shows up on receipts

        // see uc_wishlist_cart_item
        $data = unserialize($item['data']);
        uc_cart_add_item($item['nid'], $item['qty'], $data);
      }
    }
  }
}

// Deletes a wish list and all its products.
function uc_wishlist_delete($wid) {
  db_query('DELETE FROM {uc_wishlists} WHERE wid = %d', $wid);
  db_query('DELETE FROM {uc_wishlist_products} WHERE wid = %d', $wid);
}

// Display the wish list search form.
function uc_wishlist_block_search_form() {
  $form = array();
  $form['keywords'] = array(
    '#type' => 'textfield',
    '#title' => t('Search keywords'),
    '#description' => t('Enter the keywords to use to search wish list titles and addresses.'),
    '#size' => 16,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Search'),
  );
  return $form;
}
function uc_wishlist_block_search_form_submit($form, &$form_state) {
  if (empty($form_state['values']['keywords'])) {
    $form_state['redirect'] = 'wishlist/search';
  }
  else {
    $form_state['redirect'] = 'wishlist/search/' . drupal_urlencode($form_state['values']['keywords']);
  }
}

/**
 * Invoke hook_add_to_cart() in every module except uc_product_kit.
 * We do this because that module adds the kit to the cart in that hook.
 * It's probably s a bug.
 */
function uc_wishlist_invoke_hook_add_to_cart() {
  $args = func_get_args();
  $hook = 'add_to_cart';
  $return = array();
  foreach (module_implements($hook) as $module) {
    if ($module != 'uc_product_kit') {
      $function = $module . '_' . $hook;
      $result = call_user_func_array($function, $args);
      if (isset($result) && is_array($result)) {
        $return = array_merge_recursive($return, $result);
      }
      else {
        if (isset($result)) {
          $return[] = $result;
        }
      }
    }
  }
  return $return;
}

/**
 * Implements hook_views_api()
 */
function uc_wishlist_views_api() {
  return array(
    'api' => 3,
  );
}

Functions

Namesort descending Description
uc_product_update_wishlist_item Update information about a specific item in current wish list.
uc_wishlist_add_item Adds an item to a user's wish list.
uc_wishlist_add_items_to_cart
uc_wishlist_add_to_wishlist_submit uc_wishlist_add_to_wishlist
uc_wishlist_block Implementation of hook_block().
uc_wishlist_block_search_form
uc_wishlist_block_search_form_submit
uc_wishlist_cart_item Implements Ubercart hook_cart_item
uc_wishlist_create_wishlist Creates a new wishlist for the current authenticated or anonymous user.
uc_wishlist_cron Implementation of hook_cron().
uc_wishlist_delete
uc_wishlist_exit Implementation of hook_exit().
uc_wishlist_form_alter Implementation of hook_form_alter().
uc_wishlist_get_contents Get the items in a specified wish list.
uc_wishlist_get_product
uc_wishlist_get_uid
uc_wishlist_get_wid
uc_wishlist_invoke_hook_add_to_cart Invoke hook_add_to_cart() in every module except uc_product_kit. We do this because that module adds the kit to the cart in that hook. It's probably s a bug.
uc_wishlist_load Load a wish list object from the database.
uc_wishlist_menu Implementation of hook_menu().
uc_wishlist_order Implements Ubercart hook_order.
uc_wishlist_perm Implementation of hook_perm().
uc_wishlist_remove_item Remove an item from the wish list
uc_wishlist_search Implementation of hook_search().
uc_wishlist_table_settings Implementation of hook_table_settings().
uc_wishlist_theme Implementation of hook_theme().
uc_wishlist_update_item_object Updates the quantity of all the items in a cart object
uc_wishlist_update_purchase
uc_wishlist_update_wishlist
uc_wishlist_user Implementation of hook_user().
uc_wishlist_views_api Implements hook_views_api()