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