aboutsummaryrefslogtreecommitdiffstats
path: root/include/functions_category.inc.php
blob: 1c04c70d22fe835e4089b30509f7845da5cf7470 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
<?php
// +-----------------------------------------------------------------------+
// | PhpWebGallery - a PHP based picture gallery                           |
// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
// | Copyright (C) 2003-2004 PhpWebGallery Team - http://phpwebgallery.net |
// +-----------------------------------------------------------------------+
// | branch        : BSF (Best So Far)
// | file          : $RCSfile$
// | last update   : $Date$
// | last modifier : $Author$
// | revision      : $Revision$
// +-----------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or modify  |
// | it under the terms of the GNU General Public License as published by  |
// | the Free Software Foundation                                          |
// |                                                                       |
// | This program is distributed in the hope that it will be useful, but   |
// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
// | General Public License for more details.                              |
// |                                                                       |
// | You should have received a copy of the GNU General Public License     |
// | along with this program; if not, write to the Free Software           |
// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
// | USA.                                                                  |
// +-----------------------------------------------------------------------+

/**
 * Provides functions to handle categories.
 *
 * 
 */

/**
 * Is the category accessible to the connected user ?
 *
 * Note : if the user is not authorized to see this category, page creation
 * ends (exit command in this function)
 *
 * @param int category id to verify
 * @return void
 */
function check_restrictions( $category_id )
{
  global $user,$lang;

  if ( in_array( $category_id, $user['restrictions'] ) )
  {
    echo '<div style="text-align:center;">'.$lang['access_forbiden'].'<br />';
    echo '<a href="'.add_session_id( './category.php' ).'">';
    echo $lang['thumbnails'].'</a></div>';
    exit();
  }
}

/**
 * Checks whether the argument is a right parameter category id
 *
 * The argument is a right parameter if corresponds to one of these :
 *
 *  - is numeric and corresponds to a category in the database
 *  - equals 'fav' (for favorites)
 *  - equals 'search' (when the result of a search is displayed)
 *  - equals 'most_visited'
 *  - equals 'best_rated'
 *  - equals 'recent_pics'
 *  - equals 'recent_cats'
 *  - equals 'calendar'
 *  - equals 'list'
 *
 * The function fills the global var $page['cat'] and returns nothing
 *
 * @param mixed category id or special category name
 * @return void
 */
function check_cat_id( $cat )
{
  global $page;

  unset( $page['cat'] );
  if ( isset( $cat ) )
  {
    if ( isset( $page['plain_structure'][$cat] ) )
    {
      $page['cat'] = $cat;
    }
    else if ( is_numeric( $cat ) )
    {
      $query = 'SELECT id';
      $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id = '.$cat.';';
      $result = pwg_query( $query );
      if ( mysql_num_rows( $result ) != 0 )
      {
        $page['cat'] = $cat;
      }
    }
    if ( $cat == 'fav'
         or $cat == 'most_visited'
         or $cat == 'best_rated'
         or $cat == 'recent_pics'
         or $cat == 'recent_cats'
         or $cat == 'calendar' )
    {
      $page['cat'] = $cat;
    }
    if ($cat == 'search' and isset($_GET['search']))
    {
      $page['cat'] = $cat;
    }
    if ($cat == 'list'
        and isset($_GET['list'])
        and preg_match('/^\d+(,\d+)*$/', $_GET['list']))
    {
      $page['cat'] = 'list';
    }
  }
}

function get_categories_menu()
{
  global $page,$user;
  
  $infos = array('');
  
  $query = '
SELECT name,id,date_last,nb_images,global_rank
  FROM '.CATEGORIES_TABLE.'
  WHERE 1 = 1'; // stupid but permit using AND after it !
  if (!$user['expand'])
  {
    $query.= '
    AND (id_uppercat is NULL';
    if (isset ($page['tab_expand']) and count($page['tab_expand']) > 0)
    {
      $query.= ' OR id_uppercat IN ('.implode(',',$page['tab_expand']).')';
    }
    $query.= ')';
  }
  if ($user['forbidden_categories'] != '')
  {
    $query.= '
    AND id NOT IN ('.$user['forbidden_categories'].')';
  }
  $query.= '
;';

  $result = pwg_query($query);
  $cats = array();
  while ($row = mysql_fetch_array($result))
  {
    array_push($cats, $row);
  }
  usort($cats, 'global_rank_compare');

  return get_html_menu_category($cats);
}

function count_user_total_images()
{
  global $user;

  $query = 'SELECT SUM(nb_images) AS total';
  $query.= ' FROM '.CATEGORIES_TABLE;
  if ( count( $user['restrictions'] ) > 0 )
    $query.= ' WHERE id NOT IN ('.$user['forbidden_categories'].')';
  $query.= ';';
 
  $row = mysql_fetch_array( pwg_query( $query ) );

  if ( !isset( $row['total'] ) ) $row['total'] = 0;
  return $row['total'];
}

/**
 * Retrieve informations about a category in the database
 *
 * Returns an array with following keys :
 *
 *  - comment
 *  - dir : directory, might be empty for virtual categories
 *  - name : an array with indexes from 0 (lowest cat name) to n (most
 *           uppercat name findable)
 *  - nb_images
 *  - id_uppercat
 *  - site_id
 *  - 
 *
 * @param int category id
 * @return array
 */
function get_cat_info( $id )
{
  $infos = array('nb_images','id_uppercat','comment','site_id'
                 ,'dir','date_last','uploadable','status','visible'
                 ,'representative_picture_id','uppercats','commentable');
  
  $query = '
SELECT '.implode(',', $infos).'
  FROM '.CATEGORIES_TABLE.'
  WHERE id = '.$id.'
;';
  $row = mysql_fetch_array(pwg_query($query));

  $cat = array();
  foreach ($infos as $info)
  {
    if (isset($row[$info]))
    {
      $cat[$info] = $row[$info];
    }
    else
    {
      $cat[$info] = '';
    }
    // If the field is true or false, the variable is transformed into a
    // boolean value.
    if ($cat[$info] == 'true' or $cat[$info] == 'false')
    {
      $cat[$info] = get_boolean( $cat[$info] );
    }
  }
  $cat['comment'] = nl2br($cat['comment']);

  $cat['name'] = array();

  $query = '
SELECT name,id
  FROM '.CATEGORIES_TABLE.'
  WHERE id IN ('.$cat['uppercats'].')
  ORDER BY id ASC
;';
  $result = pwg_query($query);
  while($row = mysql_fetch_array($result))
  {
    $cat['name'][$row['id']] = $row['name'];
  }
  
  return $cat;
}

// get_complete_dir returns the concatenation of get_site_url and
// get_local_dir
// Example : "pets > rex > 1_year_old" is on the the same site as the
// PhpWebGallery files and this category has 22 for identifier
// get_complete_dir(22) returns "./galleries/pets/rex/1_year_old/"
function get_complete_dir( $category_id )
{
  return get_site_url($category_id).get_local_dir($category_id);
}

// get_local_dir returns an array with complete path without the site url
// Example : "pets > rex > 1_year_old" is on the the same site as the
// PhpWebGallery files and this category has 22 for identifier
// get_local_dir(22) returns "pets/rex/1_year_old/"
function get_local_dir( $category_id )
{
  global $page;

  $uppercats = '';
  $local_dir = '';

  if ( isset( $page['plain_structure'][$category_id]['uppercats'] ) )
  {
    $uppercats = $page['plain_structure'][$category_id]['uppercats'];
  }
  else
  {
    $query = 'SELECT uppercats';
    $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id = '.$category_id;
    $query.= ';';
    $row = mysql_fetch_array( pwg_query( $query ) );
    $uppercats = $row['uppercats'];
  }

  $upper_array = explode( ',', $uppercats );

  $database_dirs = array();
  $query = 'SELECT id,dir';
  $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id IN ('.$uppercats.')';
  $query.= ';';
  $result = pwg_query( $query );
  while( $row = mysql_fetch_array( $result ) )
  {
    $database_dirs[$row['id']] = $row['dir'];
  }
  foreach ($upper_array as $id)
  {
    $local_dir.= $database_dirs[$id].'/';
  }

  return $local_dir;
}

// retrieving the site url : "http://domain.com/gallery/" or
// simply "./galleries/"
function get_site_url($category_id)
{
  global $page;

  $query = '
SELECT galleries_url
  FROM '.SITES_TABLE.' AS s,'.CATEGORIES_TABLE.' AS c
  WHERE s.id = c.site_id
    AND c.id = '.$category_id.'
;';
  $row = mysql_fetch_array(pwg_query($query));
  return $row['galleries_url'];
}

// initialize_category initializes ;-) the variables in relation
// with category :
// 1. calculation of the number of pictures in the category
// 2. determination of the SQL query part to ask to find the right category
//    $page['where'] is not the same if we are in
//       - simple category
//       - search result
//       - favorites displaying
//       - most visited pictures
//       - best rated pictures
//       - recent pictures
//       - defined list (used for random)
// 3. determination of the title of the page
// 4. creation of the navigation bar
function initialize_category( $calling_page = 'category' )
{
  pwg_debug( 'start initialize_category' );
  global $page,$lang,$user,$conf;

  if ( isset( $page['cat'] ) )
  {
    // $page['nb_image_page'] is the number of picture to display on this page
    // By default, it is the same as the $user['nb_image_page']
    $page['nb_image_page'] = $user['nb_image_page'];
    // $url is used to create the navigation bar
    $url = PHPWG_ROOT_PATH.'category.php?cat='.$page['cat'];
    if ( isset($page['expand']) ) $url.= '&amp;expand='.$page['expand'];
    // simple category
    if ( is_numeric( $page['cat'] ) )
    {
      $result = get_cat_info( $page['cat'] );
      $page['comment']        = $result['comment'];
      $page['cat_dir']        = $result['dir'];
      $page['cat_name']       = $result['name'];
      $page['cat_nb_images']  = $result['nb_images'];
      $page['cat_site_id']    = $result['site_id'];
      $page['cat_uploadable'] = $result['uploadable'];
      $page['cat_commentable'] = $result['commentable'];
      $page['uppercats']      = $result['uppercats'];
      $page['title'] =
        get_cat_display_name($page['cat_name'],
                             '',
                             false);
      $page['where'] = ' WHERE category_id = '.$page['cat'];
    }
    else
    {
      if ($page['cat'] == 'search'
          or $page['cat'] == 'most_visited'
          or $page['cat'] == 'recent_pics'
          or $page['cat'] == 'recent_cats'
          or $page['cat'] == 'best_rated'
          or $page['cat'] == 'calendar'
          or $page['cat'] == 'list')
      {
        // we must not show pictures of a forbidden category
        if ( $user['forbidden_categories'] != '' )
        {
          $forbidden = ' category_id NOT IN ';
          $forbidden.= '('.$user['forbidden_categories'].')';
        }
      }
      // search result
      if ( $page['cat'] == 'search' )
      {
        // analyze search string given in URL (created in search.php)
        $tokens = explode('|', $_GET['search']);

        if (isset($tokens[1]) and $tokens[1] == 'AND')
        {
          $search['mode'] = 'AND';
        }
        else
        {
          $search['mode'] = 'OR';
        }

        $search_tokens = explode(';', $tokens[0]);
        foreach ($search_tokens as $search_token)
        {
          $tokens = explode(':', $search_token);
          $field_name = $tokens[0];
          $field_content = $tokens[1];

          $tokens = explode('~', $tokens[1]);
          if (isset($tokens[1]))
          {
            $search['fields'][$field_name]['mode'] = $tokens[1];
          }
          else
          {
            $search['fields'][$field_name]['mode'] = '';
          }

          $search['fields'][$field_name]['words'] = array();
          $tokens = explode(',', $tokens[0]);
          foreach ($tokens as $token)
          {
            array_push($search['fields'][$field_name]['words'], $token);
          }
        }
        
        $page['title'] = $lang['search_result'];
        if ( $calling_page == 'picture' )
        {
          $page['title'].= ' : <span style="font-style:italic;">';
          $page['title'].= $_GET['search']."</span>";
        }

        // SQL where clauses are stored in $clauses array during query
        // construction
        $clauses = array();
        
        $textfields = array('file', 'name', 'comment', 'keywords', 'author');
        foreach ($textfields as $textfield)
        {
          if (isset($search['fields'][$textfield]))
          {
            $local_clauses = array();
            foreach ($search['fields'][$textfield]['words'] as $word)
            {
              array_push($local_clauses, $textfield." LIKE '%".$word."%'");
            }
            // adds brackets around where clauses
            array_walk($local_clauses,create_function('&$s','$s="(".$s.")";'));
            array_push($clauses,
                       implode(' '.$search['fields'][$textfield]['mode'].' ',
                               $local_clauses));
          }
        }

        if (isset($search['fields']['allwords']))
        {
          $fields = array('file', 'name', 'comment', 'keywords');
          // in the OR mode, request bust be :
          // ((field1 LIKE '%word1%' OR field2 LIKE '%word1%')
          // OR (field1 LIKE '%word2%' OR field2 LIKE '%word2%'))
          //
          // in the AND mode :
          // ((field1 LIKE '%word1%' OR field2 LIKE '%word1%')
          // AND (field1 LIKE '%word2%' OR field2 LIKE '%word2%'))
          $word_clauses = array();
          foreach ($search['fields']['allwords']['words'] as $word)
          {
            $field_clauses = array();
            foreach ($fields as $field)
            {
              array_push($field_clauses, $field." LIKE '%".$word."%'");
            }
            // adds brackets around where clauses
            array_push($word_clauses, implode(' OR ', $field_clauses));
          }
          array_walk($word_clauses, create_function('&$s','$s="(".$s.")";'));
          array_push($clauses,
                     implode(' '.$search['fields']['allwords']['mode'].' ',
                               $word_clauses));
        }

        $datefields = array('date_available', 'date_creation');
        foreach ($datefields as $datefield)
        {
          $key = $datefield;
          if (isset($search['fields'][$key]))
          {
            $local_clause = $datefield." = '";
            $local_clause.= str_replace('.', '-',
                                        $search['fields'][$key]['words'][0]);
            $local_clause.= "'";
            array_push($clauses, $local_clause);
          }

          foreach (array('after','before') as $suffix)
          {
            $key = $datefield.'-'.$suffix;
            if (isset($search['fields'][$key]))
            {
              $local_clause = $datefield;
              if ($suffix == 'after')
              {
                $local_clause.= ' >';
              }
              else
              {
                $local_clause.= ' <';
              }
              if (isset($search['fields'][$key]['mode'])
                  and $search['fields'][$key]['mode'] == 'inc')
              {
                $local_clause.= '=';
              }
              $local_clause.= " '";
              $local_clause.= str_replace('.', '-',
                                          $search['fields'][$key]['words'][0]);
              $local_clause.= "'";
              array_push($clauses, $local_clause);
            }
          }
        }

        if (isset($search['fields']['cat']))
        {
          if ($search['fields']['cat']['mode'] == 'sub_inc')
          {
            // searching all the categories id of sub-categories
            $cat_ids = get_subcat_ids($search['fields']['cat']['words']);
          }
          else
          {
            $cat_ids = $search['fields']['cat']['words'];
          }
          
          $local_clause = 'category_id IN ('.implode(',', $cat_ids).')';
          array_push($clauses, $local_clause);
        }

        // adds brackets around where clauses
        array_walk($clauses, create_function('&$s', '$s = "(".$s.")";'));
        $page['where'] = 'WHERE '.implode(' '.$search['mode'].' ', $clauses);
        if ( isset( $forbidden ) ) $page['where'].= ' AND '.$forbidden;

        $query = '
SELECT COUNT(DISTINCT(id)) AS nb_total_images
  FROM '.IMAGES_TABLE.'
    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
  '.$page['where'].'
;';
        $url.= '&amp;search='.$_GET['search'];
      }
      // favorites displaying
      else if ( $page['cat'] == 'fav' )
      {
        check_user_favorites();
        
        $page['title'] = $lang['favorites'];

        $page['where'] = ', '.FAVORITES_TABLE.' AS fav';
        $page['where'].= ' WHERE user_id = '.$user['id'];
        $page['where'].= ' AND fav.image_id = id';
      
        $query = 'SELECT COUNT(*) AS nb_total_images';
        $query.= ' FROM '.FAVORITES_TABLE;
        $query.= ' WHERE user_id = '.$user['id'];
        $query.= ';';
      }
      // pictures within the short period
      else if ( $page['cat'] == 'recent_pics' )
      {
        $page['title'] = $lang['recent_pics_cat'];
        // We must find the date corresponding to :
        // today - $conf['periode_courte']
        $date = time() - 60*60*24*$user['recent_period'];
        $page['where'] = " WHERE date_available > '";
        $page['where'].= date( 'Y-m-d', $date )."'";
        if ( isset( $forbidden ) ) $page['where'].= ' AND '.$forbidden;

        $query = '
SELECT COUNT(DISTINCT(id)) AS nb_total_images
  FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic
    ON id = ic.image_id
  '.$page['where'].'
;';
      }
      // categories containing recent pictures
      else if ( $page['cat'] == 'recent_cats' )
      {
        $page['title'] = $lang['recent_cats_cat'];
        $page['cat_nb_images'] = 0;
      }
      // most visited pictures
      else if ( $page['cat'] == 'most_visited' )
      {
        $page['title'] = $conf['top_number'].' '.$lang['most_visited_cat'];

        $page['where'] = 'WHERE hit > 0';
        if (isset($forbidden))
        {
          $page['where'] = "\n".'    AND '.$forbidden;
        }

        $conf['order_by'] = ' ORDER BY hit DESC, file ASC';
        $page['cat_nb_images'] = $conf['top_number'];
        if ( isset( $page['start'] )
             and ($page['start']+$user['nb_image_page']>=$conf['top_number']))
        {
          $page['nb_image_page'] = $conf['top_number'] - $page['start'];
        }
      }
      else if ( $page['cat'] == 'calendar' )
      {
        $page['cat_nb_images'] = 0;
        $page['title'] = $lang['calendar'];
        if (isset($_GET['year'])
            and preg_match('/^\d+$/', $_GET['year']))
        {
          $page['calendar_year'] = (int)$_GET['year'];
        }
        if (isset($_GET['month'])
            and preg_match('/^(\d+)\.(\d{2})$/', $_GET['month'], $matches))
        {
          $page['calendar_year'] = (int)$matches[1];
          $page['calendar_month'] = (int)$matches[2];
        }
        if (isset($_GET['day'])
            and preg_match('/^(\d+)\.(\d{2})\.(\d{2})$/',
                           $_GET['day'],
                           $matches))
        {
          $page['calendar_year'] = (int)$matches[1];
          $page['calendar_month'] = (int)$matches[2];
          $page['calendar_day'] = (int)$matches[3];
        }
        if (isset($page['calendar_year']))
        {
          $page['title'] .= ' (';
          if (isset($page['calendar_day']))
          {
            $unixdate = mktime(0,0,0,
                               $page['calendar_month'],
                               $page['calendar_day'],
                               $page['calendar_year']);
            $page['title'].= $lang['day'][date("w", $unixdate)];
            $page['title'].= ' '.$page['calendar_day'].', ';
          }
          if (isset($page['calendar_month']))
          {
            $page['title'] .= $lang['month'][$page['calendar_month']].' ';
          }
          $page['title'] .= $page['calendar_year'];
          $page['title'] .= ')';
        }
        
        $page['where'] = 'WHERE '.$conf['calendar_datefield'].' IS NOT NULL';
        if (isset($forbidden))
        {
          $page['where'].= ' AND '.$forbidden;
        }
      }
      else if ($page['cat'] == 'best_rated')
      {
        $page['title'] = $conf['top_number'].' '.$lang['best_rated_cat'];

        $page['where'] = ' WHERE average_rate IS NOT NULL';
        
        if (isset($forbidden))
        {
          $page['where'].= ' AND '.$forbidden;
        }

        $conf['order_by'] = ' ORDER BY average_rate DESC, id ASC';

        // $page['cat_nb_images'] equals $conf['top_number'] unless there
        // are less rated items
        $query ='
SELECT COUNT(1) AS count
  FROM '.IMAGES_TABLE.'
    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
  '.$page['where'].'
;';
        $row = mysql_fetch_array(pwg_query($query));
        if ($row['count'] < $conf['top_number'])
        {
          $page['cat_nb_images'] = $row['count'];
        }
        else
        {
          $page['cat_nb_images'] = $conf['top_number'];
        }
        unset($query);
          

        if (isset($page['start'])
            and ($page['start']+$user['nb_image_page']>=$conf['top_number']))
        {
          $page['nb_image_page'] = $conf['top_number'] - $page['start'];
        }
      }
      else if ($page['cat'] == 'list')
      {
        $page['title'] = $lang['random_cat'];
          
        $page['where'] = 'WHERE 1=1';
        if (isset($forbidden))
        {
          $page['where'].= ' AND '.$forbidden;
        }
        $page['where'].= ' AND image_id IN ('.$_GET['list'].')';
        $page['cat_nb_images'] = count(explode(',', $_GET['list']));

        $url.= '&amp;list='.$_GET['list'];
      }

      if (isset($query))
      {
        $result = pwg_query( $query );
        $row = mysql_fetch_array( $result );
        $page['cat_nb_images'] = $row['nb_total_images'];
      }
    }
    if ( $calling_page == 'category' )
    {
      $page['navigation_bar'] =
        create_navigation_bar( $url, $page['cat_nb_images'], $page['start'],
                               $user['nb_image_page'], 'back' );
    }
  }
  else
  {
    $page['title'] = $lang['diapo_default_page_title'];
  }
  pwg_debug( 'end initialize_category' );
}

function display_select_categories($categories,
                                   $selecteds,
                                   $blockname,
                                   $fullname = true)
{
  global $template;

  foreach ($categories as $category)
  {
    $selected = '';
    if (in_array($category['id'], $selecteds))
    {
      $selected = ' selected="selected"';
    }

    if ($fullname)
    {
      $option = get_cat_display_name_cache($category['uppercats'],
                                           '',
                                           false);
    }
    else
    {
      $option = str_repeat('&nbsp;',
                           (3 * substr_count($category['global_rank'], '.')));
      $option.= '- '.$category['name'];
    }
    
    $template->assign_block_vars(
      $blockname,
      array('SELECTED'=>$selected,
            'VALUE'=>$category['id'],
            'OPTION'=>$option
        ));
  }
}

function display_select_cat_wrapper($query, $selecteds, $blockname,
                                    $fullname = true)
{
  $result = pwg_query($query);
  $categories = array();
  if (!empty($result))
  {
  while ($row = mysql_fetch_array($result))
  {
    array_push($categories, $row);
  }
  }
  usort($categories, 'global_rank_compare');
  display_select_categories($categories, $selecteds, $blockname, $fullname);
}

/**
 * returns all subcategory identifiers of given category ids
 *
 * @param array ids
 * @return array
 */
function get_subcat_ids($ids)
{
  $query = '
SELECT DISTINCT(id)
  FROM '.CATEGORIES_TABLE.'
  WHERE ';
  foreach ($ids as $num => $category_id)
  {
    if ($num > 0)
    {
      $query.= '
    OR ';
    }
    $query.= 'uppercats REGEXP \'(^|,)'.$category_id.'(,|$)\'';
  }
  $query.= '
;';
  $result = pwg_query($query);

  $subcats = array();
  while ($row = mysql_fetch_array($result))
  {
    array_push($subcats, $row['id']);
  }
  return $subcats;
}

function global_rank_compare($a, $b)
{
  return strnatcasecmp($a['global_rank'], $b['global_rank']);
}
?>