[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/gallery/ -> gallery_search.inc (source)

   1  <?php
   2  // $Id: gallery_search.inc,v 1.4.2.3 2008/02/18 23:07:15 profix898 Exp $
   3  
   4  /**
   5   * gallery.module search functions
   6   */
   7   
   8  /**
   9   * Implementation of hook_search().
  10   */
  11  function _gallery_search($op = 'search', $keys = NULL) {
  12    switch ($op) {
  13      case 'name':
  14        return t('Gallery');
  15      case 'search':
  16        $results = array();
  17        $head_array = array();
  18        $items_per_row = variable_get('gallery_search_num_per_row', 3);
  19        $rows_per_pager = variable_get('gallery_search_rows_per_pager', 4);
  20        $matches = _gallery_search_pager_search($keys, $items_per_row * $rows_per_pager);
  21        if ($matches['count']) {
  22          $results['count'] = $matches['count'];
  23          // Parameters for the search results
  24          $params = array();
  25          $params['blocks'] = 'specificItem';
  26          $param_show_array = variable_get('gallery_search_block_show', array());
  27          $params['show'] = is_array($param_show_array) ? implode('|', $param_show_array) : '';
  28          if (variable_get('gallery_search_size_method', GALLERY_SEARCH_SIZE_METHOD_DEFAULT) == 'maxsize') {
  29            $params['maxSize'] = variable_get('gallery_search_size', GALLERY_SEARCH_SIZE_DEFAULT);
  30          } 
  31          else {
  32            $params['exactSize'] = variable_get('gallery_search_size', GALLERY_SEARCH_SIZE_DEFAULT);
  33          }
  34          $params['albumFrame'] =  variable_get('gallery_search_album_frame', 'none');
  35          $params['itemFrame'] =  variable_get('gallery_search_item_frame', 'none');
  36          $params['linkTarget'] =  variable_get('gallery_search_link_target', '');
  37          $params['link'] = variable_get('gallery_search_link', '');
  38         
  39          $show_thumbs = variable_get('gallery_search_show_thumbs', 1);
  40          // Loop over the results
  41          foreach ($matches['results'] as $item) {  
  42            $excerpt = array();
  43            // Get a thumbnail for this item
  44            if ($show_thumbs) {
  45              $params['itemId'] = $item['itemId'];
  46              list($ret, $thumbnail, $head) = GalleryEmbed::getBlock('imageblock', 'ImageBlock', $params);
  47              if ($ret) {
  48                $thumbnail = t('n/a');
  49              }
  50              if ($head) {
  51                $head_array[] = $head;
  52              }
  53            }
  54            // Generate a snippet with highlighted search keys
  55            foreach ($item['fields'] as $field) {
  56              if (preg_match("/$keys/i", $field['value'])) {
  57                $excerpt[] = '<em>'. $field['key'] .':</em> '. search_excerpt($keys, $field['value']);
  58              }
  59            }
  60            // Put everything into the $results array
  61            $title = reset($item['fields']);
  62            $results[] = array(
  63              'link' => gallery_generate_url(array('itemId' => $item['itemId']), FALSE),
  64              'title' => empty($title['value']) ? t('Gallery item: Untitled') : $title['value'],
  65              'snippet' => implode('<br />', $excerpt),
  66              'thumbnail' => $thumbnail,
  67            );
  68          }
  69        }
  70        // Add html head items and css
  71        if (count($head_array)) {
  72          gallery_set_head(implode("\n", array_unique($head_array)));
  73        }
  74        return $results;
  75    }
  76  }
  77  
  78  /**
  79   * Function _gallery_search_pager_search().
  80   */
  81  function _gallery_search_pager_search(&$keys, $limit = 10, $element = 0) {
  82    // Adapted version of the pager_query() function (from /includes/pager.inc)
  83    // for use with the Gallery2 search() function
  84    //
  85    global $pager_page_array, $pager_total, $pager_total_items;
  86    $page = isset($_GET['page']) ? $_GET['page'] : '';
  87  
  88    // Convert comma-separated $page to an array, used by other functions.
  89    $pager_page_array = explode(',', $page);
  90  
  91    // We calculate the total of pages as ceil(items / limit).
  92    $count = _gallery_search_perform($keys);
  93    $pager_total_items[$element] = $count['count'];
  94    $pager_total[$element] = ceil($pager_total_items[$element] / $limit);
  95    $pager_page_array[$element] = max(0, min((int)$pager_page_array[$element], ((int)$pager_total[$element]) - 1));
  96    
  97    return _gallery_search_perform($keys, $pager_page_array[$element] * $limit, $limit);
  98  }
  99  
 100  /**
 101   * Function _gallery_search_perform().
 102   */
 103  function _gallery_search_perform($keys, $offset = 0, $limit = -1) {
 104    list($search_interface, $options) = _gallery_search_init();
 105    if (!isset($search_interface)) {
 106      return array();
 107    }
 108    // Extract query parameters
 109    if ($fields = search_query_extract($keys, 'fields')) {
 110      $keys = trim(preg_replace('/\s+fields:[\w,]*/', '', $keys));
 111    }
 112    $fields = $fields ? array_flip(explode(',', $fields)) : $options;
 113    foreach ($fields as $key => $value) {
 114      $fields[$key] = $key;
 115    }
 116    // Perform the actual search
 117    list($ret, $matches) = $search_interface->search($fields, $keys, $offset, $limit);
 118    if ($ret) {
 119      gallery_error(t('Error invoking search() method.'), $ret);
 120      return array();
 121    }
 122    
 123    return $matches;
 124  }
 125  
 126  /**
 127   * Function _gallery_search_form().
 128   */
 129  function _gallery_search_form(&$form, $form_state) {
 130    list($search_interface, $options) = _gallery_search_init();
 131    if (!count($options)) {
 132      return;
 133    }
 134    // Extend search form
 135    $form['advanced'] = array(
 136      '#type' => 'fieldset',
 137      '#title' => t('Advanced search'),
 138      '#collapsible' => TRUE,
 139      '#collapsed' => TRUE,
 140      '#attributes' => array('class' => 'search-advanced'),
 141    );
 142    
 143    $form['advanced']['fields'] = array(
 144      '#type' => 'checkboxes',
 145      '#title' => t('Only the following fields'),
 146      '#prefix' => '<div class="criterion">',
 147      '#suffix' => '</div>',
 148      '#options' => $options,
 149    );
 150    
 151    $form['advanced']['submit'] = array(
 152      '#type' => 'submit',
 153      '#value' => t('Advanced search'),
 154      '#prefix' => '<div class="action clear-block">',
 155      '#suffix' => '</div>',
 156    );
 157    
 158    $form['#validate'][] = '_gallery_search_validate';
 159  }
 160  
 161  /**
 162   * Function _gallery_search_validate().
 163   */
 164  function _gallery_search_validate($form, &$form_state) {
 165    $keys = $form_state['values']['processed_keys'];
 166    // Append field options to query
 167    if (isset($form_state['values']['fields']) && is_array($form_state['values']['fields'])) {
 168      $form_state['values']['fields'] = array_filter($form_state['values']['fields']);
 169      if (count($form_state['values']['fields'])) {
 170        $keys = search_query_insert($keys, 'fields', implode(',', array_keys($form_state['values']['fields'])));
 171        form_set_value($form['basic']['inline']['processed_keys'], trim($keys), $form_state);
 172      }
 173    }
 174  }
 175  
 176  /**
 177   * Function _gallery_search_init().
 178   */
 179  function _gallery_search_init() {
 180    if (!_gallery_init(TRUE)) {
 181      return array();
 182    }
 183    // Create instance of search interface
 184    list($ret, $search_interface) = GalleryCoreApi::newFactoryInstance('GallerySearchInterface_1_0');
 185    if ($ret) {
 186      gallery_error(t('Error creating instance of GallerySearchInterface. Make sure the \'Search\' plugin is enabled in Gallery2.'), $ret);
 187      return array(NULL, array());
 188    }
 189    // Get search module info
 190    list($ret, $module_info) = $search_interface->getSearchModuleInfo();
 191    if ($ret) {
 192      gallery_error(t('Error getting \'Search\' module options.'), $ret);
 193      return array(NULL, array());
 194    }
 195    $options = array();
 196    foreach ($module_info['options'] as $module => $info) {
 197      if ($info['enabled']) {
 198        $options[$module] = $info['description'];
 199      } 
 200    }
 201    
 202    return array($search_interface, $options);
 203  }
 204  
 205  /**
 206   * Function _gallery_search_page().
 207   */
 208  function _gallery_search_page($results) {
 209    $items_per_row = variable_get('gallery_search_num_per_row', 3);
 210    $rows_per_pager = variable_get('gallery_search_rows_per_pager', 4);
 211    
 212    $output  = '<dl class="search-results">';
 213    $output .= t('<p>Total Number of Matches: @count</p>', array('@count' => $results['count']));
 214    unset($results['count']);
 215    // Arrange items as table
 216    $rows = array();
 217    $results = array_chunk($results, $items_per_row);
 218    foreach ($results as $item_row) {
 219      $row = array();
 220      foreach ($item_row as $item) {
 221        $row[] = array('data' => theme('gallery_search_item', $item));
 222      }
 223      $rows[] = $row;
 224    }
 225    $output .= theme('table', array(), $rows);
 226    $output .= '</dl>';
 227    $output .= theme('pager', NULL, $items_per_row * $rows_per_pager, 0);
 228  
 229    return $output;
 230  }
 231  
 232  /**
 233   * Theme function : theme_gallery_search_item().
 234   */
 235  function theme_gallery_search_item($item) {
 236    $output  = ' <dt class="title"><a href="'. check_url($item['link']) .'">'. check_plain($item['title']) .'</a></dt>';
 237    $output .= '<div class="g2image_centered">'. $item['thumbnail'] .'</div>'; 
 238    $output .= ' <dd>'. ($item['snippet'] ? '<p>'. $item['snippet'] .'</p>' : '') .'</dd>';
 239    
 240    return $output;
 241  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7