aboutsummaryrefslogtreecommitdiffstats
path: root/include/functions_category.inc.php
blob: aa4e277dc3a7ca07e1b8031f1a4f9454561ce8c8 (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
<?php
/***************************************************************************
 *                         functions_category.inc.php                      *
 *                            --------------------                         *
 *   application          : PhpWebGallery 1.3                              *
 *   author               : Pierrick LE GALL <pierrick@z0rglub.com>        *
 *                                                                         *
 ***************************************************************************

 ***************************************************************************
 *                                                                         *
 *   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;                                         *
 *                                                                         *
 ***************************************************************************/
function get_subcats_id( $cat_id )
{
  $restricted_cat = array();
  $i = 0;
                
  $query = 'select id';
  $query.= ' from '.PREFIX_TABLE.'categories';
  $query.= ' where id_uppercat = '.$cat_id;
  $query.= ';';
  $result = mysql_query( $query );
  while ( $row = mysql_fetch_array( $result ) )
  {
    $restricted_cat[$i++] = $row['id'];
    $sub_restricted_cat = get_subcats_id( $row['id'] );
    for ( $j = 0; $j < sizeof( $sub_restricted_cat ); $j++ )
    {
      $restricted_cat[$i++] = $sub_restricted_cat[$j];
    }
  }
                
  return $restricted_cat;
}

function check_restrictions( $category_id )
{
  global $user,$lang;

  if ( is_user_allowed( $category_id, $user['restrictions'] ) > 0 )
  {
    echo '<div style="text-align:center;">'.$lang['access_forbiden'].'<br />';
    echo '<a href="'.add_session_id( './category.php' ).'">';
    echo $lang['thumbnails'].'</a></div>';
    exit();
  }
}
        
// the check_cat_id function check whether the $cat is a right parameter :
//  - $cat is numeric and corresponds to a category in the database
//  - $cat equals 'fav' (for favorites)
//  - $cat equals 'search' (when the result of a search is displayed)
function check_cat_id( $cat )
{
  global $page;

  unset( $page['cat'] );
  if ( isset( $cat ) )
  {
    if ( is_numeric( $cat ) )
    {
      $query = 'select id';
      $query.= ' from '.PREFIX_TABLE.'categories';
      $query.= ' where id = '.$cat;
      $query. ';';
      $result = mysql_query( $query );
      if ( mysql_num_rows( $result ) != 0 )
      {
        $page['cat'] = $cat;
      }
    }
    if ( $cat == 'fav' or $cat == 'search' or $cat == 'most_visited'
         or $cat == 'best_rated' or $cat == 'recent' )
    {
      $page['cat'] = $cat;
    }
  }
}

function display_cat( $id_uppercat, $indent, $restriction, $tab_expand )
{
  global $user,$lang,$conf,$page,$vtp,$handle;
  
  $query = 'select name,id,date_dernier,nb_images,dir';
  $query.= ' from '.PREFIX_TABLE.'categories';
  $query.= ' where id_uppercat';
  if ( $id_uppercat == "" )
  {
    $query.= ' is NULL';
  }
  else
  {
    $query.= ' = '.$id_uppercat;
  }
  $query.= ' order by rank asc;';
  $result = mysql_query( $query );
  while ( $row = mysql_fetch_array( $result ) )
  {
    if ( !in_array( $row['id'], $restriction ) )
    {
      $nb_subcats = get_nb_subcats( $row['id'] );
                                
      $expand = "";
      // si la cat�gorie n'a pas de sous cat�gorie
      // ou que l'on doit d�velopper toutes les cat�gories par d�faut
      // alors on utilise l'expand par d�faut
      if ( $nb_subcats == 0 or $user['expand'] == "true" )
      {
        $expand = $page['expand'];
      }
      // si la cat�gorie n'est pas dans les cat�gories � d�velopper
      // alors on l'ajoute aux cat�gories � d�velopper
      else if ( !in_array( $row['id'], $tab_expand ) )
      {
        $expand = implode( ",", $tab_expand );
        if ( strlen( $expand ) > 0 )
        {
          $expand.= ",";
        }
        $expand.= $row['id'];
      }
      // si la cat�gorie est d�j� dans les cat�gories � d�velopper
      // alors on la retire des cat�gories � d�velopper
      else
      {
        $expand = array_remove( $tab_expand, $row['id'] );
      }
      $url = "./category.php?cat=".$page['cat']."&amp;expand=$expand";
      if ( $page['cat'] == 'search' )
      {
        $url.= "&amp;search=".$_GET['search'].'&amp;mode='.$_GET['mode'];
      }
      $lien_cat = add_session_id( $url );
      if ( $row['name'] == "" )
      {
        $name = str_replace( "_", " ", $row['dir'] );
      }
      else
      {
        $name = $row['name'];
      }

      $vtp->addSession( $handle, 'category' );
      $vtp->setVar( $handle, 'category.indent', $indent );

      if ( $user['expand'] == "true" or $nb_subcats == 0 )
      {
        $vtp->addSession( $handle, 'bullet_wo_link' );
        $vtp->setVar( $handle, 'bullet_wo_link.bullet_url',
                      $user['lien_collapsed'] );
        $vtp->setVar( $handle, 'bullet_wo_link.bullet_alt', '&gt;' );
        $vtp->closeSession( $handle, 'bullet_wo_link' );
      }
      else
      {
        $vtp->addSession( $handle, 'bullet_w_link' );
        $vtp->setVar( $handle, 'bullet_w_link.bullet_link', $lien_cat );
        $vtp->setVar( $handle, 'bullet_w_link.bullet_alt', '&gt;' );
        if ( in_array( $row['id'], $tab_expand ) )
        {
          $vtp->setVar( $handle, 'bullet_w_link.bullet_url',
                        $user['lien_expanded'] );
        }
        else
        {
          $vtp->setVar( $handle, 'bullet_w_link.bullet_url',
                        $user['lien_collapsed'] );
        }
        $vtp->closeSession( $handle, 'bullet_w_link' );
      }
      $vtp->setVar( $handle, 'category.link_url',
                    add_session_id( './category.php?cat='.
                                    $row['id'].'&amp;expand='.$expand ) );
      $vtp->setVar( $handle, 'category.link_name', $name );
      if ( $id_uppercat == "" )
      {
        $vtp->setVar( $handle, 'category.name_style', 'font-weight:bold;' );
      }
      if ( $nb_subcats > 0 )
      {
        $vtp->addSession( $handle, 'subcat' );
        $vtp->setVar( $handle, 'subcat.nb_subcats', $nb_subcats );
        $vtp->closeSession( $handle, 'subcat' );
      }
      $vtp->setVar( $handle, 'category.total_cat', $row['nb_images'] );
      $date_dispo = explode( "-", $row['date_dernier'] );
      $date_cat = mktime( 0, 0, 0, $date_dispo[1], $date_dispo[2],
                          $date_dispo[0] );
      $vtp->setVar( $handle, 'category.cat_icon', get_icon( $date_cat ) );
      $vtp->closeSession( $handle, 'category' );

      if ( in_array( $row['id'], $tab_expand ) or $user['expand'] == "true" )
      {
        display_cat( $row['id'], $indent.'&nbsp;&nbsp;&nbsp;&nbsp;',
                     $restriction, $tab_expand );
      }
    }
  }
}
        
function get_nb_subcats( $id )
{
  global $user;
                
  $query = 'select count(*) as count';
  $query.= ' from '.PREFIX_TABLE.'categories';
  $query.= ' where id_uppercat = '.$id;
  for ( $i = 0; $i < sizeof( $user['restrictions'] ); $i++ )
  {
    $query.= " and id != ".$user['restrictions'][$i];
  }
  $query.= ';';
  $result = mysql_query( $query );
  $row = mysql_fetch_array( $result );
  return $row['count'];
}
        
function get_total_image( $id, $restriction )
{
  $total = 0;
                
  $query = 'select id,nb_images';
  $query.= ' from '.PREFIX_TABLE.'categories';
  $query.= ' where id_uppercat';
  if ( !is_numeric( $id ) )
  {
    $query.= ' is NULL';
  }
  else
  {
    $query.= ' = '.$id;
  }
  $query.= ";";
  $result = mysql_query( $query );
  while ( $row = mysql_fetch_array( $result ) )
  {
    if ( !in_array( $row['id'], $restriction ) )
    {
      $total+= $row['nb_images'];
      $total+= get_total_image( $row['id'], $restriction );
    }
  }
  return $total;
}

// variables :
// $cat['comment']
// $cat['dir']
// $cat['last_dir']
// $cat['name'] is an array :
//      - $cat['name'][0] is the lowest cat name
//      and
//      - $cat['name'][n] is the most uppercat name findable
// $cat['nb_images']
// $cat['id_uppercat']
// $cat['site_id']
function get_cat_info( $id )
{
  $cat = array();
  $cat['name'] = array();
                
  $query = 'select nb_images,id_uppercat,comment,site_id,galleries_url,dir';
  $query.= ' from '.PREFIX_TABLE.'categories as a';
  $query.= ', '.PREFIX_TABLE.'sites as b';
  $query.= ' where a.id = '.$id;
  $query.= ' and a.site_id = b.id;';
  $row = mysql_fetch_array( mysql_query( $query ) );
  $cat['site_id']     = $row['site_id'];
  $cat['id_uppercat'] = $row['id_uppercat'];
  $cat['comment']     = nl2br( $row['comment'] );
  $cat['nb_images']   = $row['nb_images'];
  $cat['last_dir']    = $row['dir'];
  $galleries_url = $row['galleries_url'];
                
  $cat['dir'] = "";
  $i = 0;
  $is_root = false;
  $row['id_uppercat'] = $id;
  while ( !$is_root )
  {
    $query = 'select name,dir,id_uppercat';
    $query.= ' from '.PREFIX_TABLE.'categories';
    $query.= ' where id = '.$row['id_uppercat'].';';
    $row = mysql_fetch_array( mysql_query( $query ) );
    $cat['dir'] = $row['dir']."/".$cat['dir'];
    if ( $row['name'] == "" )
    {
      $cat['name'][$i] = str_replace( "_", " ", $row['dir'] );
    }
    else
    {
      $cat['name'][$i] = $row['name'];
    }
    if ( $row['id_uppercat'] == "" )
    {
      $is_root = true;
    }
    $i++;
  }
  $cat['local_dir'] = substr( $cat['dir'], 0 , strlen( $cat['dir'] ) - 1 );
  $cat['dir'] = $galleries_url.$cat['dir'];
                
  return $cat;
}
        
// The function get_cat_display_name returns a string containing the list
// of upper categories to the root category from the lowest category shown
// example : "anniversaires - fete mere 2002 - animaux - erika"
// You can give two parameters :
//   - $separation : the string between each category name " - " for example
//   - $style : the style of the span tag for the lowest category,
//     "font-style:italic;" for example
function get_cat_display_name( $array_cat_names, $separation, $style )
{
  $output = "";
  for ( $i = sizeof( $array_cat_names ) - 1; $i >= 0; $i-- )
  {
    if ( $i != sizeof( $array_cat_names ) - 1 )
    {
      $output.= $separation;
    }
    if ( $i != 0 )
    {
      $output.= $array_cat_names[$i];
    }
    else
    {
      if ( $style != "" )
      {
        $output.= '<span style="'.$style.'">';
      }
      $output.= $array_cat_names[$i];
      if ( $style != "" )
      {
        $output.= "</span>";
      }
    }
  }
  return replace_space( $output );
}

// 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
// 3. determination of the title of the page
// 4. creation of the navigation bar
function initialize_category( $calling_page = '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 = './category.php?cat='.$page['cat'].'&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['title'] = get_cat_display_name( $page['cat_name'], ' - ', '' );
      $page['where'] = ' WHERE cat_id = '.$page['cat'];
    }
    else
    {
      if ( $page['cat'] == 'search' or $page['cat'] == 'most_visited'
           or $page['cat'] == 'recent' or $page['cat'] == 'best_rated' )
      {
        // we must not show pictures of a forbidden category
        $restricted_cats = get_all_restrictions( $user['id'],$user['status'] );
        foreach ( $restricted_cats as $restricted_cat ) {
          $where_append.= ' AND cat_id != '.$restricted_cat;
        }
      }
      // search result
      if ( $page['cat'] == 'search' )
      {
        $page['title'] = $lang['search_result'];
        if ( $calling_page == 'picture' )
        {
          $page['title'].= ' : <span style="font-style:italic;">';
          $page['title'].= $_GET['search']."</span>";
        }

        $page['where'] = ' WHERE (';
        $fields = array( 'file', 'name', 'comment' );
        $words = explode( ',', $_GET['search'] );
        $sql_search = array();
        foreach ( $words as $i => $word ) {
          if ( $_GET['mode'] == 'OR' )
          {
            if ( $i != 0 ) $page['where'].= ' OR';
            foreach ( $fields as $j => $field ) {
              if ( $j != 0 ) $page['where'].= ' OR';
              $page['where'].= ' '.$field." LIKE '%".$word."%'";
            }
          }
          else if ( $_GET['mode'] == 'AND' )
          {
            if ( $i != 0 ) $page['where'].= ' AND';
            $page['where'].= ' (';
            foreach ( $fields as $j => $field ) {
              if ( $j != 0 ) $page['where'].= ' OR';
              $page['where'].= ' '.$field." LIKE '%".$word."%'";
            }
            $page['where'].= ' )';
          }
        }
        $page['where'].= ' )';
        $page['where'].= $where_append;

        $query = 'SELECT COUNT(*) AS nb_total_images';
        $query.= ' FROM '.PREFIX_TABLE.'images';
        $query.= $page['where'];
        $query.= ';';

        $url.= '&amp;search='.$_GET['search'].'&amp;mode='.$_GET['mode'];
      }
      // favorites displaying
      else if ( $page['cat'] == 'fav' )
      {
        $page['title'] = $lang['favorites'];

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

        $query = 'SELECT COUNT(*) AS nb_total_images';
        $query.= ' FROM '.PREFIX_TABLE.'images';
        $query.= $page['where'];
        $query.= ';';
      }
      // most visited pictures
      else if ( $page['cat'] == 'most_visited' )
      {
        $page['title'] = $conf['top_number'].' '.$lang['most_visited_cat'];
        $page['where'] = ' WHERE cat_id != -1'.$where_append;
        $conf['order_by'] = ' ORDER BY hit DESC, file ASC';
        $page['cat_nb_images'] = $conf['top_number'];
        if ( $page['start'] + $user['nb_image_page'] >= $conf['top_number'] )
        {
          $page['nb_image_page'] = $conf['top_number'] - $page['start'];
        }
      }
      
      if ( $query != '' )
      {
        $result = mysql_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'];
  }
}

// get_non_empty_sub_cat_ids returns an array composing of the infos of the
// direct sub-categories of the given uppercat id. Each of these infos is
// associated to the first found non empty category id. eg :
//
// - catname [cat_id]
// - cat1 [1] -> given uppercat
//   - cat1.1 [2] (empty)
//     - cat1.1.1 [5] (empty)
//     - cat1.1.2 [6]
//   - cat1.2 [3]
//   - cat1.3 [4]
//
// get_non_empty_sub_cat_ids will return :
//   $cats[0]['id']            = 2;
//   $cats[0]['name']          = '';
//   $cats[0]['dir']           = 'cat1';
//   $cats[0]['date_dernier']  = '2003-05-17';
//   $cats[0]['non_empty_cat'] = 6;
//
//   $cats[1]['id']            = 3;
//   $cats[1]['non_empty_cat'] = 3;
//
//   $cats[1]['id']            = 4;
//   $cats[1]['non_empty_cat'] = 4;
function get_non_empty_sub_cat_ids( $id_uppercat )
{
  global $user;

  $cats = array();

  $query = 'SELECT id,name,dir,date_dernier,nb_images';
  $query.= ' FROM '.PREFIX_TABLE.'categories';
  $query.= ' WHERE id_uppercat = '.$id_uppercat;
  // we must not show pictures of a forbidden category
  $restricted_cats = get_all_restrictions( $user['id'],$user['status'] );
  foreach ( $restricted_cats as $restricted_cat ) {
    $query.= ' AND id != '.$restricted_cat;
  }
  $query.= ' ORDER BY rank';
  $query.= ';';

  $result = mysql_query( $query );
  while ( $row = mysql_fetch_array( $result ) )
  {
    // only categories with findable picture in any of its subcats is
    // represented.
    if ( ( $row['nb_images'] != 0 and $non_empty_cat = $row['id'] )
         or $non_empty_cat = get_first_non_empty_cat_id( $row['id'] ) )
    {
      $temp_cat = array(
        'id'            => $row['id'],
        'name'          => $row['name'],
        'dir'           => $row['dir'],
        'date_dernier'  => $row['date_dernier'],
        'non_empty_cat' => $non_empty_cat );
      array_push( $cats, $temp_cat );
    }
  }
  return $cats;
}

// get_first_non_empty_cat_id returns the id of the first non empty
// sub-category to the given uppercat. If no picture is found in any
// subcategory, false is returned.
function get_first_non_empty_cat_id( $id_uppercat )
{
  global $user;

  $query = 'SELECT id,nb_images';
  $query.= ' FROM '.PREFIX_TABLE.'categories';
  $query.= ' WHERE id_uppercat = '.$id_uppercat;
  // we must not show pictures of a forbidden category
  $restricted_cats = get_all_restrictions( $user['id'],$user['status'] );
  foreach ( $restricted_cats as $restricted_cat ) {
    $query.= ' AND id != '.$restricted_cat;
  }
  $query.= ' ORDER BY RAND()';
  $query.= ';';
  $result = mysql_query( $query );
  while ( $row = mysql_fetch_array( $result ) )
  {
    if ( $row['nb_images'] > 0 )
    {
      return $row['id'];
    }
  }
  $result = mysql_query( $query );
  while ( $row = mysql_fetch_array( $result ) )
  {
    // recursive call
    if ( $subcat = get_first_non_empty_cat_id( $row['id'] ) )
    {
      return $subcat;
    }
  }
  return false;
}
?>