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

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