You are here

class BookingConfirmationForm in Booking and Availability Management Tools for Drupal 8

Hierarchy

Expanded class hierarchy of BookingConfirmationForm

File

modules/bat_booking/bat_booking_example/src/Form/BookingConfirmationForm.php, line 21
Contains \Drupal\bat_booking_example\Form\BookingConfirmationForm.

Namespace

Drupal\bat_booking_example\Form
View source
class BookingConfirmationForm extends FormBase {

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, $start_date = NULL, $end_date = NULL, $type_id = NULL) {
    $form['start_date'] = [
      '#type' => 'hidden',
      '#value' => $start_date
        ->format('Y-m-d'),
    ];
    $form['end_date'] = [
      '#type' => 'hidden',
      '#value' => $end_date
        ->format('Y-m-d'),
    ];
    $form['type_id'] = [
      '#type' => 'hidden',
      '#value' => $type_id,
    ];
    $form['submit'] = [
      '#type' => 'submit',
      '#value' => 'Confirm booking',
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();
    $event_type = 'availability_example';
    $start_date = new \DateTime($values['start_date']);
    $end_date = new \DateTime($values['end_date']);
    $end_date
      ->sub(new \DateInterval('PT1M'));
    $type_id = $values['type_id'];
    $state_ids = array_keys(bat_event_get_states($event_type));
    $state_store = new DrupalDBStore($event_type, DrupalDBStore::BAT_STATE);
    $valid_states = array_merge([
      0,
    ], array_slice($state_ids, 0, 1));
    $drupal_units = bat_unit_load_multiple(FALSE, [
      'unit_type_id' => $type_id,
    ]);
    $bat_units = [];
    foreach ($drupal_units as $unit_id => $unit) {
      $bat_units[] = new Unit($unit_id, $unit
        ->getEventDefaultValue($event_type));
    }
    if (count($bat_units)) {
      $calendar = new Calendar($bat_units, $state_store);
      $response = $calendar
        ->getMatchingUnits($start_date, $end_date, $valid_states, []);
      $valid_unit_ids = array_keys($response
        ->getIncluded());
      if (count($valid_unit_ids)) {

        // Create a new Event.
        $event = bat_event_create([
          'type' => $event_type,
          'start_date' => $start_date
            ->format('Y-m-d H:i:s'),
          'end_date' => $end_date
            ->format('Y-m-d H:i:s'),
          'uid' => $this
            ->currentUser()
            ->id(),
        ]);
        $event
          ->set('event_bat_unit_reference', reset($valid_unit_ids));
        $event
          ->set('event_state_reference', end($state_ids));
        $event
          ->save();

        // Create a new Booking.
        $booking = bat_booking_create([
          'type' => 'standard',
          'label' => 'Example Booking',
        ]);
        $booking
          ->set('booking_start_date', $start_date
          ->format('Y-m-d H:i:s'));
        $booking
          ->set('booking_end_date', $end_date
          ->format('Y-m-d H:i:s'));
        $booking
          ->set('booking_event_reference', $event
          ->id());
        $booking
          ->save();
        $this
          ->messenger()
          ->addMessage(t('Booking created'));
      }
      else {
        $this
          ->messenger()
          ->addError(t('No units'));
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BookingConfirmationForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
BookingConfirmationForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
BookingConfirmationForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
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
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::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 87
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.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
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.