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

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