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