improvement: $page['where'] string replaced by $page['items'].

$page['where'] was an SQL clause used to retrieve pictures in #images
table. $page['items'] is the list of picture ids of the current section.

improvement: function initialize_category replaced by dedicated included PHP
script include/section_init.inc.php. Code was refactored to improve
readibility and maintenability. $page['navigation_bar'] is now build in
category.php instead of initialize_category function. Function check_cat_id
was also replaced by a piece of code in the new file. The file to include to
display thumbnails from category.php is now set in section_init.inc.php
instead of calculated in category.php.

bug fix: the test for rel="up" link for standard HTML navigation links in
category menu was not working with non numeric categories, such as
"favorites".

improvement: function check_login_authorization removed because useless but
in profile.php.


git-svn-id: http://piwigo.org/svn/trunk@1036 68402e56-0260-453c-a942-63ccdbb3a9ee
This commit is contained in:
plegall
2006-02-12 21:52:16 +00:00
parent 2dc2eb8630
commit dac7decfb5
12 changed files with 646 additions and 612 deletions
+10 -378
View File
@@ -53,71 +53,6 @@ function check_restrictions($category_id)
}
}
/**
* Checks whether the argument is a right parameter category id
*
* The argument is a right parameter if corresponds to one of these :
*
* - is numeric and corresponds to a category in the database
* - equals 'fav' (for favorites)
* - equals 'search' (when the result of a search is displayed)
* - equals 'most_visited'
* - equals 'best_rated'
* - equals 'recent_pics'
* - equals 'recent_cats'
* - equals 'calendar'
* - equals 'list'
*
* The function fills the global var $page['cat'] and returns nothing
*
* @param mixed category id or special category name
* @return void
*/
function check_cat_id( $cat )
{
global $page;
unset( $page['cat'] );
if ( isset( $cat ) )
{
if ( isset( $page['plain_structure'][$cat] ) )
{
$page['cat'] = $cat;
}
else if ( is_numeric( $cat ) )
{
$query = 'SELECT id';
$query.= ' FROM '.CATEGORIES_TABLE.' WHERE id = '.$cat.';';
$result = pwg_query( $query );
if ( mysql_num_rows( $result ) != 0 )
{
$page['cat'] = $cat;
}
}
if ( $cat == 'fav'
or $cat == 'most_visited'
or $cat == 'best_rated'
or $cat == 'recent_pics'
or $cat == 'recent_cats'
or $cat == 'calendar' )
{
$page['cat'] = $cat;
}
if ($cat == 'search'
and isset($_GET['search'])
and is_numeric($_GET['search']))
{
$page['cat'] = $cat;
}
if ($cat == 'list'
and isset($_GET['list'])
and preg_match('/^\d+(,\d+)*$/', $_GET['list']))
{
$page['cat'] = 'list';
}
}
}
function get_categories_menu()
{
global $page,$user;
@@ -332,319 +267,6 @@ function get_category_preferred_image_orders()
);
}
// initialize_category initializes ;-) the variables in relation
// with category :
// 1. calculation of the number of pictures in the category
// 2. determination of the SQL query part to ask to find the right category
// $page['where'] is not the same if we are in
// - simple category
// - search result
// - favorites displaying
// - most visited pictures
// - best rated pictures
// - recent pictures
// - defined list (used for random)
// 3. determination of the title of the page
// 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'] ) )
{
// $page['nb_image_page'] is the number of picture to display on this page
// 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 = PHPWG_ROOT_PATH.'category.php?cat='.$page['cat'];
if ( isset($page['expand']) ) $url.= '&expand='.$page['expand'];
// simple category
if ( is_numeric( $page['cat'] ) )
{
$result = get_cat_info( $page['cat'] );
$page['comment'] = $result['comment'];
$page['cat_dir'] = $result['dir'];
$page['cat_name'] = $result['name'];
$page['cat_nb_images'] = $result['nb_images'];
$page['cat_site_id'] = $result['site_id'];
$page['cat_uploadable'] = $result['uploadable'];
$page['cat_commentable'] = $result['commentable'];
$page['cat_id_uppercat'] = $result['id_uppercat'];
$page['uppercats'] = $result['uppercats'];
$page['title'] =
get_cat_display_name($page['cat_name'],
'',
false);
$page['where'] = ' WHERE category_id = '.$page['cat'];
}
else
{
if ($page['cat'] == 'search'
or $page['cat'] == 'most_visited'
or $page['cat'] == 'recent_pics'
or $page['cat'] == 'recent_cats'
or $page['cat'] == 'best_rated'
or $page['cat'] == 'calendar'
or $page['cat'] == 'list')
{
// we must not show pictures of a forbidden category
if ( $user['forbidden_categories'] != '' )
{
$forbidden = ' category_id NOT IN ';
$forbidden.= '('.$user['forbidden_categories'].')';
}
}
// search result
if ( $page['cat'] == 'search' )
{
$page['title'] = $lang['search_result'];
if ( $calling_page == 'picture' )
{
$page['title'].= ' : <span style="font-style:italic;">';
$page['title'].= $_GET['search']."</span>";
}
$page['where'] = 'WHERE '.get_sql_search_clause($_GET['search']);
if (isset($forbidden))
{
$page['where'].= "\n AND ".$forbidden;
}
$query = '
SELECT COUNT(DISTINCT(id)) AS nb_total_images
FROM '.IMAGES_TABLE.'
INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
'.$page['where'].'
;';
$url.= '&amp;search='.$_GET['search'];
}
// favorites displaying
else if ( $page['cat'] == 'fav' )
{
check_user_favorites();
$page['title'] = $lang['favorites'];
$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 '.FAVORITES_TABLE;
$query.= ' WHERE user_id = '.$user['id'];
$query.= ';';
}
// pictures within the short period
else if ( $page['cat'] == 'recent_pics' )
{
$page['title'] = $lang['recent_pics_cat'];
// We must find the date corresponding to :
// today - $conf['periode_courte']
$date = time() - 60*60*24*$user['recent_period'];
$page['where'] = " WHERE date_available > '";
$page['where'].= date( 'Y-m-d', $date )."'";
if ( isset( $forbidden ) ) $page['where'].= ' AND '.$forbidden;
$query = '
SELECT COUNT(DISTINCT(id)) AS nb_total_images
FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic
ON id = ic.image_id
'.$page['where'].'
;';
}
// categories containing recent pictures
else if ( $page['cat'] == 'recent_cats' )
{
$page['title'] = $lang['recent_cats_cat'];
$page['cat_nb_images'] = 0;
}
// most visited pictures
else if ( $page['cat'] == 'most_visited' )
{
$page['title'] = $conf['top_number'].' '.$lang['most_visited_cat'];
$page['where'] = 'WHERE hit > 0';
if (isset($forbidden))
{
$page['where'] .= "\n".' AND '.$forbidden;
}
$conf['order_by'] = ' ORDER BY hit DESC, file ASC';
// $page['cat_nb_images'] equals $conf['top_number'] unless there
// are less visited items
$query ='
SELECT COUNT(DISTINCT(id)) AS count
FROM '.IMAGES_TABLE.'
INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
'.$page['where'].'
;';
$row = mysql_fetch_array(pwg_query($query));
if ($row['count'] < $conf['top_number'])
{
$page['cat_nb_images'] = $row['count'];
}
else
{
$page['cat_nb_images'] = $conf['top_number'];
}
unset($query);
if ( isset( $page['start'] )
and ($page['start']+$user['nb_image_page']>=$conf['top_number']))
{
$page['nb_image_page'] = $conf['top_number'] - $page['start'];
}
}
else if ( $page['cat'] == 'calendar' )
{
$page['cat_nb_images'] = 0;
$page['title'] = $lang['calendar'];
if (isset($_GET['year'])
and preg_match('/^\d+$/', $_GET['year']))
{
$page['calendar_year'] = (int)$_GET['year'];
}
if (isset($_GET['month'])
and preg_match('/^(\d+)\.(\d{2})$/', $_GET['month'], $matches))
{
$page['calendar_year'] = (int)$matches[1];
$page['calendar_month'] = (int)$matches[2];
}
if (isset($_GET['day'])
and preg_match('/^(\d+)\.(\d{2})\.(\d{2})$/',
$_GET['day'],
$matches))
{
$page['calendar_year'] = (int)$matches[1];
$page['calendar_month'] = (int)$matches[2];
$page['calendar_day'] = (int)$matches[3];
}
if (isset($page['calendar_year']))
{
$page['title'] .= ' (';
if (isset($page['calendar_day']))
{
if ($page['calendar_year'] >= 1970)
{
$unixdate = mktime(0,0,0,
$page['calendar_month'],
$page['calendar_day'],
$page['calendar_year']);
$page['title'].= $lang['day'][date("w", $unixdate)];
}
$page['title'].= ' '.$page['calendar_day'].', ';
}
if (isset($page['calendar_month']))
{
$page['title'] .= $lang['month'][$page['calendar_month']].' ';
}
$page['title'] .= $page['calendar_year'];
$page['title'] .= ')';
}
$page['where'] = 'WHERE '.$conf['calendar_datefield'].' IS NOT NULL';
if (isset($forbidden))
{
$page['where'].= ' AND '.$forbidden;
}
}
else if ($page['cat'] == 'best_rated')
{
$page['title'] = $conf['top_number'].' '.$lang['best_rated_cat'];
$page['where'] = ' WHERE average_rate IS NOT NULL';
if (isset($forbidden))
{
$page['where'].= ' AND '.$forbidden;
}
$conf['order_by'] = ' ORDER BY average_rate DESC, id ASC';
// $page['cat_nb_images'] equals $conf['top_number'] unless there
// are less rated items
$query ='
SELECT COUNT(DISTINCT(id)) AS count
FROM '.IMAGES_TABLE.'
INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
'.$page['where'].'
;';
$row = mysql_fetch_array(pwg_query($query));
if ($row['count'] < $conf['top_number'])
{
$page['cat_nb_images'] = $row['count'];
}
else
{
$page['cat_nb_images'] = $conf['top_number'];
}
unset($query);
if (isset($page['start'])
and ($page['start']+$user['nb_image_page']>=$conf['top_number']))
{
$page['nb_image_page'] = $conf['top_number'] - $page['start'];
}
}
else if ($page['cat'] == 'list')
{
$page['title'] = $lang['random_cat'];
$page['where'] = 'WHERE 1=1';
if (isset($forbidden))
{
$page['where'].= ' AND '.$forbidden;
}
$page['where'].= ' AND image_id IN ('.$_GET['list'].')';
$page['cat_nb_images'] = count(explode(',', $_GET['list']));
$url.= '&amp;list='.$_GET['list'];
}
if (isset($query))
{
$result = pwg_query( $query );
$row = mysql_fetch_array( $result );
$page['cat_nb_images'] = $row['nb_total_images'];
}
}
if ( $calling_page == 'category' )
{
$page['navigation_bar'] =
create_navigation_bar( $url, $page['cat_nb_images'], $page['start'],
$user['nb_image_page'], 'back' );
}
if ($page['cat'] != 'most_visited' and $page['cat'] != 'best_rated')
{
$available_image_orders = get_category_preferred_image_orders();
$order_idx=0;
if ( isset($_COOKIE['pwg_image_order']) )
{
$order_idx = $_COOKIE['pwg_image_order'];
}
if ( $order_idx > 0 )
{
$order = $available_image_orders[$order_idx][1];
$conf['order_by'] = str_replace('ORDER BY ', 'ORDER BY '.$order.',',
$conf['order_by'] );
}
}
}
else
{
$page['title'] = $lang['no_category'];
}
pwg_debug( 'end initialize_category' );
}
function display_select_categories($categories,
$selecteds,
$blockname,
@@ -735,4 +357,14 @@ function global_rank_compare($a, $b)
{
return strnatcasecmp($a['global_rank'], $b['global_rank']);
}
function rank_compare($a, $b)
{
if ($a['rank'] == $b['rank'])
{
return 0;
}
return ($a['rank'] < $b['rank']) ? -1 : 1;
}
?>