1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | PhpWebGallery - a PHP based picture gallery | |
---|
4 | // | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net | |
---|
5 | // | Copyright (C) 2003-2005 PhpWebGallery Team - http://phpwebgallery.net | |
---|
6 | // +-----------------------------------------------------------------------+ |
---|
7 | // | branch : BSF (Best So Far) |
---|
8 | // | file : $Id: functions_category.inc.php 1651 2006-12-13 00:05:16Z rub $ |
---|
9 | // | last update : $Date: 2006-12-13 00:05:16 +0000 (Wed, 13 Dec 2006) $ |
---|
10 | // | last modifier : $Author: rub $ |
---|
11 | // | revision : $Revision: 1651 $ |
---|
12 | // +-----------------------------------------------------------------------+ |
---|
13 | // | This program is free software; you can redistribute it and/or modify | |
---|
14 | // | it under the terms of the GNU General Public License as published by | |
---|
15 | // | the Free Software Foundation | |
---|
16 | // | | |
---|
17 | // | This program is distributed in the hope that it will be useful, but | |
---|
18 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
19 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
20 | // | General Public License for more details. | |
---|
21 | // | | |
---|
22 | // | You should have received a copy of the GNU General Public License | |
---|
23 | // | along with this program; if not, write to the Free Software | |
---|
24 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
25 | // | USA. | |
---|
26 | // +-----------------------------------------------------------------------+ |
---|
27 | |
---|
28 | /** |
---|
29 | * Provides functions to handle categories. |
---|
30 | * |
---|
31 | * |
---|
32 | */ |
---|
33 | |
---|
34 | /** |
---|
35 | * Is the category accessible to the connected user ? |
---|
36 | * |
---|
37 | * Note : if the user is not authorized to see this category, page creation |
---|
38 | * ends (exit command in this function) |
---|
39 | * |
---|
40 | * @param int category id to verify |
---|
41 | * @return void |
---|
42 | */ |
---|
43 | function check_restrictions($category_id) |
---|
44 | { |
---|
45 | global $user; |
---|
46 | |
---|
47 | if (in_array($category_id, explode(',', $user['forbidden_categories']))) |
---|
48 | { |
---|
49 | access_denied(); |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | function get_categories_menu() |
---|
54 | { |
---|
55 | global $page, $user; |
---|
56 | |
---|
57 | $query = ' |
---|
58 | SELECT '; |
---|
59 | // From CATEGORIES_TABLE |
---|
60 | $query.= ' |
---|
61 | name, id, nb_images, global_rank,'; |
---|
62 | // From USER_CACHE_CATEGORIES_TABLE |
---|
63 | $query.= ' |
---|
64 | date_last, max_date_last, count_images, count_categories'; |
---|
65 | |
---|
66 | // $user['forbidden_categories'] including with USER_CACHE_CATEGORIES_TABLE |
---|
67 | $query.= ' |
---|
68 | FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.' |
---|
69 | ON id = cat_id and user_id = '.$user['id']; |
---|
70 | if ($page['filter_local_mode']) |
---|
71 | { |
---|
72 | $query.= ' |
---|
73 | where max_date_last > SUBDATE( |
---|
74 | CURRENT_DATE,INTERVAL '.$user['recent_period'].' DAY)'; |
---|
75 | } |
---|
76 | else |
---|
77 | { |
---|
78 | // Always expand when filter_local_mode is activated |
---|
79 | if (!$user['expand'] and !$user['filter_global_mode']) |
---|
80 | { |
---|
81 | $query.= ' |
---|
82 | WHERE (id_uppercat is NULL'; |
---|
83 | if (isset($page['category'])) |
---|
84 | { |
---|
85 | $query.= ' OR id_uppercat IN ('.$page['uppercats'].')'; |
---|
86 | } |
---|
87 | $query.= ')'; |
---|
88 | } |
---|
89 | } |
---|
90 | $query.= ' |
---|
91 | ;'; |
---|
92 | |
---|
93 | $result = pwg_query($query); |
---|
94 | $cats = array(); |
---|
95 | while ($row = mysql_fetch_assoc($result)) |
---|
96 | { |
---|
97 | array_push($cats, $row); |
---|
98 | } |
---|
99 | usort($cats, 'global_rank_compare'); |
---|
100 | |
---|
101 | return get_html_menu_category($cats); |
---|
102 | } |
---|
103 | |
---|
104 | |
---|
105 | /** |
---|
106 | * Retrieve informations about a category in the database |
---|
107 | * |
---|
108 | * Returns an array with following keys : |
---|
109 | * |
---|
110 | * - comment |
---|
111 | * - dir : directory, might be empty for virtual categories |
---|
112 | * - name : an array with indexes from 0 (lowest cat name) to n (most |
---|
113 | * uppercat name findable) |
---|
114 | * - nb_images |
---|
115 | * - id_uppercat |
---|
116 | * - site_id |
---|
117 | * - |
---|
118 | * |
---|
119 | * @param int category id |
---|
120 | * @return array |
---|
121 | */ |
---|
122 | function get_cat_info( $id ) |
---|
123 | { |
---|
124 | $infos = array('nb_images','id_uppercat','comment','site_id' |
---|
125 | ,'dir','date_last','uploadable','status','visible' |
---|
126 | ,'representative_picture_id','uppercats','commentable' |
---|
127 | ,'image_order'); |
---|
128 | |
---|
129 | $query = ' |
---|
130 | SELECT '.implode(',', $infos).' |
---|
131 | FROM '.CATEGORIES_TABLE.' |
---|
132 | WHERE id = '.$id.' |
---|
133 | ;'; |
---|
134 | $row = mysql_fetch_array(pwg_query($query)); |
---|
135 | if (empty($row)) |
---|
136 | return null; |
---|
137 | |
---|
138 | $cat = array(); |
---|
139 | foreach ($infos as $info) |
---|
140 | { |
---|
141 | if (isset($row[$info])) |
---|
142 | { |
---|
143 | $cat[$info] = $row[$info]; |
---|
144 | } |
---|
145 | else |
---|
146 | { |
---|
147 | $cat[$info] = ''; |
---|
148 | } |
---|
149 | // If the field is true or false, the variable is transformed into a |
---|
150 | // boolean value. |
---|
151 | if ($cat[$info] == 'true' or $cat[$info] == 'false') |
---|
152 | { |
---|
153 | $cat[$info] = get_boolean( $cat[$info] ); |
---|
154 | } |
---|
155 | } |
---|
156 | global $conf; |
---|
157 | if ( !( $conf['allow_html_descriptions'] and |
---|
158 | preg_match('/<(div|br|img|script).*>/i', $cat['comment']) ) ) |
---|
159 | { |
---|
160 | $cat['comment'] = nl2br($cat['comment']); |
---|
161 | } |
---|
162 | |
---|
163 | $names = array(); |
---|
164 | $query = ' |
---|
165 | SELECT name,id |
---|
166 | FROM '.CATEGORIES_TABLE.' |
---|
167 | WHERE id IN ('.$cat['uppercats'].') |
---|
168 | ;'; |
---|
169 | $result = pwg_query($query); |
---|
170 | while($row = mysql_fetch_array($result)) |
---|
171 | { |
---|
172 | $names[$row['id']] = $row['name']; |
---|
173 | } |
---|
174 | |
---|
175 | // category names must be in the same order than uppercats list |
---|
176 | $cat['name'] = array(); |
---|
177 | foreach (explode(',', $cat['uppercats']) as $cat_id) |
---|
178 | { |
---|
179 | $cat['name'][$cat_id] = $names[$cat_id]; |
---|
180 | } |
---|
181 | |
---|
182 | return $cat; |
---|
183 | } |
---|
184 | |
---|
185 | // get_complete_dir returns the concatenation of get_site_url and |
---|
186 | // get_local_dir |
---|
187 | // Example : "pets > rex > 1_year_old" is on the the same site as the |
---|
188 | // PhpWebGallery files and this category has 22 for identifier |
---|
189 | // get_complete_dir(22) returns "./galleries/pets/rex/1_year_old/" |
---|
190 | function get_complete_dir( $category_id ) |
---|
191 | { |
---|
192 | return get_site_url($category_id).get_local_dir($category_id); |
---|
193 | } |
---|
194 | |
---|
195 | // get_local_dir returns an array with complete path without the site url |
---|
196 | // Example : "pets > rex > 1_year_old" is on the the same site as the |
---|
197 | // PhpWebGallery files and this category has 22 for identifier |
---|
198 | // get_local_dir(22) returns "pets/rex/1_year_old/" |
---|
199 | function get_local_dir( $category_id ) |
---|
200 | { |
---|
201 | global $page; |
---|
202 | |
---|
203 | $uppercats = ''; |
---|
204 | $local_dir = ''; |
---|
205 | |
---|
206 | if ( isset( $page['plain_structure'][$category_id]['uppercats'] ) ) |
---|
207 | { |
---|
208 | $uppercats = $page['plain_structure'][$category_id]['uppercats']; |
---|
209 | } |
---|
210 | else |
---|
211 | { |
---|
212 | $query = 'SELECT uppercats'; |
---|
213 | $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id = '.$category_id; |
---|
214 | $query.= ';'; |
---|
215 | $row = mysql_fetch_array( pwg_query( $query ) ); |
---|
216 | $uppercats = $row['uppercats']; |
---|
217 | } |
---|
218 | |
---|
219 | $upper_array = explode( ',', $uppercats ); |
---|
220 | |
---|
221 | $database_dirs = array(); |
---|
222 | $query = 'SELECT id,dir'; |
---|
223 | $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id IN ('.$uppercats.')'; |
---|
224 | $query.= ';'; |
---|
225 | $result = pwg_query( $query ); |
---|
226 | while( $row = mysql_fetch_array( $result ) ) |
---|
227 | { |
---|
228 | $database_dirs[$row['id']] = $row['dir']; |
---|
229 | } |
---|
230 | foreach ($upper_array as $id) |
---|
231 | { |
---|
232 | $local_dir.= $database_dirs[$id].'/'; |
---|
233 | } |
---|
234 | |
---|
235 | return $local_dir; |
---|
236 | } |
---|
237 | |
---|
238 | // retrieving the site url : "http://domain.com/gallery/" or |
---|
239 | // simply "./galleries/" |
---|
240 | function get_site_url($category_id) |
---|
241 | { |
---|
242 | global $page; |
---|
243 | |
---|
244 | $query = ' |
---|
245 | SELECT galleries_url |
---|
246 | FROM '.SITES_TABLE.' AS s,'.CATEGORIES_TABLE.' AS c |
---|
247 | WHERE s.id = c.site_id |
---|
248 | AND c.id = '.$category_id.' |
---|
249 | ;'; |
---|
250 | $row = mysql_fetch_array(pwg_query($query)); |
---|
251 | return $row['galleries_url']; |
---|
252 | } |
---|
253 | |
---|
254 | // returns an array of image orders available for users/visitors |
---|
255 | function get_category_preferred_image_orders() |
---|
256 | { |
---|
257 | global $conf; |
---|
258 | return array( |
---|
259 | array(l10n('default_sort'), '', true), |
---|
260 | array(l10n('Average rate'), 'average_rate DESC', $conf['rate']), |
---|
261 | array(l10n('most_visited_cat'), 'hit DESC', true), |
---|
262 | array(l10n('Creation date'), 'date_creation DESC', true), |
---|
263 | array(l10n('Post date'), 'date_available DESC', true), |
---|
264 | array(l10n('File name'), 'file ASC', true) |
---|
265 | ); |
---|
266 | } |
---|
267 | |
---|
268 | function display_select_categories($categories, |
---|
269 | $selecteds, |
---|
270 | $blockname, |
---|
271 | $fullname = true) |
---|
272 | { |
---|
273 | global $template; |
---|
274 | |
---|
275 | foreach ($categories as $category) |
---|
276 | { |
---|
277 | $selected = ''; |
---|
278 | if (in_array($category['id'], $selecteds)) |
---|
279 | { |
---|
280 | $selected = ' selected="selected"'; |
---|
281 | } |
---|
282 | |
---|
283 | if ($fullname) |
---|
284 | { |
---|
285 | $option = get_cat_display_name_cache($category['uppercats'], |
---|
286 | null, |
---|
287 | false); |
---|
288 | } |
---|
289 | else |
---|
290 | { |
---|
291 | $option = str_repeat(' ', |
---|
292 | (3 * substr_count($category['global_rank'], '.'))); |
---|
293 | $option.= '- '.$category['name']; |
---|
294 | } |
---|
295 | |
---|
296 | $template->assign_block_vars( |
---|
297 | $blockname, |
---|
298 | array('SELECTED'=>$selected, |
---|
299 | 'VALUE'=>$category['id'], |
---|
300 | 'OPTION'=>$option |
---|
301 | )); |
---|
302 | } |
---|
303 | } |
---|
304 | |
---|
305 | function display_select_cat_wrapper($query, $selecteds, $blockname, |
---|
306 | $fullname = true) |
---|
307 | { |
---|
308 | $result = pwg_query($query); |
---|
309 | $categories = array(); |
---|
310 | if (!empty($result)) |
---|
311 | { |
---|
312 | while ($row = mysql_fetch_array($result)) |
---|
313 | { |
---|
314 | array_push($categories, $row); |
---|
315 | } |
---|
316 | } |
---|
317 | usort($categories, 'global_rank_compare'); |
---|
318 | display_select_categories($categories, $selecteds, $blockname, $fullname); |
---|
319 | } |
---|
320 | |
---|
321 | /** |
---|
322 | * returns all subcategory identifiers of given category ids |
---|
323 | * |
---|
324 | * @param array ids |
---|
325 | * @return array |
---|
326 | */ |
---|
327 | function get_subcat_ids($ids) |
---|
328 | { |
---|
329 | $query = ' |
---|
330 | SELECT DISTINCT(id) |
---|
331 | FROM '.CATEGORIES_TABLE.' |
---|
332 | WHERE '; |
---|
333 | foreach ($ids as $num => $category_id) |
---|
334 | { |
---|
335 | if ($num > 0) |
---|
336 | { |
---|
337 | $query.= ' |
---|
338 | OR '; |
---|
339 | } |
---|
340 | $query.= 'uppercats REGEXP \'(^|,)'.$category_id.'(,|$)\''; |
---|
341 | } |
---|
342 | $query.= ' |
---|
343 | ;'; |
---|
344 | $result = pwg_query($query); |
---|
345 | |
---|
346 | $subcats = array(); |
---|
347 | while ($row = mysql_fetch_array($result)) |
---|
348 | { |
---|
349 | array_push($subcats, $row['id']); |
---|
350 | } |
---|
351 | return $subcats; |
---|
352 | } |
---|
353 | |
---|
354 | function global_rank_compare($a, $b) |
---|
355 | { |
---|
356 | return strnatcasecmp($a['global_rank'], $b['global_rank']); |
---|
357 | } |
---|
358 | |
---|
359 | function rank_compare($a, $b) |
---|
360 | { |
---|
361 | if ($a['rank'] == $b['rank']) |
---|
362 | { |
---|
363 | return 0; |
---|
364 | } |
---|
365 | |
---|
366 | return ($a['rank'] < $b['rank']) ? -1 : 1; |
---|
367 | } |
---|
368 | |
---|
369 | /** |
---|
370 | * returns display text for information images of category |
---|
371 | * |
---|
372 | * @param array categories |
---|
373 | * @return string |
---|
374 | */ |
---|
375 | function get_display_images_count($cat_nb_images, $cat_count_images, $cat_count_categories, $short_message = true) |
---|
376 | { |
---|
377 | $display_text = ''; |
---|
378 | |
---|
379 | // Count of category is main |
---|
380 | // if not picture on categorie, test on sub-categories |
---|
381 | $count = ($cat_nb_images > 0 ? $cat_nb_images : $cat_count_images); |
---|
382 | |
---|
383 | if ($count > 0) |
---|
384 | { |
---|
385 | $display_text.= l10n_dec('image_available', 'images_available', $count); |
---|
386 | |
---|
387 | if ($cat_nb_images > 0) |
---|
388 | { |
---|
389 | if (! $short_message) |
---|
390 | { |
---|
391 | $display_text.= ' '.l10n('images_available_cpl'); |
---|
392 | } |
---|
393 | } |
---|
394 | else |
---|
395 | { |
---|
396 | $display_text.= ' '.l10n_dec('images_available_cat', 'images_available_cats', $cat_count_categories); |
---|
397 | } |
---|
398 | } |
---|
399 | |
---|
400 | return $display_text; |
---|
401 | } |
---|
402 | |
---|
403 | ?> |
---|