Backup of db before drupal security update
[yaffs-website] / web / core / includes / tablesort.inc
1 <?php
2
3 /**
4  * @file
5  * Functions to aid in the creation of sortable tables.
6  *
7  * All tables created when rendering a '#type' => 'table' have the option of
8  * having column headers that the user can click on to sort the table by that
9  * column.
10  */
11
12 use Drupal\Component\Utility\SafeMarkup;
13 use Drupal\Core\Url;
14 use Drupal\Component\Utility\UrlHelper;
15
16 /**
17  * Initializes the table sort context.
18  */
19 function tablesort_init($header) {
20   $ts = tablesort_get_order($header);
21   $ts['sort'] = tablesort_get_sort($header);
22   $ts['query'] = tablesort_get_query_parameters();
23   return $ts;
24 }
25
26 /**
27  * Formats a column header.
28  *
29  * If the cell in question is the column header for the current sort criterion,
30  * it gets special formatting. All possible sort criteria become links.
31  *
32  * @param string $cell_content
33  *   The cell content to format. Passed by reference.
34  * @param array $cell_attributes
35  *   The cell attributes. Passed by reference.
36  * @param array $header
37  *   An array of column headers in the format described in '#type' => 'table'.
38  * @param array $ts
39  *   The current table sort context as returned from tablesort_init().
40  */
41 function tablesort_header(&$cell_content, array &$cell_attributes, array $header, array $ts) {
42   // Special formatting for the currently sorted column header.
43   if (isset($cell_attributes['field'])) {
44     $title = t('sort by @s', ['@s' => $cell_content]);
45     if ($cell_content == $ts['name']) {
46       // aria-sort is a WAI-ARIA property that indicates if items in a table
47       // or grid are sorted in ascending or descending order. See
48       // http://www.w3.org/TR/wai-aria/states_and_properties#aria-sort
49       $cell_attributes['aria-sort'] = ($ts['sort'] == 'asc') ? 'ascending' : 'descending';
50       $ts['sort'] = (($ts['sort'] == 'asc') ? 'desc' : 'asc');
51       $cell_attributes['class'][] = 'is-active';
52       $tablesort_indicator = [
53         '#theme' => 'tablesort_indicator',
54         '#style' => $ts['sort'],
55       ];
56       $image = drupal_render($tablesort_indicator);
57     }
58     else {
59       // If the user clicks a different header, we want to sort ascending initially.
60       $ts['sort'] = 'asc';
61       $image = '';
62     }
63     $cell_content = \Drupal::l(SafeMarkup::format('@cell_content@image', ['@cell_content' => $cell_content, '@image' => $image]), new Url('<current>', [], [
64       'attributes' => ['title' => $title],
65       'query' => array_merge($ts['query'], [
66         'sort' => $ts['sort'],
67         'order' => $cell_content,
68       ]),
69     ]));
70
71     unset($cell_attributes['field'], $cell_attributes['sort']);
72   }
73 }
74
75 /**
76  * Composes a URL query parameter array for table sorting links.
77  *
78  * @return
79  *   A URL query parameter array that consists of all components of the current
80  *   page request except for those pertaining to table sorting.
81  */
82 function tablesort_get_query_parameters() {
83   return UrlHelper::filterQueryParameters(\Drupal::request()->query->all(), ['sort', 'order']);
84 }
85
86 /**
87  * Determines the current sort criterion.
88  *
89  * @param $headers
90  *   An array of column headers in the format described in '#type' => 'table'.
91  *
92  * @return
93  *   An associative array describing the criterion, containing the keys:
94  *   - "name": The localized title of the table column.
95  *   - "sql": The name of the database field to sort on.
96  */
97 function tablesort_get_order($headers) {
98   $order = \Drupal::request()->query->get('order', '');
99   foreach ($headers as $header) {
100     if (is_array($header)) {
101       if (isset($header['data']) && $order == $header['data']) {
102         $default = $header;
103         break;
104       }
105
106       if (empty($default) && isset($header['sort']) && ($header['sort'] == 'asc' || $header['sort'] == 'desc')) {
107         $default = $header;
108       }
109     }
110   }
111
112   if (!isset($default)) {
113     $default = reset($headers);
114     if (!is_array($default)) {
115       $default = ['data' => $default];
116     }
117   }
118
119   $default += ['data' => NULL, 'field' => NULL];
120   return ['name' => $default['data'], 'sql' => $default['field']];
121 }
122
123 /**
124  * Determines the current sort direction.
125  *
126  * @param $headers
127  *   An array of column headers in the format described in '#type' => 'table'.
128  *
129  * @return
130  *   The current sort direction ("asc" or "desc").
131  */
132 function tablesort_get_sort($headers) {
133   $query = \Drupal::request()->query;
134   if ($query->has('sort')) {
135     return (strtolower($query->get('sort')) == 'desc') ? 'desc' : 'asc';
136   }
137   // The user has not specified a sort. Use the default for the currently sorted
138   // header if specified; otherwise use "asc".
139   else {
140     // Find out which header is currently being sorted.
141     $ts = tablesort_get_order($headers);
142     foreach ($headers as $header) {
143       if (is_array($header) && isset($header['data']) && $header['data'] == $ts['name'] && isset($header['sort'])) {
144         return $header['sort'];
145       }
146     }
147   }
148   return 'asc';
149 }