You are here

public function DateRecurModularSierraModalOccurrencesForm::ajaxSubmitForm in Recurring Date Field Modular Widgets 2.x

Same name and namespace in other branches
  1. 8 src/Form/DateRecurModularSierraModalOccurrencesForm.php \Drupal\date_recur_modular\Form\DateRecurModularSierraModalOccurrencesForm::ajaxSubmitForm()
  2. 3.x src/Form/DateRecurModularSierraModalOccurrencesForm.php \Drupal\date_recur_modular\Form\DateRecurModularSierraModalOccurrencesForm::ajaxSubmitForm()

Callback to submit modal modified exclusions.

File

src/Form/DateRecurModularSierraModalOccurrencesForm.php, line 352

Class

DateRecurModularSierraModalOccurrencesForm
Generate a form to excluding occurrences, designed for display in modal.

Namespace

Drupal\date_recur_modular\Form

Code

public function ajaxSubmitForm(array &$form, FormStateInterface $form_state) {
  $response = new AjaxResponse();
  if ($form_state
    ->getErrors()) {

    // Inspired by \Drupal\form_api_example\Form\ModalForm::ajaxSubmitForm.
    $form['status_messages'] = [
      '#type' => 'status_messages',
    ];

    // Open the form again as a modal.
    return $response
      ->addCommand(new OpenModalDialogCommand($this
      ->t('Errors'), $form, [
      'width' => '575',
    ]));
  }
  $originalString = $form_state
    ->getValue('original_string');

  /** @var \DateTime $dtStart */
  $dtStart = $form_state
    ->getValue('date_start');
  try {
    $helper = DateRecurHelper::create($originalString, $dtStart);
  } catch (\Exception $e) {
  }

  // Rebuild original set without EXDATES.
  $rset = new RSet();
  if (isset($helper)) {
    array_walk($helper
      ->getRules(), function (DateRecurRuleInterface $rule) use ($rset) {
      $parts = $rule
        ->getParts();
      unset($parts['DTSTART']);
      $rset
        ->addRRule($parts);
    });
  }

  // Add checked excluded dates.
  foreach ($form_state
    ->getValue('table') as $i => $row) {
    if ($row['exclude'] !== 0) {
      $date = $form['occurrences']['table'][$i]['#date_object'];
      $rset
        ->addExDate($date);
    }
  }

  // Add out of range excluded dates.
  foreach ($form_state
    ->getValue('excludes_out_of_limit') as $exDate) {

    /** @var \DateTime $exDate */
    $rset
      ->addExDate($exDate);
  }
  $lines = [];
  foreach ($rset
    ->getRRules() as $rule) {

    /** @var \RRule\RRule $rule */
    $lines[] = 'RRULE:' . $rule
      ->rfcString(FALSE);
  }
  $utc = new \DateTimeZone('UTC');
  $exDates = array_map(function (\DateTime $exDate) use ($utc) {
    $exDate
      ->setTimezone($utc);
    return $exDate
      ->format(static::UTC_FORMAT);
  }, $rset
    ->getExDates());
  if (count($exDates) > 0) {
    $lines[] = 'EXDATE:' . implode(',', $exDates);
  }
  $collection = $this->tempStoreFactory
    ->get(DateRecurModularSierraWidget::COLLECTION_MODAL_STATE);
  $collection
    ->set(DateRecurModularSierraWidget::COLLECTION_MODAL_STATE_KEY, implode("\n", $lines));
  $refreshBtnName = sprintf('[name="%s"]', $collection
    ->get(DateRecurModularSierraWidget::COLLECTION_MODAL_STATE_REFRESH_BUTTON));
  $response
    ->addCommand(new CloseDialogCommand())
    ->addCommand(new InvokeCommand($refreshBtnName, 'trigger', [
    'click',
  ]));
  return $response;
}