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 --- include/functions_calendar.inc.php | 437 +++++++++++++++++++++++++++++++++++++ 1 file changed, 437 insertions(+) create mode 100644 include/functions_calendar.inc.php (limited to 'include/functions_calendar.inc.php') 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 -- cgit v1.2.3