You are here

class AppointmentCalendarListForm in Appointment Calendar 8

Hierarchy

Expanded class hierarchy of AppointmentCalendarListForm

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

File

src/Form/AppointmentCalendarListForm.php, line 11

Namespace

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    global $base_url;

    // Default year.
    $default_year = date('Y', time());
    $beginOfDay = strtotime("midnight", time());
    $endOfDay = strtotime("tomorrow", time()) - 1;
    $from_date = \Drupal::request()->query
      ->get('date');
    $to_date = \Drupal::request()->query
      ->get('todate');
    if ($from_date == '') {
      $from_date = DrupalDateTime::createFromTimestamp($beginOfDay);
    }
    else {
      $from_date = DrupalDateTime::createFromTimestamp($from_date);
    }
    if ($to_date == '') {
      $to_date = DrupalDateTime::createFromTimestamp($endOfDay);
    }
    else {
      $to_date = DrupalDateTime::createFromTimestamp($to_date);
    }
    $form['filter_date'] = [
      '#type' => 'datetime',
      '#title' => t('From date'),
      '#date_date_element' => 'date',
      '#date_time_element' => 'none',
      '#date_year_range' => $default_year . ':+3',
      '#default_value' => $from_date,
    ];
    $form['filter_to_date'] = [
      '#type' => 'datetime',
      '#title' => t('To date'),
      '#date_date_element' => 'date',
      '#date_time_element' => 'none',
      '#date_year_range' => $default_year . ':+3',
      '#default_value' => $to_date,
    ];
    $form['submit'] = [
      '#type' => 'submit',
      '#value' => t('Filter'),
    ];
    $form['reset'] = [
      '#type' => 'submit',
      '#value' => t('Reset'),
    ];

    // Headers.
    $headers = [
      t('Date'),
      t('No. Slots'),
      t('Operations'),
    ];
    $db_conn = \Drupal::database();
    $date_query = $db_conn
      ->select('appointment_date', 'ad');
    $date_query
      ->fields('ad');
    if (\Drupal::request()->query
      ->get('date')) {
      $date_query
        ->condition('date', \Drupal::request()->query
        ->get('date'), '>=');
    }
    if (\Drupal::request()->query
      ->get('todate')) {
      $date_query
        ->condition('date', \Drupal::request()->query
        ->get('todate'), '<=');
    }
    $date_query
      ->orderBy('date');
    $table_sort = $date_query
      ->extend('Drupal\\Core\\Database\\Query\\TableSortExtender')
      ->orderBy('date');
    $pager = $table_sort
      ->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')
      ->limit(25);
    $date_result = $pager
      ->execute();
    $rows = [];
    foreach ($date_result as $date) {
      $capacity = appointment_calendar_slot_capacity($date->date);
      $slots = count((array) json_decode($capacity));
      $view = Html::escape($base_url . '/admin/appointment-calendar/view?date=' . $date->date);
      $edit = Html::escape($base_url . '/admin/appointment-calendar/edit?date=' . $date->date);
      $delete = Html::escape($base_url . '/admin/appointment-calendar/delete?date=' . $date->date);
      $row = [];
      $row[] = date('Y-m-d', $date->date);
      $row[] = $slots;
      $row[] = Markup::create('<div><a href="' . $view . '">' . t('View') . '</a></div><div><a href="' . $edit . '">' . t('Edit') . '</a></div><div><a href="' . $delete . '">' . t('Delete') . '</a></div>');
      $rows[] = $row;
    }
    $form['data'] = [
      '#theme' => 'table',
      '#header' => $headers,
      '#rows' => $rows,
    ];
    $form['pager'] = [
      '#type' => 'pager',
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();
    $op = (string) $values['op'];
    if ($op == $this
      ->t('Filter')) {
      $filter_date = $values['filter_date']
        ->getTimestamp();
      $filter_to_date = $values['filter_to_date']
        ->getTimestamp();
      if ($filter_date > $filter_to_date) {
        $form_state
          ->setErrorByName('filter_date', $this
          ->t('From Date is greater than "TO" date'));
      }
    }
  }

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

    // Goto current path if reset.
    if ($op == $this
      ->t('Reset')) {
      $form_state
        ->setRedirect('appointment_calendar.list_page');
    }

    // Pass values to url.
    if ($op == $this
      ->t('Filter')) {
      $filter_date = $values['filter_date']
        ->getTimestamp();
      $filter_to_date = $values['filter_to_date']
        ->getTimestamp();
      $params['date'] = Html::escape($filter_date);
      $params['todate'] = Html::escape($filter_to_date);
      $form_state
        ->setRedirect('appointment_calendar.list_page', [
        $params,
      ]);
    }
  }

}

Members

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