diff --git a/fullcalendar_scheduler/fullcalendar_scheduler.info.yml b/fullcalendar_scheduler/fullcalendar_scheduler.info.yml new file mode 100644 index 0000000..7558211 --- /dev/null +++ b/fullcalendar_scheduler/fullcalendar_scheduler.info.yml @@ -0,0 +1,7 @@ +name: FullCalendar Scheduler +type: module +description: Adds Scheduler support to Fullcalendar views. +core: 8.x +package: FullCalendar +dependencies: + - fullcalendar diff --git a/fullcalendar_scheduler/fullcalendar_scheduler.libraries.yml b/fullcalendar_scheduler/fullcalendar_scheduler.libraries.yml new file mode 100644 index 0000000..9a6a0bc --- /dev/null +++ b/fullcalendar_scheduler/fullcalendar_scheduler.libraries.yml @@ -0,0 +1,22 @@ +fullcalendar_scheduler: + remote: https://fullcalendar.io/scheduler/ + version: 1.9.1 + license: + name: proprietary + url: https://github.com/fullcalendar/fullcalendar-scheduler/blob/master/LICENSE.md + gpl-compatible: true + js: + /libraries/fullcalendar-scheduler/scheduler.js: {} + css: + theme: + /libraries/fullcalendar-scheduler/scheduler.css: {} + dependencies: + - fullcalendar/fullcalendar + +drupal.fullcalendar_scheduler.js: + version: VERSION + js: + js/fullcalendar_scheduler.fullcalendar_scheduler.js: { } + dependencies: + - fullcalendar_scheduler/fullcalendar_scheduler + - fullcalendar/drupal.fullcalendar.js diff --git a/fullcalendar_scheduler/js/fullcalendar_scheduler.fullcalendar_scheduler.js b/fullcalendar_scheduler/js/fullcalendar_scheduler.fullcalendar_scheduler.js new file mode 100644 index 0000000..9f0f430 --- /dev/null +++ b/fullcalendar_scheduler/js/fullcalendar_scheduler.fullcalendar_scheduler.js @@ -0,0 +1,53 @@ +/** + * @file + * Processes the FullCalendar options and passes them to the integration. + */ + +(function ($, Drupal, drupalSettings) { + + "use strict"; + + Drupal.fullcalendar.plugins.fullcalendar_scheduler = { + options: function (fullcalendar, settings) { + + fullcalendar.parseEvents = function (callback) { + var events = []; + var details = this.$calendar.find('.fullcalendar-event-details'); + for (var i = 0; i < details.length; i++) { + var event = $(details[i]); + var eid = event.data('eid'); + events.push({ + field: event.data('field'), + index: event.data('index'), + eid: eid, + entity_type: event.data('entity-type'), + title: event.attr('title'), + start: event.data('start'), + end: event.data('end'), + url: event.attr('href'), + allDay: (event.data('all-day') === 1), + className: event.data('cn'), + editable: (event.data('editable') === 1), + color: event.data('color'), + dom_id: this.dom_id, + resourceId: settings.events[eid].resourceId + }); + } + callback(events); + }; + + var options = Drupal.fullcalendar.plugins.fullcalendar.options(fullcalendar, settings); + + // Merge in our settings. + $.extend(options, settings.fullcalendar_scheduler); + + options.resourceRender = function (resourceObj, labelTds, bodyTds) { + labelTds.find('.fc-cell-text').html(resourceObj.html); + }; + + return options; + } + + }; + +})(jQuery, Drupal, drupalSettings); diff --git a/fullcalendar_scheduler/src/Plugin/fullcalendar/type/FullcalendarScheduler.php b/fullcalendar_scheduler/src/Plugin/fullcalendar/type/FullcalendarScheduler.php new file mode 100644 index 0000000..5dd85ed --- /dev/null +++ b/fullcalendar_scheduler/src/Plugin/fullcalendar/type/FullcalendarScheduler.php @@ -0,0 +1,171 @@ + [ + 'enabled' => ['default' => FALSE], + 'resource_field' => ['default' => NULL], + 'defaultView' => ['default' => 'timelineDay'], + 'views' => ['default' => [ + 'timelineDay' => [ + 'slotLabelFormat' => '', + ], + 'agendaDay' => [ + 'slotLabelFormat' => '', + ], + 'timelineWeek' => [ + 'slotLabelFormat' => '', + ], + 'agendaWeek' => [ + 'slotLabelFormat' => '', + ], + 'timelineMonth' => [ + 'slotLabelFormat' => '', + ], + 'timelineYear' => [ + 'slotLabelFormat' => '', + ], + ]], + 'schedulerLicenseKey' => ['default' => ''], + ], + ]; + return $options; + } + + /** + * {@inheritdoc} + */ + public function buildOptionsForm(&$form, FormStateInterface $form_state) { + $form['fullcalendar_scheduler'] = [ + '#type' => 'details', + '#title' => $this->t('Scheduler'), + '#open' => TRUE, + ]; + $form['fullcalendar_scheduler']['enabled'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Use the Scheduler display'), + '#default_value' => $this->style->options['fullcalendar_scheduler']['enabled'], + '#fieldset' => 'fullcalendar_scheduler', + ]; + + $field_labels = $this->style->displayHandler->getFieldLabels(TRUE); + $form['fullcalendar_scheduler']['resource_field'] = [ + '#type' => 'select', + '#title' => $this->t('Resource field'), + '#options' => $field_labels, + '#default_value' => $this->style->options['fullcalendar_scheduler']['resource_field'], + '#fieldset' => 'fullcalendar_scheduler', + ]; + $form['fullcalendar_scheduler']['defaultView'] = [ + '#type' => 'select', + '#title' => $this->t('Initial display'), + '#options' => [ + 'timelineDay' => $this->t('Day'), + 'agendaDay' => $this->t('Day (Agenda)'), + 'timelineWeek' => $this->t('Week'), + 'agendaWeek' => $this->t('Week (Agenda)'), + 'timelineMonth' => $this->t('Month'), + 'timelineYear' => $this->t('Year'), + ], + '#description' => Link::fromTextAndUrl($this->t('More info'), Url::fromUri('http://arshaw.com/fullcalendar/docs/views/Available_Views', ['attributes' => ['target' => '_blank']])), + '#default_value' => $this->style->options['fullcalendar_scheduler']['defaultView'], + '#fieldset' => 'fullcalendar_scheduler', + ]; + $form['fullcalendar_scheduler']['views'] = [ + '#type' => 'details', + '#title' => $this->t('Display configuration'), + ]; + foreach ($form['fullcalendar_scheduler']['defaultView']['#options'] as $option => $label) { + $form['fullcalendar_scheduler']['views'][$option] = [ + '#type' => 'details', + '#title' => $label, + ]; + $form['fullcalendar_scheduler']['views'][$option]['slotLabelFormat'] = [ + '#type' => 'textfield', + '#title' => $this->t('Header date format'), + '#default_value' => $this->style->options['fullcalendar_scheduler']['views'][$option]['slotLabelFormat'], + '#description' => Link::fromTextAndUrl($this->t('More info'), Url::fromUri('http://arshaw.com/fullcalendar/docs/agenda/slotLabelFormat', array('attributes' => array('target' => '_blank')))), + ]; + $form['fullcalendar_scheduler']['views'][$option]['slotWidth'] = [ + '#type' => 'textfield', + '#title' => $this->t('Column width'), + '#default_value' => $this->style->options['fullcalendar_scheduler']['views'][$option]['slotWidth'], + '#field_suffix' => 'px', + '#description' => Link::fromTextAndUrl($this->t('More info'), Url::fromUri('http://arshaw.com/fullcalendar/docs/agenda/slotWidth', array('attributes' => array('target' => '_blank')))), + ]; + } + + $form['fullcalendar_scheduler']['schedulerLicenseKey'] = [ + '#type' => 'textfield', + '#title' => $this->t('License key'), + '#default_value' => $this->style->options['fullcalendar_scheduler']['schedulerLicenseKey'], + '#description' => Link::fromTextAndUrl($this->t('More info'), Url::fromUri('https://fullcalendar.io/scheduler/license/', ['attributes' => ['target' => '_blank']])), + ]; + } + + /** + * {@inheritdoc} + */ + public function process(&$settings) { + if ($settings['fullcalendar_scheduler']['enabled']) { + $default_view = $settings['fullcalendar_scheduler']['defaultView']; + if (isset($settings['fullcalendar']['titleFormat']) && !is_array($settings['fullcalendar']['titleFormat'])) { + $type = strtolower(substr($default_view, 8)); + $settings['fullcalendar']['titleFormat'] = $settings['fullcalendar']['titleFormat'][$type]; + } + + $resource_field = $settings['fullcalendar_scheduler']['resource_field']; + + $settings['events'] = []; + $resources = []; + foreach ($this->style->view->result as $delta => $row) { + $field_value = $this->style->getFieldValue($delta, $resource_field); + $field = (string) $this->style->getField($delta, $resource_field); + + $entity = $row->_entity; + $settings['events'][$entity->id()] = [ + 'resourceId' => $field_value, + ]; + + $resources[$field_value] = [ + 'id' => $field_value, + 'title' => strip_tags(htmlspecialchars_decode($field)), + 'html' => $field, + ]; + } + $settings['fullcalendar_scheduler']['resources'] = array_values($resources); + + unset( + $settings['fullcalendar_scheduler']['enabled'], + $settings['fullcalendar_scheduler']['resource_field'] + ); + } + else { + unset($settings['fullcalendar_scheduler']); + } + } + +}