- task: increase size of 'link' field for blocks
[project/gallery.git] / gallery_search.inc
1 <?php
2 // $Id$
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 function _gallery_search_pager_search(&$keys, $limit = 10, $element = 0) {
79 // adapted version of the pager_query() function (from /includes/pager.inc)
80 // for use with the Gallery2 search() function
81 //
82 global $pager_page_array, $pager_total, $pager_total_items;
83 $page = isset($_GET['page']) ? $_GET['page'] : '';
84
85 // convert comma-separated $page to an array, used by other functions.
86 $pager_page_array = explode(',', $page);
87
88 // we calculate the total of pages as ceil(items / limit).
89 $count = _gallery_search_perform($keys);
90 $pager_total_items[$element] = $count['count'];
91 $pager_total[$element] = ceil($pager_total_items[$element] / $limit);
92 $pager_page_array[$element] = max(0, min((int)$pager_page_array[$element], ((int)$pager_total[$element]) - 1));
93
94 return _gallery_search_perform($keys, $pager_page_array[$element] * $limit, $limit);
95 }
96
97 function _gallery_search_perform(&$keys, $offset = 0, $limit = -1) {
98 list($search_interface, $options) = _gallery_search_init();
99 if (!$search_interface) {
100 return array();
101 }
102 // extract query parameters
103 if ($fields = search_query_extract($keys, 'fields')) {
104 $keys = trim(preg_replace('/\s+fields:[\w,]*/', '', $keys));
105 }
106 $fields = $fields ? array_flip(explode(',', $fields)) : $options;
107 foreach ($fields as $key => $value) {
108 $fields[$key] = $key;
109 }
110 // perform the actual search
111 list($ret, $matches) = $search_interface->search($fields, $keys, $offset, $limit);
112
113 return $ret ? array() : $matches;
114 }
115
116 function _gallery_search_form(&$form) {
117 list($search_interface, $options) = _gallery_search_init();
118 if (!count($options)) {
119 return;
120 }
121 // extend search form
122 $form['advanced'] = array(
123 '#type' => 'fieldset',
124 '#title' => t('Advanced search'),
125 '#collapsible' => TRUE,
126 '#collapsed' => TRUE,
127 '#attributes' => array('class' => 'search-advanced'),
128 );
129
130 $form['advanced']['fields'] = array(
131 '#type' => 'checkboxes',
132 '#title' => t('Only the following fields'),
133 '#prefix' => '<div class="criterion">',
134 '#suffix' => '</div>',
135 '#options' => $options,
136 );
137
138 $form['advanced']['submit'] = array(
139 '#type' => 'submit',
140 '#value' => t('Advanced search'),
141 '#prefix' => '<div class="action clear-block">',
142 '#suffix' => '</div>',
143 );
144
145 $form['#validate']['_gallery_search_validate'] = array();
146 }
147
148 function _gallery_search_validate($form_id, $form_values, $form) {
149 $keys = $form_values['processed_keys'];
150 // append field options to query
151 if (isset($form_values['fields']) && is_array($form_values['fields'])) {
152 $form_values['fields'] = array_filter($form_values['fields']);
153 if (count($form_values['fields'])) {
154 $keys = search_query_insert($keys, 'fields', implode(',', array_keys($form_values['fields'])));
155 form_set_value($form['basic']['inline']['processed_keys'], trim($keys));
156 }
157 }
158 }
159
160 function _gallery_search_init() {
161 // init gallery
162 if (!_gallery_init(TRUE)) {
163 return array();
164 }
165 // create instance of search interface
166 list($ret, $search_interface) = GalleryCoreApi::newFactoryInstance('GallerySearchInterface_1_0');
167 if ($ret) {
168 gallery_error(t('Error creating instance of GallerySearchInterface. Make sure the \'Search\' plugin is enabled in Gallery2.'), $ret);
169 return array(NULL, array());
170 }
171 // get search module info
172 list($ret, $module_info) = $search_interface->getSearchModuleInfo();
173 if ($ret) {
174 gallery_error(t('Error getting \'Search\' module options.'), $ret);
175 return array(NULL, array());
176 }
177 $options = array();
178 foreach ($module_info['options'] as $module => $info) {
179 if ($info['enabled']) {
180 $options[$module] = $info['description'];
181 }
182 }
183
184 return array($search_interface, $options);
185 }
186
187 function _gallery_search_page($results) {
188 $items_per_row = variable_get('gallery_search_num_per_row', 3);
189 $rows_per_pager = variable_get('gallery_search_rows_per_pager', 4);
190
191 $output = '<dl class="search-results">';
192 $output .= t('<p>Total Number of Matches: @count</p>', array('@count' => $results['count']));
193 unset($results['count']);
194 // arrange items as table
195 $rows = array();
196 $results = array_chunk($results, $items_per_row);
197 foreach ($results as $item_row) {
198 $row = array();
199 foreach ($item_row as $item) {
200 $row[] = array('data' => theme('gallery_search_item', $item));
201 }
202 $rows[] = $row;
203 }
204 $output .= theme('table', array(), $rows);
205 $output .= '</dl>';
206 $output .= theme('pager', NULL, $items_per_row * $rows_per_pager, 0);
207
208 return $output;
209 }
210
211 function theme_gallery_search_item($item) {
212 $output = ' <dt class="title"><a href="'. check_url($item['link']) .'">'. check_plain($item['title']) .'</a></dt>';
213 $output .= '<div class="g2image_centered">'. $item['thumbnail'] .'</div>';
214 $output .= ' <dd>'. ($item['snippet'] ? '<p>'. $item['snippet'] .'</p>' : '') .'</dd>';
215
216 return $output;
217 }
218
219 ?>