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

Last change on this file since 362 was 362, checked in by z0rglub, 20 years ago

header global refactoring

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