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

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

Support of keywords for pictures. They are used in the search

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