Updating instructions.
[project/gallery.git] / gallery_search.inc
CommitLineData
c15e9178 1<?php
2// $Id$
3/**
4 * gallery.module search functions
5 */
6
7/**
8 * implementation of hook_search
9 */
10function _gallery_search($op = 'search', $keys = null) {
11 switch ($op) {
12 case 'name':
13 return t('gallery');
14 case 'search':
15 $find = array();
16 $g2_head = array();
17
18 list ($success, $ret) = _gallery_init(true);
19 if (!$success) {
20 $err_msg = t('Unable to initialize embedded Gallery. You need to <a href="%link">
21 configure your embedded Gallery</a>.',
22 array('%link' => url('admin/settings/gallery')));
23 gallery_error($err_msg, $ret);
24 return;
25 }
26
27 // Image block options for search
28 $params['blocks'] = "specificItem";
29 $param_show_array = variable_get('gallery_search_block_show', array());
30 $params['show'] = is_array($param_show_array) ? implode('|', $param_show_array) : "";
31 $params['maxSize'] = variable_get('gallery_search_maxsize', 160);
32 $params['albumFrame'] = variable_get('gallery_search_album_frame', 'none');
33 $params['itemFrame'] = variable_get('gallery_search_item_frame', 'none');
34 $params['linkTarget'] = variable_get('gallery_search_link_target', '');
35 $show_g2_thumbs = variable_get('gallery_search_show_thumbs', 1);
36 $max_items = variable_get('gallery_search_max_items', 50);
37
38 list ($ret, $results) = GalleryEmbed::searchScan($keys, $max_items);
39 if (!$ret) {
40 /**
41 * Format of $results is:
42 * [GalleryCoreSearch] => Array(
43 [start] => 1
44 [end] => 13
45 [count] => 13
46 [results] => Array(
47 [0] => Array(
48 [itemId] => 46
49 [fields] => Array(
50 [0] => Array([key] => Title, [value] => DSCN2884.JPG)
51 [1] => Array([key] => Summary, [value] => )
52 [2] => Array([key] => Keywords, [value] => )
53 [3] => Array([key] => Description, [value] => )
54 [4] => Array([key] => Owner, [value] => Me )
55 )
56 )
57 [1] => ...
58 [name] => Gallery Core
59 )
60 [comment] => ...
61 [CustomField] => ...
62 [MultiLang] => ...
63 Other modules
64 )
65
66 * You can set the maximum number of items, but not a start offset, unfortunately.
67 */
68 $urlGenerator =& $GLOBALS['gallery']->getUrlGenerator();
69 // Copy the results to a new array, and overwrite the results portions to a new format
70 $find = $results;
71 $g2_thumb = '';
72 foreach ($results as $name => $module) {
73 if (count($module['results']) > 0) {
74 $this_module_results = array();
75 foreach ($module['results'] as $result) {
76 $excerpt = array();
77 $g2_thumb = '';
78 foreach ($result['fields'] as $field) {
79 if (preg_match("/$keys/i", $field['value'])) {
80 $excerpt[] = '<em>'.$field['key'] .':</em> '.
81 search_excerpt($keys, $field['value']);
82 }
83 }
84 $link = $urlGenerator->generateUrl(array('itemId' => $result['itemId']),
85 array('htmlEntities' => false));
86 if ($show_g2_thumbs) {
87 $params['itemId'] = $result['itemId'];
88 // No error checking. If it failed then no thumb is returned. Should be OK
89 // (maybe there is no thumb for that item)
90 list($ret, $g2_thumb, $head) = GalleryEmbed::getImageBlock($params);
91 if ($head) {
92 $g2_head[] = $head;
93 }
94 }
95 $this_module_results[] = array(
96 'title' => $result['fields'][0]['value'],
97 'link' => $link,
98 'type' => '',
99 'snippet' => implode('<br />', $excerpt),
100 'extra' => array(),
101 'g2_thumb' => $g2_thumb,
102 );
103 }
104 $find[$name]['results'] = $this_module_results;
105 }
106 }
107 // Try not to send the G2 head information multiple times
108 if ($g2_head) {
109 drupal_set_html_head(implode("\n", array_unique($g2_head)));
110 }
111 return $find;
112 }
113 }
114}
115
116/**
117 * Implementation of hook_search_page to override how to display the search results
118 */
119function _gallery_search_page($results) {
120 $num_items_per_row = variable_get('gallery_search_num_per_row', 3);
121 $max_rows_per_pager = variable_get('gallery_search_max_rows_per_pager', 5);
122 $max_items = variable_get('gallery_search_max_items', 50);
123 $header = array_fill(0, $num_items_per_row, '');
124
125 $output = '<dl class="search-results">';
126
127 foreach ($results as $entry) {
128 $output .= gallery_search_item($entry);
129 }
130 $output .= '</dl>';
131
132
133/**
134 * From /drupal/includes/pager.inc
135 * $pager_page_array[i] = page number (starts at 0) (eg 7)
136 * $pager_total[i] = total number of pages = ceil($pager_total_items[i] / limit) (eg 15)
137 * $pager_total_items[i] = total number of items in the pager (eg 75)
138 * where i is the pager ID (must be a number!!!) (for multiple pagers on a page)
139 * eg ?page=1,2,1
140 * => page 2 for pager1, page 3 for pager2, page 2 for pager 3
141 */
142 global $pager_page_array, $pager_total, $pager_total_items;
143 $page = isset($_GET['page']) ? $_GET['page'] : '';
144 $pager_page_array = explode(',', $page);
145
146 $output = '';
147 $element = 0;
148 // Split the search results from each module into individual pagers
149 foreach ($results as $module => $table) {
150 $search_items = array();
151 $row = 0;
152 $col = 0;
153 foreach ($table['results'] as $key => $item){
154 // Format the search results
155 $search_items[$row][$col++] = gallery_search_item($item);
156 if ($col >= $num_items_per_row) {
157 $row++;
158 $col = 0;
159 }
160 }
161 // Add the title for that pager
162 $output .= '<h2>';
163 $count = $table['count'];
164 if ($count > $max_items) {
165 $count = $max_items;
166 $count_text = t(' (>%count items found)', array(
167 '%count' => $count));
168 } else {
169 $count_text = t(' (%count %item found)', array(
170 '%item' => ($count == 1) ? t('item') : t('items'),
171 '%count' => $count));
172 }
173 $output .= t('%name Results', array('%name' => $table['name']));
174 $output .= $count_text . '</h2>';
175
176 if ($count > 0) {
177 $pager_total_items[$element] = ceil($count/ $num_items_per_row);
178 $pager_total[$element] = ceil($pager_total_items[$element] / $max_rows_per_pager);
179 $first_item = $pager_page_array[$element] * $max_rows_per_pager;
180 // Slice the array to display only the results for this page of the pager
181 $search_items = array_slice($search_items, $first_item, $max_rows_per_pager);
182 // Add empty cells if needed to complete the last row
183 $last_row = count($search_items) - 1;
184 if (count($search_items[$last_row]) < $num_items_per_row) {
185 $search_items[$last_row] = array_merge(
186 $search_items[$last_row],
187 array_fill(0, $num_items_per_row - count($search_items[$last_row]), ''));
188 }
189
190 $output .= theme('table', $header, $search_items);
191 $output .= theme('pager', NULL, $max_rows_per_pager, $element);
192 $element++;
193 }
194 }
195 return $output;
196}
197
198function gallery_search_item($item) {
199 $output = '';
200 if ($item['g2_thumb']) {
201 $output .= ' <dd>' . $item['g2_thumb'] . '</dd>';
202 } else {
203 $output = '<dt class="title"><a href="'. check_url($item['link']) .'">'.
204 check_plain($item['title']) .'</a></dt>';
205 }
206
207 $info = array();
208 if ($item['type']) {
209 $info[] = $item['type'];
210 }
211 if ($item['user']) {
212 $info[] = $item['user'];
213 }
214 if ($item['date']) {
215 $info[] = format_date($item['date'], 'small');
216 }
217 if (is_array($item['extra'])) {
218 $info = array_merge($info, $item['extra']);
219 }
220 $output .= ' <dd>'. ($item['snippet'] ? '<p>'. $item['snippet'] . '</p>' : '') .
221 '<p class="search-info">' . implode(' - ', $info) .'</p></dd>';
222
223 return $output;
224}
225
226?>