bat-8.x-1.x-dev/modules/bat_fullcalendar/bat_fullcalendar.module

modules/bat_fullcalendar/bat_fullcalendar.module
<?php

/**
 * @file
 * Manages the display of FullCalendar.
 *
 * Manages the display of FullCalendar and provides ways for other
 * modules to easily modify it.
 */

use Drupal\Component\Utility\Html;
use Drupal\Core\Ajax\RedirectCommand;
use Drupal\Core\Link;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Url;
use Drupal\bat_event\Controller\EventController;
use Symfony\Component\HttpFoundation\RedirectResponse;

/**
 * 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']) {

      $url = Url::fromUri('base:admin/bat/calendar/all/' . $event_type->id());
      return [
        'bat_fullcalendar' => [
          'commands' => [new RedirectCommand($url->toString())],
        ],
      ];

    }
    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.
 *   This old function may be deprecated today?
 *
 * @param array $fc_settings
 *   Some embedded FC settings.
 *
 * @return array
 *   Description message.
 */
function bat_fullcalendar_configure(array $fc_settings) {

  $settings = [];
  $system_date = \Drupal::config('system.date');
  $first_day = $system_date->get("first_day");

  foreach ($fc_settings['batCalendar'] as $id => $fc_setting) {

    $calendar_views = 'timelineDay, timelineTenDay, timelineMonth, timelineYear';
    $default_date = (new DateTime())->format('Y-m-d');

    if ($fc_setting['eventGranularity'] == 'bat_daily') {
      $calendar_views = 'timelineThirtyDay, timelineYear';

      $default_date = (new DateTime())->modify('-5 days')->format('Y-m-d');
    }
    elseif ($fc_setting['eventGranularity']) {
      $calendar_views = 'timelineDay, timelineTenDay, timelineMonth';
    }

    if (isset($fc_setting['views']) && !empty($fc_setting['views'])) {
      $calendar_views = $fc_setting['views'];
    }

    if (!isset($fc_setting['businessHours'])) {
      $business_hours = [
        'start' => '00:00',
        'end' => '24:00',
        'dow' => [0, 1, 2, 3, 4, 5, 6],
      ];
    }
    else {
      $business_hours = $fc_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));

    $values = [
      'allDayDefault' => NULL,
      'allDaySlot' => TRUE,
      'businessHours' => $business_hours,
      'collapse_childrens' => 0,
      'customButtons' => [],
      'defaultTimedEventDuration' => '00:00:00',
      'defaultDate' => $default_date,
      'dialogOptions' => bat_fullcalendar_modal_style('default'),
      'editable' => FALSE,
      'enableModal' => TRUE,
      'errorMessage' => t('Action not allowed. User may not have the right permissions.'),
      'eventDurationEditable' => TRUE,
      'eventType' => '',
      'eventOrder' => 'title',
      'eventGranularity' => $fc_setting['eventGranularity'],
      'eventStartEditable' => TRUE,
      'firstDay' => $first_day,
      'grouping_entity_type' => '',
      'grouping_ids' => '',
      'locale' => \Drupal::languageManager()->getCurrentLanguage()->getId(),
      'selectable' => TRUE,
      'headerLeft' => 'today, prev, next',
      'headerCenter' => 'title',
      'headerRight' => $calendar_views,
      'hiddenDays' => [],
      'hideResourceTypes' => FALSE,
      'minTime' => '00:00:00',
      'maxTime' => '24:00:00',
      'themeSystem' => 'standard',
      'unitType' => '',
      'unitIds' => '',
      'views' => $calendar_views,
      'viewsTimelineDayButtonText' => ':15 slots',
      'viewsTimelineDaySlotDuration' => '00:15',
      'viewsTimelineDaySlotLabelFormat' => FALSE,
      'viewsTimelineDayTitleFormat' => '',
      'viewsTimelineSevenDayButtonText' => t('7 days'),
      'viewsTimelineSevenDayDuration' => ['days' => 7],
      'viewsTimelineSevenDaySlotDuration' => '01:00',
      'viewsTimelineSevenDayTitleFormat' => '',
      'viewsTimelineSevenDaySlotLabelFormat' => FALSE,
      'viewsTimelineTenDayButtonText' => t('10 days'),
      'viewsTimelineTenDayDuration' => ['days' => 10],
      'viewsTimelineTenDaySlotDuration' => '01:00',
      'viewsTimelineTenDaySlotLabelFormat' => FALSE,
      'viewsTimelineTenDayTitleFormat' => '',
      'viewsTimelineThirtyDayButtonText' => t('30 days'),
      'viewsTimelineThirtyDayDuration' => ['days' => 30],
      'viewsTimelineThirtyDaySlotDuration' => '01:00',
      'viewsTimelineThirtyDaySlotLabelFormat' => FALSE,
      'viewsTimelineThirtyDayTitleFormat' => '',
      'viewsTimeline365DayButtonText' => t('1 year'),
      'viewsTimeline365DayDuration' => ['days' => 365],
      'viewsTimeline365DaySlotLabelFormat' => FALSE,
      'viewsTimeline365DayTitleFormat' => '',
      'viewsAgendaOneDayButtonText' => 'day',
      'viewsAgendaOneDayDuration' => ['days' => 1],
      'viewsAgendaSevenDayButtonText' => 'week',
      'viewsAgendaSevenDayDuration' => ['days' => 7],
      'repeatEventTitle' => TRUE,
      'selectConstraint' => NULL,
      'scrollTime' => '06:00:00',
      'selectAllowBusinessHours' => FALSE,
      'showBackgroundEventTitle' => FALSE,
      'slotLabelFormat' => FALSE,
      'titleFormat' => '',
      'validRange' => NULL,
      'viewsAgendaButtonText' => '',
      'viewsWeekButtonText' => '',
      'viewsDayButtonText' => '',
      'qwe' => 'eCdd2w',
    ];

    $settings['batCalendar'][$id] = array_replace_recursive($values, $fc_setting);

    // Allow other modules to alter further.
    \Drupal::moduleHandler()->alter('bat_calendar_settings', $settings);
  }

  return $settings;
}


function bat_fullcalendar_page_attachments(array &$page) {

  // $page['#attached']['library'][] = 'bat_fullcalendar/bat-fullcalendar-scheduler';

  $config = \Drupal::config('bat_fullcalendar.settings');

  // when on /admin, check fullcalendar settings
  if (\Drupal::service('router.admin_context')->isAdminRoute()) {
    if ( $config->get('bat_fullcalendar_calendar_timerange_start') == NULL ) {
       $url = Url::fromRoute('bat_fullcalendar.fullcalendar');
       $link = Link::fromTextAndUrl(t('Set up your Fullcalendar'), $url);
      \Drupal::messenger()->addError($link->toString() );
    }
  }

}

/**
 * Fullcalendar UI settings.
 *   Apply FC settings from /admin/bat/config/fullcalendar
 *
 * @param array $fc_settings
 *   Some embedded FC settings.
 *
 * @return array $fc_settings
 *   Array with FC settings.
 */
function bat_fullcalendar_ui_settings($fc_settings) {

  $settings = [];

  $config = \Drupal::config('bat_fullcalendar.settings');

  foreach ($fc_settings['batCalendar'] as $id => $fc_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;
    }

    // some default
    $default_view = 'timelineDay';
    // 'defaultView' => $default_view,

    $values = [
      'bat_timerange_start' => $config->get('bat_fullcalendar_calendar_timerange_start'),
      'bat_timerange_end' => $config->get('bat_fullcalendar_calendar_timerange_end'),
      'calendarHeight' => $config->get("bat_fullcalendar_calendar_height"),
      'defaultView' => $default_view,
      'height' => $config->get('bat_fullcalendar_calendar_height'),
      'initialView' => $config->get('bat_fullcalendar_calendar_view_edit'),
      'schedulerLicenseKey' => $schedulerlicensekey,
      'test_da_bat' => 'OK',
    ];

    $settings['batCalendar'][$id] = array_replace_recursive($values, $fc_setting);
  }
  return $settings;
}


/**
 * Theme function for FullCalendar.
 */
function template_preprocess_bat_fullcalendar(&$variables) {

  $unit_ids = $variables['calendar_settings']['fc_settings']['batCalendar'][0]['unitIds'];

  $calendar_id = $variables['calendar_settings']['calendar_id'];
  $fc_settings = $variables['calendar_settings']['fc_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 more (was: "ones coming from the user").
  // This will scan hooks as well (IE: BEE)
  $settings = bat_fullcalendar_configure($variables['calendar_settings']['fc_settings']);

  // Apr25: Override settings with UI settings.
  $settings = bat_fullcalendar_ui_settings($settings);

  $settings['batCalendar'][0]['id'] = $attributes['id'];

  $attributes = new Attribute($attributes);
  $variables['#attached'] = [
    'library' => [
      // bat_fullcalendar/bat-fullcalendar may be overriden by BEE
      // @todo: check overriding flow in BEE
      'bat_fullcalendar/bat-fullcalendar',
    ],
    '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 string $style
 *   Some comment is required.
 */
function bat_fullcalendar_modal_style($style = 'default') {
  global $base_path;

  // Styles to use for the modal.
  $modal_style = [
    'buttons' => FALSE,
    'modal' => TRUE,
    'width' => 820,
    'height' => 520,
    'title' => t('Loading...'),
    'loading' => '<img class="loading" src="' . $base_path . \Drupal::service('extension.list.module')->getPath('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;
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc