You are here

function uc_wishlist_create_wishlist in UC Wish List 6

Same name and namespace in other branches
  1. 8 uc_wishlist.module \uc_wishlist_create_wishlist()

Creates a new wishlist for the current authenticated or anonymous user.

1 call to uc_wishlist_create_wishlist()
uc_wishlist_add_item in ./uc_wishlist.module
Adds an item to a user's wish list.

File

./uc_wishlist.module, line 613
Allows users to create public shopping/wish lists.

Code

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;
}