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

Last change on this file since 926 was 867, checked in by plg, 19 years ago
  • bug 101 fixed: correction reported from branch 1.4
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 24.9 KB
RevLine 
[2]1<?php
[362]2// +-----------------------------------------------------------------------+
[593]3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
[675]5// | Copyright (C) 2003-2005 PhpWebGallery Team - http://phpwebgallery.net |
[362]6// +-----------------------------------------------------------------------+
[593]7// | branch        : BSF (Best So Far)
[362]8// | file          : $RCSfile$
9// | last update   : $Date: 2005-09-18 21:09:44 +0000 (Sun, 18 Sep 2005) $
10// | last modifier : $Author: plg $
11// | revision      : $Revision: 867 $
12// +-----------------------------------------------------------------------+
13// | This program is free software; you can redistribute it and/or modify  |
14// | it under the terms of the GNU General Public License as published by  |
15// | the Free Software Foundation                                          |
16// |                                                                       |
17// | This program is distributed in the hope that it will be useful, but   |
18// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
19// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
20// | General Public License for more details.                              |
21// |                                                                       |
22// | You should have received a copy of the GNU General Public License     |
23// | along with this program; if not, write to the Free Software           |
24// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
25// | USA.                                                                  |
26// +-----------------------------------------------------------------------+
[2]27
[423]28/**
29 * Provides functions to handle categories.
30 *
31 *
32 */
33
34/**
35 * Is the category accessible to the connected user ?
36 *
37 * Note : if the user is not authorized to see this category, page creation
38 * ends (exit command in this function)
39 *
40 * @param int category id to verify
41 * @return void
42 */
[808]43function check_restrictions($category_id)
[2]44{
[808]45  global $user, $lang;
[2]46
[808]47  if (in_array($category_id, explode(',', $user['forbidden_categories'])))
[2]48  {
49    echo '<div style="text-align:center;">'.$lang['access_forbiden'].'<br />';
50    echo '<a href="'.add_session_id( './category.php' ).'">';
51    echo $lang['thumbnails'].'</a></div>';
52    exit();
53  }
54}
[345]55
[423]56/**
57 * Checks whether the argument is a right parameter category id
58 *
59 * The argument is a right parameter if corresponds to one of these :
60 *
61 *  - is numeric and corresponds to a category in the database
[429]62 *  - equals 'fav' (for favorites)
63 *  - equals 'search' (when the result of a search is displayed)
64 *  - equals 'most_visited'
65 *  - equals 'best_rated'
[437]66 *  - equals 'recent_pics'
67 *  - equals 'recent_cats'
[510]68 *  - equals 'calendar'
[605]69 *  - equals 'list'
[423]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 */
[2]76function check_cat_id( $cat )
77{
[13]78  global $page;
79
[2]80  unset( $page['cat'] );
81  if ( isset( $cat ) )
82  {
[345]83    if ( isset( $page['plain_structure'][$cat] ) )
[2]84    {
[345]85      $page['cat'] = $cat;
[26]86    }
87    else if ( is_numeric( $cat ) )
88    {
89      $query = 'SELECT id';
[345]90      $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id = '.$cat.';';
[587]91      $result = pwg_query( $query );
[2]92      if ( mysql_num_rows( $result ) != 0 )
93      {
94        $page['cat'] = $cat;
95      }
96    }
[26]97    if ( $cat == 'fav'
98         or $cat == 'most_visited'
99         or $cat == 'best_rated'
[434]100         or $cat == 'recent_pics'
[437]101         or $cat == 'recent_cats'
[605]102         or $cat == 'calendar' )
[2]103    {
104      $page['cat'] = $cat;
105    }
[456]106    if ($cat == 'search' and isset($_GET['search']))
107    {
108      $page['cat'] = $cat;
109    }
[605]110    if ($cat == 'list'
111        and isset($_GET['list'])
112        and preg_match('/^\d+(,\d+)*$/', $_GET['list']))
113    {
114      $page['cat'] = 'list';
115    }
[2]116  }
117}
118
[614]119function get_categories_menu()
[2]120{
[345]121  global $page,$user;
[2]122 
[614]123  $infos = array('');
[345]124 
[589]125  $query = '
[614]126SELECT name,id,date_last,nb_images,global_rank
[589]127  FROM '.CATEGORIES_TABLE.'
128  WHERE 1 = 1'; // stupid but permit using AND after it !
129  if (!$user['expand'])
[345]130  {
[589]131    $query.= '
132    AND (id_uppercat is NULL';
133    if (isset ($page['tab_expand']) and count($page['tab_expand']) > 0)
[345]134    {
[387]135      $query.= ' OR id_uppercat IN ('.implode(',',$page['tab_expand']).')';
[345]136    }
137    $query.= ')';
[2]138  }
[589]139  if ($user['forbidden_categories'] != '')
[345]140  {
[589]141    $query.= '
142    AND id NOT IN ('.$user['forbidden_categories'].')';
[345]143  }
[589]144  $query.= '
145;';
[26]146
[589]147  $result = pwg_query($query);
[614]148  $cats = array();
[589]149  while ($row = mysql_fetch_array($result))
[2]150  {
[614]151    array_push($cats, $row);
[26]152  }
[614]153  usort($cats, 'global_rank_compare');
[2]154
[614]155  return get_html_menu_category($cats);
[26]156}
[2]157
[761]158/**
159 * returns the total number of elements viewable in the gallery by the
160 * connected user
161 *
162 * @return int
163 */
[345]164function count_user_total_images()
165{
166  global $user;
167
[761]168  $query = '
169SELECT COUNT(DISTINCT(image_id)) as total
[808]170  FROM '.IMAGE_CATEGORY_TABLE.'
171  WHERE category_id NOT IN ('.$user['forbidden_categories'].')
[761]172;';
[808]173  list($total) = mysql_fetch_array(pwg_query($query));
174 
175  return $total;
[345]176}
177
[423]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 */
[2]195function get_cat_info( $id )
196{
[603]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));
[345]207
208  $cat = array();
[603]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    }
[345]219    // If the field is true or false, the variable is transformed into a
220    // boolean value.
[603]221    if ($cat[$info] == 'true' or $cat[$info] == 'false')
[345]222    {
223      $cat[$info] = get_boolean( $cat[$info] );
224    }
225  }
[603]226  $cat['comment'] = nl2br($cat['comment']);
[345]227
[672]228  $names = array();
[603]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))
[2]236  {
[672]237    $names[$row['id']] = $row['name'];
[2]238  }
[672]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  }
[345]246 
[2]247  return $cat;
248}
[61]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{
[579]257  return get_site_url($category_id).get_local_dir($category_id);
[61]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
[345]268  $uppercats = '';
269  $local_dir = '';
270
271  if ( isset( $page['plain_structure'][$category_id]['uppercats'] ) )
[61]272  {
[345]273    $uppercats = $page['plain_structure'][$category_id]['uppercats'];
[61]274  }
[345]275  else
276  {
277    $query = 'SELECT uppercats';
278    $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id = '.$category_id;
279    $query.= ';';
[587]280    $row = mysql_fetch_array( pwg_query( $query ) );
[345]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.= ';';
[587]290  $result = pwg_query( $query );
[345]291  while( $row = mysql_fetch_array( $result ) )
292  {
293    $database_dirs[$row['id']] = $row['dir'];
294  }
[579]295  foreach ($upper_array as $id)
296  {
[345]297    $local_dir.= $database_dirs[$id].'/';
298  }
299
300  return $local_dir;
[61]301}
302
303// retrieving the site url : "http://domain.com/gallery/" or
304// simply "./galleries/"
[579]305function get_site_url($category_id)
[61]306{
307  global $page;
308
[579]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;';
[587]315  $row = mysql_fetch_array(pwg_query($query));
[61]316  return $row['galleries_url'];
317}
318
[2]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
[605]330//       - defined list (used for random)
[2]331// 3. determination of the title of the page
332// 4. creation of the navigation bar
333function initialize_category( $calling_page = 'category' )
334{
[345]335  pwg_debug( 'start initialize_category' );
[13]336  global $page,$lang,$user,$conf;
[38]337
[2]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
[621]344    $url = PHPWG_ROOT_PATH.'category.php?cat='.$page['cat'];
[345]345    if ( isset($page['expand']) ) $url.= '&amp;expand='.$page['expand'];
[2]346    // simple category
347    if ( is_numeric( $page['cat'] ) )
348    {
349      $result = get_cat_info( $page['cat'] );
[38]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'];
[602]356      $page['cat_commentable'] = $result['commentable'];
[345]357      $page['uppercats']      = $result['uppercats'];
[641]358      $page['title'] =
359        get_cat_display_name($page['cat_name'],
[654]360                             '',
[641]361                             false);
[61]362      $page['where'] = ' WHERE category_id = '.$page['cat'];
[2]363    }
364    else
365    {
[605]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')
[17]373      {
374        // we must not show pictures of a forbidden category
[345]375        if ( $user['forbidden_categories'] != '' )
[67]376        {
[345]377          $forbidden = ' category_id NOT IN ';
378          $forbidden.= '('.$user['forbidden_categories'].')';
[17]379        }
380      }
[2]381      // search result
382      if ( $page['cat'] == 'search' )
383      {
[456]384        // analyze search string given in URL (created in search.php)
385        $tokens = explode('|', $_GET['search']);
386
387        if (isset($tokens[1]) and $tokens[1] == 'AND')
388        {
389          $search['mode'] = 'AND';
390        }
391        else
392        {
393          $search['mode'] = 'OR';
394        }
395
[867]396        $search_tokens = explode('--', $tokens[0]);
[456]397        foreach ($search_tokens as $search_token)
398        {
399          $tokens = explode(':', $search_token);
400          $field_name = $tokens[0];
401          $field_content = $tokens[1];
402
403          $tokens = explode('~', $tokens[1]);
404          if (isset($tokens[1]))
405          {
406            $search['fields'][$field_name]['mode'] = $tokens[1];
407          }
408          else
409          {
410            $search['fields'][$field_name]['mode'] = '';
411          }
412
413          $search['fields'][$field_name]['words'] = array();
414          $tokens = explode(',', $tokens[0]);
415          foreach ($tokens as $token)
416          {
[715]417            array_push($search['fields'][$field_name]['words'],
418                       htmlentities($token));
[456]419          }
420        }
421       
[2]422        $page['title'] = $lang['search_result'];
423        if ( $calling_page == 'picture' )
424        {
425          $page['title'].= ' : <span style="font-style:italic;">';
426          $page['title'].= $_GET['search']."</span>";
427        }
428
[456]429        // SQL where clauses are stored in $clauses array during query
430        // construction
[634]431        $clauses = array();
432       
[710]433        $textfields = array('file', 'name', 'comment', 'keywords', 'author');
434        foreach ($textfields as $textfield)
435        {
436          if (isset($search['fields'][$textfield]))
[17]437          {
[456]438            $local_clauses = array();
[710]439            foreach ($search['fields'][$textfield]['words'] as $word)
[456]440            {
[710]441              array_push($local_clauses, $textfield." LIKE '%".$word."%'");
[17]442            }
[456]443            // adds brackets around where clauses
444            array_walk($local_clauses,create_function('&$s','$s="(".$s.")";'));
[634]445            array_push($clauses,
[710]446                       implode(' '.$search['fields'][$textfield]['mode'].' ',
[456]447                               $local_clauses));
[17]448          }
[710]449        }
[456]450
[634]451        if (isset($search['fields']['allwords']))
452        {
[710]453          $fields = array('file', 'name', 'comment', 'keywords', 'author');
[634]454          // in the OR mode, request bust be :
455          // ((field1 LIKE '%word1%' OR field2 LIKE '%word1%')
456          // OR (field1 LIKE '%word2%' OR field2 LIKE '%word2%'))
457          //
458          // in the AND mode :
459          // ((field1 LIKE '%word1%' OR field2 LIKE '%word1%')
460          // AND (field1 LIKE '%word2%' OR field2 LIKE '%word2%'))
461          $word_clauses = array();
462          foreach ($search['fields']['allwords']['words'] as $word)
463          {
464            $field_clauses = array();
[710]465            foreach ($fields as $field)
[634]466            {
467              array_push($field_clauses, $field." LIKE '%".$word."%'");
468            }
469            // adds brackets around where clauses
470            array_push($word_clauses, implode(' OR ', $field_clauses));
471          }
472          array_walk($word_clauses, create_function('&$s','$s="(".$s.")";'));
473          array_push($clauses,
474                     implode(' '.$search['fields']['allwords']['mode'].' ',
475                               $word_clauses));
476        }
477
[456]478        $datefields = array('date_available', 'date_creation');
479        foreach ($datefields as $datefield)
480        {
481          $key = $datefield;
482          if (isset($search['fields'][$key]))
[17]483          {
[634]484            $local_clause = $datefield." = '";
485            $local_clause.= str_replace('.', '-',
[456]486                                        $search['fields'][$key]['words'][0]);
[634]487            $local_clause.= "'";
488            array_push($clauses, $local_clause);
489          }
490
491          foreach (array('after','before') as $suffix)
492          {
493            $key = $datefield.'-'.$suffix;
494            if (isset($search['fields'][$key]))
495            {
496              $local_clause = $datefield;
497              if ($suffix == 'after')
498              {
499                $local_clause.= ' >';
500              }
501              else
502              {
503                $local_clause.= ' <';
504              }
505              if (isset($search['fields'][$key]['mode'])
506                  and $search['fields'][$key]['mode'] == 'inc')
507              {
508                $local_clause.= '=';
509              }
510              $local_clause.= " '";
511              $local_clause.= str_replace('.', '-',
512                                          $search['fields'][$key]['words'][0]);
513              $local_clause.= "'";
514              array_push($clauses, $local_clause);
515            }
516          }
517        }
518
[456]519        if (isset($search['fields']['cat']))
520        {
[502]521          if ($search['fields']['cat']['mode'] == 'sub_inc')
522          {
523            // searching all the categories id of sub-categories
[652]524            $cat_ids = get_subcat_ids($search['fields']['cat']['words']);
[502]525          }
526          else
527          {
[652]528            $cat_ids = $search['fields']['cat']['words'];
[502]529          }
[652]530         
531          $local_clause = 'category_id IN ('.implode(',', $cat_ids).')';
532          array_push($clauses, $local_clause);
[456]533        }
534
535        // adds brackets around where clauses
536        array_walk($clauses, create_function('&$s', '$s = "(".$s.")";'));
[634]537        $page['where'] = 'WHERE '.implode(' '.$search['mode'].' ', $clauses);
[345]538        if ( isset( $forbidden ) ) $page['where'].= ' AND '.$forbidden;
[17]539
[456]540        $query = '
541SELECT COUNT(DISTINCT(id)) AS nb_total_images
542  FROM '.IMAGES_TABLE.'
543    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
544  '.$page['where'].'
545;';
546        $url.= '&amp;search='.$_GET['search'];
[2]547      }
548      // favorites displaying
549      else if ( $page['cat'] == 'fav' )
550      {
[647]551        check_user_favorites();
552       
[2]553        $page['title'] = $lang['favorites'];
554
[345]555        $page['where'] = ', '.FAVORITES_TABLE.' AS fav';
[16]556        $page['where'].= ' WHERE user_id = '.$user['id'];
[64]557        $page['where'].= ' AND fav.image_id = id';
[2]558     
[16]559        $query = 'SELECT COUNT(*) AS nb_total_images';
[345]560        $query.= ' FROM '.FAVORITES_TABLE;
[16]561        $query.= ' WHERE user_id = '.$user['id'];
[2]562        $query.= ';';
563      }
564      // pictures within the short period
[434]565      else if ( $page['cat'] == 'recent_pics' )
[2]566      {
[496]567        $page['title'] = $lang['recent_pics_cat'];
[2]568        // We must find the date corresponding to :
569        // today - $conf['periode_courte']
[460]570        $date = time() - 60*60*24*$user['recent_period'];
[16]571        $page['where'] = " WHERE date_available > '";
[2]572        $page['where'].= date( 'Y-m-d', $date )."'";
[345]573        if ( isset( $forbidden ) ) $page['where'].= ' AND '.$forbidden;
[2]574
[636]575        $query = '
576SELECT COUNT(DISTINCT(id)) AS nb_total_images
577  FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic
578    ON id = ic.image_id
579  '.$page['where'].'
580;';
[2]581      }
[437]582      // categories containing recent pictures
583      else if ( $page['cat'] == 'recent_cats' )
584      {
[496]585        $page['title'] = $lang['recent_cats_cat'];
[437]586        $page['cat_nb_images'] = 0;
587      }
[2]588      // most visited pictures
589      else if ( $page['cat'] == 'most_visited' )
590      {
591        $page['title'] = $conf['top_number'].' '.$lang['most_visited_cat'];
[587]592
593        $page['where'] = 'WHERE hit > 0';
594        if (isset($forbidden))
595        {
596          $page['where'] = "\n".'    AND '.$forbidden;
597        }
598
[16]599        $conf['order_by'] = ' ORDER BY hit DESC, file ASC';
[694]600
601        // $page['cat_nb_images'] equals $conf['top_number'] unless there
602        // are less visited items
603        $query ='
604SELECT COUNT(DISTINCT(id)) AS count
605  FROM '.IMAGES_TABLE.'
606    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
607  '.$page['where'].'
608;';
609        $row = mysql_fetch_array(pwg_query($query));
610        if ($row['count'] < $conf['top_number'])
611        {
612          $page['cat_nb_images'] = $row['count'];
613        }
614        else
615        {
616          $page['cat_nb_images'] = $conf['top_number'];
617        }
618        unset($query);
619       
[345]620        if ( isset( $page['start'] )
621             and ($page['start']+$user['nb_image_page']>=$conf['top_number']))
[2]622        {
623          $page['nb_image_page'] = $conf['top_number'] - $page['start'];
624        }
625      }
[429]626      else if ( $page['cat'] == 'calendar' )
627      {
628        $page['cat_nb_images'] = 0;
629        $page['title'] = $lang['calendar'];
[442]630        if (isset($_GET['year'])
631            and preg_match('/^\d+$/', $_GET['year']))
[429]632        {
633          $page['calendar_year'] = (int)$_GET['year'];
634        }
[442]635        if (isset($_GET['month'])
636            and preg_match('/^(\d+)\.(\d{2})$/', $_GET['month'], $matches))
[429]637        {
638          $page['calendar_year'] = (int)$matches[1];
639          $page['calendar_month'] = (int)$matches[2];
640        }
[442]641        if (isset($_GET['day'])
642            and preg_match('/^(\d+)\.(\d{2})\.(\d{2})$/',
643                           $_GET['day'],
644                           $matches))
[429]645        {
[442]646          $page['calendar_year'] = (int)$matches[1];
647          $page['calendar_month'] = (int)$matches[2];
648          $page['calendar_day'] = (int)$matches[3];
649        }
650        if (isset($page['calendar_year']))
651        {
[429]652          $page['title'] .= ' (';
[442]653          if (isset($page['calendar_day']))
[429]654          {
[698]655            if ($page['calendar_year'] >= 1970)
656            {
657              $unixdate = mktime(0,0,0,
658                                 $page['calendar_month'],
659                                 $page['calendar_day'],
660                                 $page['calendar_year']);
661              $page['title'].= $lang['day'][date("w", $unixdate)];
662            }
[442]663            $page['title'].= ' '.$page['calendar_day'].', ';
664          }
665          if (isset($page['calendar_month']))
666          {
[429]667            $page['title'] .= $lang['month'][$page['calendar_month']].' ';
668          }
669          $page['title'] .= $page['calendar_year'];
670          $page['title'] .= ')';
671        }
[497]672       
673        $page['where'] = 'WHERE '.$conf['calendar_datefield'].' IS NOT NULL';
[442]674        if (isset($forbidden))
[429]675        {
[497]676          $page['where'].= ' AND '.$forbidden;
[429]677        }
678      }
[507]679      else if ($page['cat'] == 'best_rated')
680      {
681        $page['title'] = $conf['top_number'].' '.$lang['best_rated_cat'];
[67]682
[507]683        $page['where'] = ' WHERE average_rate IS NOT NULL';
684       
685        if (isset($forbidden))
686        {
[510]687          $page['where'].= ' AND '.$forbidden;
[507]688        }
689
690        $conf['order_by'] = ' ORDER BY average_rate DESC, id ASC';
691
692        // $page['cat_nb_images'] equals $conf['top_number'] unless there
693        // are less rated items
694        $query ='
[676]695SELECT COUNT(DISTINCT(id)) AS count
[507]696  FROM '.IMAGES_TABLE.'
[652]697    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
[507]698  '.$page['where'].'
699;';
[587]700        $row = mysql_fetch_array(pwg_query($query));
[507]701        if ($row['count'] < $conf['top_number'])
702        {
703          $page['cat_nb_images'] = $row['count'];
704        }
705        else
706        {
707          $page['cat_nb_images'] = $conf['top_number'];
708        }
709        unset($query);
710         
711
712        if (isset($page['start'])
713            and ($page['start']+$user['nb_image_page']>=$conf['top_number']))
714        {
715          $page['nb_image_page'] = $conf['top_number'] - $page['start'];
716        }
717      }
[605]718      else if ($page['cat'] == 'list')
[510]719      {
720        $page['title'] = $lang['random_cat'];
721         
[605]722        $page['where'] = 'WHERE 1=1';
[510]723        if (isset($forbidden))
724        {
[605]725          $page['where'].= ' AND '.$forbidden;
[510]726        }
[605]727        $page['where'].= ' AND image_id IN ('.$_GET['list'].')';
728        $page['cat_nb_images'] = count(explode(',', $_GET['list']));
[617]729
730        $url.= '&amp;list='.$_GET['list'];
[510]731      }
732
[442]733      if (isset($query))
[2]734      {
[587]735        $result = pwg_query( $query );
[2]736        $row = mysql_fetch_array( $result );
737        $page['cat_nb_images'] = $row['nb_total_images'];
738      }
739    }
740    if ( $calling_page == 'category' )
741    {
742      $page['navigation_bar'] =
743        create_navigation_bar( $url, $page['cat_nb_images'], $page['start'],
744                               $user['nb_image_page'], 'back' );
745    }
746  }
747  else
748  {
[657]749    $page['title'] = $lang['no_category'];
[2]750  }
[345]751  pwg_debug( 'end initialize_category' );
[2]752}
[16]753
[589]754function display_select_categories($categories,
755                                   $selecteds,
[602]756                                   $blockname,
[614]757                                   $fullname = true)
[589]758{
[614]759  global $template;
[589]760
761  foreach ($categories as $category)
762  {
[614]763    $selected = '';
764    if (in_array($category['id'], $selecteds))
[589]765    {
[614]766      $selected = ' selected="selected"';
767    }
[589]768
[614]769    if ($fullname)
770    {
771      $option = get_cat_display_name_cache($category['uppercats'],
772                                           '',
773                                           false);
[589]774    }
[614]775    else
776    {
777      $option = str_repeat('&nbsp;',
778                           (3 * substr_count($category['global_rank'], '.')));
779      $option.= '- '.$category['name'];
780    }
781   
782    $template->assign_block_vars(
783      $blockname,
784      array('SELECTED'=>$selected,
785            'VALUE'=>$category['id'],
786            'OPTION'=>$option
787        ));
[589]788  }
789}
[603]790
[614]791function display_select_cat_wrapper($query, $selecteds, $blockname,
792                                    $fullname = true)
793{
794  $result = pwg_query($query);
795  $categories = array();
[655]796  if (!empty($result))
797  {
[657]798    while ($row = mysql_fetch_array($result))
799    {
800      array_push($categories, $row);
801    }
[614]802  }
803  usort($categories, 'global_rank_compare');
804  display_select_categories($categories, $selecteds, $blockname, $fullname);
805}
806
[603]807/**
808 * returns all subcategory identifiers of given category ids
809 *
810 * @param array ids
811 * @return array
812 */
813function get_subcat_ids($ids)
814{
815  $query = '
816SELECT DISTINCT(id)
817  FROM '.CATEGORIES_TABLE.'
818  WHERE ';
819  foreach ($ids as $num => $category_id)
820  {
821    if ($num > 0)
822    {
823      $query.= '
824    OR ';
825    }
826    $query.= 'uppercats REGEXP \'(^|,)'.$category_id.'(,|$)\'';
827  }
828  $query.= '
829;';
830  $result = pwg_query($query);
831
832  $subcats = array();
833  while ($row = mysql_fetch_array($result))
834  {
835    array_push($subcats, $row['id']);
836  }
837  return $subcats;
838}
[614]839
840function global_rank_compare($a, $b)
841{
842  return strnatcasecmp($a['global_rank'], $b['global_rank']);
843}
[362]844?>
Note: See TracBrowser for help on using the repository browser.