You are here

class SmartDateProcessor in Smart Date 8.2

Same name and namespace in other branches
  1. 3.x src/Plugin/FullcalendarViewProcessor/SmartDateProcessor.php \Drupal\smart_date\Plugin\FullcalendarViewProcessor\SmartDateProcessor
  2. 3.0.x src/Plugin/FullcalendarViewProcessor/SmartDateProcessor.php \Drupal\smart_date\Plugin\FullcalendarViewProcessor\SmartDateProcessor
  3. 3.1.x src/Plugin/FullcalendarViewProcessor/SmartDateProcessor.php \Drupal\smart_date\Plugin\FullcalendarViewProcessor\SmartDateProcessor
  4. 3.2.x src/Plugin/FullcalendarViewProcessor/SmartDateProcessor.php \Drupal\smart_date\Plugin\FullcalendarViewProcessor\SmartDateProcessor
  5. 3.3.x src/Plugin/FullcalendarViewProcessor/SmartDateProcessor.php \Drupal\smart_date\Plugin\FullcalendarViewProcessor\SmartDateProcessor
  6. 3.4.x src/Plugin/FullcalendarViewProcessor/SmartDateProcessor.php \Drupal\smart_date\Plugin\FullcalendarViewProcessor\SmartDateProcessor

Smart Date plugin.

Plugin annotation


@FullcalendarViewProcessor(
  id = "fullcalendar_view_smart_date",
  label = @Translation("Smart date processor"),
  field_types = {
    "smartdate"
  }
)

Hierarchy

Expanded class hierarchy of SmartDateProcessor

File

src/Plugin/FullcalendarViewProcessor/SmartDateProcessor.php, line 19

Namespace

Drupal\smart_date\Plugin\FullcalendarViewProcessor
View source
class SmartDateProcessor extends FullcalendarViewProcessorBase {

  /**
   * @inheritDoc
   *
   * processing view results of fullcalendar_view for a smart date field
   *
   * ToDo:
   * - handling of drag and drop updates full calendar
   * - timezone handling
   * - smart date recurring events handling (should be roughly working)
   * - fullcalender_view recurring events handling (not considered yet, maybe
   * this is not to be supported by smart date anyway
   */
  public function process(array &$variables) {

    /* @var \Drupal\views\ViewExecutable $view */
    $view = $variables['view'];
    $view_index = key($variables['#attached']['drupalSettings']['fullCalendarView']);
    $fields = $view->field;
    $options = $view->style_plugin->options;
    $start_field = $options['start'];
    $start_field_options = $fields[$start_field]->options;
    $format_label = $start_field_options['settings']['format'];

    // Load the format specified in the View.
    $format = SmartDateTrait::loadSmartDateFormat($format_label);
    $multiple = $fields[$start_field]->multiple;

    // If not a Smart Date field or not existing config, nothing to do.
    if (strpos($start_field_options['type'], 'smartdate') !== 0 || empty($variables['#attached']['drupalSettings']['fullCalendarView'][$view_index]['calendar_options'])) {
      return;
    }
    $calendar_options = json_decode($variables['#attached']['drupalSettings']['fullCalendarView'][$view_index]['calendar_options'], TRUE);
    $entries = $calendar_options['events'];
    $mappings = $this
      ->getIdMappings($entries);
    if ($multiple && $start_field_options['group_rows'] == FALSE) {
      $messenger = \Drupal::messenger();
      $messenger
        ->addMessage('Please group the rows', $messenger::TYPE_WARNING);
    }
    foreach ($view->result as $key => $row) {
      $current_entity = $row->_entity;
      $values = $current_entity
        ->get($start_field)
        ->getValue();
      $row_data = [];
      $row_data['format'] = $format;
      if ($multiple) {
        foreach ($values as $delta => $value) {
          $value['delta'] = $delta;
          $value['id'] = $row->_entity
            ->id();
          $lookup_key = $value['id'] . '-' . $delta;
          $entries_index = $mappings[$lookup_key];
          $this
            ->updateEntry($entries[$entries_index], $value, $format);
        }
      }
      else {
        $values[0]['delta'] = $key;
        $values[0]['id'] = $row->_entity
          ->id();
        $this
          ->updateEntry($entries[$key], $values[0], $format);
      }
    }

    // Update the entries.
    if ($entries) {
      $calendar_options['events'] = $entries;
      $variables['#attached']['drupalSettings']['fullCalendarView'][$view_index]['calendar_options'] = json_encode($calendar_options);
    }
  }

  /**
   * Helper function to extract a simple array mapping ids to their array keys.
   *
   * @param array $entries
   *   The entries as created by Fullcalendar View.
   * @return array $ids
   *   An array to map
   */
  private function getIdMappings(array $entries) {
    $ids = [];
    foreach ($entries as $key => $entry) {
      $id_parts = explode('-', $entry['id']);
      $label = $entry['eid'] . '-' . $id_parts[1];
      $ids[$label] = $key;
    }
    return $ids;
  }

  /**
   * Helper function to update the FCV-created data arrary.
   *
   * @param array $entry
   *   The original data, created by Fullcalendar View.
   * @param array $row_data
   *   Data that will be used to update the calendar entry.
   * @param array $format
   *   Formatting options from the specified Smart Date Format.
   */
  private function updateEntry(array &$entry, array $row_data, array $format) {
    $start = $row_data['value'];
    $end = $row_data['end_value'];
    $timezone = $row_data['timezone'] ?: NULL;
    if (empty($entry) || empty($start) || empty($end)) {
      return FALSE;
    }

    // Check for all day events.
    if (SmartDateTrait::isAllDay($start, $end)) {
      $entry['start'] = date('Y-m-d', $start);

      // The end date is inclusive for a all day event in full calendar,
      // which is not what we want. So we need one day offset.
      $entry['end'] = date('Y-m-d', $end + 60 * 60 * 24);
      $entry['allDay'] = TRUE;
    }
    else {
      $entry['start'] = date(DATE_ATOM, $start);
      $entry['end'] = date(DATE_ATOM, $end);
      $entry['allDay'] = FALSE;
    }

    // Append the id with necessary additional data.
    if (!empty($row_data['rrule'])) {
      $entry['eid'] = $row_data['id'] . '-R-' . $row_data['rrule'] . '-I-' . $row_data['rrule_index'];
    }
    else {
      if (isset($row_data['delta'])) {
        $entry['eid'] = $row_data['id'] . '-D-' . $row_data['delta'];
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
SmartDateProcessor::getIdMappings private function Helper function to extract a simple array mapping ids to their array keys.
SmartDateProcessor::process public function @inheritDoc Overrides FullcalendarViewProcessorBase::process
SmartDateProcessor::updateEntry private function Helper function to update the FCV-created data arrary.