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

Last change on this file since 657 was 657, checked in by plg, 19 years ago
  • user permissions ask update at each admin page generation. Table user_forbidden must be updated only if current user is not in administrative section
  • bug fixed : category.php, error on page title when non category selected
  • admin/search : bug on variable $PHP_SELF, replaced by $_SERVERPHP_SELF
  • admin/user_perm : inheritence management. When a category become authorized, all parent categories become authorized, when a category become forbidden, all child category become forbidden
  • no more recursivity in delete_categories function
  • new function get_fs_directories for future new method of synchronization
  • new function get_uppercat_ids replacing several pieces of code doing the same
  • new function get_fulldirs used for metadata function get_filelist and future new method of synchronization
  • new function get_fs for future new method of synchronization
  • typo correction on lang item "about_message"
  • no link to category privacy status management on user permission anymore (giving the menu item instead)
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 24.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-2004 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $RCSfile$
9// | last update   : $Date: 2004-12-25 19:33:36 +0000 (Sat, 25 Dec 2004) $
10// | last modifier : $Author: plg $
11// | revision      : $Revision: 657 $
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'] =
350        get_cat_display_name($page['cat_name'],
351                             '',
352                             false);
353      $page['where'] = ' WHERE category_id = '.$page['cat'];
354    }
355    else
356    {
357      if ($page['cat'] == 'search'
358          or $page['cat'] == 'most_visited'
359          or $page['cat'] == 'recent_pics'
360          or $page['cat'] == 'recent_cats'
361          or $page['cat'] == 'best_rated'
362          or $page['cat'] == 'calendar'
363          or $page['cat'] == 'list')
364      {
365        // we must not show pictures of a forbidden category
366        if ( $user['forbidden_categories'] != '' )
367        {
368          $forbidden = ' category_id NOT IN ';
369          $forbidden.= '('.$user['forbidden_categories'].')';
370        }
371      }
372      // search result
373      if ( $page['cat'] == 'search' )
374      {
375        // analyze search string given in URL (created in search.php)
376        $tokens = explode('|', $_GET['search']);
377
378        if (isset($tokens[1]) and $tokens[1] == 'AND')
379        {
380          $search['mode'] = 'AND';
381        }
382        else
383        {
384          $search['mode'] = 'OR';
385        }
386
387        $search_tokens = explode(';', $tokens[0]);
388        foreach ($search_tokens as $search_token)
389        {
390          $tokens = explode(':', $search_token);
391          $field_name = $tokens[0];
392          $field_content = $tokens[1];
393
394          $tokens = explode('~', $tokens[1]);
395          if (isset($tokens[1]))
396          {
397            $search['fields'][$field_name]['mode'] = $tokens[1];
398          }
399          else
400          {
401            $search['fields'][$field_name]['mode'] = '';
402          }
403
404          $search['fields'][$field_name]['words'] = array();
405          $tokens = explode(',', $tokens[0]);
406          foreach ($tokens as $token)
407          {
408            array_push($search['fields'][$field_name]['words'], $token);
409          }
410        }
411       
412        $page['title'] = $lang['search_result'];
413        if ( $calling_page == 'picture' )
414        {
415          $page['title'].= ' : <span style="font-style:italic;">';
416          $page['title'].= $_GET['search']."</span>";
417        }
418
419        // SQL where clauses are stored in $clauses array during query
420        // construction
421        $clauses = array();
422       
423        $textfields = array('file', 'name', 'comment', 'keywords', 'author');
424        foreach ($textfields as $textfield)
425        {
426          if (isset($search['fields'][$textfield]))
427          {
428            $local_clauses = array();
429            foreach ($search['fields'][$textfield]['words'] as $word)
430            {
431              array_push($local_clauses, $textfield." LIKE '%".$word."%'");
432            }
433            // adds brackets around where clauses
434            array_walk($local_clauses,create_function('&$s','$s="(".$s.")";'));
435            array_push($clauses,
436                       implode(' '.$search['fields'][$textfield]['mode'].' ',
437                               $local_clauses));
438          }
439        }
440
441        if (isset($search['fields']['allwords']))
442        {
443          $fields = array('file', 'name', 'comment', 'keywords');
444          // in the OR mode, request bust be :
445          // ((field1 LIKE '%word1%' OR field2 LIKE '%word1%')
446          // OR (field1 LIKE '%word2%' OR field2 LIKE '%word2%'))
447          //
448          // in the AND mode :
449          // ((field1 LIKE '%word1%' OR field2 LIKE '%word1%')
450          // AND (field1 LIKE '%word2%' OR field2 LIKE '%word2%'))
451          $word_clauses = array();
452          foreach ($search['fields']['allwords']['words'] as $word)
453          {
454            $field_clauses = array();
455            foreach ($fields as $field)
456            {
457              array_push($field_clauses, $field." LIKE '%".$word."%'");
458            }
459            // adds brackets around where clauses
460            array_push($word_clauses, implode(' OR ', $field_clauses));
461          }
462          array_walk($word_clauses, create_function('&$s','$s="(".$s.")";'));
463          array_push($clauses,
464                     implode(' '.$search['fields']['allwords']['mode'].' ',
465                               $word_clauses));
466        }
467
468        $datefields = array('date_available', 'date_creation');
469        foreach ($datefields as $datefield)
470        {
471          $key = $datefield;
472          if (isset($search['fields'][$key]))
473          {
474            $local_clause = $datefield." = '";
475            $local_clause.= str_replace('.', '-',
476                                        $search['fields'][$key]['words'][0]);
477            $local_clause.= "'";
478            array_push($clauses, $local_clause);
479          }
480
481          foreach (array('after','before') as $suffix)
482          {
483            $key = $datefield.'-'.$suffix;
484            if (isset($search['fields'][$key]))
485            {
486              $local_clause = $datefield;
487              if ($suffix == 'after')
488              {
489                $local_clause.= ' >';
490              }
491              else
492              {
493                $local_clause.= ' <';
494              }
495              if (isset($search['fields'][$key]['mode'])
496                  and $search['fields'][$key]['mode'] == 'inc')
497              {
498                $local_clause.= '=';
499              }
500              $local_clause.= " '";
501              $local_clause.= str_replace('.', '-',
502                                          $search['fields'][$key]['words'][0]);
503              $local_clause.= "'";
504              array_push($clauses, $local_clause);
505            }
506          }
507        }
508
509        if (isset($search['fields']['cat']))
510        {
511          if ($search['fields']['cat']['mode'] == 'sub_inc')
512          {
513            // searching all the categories id of sub-categories
514            $cat_ids = get_subcat_ids($search['fields']['cat']['words']);
515          }
516          else
517          {
518            $cat_ids = $search['fields']['cat']['words'];
519          }
520         
521          $local_clause = 'category_id IN ('.implode(',', $cat_ids).')';
522          array_push($clauses, $local_clause);
523        }
524
525        // adds brackets around where clauses
526        array_walk($clauses, create_function('&$s', '$s = "(".$s.")";'));
527        $page['where'] = 'WHERE '.implode(' '.$search['mode'].' ', $clauses);
528        if ( isset( $forbidden ) ) $page['where'].= ' AND '.$forbidden;
529
530        $query = '
531SELECT COUNT(DISTINCT(id)) AS nb_total_images
532  FROM '.IMAGES_TABLE.'
533    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
534  '.$page['where'].'
535;';
536        $url.= '&amp;search='.$_GET['search'];
537      }
538      // favorites displaying
539      else if ( $page['cat'] == 'fav' )
540      {
541        check_user_favorites();
542       
543        $page['title'] = $lang['favorites'];
544
545        $page['where'] = ', '.FAVORITES_TABLE.' AS fav';
546        $page['where'].= ' WHERE user_id = '.$user['id'];
547        $page['where'].= ' AND fav.image_id = id';
548     
549        $query = 'SELECT COUNT(*) AS nb_total_images';
550        $query.= ' FROM '.FAVORITES_TABLE;
551        $query.= ' WHERE user_id = '.$user['id'];
552        $query.= ';';
553      }
554      // pictures within the short period
555      else if ( $page['cat'] == 'recent_pics' )
556      {
557        $page['title'] = $lang['recent_pics_cat'];
558        // We must find the date corresponding to :
559        // today - $conf['periode_courte']
560        $date = time() - 60*60*24*$user['recent_period'];
561        $page['where'] = " WHERE date_available > '";
562        $page['where'].= date( 'Y-m-d', $date )."'";
563        if ( isset( $forbidden ) ) $page['where'].= ' AND '.$forbidden;
564
565        $query = '
566SELECT COUNT(DISTINCT(id)) AS nb_total_images
567  FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic
568    ON id = ic.image_id
569  '.$page['where'].'
570;';
571      }
572      // categories containing recent pictures
573      else if ( $page['cat'] == 'recent_cats' )
574      {
575        $page['title'] = $lang['recent_cats_cat'];
576        $page['cat_nb_images'] = 0;
577      }
578      // most visited pictures
579      else if ( $page['cat'] == 'most_visited' )
580      {
581        $page['title'] = $conf['top_number'].' '.$lang['most_visited_cat'];
582
583        $page['where'] = 'WHERE hit > 0';
584        if (isset($forbidden))
585        {
586          $page['where'] = "\n".'    AND '.$forbidden;
587        }
588
589        $conf['order_by'] = ' ORDER BY hit DESC, file ASC';
590        $page['cat_nb_images'] = $conf['top_number'];
591        if ( isset( $page['start'] )
592             and ($page['start']+$user['nb_image_page']>=$conf['top_number']))
593        {
594          $page['nb_image_page'] = $conf['top_number'] - $page['start'];
595        }
596      }
597      else if ( $page['cat'] == 'calendar' )
598      {
599        $page['cat_nb_images'] = 0;
600        $page['title'] = $lang['calendar'];
601        if (isset($_GET['year'])
602            and preg_match('/^\d+$/', $_GET['year']))
603        {
604          $page['calendar_year'] = (int)$_GET['year'];
605        }
606        if (isset($_GET['month'])
607            and preg_match('/^(\d+)\.(\d{2})$/', $_GET['month'], $matches))
608        {
609          $page['calendar_year'] = (int)$matches[1];
610          $page['calendar_month'] = (int)$matches[2];
611        }
612        if (isset($_GET['day'])
613            and preg_match('/^(\d+)\.(\d{2})\.(\d{2})$/',
614                           $_GET['day'],
615                           $matches))
616        {
617          $page['calendar_year'] = (int)$matches[1];
618          $page['calendar_month'] = (int)$matches[2];
619          $page['calendar_day'] = (int)$matches[3];
620        }
621        if (isset($page['calendar_year']))
622        {
623          $page['title'] .= ' (';
624          if (isset($page['calendar_day']))
625          {
626            $unixdate = mktime(0,0,0,
627                               $page['calendar_month'],
628                               $page['calendar_day'],
629                               $page['calendar_year']);
630            $page['title'].= $lang['day'][date("w", $unixdate)];
631            $page['title'].= ' '.$page['calendar_day'].', ';
632          }
633          if (isset($page['calendar_month']))
634          {
635            $page['title'] .= $lang['month'][$page['calendar_month']].' ';
636          }
637          $page['title'] .= $page['calendar_year'];
638          $page['title'] .= ')';
639        }
640       
641        $page['where'] = 'WHERE '.$conf['calendar_datefield'].' IS NOT NULL';
642        if (isset($forbidden))
643        {
644          $page['where'].= ' AND '.$forbidden;
645        }
646      }
647      else if ($page['cat'] == 'best_rated')
648      {
649        $page['title'] = $conf['top_number'].' '.$lang['best_rated_cat'];
650
651        $page['where'] = ' WHERE average_rate IS NOT NULL';
652       
653        if (isset($forbidden))
654        {
655          $page['where'].= ' AND '.$forbidden;
656        }
657
658        $conf['order_by'] = ' ORDER BY average_rate DESC, id ASC';
659
660        // $page['cat_nb_images'] equals $conf['top_number'] unless there
661        // are less rated items
662        $query ='
663SELECT COUNT(1) AS count
664  FROM '.IMAGES_TABLE.'
665    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
666  '.$page['where'].'
667;';
668        $row = mysql_fetch_array(pwg_query($query));
669        if ($row['count'] < $conf['top_number'])
670        {
671          $page['cat_nb_images'] = $row['count'];
672        }
673        else
674        {
675          $page['cat_nb_images'] = $conf['top_number'];
676        }
677        unset($query);
678         
679
680        if (isset($page['start'])
681            and ($page['start']+$user['nb_image_page']>=$conf['top_number']))
682        {
683          $page['nb_image_page'] = $conf['top_number'] - $page['start'];
684        }
685      }
686      else if ($page['cat'] == 'list')
687      {
688        $page['title'] = $lang['random_cat'];
689         
690        $page['where'] = 'WHERE 1=1';
691        if (isset($forbidden))
692        {
693          $page['where'].= ' AND '.$forbidden;
694        }
695        $page['where'].= ' AND image_id IN ('.$_GET['list'].')';
696        $page['cat_nb_images'] = count(explode(',', $_GET['list']));
697
698        $url.= '&amp;list='.$_GET['list'];
699      }
700
701      if (isset($query))
702      {
703        $result = pwg_query( $query );
704        $row = mysql_fetch_array( $result );
705        $page['cat_nb_images'] = $row['nb_total_images'];
706      }
707    }
708    if ( $calling_page == 'category' )
709    {
710      $page['navigation_bar'] =
711        create_navigation_bar( $url, $page['cat_nb_images'], $page['start'],
712                               $user['nb_image_page'], 'back' );
713    }
714  }
715  else
716  {
717    $page['title'] = $lang['no_category'];
718  }
719  pwg_debug( 'end initialize_category' );
720}
721
722function display_select_categories($categories,
723                                   $selecteds,
724                                   $blockname,
725                                   $fullname = true)
726{
727  global $template;
728
729  foreach ($categories as $category)
730  {
731    $selected = '';
732    if (in_array($category['id'], $selecteds))
733    {
734      $selected = ' selected="selected"';
735    }
736
737    if ($fullname)
738    {
739      $option = get_cat_display_name_cache($category['uppercats'],
740                                           '',
741                                           false);
742    }
743    else
744    {
745      $option = str_repeat('&nbsp;',
746                           (3 * substr_count($category['global_rank'], '.')));
747      $option.= '- '.$category['name'];
748    }
749   
750    $template->assign_block_vars(
751      $blockname,
752      array('SELECTED'=>$selected,
753            'VALUE'=>$category['id'],
754            'OPTION'=>$option
755        ));
756  }
757}
758
759function display_select_cat_wrapper($query, $selecteds, $blockname,
760                                    $fullname = true)
761{
762  $result = pwg_query($query);
763  $categories = array();
764  if (!empty($result))
765  {
766    while ($row = mysql_fetch_array($result))
767    {
768      array_push($categories, $row);
769    }
770  }
771  usort($categories, 'global_rank_compare');
772  display_select_categories($categories, $selecteds, $blockname, $fullname);
773}
774
775/**
776 * returns all subcategory identifiers of given category ids
777 *
778 * @param array ids
779 * @return array
780 */
781function get_subcat_ids($ids)
782{
783  $query = '
784SELECT DISTINCT(id)
785  FROM '.CATEGORIES_TABLE.'
786  WHERE ';
787  foreach ($ids as $num => $category_id)
788  {
789    if ($num > 0)
790    {
791      $query.= '
792    OR ';
793    }
794    $query.= 'uppercats REGEXP \'(^|,)'.$category_id.'(,|$)\'';
795  }
796  $query.= '
797;';
798  $result = pwg_query($query);
799
800  $subcats = array();
801  while ($row = mysql_fetch_array($result))
802  {
803    array_push($subcats, $row['id']);
804  }
805  return $subcats;
806}
807
808function global_rank_compare($a, $b)
809{
810  return strnatcasecmp($a['global_rank'], $b['global_rank']);
811}
812?>
Note: See TracBrowser for help on using the repository browser.