You are here

class EditRepeatingRuleModalForm in Booking and Availability Management Tools for Drupal 8

Hierarchy

Expanded class hierarchy of EditRepeatingRuleModalForm

1 string reference to 'EditRepeatingRuleModalForm'
bat_event_series.routing.yml in modules/bat_event_series/bat_event_series.routing.yml
modules/bat_event_series/bat_event_series.routing.yml

File

modules/bat_event_series/src/Form/EditRepeatingRuleModalForm.php, line 28
Contains \Drupal\bat_event_series\Form\EditRepeatingRuleModalForm.

Namespace

Drupal\bat_event_series\Form
View source
class EditRepeatingRuleModalForm extends FormBase {

  /**
   * Event series object.
   *
   * @var \Drupal\bat_event_series\Entity\EventSeries
   */
  protected $event_series;

  /**
   * The tempstore object.
   *
   * @var \Drupal\user\SharedTempStore
   */
  protected $tempStore;

  /**
   * The form builder.
   *
   * @var \Drupal\Core\Form\FormBuilder
   */
  protected $formBuilder;

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'edit_repeating_rule_modal_form';
  }

  /**
   * Constructs a new EditRepeatingRuleModalForm object.
   *
   * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory
   *   The tempstore factory.
   */
  public function __construct(PrivateTempStoreFactory $temp_store_factory, FormBuilder $formBuilder) {
    $this->tempStore = $temp_store_factory
      ->get('edit_repeating_rule');
    $this->formBuilder = $formBuilder;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('tempstore.private'), $container
      ->get('form_builder'));
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, EventSeries $bat_event_series = NULL) {
    $this->event_series = $bat_event_series;
    $event_series_type = bat_event_series_type_load($bat_event_series
      ->bundle());
    $start_date = new \DateTime($bat_event_series
      ->get('event_dates')->value);
    $end_date = new \DateTime($bat_event_series
      ->get('event_dates')->end_value);
    $rrule = RfcParser::parseRRule($bat_event_series
      ->getRRule());
    $event_type = $event_series_type
      ->getEventGranularity();
    $form['errors'] = [
      '#markup' => '<div class="form-validation-errors"></div>',
    ];
    $form['start_date'] = [
      '#type' => $event_type == 'bat_daily' ? 'date' : 'datetime',
      '#title' => t('Start date'),
      '#default_value' => $event_type == 'bat_daily' ? $start_date
        ->format('Y-m-d') : new DrupalDateTime($start_date
        ->format('Y-m-d H:00')),
      '#date_increment' => 60,
      '#required' => TRUE,
    ];
    $form['end_date'] = [
      '#type' => $event_series_type
        ->getEventGranularity() == 'bat_daily' ? 'date' : 'datetime',
      '#title' => t('End date'),
      '#default_value' => $event_type == 'bat_daily' ? $end_date
        ->format('Y-m-d') : new DrupalDateTime($end_date
        ->format('Y-m-d H:00')),
      '#date_increment' => 60,
      '#required' => TRUE,
    ];
    $form['repeat_frequency'] = [
      '#type' => 'select',
      '#title' => t('Repeat frequency'),
      '#options' => [
        'daily' => t('Daily'),
        'weekly' => t('Weekly'),
        'monthly' => t('Monthly'),
      ],
      '#default_value' => isset($rrule['FREQ']) ? strtolower($rrule['FREQ']) : '',
      '#required' => TRUE,
    ];
    $form['repeat_until'] = [
      '#type' => 'date',
      '#title' => t('Repeat until'),
      '#default_value' => isset($rrule['UNTIL']) ? $rrule['UNTIL']
        ->format('Y-m-d') : '',
      '#required' => TRUE,
    ];
    $form['actions'] = [
      '#type' => 'actions',
    ];
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Update this event series'),
      '#attributes' => [
        'class' => [
          'button--primary',
        ],
      ],
      '#ajax' => [
        'callback' => [
          $this,
          'ajaxSubmit',
        ],
        'url' => Url::fromRoute('entity.bat_event_series.edit_form_modal', [
          'bat_event_series' => $bat_event_series
            ->id(),
        ]),
        'options' => [
          'query' => [
            FormBuilderInterface::AJAX_FORM_REQUEST => TRUE,
          ],
        ],
      ],
    ];
    $form['actions']['cancel'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Cancel'),
      '#attributes' => [
        'class' => [
          'button--danger',
          'dialog-cancel',
        ],
      ],
    ];
    $form['#attached']['library'][] = 'core/drupal.dialog.ajax';
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();
    $start_date = $values['start_date'];
    $end_date = $values['end_date'];
    $event_series_type = bat_event_series_type_load($this->event_series
      ->bundle());
    $event_type = $event_series_type
      ->getEventGranularity();
    if ($event_type == 'bat_daily') {
      $start_date = new \DateTime($start_date);
      $end_date = new \DateTime($end_date);
    }
    $date_start_date = $start_date
      ->format('Y-m-d');
    $date_end_date = $end_date
      ->format('Y-m-d');
    $dates_valid = TRUE;
    if ($event_type == 'bat_hourly') {

      // Validate the input dates.
      if (!$start_date instanceof DrupalDateTime) {
        $form_state
          ->setErrorByName('start_date', $this
          ->t('The start date is not valid.'));
        $dates_valid = FALSE;
      }
      if (!$end_date instanceof DrupalDateTime) {
        $form_state
          ->setErrorByName('end_date', $this
          ->t('The end date is not valid.'));
        $dates_valid = FALSE;
      }
    }
    if ($dates_valid) {
      if ($end_date <= $start_date) {
        $form_state
          ->setErrorByName('end_date', $this
          ->t('End date must be after the start date.'));
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $values = [
      'start_date' => $form_state
        ->getValue('start_date'),
      'end_date' => $form_state
        ->getValue('end_date'),
      'repeat_frequency' => $form_state
        ->getValue('repeat_frequency'),
      'repeat_until' => $form_state
        ->getValue('repeat_until'),
    ];
    $this->tempStore
      ->set($this
      ->currentUser()
      ->id(), $values);
  }
  public function ajaxSubmit(array &$form, FormStateInterface $form_state) {
    $response = new AjaxResponse();
    $messages = [
      '#type' => 'status_messages',
    ];
    $response
      ->addCommand(new HtmlCommand('.form-validation-errors', $messages));
    if (!$form_state
      ->getErrors()) {
      $response
        ->addCommand(new CloseModalDialogCommand());
      $modal_form = $this->formBuilder
        ->getForm('Drupal\\bat_event_series\\Form\\EditRepeatingRuleConfirmationModalForm', $this->event_series);
      $modal_form['#attached']['library'][] = 'core/drupal.dialog.ajax';
      $response
        ->addCommand(new OpenModalDialogCommand($this
        ->t('Edit repeating rule'), $modal_form, [
        'width' => 600,
      ]));
    }
    return $response;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EditRepeatingRuleModalForm::$event_series protected property Event series object.
EditRepeatingRuleModalForm::$formBuilder protected property The form builder.
EditRepeatingRuleModalForm::$tempStore protected property The tempstore object.
EditRepeatingRuleModalForm::ajaxSubmit public function
EditRepeatingRuleModalForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
EditRepeatingRuleModalForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
EditRepeatingRuleModalForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
EditRepeatingRuleModalForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
EditRepeatingRuleModalForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
EditRepeatingRuleModalForm::__construct public function Constructs a new EditRepeatingRuleModalForm object.
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.