From d62ce7376079a7435317fc843eb04f3927e4adad Mon Sep 17 00:00:00 2001 From: rvelices Date: Mon, 20 Feb 2006 04:39:37 +0000 Subject: calendar redesign: step 1 git-svn-id: http://piwigo.org/svn/trunk@1047 68402e56-0260-453c-a942-63ccdbb3a9ee --- category.php | 63 ++++-- include/category_default.inc.php | 12 +- include/functions_calendar.inc.php | 437 +++++++++++++++++++++++++++++++++++++ include/section_init.inc.php | 14 +- template/yoga/category.tpl | 7 + 5 files changed, 506 insertions(+), 27 deletions(-) create mode 100644 include/functions_calendar.inc.php diff --git a/category.php b/category.php index 0f0a370b5..d8e93d1d5 100644 --- a/category.php +++ b/category.php @@ -77,25 +77,37 @@ if (isset($page['cat']) and is_numeric($page['cat'])) check_restrictions($page['cat']); } -if (isset($page['cat']) +if ( isset($page['cat_nb_images']) and $page['cat_nb_images'] > $user['nb_image_page']) { // $nav_url is used to create the navigation bar - $nav_url = PHPWG_ROOT_PATH.'category.php?cat='.$page['cat']; + $nav_url = PHPWG_ROOT_PATH.'category.php?'; + if ( isset($page['cat']) ) + { + $nav_url .= 'cat='.$page['cat'].'&'; + } switch ($page['cat']) { case 'search': { - $nav_url.= '&search='.$_GET['search']; + $nav_url.= 'search='.$_GET['search'].'&'; break; } case 'list': { - $nav_url.= '&list='.$_GET['list']; + $nav_url.= 'list='.$_GET['list'].'&'; break; } } + if ( isset($_GET['calendar']) ) + { + $nav_url.= 'calendar='.$_GET['calendar']; + } + else + { + $nav_url = preg_replace('/&$/', '', $nav_url); + } $page['navigation_bar'] = create_navigation_bar( $nav_url, @@ -144,6 +156,13 @@ if ( isset( $page['cat_nb_images'] ) and $page['cat_nb_images'] > 0 ) $icon_recent = get_icon(date('Y-m-d')); +$calendar_view_link = PHPWG_ROOT_PATH.'category.php' + .get_query_string_diff(array('start','calendar')); +if ( ! isset($_GET['calendar']) ) +{ + $calendar_view_link .= (empty($_GET)? '?':'&' ) . 'calendar='; +} + $template->assign_vars( array( 'NB_PICTURE' => count_user_total_images(), @@ -180,7 +199,8 @@ $template->assign_vars( 'U_LOST_PASSWORD' => PHPWG_ROOT_PATH.'password.php', 'U_LOGOUT' => PHPWG_ROOT_PATH.'category.php?act=logout', 'U_ADMIN'=> PHPWG_ROOT_PATH.'admin.php', - 'U_PROFILE'=> PHPWG_ROOT_PATH.'profile.php' + 'U_PROFILE'=> PHPWG_ROOT_PATH.'profile.php', + 'U_CALENDAR' => $calendar_view_link ) ); @@ -352,8 +372,24 @@ if (isset($page['cat']) } //------------------------------------------------------ main part : thumbnails -include(PHPWG_ROOT_PATH.$page['thumbnails_include']); +if ( isset($page['thumbnails_include']) ) +{ + include(PHPWG_ROOT_PATH.$page['thumbnails_include']); +} //------------------------------------------------------- category informations +if ( $page['navigation_bar'] != '' + or ( isset( $page['comment'] ) and $page['comment'] != '' ) ) +{ + $template->assign_block_vars('cat_infos',array()); +} +// navigation bar +if ( $page['navigation_bar'] != '' ) +{ + $template->assign_block_vars( + 'cat_infos.navigation', + array('NAV_BAR' => $page['navigation_bar']) + ); +} if ( isset ( $page['cat'] ) ) { // upload a picture in the category @@ -368,21 +404,6 @@ if ( isset ( $page['cat'] ) ) array('U_UPLOAD'=> $url ) ); } - - if ( $page['navigation_bar'] != '' - or ( isset( $page['comment'] ) and $page['comment'] != '' ) ) - { - $template->assign_block_vars('cat_infos',array()); - } - - // navigation bar - if ( $page['navigation_bar'] != '' ) - { - $template->assign_block_vars( - 'cat_infos.navigation', - array('NAV_BAR' => $page['navigation_bar']) - ); - } // category comment if ( isset( $page['comment'] ) and $page['comment'] != '' ) { diff --git a/include/category_default.inc.php b/include/category_default.inc.php index 009d9ef14..558e3d389 100644 --- a/include/category_default.inc.php +++ b/include/category_default.inc.php @@ -80,8 +80,12 @@ foreach ($pictures as $row) $thumbnail_title .= ' : '.$row['filesize'].' KB'; } // url link on picture.php page - $url_link = PHPWG_ROOT_PATH.'picture.php?cat='.$page['cat']; - $url_link.= '&image_id='.$row['id']; + $url_link = PHPWG_ROOT_PATH.'picture.php?'; + if ( isset($page['cat']) ) + { + $url_link .= 'cat='.$page['cat'].'&'; + } + $url_link.= 'image_id='.$row['id']; if ($page['cat'] == 'search') { $url_link.= '&search='.$_GET['search']; @@ -90,6 +94,10 @@ foreach ($pictures as $row) { $url_link.= '&list='.$_GET['list']; } + if ( isset($_GET['calendar']) ) + { + $url_link.= '&calendar='.$_GET['calendar']; + } $template->assign_block_vars( 'thumbnails.line.thumbnail', diff --git a/include/functions_calendar.inc.php b/include/functions_calendar.inc.php new file mode 100644 index 000000000..9260bbbed --- /dev/null +++ b/include/functions_calendar.inc.php @@ -0,0 +1,437 @@ +sql = $sql; + $this->allow_any = $allow_any; + $this->labels = $labels; + } + + function sql() + { + return $this->sql; + } + function sql_equal($item) + { + return $this->sql.'='.$item; + } + function allow_any() + { + return $this->allow_any; + } + function get_label($item) + { + if ( isset($this->labels[$item]) ) + { + return $this->labels[$item]; + } + return $item; + } + + var $sql; + var $allow_any; + var $labels; +} + +class YearMonthCalendarLevel extends BaseCalendarLevel +{ + function YearMonthCalendarLevel() + { + global $conf; + parent::BaseCalendarLevel('DATE_FORMAT('.$conf['calendar_datefield'].',"%Y%m")', false); + } + + function sql_equal($item) + { + global $conf; + $y = (int)($item/100); + $m = (int)$item%100; + // There seems to be much difference in performance between these: + return $conf['calendar_datefield']." BETWEEN '$y-$m-01' AND '$y-$m-31'"; +/* return '(YEAR('.$conf['calendar_datefield'].')='.$y.' + AND MONTH('.$conf['calendar_datefield'].')='.$m.')';*/ +// return parent::sql_equal($item); + } + + function get_label($item) + { + global $lang; + if ( preg_match( '/(\d{4})(\d{2})/', $item, $matches) ) + { + return $lang['month'][(int)$matches[2]].' '.$matches[1]; + } + return $item; + } +} + +// just to optimize MySql query so that it uses the index +class YearCalendarLevel extends BaseCalendarLevel +{ + function YearCalendarLevel($sql, $allow_any=true) + { + parent::BaseCalendarLevel($sql, $allow_any); + } + + function sql_equal($item) + { + global $conf; + return $conf['calendar_datefield']." BETWEEN '$item-01-01' AND '$item-12-31'"; + } +} + +/** + * Parses $param and returns an array of calendar levels + * @param requested array of requested items for each calendar level + * @param cal_type is the requested calendar type + */ +function get_calendar_params($param, &$requested, &$cal_type) +{ + global $conf, $lang; + $requested = explode('-', $param); + $cal_struct = array(); + if ($requested[0]=='ywd') + { + array_push($cal_struct, new YearCalendarLevel( + 'YEAR('.$conf['calendar_datefield'].')' ) ); + array_push($cal_struct, new BaseCalendarLevel( + 'WEEK('.$conf['calendar_datefield'].')+1' ) ); + array_push($cal_struct, new BaseCalendarLevel( + 'DAYOFWEEK('.$conf['calendar_datefield'].')-1', true, $lang['day'] ) ); + $cal_type=array_shift($requested); + } + else if ($requested[0]=='md') + { + array_push($cal_struct, new YearMonthCalendarLevel() ); + array_push($cal_struct, new BaseCalendarLevel( + 'DAY('.$conf['calendar_datefield'].')' ) ); + $cal_type=array_shift($requested); + } + else + { + array_push($cal_struct, new YearCalendarLevel( + 'YEAR('.$conf['calendar_datefield'].')' ) ); + array_push($cal_struct, new BaseCalendarLevel( + 'MONTH('.$conf['calendar_datefield'].')', true, $lang['month'] ) ); + array_push($cal_struct, new BaseCalendarLevel( + 'DAY('.$conf['calendar_datefield'].')' ) ); + + if ($requested[0]=='ymd') + { + $cal_type=array_shift($requested); + } + else + { + $cal_type=''; + } + } + + // perform a sanity check on $requested + while (count($requested)>count($cal_struct)) + { + array_pop($requested); + } + + $any_count = 0; + for ($i=0; $iallow_any() ) + { + while ($i'. var_export($cal_struct, true) . ''); + //echo ('
'. var_export($requested, true) . '
'); + + $category_calling = false; + if (basename($_SERVER["PHP_SELF"]) == 'category.php') + { + $category_calling = true; + } + + if ($category_calling) + { + $template->assign_block_vars('calendar', array()); + + $url_base = get_query_string_diff(array('start','calendar')); + $url_base .= empty($url_base) ? '?' : '&'; + $url_base .= 'calendar='; + $url_base = PHPWG_ROOT_PATH.'category.php'.$url_base; + + // Build navigation bar for calendar styles + $nav_bar = 'Styles: '; + foreach ( array('ymd','md','ywd') as $type) + { + if ( $type==$cal_type or ($cal_type=='' and $type=='ymd') ) + { + $nav_bar .= $type.' '; + } + else + { + $nav_bar .= ''.$type.' '; + } + } + $template->assign_block_vars( 'calendar.navbar', + array( 'BAR' => $nav_bar) + ); + + $url_base .= $cal_type; + if ($cal_type!='') + { + $url_base .= '-'; + } + + + $prev_level_query=' +AND '.$conf['calendar_datefield'].' IS NOT NULL '; + + for ($i=0; $isql().') AS period, COUNT(id) as count +FROM '.IMAGES_TABLE; + if ( isset($category_restriction) ) + { + $query.= ' +INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id +WHERE category_id' . $category_restriction; + } + else + { + $query.= ' +WHERE id IN (' . implode(',',$page['items']) .')'; + } + $query.= $prev_level_query; + $query.= ' +GROUP BY period'; + + $level_items=array(); + $result = pwg_query($query); + $total_pics = 0; + while ($row = mysql_fetch_array($result)) + { + $level_items[$row['period']] = (int)$row['count']; + $total_pics += $row['count']; + } + //echo ('
'. var_export($level_items, true) . '
'); + + if ( $requested[$i] == 'any' and ! $crt_cal_level->allow_any() ) + { + unset($requested[$i]); + } + + // --- Build the navigation bar + if ( $crt_cal_level->allow_any() ) + { + $level_items['any'] = $total_pics; + } + $nav_bar=''; + foreach ($level_items as $item => $nb_images) + { + $label = $crt_cal_level->get_label($item); + if ( $item==$requested[$i] ) + { + $nav_bar .= ' '; + $nav_bar .= $label; + $nav_bar.= ''; + } + else + { + $url = $url_base . $item; + $nav_bar .= ''; + $nav_bar .= $label; + $nav_bar .= ''; + } + $nav_bar .= ' '; + } + $template->assign_block_vars( 'calendar.navbar', + array( 'BAR' => $nav_bar) + ); + + if ( !isset($requested[$i]) ) + break; + if ($requested[$i]!='any') + { + $prev_level_query.= ' AND '.$crt_cal_level->sql_equal($requested[$i]); + } + $url_base .= $requested[$i].'-'; + } // end for each calendar level + + + if ( $i < count($cal_struct) ) + { + $template->assign_block_vars('thumbnails', array()); + $template->assign_block_vars('thumbnails.line', array()); + foreach ($level_items as $level_item => $nb_pics) + { + if ($level_item=='any') + continue; + $query = ' +SELECT file,tn_ext,'.$conf['calendar_datefield'].',path +FROM '.IMAGES_TABLE; + if ( isset($category_restriction) ) + { + $query.= ' +INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id +WHERE category_id' . $category_restriction; + } + else + { + $query.= ' +WHERE id IN (' . implode(',',$page['items']) .')'; + } + $query.= $prev_level_query; + $query.= ' AND '.$crt_cal_level->sql_equal($level_item); + $query.= ' +ORDER BY RAND() +LIMIT 0,1'; + $row = mysql_fetch_array(pwg_query($query)); + + $thumbnail_src = get_thumbnail_src($row['path'], @$row['tn_ext']); + $thumbnail_title = $crt_cal_level->get_label($level_item); + $name = $thumbnail_title .' ('.$nb_pics.')'; + + $template->assign_block_vars( + 'thumbnails.line.thumbnail', + array( + 'IMAGE'=>$thumbnail_src, + 'IMAGE_ALT'=>$row['file'], + 'IMAGE_TITLE'=>$thumbnail_title, + 'U_IMG_LINK'=>$url_base.$level_item + ) + ); + $template->assign_block_vars( + 'thumbnails.line.thumbnail.category_name', + array( + 'NAME' => $name + ) + ); + } + unset( $page['thumbnails_include'] ); // maybe move everything to a new include file ? + pwg_debug('end initialize_calendar for thumbs'); + return; + } + } + + if (!$category_calling or $i==count($cal_struct) ) + { + $query = 'SELECT DISTINCT(id) FROM '.IMAGES_TABLE; + if ( isset($category_restriction) ) + { + $query.= ' +INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id +WHERE category_id' . $category_restriction; + } + else + { + $query.= ' +WHERE id IN ('.implode(',',$page['items']).')'; + } + $query.= ' +AND '.$conf['calendar_datefield'].' IS NOT NULL '; + + for ($i=0; $isql_equal($requested[$i]); + } + + } + + $page['items'] = array_from_query($query, 'id'); + $page['cat_nb_images'] = count($page['items']); + $page['thumbnails_include'] = 'include/category_default.inc.php'; + } + pwg_debug('end initialize_calendar'); +} + +?> \ No newline at end of file diff --git a/include/section_init.inc.php b/include/section_init.inc.php index f864a288e..d0c9522cd 100644 --- a/include/section_init.inc.php +++ b/include/section_init.inc.php @@ -93,13 +93,12 @@ if (isset($_GET['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']; 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']; - if ($page['cat'] != 'most_visited' and $page['cat'] != 'best_rated') { if (isset($_COOKIE['pwg_image_order']) @@ -397,4 +396,11 @@ else $page['title'] = $lang['no_category']; $page['thumbnails_include'] = 'include/category_subcats.inc.php'; } + +if ( isset($_GET['calendar']) ) +{ + include_once( PHPWG_ROOT_PATH.'include/functions_calendar.inc.php' ); + initialize_calendar(); +} + ?> \ No newline at end of file diff --git a/template/yoga/category.tpl b/template/yoga/category.tpl index 39056d720..906db4dfa 100644 --- a/template/yoga/category.tpl +++ b/template/yoga/category.tpl @@ -130,6 +130,7 @@ + Toggle calendar

{TITLE}

@@ -141,6 +142,12 @@ + + + + + +
    -- cgit v1.2.3