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

Last change on this file since 462 was 460, checked in by z0rglub, 20 years ago

replacement of short_period with recent_period

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