Lines: 100.00%
| Line | Hits | Source |
|---|---|---|
| 1 | <?php | |
| 2 | ||
| 3 | namespace Drupal\visitors\Form; | |
| 4 | ||
| 5 | use Drupal\Core\Datetime\DateFormatterInterface; | |
| 6 | use Drupal\Core\Form\FormBase; | |
| 7 | use Drupal\Core\Form\FormStateInterface; | |
| 8 | use Drupal\visitors\Service\DateRangeService; | |
| 9 | use Symfony\Component\DependencyInjection\ContainerInterface; | |
| 10 | ||
| 11 | /** | |
| 12 | * Date Filter form. | |
| 13 | */ | |
| 14 | class DateFilterForm extends FormBase { | |
| 15 | ||
| 16 | ||
| 17 | /** | |
| 18 | * The date service. | |
| 19 | * | |
| 20 | * @var \Drupal\Core\Datetime\DateFormatterInterface | |
| 21 | */ | |
| 22 | protected $dateFormatter; | |
| 23 | ||
| 24 | /** | |
| 25 | * The date range service. | |
| 26 | * | |
| 27 | * @var \Drupal\visitors\Service\DateRangeService | |
| 28 | */ | |
| 29 | protected $dateRangeService; | |
| 30 | ||
| 31 | /** | |
| 32 | * Date Filter Form constructor. | |
| 33 | * | |
| 34 | * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter | |
| 35 | * The date formatter service. | |
| 36 | * @param \Drupal\visitors\Service\DateRangeService $date_range_service | |
| 37 | * The date range service. | |
| 38 | */ | |
| 39 | public function __construct(DateFormatterInterface $date_formatter, DateRangeService $date_range_service) { | |
| 40 | 5 | $this->dateFormatter = $date_formatter; |
| 41 | 5 | $this->dateRangeService = $date_range_service; |
| 42 | } | |
| 43 | ||
| 44 | /** | |
| 45 | * {@inheritdoc} | |
| 46 | */ | |
| 47 | public static function create(ContainerInterface $container): DateFilterForm { | |
| 48 | 5 | return new static( |
| 49 | 5 | $container->get('date.formatter'), |
| 50 | 5 | $container->get('visitors.date_range') |
| 51 | 5 | ); |
| 52 | } | |
| 53 | ||
| 54 | /** | |
| 55 | * {@inheritdoc} | |
| 56 | */ | |
| 57 | public function getFormId() { | |
| 58 | 2 | return 'visitors_date_filter_form'; |
| 59 | } | |
| 60 | ||
| 61 | /** | |
| 62 | * {@inheritdoc} | |
| 63 | */ | |
| 64 | public function buildForm(array $form, FormStateInterface $form_state) { | |
| 65 | ||
| 66 | 2 | $form = []; |
| 67 | ||
| 68 | 2 | $form['visitors_date_filter'] = [ |
| 69 | 2 | '#collapsible' => TRUE, |
| 70 | 2 | '#title' => $this->dateRangeService->getSummary(), |
| 71 | 2 | '#type' => 'details', |
| 72 | 2 | '#open' => FALSE, |
| 73 | 2 | ]; |
| 74 | ||
| 75 | 2 | $form['visitors_date_filter']['from'] = [ |
| 76 | 2 | '#title' => $this->t('From'), |
| 77 | 2 | '#type' => 'date', |
| 78 | 2 | '#default_value' => $this->dateRangeService->getStartDate(), |
| 79 | 2 | ]; |
| 80 | ||
| 81 | 2 | $form['visitors_date_filter']['to'] = [ |
| 82 | 2 | '#title' => $this->t('To'), |
| 83 | 2 | '#type' => 'date', |
| 84 | 2 | '#default_value' => $this->dateRangeService->getEndDate(), |
| 85 | 2 | '#states' => [ |
| 86 | 2 | 'visible' => [ |
| 87 | 2 | ':input[name="period"]' => ['value' => 'range'], |
| 88 | 2 | ], |
| 89 | 2 | ], |
| 90 | 2 | ]; |
| 91 | ||
| 92 | 2 | $form['visitors_date_filter']['period'] = [ |
| 93 | 2 | '#type' => 'radios', |
| 94 | 2 | '#title' => $this->t('Period'), |
| 95 | 2 | '#options' => [ |
| 96 | 2 | 'day' => $this->t('Day'), |
| 97 | 2 | 'week' => $this->t('Week'), |
| 98 | 2 | 'month' => $this->t('Month'), |
| 99 | 2 | 'year' => $this->t('Year'), |
| 100 | 2 | 'range' => $this->t('Range'), |
| 101 | 2 | ], |
| 102 | 2 | '#default_value' => $this->dateRangeService->getPeriod(), |
| 103 | 2 | ]; |
| 104 | ||
| 105 | 2 | $form['visitors_date_filter']['submit'] = [ |
| 106 | 2 | '#type' => 'submit', |
| 107 | 2 | '#button_type' => 'primary', |
| 108 | 2 | '#value' => $this->t('Apply'), |
| 109 | 2 | ]; |
| 110 | ||
| 111 | 2 | return $form; |
| 112 | } | |
| 113 | ||
| 114 | /** | |
| 115 | * {@inheritdoc} | |
| 116 | */ | |
| 117 | public function submitForm(array &$form, FormStateInterface $form_state) { | |
| 118 | 1 | $period = $form_state->getValue('period'); |
| 119 | 1 | $from = $form_state->getValue('from'); |
| 120 | 1 | $to = $form_state->getValue('to'); |
| 121 | ||
| 122 | 1 | $this->dateRangeService->setPeriodAndDates($period, $from, $to); |
| 123 | } | |
| 124 | ||
| 125 | } |