Lines: 100.00%
| Line | Hits | Source |
|---|---|---|
| 1 | <?php | |
| 2 | ||
| 3 | namespace Drupal\visitors\Plugin\views\sort; | |
| 4 | ||
| 5 | use Drupal\Core\Form\FormStateInterface; | |
| 6 | use Drupal\views\Attribute\ViewsSort; | |
| 7 | use Drupal\views\Plugin\views\sort\SortPluginBase; | |
| 8 | use Drupal\visitors\Service\SequenceService; | |
| 9 | ||
| 10 | /** | |
| 11 | * Sort handler that uses defined number ranges instead of raw values. | |
| 12 | */ | |
| 13 | #[ViewsSort("visitors_number_range")] | |
| 14 | class NumberRange extends SortPluginBase { | |
| 15 | ||
| 16 | /** | |
| 17 | * Gets the total count of configured ranges. | |
| 18 | * | |
| 19 | * @return int | |
| 20 | * The total number of ranges. | |
| 21 | */ | |
| 22 | public function getTotalRanges(): int { | |
| 23 | 12 | $ranges_text = $this->options['ranges']; |
| 24 | 12 | $ranges = preg_split('/\r\n|\r|\n/', $ranges_text); |
| 25 | 12 | $ranges = array_map('trim', $ranges); |
| 26 | 12 | $ranges = array_filter($ranges, function ($range) { |
| 27 | 12 | return trim($range) !== ''; |
| 28 | 12 | }); |
| 29 | ||
| 30 | 12 | return count($ranges); |
| 31 | } | |
| 32 | ||
| 33 | /** | |
| 34 | * {@inheritdoc} | |
| 35 | */ | |
| 36 | public function defineOptions() { | |
| 37 | 11 | $options = parent::defineOptions(); |
| 38 | 11 | $options['ranges'] = ['default' => "0\n1\n2\n3\n4\n5\n6-7\n8-10\n11-14\n15-20\n21+"]; |
| 39 | 11 | return $options; |
| 40 | } | |
| 41 | ||
| 42 | /** | |
| 43 | * {@inheritdoc} | |
| 44 | */ | |
| 45 | public function buildOptionsForm(&$form, FormStateInterface $form_state) { | |
| 46 | 1 | parent::buildOptionsForm($form, $form_state); |
| 47 | ||
| 48 | 1 | $form['ranges'] = [ |
| 49 | 1 | '#type' => 'textarea', |
| 50 | 1 | '#title' => $this->t('Number ranges'), |
| 51 | 1 | '#description' => $this->t("Enter one range per line. Examples: <code>0</code>, <code>6-7</code>, <code>21+</code>. The sort order will follow the order you define here."), |
| 52 | 1 | '#default_value' => $this->options['ranges'], |
| 53 | 1 | ]; |
| 54 | } | |
| 55 | ||
| 56 | /** | |
| 57 | * {@inheritdoc} | |
| 58 | */ | |
| 59 | public function query() { | |
| 60 | // Add the base field to the query. | |
| 61 | 7 | $this->ensureMyTable(); |
| 62 | ||
| 63 | // Check if the field already exists in the query. | |
| 64 | 7 | $existing_field_alias = $this->tableAlias . '_' . $this->realField . '__range'; |
| 65 | ||
| 66 | // Look through the query fields to see if our target field already exists. | |
| 67 | 7 | foreach ($this->query->fields as $field_info) { |
| 68 | 6 | if (isset($field_info['alias']) && $field_info['alias'] === $existing_field_alias) { |
| 69 | // Field already exists, use it for sorting. | |
| 70 | 2 | $this->query->addOrderBy(NULL, NULL, $this->options['order'], $existing_field_alias); |
| 71 | 2 | return; |
| 72 | } | |
| 73 | } | |
| 74 | ||
| 75 | // Field doesn't exist, create our own CASE expression. | |
| 76 | 5 | $field = "$this->tableAlias.$this->realField"; |
| 77 | ||
| 78 | 5 | $ranges_text = $this->options['ranges']; |
| 79 | 5 | $ranges = preg_split('/\r\n|\r|\n/', $ranges_text); |
| 80 | 5 | $ranges = array_map('trim', $ranges); |
| 81 | 5 | $ranges = array_filter($ranges, function ($range) { |
| 82 | 5 | return trim($range) !== ''; |
| 83 | 5 | }); |
| 84 | ||
| 85 | 5 | $case_sql = []; |
| 86 | $i = 0; | |
| 87 | 5 | foreach ($ranges as $range) { |
| 88 | // Range "X-Y". | |
| 89 | 4 | if (preg_match('/^(\d+)\-(\d+)$/', $range, $m)) { |
| 90 | 3 | $case_sql[] = "WHEN $field BETWEEN {$m[1]} AND {$m[2]} THEN $i"; |
| 91 | } | |
| 92 | // Range "X+". | |
| 93 | 4 | elseif (preg_match('/^(\d+)\+$/', $range, $m)) { |
| 94 | 3 | $case_sql[] = "WHEN $field >= {$m[1]} THEN $i"; |
| 95 | } | |
| 96 | // Exact number. | |
| 97 | 4 | elseif (is_numeric($range)) { |
| 98 | 4 | $case_sql[] = "WHEN $field = {$range} THEN $i"; |
| 99 | } | |
| 100 | } | |
| 101 | ||
| 102 | 5 | $case_expression = "CASE " . implode(' ', $case_sql) . " ELSE 9999 END"; |
| 103 | ||
| 104 | // Tell Views how to sort. | |
| 105 | 5 | $this->query->addOrderBy(NULL, $case_expression, $this->options['order'], $existing_field_alias); |
| 106 | } | |
| 107 | ||
| 108 | /** | |
| 109 | * {@inheritdoc} | |
| 110 | */ | |
| 111 | public function postExecute(&$values) { | |
| 112 | 10 | $values = SequenceService::integer($values, $this->tableAlias . '_' . $this->realField . '__range', $this->getTotalRanges()); |
| 113 | } | |
| 114 | ||
| 115 | } |