summaryrefslogtreecommitdiffstats
path: root/gallery_search.inc
blob: 6ed2cc9253bbcdc1b45e1300f50d1c940f16b35a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
// $Id$

/**
 * gallery.module search functions
 */
 
/**
 * Implementation of hook_search
 */
function _gallery_search($op = 'search', $keys = NULL) {
  switch ($op) {
    case 'name':
      return t('Gallery');
    case 'search':
      $results = array();
      $html_head = array();
      $items_per_row = variable_get('gallery_search_num_per_row', 3);
      $rows_per_pager = variable_get('gallery_search_rows_per_pager', 4);
      $matches = _gallery_search_pager_search($keys, $items_per_row * $rows_per_pager);
      if ($matches['count']) {
        $results['count'] = $matches['count'];
        // parameters for the search results
        $params = array();
        $params['blocks'] = 'specificItem';
        $param_show_array = variable_get('gallery_search_block_show', array());
        $params['show'] = is_array($param_show_array) ? implode('|', $param_show_array) : '';
        if (variable_get('gallery_search_size_method',
          GALLERY_SEARCH_SIZE_METHOD_DEFAULT) == 'maxsize') {
          $params['maxSize'] = variable_get('gallery_search_size', GALLERY_SEARCH_SIZE_DEFAULT);
        } 
        else {
          $params['exactSize'] = variable_get('gallery_search_size', GALLERY_SEARCH_SIZE_DEFAULT);
        }
        $params['albumFrame'] =  variable_get('gallery_search_album_frame', 'none');
        $params['itemFrame'] =  variable_get('gallery_search_item_frame', 'none');
        $params['linkTarget'] =  variable_get('gallery_search_link_target', '');
        $params['link'] = variable_get('gallery_search_link', '');
       
        $show_thumbs = variable_get('gallery_search_show_thumbs', 1);
        // loop over the results      
        foreach ($matches['results'] as $item) {  
          $excerpt = array();
          // get a thumbnail for this item
          if ($show_thumbs) {
            $params['itemId'] = $item['itemId'];
            list($ret, $thumbnail, $head) = GalleryEmbed::getImageBlock($params);
            if ($ret) {
              $thumbnail = t('n/a');
            }
            if ($head) {
              $html_head[] = $head;
            }
          }
          // generate a snippet with highlighted search keys
          foreach ($item['fields'] as $field) {
            if (preg_match("/$keys/i", $field['value'])) {
              $excerpt[] = '<em>'. $field['key'] .':</em> '. search_excerpt($keys, $field['value']);
            }
          }
          // put everything into the $results array
          $title = reset($item['fields']);
          $results[] = array(
            'link' => gallery_generate_url(array('itemId' => $item['itemId']), FALSE),
            'title' => empty($title['value']) ? t('Gallery item: Untitled') : $title['value'],
            'snippet' => implode('<br />', $excerpt),
            'thumbnail' => $thumbnail,
          );
        }
      }
      if ($html_head) {
        drupal_set_html_head(implode("\n", array_unique($html_head)));
      }
      return $results;
  }
}

function _gallery_search_pager_search(&$keys, $limit = 10, $element = 0) {
  // adapted version of the pager_query() function (from /includes/pager.inc)
  // for use with the Gallery2 search() function
  //
  global $pager_page_array, $pager_total, $pager_total_items;
  $page = isset($_GET['page']) ? $_GET['page'] : '';

  // convert comma-separated $page to an array, used by other functions.
  $pager_page_array = explode(',', $page);

  // we calculate the total of pages as ceil(items / limit).
  $count = _gallery_search_perform($keys);
  $pager_total_items[$element] = $count['count'];
  $pager_total[$element] = ceil($pager_total_items[$element] / $limit);
  $pager_page_array[$element] = max(0, min((int)$pager_page_array[$element], ((int)$pager_total[$element]) - 1));
  
  return _gallery_search_perform($keys, $pager_page_array[$element] * $limit, $limit);
}

function _gallery_search_perform(&$keys, $offset = 0, $limit = -1) {
  list($search_interface, $options) = _gallery_search_init();
  if (!$search_interface) {
    return array();
  }
  // extract query parameters
  if ($fields = search_query_extract($keys, 'fields')) {
    $keys = trim(preg_replace('/\s+fields:[\w,]*/', '', $keys));
  }
  $fields = $fields ? array_flip(explode(',', $fields)) : $options;
  foreach ($fields as $key => $value) {
    $fields[$key] = $key;
  }
  // perform the actual search
  list($ret, $matches) = $search_interface->search($fields, $keys, $offset, $limit);
  
  return $ret ? array() : $matches;
}

function _gallery_search_form(&$form) {
  list($search_interface, $options) = _gallery_search_init();
  if (!count($options)) {
    return;
  }
  // extend search form
  $form['advanced'] = array(
    '#type' => 'fieldset',
    '#title' => t('Advanced search'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#attributes' => array('class' => 'search-advanced'),
  );
  
  $form['advanced']['fields'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Only the following fields'),
    '#prefix' => '<div class="criterion">',
    '#suffix' => '</div>',
    '#options' => $options,
  );
  
  $form['advanced']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Advanced search'),
    '#prefix' => '<div class="action clear-block">',
    '#suffix' => '</div>',
  );
  
  $form['#validate']['_gallery_search_validate'] = array();
}

function _gallery_search_validate($form_id, $form_values, $form) {
  $keys = $form_values['processed_keys'];
  // append field options to query
  if (isset($form_values['fields']) && is_array($form_values['fields'])) {
    $form_values['fields'] = array_filter($form_values['fields']);
    if (count($form_values['fields'])) {
      $keys = search_query_insert($keys, 'fields', implode(',', array_keys($form_values['fields'])));
      form_set_value($form['basic']['inline']['processed_keys'], trim($keys));
    }
  }
}

function _gallery_search_init() {
  // init gallery
  if (!_gallery_init(TRUE)) {
    return array();
  }
  // create instance of search interface
  list($ret, $search_interface) = GalleryCoreApi::newFactoryInstance('GallerySearchInterface_1_0');
  if ($ret) {
    gallery_error(t('Error creating instance of GallerySearchInterface. Make sure the \'Search\' plugin is enabled in Gallery2.'), $ret);
    return array(NULL, array());
  }
  // get search module info
  list($ret, $module_info) = $search_interface->getSearchModuleInfo();
  if ($ret) {
    gallery_error(t('Error getting \'Search\' module options.'), $ret);
    return array(NULL, array());
  }
  $options = array();
  foreach ($module_info['options'] as $module => $info) {
    if ($info['enabled']) {
      $options[$module] = $info['description'];
    } 
  }
  
  return array($search_interface, $options);
}

function _gallery_search_page($results) {
  $items_per_row = variable_get('gallery_search_num_per_row', 3);
  $rows_per_pager = variable_get('gallery_search_rows_per_pager', 4);
  
  $output  = '<dl class="search-results">';
  $output .= t('<p>Total Number of Matches: @count</p>', array('@count' => $results['count']));
  unset($results['count']);
  // arrange items as table
  $rows = array();
  $results = array_chunk($results, $items_per_row);
  foreach ($results as $item_row) {
    $row = array();
    foreach ($item_row as $item) {
      $row[] = array('data' => theme('gallery_search_item', $item));
    }
    $rows[] = $row;
  }
  $output .= theme('table', array(), $rows);
  $output .= '</dl>';
  $output .= theme('pager', NULL, $items_per_row * $rows_per_pager, 0);

  return $output;
}

function theme_gallery_search_item($item) {
  $output  = ' <dt class="title"><a href="'. check_url($item['link']) .'">'. check_plain($item['title']) .'</a></dt>';
  $output .= '<div class="g2image_centered">'. $item['thumbnail'] .'</div>'; 
  $output .= ' <dd>'. ($item['snippet'] ? '<p>'. $item['snippet'] .'</p>' : '') .'</dd>';
  
  return $output;
}

?>