View source
<?php
use Drupal\calendar\Plugin\views\argument\CalendarDate;
use Drupal\Component\Datetime\DateTimePlus;
use Drupal\Core\Url;
use Drupal\views\ViewExecutable;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\calendar\CalendarHelper;
define('CALENDAR_SHOW_ALL', 0);
define('CALENDAR_HIDE_ALL', -1);
define('CALENDAR_EMPTY_STRIPE', '#ffffff');
function calendar_page_attachments(array &$page) {
$page['#attached']['library'][] = 'calendar/calendar.theme';
}
function calendar_date_default_argument_alter(&$argument, &$value) {
$user = \Drupal::currentUser();
$style_options = $argument->view->display_handler
->get_option('style_options');
$tracking = \Drupal::config('calendar.settings')
->get('calendar_track_date');
if (!empty($tracking) && ($tracking == 2 || !empty($user->uid))) {
if (!empty($_SESSION[$argument->view->name]['default_date'])) {
$default_date = $_SESSION[$argument->view->name]['default_date'];
}
else {
$default_date = date_now();
$_SESSION[$argument->view->name]['default_date'] = $default_date;
}
$value = $default_date
->format($argument->arg_format);
}
}
function calendar_views_pre_render(ViewExecutable $view) {
_calendar_views_track_current_date($view);
}
function _calendar_views_track_current_date(ViewExecutable $view) {
$user = \Drupal::currentUser();
$tracking = \Drupal::config('calendar.settings')
->get('calendar_track_date');
if (!empty($tracking) && ($tracking == 2 || !empty($user->uid))) {
foreach ($view->argument as $id => &$argument) {
if ($argument instanceof CalendarDate && empty($argument->is_default)) {
$date_range = $argument->date_handler
->arg_range($argument->argument);
$session_date = $date_range[0];
$days = intval(($date_range[1]
->format('U') - $date_range[0]
->format('U')) / 3600 / 24 / 2);
date_modify($session_date, "+{$days} days");
$_SESSION[$view->name]['default_date'] = $session_date;
}
}
}
}
function calendar_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.calendar':
return '<p>' . t('View complete documentation at <a href=":link">Date and Calendar Documentation</a>.', [
':link' => 'http://drupal.org/node/262062',
]) . '</p>';
}
}
function calendar_theme() {
return [
'calendar_mini' => [
'variables' => [
'view' => NULL,
'options' => [],
'rows' => [],
],
'file' => 'calendar.theme.inc',
],
'calendar_month_multiple_entity' => [
'variables' => [
'curday' => NULL,
'count' => NULL,
'view' => NULL,
'ids' => NULL,
],
],
'calendar_datebox' => [
'variables' => [
'date' => NULL,
'view' => NULL,
'items' => NULL,
'selected' => NULL,
],
'file' => 'calendar.theme.inc',
],
'calendar_empty_day' => [
'variables' => [
'curday' => NULL,
'view' => NULL,
],
],
'calendar_month_col' => [
'variables' => [
'item' => NULL,
],
],
'calendar_month_row' => [
'variables' => [
'inner' => NULL,
'class' => NULL,
'iehint' => NULL,
],
],
'calendar_day' => [
'variables' => [
'view' => NULL,
'options' => [],
'rows' => [],
],
'file' => 'calendar.theme.inc',
],
'calendar_month' => [
'variables' => [
'view' => NULL,
'options' => [],
'rows' => [],
],
'file' => 'calendar.theme.inc',
],
'calendar_stripe_legend' => [
'variables' => [
'view_and_display_id' => NULL,
'title' => NULL,
],
],
'calendar_item' => [
'variables' => [
'view' => NULL,
'rendered_fields' => NULL,
'item' => NULL,
],
'file' => 'calendar.theme.inc',
],
'calendar_week' => [
'variables' => [
'view' => NULL,
'day_names' => [],
'rows' => [],
],
'file' => 'calendar.theme.inc',
],
'calendar_week_overlap' => [
'variables' => [
'view' => NULL,
'day_names' => [],
'rows' => [],
],
'file' => 'calendar.theme.inc',
],
'calendar_day_overlap' => [
'variables' => [
'view' => NULL,
'options' => [],
'rows' => [],
],
'file' => 'calendar.theme.inc',
],
'calendar_year' => [
'variables' => [
'view' => NULL,
'rows' => [],
'options' => [],
'months' => [],
'mini' => FALSE,
],
'file' => 'calendar.theme.inc',
],
'calendar_header' => [
'variables' => [
'view' => NULL,
'options' => [],
'title' => '',
'empty' => FALSE,
'granularity' => NULL,
],
'file' => 'calendar.theme.inc',
],
'calendar_time_row_heading' => [
'variables' => [
'start_time' => NULL,
'next_start_time' => NULL,
'curday_date' => NULL,
],
],
'calendar_pager' => [
'variables' => [
'parameters' => [],
'items' => [],
'exclude' => FALSE,
],
],
];
}
function calendar_groupby_times($type = '') {
$times = [];
switch ($type) {
case 'hour':
for ($i = 0; $i <= 23; $i++) {
$times[] = DateTimePlus::datePad($i) . ':00:00';
}
break;
case 'half':
for ($i = 0; $i <= 23; $i++) {
$times[] = DateTimePlus::datePad($i) . ':00:00';
$times[] = DateTimePlus::datePad($i) . ':30:00';
}
break;
default:
break;
}
return $times;
}
function calendar_granularity_path(ViewExecutable &$view, $granularity) {
$paths =& drupal_static(__FUNCTION__, []);
$view_name = $view
->id();
if (!array_key_exists($view_name, $paths) || isset($paths[$view
->id()]) && !array_key_exists($granularity, $paths[$view
->id()])) {
$paths[$view_name][$granularity] = '';
foreach ($view->displayHandlers
->getConfiguration() as $id => $display) {
if ($display['display_plugin'] != 'feed' && !empty($display['display_options']['path']) && !empty($display['display_options']['arguments'])) {
$type = 'month';
$argument = CalendarHelper::getDateArgumentHandler($view, $id);
if ($argument
->getGranularity() == $granularity) {
$part_path = $display['display_options']['path'];
$parts = explode('/', $part_path);
if (in_array('%', $parts)) {
$url = Url::fromRoute('<current>');
$current_path = $url
->toString();
$current_parts = explode('/', $current_path);
foreach ($parts as $key => $part) {
if ($part == '%' && !empty($current_parts[$key])) {
$parts[$key] = $current_parts[$key];
}
}
$part_path = implode('/', $parts);
}
$paths[$view
->id()][$granularity] = $part_path;
}
}
}
}
return $paths[$view
->id()][$granularity];
}