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

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

table user_category dropped

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