/[drupal]/contributions/modules/gallery/gallery_search.inc
ViewVC logotype

Contents of /contributions/modules/gallery/gallery_search.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.4 - (show annotations) (download) (as text)
Fri Nov 23 11:20:33 2007 UTC (2 years ago) by profix898
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1
Changes since 1.3: +20 -18 lines
File MIME type: text/x-php
- very early D6 version (Oct-01, 2007)
1 <?php
2 // $Id: gallery_search.inc,v 1.2.2.8 2007/09/10 18:04:08 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 $html_head = 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',
29 GALLERY_SEARCH_SIZE_METHOD_DEFAULT) == 'maxsize') {
30 $params['maxSize'] = variable_get('gallery_search_size', GALLERY_SEARCH_SIZE_DEFAULT);
31 }
32 else {
33 $params['exactSize'] = variable_get('gallery_search_size', GALLERY_SEARCH_SIZE_DEFAULT);
34 }
35 $params['albumFrame'] = variable_get('gallery_search_album_frame', 'none');
36 $params['itemFrame'] = variable_get('gallery_search_item_frame', 'none');
37 $params['linkTarget'] = variable_get('gallery_search_link_target', '');
38 $params['link'] = variable_get('gallery_search_link', '');
39
40 $show_thumbs = variable_get('gallery_search_show_thumbs', 1);
41 // loop over the results
42 foreach ($matches['results'] as $item) {
43 $excerpt = array();
44 // get a thumbnail for this item
45 if ($show_thumbs) {
46 $params['itemId'] = $item['itemId'];
47 list($ret, $thumbnail, $head) = GalleryEmbed::getImageBlock($params);
48 if ($ret) {
49 $thumbnail = t('n/a');
50 }
51 if ($head) {
52 $html_head[] = $head;
53 }
54 }
55 // generate a snippet with highlighted search keys
56 foreach ($item['fields'] as $field) {
57 if (preg_match("/$keys/i", $field['value'])) {
58 $excerpt[] = '<em>'. $field['key'] .':</em> '. search_excerpt($keys, $field['value']);
59 }
60 }
61 // put everything into the $results array
62 $title = reset($item['fields']);
63 $results[] = array(
64 'link' => gallery_generate_url(array('itemId' => $item['itemId']), FALSE),
65 'title' => empty($title['value']) ? t('Gallery item: Untitled') : $title['value'],
66 'snippet' => implode('<br />', $excerpt),
67 'thumbnail' => $thumbnail,
68 );
69 }
70 }
71 if ($html_head) {
72 drupal_set_html_head(implode("\n", array_unique($html_head)));
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 (!$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) {
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'] = array();
159 }
160
161 /**
162 * Function _gallery_search_validate().
163 */
164 function _gallery_search_validate($form_id, $form_values, $form) {
165 $keys = $form_values['processed_keys'];
166 // append field options to query
167 if (isset($form_values['fields']) && is_array($form_values['fields'])) {
168 $form_values['fields'] = array_filter($form_values['fields']);
169 if (count($form_values['fields'])) {
170 $keys = search_query_insert($keys, 'fields', implode(',', array_keys($form_values['fields'])));
171 form_set_value($form['basic']['inline']['processed_keys'], trim($keys));
172 }
173 }
174 }
175
176 /**
177 * Function _gallery_search_init().
178 */
179 function _gallery_search_init() {
180 // init gallery
181 if (!_gallery_init(TRUE)) {
182 return array();
183 }
184 // create instance of search interface
185 list($ret, $search_interface) = GalleryCoreApi::newFactoryInstance('GallerySearchInterface_1_0');
186 if ($ret) {
187 gallery_error(t('Error creating instance of GallerySearchInterface. Make sure the \'Search\' plugin is enabled in Gallery2.'), $ret);
188 return array(NULL, array());
189 }
190 // get search module info
191 list($ret, $module_info) = $search_interface->getSearchModuleInfo();
192 if ($ret) {
193 gallery_error(t('Error getting \'Search\' module options.'), $ret);
194 return array(NULL, array());
195 }
196 $options = array();
197 foreach ($module_info['options'] as $module => $info) {
198 if ($info['enabled']) {
199 $options[$module] = $info['description'];
200 }
201 }
202
203 return array($search_interface, $options);
204 }
205
206 /**
207 * Function _gallery_search_page().
208 */
209 function _gallery_search_page($results) {
210 $items_per_row = variable_get('gallery_search_num_per_row', 3);
211 $rows_per_pager = variable_get('gallery_search_rows_per_pager', 4);
212
213 $output = '<dl class="search-results">';
214 $output .= t('<p>Total Number of Matches: @count</p>', array('@count' => $results['count']));
215 unset($results['count']);
216 // arrange items as table
217 $rows = array();
218 $results = array_chunk($results, $items_per_row);
219 foreach ($results as $item_row) {
220 $row = array();
221 foreach ($item_row as $item) {
222 $row[] = array('data' => theme('gallery_search_item', $item));
223 }
224 $rows[] = $row;
225 }
226 $output .= theme('table', array(), $rows);
227 $output .= '</dl>';
228 $output .= theme('pager', NULL, $items_per_row * $rows_per_pager, 0);
229
230 return $output;
231 }
232
233 /**
234 * Theme function : theme_gallery_search_item().
235 */
236 function theme_gallery_search_item($item) {
237 $output = ' <dt class="title"><a href="'. check_url($item['link']) .'">'. check_plain($item['title']) .'</a></dt>';
238 $output .= '<div class="g2image_centered">'. $item['thumbnail'] .'</div>';
239 $output .= ' <dd>'. ($item['snippet'] ? '<p>'. $item['snippet'] .'</p>' : '') .'</dd>';
240
241 return $output;
242 }

  ViewVC Help
Powered by ViewVC 1.1.2