source: trunk/include/functions_category.inc.php @ 64

Last change on this file since 64 was 64, checked in by z0rglub, 21 years ago

Correcting a bug in special categories (favorites, most seen, most
recent... non numeric categories) : when a picture is linked to more than
one category, it's only displayed once.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 20.3 KB
Line 
1<?php
2/***************************************************************************
3 *                         functions_category.inc.php                      *
4 *                            --------------------                         *
5 *   application          : PhpWebGallery 1.3 <http://phpwebgallery.net>   *
6 *   author        : Pierrick LE GALL <pierrick@z0rglub.com>               *
7 *                                                                         *
8 *   $Id: functions_category.inc.php 64 2003-09-05 19:27:45Z z0rglub $
9 *                                                                         *
10 ***************************************************************************
11
12 ***************************************************************************
13 *                                                                         *
14 *   This program is free software; you can redistribute it and/or modify  *
15 *   it under the terms of the GNU General Public License as published by  *
16 *   the Free Software Foundation;                                         *
17 *                                                                         *
18 ***************************************************************************/
19
20function get_subcats_id( $cat_id )
21{
22  $restricted_cats = array();
23               
24  $query = 'SELECT id';
25  $query.= ' FROM '.PREFIX_TABLE.'categories';
26  $query.= ' WHERE id_uppercat = '.$cat_id;
27  $query.= ';';
28  $result = mysql_query( $query );
29  while ( $row = mysql_fetch_array( $result ) )
30  {
31    array_push( $restricted_cats, $row['id'] );
32    $sub_restricted_cats = get_subcats_id( $row['id'] );
33    foreach ( $sub_restricted_cats as $sub_restricted_cat ) {
34      array_push( $restricted_cats, $sub_restricted_cat );
35    }
36  }
37  return $restricted_cats;
38}
39
40function check_restrictions( $category_id )
41{
42  global $user,$lang;
43
44  if ( is_user_allowed( $category_id, $user['restrictions'] ) > 0 )
45  {
46    echo '<div style="text-align:center;">'.$lang['access_forbiden'].'<br />';
47    echo '<a href="'.add_session_id( './category.php' ).'">';
48    echo $lang['thumbnails'].'</a></div>';
49    exit();
50  }
51}
52       
53// the check_cat_id function check whether the $cat is a right parameter :
54//  - $cat is numeric and corresponds to a category in the database
55//  - $cat equals 'fav' (for favorites)
56//  - $cat equals 'search' (when the result of a search is displayed)
57function check_cat_id( $cat )
58{
59  global $page;
60
61  unset( $page['cat'] );
62  if ( isset( $cat ) )
63  {
64    if ( isset( $page['plain_structure'] ) )
65    {
66      if ( isset( $page['plain_structure'][$cat] ) )
67      {
68        $page['cat'] = $cat;
69      }
70    }
71    else if ( is_numeric( $cat ) )
72    {
73      $query = 'SELECT id';
74      $query.= ' FROM '.PREFIX_TABLE.'categories';
75      $query.= ' WHERE id = '.$cat;
76      $query. ';';
77      $result = mysql_query( $query );
78      if ( mysql_num_rows( $result ) != 0 )
79      {
80        $page['cat'] = $cat;
81      }
82    }
83    if ( $cat == 'fav'
84         or $cat == 'search'
85         or $cat == 'most_visited'
86         or $cat == 'best_rated'
87         or $cat == 'recent' )
88    {
89      $page['cat'] = $cat;
90    }
91  }
92}
93
94function get_plain_structure()
95{
96  $infos = array( 'name','id','date_last','nb_images','dir','id_uppercat',
97                  'rank','site_id');
98 
99  $query = 'SELECT ';
100  foreach ( $infos as $i => $info ) {
101    if ( $i > 0 ) $query.= ',';
102    $query.= $info;
103  }
104  $query.= ' FROM '.PREFIX_TABLE.'categories';
105  $query.= ' ORDER BY id_uppercat ASC, rank ASC';
106  $query.= ';';
107
108  $plain_structure = array();
109  $result = mysql_query( $query );
110  while ( $row = mysql_fetch_array( $result ) )
111  {
112    $category = array();
113    foreach ( $infos as $info ) {
114      $category[$info] = $row[$info];
115      if ( $info == 'date_last' )
116      {
117        list($year,$month,$day) = explode( '-', $row[$info] );
118        $category[$info] = mktime(0,0,0,$month,$day,$year);
119      }
120    }
121    $plain_structure[$row['id']] = $category;
122  }
123
124  return $plain_structure;
125}
126
127function create_structure( $id_uppercat, $restrictions )
128{
129  global $page;
130
131  $structure = array();
132  $ids = get_subcat_ids( $id_uppercat );
133  foreach ( $ids as $id ) {
134    if ( !in_array( $id, $restrictions ) )
135    {
136      $category = $page['plain_structure'][$id];
137      $category['subcats'] = create_structure( $id, $restrictions );
138      array_push( $structure, $category );
139    }
140  }
141  return $structure;
142}
143
144function get_subcat_ids( $id_uppercat )
145{
146  global $page;
147
148  $ids = array();
149  foreach ( $page['plain_structure'] as $id => $category ) {
150    if ( $category['id_uppercat'] == $id_uppercat ) array_push( $ids, $id );
151    else if ( count( $ids ) > 0 )                   return $ids;
152  }
153  return $ids;
154}
155
156// update_structure updates or add informations about each node of the
157// structure : the last date, should the category be expanded in the menu ?,
158// the associated expand string "48,14,54"
159//
160// 1. last date
161// for each category of the structure, we have to find the most recent
162// subcat so that the parent cat has the same last_date info.
163// For example : we have :
164// > pets       (2003.02.15)
165//    > dogs    (2003.06.14)
166//       > rex  (2003.06.18)
167//       > toby (2003.06.13)
168//    > kitten  (2003.07.05)
169// We finally want to have :
170// > pets       (2003.07.05) <- changed to pets > kitten last date
171//    > dogs    (2003.06.18) <- changed to pets > dogs > rex last date
172//       > rex  (2003.06.18)
173//       > toby (2003.06.13)
174//    > kitten  (2003.07.05)
175//
176// 2. should the category be expanded in the menu ?
177// If the category has to be expanded (ie its id is in the
178// $page['tab_expand'] or all the categories must be expanded by default),
179// $category['expanded'] is set to true.
180//
181// 3. associated expand string
182// in the menu, there is a expand string (used in the URL) to tell which
183// categories must be expanded in the menu if this category is chosen
184function update_structure( $categories )
185{
186  global $page, $user;
187
188  $updated_categories = array();
189
190  foreach ( $categories as $category ) {
191    // update the last date of the category
192    $last_date = search_last_date( $category );
193    $category['date_last'] = $last_date;
194    // update the "expanded" key
195    if ( $user['expand']
196         or $page['expand'] == 'all'
197         or in_array( $category['id'], $page['tab_expand'] ) )
198    {
199      $category['expanded'] = true;
200    }
201    else
202    {
203      $category['expanded'] = false;
204    }
205    // update the  "expand_string" key
206    if ( $page['expand'] == 'all' )
207    {
208      $category['expand_string'] = 'all';
209    }
210    else
211    {
212      $tab_expand = $page['tab_expand'];
213      if ( in_array( $category['id'], $page['tab_expand'] ) )
214      {
215        // the expand string corresponds to the $page['tab_expand'] without
216        // the $category['id']
217        $tab_expand = array_diff( $page['tab_expand'],array($category['id']) );
218      }
219      else if ( count( $category['subcats'] ) > 0 )
220      {
221        // we have this time to add the $category['id']...
222        $tab_expand = array_merge($page['tab_expand'],array($category['id']));
223      }
224      $category['expand_string'] = implode( ',', $tab_expand );
225    }
226    // recursive call
227    $category['subcats'] = update_structure( $category['subcats'] );
228    // adding the updated category
229    array_push( $updated_categories, $category );
230  }
231
232  return $updated_categories;
233}
234
235// search_last_date searchs the last date for a given category. If we take
236// back the example given for update_last_dates, we should have :
237// search_last_date( pets )        --> 2003.07.05
238// search_last_date( pets > dogs ) --> 2003.06.18
239// and so on
240function search_last_date( $category )
241{
242  $date_last = $category['date_last'];
243  foreach ( $category['subcats'] as $subcat ) {
244    $subcat_date_last = search_last_date( $subcat );
245    if ( $subcat_date_last > $date_last )
246    {
247      $date_last = $subcat_date_last;
248    }
249  }
250  return $date_last;
251}
252
253// count_images returns the number of pictures contained in the given
254// category represented by an array, in this array, we have (among other
255// things) :
256// $category['nb_images'] -> number of pictures in this category
257// $category['subcats'] -> array of sub-categories
258// count_images goes to the deepest sub-category to find the total number of
259// pictures contained in the given given category
260function count_images( $categories )
261{
262  $total = 0;
263  foreach ( $categories as $category ) {
264    $total+= $category['nb_images'];
265    $total+= count_images( $category['subcats'] );
266  }
267  return $total;
268}
269
270// variables :
271// $cat['comment']
272// $cat['dir']
273// $cat['dir']
274// $cat['name'] is an array :
275//      - $cat['name'][0] is the lowest cat name
276//      and
277//      - $cat['name'][n] is the most uppercat name findable
278// $cat['nb_images']
279// $cat['id_uppercat']
280// $cat['site_id']
281function get_cat_info( $id )
282{
283  global $page;
284
285  $cat = array();
286               
287  $query = 'SELECT nb_images,id_uppercat,comment,site_id,galleries_url,dir';
288  $query.= ',date_last,uploadable,status,visible';
289  $query.= ' FROM '.PREFIX_TABLE.'categories AS a';
290  $query.= ', '.PREFIX_TABLE.'sites AS b';
291  $query.= ' WHERE a.id = '.$id;
292  $query.= ' AND a.site_id = b.id;';
293  $row = mysql_fetch_array( mysql_query( $query ) );
294  $cat['site_id']     = $row['site_id'];
295  $cat['id_uppercat'] = $row['id_uppercat'];
296  $cat['comment']     = nl2br( $row['comment'] );
297  $cat['nb_images']   = $row['nb_images'];
298  $cat['dir']         = $row['dir'];
299  $cat['date_last']   = $row['date_last'];
300  $cat['uploadable']  = get_boolean( $row['uploadable'] );
301  $cat['status']      = $row['status'];
302  $cat['visible']     = get_boolean( $row['visible'] );
303
304  $cat['name'] = array();
305  array_push( $cat['name'], $page['plain_structure'][$id]['name'] );
306  while ( $page['plain_structure'][$id]['id_uppercat'] != '' )
307  {
308    $id = $page['plain_structure'][$id]['id_uppercat'];
309    array_push( $cat['name'], $page['plain_structure'][$id]['name'] );
310  }
311  return $cat;
312}
313
314// get_complete_dir returns the concatenation of get_site_url and
315// get_local_dir
316// Example : "pets > rex > 1_year_old" is on the the same site as the
317// PhpWebGallery files and this category has 22 for identifier
318// get_complete_dir(22) returns "./galleries/pets/rex/1_year_old/"
319function get_complete_dir( $category_id )
320{
321  return get_site_url( $category_id ).get_local_dir( $category_id );
322}
323
324// get_local_dir returns an array with complete path without the site url
325// Example : "pets > rex > 1_year_old" is on the the same site as the
326// PhpWebGallery files and this category has 22 for identifier
327// get_local_dir(22) returns "pets/rex/1_year_old/"
328function get_local_dir( $category_id )
329{
330  global $page;
331
332  // creating the local path : "root_cat/sub_cat/sub_sub_cat/"
333  $dir = $page['plain_structure'][$category_id]['dir'].'/';
334  while ( $page['plain_structure'][$category_id]['id_uppercat'] != '' )
335  {
336    $category_id = $page['plain_structure'][$category_id]['id_uppercat'];
337    $dir = $page['plain_structure'][$category_id]['dir'].'/'.$dir;
338  }
339  return $dir;
340}
341
342// retrieving the site url : "http://domain.com/gallery/" or
343// simply "./galleries/"
344function get_site_url( $category_id )
345{
346  global $page;
347
348  $query = 'SELECT galleries_url';
349  $query.= ' FROM '.PREFIX_TABLE.'sites';
350  $query.= ' WHERE id = '.$page['plain_structure'][$category_id]['site_id'];
351  $query.= ';';
352  $row = mysql_fetch_array( mysql_query( $query ) );
353  return $row['galleries_url'];
354}
355
356// The function get_cat_display_name returns a string containing the list
357// of upper categories to the root category from the lowest category shown
358// example : "anniversaires - fete mere 2002 - animaux - erika"
359// You can give two parameters :
360//   - $separation : the string between each category name " - " for example
361//   - $style : the style of the span tag for the lowest category,
362//     "font-style:italic;" for example
363function get_cat_display_name( $array_cat_names, $separation, $style )
364{
365  $output = "";
366  for ( $i = sizeof( $array_cat_names ) - 1; $i >= 0; $i-- )
367  {
368    if ( $i != sizeof( $array_cat_names ) - 1 )
369    {
370      $output.= $separation;
371    }
372    if ( $i != 0 )
373    {
374      $output.= $array_cat_names[$i];
375    }
376    else
377    {
378      if ( $style != "" )
379      {
380        $output.= '<span style="'.$style.'">';
381      }
382      $output.= $array_cat_names[$i];
383      if ( $style != "" )
384      {
385        $output.= "</span>";
386      }
387    }
388  }
389  return replace_space( $output );
390}
391
392// initialize_category initializes ;-) the variables in relation
393// with category :
394// 1. calculation of the number of pictures in the category
395// 2. determination of the SQL query part to ask to find the right category
396//    $page['where'] is not the same if we are in
397//       - simple category
398//       - search result
399//       - favorites displaying
400//       - most visited pictures
401//       - best rated pictures
402//       - recent pictures
403// 3. determination of the title of the page
404// 4. creation of the navigation bar
405function initialize_category( $calling_page = 'category' )
406{
407  global $page,$lang,$user,$conf;
408
409  if ( isset( $page['cat'] ) )
410  {
411    // $page['nb_image_page'] is the number of picture to display on this page
412    // By default, it is the same as the $user['nb_image_page']
413    $page['nb_image_page'] = $user['nb_image_page'];
414    // $url is used to create the navigation bar
415    $url = './category.php?cat='.$page['cat'].'&amp;expand='.$page['expand'];
416    // simple category
417    if ( is_numeric( $page['cat'] ) )
418    {
419      $result = get_cat_info( $page['cat'] );
420      $page['comment']        = $result['comment'];
421      $page['cat_dir']        = $result['dir'];
422      $page['cat_name']       = $result['name'];
423      $page['cat_nb_images']  = $result['nb_images'];
424      $page['cat_site_id']    = $result['site_id'];
425      $page['cat_uploadable'] = $result['uploadable'];
426      $page['title'] = get_cat_display_name( $page['cat_name'], ' - ', '' );
427      $page['where'] = ' WHERE category_id = '.$page['cat'];
428    }
429    else
430    {
431      if ( $page['cat'] == 'search' or $page['cat'] == 'most_visited'
432           or $page['cat'] == 'recent' or $page['cat'] == 'best_rated' )
433      {
434        // we must not show pictures of a forbidden category
435        $restricted_cats = get_all_restrictions( $user['id'],$user['status'] );
436        foreach ( $restricted_cats as $restricted_cat ) {
437          $where_append.= ' AND category_id != '.$restricted_cat;
438        }
439      }
440      // search result
441      if ( $page['cat'] == 'search' )
442      {
443        $page['title'] = $lang['search_result'];
444        if ( $calling_page == 'picture' )
445        {
446          $page['title'].= ' : <span style="font-style:italic;">';
447          $page['title'].= $_GET['search']."</span>";
448        }
449
450        $page['where'] = ' WHERE (';
451        $fields = array( 'file', 'name', 'comment', 'keywords' );
452        $words = explode( ',', $_GET['search'] );
453        $sql_search = array();
454        foreach ( $words as $i => $word ) {
455          // if the user searchs any of the words, the where statement must
456          // be :
457          // field1 LIKE '%$word1%' OR field2 LIKE '%$word1%' ...
458          // OR field1 LIKE '%$word2%' OR field2 LIKE '%$word2%' ...
459          if ( $_GET['mode'] == 'OR' )
460          {
461            if ( $i != 0 ) $page['where'].= ' OR';
462            foreach ( $fields as $j => $field ) {
463              if ( $j != 0 ) $page['where'].= ' OR';
464              $page['where'].= ' '.$field." LIKE '%".$word."%'";
465            }
466          }
467          // if the user searchs all the words :
468          // ( field1 LIKE '%$word1%' OR field2 LIKE '%$word1%' )
469          // AND ( field1 LIKE '%$word2%' OR field2 LIKE '%$word2%' )
470          else if ( $_GET['mode'] == 'AND' )
471          {
472            if ( $i != 0 ) $page['where'].= ' AND';
473            $page['where'].= ' (';
474            foreach ( $fields as $j => $field ) {
475              if ( $j != 0 ) $page['where'].= ' OR';
476              $page['where'].= ' '.$field." LIKE '%".$word."%'";
477            }
478            $page['where'].= ' )';
479          }
480        }
481        $page['where'].= ' )';
482        $page['where'].= $where_append;
483
484        $query = 'SELECT COUNT(*) AS nb_total_images';
485        $query.= ' FROM '.PREFIX_TABLE.'images';
486        $query.= $page['where'];
487        $query.= ';';
488
489        $url.= '&amp;search='.$_GET['search'].'&amp;mode='.$_GET['mode'];
490      }
491      // favorites displaying
492      else if ( $page['cat'] == 'fav' )
493      {
494        $page['title'] = $lang['favorites'];
495
496        $page['where'] = ', '.PREFIX_TABLE.'favorites AS fav';
497        $page['where'].= ' WHERE user_id = '.$user['id'];
498        $page['where'].= ' AND fav.image_id = id';
499     
500        $query = 'SELECT COUNT(*) AS nb_total_images';
501        $query.= ' FROM '.PREFIX_TABLE.'favorites';
502        $query.= ' WHERE user_id = '.$user['id'];
503        $query.= ';';
504      }
505      // pictures within the short period
506      else if ( $page['cat'] == 'recent' )
507      {
508        $page['title'] = $lang['recent_cat_title'];
509        // We must find the date corresponding to :
510        // today - $conf['periode_courte']
511        $date = time() - 60*60*24*$user['short_period'];
512        $page['where'] = " WHERE date_available > '";
513        $page['where'].= date( 'Y-m-d', $date )."'";
514        $page['where'].= $where_append;
515
516        $query = 'SELECT COUNT(*) AS nb_total_images';
517        $query.= ' FROM '.PREFIX_TABLE.'images';
518        $query.= $page['where'];
519        $query.= ';';
520      }
521      // most visited pictures
522      else if ( $page['cat'] == 'most_visited' )
523      {
524        $page['title'] = $conf['top_number'].' '.$lang['most_visited_cat'];
525        $page['where'] = ' WHERE category_id != -1'.$where_append;
526        $conf['order_by'] = ' ORDER BY hit DESC, file ASC';
527        $page['cat_nb_images'] = $conf['top_number'];
528        if ( $page['start'] + $user['nb_image_page'] >= $conf['top_number'] )
529        {
530          $page['nb_image_page'] = $conf['top_number'] - $page['start'];
531        }
532      }
533     
534      if ( $query != '' )
535      {
536        $result = mysql_query( $query );
537        $row = mysql_fetch_array( $result );
538        $page['cat_nb_images'] = $row['nb_total_images'];
539      }
540    }
541    if ( $calling_page == 'category' )
542    {
543      $page['navigation_bar'] =
544        create_navigation_bar( $url, $page['cat_nb_images'], $page['start'],
545                               $user['nb_image_page'], 'back' );
546    }
547  }
548  else
549  {
550    $page['title'] = $lang['diapo_default_page_title'];
551  }
552}
553
554// get_non_empty_subcat_ids returns an array with sub-categories id
555// associated with their first non empty category id.
556//
557//                          example :
558//
559// - catname [cat_id]
560// - cat1 [1] -> given uppercat
561//   - cat1.1 [12] (empty)
562//     - cat1.1.1 [5] (empty)
563//     - cat1.1.2 [6]
564//   - cat1.2 [3]
565//   - cat1.3 [4]
566//
567// get_non_empty_sub_cat_ids will return :
568//   $ids[12] = 6;
569//   $ids[3]  = 3;
570//   $ids[4]  = 4;
571function get_non_empty_subcat_ids( $id_uppercat )
572{
573  global $user;
574
575  $ids = array();
576
577  $query = 'SELECT id,nb_images';
578  $query.= ' FROM '.PREFIX_TABLE.'categories';
579  $query.= ' WHERE id_uppercat ';
580  if ( !is_numeric( $id_uppercat ) ) $query.= 'is NULL';
581  else                               $query.= '= '.$id_uppercat;
582  // we must not show pictures of a forbidden category
583  foreach ( $user['restrictions'] as $restricted_cat ) {
584    $query.= ' AND id != '.$restricted_cat;
585  }
586  $query.= ' ORDER BY rank';
587  $query.= ';';
588
589  $result = mysql_query( $query );
590  while ( $row = mysql_fetch_array( $result ) )
591  {
592    // only categories with findable picture in any of its subcats is
593    // represented.
594    if ( ( $row['nb_images'] != 0 and $non_empty_cat = $row['id'] )
595         or $non_empty_cat = get_first_non_empty_cat_id( $row['id'] ) )
596    {
597      $ids[$row['id']] = $non_empty_cat;
598    }
599  }
600  return $ids;
601}
602
603// get_first_non_empty_cat_id returns the id of the first non empty
604// sub-category to the given uppercat. If no picture is found in any
605// subcategory, false is returned.
606function get_first_non_empty_cat_id( $id_uppercat )
607{
608  global $user;
609
610  $query = 'SELECT id,nb_images';
611  $query.= ' FROM '.PREFIX_TABLE.'categories';
612  $query.= ' WHERE id_uppercat = '.$id_uppercat;
613  // we must not show pictures of a forbidden category
614  foreach ( $user['restrictions'] as $restricted_cat ) {
615    $query.= ' AND id != '.$restricted_cat;
616  }
617  $query.= ' ORDER BY RAND()';
618  $query.= ';';
619  $result = mysql_query( $query );
620  while ( $row = mysql_fetch_array( $result ) )
621  {
622    if ( $row['nb_images'] > 0 )
623    {
624      return $row['id'];
625    }
626  }
627  $result = mysql_query( $query );
628  while ( $row = mysql_fetch_array( $result ) )
629  {
630    // recursive call
631    if ( $subcat = get_first_non_empty_cat_id( $row['id'] ) )
632    {
633      return $subcat;
634    }
635  }
636  return false;
637}
638?>
Note: See TracBrowser for help on using the repository browser.