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 : $RCSfile$ |
---|
9 | // | last update : $Date: 2005-09-18 21:09:44 +0000 (Sun, 18 Sep 2005) $ |
---|
10 | // | last modifier : $Author: plg $ |
---|
11 | // | revision : $Revision: 867 $ |
---|
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, $lang; |
---|
46 | |
---|
47 | if (in_array($category_id, explode(',', $user['forbidden_categories']))) |
---|
48 | { |
---|
49 | echo '<div style="text-align:center;">'.$lang['access_forbiden'].'<br />'; |
---|
50 | echo '<a href="'.add_session_id( './category.php' ).'">'; |
---|
51 | echo $lang['thumbnails'].'</a></div>'; |
---|
52 | exit(); |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | /** |
---|
57 | * Checks whether the argument is a right parameter category id |
---|
58 | * |
---|
59 | * The argument is a right parameter if corresponds to one of these : |
---|
60 | * |
---|
61 | * - is numeric and corresponds to a category in the database |
---|
62 | * - equals 'fav' (for favorites) |
---|
63 | * - equals 'search' (when the result of a search is displayed) |
---|
64 | * - equals 'most_visited' |
---|
65 | * - equals 'best_rated' |
---|
66 | * - equals 'recent_pics' |
---|
67 | * - equals 'recent_cats' |
---|
68 | * - equals 'calendar' |
---|
69 | * - equals 'list' |
---|
70 | * |
---|
71 | * The function fills the global var $page['cat'] and returns nothing |
---|
72 | * |
---|
73 | * @param mixed category id or special category name |
---|
74 | * @return void |
---|
75 | */ |
---|
76 | function check_cat_id( $cat ) |
---|
77 | { |
---|
78 | global $page; |
---|
79 | |
---|
80 | unset( $page['cat'] ); |
---|
81 | if ( isset( $cat ) ) |
---|
82 | { |
---|
83 | if ( isset( $page['plain_structure'][$cat] ) ) |
---|
84 | { |
---|
85 | $page['cat'] = $cat; |
---|
86 | } |
---|
87 | else if ( is_numeric( $cat ) ) |
---|
88 | { |
---|
89 | $query = 'SELECT id'; |
---|
90 | $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id = '.$cat.';'; |
---|
91 | $result = pwg_query( $query ); |
---|
92 | if ( mysql_num_rows( $result ) != 0 ) |
---|
93 | { |
---|
94 | $page['cat'] = $cat; |
---|
95 | } |
---|
96 | } |
---|
97 | if ( $cat == 'fav' |
---|
98 | or $cat == 'most_visited' |
---|
99 | or $cat == 'best_rated' |
---|
100 | or $cat == 'recent_pics' |
---|
101 | or $cat == 'recent_cats' |
---|
102 | or $cat == 'calendar' ) |
---|
103 | { |
---|
104 | $page['cat'] = $cat; |
---|
105 | } |
---|
106 | if ($cat == 'search' and isset($_GET['search'])) |
---|
107 | { |
---|
108 | $page['cat'] = $cat; |
---|
109 | } |
---|
110 | if ($cat == 'list' |
---|
111 | and isset($_GET['list']) |
---|
112 | and preg_match('/^\d+(,\d+)*$/', $_GET['list'])) |
---|
113 | { |
---|
114 | $page['cat'] = 'list'; |
---|
115 | } |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | function get_categories_menu() |
---|
120 | { |
---|
121 | global $page,$user; |
---|
122 | |
---|
123 | $infos = array(''); |
---|
124 | |
---|
125 | $query = ' |
---|
126 | SELECT name,id,date_last,nb_images,global_rank |
---|
127 | FROM '.CATEGORIES_TABLE.' |
---|
128 | WHERE 1 = 1'; // stupid but permit using AND after it ! |
---|
129 | if (!$user['expand']) |
---|
130 | { |
---|
131 | $query.= ' |
---|
132 | AND (id_uppercat is NULL'; |
---|
133 | if (isset ($page['tab_expand']) and count($page['tab_expand']) > 0) |
---|
134 | { |
---|
135 | $query.= ' OR id_uppercat IN ('.implode(',',$page['tab_expand']).')'; |
---|
136 | } |
---|
137 | $query.= ')'; |
---|
138 | } |
---|
139 | if ($user['forbidden_categories'] != '') |
---|
140 | { |
---|
141 | $query.= ' |
---|
142 | AND id NOT IN ('.$user['forbidden_categories'].')'; |
---|
143 | } |
---|
144 | $query.= ' |
---|
145 | ;'; |
---|
146 | |
---|
147 | $result = pwg_query($query); |
---|
148 | $cats = array(); |
---|
149 | while ($row = mysql_fetch_array($result)) |
---|
150 | { |
---|
151 | array_push($cats, $row); |
---|
152 | } |
---|
153 | usort($cats, 'global_rank_compare'); |
---|
154 | |
---|
155 | return get_html_menu_category($cats); |
---|
156 | } |
---|
157 | |
---|
158 | /** |
---|
159 | * returns the total number of elements viewable in the gallery by the |
---|
160 | * connected user |
---|
161 | * |
---|
162 | * @return int |
---|
163 | */ |
---|
164 | function count_user_total_images() |
---|
165 | { |
---|
166 | global $user; |
---|
167 | |
---|
168 | $query = ' |
---|
169 | SELECT COUNT(DISTINCT(image_id)) as total |
---|
170 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
171 | WHERE category_id NOT IN ('.$user['forbidden_categories'].') |
---|
172 | ;'; |
---|
173 | list($total) = mysql_fetch_array(pwg_query($query)); |
---|
174 | |
---|
175 | return $total; |
---|
176 | } |
---|
177 | |
---|
178 | /** |
---|
179 | * Retrieve informations about a category in the database |
---|
180 | * |
---|
181 | * Returns an array with following keys : |
---|
182 | * |
---|
183 | * - comment |
---|
184 | * - dir : directory, might be empty for virtual categories |
---|
185 | * - name : an array with indexes from 0 (lowest cat name) to n (most |
---|
186 | * uppercat name findable) |
---|
187 | * - nb_images |
---|
188 | * - id_uppercat |
---|
189 | * - site_id |
---|
190 | * - |
---|
191 | * |
---|
192 | * @param int category id |
---|
193 | * @return array |
---|
194 | */ |
---|
195 | function get_cat_info( $id ) |
---|
196 | { |
---|
197 | $infos = array('nb_images','id_uppercat','comment','site_id' |
---|
198 | ,'dir','date_last','uploadable','status','visible' |
---|
199 | ,'representative_picture_id','uppercats','commentable'); |
---|
200 | |
---|
201 | $query = ' |
---|
202 | SELECT '.implode(',', $infos).' |
---|
203 | FROM '.CATEGORIES_TABLE.' |
---|
204 | WHERE id = '.$id.' |
---|
205 | ;'; |
---|
206 | $row = mysql_fetch_array(pwg_query($query)); |
---|
207 | |
---|
208 | $cat = array(); |
---|
209 | foreach ($infos as $info) |
---|
210 | { |
---|
211 | if (isset($row[$info])) |
---|
212 | { |
---|
213 | $cat[$info] = $row[$info]; |
---|
214 | } |
---|
215 | else |
---|
216 | { |
---|
217 | $cat[$info] = ''; |
---|
218 | } |
---|
219 | // If the field is true or false, the variable is transformed into a |
---|
220 | // boolean value. |
---|
221 | if ($cat[$info] == 'true' or $cat[$info] == 'false') |
---|
222 | { |
---|
223 | $cat[$info] = get_boolean( $cat[$info] ); |
---|
224 | } |
---|
225 | } |
---|
226 | $cat['comment'] = nl2br($cat['comment']); |
---|
227 | |
---|
228 | $names = array(); |
---|
229 | $query = ' |
---|
230 | SELECT name,id |
---|
231 | FROM '.CATEGORIES_TABLE.' |
---|
232 | WHERE id IN ('.$cat['uppercats'].') |
---|
233 | ;'; |
---|
234 | $result = pwg_query($query); |
---|
235 | while($row = mysql_fetch_array($result)) |
---|
236 | { |
---|
237 | $names[$row['id']] = $row['name']; |
---|
238 | } |
---|
239 | |
---|
240 | // category names must be in the same order than uppercats list |
---|
241 | $cat['name'] = array(); |
---|
242 | foreach (explode(',', $cat['uppercats']) as $cat_id) |
---|
243 | { |
---|
244 | $cat['name'][$cat_id] = $names[$cat_id]; |
---|
245 | } |
---|
246 | |
---|
247 | return $cat; |
---|
248 | } |
---|
249 | |
---|
250 | // get_complete_dir returns the concatenation of get_site_url and |
---|
251 | // get_local_dir |
---|
252 | // Example : "pets > rex > 1_year_old" is on the the same site as the |
---|
253 | // PhpWebGallery files and this category has 22 for identifier |
---|
254 | // get_complete_dir(22) returns "./galleries/pets/rex/1_year_old/" |
---|
255 | function get_complete_dir( $category_id ) |
---|
256 | { |
---|
257 | return get_site_url($category_id).get_local_dir($category_id); |
---|
258 | } |
---|
259 | |
---|
260 | // get_local_dir returns an array with complete path without the site url |
---|
261 | // Example : "pets > rex > 1_year_old" is on the the same site as the |
---|
262 | // PhpWebGallery files and this category has 22 for identifier |
---|
263 | // get_local_dir(22) returns "pets/rex/1_year_old/" |
---|
264 | function get_local_dir( $category_id ) |
---|
265 | { |
---|
266 | global $page; |
---|
267 | |
---|
268 | $uppercats = ''; |
---|
269 | $local_dir = ''; |
---|
270 | |
---|
271 | if ( isset( $page['plain_structure'][$category_id]['uppercats'] ) ) |
---|
272 | { |
---|
273 | $uppercats = $page['plain_structure'][$category_id]['uppercats']; |
---|
274 | } |
---|
275 | else |
---|
276 | { |
---|
277 | $query = 'SELECT uppercats'; |
---|
278 | $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id = '.$category_id; |
---|
279 | $query.= ';'; |
---|
280 | $row = mysql_fetch_array( pwg_query( $query ) ); |
---|
281 | $uppercats = $row['uppercats']; |
---|
282 | } |
---|
283 | |
---|
284 | $upper_array = explode( ',', $uppercats ); |
---|
285 | |
---|
286 | $database_dirs = array(); |
---|
287 | $query = 'SELECT id,dir'; |
---|
288 | $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id IN ('.$uppercats.')'; |
---|
289 | $query.= ';'; |
---|
290 | $result = pwg_query( $query ); |
---|
291 | while( $row = mysql_fetch_array( $result ) ) |
---|
292 | { |
---|
293 | $database_dirs[$row['id']] = $row['dir']; |
---|
294 | } |
---|
295 | foreach ($upper_array as $id) |
---|
296 | { |
---|
297 | $local_dir.= $database_dirs[$id].'/'; |
---|
298 | } |
---|
299 | |
---|
300 | return $local_dir; |
---|
301 | } |
---|
302 | |
---|
303 | // retrieving the site url : "http://domain.com/gallery/" or |
---|
304 | // simply "./galleries/" |
---|
305 | function get_site_url($category_id) |
---|
306 | { |
---|
307 | global $page; |
---|
308 | |
---|
309 | $query = ' |
---|
310 | SELECT galleries_url |
---|
311 | FROM '.SITES_TABLE.' AS s,'.CATEGORIES_TABLE.' AS c |
---|
312 | WHERE s.id = c.site_id |
---|
313 | AND c.id = '.$category_id.' |
---|
314 | ;'; |
---|
315 | $row = mysql_fetch_array(pwg_query($query)); |
---|
316 | return $row['galleries_url']; |
---|
317 | } |
---|
318 | |
---|
319 | // initialize_category initializes ;-) the variables in relation |
---|
320 | // with category : |
---|
321 | // 1. calculation of the number of pictures in the category |
---|
322 | // 2. determination of the SQL query part to ask to find the right category |
---|
323 | // $page['where'] is not the same if we are in |
---|
324 | // - simple category |
---|
325 | // - search result |
---|
326 | // - favorites displaying |
---|
327 | // - most visited pictures |
---|
328 | // - best rated pictures |
---|
329 | // - recent pictures |
---|
330 | // - defined list (used for random) |
---|
331 | // 3. determination of the title of the page |
---|
332 | // 4. creation of the navigation bar |
---|
333 | function initialize_category( $calling_page = 'category' ) |
---|
334 | { |
---|
335 | pwg_debug( 'start initialize_category' ); |
---|
336 | global $page,$lang,$user,$conf; |
---|
337 | |
---|
338 | if ( isset( $page['cat'] ) ) |
---|
339 | { |
---|
340 | // $page['nb_image_page'] is the number of picture to display on this page |
---|
341 | // By default, it is the same as the $user['nb_image_page'] |
---|
342 | $page['nb_image_page'] = $user['nb_image_page']; |
---|
343 | // $url is used to create the navigation bar |
---|
344 | $url = PHPWG_ROOT_PATH.'category.php?cat='.$page['cat']; |
---|
345 | if ( isset($page['expand']) ) $url.= '&expand='.$page['expand']; |
---|
346 | // simple category |
---|
347 | if ( is_numeric( $page['cat'] ) ) |
---|
348 | { |
---|
349 | $result = get_cat_info( $page['cat'] ); |
---|
350 | $page['comment'] = $result['comment']; |
---|
351 | $page['cat_dir'] = $result['dir']; |
---|
352 | $page['cat_name'] = $result['name']; |
---|
353 | $page['cat_nb_images'] = $result['nb_images']; |
---|
354 | $page['cat_site_id'] = $result['site_id']; |
---|
355 | $page['cat_uploadable'] = $result['uploadable']; |
---|
356 | $page['cat_commentable'] = $result['commentable']; |
---|
357 | $page['uppercats'] = $result['uppercats']; |
---|
358 | $page['title'] = |
---|
359 | get_cat_display_name($page['cat_name'], |
---|
360 | '', |
---|
361 | false); |
---|
362 | $page['where'] = ' WHERE category_id = '.$page['cat']; |
---|
363 | } |
---|
364 | else |
---|
365 | { |
---|
366 | if ($page['cat'] == 'search' |
---|
367 | or $page['cat'] == 'most_visited' |
---|
368 | or $page['cat'] == 'recent_pics' |
---|
369 | or $page['cat'] == 'recent_cats' |
---|
370 | or $page['cat'] == 'best_rated' |
---|
371 | or $page['cat'] == 'calendar' |
---|
372 | or $page['cat'] == 'list') |
---|
373 | { |
---|
374 | // we must not show pictures of a forbidden category |
---|
375 | if ( $user['forbidden_categories'] != '' ) |
---|
376 | { |
---|
377 | $forbidden = ' category_id NOT IN '; |
---|
378 | $forbidden.= '('.$user['forbidden_categories'].')'; |
---|
379 | } |
---|
380 | } |
---|
381 | // search result |
---|
382 | if ( $page['cat'] == 'search' ) |
---|
383 | { |
---|
384 | // analyze search string given in URL (created in search.php) |
---|
385 | $tokens = explode('|', $_GET['search']); |
---|
386 | |
---|
387 | if (isset($tokens[1]) and $tokens[1] == 'AND') |
---|
388 | { |
---|
389 | $search['mode'] = 'AND'; |
---|
390 | } |
---|
391 | else |
---|
392 | { |
---|
393 | $search['mode'] = 'OR'; |
---|
394 | } |
---|
395 | |
---|
396 | $search_tokens = explode('--', $tokens[0]); |
---|
397 | foreach ($search_tokens as $search_token) |
---|
398 | { |
---|
399 | $tokens = explode(':', $search_token); |
---|
400 | $field_name = $tokens[0]; |
---|
401 | $field_content = $tokens[1]; |
---|
402 | |
---|
403 | $tokens = explode('~', $tokens[1]); |
---|
404 | if (isset($tokens[1])) |
---|
405 | { |
---|
406 | $search['fields'][$field_name]['mode'] = $tokens[1]; |
---|
407 | } |
---|
408 | else |
---|
409 | { |
---|
410 | $search['fields'][$field_name]['mode'] = ''; |
---|
411 | } |
---|
412 | |
---|
413 | $search['fields'][$field_name]['words'] = array(); |
---|
414 | $tokens = explode(',', $tokens[0]); |
---|
415 | foreach ($tokens as $token) |
---|
416 | { |
---|
417 | array_push($search['fields'][$field_name]['words'], |
---|
418 | htmlentities($token)); |
---|
419 | } |
---|
420 | } |
---|
421 | |
---|
422 | $page['title'] = $lang['search_result']; |
---|
423 | if ( $calling_page == 'picture' ) |
---|
424 | { |
---|
425 | $page['title'].= ' : <span style="font-style:italic;">'; |
---|
426 | $page['title'].= $_GET['search']."</span>"; |
---|
427 | } |
---|
428 | |
---|
429 | // SQL where clauses are stored in $clauses array during query |
---|
430 | // construction |
---|
431 | $clauses = array(); |
---|
432 | |
---|
433 | $textfields = array('file', 'name', 'comment', 'keywords', 'author'); |
---|
434 | foreach ($textfields as $textfield) |
---|
435 | { |
---|
436 | if (isset($search['fields'][$textfield])) |
---|
437 | { |
---|
438 | $local_clauses = array(); |
---|
439 | foreach ($search['fields'][$textfield]['words'] as $word) |
---|
440 | { |
---|
441 | array_push($local_clauses, $textfield." LIKE '%".$word."%'"); |
---|
442 | } |
---|
443 | // adds brackets around where clauses |
---|
444 | array_walk($local_clauses,create_function('&$s','$s="(".$s.")";')); |
---|
445 | array_push($clauses, |
---|
446 | implode(' '.$search['fields'][$textfield]['mode'].' ', |
---|
447 | $local_clauses)); |
---|
448 | } |
---|
449 | } |
---|
450 | |
---|
451 | if (isset($search['fields']['allwords'])) |
---|
452 | { |
---|
453 | $fields = array('file', 'name', 'comment', 'keywords', 'author'); |
---|
454 | // in the OR mode, request bust be : |
---|
455 | // ((field1 LIKE '%word1%' OR field2 LIKE '%word1%') |
---|
456 | // OR (field1 LIKE '%word2%' OR field2 LIKE '%word2%')) |
---|
457 | // |
---|
458 | // in the AND mode : |
---|
459 | // ((field1 LIKE '%word1%' OR field2 LIKE '%word1%') |
---|
460 | // AND (field1 LIKE '%word2%' OR field2 LIKE '%word2%')) |
---|
461 | $word_clauses = array(); |
---|
462 | foreach ($search['fields']['allwords']['words'] as $word) |
---|
463 | { |
---|
464 | $field_clauses = array(); |
---|
465 | foreach ($fields as $field) |
---|
466 | { |
---|
467 | array_push($field_clauses, $field." LIKE '%".$word."%'"); |
---|
468 | } |
---|
469 | // adds brackets around where clauses |
---|
470 | array_push($word_clauses, implode(' OR ', $field_clauses)); |
---|
471 | } |
---|
472 | array_walk($word_clauses, create_function('&$s','$s="(".$s.")";')); |
---|
473 | array_push($clauses, |
---|
474 | implode(' '.$search['fields']['allwords']['mode'].' ', |
---|
475 | $word_clauses)); |
---|
476 | } |
---|
477 | |
---|
478 | $datefields = array('date_available', 'date_creation'); |
---|
479 | foreach ($datefields as $datefield) |
---|
480 | { |
---|
481 | $key = $datefield; |
---|
482 | if (isset($search['fields'][$key])) |
---|
483 | { |
---|
484 | $local_clause = $datefield." = '"; |
---|
485 | $local_clause.= str_replace('.', '-', |
---|
486 | $search['fields'][$key]['words'][0]); |
---|
487 | $local_clause.= "'"; |
---|
488 | array_push($clauses, $local_clause); |
---|
489 | } |
---|
490 | |
---|
491 | foreach (array('after','before') as $suffix) |
---|
492 | { |
---|
493 | $key = $datefield.'-'.$suffix; |
---|
494 | if (isset($search['fields'][$key])) |
---|
495 | { |
---|
496 | $local_clause = $datefield; |
---|
497 | if ($suffix == 'after') |
---|
498 | { |
---|
499 | $local_clause.= ' >'; |
---|
500 | } |
---|
501 | else |
---|
502 | { |
---|
503 | $local_clause.= ' <'; |
---|
504 | } |
---|
505 | if (isset($search['fields'][$key]['mode']) |
---|
506 | and $search['fields'][$key]['mode'] == 'inc') |
---|
507 | { |
---|
508 | $local_clause.= '='; |
---|
509 | } |
---|
510 | $local_clause.= " '"; |
---|
511 | $local_clause.= str_replace('.', '-', |
---|
512 | $search['fields'][$key]['words'][0]); |
---|
513 | $local_clause.= "'"; |
---|
514 | array_push($clauses, $local_clause); |
---|
515 | } |
---|
516 | } |
---|
517 | } |
---|
518 | |
---|
519 | if (isset($search['fields']['cat'])) |
---|
520 | { |
---|
521 | if ($search['fields']['cat']['mode'] == 'sub_inc') |
---|
522 | { |
---|
523 | // searching all the categories id of sub-categories |
---|
524 | $cat_ids = get_subcat_ids($search['fields']['cat']['words']); |
---|
525 | } |
---|
526 | else |
---|
527 | { |
---|
528 | $cat_ids = $search['fields']['cat']['words']; |
---|
529 | } |
---|
530 | |
---|
531 | $local_clause = 'category_id IN ('.implode(',', $cat_ids).')'; |
---|
532 | array_push($clauses, $local_clause); |
---|
533 | } |
---|
534 | |
---|
535 | // adds brackets around where clauses |
---|
536 | array_walk($clauses, create_function('&$s', '$s = "(".$s.")";')); |
---|
537 | $page['where'] = 'WHERE '.implode(' '.$search['mode'].' ', $clauses); |
---|
538 | if ( isset( $forbidden ) ) $page['where'].= ' AND '.$forbidden; |
---|
539 | |
---|
540 | $query = ' |
---|
541 | SELECT COUNT(DISTINCT(id)) AS nb_total_images |
---|
542 | FROM '.IMAGES_TABLE.' |
---|
543 | INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id |
---|
544 | '.$page['where'].' |
---|
545 | ;'; |
---|
546 | $url.= '&search='.$_GET['search']; |
---|
547 | } |
---|
548 | // favorites displaying |
---|
549 | else if ( $page['cat'] == 'fav' ) |
---|
550 | { |
---|
551 | check_user_favorites(); |
---|
552 | |
---|
553 | $page['title'] = $lang['favorites']; |
---|
554 | |
---|
555 | $page['where'] = ', '.FAVORITES_TABLE.' AS fav'; |
---|
556 | $page['where'].= ' WHERE user_id = '.$user['id']; |
---|
557 | $page['where'].= ' AND fav.image_id = id'; |
---|
558 | |
---|
559 | $query = 'SELECT COUNT(*) AS nb_total_images'; |
---|
560 | $query.= ' FROM '.FAVORITES_TABLE; |
---|
561 | $query.= ' WHERE user_id = '.$user['id']; |
---|
562 | $query.= ';'; |
---|
563 | } |
---|
564 | // pictures within the short period |
---|
565 | else if ( $page['cat'] == 'recent_pics' ) |
---|
566 | { |
---|
567 | $page['title'] = $lang['recent_pics_cat']; |
---|
568 | // We must find the date corresponding to : |
---|
569 | // today - $conf['periode_courte'] |
---|
570 | $date = time() - 60*60*24*$user['recent_period']; |
---|
571 | $page['where'] = " WHERE date_available > '"; |
---|
572 | $page['where'].= date( 'Y-m-d', $date )."'"; |
---|
573 | if ( isset( $forbidden ) ) $page['where'].= ' AND '.$forbidden; |
---|
574 | |
---|
575 | $query = ' |
---|
576 | SELECT COUNT(DISTINCT(id)) AS nb_total_images |
---|
577 | FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic |
---|
578 | ON id = ic.image_id |
---|
579 | '.$page['where'].' |
---|
580 | ;'; |
---|
581 | } |
---|
582 | // categories containing recent pictures |
---|
583 | else if ( $page['cat'] == 'recent_cats' ) |
---|
584 | { |
---|
585 | $page['title'] = $lang['recent_cats_cat']; |
---|
586 | $page['cat_nb_images'] = 0; |
---|
587 | } |
---|
588 | // most visited pictures |
---|
589 | else if ( $page['cat'] == 'most_visited' ) |
---|
590 | { |
---|
591 | $page['title'] = $conf['top_number'].' '.$lang['most_visited_cat']; |
---|
592 | |
---|
593 | $page['where'] = 'WHERE hit > 0'; |
---|
594 | if (isset($forbidden)) |
---|
595 | { |
---|
596 | $page['where'] = "\n".' AND '.$forbidden; |
---|
597 | } |
---|
598 | |
---|
599 | $conf['order_by'] = ' ORDER BY hit DESC, file ASC'; |
---|
600 | |
---|
601 | // $page['cat_nb_images'] equals $conf['top_number'] unless there |
---|
602 | // are less visited items |
---|
603 | $query =' |
---|
604 | SELECT COUNT(DISTINCT(id)) AS count |
---|
605 | FROM '.IMAGES_TABLE.' |
---|
606 | INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id |
---|
607 | '.$page['where'].' |
---|
608 | ;'; |
---|
609 | $row = mysql_fetch_array(pwg_query($query)); |
---|
610 | if ($row['count'] < $conf['top_number']) |
---|
611 | { |
---|
612 | $page['cat_nb_images'] = $row['count']; |
---|
613 | } |
---|
614 | else |
---|
615 | { |
---|
616 | $page['cat_nb_images'] = $conf['top_number']; |
---|
617 | } |
---|
618 | unset($query); |
---|
619 | |
---|
620 | if ( isset( $page['start'] ) |
---|
621 | and ($page['start']+$user['nb_image_page']>=$conf['top_number'])) |
---|
622 | { |
---|
623 | $page['nb_image_page'] = $conf['top_number'] - $page['start']; |
---|
624 | } |
---|
625 | } |
---|
626 | else if ( $page['cat'] == 'calendar' ) |
---|
627 | { |
---|
628 | $page['cat_nb_images'] = 0; |
---|
629 | $page['title'] = $lang['calendar']; |
---|
630 | if (isset($_GET['year']) |
---|
631 | and preg_match('/^\d+$/', $_GET['year'])) |
---|
632 | { |
---|
633 | $page['calendar_year'] = (int)$_GET['year']; |
---|
634 | } |
---|
635 | if (isset($_GET['month']) |
---|
636 | and preg_match('/^(\d+)\.(\d{2})$/', $_GET['month'], $matches)) |
---|
637 | { |
---|
638 | $page['calendar_year'] = (int)$matches[1]; |
---|
639 | $page['calendar_month'] = (int)$matches[2]; |
---|
640 | } |
---|
641 | if (isset($_GET['day']) |
---|
642 | and preg_match('/^(\d+)\.(\d{2})\.(\d{2})$/', |
---|
643 | $_GET['day'], |
---|
644 | $matches)) |
---|
645 | { |
---|
646 | $page['calendar_year'] = (int)$matches[1]; |
---|
647 | $page['calendar_month'] = (int)$matches[2]; |
---|
648 | $page['calendar_day'] = (int)$matches[3]; |
---|
649 | } |
---|
650 | if (isset($page['calendar_year'])) |
---|
651 | { |
---|
652 | $page['title'] .= ' ('; |
---|
653 | if (isset($page['calendar_day'])) |
---|
654 | { |
---|
655 | if ($page['calendar_year'] >= 1970) |
---|
656 | { |
---|
657 | $unixdate = mktime(0,0,0, |
---|
658 | $page['calendar_month'], |
---|
659 | $page['calendar_day'], |
---|
660 | $page['calendar_year']); |
---|
661 | $page['title'].= $lang['day'][date("w", $unixdate)]; |
---|
662 | } |
---|
663 | $page['title'].= ' '.$page['calendar_day'].', '; |
---|
664 | } |
---|
665 | if (isset($page['calendar_month'])) |
---|
666 | { |
---|
667 | $page['title'] .= $lang['month'][$page['calendar_month']].' '; |
---|
668 | } |
---|
669 | $page['title'] .= $page['calendar_year']; |
---|
670 | $page['title'] .= ')'; |
---|
671 | } |
---|
672 | |
---|
673 | $page['where'] = 'WHERE '.$conf['calendar_datefield'].' IS NOT NULL'; |
---|
674 | if (isset($forbidden)) |
---|
675 | { |
---|
676 | $page['where'].= ' AND '.$forbidden; |
---|
677 | } |
---|
678 | } |
---|
679 | else if ($page['cat'] == 'best_rated') |
---|
680 | { |
---|
681 | $page['title'] = $conf['top_number'].' '.$lang['best_rated_cat']; |
---|
682 | |
---|
683 | $page['where'] = ' WHERE average_rate IS NOT NULL'; |
---|
684 | |
---|
685 | if (isset($forbidden)) |
---|
686 | { |
---|
687 | $page['where'].= ' AND '.$forbidden; |
---|
688 | } |
---|
689 | |
---|
690 | $conf['order_by'] = ' ORDER BY average_rate DESC, id ASC'; |
---|
691 | |
---|
692 | // $page['cat_nb_images'] equals $conf['top_number'] unless there |
---|
693 | // are less rated items |
---|
694 | $query =' |
---|
695 | SELECT COUNT(DISTINCT(id)) AS count |
---|
696 | FROM '.IMAGES_TABLE.' |
---|
697 | INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id |
---|
698 | '.$page['where'].' |
---|
699 | ;'; |
---|
700 | $row = mysql_fetch_array(pwg_query($query)); |
---|
701 | if ($row['count'] < $conf['top_number']) |
---|
702 | { |
---|
703 | $page['cat_nb_images'] = $row['count']; |
---|
704 | } |
---|
705 | else |
---|
706 | { |
---|
707 | $page['cat_nb_images'] = $conf['top_number']; |
---|
708 | } |
---|
709 | unset($query); |
---|
710 | |
---|
711 | |
---|
712 | if (isset($page['start']) |
---|
713 | and ($page['start']+$user['nb_image_page']>=$conf['top_number'])) |
---|
714 | { |
---|
715 | $page['nb_image_page'] = $conf['top_number'] - $page['start']; |
---|
716 | } |
---|
717 | } |
---|
718 | else if ($page['cat'] == 'list') |
---|
719 | { |
---|
720 | $page['title'] = $lang['random_cat']; |
---|
721 | |
---|
722 | $page['where'] = 'WHERE 1=1'; |
---|
723 | if (isset($forbidden)) |
---|
724 | { |
---|
725 | $page['where'].= ' AND '.$forbidden; |
---|
726 | } |
---|
727 | $page['where'].= ' AND image_id IN ('.$_GET['list'].')'; |
---|
728 | $page['cat_nb_images'] = count(explode(',', $_GET['list'])); |
---|
729 | |
---|
730 | $url.= '&list='.$_GET['list']; |
---|
731 | } |
---|
732 | |
---|
733 | if (isset($query)) |
---|
734 | { |
---|
735 | $result = pwg_query( $query ); |
---|
736 | $row = mysql_fetch_array( $result ); |
---|
737 | $page['cat_nb_images'] = $row['nb_total_images']; |
---|
738 | } |
---|
739 | } |
---|
740 | if ( $calling_page == 'category' ) |
---|
741 | { |
---|
742 | $page['navigation_bar'] = |
---|
743 | create_navigation_bar( $url, $page['cat_nb_images'], $page['start'], |
---|
744 | $user['nb_image_page'], 'back' ); |
---|
745 | } |
---|
746 | } |
---|
747 | else |
---|
748 | { |
---|
749 | $page['title'] = $lang['no_category']; |
---|
750 | } |
---|
751 | pwg_debug( 'end initialize_category' ); |
---|
752 | } |
---|
753 | |
---|
754 | function display_select_categories($categories, |
---|
755 | $selecteds, |
---|
756 | $blockname, |
---|
757 | $fullname = true) |
---|
758 | { |
---|
759 | global $template; |
---|
760 | |
---|
761 | foreach ($categories as $category) |
---|
762 | { |
---|
763 | $selected = ''; |
---|
764 | if (in_array($category['id'], $selecteds)) |
---|
765 | { |
---|
766 | $selected = ' selected="selected"'; |
---|
767 | } |
---|
768 | |
---|
769 | if ($fullname) |
---|
770 | { |
---|
771 | $option = get_cat_display_name_cache($category['uppercats'], |
---|
772 | '', |
---|
773 | false); |
---|
774 | } |
---|
775 | else |
---|
776 | { |
---|
777 | $option = str_repeat(' ', |
---|
778 | (3 * substr_count($category['global_rank'], '.'))); |
---|
779 | $option.= '- '.$category['name']; |
---|
780 | } |
---|
781 | |
---|
782 | $template->assign_block_vars( |
---|
783 | $blockname, |
---|
784 | array('SELECTED'=>$selected, |
---|
785 | 'VALUE'=>$category['id'], |
---|
786 | 'OPTION'=>$option |
---|
787 | )); |
---|
788 | } |
---|
789 | } |
---|
790 | |
---|
791 | function display_select_cat_wrapper($query, $selecteds, $blockname, |
---|
792 | $fullname = true) |
---|
793 | { |
---|
794 | $result = pwg_query($query); |
---|
795 | $categories = array(); |
---|
796 | if (!empty($result)) |
---|
797 | { |
---|
798 | while ($row = mysql_fetch_array($result)) |
---|
799 | { |
---|
800 | array_push($categories, $row); |
---|
801 | } |
---|
802 | } |
---|
803 | usort($categories, 'global_rank_compare'); |
---|
804 | display_select_categories($categories, $selecteds, $blockname, $fullname); |
---|
805 | } |
---|
806 | |
---|
807 | /** |
---|
808 | * returns all subcategory identifiers of given category ids |
---|
809 | * |
---|
810 | * @param array ids |
---|
811 | * @return array |
---|
812 | */ |
---|
813 | function get_subcat_ids($ids) |
---|
814 | { |
---|
815 | $query = ' |
---|
816 | SELECT DISTINCT(id) |
---|
817 | FROM '.CATEGORIES_TABLE.' |
---|
818 | WHERE '; |
---|
819 | foreach ($ids as $num => $category_id) |
---|
820 | { |
---|
821 | if ($num > 0) |
---|
822 | { |
---|
823 | $query.= ' |
---|
824 | OR '; |
---|
825 | } |
---|
826 | $query.= 'uppercats REGEXP \'(^|,)'.$category_id.'(,|$)\''; |
---|
827 | } |
---|
828 | $query.= ' |
---|
829 | ;'; |
---|
830 | $result = pwg_query($query); |
---|
831 | |
---|
832 | $subcats = array(); |
---|
833 | while ($row = mysql_fetch_array($result)) |
---|
834 | { |
---|
835 | array_push($subcats, $row['id']); |
---|
836 | } |
---|
837 | return $subcats; |
---|
838 | } |
---|
839 | |
---|
840 | function global_rank_compare($a, $b) |
---|
841 | { |
---|
842 | return strnatcasecmp($a['global_rank'], $b['global_rank']); |
---|
843 | } |
---|
844 | ?> |
---|