diff options
Diffstat (limited to 'include/functions_calendar.inc.php')
-rw-r--r-- | include/functions_calendar.inc.php | 457 |
1 files changed, 131 insertions, 326 deletions
diff --git a/include/functions_calendar.inc.php b/include/functions_calendar.inc.php index 9260bbbed..a5a1e87bb 100644 --- a/include/functions_calendar.inc.php +++ b/include/functions_calendar.inc.php @@ -5,9 +5,9 @@ // +-----------------------------------------------------------------------+
// | branch : BSF (Best So Far)
// | file : $RCSfile$
-// | last update : $Date: 2006-02-12 16:52:16 -0500 (Sun, 12 Feb 2006) $
-// | last modifier : $Author: plg $
-// | revision : $Revision: 1036 $
+// | last update : $Date: 2006-01-27 02:11:43 +0100 (ven, 27 jan 2006) $
+// | last modifier : $Author: rvelices $
+// | revision : $Revision: 1014 $
// +-----------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or modify |
// | it under the terms of the GNU General Public License as published by |
@@ -24,146 +24,107 @@ // | USA. |
// +-----------------------------------------------------------------------+
-class BaseCalendarLevel
-{
- function BaseCalendarLevel($sql, $allow_any=true, $labels=null)
- {
- $this->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;
- }
+define('CAL_VIEW_LIST','l');
+define('CAL_VIEW_CALENDAR','c');
- var $sql;
- var $allow_any;
- var $labels;
-}
-
-class YearMonthCalendarLevel extends BaseCalendarLevel
+function initialize_calendar()
{
- function YearMonthCalendarLevel()
- {
- global $conf;
- parent::BaseCalendarLevel('DATE_FORMAT('.$conf['calendar_datefield'].',"%Y%m")', false);
- }
+ global $page, $conf, $user, $template;
- 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);
+//------------------ initialize the condition on items to take into account ---
+ $inner_sql = ' FROM ' . IMAGES_TABLE;
+ if ( !isset($page['cat']) or is_numeric($page['cat']) )
+ { // we will regenerate the items by including subcats elements
+ $page['cat_nb_images']=0;
+ $page['items']=array();
+ $inner_sql .= '
+INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id';
+ if ( is_numeric($page['cat']) )
+ {
+ $sub_ids = get_subcat_ids(array($page['cat']));
+ $sub_ids = array_diff($sub_ids,
+ explode(',', $user['forbidden_categories']) );
+ if (empty($sub_ids))
+ {
+ return; // nothing to do
+ }
+ $inner_sql .= '
+WHERE category_id IN ('.implode(',',$sub_ids).')';
+ }
+ else
+ {
+ $inner_sql .= '
+WHERE category_id NOT IN ('.$user['forbidden_categories'].')';
+ }
}
-
- function get_label($item)
+ else
{
- global $lang;
- if ( preg_match( '/(\d{4})(\d{2})/', $item, $matches) )
+ if ( empty($page['items']) )
{
- return $lang['month'][(int)$matches[2]].' '.$matches[1];
+ return; // nothing to do
}
- return $item;
+ $inner_sql .= '
+WHERE id IN (' . implode(',',$page['items']) .')';
}
-}
-// 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);
+//-------------------------------------- initialize the calendar parameters ---
+ pwg_debug('start initialize_calendar');
+ $cal_styles = array(
+ array('link'=>'m', 'default_link'=>'', 'name'=>l10n('Monthly'),
+ 'include'=>'calendar_monthly.class.php', 'view_calendar'=>true ),
+ array('link'=>'w', 'default_link'=>'w-', 'name'=>l10n('Weekly'),
+ 'include'=>'calendar_weekly.class.php', ),
+ );
+
+ $requested = explode('-', $_GET['calendar']);
+ $calendar = null;
+ foreach( $cal_styles as $cal_style)
+ {
+ if ($requested[0]==$cal_style['link'])
+ {
+ include( PHPWG_ROOT_PATH.'include/'.$cal_style['include']);
+ $calendar = new Calendar();
+ array_shift($requested);
+ break;
+ }
}
-
- function sql_equal($item)
+ if ( !isset($calendar) )
{
- global $conf;
- return $conf['calendar_datefield']." BETWEEN '$item-01-01' AND '$item-12-31'";
+ foreach( $cal_styles as $cal_style)
+ {
+ if (''==$cal_style['default_link'])
+ break;
+ }
+ include( PHPWG_ROOT_PATH.'include/'.$cal_style['include']);
+ $calendar = new Calendar();
}
-}
-/**
- * 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')
+ $view_type=CAL_VIEW_LIST;
+ if ($requested[0]==CAL_VIEW_LIST)
{
- array_push($cal_struct, new YearMonthCalendarLevel() );
- array_push($cal_struct, new BaseCalendarLevel(
- 'DAY('.$conf['calendar_datefield'].')' ) );
- $cal_type=array_shift($requested);
+ array_shift($requested);
}
- else
+ elseif ($requested[0]==CAL_VIEW_CALENDAR)
{
- 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
+ if ($cal_style['view_calendar'])
{
- $cal_type='';
+ $view_type=CAL_VIEW_CALENDAR;
}
+ array_shift($requested);
}
-
// perform a sanity check on $requested
- while (count($requested)>count($cal_struct))
+ while (count($requested)>3)
{
array_pop($requested);
}
-
+
$any_count = 0;
for ($i=0; $i<count($requested); $i++)
{
if ($requested[$i]=='any')
{
- if (! $cal_struct[$i]->allow_any() )
- {
+ if ($view_type==CAL_VIEW_CALENDAR)
+ {// we dont allow any in calendar view
while ($i<count($requested))
{
array_pop( $requested );
@@ -172,7 +133,7 @@ function get_calendar_params($param, &$requested, &$cal_type) }
$any_count++;
}
- elseif ( empty($requested[$i]) )
+ elseif ( $requested[$i]=='' )
{
while ($i<count($requested))
{
@@ -180,60 +141,22 @@ function get_calendar_params($param, &$requested, &$cal_type) }
}
}
- if ($any_count==count($cal_struct))
+ if ($any_count==3)
{
array_pop($requested);
}
- return $cal_struct;
-}
-
-
-
-function initialize_calendar()
-{
- global $page, $conf, $user, $template;
-
- if ( !isset($page['cat']) or is_numeric($page['cat']) )
- { // we will regenerate the items by including subcats elements
- $page['cat_nb_images']=0;
- $page['items']=array();
- if ( is_numeric($page['cat']) )
- {
- $sub_ids = get_subcat_ids(array($page['cat']));
- $sub_ids = array_diff($sub_ids,
- explode(',', $user['forbidden_categories']) );
- if (empty($sub_ids))
- {
- return; // nothing to do
- }
- $category_restriction .= ' IN ('.implode(',',$sub_ids).')';
- }
- else
- {
- $category_restriction = ' NOT IN ('.$user['forbidden_categories'].')';
- }
- }
- else
- {
- if ( empty($page['items']) )
- {
- return; // nothing to do
- }
- }
-
- pwg_debug('start initialize_calendar');
-
- $cal_struct = get_calendar_params($_GET['calendar'], $requested, $cal_type);
- //echo ('<pre>'. var_export($cal_struct, true) . '</pre>');
+ $calendar->initialize($conf['calendar_datefield'], $inner_sql);
//echo ('<pre>'. var_export($requested, true) . '</pre>');
-
+ //echo ('<pre>'. var_export($calendar, true) . '</pre>');
+
$category_calling = false;
if (basename($_SERVER["PHP_SELF"]) == 'category.php')
{
$category_calling = true;
}
-
+
+ $must_show_list = true;
if ($category_calling)
{
$template->assign_block_vars('calendar', array());
@@ -242,191 +165,73 @@ function initialize_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 .= '<a href="'. $url_base.$type . '">'.$type.'</a> ';
- }
- }
- $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; $i<count($cal_struct); $i++)
+ if ( $calendar->generate_category_content(
+ $url_base.$cal_style['default_link'], $view_type, $requested) )
{
- $crt_cal_level = $cal_struct[$i];
- $query = '
-SELECT DISTINCT('.$crt_cal_level->sql().') 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 ('<pre>'. var_export($level_items, true) . '</pre>');
+ unset( $page['thumbnails_include'] );
+ unset( $page['items'] );
+ unset( $page['cat_nb_images'] );
+ $must_show_list = false;
+ }
- 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)
+ if ($cal_style['view_calendar'])
+ { // Build bar for view modes (List/Calendar)
+ $views = array(
+ array(CAL_VIEW_LIST, l10n('List') ),
+ array(CAL_VIEW_CALENDAR, l10n('calendar') ),
+ );
+ $views_bar = '';
+ foreach( $views as $view )
{
- $label = $crt_cal_level->get_label($item);
- if ( $item==$requested[$i] )
+ $v = $view[1];
+ if ( $view_type!=$view[0] )
{
- $nav_bar .= ' <span class="dateSelected">';
- $nav_bar .= $label;
- $nav_bar.= '</span>';
+ $url = $url_base.$cal_style['default_link'].$view[0].'-';
+ $url .= implode('-', $requested);
+ $v = '<a href="'.$url.'">'.$v.'</a> ';
}
else
{
- $url = $url_base . $item;
- $nav_bar .= '<a href="'.$url.'">';
- $nav_bar .= $label;
- $nav_bar .= '</a>';
+ $v = $v.' ';
}
- $nav_bar .= ' ';
+ $views_bar .= $v . ' ';
}
- $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
-
+ $template->assign_block_vars('calendar.views', array(
+ 'BAR'=>$views_bar
+ ));
+ }
- if ( $i < count($cal_struct) )
+ // Build bar for calendar styles (Monthly, Weekly)
+ $styles_bar = '';
+ foreach ( $cal_styles as $style)
{
- $template->assign_block_vars('thumbnails', array());
- $template->assign_block_vars('thumbnails.line', array());
- foreach ($level_items as $level_item => $nb_pics)
+ if ($cal_style['link']!=$style['link'])
{
- if ($level_item=='any')
- continue;
- $query = '
-SELECT file,tn_ext,'.$conf['calendar_datefield'].',path
-FROM '.IMAGES_TABLE;
- if ( isset($category_restriction) )
+ $url = $url_base.$style['default_link'];
+ $url .= $view_type;
+ if (isset($requested[0]))
{
- $query.= '
-INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id
-WHERE category_id' . $category_restriction;
+ $url .= '-' . $requested[0];
}
- 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
- )
- );
+ $styles_bar .= '<a href="'. $url . '">'.$style['name'].'</a> ';
}
- 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; $i<count($cal_struct); $i++)
- {
- assert( isset($requested[$i]) ); // otherwise we should not be here
- if ($requested[$i]!='any')
+ else
{
- $query.= '
-AND '. $cal_struct[$i]->sql_equal($requested[$i]);
+ $styles_bar .= $style['name'].' ';
}
-
}
-
+ $template->assign_block_vars( 'calendar.styles',
+ array( 'BAR' => $styles_bar)
+ );
+ } // end category calling
+
+ if ($must_show_list)
+ {
+ $query = 'SELECT DISTINCT(id)';
+ $query .= $calendar->inner_sql;
+ $query .= $calendar->get_date_where($requested);
+
$page['items'] = array_from_query($query, 'id');
$page['cat_nb_images'] = count($page['items']);
$page['thumbnails_include'] = 'include/category_default.inc.php';
|