You are here

class AppointmentCalendarEditForm in Appointment Calendar 8

Hierarchy

Expanded class hierarchy of AppointmentCalendarEditForm

1 string reference to 'AppointmentCalendarEditForm'
appointment_calendar.routing.yml in ./appointment_calendar.routing.yml
appointment_calendar.routing.yml

File

src/Form/AppointmentCalendarEditForm.php, line 10

Namespace

Drupal\appointment_calendar\Form
View source
class AppointmentCalendarEditForm extends FormBase {

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $from_date = \Drupal::request()->query
      ->get('date');

    // Date edit page.
    if ($from_date != '') {
      $form['appointment_slot_date'] = [
        '#type' => 'datetime',
        '#title' => $this
          ->t('Date'),
        '#date_date_element' => 'date',
        '#date_time_element' => 'none',
        '#default_value' => DrupalDateTime::createFromTimestamp($from_date),
        '#disabled' => TRUE,
      ];

      // Fetching Slot previous capacity filled.
      $capacity = appointment_calendar_slot_capacity($from_date);
      if ($capacity) {
        $i = 1;

        // Show slots and capacity.
        foreach (json_decode($capacity) as $key => $value) {

          // Check if any appointment booked.
          $slot_check = appointment_calendar_slot_capacity_value($from_date, $key);
          $form['time_slot_' . $i] = [
            '#type' => 'textfield',
            '#title' => Html::escape('Time Slot ' . $i . ' :'),
            '#description' => t('Ex: 10:00-11:00, 13:00-14:00, etc.,'),
            '#default_value' => $key,
            '#prefix' => '<div class="time-slot-field-form">',
          ];
          if ($slot_check > 0) {
            $form['time_slot_' . $i]['#disabled'] = TRUE;
            $form['time_slot_' . $i]['#description'] = t('<b>Slot :i </b>booked atleast once', [
              ':i' => $i,
            ]);
          }
          $form['time_slot_' . $i . '_capacity'] = [
            '#type' => 'textfield',
            '#title' => Html::escape('Slot ' . $i . ' Capacity'),
            '#description' => t('Only Numeric'),
            '#default_value' => $value,
            '#suffix' => '</div>',
          ];
          $i++;
        }
      }
      $form['appointment_slot'] = [
        '#type' => 'textfield',
        '#title' => $this
          ->t('No of Extra Slots:'),
      ];
      $values = $form_state
        ->getValues();

      // Display Extra slots.
      if (!empty($values)) {
        $extra_slots = $values['appointment_slot'];
        $extra_slots += $i - 1;
        for ($j = $i; $j <= $extra_slots; $j++) {
          $form['slots']['time_slot_' . $j] = [
            '#type' => 'textfield',
            '#title' => Html::escape('Time Slot ' . $j . ' :'),
            '#description' => t('Ex: 10:00-11:00, 13:00-14:00, etc.,'),
            '#default_value' => '',
            '#prefix' => '<div class="time-slot-field-form">',
          ];
          $form['slots']['time_slot_' . $j . '_capacity'] = [
            '#type' => 'textfield',
            '#title' => Html::escape('Slot ' . $j . ' Capacity'),
            '#description' => t('Only Numeric'),
            '#default_value' => '',
            '#suffix' => '</div>',
          ];
        }
        $j++;
      }
      $form['add_more'] = [
        '#type' => 'submit',
        '#value' => t('Add More Slots'),
      ];
      $form['submit'] = [
        '#type' => 'submit',
        '#value' => t('Submit'),
      ];
      return $form;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();
    $op = (string) $values['op'];
    if ($op == $this
      ->t('Submit')) {
      $date = $values['appointment_slot_date']
        ->getTimestamp();
      $capacity = appointment_calendar_slot_capacity($date);
      $slots = count((array) json_decode($capacity));
      if (!empty($values['appointment_slot'])) {
        $slots += $values['appointment_slot'];
      }

      // Time slot and Capacity Validation.
      for ($i = 1; $i <= $slots; $i++) {
        $booked_capacity = '';
        $time_slot = $values['time_slot_' . $i];
        $time_capacity = $values['time_slot_' . $i . '_capacity'];
        $regex = '/^(?:[01][0-9]|2[0-3]):[0-5][0-9]-(?:[01][0-9]|2[0-3]):[0-5][0-9]$/';

        // Timeslot.
        if (!preg_match($regex, $time_slot)) {
          $form_state
            ->setErrorByName('time_slot_' . $i, t('Time slot should be in between 00:00-23:59 (in between 24 hrs)'));
        }

        // Slot Capacity.
        if ($time_capacity < 0) {
          $form_state
            ->setErrorByName('time_slot_' . $i . '_capacity', t('Slot Capacity should be greater than 0'));
        }
        $booked_capacity = appointment_calendar_slot_capacity_value($date, $time_slot);
        if ($time_capacity < $booked_capacity) {
          $form_state
            ->setErrorByName('time_slot_' . $i, t('Already :slots Appointment(s) booked in this Slot. So it should not be less than :slots', [
            ':slots' => $booked_capacity,
          ]));
        }

        // Checking duplicate slots.
        $time_slots_check[] = $time_slot;
        $vals = array_count_values($time_slots_check);
        if ($vals[$time_slot] > 1) {
          $form_state
            ->setErrorByName('time_slot_' . $i, t('Time slot cannot redeclare twice or more.'));
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();
    $op = (string) $values['op'];
    if ($op == $this
      ->t('Add More Slots')) {
      $form_state
        ->setRebuild();
    }

    // Pass values to url.
    if ($op == $this
      ->t('Submit')) {
      $date = $values['appointment_slot_date']
        ->getTimestamp();
      $capacity = appointment_calendar_slot_capacity($date);
      $slots = count((array) json_decode($capacity));
      if (!empty($values['appointment_slot'])) {
        $slots += $values['appointment_slot'];
      }

      // Fetching Timeslot and capacity values.
      for ($i = 1; $i <= $slots; $i++) {
        $time_slot = $values['time_slot_' . $i];
        $time_capacity = $values['time_slot_' . $i . '_capacity'];
        $slots_save[$time_slot] = $time_slot;
        $slots_capacity[$time_slot] = $time_capacity;
      }

      // Saving date with time slots and capacity.
      $db_conn = \Drupal::database();
      $db_conn
        ->merge('appointment_date')
        ->key([
        'date' => $date,
      ])
        ->fields([
        'no_slots' => $slots,
        'slot_values' => json_encode($slots_save),
        'slot_capacity' => json_encode($slots_capacity),
      ])
        ->execute();
      $this
        ->messenger()
        ->addStatus(t('Changes made successfully'));
      $form_state
        ->setRedirect('appointment_calendar.list_page');
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AppointmentCalendarEditForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
AppointmentCalendarEditForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
AppointmentCalendarEditForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
AppointmentCalendarEditForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
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.
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.