diff options
author | gweltas <gweltas@piwigo.org> | 2004-02-02 00:55:18 +0000 |
---|---|---|
committer | gweltas <gweltas@piwigo.org> | 2004-02-02 00:55:18 +0000 |
commit | bef4b3e3aa8e3d54cbf8b4962b9b5d4a89b55429 (patch) | |
tree | 647b2cf07ee8451a9314e1e8aebd11d9396cb32b /include/functions_category.inc.php | |
parent | eea989f019f21fbd7ae4aa8e2f4a1503992c23bf (diff) |
Merge of the 1.3.1 release
Creation of an unique include file (common.php)
Creation of an unique define file (include/constants.php)
Modification of the installation procedure
git-svn-id: http://piwigo.org/svn/trunk@345 68402e56-0260-453c-a942-63ccdbb3a9ee
Diffstat (limited to '')
-rw-r--r-- | include/functions_category.inc.php | 355 |
1 files changed, 171 insertions, 184 deletions
diff --git a/include/functions_category.inc.php b/include/functions_category.inc.php index a691140d6..311935b49 100644 --- a/include/functions_category.inc.php +++ b/include/functions_category.inc.php @@ -17,31 +17,11 @@ * * ***************************************************************************/ -function get_subcats_id( $cat_id ) -{ - $restricted_cats = array(); - - $query = 'SELECT id'; - $query.= ' FROM '.PREFIX_TABLE.'categories'; - $query.= ' WHERE id_uppercat = '.$cat_id; - $query.= ';'; - $result = mysql_query( $query ); - while ( $row = mysql_fetch_array( $result ) ) - { - array_push( $restricted_cats, $row['id'] ); - $sub_restricted_cats = get_subcats_id( $row['id'] ); - foreach ( $sub_restricted_cats as $sub_restricted_cat ) { - array_push( $restricted_cats, $sub_restricted_cat ); - } - } - return $restricted_cats; -} - function check_restrictions( $category_id ) { global $user,$lang; - if ( is_user_allowed( $category_id, $user['restrictions'] ) > 0 ) + if ( in_array( $category_id, $user['restrictions'] ) ) { echo '<div style="text-align:center;">'.$lang['access_forbiden'].'<br />'; echo '<a href="'.add_session_id( './category.php' ).'">'; @@ -49,7 +29,7 @@ function check_restrictions( $category_id ) exit(); } } - + // the check_cat_id function check whether the $cat is a right parameter : // - $cat is numeric and corresponds to a category in the database // - $cat equals 'fav' (for favorites) @@ -61,19 +41,14 @@ function check_cat_id( $cat ) unset( $page['cat'] ); if ( isset( $cat ) ) { - if ( isset( $page['plain_structure'] ) ) + if ( isset( $page['plain_structure'][$cat] ) ) { - if ( isset( $page['plain_structure'][$cat] ) ) - { - $page['cat'] = $cat; - } + $page['cat'] = $cat; } else if ( is_numeric( $cat ) ) { $query = 'SELECT id'; - $query.= ' FROM '.PREFIX_TABLE.'categories'; - $query.= ' WHERE id = '.$cat; - $query. ';'; + $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id = '.$cat.';'; $result = mysql_query( $query ); if ( mysql_num_rows( $result ) != 0 ) { @@ -91,17 +66,34 @@ function check_cat_id( $cat ) } } -function get_plain_structure() +function get_user_plain_structure() { - $infos = array( 'name','id','date_last','nb_images','dir','id_uppercat', - 'rank','site_id'); + global $page,$user; + + $infos = array( 'name','id','uc.date_last','nb_images','dir','id_uppercat', + 'rank','site_id','nb_sub_categories','uppercats'); - $query = 'SELECT '; - foreach ( $infos as $i => $info ) { - if ( $i > 0 ) $query.= ','; - $query.= $info; + $query = 'SELECT '.implode( ',', $infos ); + $query.= ' FROM '.CATEGORIES_TABLE.' AS c'; +// $query.= ' ,'.PREFIX_TABLE.'user_category AS uc'; + $query.= ' INNER JOIN '.USER_CATEGORY_TABLE.' AS uc'; + $query.= ' ON c.id = uc.category_id'; + $query.= ' WHERE user_id = '.$user['id']; + if ( $page['expand'] != 'all' ) + { + $query.= ' AND (id_uppercat is NULL'; + if ( count( $page['tab_expand'] ) > 0 ) + { + $query.= ' OR id_uppercat IN ('.$page['expand'].')'; + } + $query.= ')'; + } + if ( $user['forbidden_categories'] != '' ) + { + $query.= ' AND id NOT IN '; + $query.= '('.$user['forbidden_categories'].')'; } - $query.= ' FROM '.PREFIX_TABLE.'categories'; +// $query.= ' AND c.id = uc.category_id'; $query.= ' ORDER BY id_uppercat ASC, rank ASC'; $query.= ';'; @@ -111,12 +103,13 @@ function get_plain_structure() { $category = array(); foreach ( $infos as $info ) { - $category[$info] = $row[$info]; - if ( $info == 'date_last' ) + if ( $info == 'uc.date_last' ) { - list($year,$month,$day) = explode( '-', $row[$info] ); - $category[$info] = mktime(0,0,0,$month,$day,$year); + list($year,$month,$day) = explode( '-', $row['date_last'] ); + $category['date_last'] = mktime(0,0,0,$month,$day,$year); } + else if ( isset( $row[$info] ) ) $category[$info] = $row[$info]; + else $category[$info] = ''; } $plain_structure[$row['id']] = $category; } @@ -124,27 +117,24 @@ function get_plain_structure() return $plain_structure; } -function create_structure( $id_uppercat, $restrictions ) +function create_user_structure( $id_uppercat ) { global $page; if ( !isset( $page['plain_structure'] ) ) - $page['plain_structure'] = get_plain_structure(); + $page['plain_structure'] = get_user_plain_structure(); $structure = array(); - $ids = get_subcat_ids( $id_uppercat ); + $ids = get_user_subcat_ids( $id_uppercat ); foreach ( $ids as $id ) { - if ( !in_array( $id, $restrictions ) ) - { - $category = $page['plain_structure'][$id]; - $category['subcats'] = create_structure( $id, $restrictions ); - array_push( $structure, $category ); - } + $category = $page['plain_structure'][$id]; + $category['subcats'] = create_user_structure( $id ); + array_push( $structure, $category ); } return $structure; } -function get_subcat_ids( $id_uppercat ) +function get_user_subcat_ids( $id_uppercat ) { global $page; @@ -157,31 +147,14 @@ function get_subcat_ids( $id_uppercat ) } // update_structure updates or add informations about each node of the -// structure : the last date, should the category be expanded in the menu ?, -// the associated expand string "48,14,54" -// -// 1. last date -// for each category of the structure, we have to find the most recent -// subcat so that the parent cat has the same last_date info. -// For example : we have : -// > pets (2003.02.15) -// > dogs (2003.06.14) -// > rex (2003.06.18) -// > toby (2003.06.13) -// > kitten (2003.07.05) -// We finally want to have : -// > pets (2003.07.05) <- changed to pets > kitten last date -// > dogs (2003.06.18) <- changed to pets > dogs > rex last date -// > rex (2003.06.18) -// > toby (2003.06.13) -// > kitten (2003.07.05) +// structure : // -// 2. should the category be expanded in the menu ? +// 1. should the category be expanded in the menu ? // If the category has to be expanded (ie its id is in the // $page['tab_expand'] or all the categories must be expanded by default), // $category['expanded'] is set to true. // -// 3. associated expand string +// 2. associated expand string // in the menu, there is a expand string (used in the URL) to tell which // categories must be expanded in the menu if this category is chosen function update_structure( $categories ) @@ -191,9 +164,6 @@ function update_structure( $categories ) $updated_categories = array(); foreach ( $categories as $category ) { - // update the last date of the category - $last_date = search_last_date( $category ); - $category['date_last'] = $last_date; // update the "expanded" key if ( $user['expand'] or $page['expand'] == 'all' @@ -205,7 +175,7 @@ function update_structure( $categories ) { $category['expanded'] = false; } - // update the "expand_string" key + // update the "expand_string" key if ( $page['expand'] == 'all' ) { $category['expand_string'] = 'all'; @@ -219,7 +189,7 @@ function update_structure( $categories ) // the $category['id'] $tab_expand = array_diff( $page['tab_expand'],array($category['id']) ); } - else if ( count( $category['subcats'] ) > 0 ) + else if ( $category['nb_sub_categories'] > 0 ) { // we have this time to add the $category['id']... $tab_expand = array_merge($page['tab_expand'],array($category['id'])); @@ -235,24 +205,6 @@ function update_structure( $categories ) return $updated_categories; } -// search_last_date searchs the last date for a given category. If we take -// back the example given for update_last_dates, we should have : -// search_last_date( pets ) --> 2003.07.05 -// search_last_date( pets > dogs ) --> 2003.06.18 -// and so on -function search_last_date( $category ) -{ - $date_last = $category['date_last']; - foreach ( $category['subcats'] as $subcat ) { - $subcat_date_last = search_last_date( $subcat ); - if ( $subcat_date_last > $date_last ) - { - $date_last = $subcat_date_last; - } - } - return $date_last; -} - // count_images returns the number of pictures contained in the given // category represented by an array, in this array, we have (among other // things) : @@ -262,6 +214,7 @@ function search_last_date( $category ) // pictures contained in the given given category function count_images( $categories ) { + return count_user_total_images(); $total = 0; foreach ( $categories as $category ) { $total+= $category['nb_images']; @@ -270,6 +223,23 @@ function count_images( $categories ) return $total; } +function count_user_total_images() +{ + global $user; + + $query = 'SELECT SUM(nb_images) AS total'; + $query.= ' FROM '.CATEGORIES_TABLE; + if ( count( $user['restrictions'] ) > 0 ) + $query.= ' WHERE id NOT IN ('.$user['forbidden_categories'].')'; + $query.= ';'; + + $row = mysql_fetch_array( mysql_query( $query ) ); + + if ( !isset( $row['total'] ) ) $row['total'] = 0; + + return $row['total']; +} + // variables : // $cat['comment'] // $cat['dir'] @@ -285,37 +255,45 @@ function get_cat_info( $id ) { global $page; - $cat = array(); - - $query = 'SELECT nb_images,id_uppercat,comment,site_id,galleries_url,dir'; - $query.= ',date_last,uploadable,status,visible,representative_picture_id'; - $query.= ' FROM '.PREFIX_TABLE.'categories AS a'; - $query.= ', '.PREFIX_TABLE.'sites AS b'; + $infos = array( 'nb_images','id_uppercat','comment','site_id','galleries_url' + ,'dir','date_last','uploadable','status','visible' + ,'representative_picture_id','uppercats' ); + + $query = 'SELECT '.implode( ',', $infos ); + $query.= ' FROM '.CATEGORIES_TABLE.' AS a'; + $query.= ', '.SITES_TABLE.' AS b'; $query.= ' WHERE a.id = '.$id; - $query.= ' AND a.site_id = b.id;'; + $query.= ' AND a.site_id = b.id'; + $query.= ';'; $row = mysql_fetch_array( mysql_query( $query ) ); - $cat['site_id'] = $row['site_id']; - $cat['id_uppercat'] = $row['id_uppercat']; - $cat['comment'] = nl2br( $row['comment'] ); - $cat['nb_images'] = $row['nb_images']; - $cat['dir'] = $row['dir']; - $cat['date_last'] = $row['date_last']; - $cat['uploadable'] = get_boolean( $row['uploadable'] ); - $cat['status'] = $row['status']; - $cat['visible'] = get_boolean( $row['visible'] ); - $cat['representative_picture_id'] = $row['representative_picture_id']; - $cat['name'] = array(); + $cat = array(); + // affectation of each field of the table "config" to an information of the + // array $cat. + foreach ( $infos as $info ) { + if ( isset( $row[$info] ) ) $cat[$info] = $row[$info]; + else $cat[$info] = ''; + // If the field is true or false, the variable is transformed into a + // boolean value. + if ( $cat[$info] == 'true' or $cat[$info] == 'false' ) + { + $cat[$info] = get_boolean( $cat[$info] ); + } + } + $cat['comment'] = nl2br( $cat['comment'] ); - if ( !isset( $page['plain_structure'] ) ) - $page['plain_structure'] = get_plain_structure(); + $cat['name'] = array(); - array_push( $cat['name'], $page['plain_structure'][$id]['name'] ); - while ( $page['plain_structure'][$id]['id_uppercat'] != '' ) + $query = 'SELECT name FROM '.CATEGORIES_TABLE; + $query.= ' WHERE id IN ('.$cat['uppercats'].')'; + $query.= ' ORDER BY id ASC'; + $query.= ';'; + $result = mysql_query( $query ); + while( $row = mysql_fetch_array( $result ) ) { - $id = $page['plain_structure'][$id]['id_uppercat']; - array_push( $cat['name'], $page['plain_structure'][$id]['name'] ); + array_push( $cat['name'], $row['name'] ); } + return $cat; } @@ -337,17 +315,38 @@ function get_local_dir( $category_id ) { global $page; - if ( !isset( $page['plain_structure'] ) ) - $page['plain_structure'] = get_plain_structure(); - - // creating the local path : "root_cat/sub_cat/sub_sub_cat/" - $dir = $page['plain_structure'][$category_id]['dir'].'/'; - while ( $page['plain_structure'][$category_id]['id_uppercat'] != '' ) + $uppercats = ''; + $local_dir = ''; + + if ( isset( $page['plain_structure'][$category_id]['uppercats'] ) ) + { + $uppercats = $page['plain_structure'][$category_id]['uppercats']; + } + else + { + $query = 'SELECT uppercats'; + $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id = '.$category_id; + $query.= ';'; + $row = mysql_fetch_array( mysql_query( $query ) ); + $uppercats = $row['uppercats']; + } + + $upper_array = explode( ',', $uppercats ); + + $database_dirs = array(); + $query = 'SELECT id,dir'; + $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id IN ('.$uppercats.')'; + $query.= ';'; + $result = mysql_query( $query ); + while( $row = mysql_fetch_array( $result ) ) { - $category_id = $page['plain_structure'][$category_id]['id_uppercat']; - $dir = $page['plain_structure'][$category_id]['dir'].'/'.$dir; + $database_dirs[$row['id']] = $row['dir']; + } + foreach ( $upper_array as $id ) { + $local_dir.= $database_dirs[$id].'/'; } - return $dir; + + return $local_dir; } // retrieving the site url : "http://domain.com/gallery/" or @@ -356,12 +355,10 @@ function get_site_url( $category_id ) { global $page; - if ( !isset( $page['plain_structure'] ) ) - $page['plain_structure'] = get_plain_structure(); - $query = 'SELECT galleries_url'; - $query.= ' FROM '.PREFIX_TABLE.'sites'; - $query.= ' WHERE id = '.$page['plain_structure'][$category_id]['site_id']; + $query.= ' FROM '.SITES_TABLE.' AS s,'.CATEGORIES_TABLE.' AS c'; + $query.= ' WHERE s.id = c.site_id'; + $query.= ' AND c.id = '.$category_id; $query.= ';'; $row = mysql_fetch_array( mysql_query( $query ) ); return $row['galleries_url']; @@ -374,33 +371,19 @@ function get_site_url( $category_id ) // - $separation : the string between each category name " - " for example // - $style : the style of the span tag for the lowest category, // "font-style:italic;" for example -function get_cat_display_name( $array_cat_names, $separation, $style ) +function get_cat_display_name( $array_cat_names, $separation, + $style, $replace_space = true ) { - $output = ""; - for ( $i = sizeof( $array_cat_names ) - 1; $i >= 0; $i-- ) - { - if ( $i != sizeof( $array_cat_names ) - 1 ) - { - $output.= $separation; - } - if ( $i != 0 ) - { - $output.= $array_cat_names[$i]; - } + $output = ''; + foreach ( $array_cat_names as $i => $name ) { + if ( $i > 0 ) $output.= $separation; + if ( $i < count( $array_cat_names ) - 1 or $style == '') + $output.= $name; else - { - if ( $style != "" ) - { - $output.= '<span style="'.$style.'">'; - } - $output.= $array_cat_names[$i]; - if ( $style != "" ) - { - $output.= "</span>"; - } - } + $output.= '<span style="'.$style.'">'.$name.'</span>'; } - return replace_space( $output ); + if ( $replace_space ) return replace_space( $output ); + else return $output; } // initialize_category initializes ;-) the variables in relation @@ -418,6 +401,7 @@ function get_cat_display_name( $array_cat_names, $separation, $style ) // 4. creation of the navigation bar function initialize_category( $calling_page = 'category' ) { + pwg_debug( 'start initialize_category' ); global $page,$lang,$user,$conf; if ( isset( $page['cat'] ) ) @@ -426,7 +410,8 @@ function initialize_category( $calling_page = 'category' ) // By default, it is the same as the $user['nb_image_page'] $page['nb_image_page'] = $user['nb_image_page']; // $url is used to create the navigation bar - $url = './category.php?cat='.$page['cat'].'&expand='.$page['expand']; + $url = './category.php?cat='.$page['cat']; + if ( isset($page['expand']) ) $url.= '&expand='.$page['expand']; // simple category if ( is_numeric( $page['cat'] ) ) { @@ -437,7 +422,8 @@ function initialize_category( $calling_page = 'category' ) $page['cat_nb_images'] = $result['nb_images']; $page['cat_site_id'] = $result['site_id']; $page['cat_uploadable'] = $result['uploadable']; - $page['title'] = get_cat_display_name( $page['cat_name'], ' - ', '' ); + $page['uppercats'] = $result['uppercats']; + $page['title'] = get_cat_display_name( $page['cat_name'],' - ','',false); $page['where'] = ' WHERE category_id = '.$page['cat']; } else @@ -446,15 +432,10 @@ function initialize_category( $calling_page = 'category' ) or $page['cat'] == 'recent' or $page['cat'] == 'best_rated' ) { // we must not show pictures of a forbidden category - $restricted_cats = get_all_restrictions( $user['id'],$user['status'] ); - if ( count( $restricted_cats ) > 0 ) + if ( $user['forbidden_categories'] != '' ) { - $where_append.= ' AND category_id NOT IN ('; - foreach ( $restricted_cats as $i => $restricted_cat ) { - if ( $i > 0 ) $where_append.= ','; - $where_append.= $restricted_cat; - } - $where_append.= ')'; + $forbidden = ' category_id NOT IN '; + $forbidden.= '('.$user['forbidden_categories'].')'; } } // search result @@ -499,11 +480,11 @@ function initialize_category( $calling_page = 'category' ) } } $page['where'].= ' )'; - $page['where'].= $where_append; + if ( isset( $forbidden ) ) $page['where'].= ' AND '.$forbidden; $query = 'SELECT COUNT(DISTINCT(id)) AS nb_total_images'; - $query.= ' FROM '.PREFIX_TABLE.'images'; - $query.= ' LEFT JOIN '.PREFIX_TABLE.'image_category AS ic'; + $query.= ' FROM '.IMAGES_TABLE; + $query.= ' INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic'; $query.= ' ON id = ic.image_id'; $query.= $page['where']; $query.= ';'; @@ -515,12 +496,12 @@ function initialize_category( $calling_page = 'category' ) { $page['title'] = $lang['favorites']; - $page['where'] = ', '.PREFIX_TABLE.'favorites AS fav'; + $page['where'] = ', '.FAVORITES_TABLE.' AS fav'; $page['where'].= ' WHERE user_id = '.$user['id']; $page['where'].= ' AND fav.image_id = id'; $query = 'SELECT COUNT(*) AS nb_total_images'; - $query.= ' FROM '.PREFIX_TABLE.'favorites'; + $query.= ' FROM '.FAVORITES_TABLE; $query.= ' WHERE user_id = '.$user['id']; $query.= ';'; } @@ -533,11 +514,11 @@ function initialize_category( $calling_page = 'category' ) $date = time() - 60*60*24*$user['short_period']; $page['where'] = " WHERE date_available > '"; $page['where'].= date( 'Y-m-d', $date )."'"; - $page['where'].= $where_append; + if ( isset( $forbidden ) ) $page['where'].= ' AND '.$forbidden; $query = 'SELECT COUNT(DISTINCT(id)) AS nb_total_images'; - $query.= ' FROM '.PREFIX_TABLE.'images'; - $query.= ' LEFT JOIN '.PREFIX_TABLE.'image_category AS ic'; + $query.= ' FROM '.IMAGES_TABLE; + $query.= ' INNER JOIN '.PREFIX_TABLE.'image_category AS ic'; $query.= ' ON id = ic.image_id'; $query.= $page['where']; $query.= ';'; @@ -546,16 +527,19 @@ function initialize_category( $calling_page = 'category' ) else if ( $page['cat'] == 'most_visited' ) { $page['title'] = $conf['top_number'].' '.$lang['most_visited_cat']; - $page['where'] = ' WHERE category_id != -1'.$where_append; + + if ( isset( $forbidden ) ) $page['where'] = ' WHERE '.$forbidden; + else $page['where'] = ''; $conf['order_by'] = ' ORDER BY hit DESC, file ASC'; $page['cat_nb_images'] = $conf['top_number']; - if ( $page['start'] + $user['nb_image_page'] >= $conf['top_number'] ) + if ( isset( $page['start'] ) + and ($page['start']+$user['nb_image_page']>=$conf['top_number'])) { $page['nb_image_page'] = $conf['top_number'] - $page['start']; } } - if ( $query != '' ) + if ( isset($query)) { $result = mysql_query( $query ); $row = mysql_fetch_array( $result ); @@ -573,6 +557,7 @@ function initialize_category( $calling_page = 'category' ) { $page['title'] = $lang['diapo_default_page_title']; } + pwg_debug( 'end initialize_category' ); } // get_non_empty_subcat_ids returns an array with sub-categories id @@ -599,13 +584,14 @@ function get_non_empty_subcat_ids( $id_uppercat ) $ids = array(); $query = 'SELECT id,nb_images'; - $query.= ' FROM '.PREFIX_TABLE.'categories'; + $query.= ' FROM '.CATEGORIES_TABLE; $query.= ' WHERE id_uppercat '; if ( !is_numeric( $id_uppercat ) ) $query.= 'is NULL'; else $query.= '= '.$id_uppercat; // we must not show pictures of a forbidden category - foreach ( $user['restrictions'] as $restricted_cat ) { - $query.= ' AND id != '.$restricted_cat; + if ( $user['forbidden_categories'] != '' ) + { + $query.= ' AND id NOT IN ('.$user['forbidden_categories'].')'; } $query.= ' ORDER BY rank'; $query.= ';'; @@ -632,11 +618,12 @@ function get_first_non_empty_cat_id( $id_uppercat ) global $user; $query = 'SELECT id,nb_images'; - $query.= ' FROM '.PREFIX_TABLE.'categories'; + $query.= ' FROM '.CATEGORIES_TABLE; $query.= ' WHERE id_uppercat = '.$id_uppercat; // we must not show pictures of a forbidden category - foreach ( $user['restrictions'] as $restricted_cat ) { - $query.= ' AND id != '.$restricted_cat; + if ( $user['forbidden_categories'] != '' ) + { + $query.= ' AND id NOT IN ('.$user['forbidden_categories'].')'; } $query.= ' ORDER BY RAND()'; $query.= ';'; |