bat_fullcalendar.module in Booking and Availability Management Tools for Drupal 8
Same filename and directory in other branches
Manages the display of FullCalendar and provides ways for other modules to easily modify it.
File
modules/bat_fullcalendar/bat_fullcalendar.moduleView source
<?php
/**
* @file
*
* Manages the display of FullCalendar and provides ways for other modules
* to easily modify it.
*/
use Drupal\Core\Ajax\CloseModalDialogCommand;
use Drupal\Core\Template\Attribute;
use Drupal\Component\Utility\Html;
use Drupal\bat_event\Controller\EventController;
/**
* Implements hook_bat_fullcalendar_modal_content();
*/
function bat_fullcalendar_bat_fullcalendar_modal_content($entity_id, $event_type, $event_id, $start_date, $end_date) {
if ($event_id > 0) {
$event = bat_event_load($event_id);
$event_form = (new EventController(\Drupal::request()))
->editEvent($event);
$event_form['#attached']['library'][] = 'core/drupal.ajax';
$event_form['#attached']['library'][] = 'core/jquery.form';
if (isset($event_form['#validated']) && $event_form['#validated']) {
return [
'bat_fullcalendar' => [
'commands' => [
new CloseModalDialogCommand(),
],
],
];
}
else {
return [
'bat_fullcalendar' => [
'title' => t('Edit event'),
'content' => $event_form,
],
];
}
}
else {
return [
'bat_fullcalendar' => [
'title' => t('Event Management'),
'content' => \Drupal::formBuilder()
->getForm('Drupal\\bat_fullcalendar\\Form\\FullcalendarEventManagerForm', $entity_id, $event_type, $event_id, $start_date, $end_date),
],
];
}
}
/**
* Fullcalendar settings.
*
* @param $user_settings
* @return array
*/
function bat_fullcalendar_configure($user_settings) {
$settings = [];
$config = \Drupal::config('bat_fullcalendar.settings');
foreach ($user_settings['batCalendar'] as $id => $user_setting) {
// Set the scheduler license.
$schedulerlicense = $config
->get('bat_fullcalendar_scheduler_key');
switch ($schedulerlicense) {
case 'gpl':
$schedulerlicensekey = 'GPL-My-Project-Is-Open-Source';
break;
case 'non-commercial':
$schedulerlicensekey = 'CC-Attribution-NonCommercial-NoDerivatives';
break;
case 'commercial':
$schedulerlicensekey = $config
->get('bat_fullcalendar_scheduler_commercial_key');
break;
case 'none':
$schedulerlicensekey = '';
break;
default:
$schedulerlicensekey = '';
break;
}
$calendar_views = 'timelineDay, timelineTenDay, timelineMonth, timelineYear';
$default_view = 'timelineDay';
$default_date = (new DateTime())
->format('Y-m-d');
if ($user_setting['eventGranularity'] == 'bat_daily') {
$calendar_views = 'timelineThirtyDay, timelineYear';
$default_view = 'timelineThirtyDay';
$default_date = (new DateTime())
->modify('-5 days')
->format('Y-m-d');
}
elseif ($user_setting['eventGranularity']) {
$calendar_views = 'timelineDay, timelineTenDay, timelineMonth';
$default_view = 'timelineDay';
}
if (isset($user_setting['views']) && !empty($user_setting['views'])) {
$calendar_views = $user_setting['views'];
}
if (isset($user_setting['defaultView']) && !empty($user_setting['defaultView'])) {
$default_view = $user_setting['defaultView'];
}
if (!isset($user_setting['businessHours'])) {
$business_hours = [
'start' => '00:00',
'end' => '24:00',
'dow' => [
0,
1,
2,
3,
4,
5,
6,
],
];
}
else {
$business_hours = $user_setting['businessHours'];
}
$user_timezone = new DateTimeZone(date_default_timezone_get());
$gmt_timezone = new DateTimeZone('GMT');
$timezone_offset = $user_timezone
->getOffset(new DateTime('now', $gmt_timezone));
$config = [
'schedulerLicenseKey' => $schedulerlicensekey,
'themeSystem' => 'standard',
'unitType' => '',
'unitIds' => '',
'eventType' => '',
'grouping_entity_type' => '',
'grouping_ids' => '',
'collapse_childrens' => 0,
'eventGranularity' => $user_setting['eventGranularity'],
'locale' => \Drupal::languageManager()
->getCurrentLanguage()
->getId(),
'slotWidth' => FALSE,
'calendarHeight' => 500,
'editable' => TRUE,
'selectable' => TRUE,
'eventStartEditable' => TRUE,
'eventDurationEditable' => TRUE,
'headerLeft' => 'today, prev, next',
'headerCenter' => 'title',
'headerRight' => $calendar_views,
'views' => $calendar_views,
'defaultView' => $default_view,
'viewsTimelineDayButtonText' => ':15 slots',
'viewsTimelineDaySlotDuration' => '00:15',
'viewsTimelineDaySlotLabelFormat' => FALSE,
'viewsTimelineDayTitleFormat' => '',
'viewsTimelineSevenDayButtonText' => '7 days',
'viewsTimelineSevenDayDuration' => [
'days' => 7,
],
'viewsTimelineSevenDaySlotDuration' => '01:00',
'viewsTimelineSevenDayTitleFormat' => '',
'viewsTimelineSevenDaySlotLabelFormat' => FALSE,
'viewsTimelineTenDayButtonText' => '10 days',
'viewsTimelineTenDayDuration' => [
'days' => 10,
],
'viewsTimelineTenDaySlotDuration' => '01:00',
'viewsTimelineTenDaySlotLabelFormat' => FALSE,
'viewsTimelineTenDayTitleFormat' => '',
'viewsTimelineThirtyDayButtonText' => '30 days',
'viewsTimelineThirtyDayDuration' => [
'days' => 30,
],
'viewsTimelineThirtyDaySlotDuration' => '01:00',
'viewsTimelineThirtyDaySlotLabelFormat' => FALSE,
'viewsTimelineThirtyDayTitleFormat' => '',
'viewsTimeline365DayButtonText' => '1 year',
'viewsTimeline365DayDuration' => [
'days' => 365,
],
'viewsTimeline365DaySlotLabelFormat' => FALSE,
'viewsTimeline365DayTitleFormat' => '',
'viewsAgendaOneDayButtonText' => 'day',
'viewsAgendaOneDayDuration' => [
'days' => 1,
],
'viewsAgendaSevenDayButtonText' => 'week',
'viewsAgendaSevenDayDuration' => [
'days' => 7,
],
'resourceAreaWidth' => '25%',
'resourceLabelText' => t('Types'),
'errorMessage' => t('Action not allowed. User may not have the right permissions.'),
'businessHours' => $business_hours,
'selectConstraint' => NULL,
'scrollTime' => '06:00:00',
'minTime' => '00:00:00',
'maxTime' => '24:00:00',
'hiddenDays' => [],
'validRange' => NULL,
'defaultDate' => $default_date,
'repeatEventTitle' => TRUE,
'showBackgroundEventTitle' => FALSE,
'enableModal' => TRUE,
'customButtons' => [],
'eventOrder' => 'title',
'titleFormat' => '',
'slotLabelFormat' => FALSE,
'defaultTimedEventDuration' => '00:00:00',
'selectAllowBusinessHours' => FALSE,
'groupByResource' => FALSE,
'groupByDateAndResource' => FALSE,
'allDaySlot' => TRUE,
'hideResourceTypes' => FALSE,
'firstDay' => 0,
'viewsAgendaButtonText' => '',
'viewsWeekButtonText' => '',
'viewsDayButtonText' => '',
'allDayDefault' => NULL,
'dialogOptions' => bat_fullcalendar_modal_style('default'),
];
$settings['batCalendar'][$id] = array_replace_recursive($config, $user_setting);
// Allow other modules to alter further.
\Drupal::moduleHandler()
->alter('bat_calendar_settings', $settings);
}
return $settings;
}
/**
* Theme function for FullCalendar
*/
function template_preprocess_bat_fullcalendar(&$variables) {
$calendar_id = $variables['calendar_settings']['calendar_id'];
$user_settings = $variables['calendar_settings']['user_settings'];
// Attributes.
if (!empty($variables['attributes'])) {
$attributes = $variables['attributes'];
}
else {
$attributes = [
'id' => Html::getUniqueId('calendar'),
'class' => [
'calendar-set',
'clearfix',
],
];
if (isset($variables['calendar_settings']['class']) && !empty($variables['calendar_settings']['class'])) {
$attributes['class'] = array_merge($attributes['class'], $variables['calendar_settings']['class']);
}
}
// Override default settings with ones coming from the user.
$settings = bat_fullcalendar_configure($variables['calendar_settings']['user_settings']);
$settings['batCalendar'][0]['id'] = $attributes['id'];
$attributes = new Attribute($attributes);
$variables['#attached'] = [
'library' => [
'bat_fullcalendar/fullcalendar-scheduler',
],
'drupalSettings' => $settings,
];
$variables['content'] = [
'#markup' => '<div' . $attributes
->__toString() . '></div>',
];
}
/**
* Implements hook_theme().
*/
function bat_fullcalendar_theme($existing, $type, $theme, $path) {
return [
'bat_fullcalendar' => [
'variables' => [
'calendar_settings' => [],
'attributes' => [],
],
],
];
}
/**
* Define modal JS style and dependencies.
*
* @param $style
*/
function bat_fullcalendar_modal_style($style = 'default') {
global $base_path;
// Styles to use for the modal.
$modal_style = [
'buttons' => FALSE,
'modal' => TRUE,
'width' => 520,
'height' => 520,
'title' => t('Loading...'),
'loading' => '<img class="loading" src="' . $base_path . drupal_get_path('module', 'bat_fullcalendar') . '/images/throbber.gif">',
];
// Allow other modules to change the modal style.
\Drupal::moduleHandler()
->alter('bat_fullcalendar_modal_style', $modal_style);
return $modal_style;
}
Functions
Name | Description |
---|---|
bat_fullcalendar_bat_fullcalendar_modal_content | Implements hook_bat_fullcalendar_modal_content(); |
bat_fullcalendar_configure | Fullcalendar settings. |
bat_fullcalendar_modal_style | Define modal JS style and dependencies. |
bat_fullcalendar_theme | Implements hook_theme(). |
template_preprocess_bat_fullcalendar | Theme function for FullCalendar |