[2] | 1 | <?php |
---|
[362] | 2 | // +-----------------------------------------------------------------------+ |
---|
[593] | 3 | // | PhpWebGallery - a PHP based picture gallery | |
---|
| 4 | // | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net | |
---|
[1677] | 5 | // | Copyright (C) 2003-2006 PhpWebGallery Team - http://phpwebgallery.net | |
---|
[362] | 6 | // +-----------------------------------------------------------------------+ |
---|
[593] | 7 | // | branch : BSF (Best So Far) |
---|
[1113] | 8 | // | file : $Id: functions_category.inc.php 1677 2006-12-21 21:38:20Z rub $ |
---|
[362] | 9 | // | last update : $Date: 2006-12-21 21:38:20 +0000 (Thu, 21 Dec 2006) $ |
---|
| 10 | // | last modifier : $Author: rub $ |
---|
| 11 | // | revision : $Revision: 1677 $ |
---|
| 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 | |
---|
[1677] | 47 | // $filter['visible_categories'] and $filter['visible_images'] |
---|
| 48 | // are not used because it's not necessary (filter <> restriction) |
---|
[808] | 49 | if (in_array($category_id, explode(',', $user['forbidden_categories']))) |
---|
[2] | 50 | { |
---|
[1113] | 51 | access_denied(); |
---|
[2] | 52 | } |
---|
| 53 | } |
---|
[345] | 54 | |
---|
[614] | 55 | function get_categories_menu() |
---|
[2] | 56 | { |
---|
[1677] | 57 | global $page, $user, $filter; |
---|
[1059] | 58 | |
---|
[1624] | 59 | $query = ' |
---|
| 60 | SELECT '; |
---|
| 61 | // From CATEGORIES_TABLE |
---|
| 62 | $query.= ' |
---|
| 63 | name, id, nb_images, global_rank,'; |
---|
| 64 | // From USER_CACHE_CATEGORIES_TABLE |
---|
| 65 | $query.= ' |
---|
[1641] | 66 | date_last, max_date_last, count_images, count_categories'; |
---|
[1059] | 67 | |
---|
[1624] | 68 | // $user['forbidden_categories'] including with USER_CACHE_CATEGORIES_TABLE |
---|
| 69 | $query.= ' |
---|
[1677] | 70 | FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.' |
---|
[1624] | 71 | ON id = cat_id and user_id = '.$user['id']; |
---|
[1677] | 72 | |
---|
| 73 | // Always expand when filter is activated |
---|
| 74 | if (!$user['expand'] and !$filter['enabled']) |
---|
[345] | 75 | { |
---|
[589] | 76 | $query.= ' |
---|
[1677] | 77 | WHERE |
---|
| 78 | (id_uppercat is NULL'; |
---|
| 79 | if (isset($page['category'])) |
---|
| 80 | { |
---|
| 81 | $query.= ' OR id_uppercat IN ('.$page['uppercats'].')'; |
---|
| 82 | } |
---|
| 83 | $query.= ')'; |
---|
[1648] | 84 | } |
---|
| 85 | else |
---|
| 86 | { |
---|
[1677] | 87 | $query.= ' |
---|
| 88 | '.get_sql_condition_FandF |
---|
| 89 | ( |
---|
| 90 | array |
---|
| 91 | ( |
---|
| 92 | 'visible_categories' => 'id', |
---|
| 93 | ), |
---|
| 94 | 'WHERE' |
---|
| 95 | ); |
---|
[2] | 96 | } |
---|
[1677] | 97 | |
---|
[589] | 98 | $query.= ' |
---|
| 99 | ;'; |
---|
[26] | 100 | |
---|
[589] | 101 | $result = pwg_query($query); |
---|
[614] | 102 | $cats = array(); |
---|
[1641] | 103 | while ($row = mysql_fetch_assoc($result)) |
---|
[2] | 104 | { |
---|
[614] | 105 | array_push($cats, $row); |
---|
[26] | 106 | } |
---|
[614] | 107 | usort($cats, 'global_rank_compare'); |
---|
[2] | 108 | |
---|
[1677] | 109 | // Update filtered data |
---|
| 110 | update_cats_with_filtered_data($cats); |
---|
| 111 | |
---|
[614] | 112 | return get_html_menu_category($cats); |
---|
[26] | 113 | } |
---|
[2] | 114 | |
---|
[1624] | 115 | |
---|
[761] | 116 | /** |
---|
[423] | 117 | * Retrieve informations about a category in the database |
---|
| 118 | * |
---|
| 119 | * Returns an array with following keys : |
---|
| 120 | * |
---|
| 121 | * - comment |
---|
| 122 | * - dir : directory, might be empty for virtual categories |
---|
| 123 | * - name : an array with indexes from 0 (lowest cat name) to n (most |
---|
| 124 | * uppercat name findable) |
---|
| 125 | * - nb_images |
---|
| 126 | * - id_uppercat |
---|
| 127 | * - site_id |
---|
[1059] | 128 | * - |
---|
[423] | 129 | * |
---|
| 130 | * @param int category id |
---|
| 131 | * @return array |
---|
| 132 | */ |
---|
[2] | 133 | function get_cat_info( $id ) |
---|
| 134 | { |
---|
[603] | 135 | $infos = array('nb_images','id_uppercat','comment','site_id' |
---|
| 136 | ,'dir','date_last','uploadable','status','visible' |
---|
[1500] | 137 | ,'representative_picture_id','uppercats','commentable' |
---|
| 138 | ,'image_order'); |
---|
[1059] | 139 | |
---|
[603] | 140 | $query = ' |
---|
| 141 | SELECT '.implode(',', $infos).' |
---|
| 142 | FROM '.CATEGORIES_TABLE.' |
---|
| 143 | WHERE id = '.$id.' |
---|
| 144 | ;'; |
---|
| 145 | $row = mysql_fetch_array(pwg_query($query)); |
---|
[1288] | 146 | if (empty($row)) |
---|
| 147 | return null; |
---|
[345] | 148 | |
---|
| 149 | $cat = array(); |
---|
[603] | 150 | foreach ($infos as $info) |
---|
| 151 | { |
---|
| 152 | if (isset($row[$info])) |
---|
| 153 | { |
---|
| 154 | $cat[$info] = $row[$info]; |
---|
| 155 | } |
---|
| 156 | else |
---|
| 157 | { |
---|
| 158 | $cat[$info] = ''; |
---|
| 159 | } |
---|
[345] | 160 | // If the field is true or false, the variable is transformed into a |
---|
| 161 | // boolean value. |
---|
[603] | 162 | if ($cat[$info] == 'true' or $cat[$info] == 'false') |
---|
[345] | 163 | { |
---|
| 164 | $cat[$info] = get_boolean( $cat[$info] ); |
---|
| 165 | } |
---|
| 166 | } |
---|
[1046] | 167 | global $conf; |
---|
| 168 | if ( !( $conf['allow_html_descriptions'] and |
---|
[1573] | 169 | preg_match('/<(div|br|img|script).*>/i', $cat['comment']) ) ) |
---|
[1046] | 170 | { |
---|
| 171 | $cat['comment'] = nl2br($cat['comment']); |
---|
| 172 | } |
---|
[345] | 173 | |
---|
[672] | 174 | $names = array(); |
---|
[603] | 175 | $query = ' |
---|
| 176 | SELECT name,id |
---|
| 177 | FROM '.CATEGORIES_TABLE.' |
---|
| 178 | WHERE id IN ('.$cat['uppercats'].') |
---|
| 179 | ;'; |
---|
| 180 | $result = pwg_query($query); |
---|
| 181 | while($row = mysql_fetch_array($result)) |
---|
[2] | 182 | { |
---|
[672] | 183 | $names[$row['id']] = $row['name']; |
---|
[2] | 184 | } |
---|
[672] | 185 | |
---|
| 186 | // category names must be in the same order than uppercats list |
---|
| 187 | $cat['name'] = array(); |
---|
| 188 | foreach (explode(',', $cat['uppercats']) as $cat_id) |
---|
| 189 | { |
---|
| 190 | $cat['name'][$cat_id] = $names[$cat_id]; |
---|
| 191 | } |
---|
[1059] | 192 | |
---|
[2] | 193 | return $cat; |
---|
| 194 | } |
---|
[61] | 195 | |
---|
| 196 | // get_complete_dir returns the concatenation of get_site_url and |
---|
| 197 | // get_local_dir |
---|
| 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_complete_dir(22) returns "./galleries/pets/rex/1_year_old/" |
---|
| 201 | function get_complete_dir( $category_id ) |
---|
| 202 | { |
---|
[579] | 203 | return get_site_url($category_id).get_local_dir($category_id); |
---|
[61] | 204 | } |
---|
| 205 | |
---|
| 206 | // get_local_dir returns an array with complete path without the site url |
---|
| 207 | // Example : "pets > rex > 1_year_old" is on the the same site as the |
---|
| 208 | // PhpWebGallery files and this category has 22 for identifier |
---|
| 209 | // get_local_dir(22) returns "pets/rex/1_year_old/" |
---|
| 210 | function get_local_dir( $category_id ) |
---|
| 211 | { |
---|
| 212 | global $page; |
---|
| 213 | |
---|
[345] | 214 | $uppercats = ''; |
---|
| 215 | $local_dir = ''; |
---|
| 216 | |
---|
| 217 | if ( isset( $page['plain_structure'][$category_id]['uppercats'] ) ) |
---|
[61] | 218 | { |
---|
[345] | 219 | $uppercats = $page['plain_structure'][$category_id]['uppercats']; |
---|
[61] | 220 | } |
---|
[345] | 221 | else |
---|
| 222 | { |
---|
| 223 | $query = 'SELECT uppercats'; |
---|
| 224 | $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id = '.$category_id; |
---|
| 225 | $query.= ';'; |
---|
[587] | 226 | $row = mysql_fetch_array( pwg_query( $query ) ); |
---|
[345] | 227 | $uppercats = $row['uppercats']; |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | $upper_array = explode( ',', $uppercats ); |
---|
| 231 | |
---|
| 232 | $database_dirs = array(); |
---|
| 233 | $query = 'SELECT id,dir'; |
---|
| 234 | $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id IN ('.$uppercats.')'; |
---|
| 235 | $query.= ';'; |
---|
[587] | 236 | $result = pwg_query( $query ); |
---|
[345] | 237 | while( $row = mysql_fetch_array( $result ) ) |
---|
| 238 | { |
---|
| 239 | $database_dirs[$row['id']] = $row['dir']; |
---|
| 240 | } |
---|
[579] | 241 | foreach ($upper_array as $id) |
---|
| 242 | { |
---|
[345] | 243 | $local_dir.= $database_dirs[$id].'/'; |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | return $local_dir; |
---|
[61] | 247 | } |
---|
| 248 | |
---|
| 249 | // retrieving the site url : "http://domain.com/gallery/" or |
---|
| 250 | // simply "./galleries/" |
---|
[579] | 251 | function get_site_url($category_id) |
---|
[61] | 252 | { |
---|
| 253 | global $page; |
---|
| 254 | |
---|
[579] | 255 | $query = ' |
---|
| 256 | SELECT galleries_url |
---|
| 257 | FROM '.SITES_TABLE.' AS s,'.CATEGORIES_TABLE.' AS c |
---|
| 258 | WHERE s.id = c.site_id |
---|
| 259 | AND c.id = '.$category_id.' |
---|
| 260 | ;'; |
---|
[587] | 261 | $row = mysql_fetch_array(pwg_query($query)); |
---|
[61] | 262 | return $row['galleries_url']; |
---|
| 263 | } |
---|
| 264 | |
---|
[1020] | 265 | // returns an array of image orders available for users/visitors |
---|
| 266 | function get_category_preferred_image_orders() |
---|
| 267 | { |
---|
[1029] | 268 | global $conf; |
---|
[1020] | 269 | return array( |
---|
[1059] | 270 | array(l10n('default_sort'), '', true), |
---|
[1042] | 271 | array(l10n('Average rate'), 'average_rate DESC', $conf['rate']), |
---|
[1031] | 272 | array(l10n('most_visited_cat'), 'hit DESC', true), |
---|
| 273 | array(l10n('Creation date'), 'date_creation DESC', true), |
---|
[1059] | 274 | array(l10n('Post date'), 'date_available DESC', true), |
---|
[1031] | 275 | array(l10n('File name'), 'file ASC', true) |
---|
[1020] | 276 | ); |
---|
| 277 | } |
---|
| 278 | |
---|
[589] | 279 | function display_select_categories($categories, |
---|
| 280 | $selecteds, |
---|
[602] | 281 | $blockname, |
---|
[614] | 282 | $fullname = true) |
---|
[589] | 283 | { |
---|
[614] | 284 | global $template; |
---|
[589] | 285 | |
---|
| 286 | foreach ($categories as $category) |
---|
| 287 | { |
---|
[614] | 288 | $selected = ''; |
---|
| 289 | if (in_array($category['id'], $selecteds)) |
---|
[589] | 290 | { |
---|
[614] | 291 | $selected = ' selected="selected"'; |
---|
| 292 | } |
---|
[589] | 293 | |
---|
[614] | 294 | if ($fullname) |
---|
| 295 | { |
---|
| 296 | $option = get_cat_display_name_cache($category['uppercats'], |
---|
[1092] | 297 | null, |
---|
[614] | 298 | false); |
---|
[589] | 299 | } |
---|
[614] | 300 | else |
---|
| 301 | { |
---|
| 302 | $option = str_repeat(' ', |
---|
| 303 | (3 * substr_count($category['global_rank'], '.'))); |
---|
| 304 | $option.= '- '.$category['name']; |
---|
| 305 | } |
---|
[1059] | 306 | |
---|
[614] | 307 | $template->assign_block_vars( |
---|
| 308 | $blockname, |
---|
| 309 | array('SELECTED'=>$selected, |
---|
| 310 | 'VALUE'=>$category['id'], |
---|
| 311 | 'OPTION'=>$option |
---|
| 312 | )); |
---|
[589] | 313 | } |
---|
| 314 | } |
---|
[603] | 315 | |
---|
[614] | 316 | function display_select_cat_wrapper($query, $selecteds, $blockname, |
---|
| 317 | $fullname = true) |
---|
| 318 | { |
---|
| 319 | $result = pwg_query($query); |
---|
| 320 | $categories = array(); |
---|
[655] | 321 | if (!empty($result)) |
---|
| 322 | { |
---|
[657] | 323 | while ($row = mysql_fetch_array($result)) |
---|
| 324 | { |
---|
| 325 | array_push($categories, $row); |
---|
| 326 | } |
---|
[614] | 327 | } |
---|
| 328 | usort($categories, 'global_rank_compare'); |
---|
| 329 | display_select_categories($categories, $selecteds, $blockname, $fullname); |
---|
| 330 | } |
---|
| 331 | |
---|
[603] | 332 | /** |
---|
| 333 | * returns all subcategory identifiers of given category ids |
---|
| 334 | * |
---|
| 335 | * @param array ids |
---|
| 336 | * @return array |
---|
| 337 | */ |
---|
| 338 | function get_subcat_ids($ids) |
---|
| 339 | { |
---|
| 340 | $query = ' |
---|
| 341 | SELECT DISTINCT(id) |
---|
| 342 | FROM '.CATEGORIES_TABLE.' |
---|
| 343 | WHERE '; |
---|
| 344 | foreach ($ids as $num => $category_id) |
---|
| 345 | { |
---|
| 346 | if ($num > 0) |
---|
| 347 | { |
---|
| 348 | $query.= ' |
---|
| 349 | OR '; |
---|
| 350 | } |
---|
| 351 | $query.= 'uppercats REGEXP \'(^|,)'.$category_id.'(,|$)\''; |
---|
| 352 | } |
---|
| 353 | $query.= ' |
---|
| 354 | ;'; |
---|
| 355 | $result = pwg_query($query); |
---|
| 356 | |
---|
| 357 | $subcats = array(); |
---|
| 358 | while ($row = mysql_fetch_array($result)) |
---|
| 359 | { |
---|
| 360 | array_push($subcats, $row['id']); |
---|
| 361 | } |
---|
| 362 | return $subcats; |
---|
| 363 | } |
---|
[614] | 364 | |
---|
| 365 | function global_rank_compare($a, $b) |
---|
| 366 | { |
---|
| 367 | return strnatcasecmp($a['global_rank'], $b['global_rank']); |
---|
| 368 | } |
---|
[1036] | 369 | |
---|
| 370 | function rank_compare($a, $b) |
---|
| 371 | { |
---|
| 372 | if ($a['rank'] == $b['rank']) |
---|
| 373 | { |
---|
| 374 | return 0; |
---|
| 375 | } |
---|
| 376 | |
---|
| 377 | return ($a['rank'] < $b['rank']) ? -1 : 1; |
---|
| 378 | } |
---|
[1624] | 379 | |
---|
| 380 | /** |
---|
| 381 | * returns display text for information images of category |
---|
| 382 | * |
---|
| 383 | * @param array categories |
---|
| 384 | * @return string |
---|
| 385 | */ |
---|
| 386 | function get_display_images_count($cat_nb_images, $cat_count_images, $cat_count_categories, $short_message = true) |
---|
| 387 | { |
---|
| 388 | $display_text = ''; |
---|
| 389 | |
---|
| 390 | // Count of category is main |
---|
| 391 | // if not picture on categorie, test on sub-categories |
---|
| 392 | $count = ($cat_nb_images > 0 ? $cat_nb_images : $cat_count_images); |
---|
| 393 | |
---|
| 394 | if ($count > 0) |
---|
| 395 | { |
---|
[1637] | 396 | $display_text.= l10n_dec('image_available', 'images_available', $count); |
---|
[1624] | 397 | |
---|
| 398 | if ($cat_nb_images > 0) |
---|
| 399 | { |
---|
| 400 | if (! $short_message) |
---|
| 401 | { |
---|
| 402 | $display_text.= ' '.l10n('images_available_cpl'); |
---|
| 403 | } |
---|
| 404 | } |
---|
| 405 | else |
---|
| 406 | { |
---|
[1637] | 407 | $display_text.= ' '.l10n_dec('images_available_cat', 'images_available_cats', $cat_count_categories); |
---|
[1624] | 408 | } |
---|
| 409 | } |
---|
| 410 | |
---|
| 411 | return $display_text; |
---|
| 412 | } |
---|
| 413 | |
---|
[1641] | 414 | ?> |
---|