source: branches/branch-1_5/include/functions_category.inc.php @ 1005

Last change on this file since 1005 was 1005, checked in by nikrou, 18 years ago

Revert to revision 1002

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