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

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

feature 169: each category can have its own image order

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