You are here

function uc_wishlist_search_form in UC Wish List 7

Same name and namespace in other branches
  1. 6 uc_wishlist.pages.inc \uc_wishlist_search_form()

Display the wish list search form.

1 string reference to 'uc_wishlist_search_form'
uc_wishlist_menu in ./uc_wishlist.module
Implements hook_menu().

File

./uc_wishlist.pages.inc, line 661
Page callback and functions for wish lists.

Code

function uc_wishlist_search_form($form, $form_state, $keywords = '') {
  global $user;
  $form = array();

  // Generate link to 'create or manage your wish list'.
  $path = 'wishlist';
  $query = NULL;
  if (!$user->uid && !user_access('create wish lists')) {
    $path = 'user';
    $query = array(
      'destination' => 'wishlist',
    );
  }
  $form['wishlist_link'] = array(
    '#type' => 'item',
    '#markup' => '<div>' . l(t('Create or manage your wish list.'), $path, array(
      'query' => $query,
    )) . '</div>',
  );
  $form['search'] = array(
    '#type' => 'fieldset',
  );
  $form['search']['keywords'] = array(
    '#type' => 'textfield',
    '#title' => t('Search keywords'),
    '#description' => t('Enter the keywords to use to search wish list titles and addresses.'),
    '#default_value' => $keywords,
  );
  $form['search']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Search'),
  );
  if (!empty($keywords)) {

    // Check for user, wish list title, or address matches.
    $query = db_select('uc_wishlists', 'w');
    $query
      ->join('users', 'u', 'w.uid = u.uid');
    $query
      ->fields('w', array(
      'wid',
      'title',
    ));
    $query
      ->distinct();
    $query
      ->condition(db_or()
      ->condition('u.name', '%' . $keywords . '%', 'LIKE')
      ->condition('w.title', '%' . $keywords . '%', 'LIKE')
      ->condition('w.address', '%' . $keywords . '%', 'LIKE'));
  }
  else {
    $query = db_select('uc_wishlists', 'w');
    $query
      ->fields('w', array(
      'wid',
      'title',
    ));
  }
  $query
    ->condition('w.private', 0, '=');
  $result = $query
    ->orderBy('w.title')
    ->extend('PagerDefault')
    ->limit(25)
    ->execute();
  $links = array();
  foreach ($result as $wishlist) {
    $links[] = array(
      'title' => filter_xss($wishlist->title, array()),
      'href' => 'wishlist/' . $wishlist->wid,
    );
  }
  if (!empty($links)) {
    $output = theme_links(array(
      'links' => $links,
      'attributes' => array(
        'class' => array(
          'wishlist',
        ),
      ),
      'heading' => NULL,
    ));
  }
  else {
    $output = ' ' . t('No wish lists found.');
  }
  $form['output'] = array(
    '#type' => 'item',
    '#markup' => filter_xss('<div><h2>' . t('Wish lists:') . '</h2>' . $output . '</div>'),
    '#access' => !empty($keywords) || variable_get('uc_wishlist_show_all', TRUE),
  );
  return $form;
}