Backup of db before drupal security update
[yaffs-website] / web / core / lib / Drupal / Core / Database / Query / TableSortExtender.php
1 <?php
2
3 namespace Drupal\Core\Database\Query;
4
5 use Drupal\Core\Database\Connection;
6
7 /**
8  * Query extender class for tablesort queries.
9  */
10 class TableSortExtender extends SelectExtender {
11
12   /**
13    * The array of fields that can be sorted by.
14    */
15   protected $header = [];
16
17   public function __construct(SelectInterface $query, Connection $connection) {
18     parent::__construct($query, $connection);
19
20     // Add convenience tag to mark that this is an extended query. We have to
21     // do this in the constructor to ensure that it is set before preExecute()
22     // gets called.
23     $this->addTag('tablesort');
24   }
25
26   /**
27    * Order the query based on a header array.
28    *
29    * @param array $header
30    *   Table header array.
31    *
32    * @return \Drupal\Core\Database\Query\SelectInterface
33    *   The called object.
34    *
35    * @see table.html.twig
36    */
37   public function orderByHeader(array $header) {
38     $this->header = $header;
39     $ts = $this->init();
40     if (!empty($ts['sql'])) {
41       // Based on code from db_escape_table(), but this can also contain a dot.
42       $field = preg_replace('/[^A-Za-z0-9_.]+/', '', $ts['sql']);
43
44       // orderBy() will ensure that only ASC/DESC values are accepted, so we
45       // don't need to sanitize that here.
46       $this->orderBy($field, $ts['sort']);
47     }
48     return $this;
49   }
50
51   /**
52    * Initialize the table sort context.
53    */
54   protected function init() {
55     $ts = $this->order();
56     $ts['sort'] = $this->getSort();
57     $ts['query'] = $this->getQueryParameters();
58     return $ts;
59   }
60
61   /**
62    * Determine the current sort direction.
63    *
64    * @return
65    *   The current sort direction ("asc" or "desc").
66    *
67    * @see tablesort_get_sort()
68    */
69   protected function getSort() {
70     return tablesort_get_sort($this->header);
71   }
72
73   /**
74    * Compose a URL query parameter array to append to table sorting requests.
75    *
76    * @return
77    *   A URL query parameter array that consists of all components of the current
78    *   page request except for those pertaining to table sorting.
79    *
80    * @see tablesort_get_query_parameters()
81    */
82   protected function getQueryParameters() {
83     return tablesort_get_query_parameters();
84   }
85
86   /**
87    * Determine the current sort criterion.
88    *
89    * @return
90    *   An associative array describing the criterion, containing the keys:
91    *   - "name": The localized title of the table column.
92    *   - "sql": The name of the database field to sort on.
93    *
94    * @see tablesort_get_order()
95    */
96   protected function order() {
97     return tablesort_get_order($this->header);
98   }
99
100 }