PaymentRequestCorrectionDeadlineFilter.php

PHOTO EMBED

Mon Oct 03 2022 21:43:15 GMT+0000 (Coordinated Universal Time)

Saved by @igor #drupal #mysql

<?php

namespace Drupal\cfp_payment_request\Plugin\views\filter;

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\views\Plugin\views\filter\FilterPluginBase;

/**
 * Filter PR view by correction deadline date.
 *
 * @ingroup views_filter_handlers
 *
 * @ViewsFilter("pr_correction_deadline")
 */
class PaymentRequestCorrectionDeadlineFilter extends FilterPluginBase {

  use StringTranslationTrait;

  /**
   * {@inheritDoc}
   */
  public function acceptExposedInput($input) {
    return TRUE;
  }

  /**
   * {@inheritDoc}
   */
  public function buildExposedForm(&$form, FormStateInterface $form_state) {
    if (empty($this->options['exposed'])) {
      return;
    }

    $identifier = $this->options['expose']['identifier'];
    $exposed_input = $this->view->getExposedInput();

    $form[$identifier] = [
      '#type' => 'fieldset',
      '#title' => $this->t('Correction deadline'),
    ];
    $form[$identifier]['start_date'] = [
      '#type' => 'date',
      '#title' => $this->t('Start date'),
      '#default_value' => $exposed_input['start_date'] ?? NULL,
    ];
    $form[$identifier]['end_date'] = [
      '#type' => 'date',
      '#title' => $this->t('End date'),
      '#default_value' => $exposed_input['end_date'] ?? NULL,
    ];

    return $form;
  }

  /**
   * {@inheritDoc}
   */
  public function query() {
    $this->ensureMyTable();

    /** @var \Drupal\views\Plugin\views\query\Sql $query */
    $query = $this->query;

    $query->addTable("cfp_payment_request__transition_history");
    $input = $this->view->getExposedInput();
    $start = isset($input['start_date']) ? strtotime($input['start_date']) : NULL;
    if ($start) {
      $query->addWhere($this->options['group'], "cfp_payment_request__transition_history.transition_history_deadline", $start, ">=");
    }
    $end = isset($input['end_date']) ? strtotime($input['end_date']) : NULL;
    if ($end) {
      $query->addWhere($this->options['group'], "cfp_payment_request__transition_history.transition_history_deadline", $end, "<=");
    }
  }

}
content_copyCOPY