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