Skip to content

Instantly share code, notes, and snippets.

@shadcn
Created August 22, 2014 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shadcn/69af1fd959c6df41211d to your computer and use it in GitHub Desktop.
Save shadcn/69af1fd959c6df41211d to your computer and use it in GitHub Desktop.
/**
* Display a day view.
*/
function template_preprocess_calendar_day(&$vars) {
$vars['view']->style_with_weekno = FALSE;
$view = $vars['view'];
$rows = $vars['rows'];
$item_count = 0;
$by_hour_count = 0;
$grouping_field = !empty($view->date_info->style_groupby_field) ? ($view->date_info->style_groupby_field) : NULL;
$display_overlap = !empty($view->date_info->style_theme_style) && !empty($view->date_info->style_groupby_times);
$vars['scroll_content'] = !empty($view->date_info->style_theme_style) && $view->date_info->style_theme_style == 1;
// Add optional css
if ($display_overlap) {
$overlapped_items = array();
drupal_add_css(drupal_get_path('module', 'calendar') . '/css/calendar-overlap.css');
if (empty($view->live_preview) && !empty($vars['scroll_content'])) {
drupal_add_js(drupal_get_path('module', 'calendar') . '/js/calendar_overlap.js');
}
if (empty($vars['scroll_content'])) {
drupal_add_css(drupal_get_path('module', 'calendar') . '/css/calendar-overlap-no-scroll.css');
}
}
// If we're not grouping by time, move all items into the 'all day' array.
if (empty($view->date_info->style_groupby_times)) {
// Items are already grouped into times, so we need to process each time-group.
foreach ($rows['items'] as $time => $items) {
foreach ($items as $item) {
$rows['all_day'][] = $item;
}
}
$rows['items'] = array();
}
$columns = array();
// Move all_day items into the right columns and render them.
$grouped_items = array();
foreach ($rows['all_day'] as $item) {
if (!empty($item->rendered_fields[$grouping_field])) {
$column = $item->rendered_fields[$grouping_field];
if (!in_array($column, $columns)) {
$columns[] = $column;
}
}
else {
$column = t('Items');
}
$grouped_items[$column][] = theme('calendar_item', array('view' => $view, 'rendered_fields' => $item->rendered_fields, 'item' => $item));
$item_count++;
}
$vars['rows']['all_day'] = $grouped_items;
// Moved timed items into the right columns and render them.
$start_times = $view->date_info->style_groupby_times;
$show_empty_times = $view->date_info->style_show_empty_times;
$end_start_time = '23:59:59';
$start_time = array_shift($start_times);
$next_start_time = count($start_times) ? array_shift($start_times) : $end_start_time;
$grouped_items = array();
foreach ($rows['items'] as &$items) {
foreach ($items as &$item) {
$time = date_format($item->calendar_start_date, 'H:i:s');
if (!empty($item->rendered_fields[$grouping_field])) {
$column = $item->rendered_fields[$grouping_field];
if (!in_array($column, $columns)) {
$columns[] = $column;
}
}
else {
$column = t('Items');
}
// Find the next time slot and fill it. Populate the skipped
// slots if the option to show empty times was chosen.
while ($time >= $next_start_time && $time < $end_start_time) {
if ((!empty($show_empty_times) || $display_overlap) && !array_key_exists($start_time, $grouped_items)) {
$grouped_items[$start_time]['values'] = array();
}
$start_time = $next_start_time;
$next_start_time = count($start_times) ? array_shift($start_times) : $end_start_time;
}
$grouped_items[$start_time]['values'][$column][] = $item;
if ($display_overlap) {
$time_end = date_format($item->calendar_end_date, 'H:i:s');
$item->time_start = $time;
$item->time_end = $time_end;
_calc_indents($overlapped_items, $time, $time_end, $item);
}
$item_count++;
$by_hour_count++;
}
}
// Finish out the day's time values if we want to see empty times.
if (!empty($show_empty_times) || $display_overlap) {
while ($start_time < $end_start_time && (!empty($start_time) || $display_overlap)) {
if (empty($start_time)) {
$start_times = $view->date_info->style_groupby_times;
$start_time = array_shift($start_times);
$next_start_time = array_shift($start_times);
}
if (!array_key_exists($start_time, $grouped_items)) {
$grouped_items[$start_time]['values'] = array();
}
$start_time = $next_start_time;
$next_start_time = count($start_times) ? array_shift($start_times) : $end_start_time;
}
}
// Do the headers last, once we know what the actual values are.
$i = 0;
$start_times = array_keys($grouped_items);
foreach ($start_times as $start_time) {
$next_start_time = array_key_exists($i + 1, $start_times) ? $start_times[$i + 1] : '23:59:59';
$variables = array(
'start_time' => $start_time,
'next_start_time' => $next_start_time,
'curday_date' => $rows['date'],
);
$heading = theme('calendar_time_row_heading', $variables);
$grouped_items[$start_time]['hour'] = $heading['hour'];
$grouped_items[$start_time]['ampm'] = $heading['ampm'];
foreach ($grouped_items[$start_time]['values'] as $column => &$items) {
foreach ($items as $index => &$item) {
$group_time = NULL;
$divisor = NULL;
if ($display_overlap) {
if (!empty($view->style_options['groupby_times'])) {
if ($view->style_options['groupby_times'] == 'half') {
$group_time = 30;
$divisor = 7.5;
}
elseif ($view->style_options['groupby_times'] == 'hour') {
$group_time = 60;
$divisor = 15;
}
}
else {
$item->class = '';
}
if (!empty($group_time) && !empty($divisor)) {
$start_minute = intval(substr($start_time, 3, 2));
$offset = round((date_format($item->date_start, 'i') - $start_minute) / $divisor);
$duration = round(($item->date_end->format('U') - $item->date_start->format('U')) / 60 / $divisor);
$item->class = 'd_' . $duration . ' o_' . $offset . ' i_' . $item->indent . ' md_' . min($item->max_depth, 5);
}
}
$grouped_items[$start_time]['values'][$column][$index] = theme('calendar_item', array('view' => $view, 'rendered_fields' => $item->rendered_fields, 'item' => $item));
}
}
$i++;
}
ksort($grouped_items);
$vars['rows']['items'] = $grouped_items;
if (empty($columns)) {
$columns = array(t('Items'));
}
$vars['columns'] = $columns;
$vars['agenda_hour_class'] = 'calendar-agenda-hour';
$first_column_width = 10;
if (empty($view->date_info->style_groupby_times)) {
$vars['agenda_hour_class'] .= ' calendar-agenda-no-hours';
$first_column_width = 1;
}
$vars['first_column_width'] = $first_column_width;
if (count($columns)) {
$vars['column_width'] = round((100 - $first_column_width)/count($columns));
}
else {
$vars['column_width'] = (100 - $first_column_width);
}
$vars['item_count'] = $item_count;
$vars['by_hour_count'] = $by_hour_count;
$vars['start_times'] = $view->date_info->style_groupby_times;
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment