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

Last change on this file since 345 was 345, checked in by gweltas, 20 years ago

Merge of the 1.3.1 release
Creation of an unique include file (common.php)
Creation of an unique define file (include/constants.php)
Modification of the installation procedure

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 20.6 KB
Line 
1<?php
2/***************************************************************************
3 *                         functions_category.inc.php                      *
4 *                            --------------------                         *
5 *   application          : PhpWebGallery 1.3 <http://phpwebgallery.net>   *
6 *   author        : Pierrick LE GALL <pierrick@z0rglub.com>               *
7 *                                                                         *
8 *   $Id: functions_category.inc.php 345 2004-02-02 00:55:18Z gweltas $
9 *                                                                         *
10 ***************************************************************************
11
12 ***************************************************************************
13 *                                                                         *
14 *   This program is free software; you can redistribute it and/or modify  *
15 *   it under the terms of the GNU General Public License as published by  *
16 *   the Free Software Foundation;                                         *
17 *                                                                         *
18 ***************************************************************************/
19
20function check_restrictions( $category_id )
21{
22  global $user,$lang;
23
24  if ( in_array( $category_id, $user['restrictions'] ) )
25  {
26    echo '<div style="text-align:center;">'.$lang['access_forbiden'].'<br />';
27    echo '<a href="'.add_session_id( './category.php' ).'">';
28    echo $lang['thumbnails'].'</a></div>';
29    exit();
30  }
31}
32
33// the check_cat_id function check whether the $cat is a right parameter :
34//  - $cat is numeric and corresponds to a category in the database
35//  - $cat equals 'fav' (for favorites)
36//  - $cat equals 'search' (when the result of a search is displayed)
37function check_cat_id( $cat )
38{
39  global $page;
40
41  unset( $page['cat'] );
42  if ( isset( $cat ) )
43  {
44    if ( isset( $page['plain_structure'][$cat] ) )
45    {
46      $page['cat'] = $cat;
47    }
48    else if ( is_numeric( $cat ) )
49    {
50      $query = 'SELECT id';
51      $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id = '.$cat.';';
52      $result = mysql_query( $query );
53      if ( mysql_num_rows( $result ) != 0 )
54      {
55        $page['cat'] = $cat;
56      }
57    }
58    if ( $cat == 'fav'
59         or $cat == 'search'
60         or $cat == 'most_visited'
61         or $cat == 'best_rated'
62         or $cat == 'recent' )
63    {
64      $page['cat'] = $cat;
65    }
66  }
67}
68
69function get_user_plain_structure()
70{
71  global $page,$user;
72 
73  $infos = array( 'name','id','uc.date_last','nb_images','dir','id_uppercat',
74                  'rank','site_id','nb_sub_categories','uppercats');
75 
76  $query = 'SELECT '.implode( ',', $infos );
77  $query.= ' FROM '.CATEGORIES_TABLE.' AS c';
78//  $query.= ' ,'.PREFIX_TABLE.'user_category AS uc';
79  $query.= ' INNER JOIN '.USER_CATEGORY_TABLE.' AS uc';
80  $query.= ' ON c.id = uc.category_id';
81  $query.= ' WHERE user_id = '.$user['id'];
82  if ( $page['expand'] != 'all' )
83  {
84    $query.= ' AND (id_uppercat is NULL';
85    if ( count( $page['tab_expand'] ) > 0 )
86    {
87      $query.= ' OR id_uppercat IN ('.$page['expand'].')';
88    }
89    $query.= ')';
90  }
91  if ( $user['forbidden_categories'] != '' )
92  {
93    $query.= ' AND id NOT IN ';
94    $query.= '('.$user['forbidden_categories'].')';
95  }
96//  $query.= ' AND c.id = uc.category_id';
97  $query.= ' ORDER BY id_uppercat ASC, rank ASC';
98  $query.= ';';
99
100  $plain_structure = array();
101  $result = mysql_query( $query );
102  while ( $row = mysql_fetch_array( $result ) )
103  {
104    $category = array();
105    foreach ( $infos as $info ) {
106      if ( $info == 'uc.date_last' )
107      {
108        list($year,$month,$day) = explode( '-', $row['date_last'] );
109        $category['date_last'] = mktime(0,0,0,$month,$day,$year);
110      }
111      else if ( isset( $row[$info] ) ) $category[$info] = $row[$info];
112      else                             $category[$info] = '';
113    }
114    $plain_structure[$row['id']] = $category;
115  }
116
117  return $plain_structure;
118}
119
120function create_user_structure( $id_uppercat )
121{
122  global $page;
123
124  if ( !isset( $page['plain_structure'] ) )
125    $page['plain_structure'] = get_user_plain_structure();
126
127  $structure = array();
128  $ids = get_user_subcat_ids( $id_uppercat );
129  foreach ( $ids as $id ) {
130    $category = $page['plain_structure'][$id];
131    $category['subcats'] = create_user_structure( $id );
132    array_push( $structure, $category );
133  }
134  return $structure;
135}
136
137function get_user_subcat_ids( $id_uppercat )
138{
139  global $page;
140
141  $ids = array();
142  foreach ( $page['plain_structure'] as $id => $category ) {
143    if ( $category['id_uppercat'] == $id_uppercat ) array_push( $ids, $id );
144    else if ( count( $ids ) > 0 )                   return $ids;
145  }
146  return $ids;
147}
148
149// update_structure updates or add informations about each node of the
150// structure :
151//
152// 1. should the category be expanded in the menu ?
153// If the category has to be expanded (ie its id is in the
154// $page['tab_expand'] or all the categories must be expanded by default),
155// $category['expanded'] is set to true.
156//
157// 2. associated expand string
158// in the menu, there is a expand string (used in the URL) to tell which
159// categories must be expanded in the menu if this category is chosen
160function update_structure( $categories )
161{
162  global $page, $user;
163
164  $updated_categories = array();
165
166  foreach ( $categories as $category ) {
167    // update the "expanded" key
168    if ( $user['expand']
169         or $page['expand'] == 'all'
170         or in_array( $category['id'], $page['tab_expand'] ) )
171    {
172      $category['expanded'] = true;
173    }
174    else
175    {
176      $category['expanded'] = false;
177    }
178    // update the "expand_string" key
179    if ( $page['expand'] == 'all' )
180    {
181      $category['expand_string'] = 'all';
182    }
183    else
184    {
185      $tab_expand = $page['tab_expand'];
186      if ( in_array( $category['id'], $page['tab_expand'] ) )
187      {
188        // the expand string corresponds to the $page['tab_expand'] without
189        // the $category['id']
190        $tab_expand = array_diff( $page['tab_expand'],array($category['id']) );
191      }
192      else if ( $category['nb_sub_categories'] > 0 )
193      {
194        // we have this time to add the $category['id']...
195        $tab_expand = array_merge($page['tab_expand'],array($category['id']));
196      }
197      $category['expand_string'] = implode( ',', $tab_expand );
198    }
199    // recursive call
200    $category['subcats'] = update_structure( $category['subcats'] );
201    // adding the updated category
202    array_push( $updated_categories, $category );
203  }
204
205  return $updated_categories;
206}
207
208// count_images returns the number of pictures contained in the given
209// category represented by an array, in this array, we have (among other
210// things) :
211// $category['nb_images'] -> number of pictures in this category
212// $category['subcats'] -> array of sub-categories
213// count_images goes to the deepest sub-category to find the total number of
214// pictures contained in the given given category
215function count_images( $categories )
216{
217  return count_user_total_images();
218  $total = 0;
219  foreach ( $categories as $category ) {
220    $total+= $category['nb_images'];
221    $total+= count_images( $category['subcats'] );
222  }
223  return $total;
224}
225
226function count_user_total_images()
227{
228  global $user;
229
230  $query = 'SELECT SUM(nb_images) AS total';
231  $query.= ' FROM '.CATEGORIES_TABLE;
232  if ( count( $user['restrictions'] ) > 0 )
233    $query.= ' WHERE id NOT IN ('.$user['forbidden_categories'].')';
234  $query.= ';';
235 
236  $row = mysql_fetch_array( mysql_query( $query ) );
237
238  if ( !isset( $row['total'] ) ) $row['total'] = 0;
239
240  return $row['total'];
241}
242
243// variables :
244// $cat['comment']
245// $cat['dir']
246// $cat['dir']
247// $cat['name'] is an array :
248//      - $cat['name'][0] is the lowest cat name
249//      and
250//      - $cat['name'][n] is the most uppercat name findable
251// $cat['nb_images']
252// $cat['id_uppercat']
253// $cat['site_id']
254function get_cat_info( $id )
255{
256  global $page;
257
258  $infos = array( 'nb_images','id_uppercat','comment','site_id','galleries_url'
259                  ,'dir','date_last','uploadable','status','visible'
260                  ,'representative_picture_id','uppercats' );
261
262  $query = 'SELECT '.implode( ',', $infos );
263  $query.= ' FROM '.CATEGORIES_TABLE.' AS a';
264  $query.= ', '.SITES_TABLE.' AS b';
265  $query.= ' WHERE a.id = '.$id;
266  $query.= ' AND a.site_id = b.id';
267  $query.= ';';
268  $row = mysql_fetch_array( mysql_query( $query ) );
269
270  $cat = array();
271  // affectation of each field of the table "config" to an information of the
272  // array $cat.
273  foreach ( $infos as $info ) {
274    if ( isset( $row[$info] ) ) $cat[$info] = $row[$info];
275    else                        $cat[$info] = '';
276    // If the field is true or false, the variable is transformed into a
277    // boolean value.
278    if ( $cat[$info] == 'true' or $cat[$info] == 'false' )
279    {
280      $cat[$info] = get_boolean( $cat[$info] );
281    }
282  }
283  $cat['comment'] = nl2br( $cat['comment'] );
284
285  $cat['name'] = array();
286
287  $query = 'SELECT name FROM '.CATEGORIES_TABLE;
288  $query.= ' WHERE id IN ('.$cat['uppercats'].')';
289  $query.= ' ORDER BY id ASC';
290  $query.= ';';
291  $result = mysql_query( $query );
292  while( $row = mysql_fetch_array( $result ) )
293  {
294    array_push( $cat['name'], $row['name'] );
295  }
296 
297  return $cat;
298}
299
300// get_complete_dir returns the concatenation of get_site_url and
301// get_local_dir
302// Example : "pets > rex > 1_year_old" is on the the same site as the
303// PhpWebGallery files and this category has 22 for identifier
304// get_complete_dir(22) returns "./galleries/pets/rex/1_year_old/"
305function get_complete_dir( $category_id )
306{
307  return get_site_url( $category_id ).get_local_dir( $category_id );
308}
309
310// get_local_dir returns an array with complete path without the site url
311// Example : "pets > rex > 1_year_old" is on the the same site as the
312// PhpWebGallery files and this category has 22 for identifier
313// get_local_dir(22) returns "pets/rex/1_year_old/"
314function get_local_dir( $category_id )
315{
316  global $page;
317
318  $uppercats = '';
319  $local_dir = '';
320
321  if ( isset( $page['plain_structure'][$category_id]['uppercats'] ) )
322  {
323    $uppercats = $page['plain_structure'][$category_id]['uppercats'];
324  }
325  else
326  {
327    $query = 'SELECT uppercats';
328    $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id = '.$category_id;
329    $query.= ';';
330    $row = mysql_fetch_array( mysql_query( $query ) );
331    $uppercats = $row['uppercats'];
332  }
333
334  $upper_array = explode( ',', $uppercats );
335
336  $database_dirs = array();
337  $query = 'SELECT id,dir';
338  $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id IN ('.$uppercats.')';
339  $query.= ';';
340  $result = mysql_query( $query );
341  while( $row = mysql_fetch_array( $result ) )
342  {
343    $database_dirs[$row['id']] = $row['dir'];
344  }
345  foreach ( $upper_array as $id ) {
346    $local_dir.= $database_dirs[$id].'/';
347  }
348
349  return $local_dir;
350}
351
352// retrieving the site url : "http://domain.com/gallery/" or
353// simply "./galleries/"
354function get_site_url( $category_id )
355{
356  global $page;
357
358  $query = 'SELECT galleries_url';
359  $query.= ' FROM '.SITES_TABLE.' AS s,'.CATEGORIES_TABLE.' AS c';
360  $query.= ' WHERE s.id = c.site_id';
361  $query.= ' AND c.id = '.$category_id;
362  $query.= ';';
363  $row = mysql_fetch_array( mysql_query( $query ) );
364  return $row['galleries_url'];
365}
366
367// The function get_cat_display_name returns a string containing the list
368// of upper categories to the root category from the lowest category shown
369// example : "anniversaires - fete mere 2002 - animaux - erika"
370// You can give two parameters :
371//   - $separation : the string between each category name " - " for example
372//   - $style : the style of the span tag for the lowest category,
373//     "font-style:italic;" for example
374function get_cat_display_name( $array_cat_names, $separation,
375                               $style, $replace_space = true )
376{
377  $output = '';
378  foreach ( $array_cat_names as $i => $name ) {
379    if ( $i > 0 ) $output.= $separation;
380    if ( $i < count( $array_cat_names ) - 1 or $style == '')
381      $output.= $name;
382    else
383      $output.= '<span style="'.$style.'">'.$name.'</span>';
384  }
385  if ( $replace_space ) return replace_space( $output );
386  else                  return $output;
387}
388
389// initialize_category initializes ;-) the variables in relation
390// with category :
391// 1. calculation of the number of pictures in the category
392// 2. determination of the SQL query part to ask to find the right category
393//    $page['where'] is not the same if we are in
394//       - simple category
395//       - search result
396//       - favorites displaying
397//       - most visited pictures
398//       - best rated pictures
399//       - recent pictures
400// 3. determination of the title of the page
401// 4. creation of the navigation bar
402function initialize_category( $calling_page = 'category' )
403{
404  pwg_debug( 'start initialize_category' );
405  global $page,$lang,$user,$conf;
406
407  if ( isset( $page['cat'] ) )
408  {
409    // $page['nb_image_page'] is the number of picture to display on this page
410    // By default, it is the same as the $user['nb_image_page']
411    $page['nb_image_page'] = $user['nb_image_page'];
412    // $url is used to create the navigation bar
413    $url = './category.php?cat='.$page['cat'];
414    if ( isset($page['expand']) ) $url.= '&amp;expand='.$page['expand'];
415    // simple category
416    if ( is_numeric( $page['cat'] ) )
417    {
418      $result = get_cat_info( $page['cat'] );
419      $page['comment']        = $result['comment'];
420      $page['cat_dir']        = $result['dir'];
421      $page['cat_name']       = $result['name'];
422      $page['cat_nb_images']  = $result['nb_images'];
423      $page['cat_site_id']    = $result['site_id'];
424      $page['cat_uploadable'] = $result['uploadable'];
425      $page['uppercats']      = $result['uppercats'];
426      $page['title'] = get_cat_display_name( $page['cat_name'],' - ','',false);
427      $page['where'] = ' WHERE category_id = '.$page['cat'];
428    }
429    else
430    {
431      if ( $page['cat'] == 'search' or $page['cat'] == 'most_visited'
432           or $page['cat'] == 'recent' or $page['cat'] == 'best_rated' )
433      {
434        // we must not show pictures of a forbidden category
435        if ( $user['forbidden_categories'] != '' )
436        {
437          $forbidden = ' category_id NOT IN ';
438          $forbidden.= '('.$user['forbidden_categories'].')';
439        }
440      }
441      // search result
442      if ( $page['cat'] == 'search' )
443      {
444        $page['title'] = $lang['search_result'];
445        if ( $calling_page == 'picture' )
446        {
447          $page['title'].= ' : <span style="font-style:italic;">';
448          $page['title'].= $_GET['search']."</span>";
449        }
450
451        $page['where'] = ' WHERE (';
452        $fields = array( 'file', 'name', 'comment', 'keywords' );
453        $words = explode( ',', $_GET['search'] );
454        $sql_search = array();
455        foreach ( $words as $i => $word ) {
456          // if the user searchs any of the words, the where statement must
457          // be :
458          // field1 LIKE '%$word1%' OR field2 LIKE '%$word1%' ...
459          // OR field1 LIKE '%$word2%' OR field2 LIKE '%$word2%' ...
460          if ( $_GET['mode'] == 'OR' )
461          {
462            if ( $i != 0 ) $page['where'].= ' OR';
463            foreach ( $fields as $j => $field ) {
464              if ( $j != 0 ) $page['where'].= ' OR';
465              $page['where'].= ' '.$field." LIKE '%".$word."%'";
466            }
467          }
468          // if the user searchs all the words :
469          // ( field1 LIKE '%$word1%' OR field2 LIKE '%$word1%' )
470          // AND ( field1 LIKE '%$word2%' OR field2 LIKE '%$word2%' )
471          else if ( $_GET['mode'] == 'AND' )
472          {
473            if ( $i != 0 ) $page['where'].= ' AND';
474            $page['where'].= ' (';
475            foreach ( $fields as $j => $field ) {
476              if ( $j != 0 ) $page['where'].= ' OR';
477              $page['where'].= ' '.$field." LIKE '%".$word."%'";
478            }
479            $page['where'].= ' )';
480          }
481        }
482        $page['where'].= ' )';
483        if ( isset( $forbidden ) ) $page['where'].= ' AND '.$forbidden;
484
485        $query = 'SELECT COUNT(DISTINCT(id)) AS nb_total_images';
486        $query.= ' FROM '.IMAGES_TABLE;
487        $query.= ' INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic';
488        $query.= ' ON id = ic.image_id';
489        $query.= $page['where'];
490        $query.= ';';
491
492        $url.= '&amp;search='.$_GET['search'].'&amp;mode='.$_GET['mode'];
493      }
494      // favorites displaying
495      else if ( $page['cat'] == 'fav' )
496      {
497        $page['title'] = $lang['favorites'];
498
499        $page['where'] = ', '.FAVORITES_TABLE.' AS fav';
500        $page['where'].= ' WHERE user_id = '.$user['id'];
501        $page['where'].= ' AND fav.image_id = id';
502     
503        $query = 'SELECT COUNT(*) AS nb_total_images';
504        $query.= ' FROM '.FAVORITES_TABLE;
505        $query.= ' WHERE user_id = '.$user['id'];
506        $query.= ';';
507      }
508      // pictures within the short period
509      else if ( $page['cat'] == 'recent' )
510      {
511        $page['title'] = $lang['recent_cat_title'];
512        // We must find the date corresponding to :
513        // today - $conf['periode_courte']
514        $date = time() - 60*60*24*$user['short_period'];
515        $page['where'] = " WHERE date_available > '";
516        $page['where'].= date( 'Y-m-d', $date )."'";
517        if ( isset( $forbidden ) ) $page['where'].= ' AND '.$forbidden;
518
519        $query = 'SELECT COUNT(DISTINCT(id)) AS nb_total_images';
520        $query.= ' FROM '.IMAGES_TABLE;
521        $query.= ' INNER JOIN '.PREFIX_TABLE.'image_category AS ic';
522        $query.= ' ON id = ic.image_id';
523        $query.= $page['where'];
524        $query.= ';';
525      }
526      // most visited pictures
527      else if ( $page['cat'] == 'most_visited' )
528      {
529        $page['title'] = $conf['top_number'].' '.$lang['most_visited_cat'];
530       
531        if ( isset( $forbidden ) ) $page['where'] = ' WHERE '.$forbidden;
532        else                       $page['where'] = '';
533        $conf['order_by'] = ' ORDER BY hit DESC, file ASC';
534        $page['cat_nb_images'] = $conf['top_number'];
535        if ( isset( $page['start'] )
536             and ($page['start']+$user['nb_image_page']>=$conf['top_number']))
537        {
538          $page['nb_image_page'] = $conf['top_number'] - $page['start'];
539        }
540      }
541
542      if ( isset($query))
543      {
544        $result = mysql_query( $query );
545        $row = mysql_fetch_array( $result );
546        $page['cat_nb_images'] = $row['nb_total_images'];
547      }
548    }
549    if ( $calling_page == 'category' )
550    {
551      $page['navigation_bar'] =
552        create_navigation_bar( $url, $page['cat_nb_images'], $page['start'],
553                               $user['nb_image_page'], 'back' );
554    }
555  }
556  else
557  {
558    $page['title'] = $lang['diapo_default_page_title'];
559  }
560  pwg_debug( 'end initialize_category' );
561}
562
563// get_non_empty_subcat_ids returns an array with sub-categories id
564// associated with their first non empty category id.
565//
566//                          example :
567//
568// - catname [cat_id]
569// - cat1 [1] -> given uppercat
570//   - cat1.1 [12] (empty)
571//     - cat1.1.1 [5] (empty)
572//     - cat1.1.2 [6]
573//   - cat1.2 [3]
574//   - cat1.3 [4]
575//
576// get_non_empty_sub_cat_ids will return :
577//   $ids[12] = 6;
578//   $ids[3]  = 3;
579//   $ids[4]  = 4;
580function get_non_empty_subcat_ids( $id_uppercat )
581{
582  global $user;
583
584  $ids = array();
585
586  $query = 'SELECT id,nb_images';
587  $query.= ' FROM '.CATEGORIES_TABLE;
588  $query.= ' WHERE id_uppercat ';
589  if ( !is_numeric( $id_uppercat ) ) $query.= 'is NULL';
590  else                               $query.= '= '.$id_uppercat;
591  // we must not show pictures of a forbidden category
592  if ( $user['forbidden_categories'] != '' )
593  {
594    $query.= ' AND id NOT IN ('.$user['forbidden_categories'].')';
595  }
596  $query.= ' ORDER BY rank';
597  $query.= ';';
598
599  $result = mysql_query( $query );
600  while ( $row = mysql_fetch_array( $result ) )
601  {
602    // only categories with findable picture in any of its subcats is
603    // represented.
604    if ( ( $row['nb_images'] != 0 and $non_empty_cat = $row['id'] )
605         or $non_empty_cat = get_first_non_empty_cat_id( $row['id'] ) )
606    {
607      $ids[$row['id']] = $non_empty_cat;
608    }
609  }
610  return $ids;
611}
612
613// get_first_non_empty_cat_id returns the id of the first non empty
614// sub-category to the given uppercat. If no picture is found in any
615// subcategory, false is returned.
616function get_first_non_empty_cat_id( $id_uppercat )
617{
618  global $user;
619
620  $query = 'SELECT id,nb_images';
621  $query.= ' FROM '.CATEGORIES_TABLE;
622  $query.= ' WHERE id_uppercat = '.$id_uppercat;
623  // we must not show pictures of a forbidden category
624  if ( $user['forbidden_categories'] != '' )
625  {
626    $query.= ' AND id NOT IN ('.$user['forbidden_categories'].')';
627  }
628  $query.= ' ORDER BY RAND()';
629  $query.= ';';
630  $result = mysql_query( $query );
631  while ( $row = mysql_fetch_array( $result ) )
632  {
633    if ( $row['nb_images'] > 0 )
634    {
635      return $row['id'];
636    }
637  }
638  $result = mysql_query( $query );
639  while ( $row = mysql_fetch_array( $result ) )
640  {
641    // recursive call
642    if ( $subcat = get_first_non_empty_cat_id( $row['id'] ) )
643    {
644      return $subcat;
645    }
646  }
647  return false;
648}
649?>
Note: See TracBrowser for help on using the repository browser.