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

Last change on this file since 133 was 133, checked in by z0rglub, 21 years ago

A category can have its representative picture

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