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

Last change on this file since 1042 was 1042, checked in by rvelices, 18 years ago

feature 288: manage rates for administrators

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.6 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2005 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $RCSfile$
9// | last update   : $Date: 2006-02-15 02:29:26 +0000 (Wed, 15 Feb 2006) $
10// | last modifier : $Author: rvelices $
11// | revision      : $Revision: 1042 $
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, explode(',', $user['forbidden_categories'])))
48  {
49    echo '<div style="text-align:center;">'.$lang['access_forbiden'].'<br />';
50    echo '<a href="./category.php">';
51    echo $lang['thumbnails'].'</a></div>';
52    exit();
53  }
54}
55
56function get_categories_menu()
57{
58  global $page,$user;
59 
60  $infos = array('');
61 
62  $query = '
63SELECT name,id,date_last,nb_images,global_rank
64  FROM '.CATEGORIES_TABLE.'
65  WHERE 1 = 1'; // stupid but permit using AND after it !
66  if (!$user['expand'])
67  {
68    $query.= '
69    AND (id_uppercat is NULL';
70    if ( isset( $page['cat'] ) and is_numeric( $page['cat'] ) )
71    {
72      $query.= ' OR id_uppercat IN ('.$page['uppercats'].')';
73    }
74    $query.= ')';
75  }
76  if ($user['forbidden_categories'] != '')
77  {
78    $query.= '
79    AND id NOT IN ('.$user['forbidden_categories'].')';
80  }
81  $query.= '
82;';
83
84  $result = pwg_query($query);
85  $cats = array();
86  while ($row = mysql_fetch_array($result))
87  {
88    array_push($cats, $row);
89  }
90  usort($cats, 'global_rank_compare');
91
92  return get_html_menu_category($cats);
93}
94
95/**
96 * returns the total number of elements viewable in the gallery by the
97 * connected user
98 *
99 * @return int
100 */
101function count_user_total_images()
102{
103  global $user;
104
105  $query = '
106SELECT COUNT(DISTINCT(image_id)) as total
107  FROM '.IMAGE_CATEGORY_TABLE.'
108  WHERE category_id NOT IN ('.$user['forbidden_categories'].')
109;';
110  list($total) = mysql_fetch_array(pwg_query($query));
111 
112  return $total;
113}
114
115/**
116 * Retrieve informations about a category in the database
117 *
118 * Returns an array with following keys :
119 *
120 *  - comment
121 *  - dir : directory, might be empty for virtual categories
122 *  - name : an array with indexes from 0 (lowest cat name) to n (most
123 *           uppercat name findable)
124 *  - nb_images
125 *  - id_uppercat
126 *  - site_id
127 *  -
128 *
129 * @param int category id
130 * @return array
131 */
132function get_cat_info( $id )
133{
134  $infos = array('nb_images','id_uppercat','comment','site_id'
135                 ,'dir','date_last','uploadable','status','visible'
136                 ,'representative_picture_id','uppercats','commentable');
137 
138  $query = '
139SELECT '.implode(',', $infos).'
140  FROM '.CATEGORIES_TABLE.'
141  WHERE id = '.$id.'
142;';
143  $row = mysql_fetch_array(pwg_query($query));
144
145  $cat = array();
146  foreach ($infos as $info)
147  {
148    if (isset($row[$info]))
149    {
150      $cat[$info] = $row[$info];
151    }
152    else
153    {
154      $cat[$info] = '';
155    }
156    // If the field is true or false, the variable is transformed into a
157    // boolean value.
158    if ($cat[$info] == 'true' or $cat[$info] == 'false')
159    {
160      $cat[$info] = get_boolean( $cat[$info] );
161    }
162  }
163  $cat['comment'] = nl2br($cat['comment']);
164
165  $names = array();
166  $query = '
167SELECT name,id
168  FROM '.CATEGORIES_TABLE.'
169  WHERE id IN ('.$cat['uppercats'].')
170;';
171  $result = pwg_query($query);
172  while($row = mysql_fetch_array($result))
173  {
174    $names[$row['id']] = $row['name'];
175  }
176
177  // category names must be in the same order than uppercats list
178  $cat['name'] = array();
179  foreach (explode(',', $cat['uppercats']) as $cat_id)
180  {
181    $cat['name'][$cat_id] = $names[$cat_id];
182  }
183 
184  return $cat;
185}
186
187// get_complete_dir returns the concatenation of get_site_url and
188// get_local_dir
189// Example : "pets > rex > 1_year_old" is on the the same site as the
190// PhpWebGallery files and this category has 22 for identifier
191// get_complete_dir(22) returns "./galleries/pets/rex/1_year_old/"
192function get_complete_dir( $category_id )
193{
194  return get_site_url($category_id).get_local_dir($category_id);
195}
196
197// get_local_dir returns an array with complete path without the site url
198// Example : "pets > rex > 1_year_old" is on the the same site as the
199// PhpWebGallery files and this category has 22 for identifier
200// get_local_dir(22) returns "pets/rex/1_year_old/"
201function get_local_dir( $category_id )
202{
203  global $page;
204
205  $uppercats = '';
206  $local_dir = '';
207
208  if ( isset( $page['plain_structure'][$category_id]['uppercats'] ) )
209  {
210    $uppercats = $page['plain_structure'][$category_id]['uppercats'];
211  }
212  else
213  {
214    $query = 'SELECT uppercats';
215    $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id = '.$category_id;
216    $query.= ';';
217    $row = mysql_fetch_array( pwg_query( $query ) );
218    $uppercats = $row['uppercats'];
219  }
220
221  $upper_array = explode( ',', $uppercats );
222
223  $database_dirs = array();
224  $query = 'SELECT id,dir';
225  $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id IN ('.$uppercats.')';
226  $query.= ';';
227  $result = pwg_query( $query );
228  while( $row = mysql_fetch_array( $result ) )
229  {
230    $database_dirs[$row['id']] = $row['dir'];
231  }
232  foreach ($upper_array as $id)
233  {
234    $local_dir.= $database_dirs[$id].'/';
235  }
236
237  return $local_dir;
238}
239
240// retrieving the site url : "http://domain.com/gallery/" or
241// simply "./galleries/"
242function get_site_url($category_id)
243{
244  global $page;
245
246  $query = '
247SELECT galleries_url
248  FROM '.SITES_TABLE.' AS s,'.CATEGORIES_TABLE.' AS c
249  WHERE s.id = c.site_id
250    AND c.id = '.$category_id.'
251;';
252  $row = mysql_fetch_array(pwg_query($query));
253  return $row['galleries_url'];
254}
255
256// returns an array of image orders available for users/visitors
257function get_category_preferred_image_orders()
258{
259  global $conf;
260  return array(
261    array('Default', '', true),
262    array(l10n('Average rate'), 'average_rate DESC', $conf['rate']),
263    array(l10n('most_visited_cat'), 'hit DESC', true),
264    array(l10n('Creation date'), 'date_creation DESC', true),
265    array(l10n('Availability date'), 'date_available DESC', true),
266    array(l10n('File name'), 'file ASC', true)
267  );
268}
269
270function display_select_categories($categories,
271                                   $selecteds,
272                                   $blockname,
273                                   $fullname = true)
274{
275  global $template;
276
277  foreach ($categories as $category)
278  {
279    $selected = '';
280    if (in_array($category['id'], $selecteds))
281    {
282      $selected = ' selected="selected"';
283    }
284
285    if ($fullname)
286    {
287      $option = get_cat_display_name_cache($category['uppercats'],
288                                           '',
289                                           false);
290    }
291    else
292    {
293      $option = str_repeat('&nbsp;',
294                           (3 * substr_count($category['global_rank'], '.')));
295      $option.= '- '.$category['name'];
296    }
297   
298    $template->assign_block_vars(
299      $blockname,
300      array('SELECTED'=>$selected,
301            'VALUE'=>$category['id'],
302            'OPTION'=>$option
303        ));
304  }
305}
306
307function display_select_cat_wrapper($query, $selecteds, $blockname,
308                                    $fullname = true)
309{
310  $result = pwg_query($query);
311  $categories = array();
312  if (!empty($result))
313  {
314    while ($row = mysql_fetch_array($result))
315    {
316      array_push($categories, $row);
317    }
318  }
319  usort($categories, 'global_rank_compare');
320  display_select_categories($categories, $selecteds, $blockname, $fullname);
321}
322
323/**
324 * returns all subcategory identifiers of given category ids
325 *
326 * @param array ids
327 * @return array
328 */
329function get_subcat_ids($ids)
330{
331  $query = '
332SELECT DISTINCT(id)
333  FROM '.CATEGORIES_TABLE.'
334  WHERE ';
335  foreach ($ids as $num => $category_id)
336  {
337    if ($num > 0)
338    {
339      $query.= '
340    OR ';
341    }
342    $query.= 'uppercats REGEXP \'(^|,)'.$category_id.'(,|$)\'';
343  }
344  $query.= '
345;';
346  $result = pwg_query($query);
347
348  $subcats = array();
349  while ($row = mysql_fetch_array($result))
350  {
351    array_push($subcats, $row['id']);
352  }
353  return $subcats;
354}
355
356function global_rank_compare($a, $b)
357{
358  return strnatcasecmp($a['global_rank'], $b['global_rank']);
359}
360
361function rank_compare($a, $b)
362{
363  if ($a['rank'] == $b['rank'])
364  {
365    return 0;
366  }
367
368  return ($a['rank'] < $b['rank']) ? -1 : 1;
369}
370?>
Note: See TracBrowser for help on using the repository browser.