Skip to content
Snippets Groups Projects
Commit bcd56a08 authored by Karen Stevenson's avatar Karen Stevenson
Browse files

Setting better defaults for month, week, and day selectors, more fixes to handling display of weeks

parent e3498370
No related branches found
No related tags found
No related merge requests found
......@@ -317,7 +317,7 @@ function calendar_nav($view, $mini = FALSE) {
return $view->subtitle;
}
if (_calendar_is_valid($view->week, 'week')) {
if ($view->calendar_type == 'week' && _calendar_is_valid($view->week, 'week')) {
$timezone = _views_get_timezone();
$cur_stamp = strtotime('Jan 1, '.$view->year.' + '.intval($view->week).' weeks GMT') - $timezone;
} else {
......@@ -628,32 +628,44 @@ function calendar_views_query_alter(&$query, &$view) {
}
}
// see if this is a week view and reconstruct view info as needed
// the difference between a calendar_month arg and a calendar_week arg is the preceeding 'W'
if (strstr($view->args[1], 'W') && $view->build_type == 'page') {
// either a month or a week argument could occupy the second position of the group
// this is done so that a single view has the capability to switch between all calendar layouts
// to make this work we must make some adjustments to the view
// keep track of the original values so blocks rendered on the same page don't get wrong values
$view->reset_argument = $view->argument;
$view->reset_args = $view->args;
if ($view->build_type == 'page') {
foreach ($view->argument as $delta => $argument) {
if ($argument['type'] == 'calendar_week' || $argument['type'] == 'calendar_month') {
// see if this is a week view and reconstruct view info as needed
// the difference between a calendar_month arg and a calendar_week arg is the preceeding 'W'
if (strstr($view->args[$delta], 'W') && $view->build_type == 'page') {
// keep track of the original values so blocks rendered on the same page don't get wrong values
$view->reset_argument = $view->argument;
$view->reset_args = $view->args;
$view->argument[1]['type'] = 'calendar_week';
$view->argument[1]['id'] = 'calendar_week';
$view->argument[1]['title'] = t('Week');
$view->args[1] = str_replace('W', '', $view->args[1]);
// if there's a day set, unset it
unset($view->args[2]);
unset($view->argument[2]);
}
// if this is not a week argument and view was created with a week argument, change it back
elseif (!strstr($view->args[1], 'W') && $view->build_type == 'page' && $view->argument[1]['type'] == 'calendar_week') {
$view->argument[1]['type'] = 'calendar_month';
$view->argument[1]['id'] = 'calendar_month';
$view->argument[1]['title'] = t('Month');
if ($view->args[2]) {
$view->argument[2]['type'] = 'calendar_day';
$view->argument[2]['id'] = 'calendar_day';
$view->argument[2]['title'] = t('Day');
$view->argument[$delta]['type'] = 'calendar_week';
$view->argument[$delta]['id'] = 'calendar_week';
$view->argument[$delta]['title'] = t('Week');
$view->args[$delta] = str_replace('W', '', $view->args[1]);
// if there's a day set, unset it
unset($view->args[$delta + 1]);
unset($view->argument[$delta + 1]);
}
// if this is not a week argument and view was created with a week argument, change it back
elseif (!strstr($view->args[$delta], 'W') && $view->build_type == 'page' && $view->argument[$delta]['type'] == 'calendar_week') {
$view->argument[$delta]['type'] = 'calendar_month';
$view->argument[$delta]['id'] = 'calendar_month';
$view->argument[$delta]['title'] = t('Month');
if ($view->args[$delta + 1]) {
$view->argument[$delta + 1]['type'] = 'calendar_day';
$view->argument[$delta + 1]['id'] = 'calendar_day';
$view->argument[$delta + 1]['title'] = t('Day');
}
}
}
}
}
......@@ -677,9 +689,11 @@ function calendar_views_query_alter(&$query, &$view) {
$view->args[$delta] = $view->year;
}
$view->calendar_type = str_replace('calendar_', '', $argument['type']);
$view->delta['year'] = $delta;
}
if ($argument['type'] == 'calendar_month' && $view->args[$delta]) {
$view->month = _calendar_is_valid($view->args[$delta], 'month') ? $view->args[$delta] : calendar_user_date('month');
$view->week = calendar_week('week', $view);
$query_min .= '-'. sprintf('%02d', $view->month);
$query_max .= '-'. sprintf('%02d', $view->month);
// use the second day of the month because gm functions sometimes return the previous month
......@@ -687,6 +701,7 @@ function calendar_views_query_alter(&$query, &$view) {
$iso = $view->year .'-'. sprintf('%02d', $view->month) .'-02';
$view->subtitle = l(gmdate($format, strtotime($iso)), $view->url .'/'. $view->year .'/'. $view->month);
$view->calendar_type = str_replace('calendar_', '', $argument['type']);
$view->delta['month'] = $delta;
}
if ($argument['type'] == 'calendar_day' && $view->args[$delta]) {
$view->day = _calendar_is_valid($view->args[$delta], 'day') ? $view->args[$delta] : calendar_user_date('day');
......@@ -695,18 +710,21 @@ function calendar_views_query_alter(&$query, &$view) {
$iso = $view->year .'-'. sprintf('%02d', $view->month) .'-'. sprintf('%02d', $view->day) .'T12:00:00';
$view->subtitle = l(gmdate('l, F j Y', strtotime($iso)), $view->url .'/'. $view->year .'/'. $view->month .'/'. $view->day);
$view->calendar_type = str_replace('calendar_', '', $argument['type']);
$view->delta['week'] = $delta;
}
if ($argument['type'] == 'calendar_week' && $view->args[$delta]) {
$view->week = _calendar_is_valid($view->args[$delta], 'week') ? $view->args[$delta] : 1;
// use strtotime to find first day of requested week
$week = $view->args[$delta];
$view->week = $week;
$week_start = strtotime('Jan 1, '.$view->year.' + '.intval($week - 1).' weeks');
$week_end = $week_start + 604800;
$query_min = date('Y', $week_start) .'-'. date('m', $week_start) .'-'. date('d', $week_start) .'T00:00:00';
$query_max = date('Y', $week_end) .'-'. date('m', $week_end) .'-'. date('d', $week_end) .'T23:59:59';
$view->subtitle = l(t("Week of %date", array('%date' => gmdate('F j Y', $week_start))), $view->url .'/'. $view->year .'/W'. $view->week);
$query_min = calendar_week('start_year', $view, $week) .'-'. sprintf('%02d', calendar_week('start_month', $view, $week)) .'-'. calendar_week('start_day', $view, $week) .'T00:00:00';
$query_max = calendar_week('end_year', $view, $week) .'-'. sprintf('%02d', calendar_week('end_month', $view, $week)) .'-'. calendar_week('end_day', $view, $week) .'T23:59:59';
$view->subtitle = l(t("Week of %date", array('%date' => gmdate('F j Y', calendar_week('start_timestamp', $view, $week)))), $view->url .'/'. $view->year .'/W'. $view->week);
$view->calendar_type = str_replace('calendar_', '', $argument['type']);
$view->month = calendar_week('start_month', $view, $week);
$view->day = calendar_week('start_day', $view, $week);
$view->delta['day'] = $delta;
}
}
}
......@@ -721,8 +739,8 @@ function calendar_views_query_alter(&$query, &$view) {
$minmax = array(
'year' => array('-01-01', '-12-31'),
'month' => array('-01', '-31'),
'day' => array('T00', 'T23'),
'hour' => array(':00', ':59'),
'day' => array('T00:00:00', 'T23:59:59'),
'hour' => array(':00:00', ':59:59'),
'minute' => array(':00', ':59'),
);
......@@ -789,15 +807,52 @@ function calendar_views_pre_view(&$view, &$items) {
// add links to the top of the calendar to switch from one view to another
if ($view->build_type == 'page') {
$base_url = $view->url .'/'. $view->year;
$month = $view->month ? $view->month : calendar_user_date('month');
$day = $view->day ? $view->day : calendar_user_date('day');
$week = $view->week ? $view->week : calendar_user_date('week');
$calendar_links[] = $view->calendar_type == 'year' ? t('Year') : l(t('Year'), $view->url .'/'. $view->year);
$calendar_links[] = $view->calendar_type == 'month' ? t('Month') : l(t('Month'), $view->url .'/'. $view->year .'/'. ($view->month ? $view->month : calendar_user_date('month')));
$calendar_links[] = $view->calendar_type == 'week' ? t('Week') : l(t('Week'), $view->url .'/'. $view->year .'/W'. ($view->week ? $view->week : calendar_user_date('week')));
$calendar_links[] = $view->calendar_type == 'day' ? t('Day') : l(t('Day'), $view->url .'/'. $view->year .'/'. ($view->month ? $view->month : calendar_user_date('month')) .'/'. ($view->day ? $view->day : calendar_user_date('day')));
$calendar_links[] = $view->calendar_type == 'month' ? t('Month') : l(t('Month'), $view->url .'/'. $view->year .'/'. $month);
$calendar_links[] = $view->calendar_type == 'week' ? t('Week') : l(t('Week'), $view->url .'/'. $view->year .'/W'. $week);
$calendar_links[] = $view->calendar_type == 'day' ? t('Day') : l(t('Day'), $view->url .'/'. $view->year .'/'. $month .'/'. $day);
return theme('calendar_links', $calendar_links, 'month');
}
return;
}
/**
* Handle a lot of messy week calculations all in one place to make updates easier
*/
function calendar_week($op, $view, $week = 0) {
if ($week == 0) {
$date = strtotime($view->year .'-'. sprintf('%02d', $view->month ? $view->month : calendar_user_date('month')) .'-'. sprintf('%02d', $view->day ? $view->day : calendar_user_date('day')));
$week = date('W', $date);
if ($op == 'week') return $week;
}
$week_start = strtotime('Jan 1, '.$view->year.' + '.intval($week - 1).' weeks');
$week_end = $week_start + 604800;
switch ($op) {
case ('start_year'):
return date('Y', $week_start);
case ('end_year'):
return date('Y', $week_end);
case ('start_month'):
return date('n', $week_start);
case ('end_month'):
return date('n', $week_end);
case ('start_day'):
return date('d', $week_start);
case ('end_day'):
return date('d', $week_end);
default:
return $week_start;
}
}
/**
* A function to test the validity of various date parts
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment