Elgg  Version 6.1
FilterSortItems.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Menus;
4 
6 
13 
21  public static function registerTimeCreatedSorting(\Elgg\Event $event): ?MenuItems {
22 
23  if (!(bool) $event->getParam('filter_sorting', true)) {
24  // sorting is disabled for this menu
25  return null;
26  }
27 
28  /* @var $result MenuItems */
29  $result = $event->getValue();
30 
32  'name' => 'sort:time_created:desc',
33  'icon' => 'sort-numeric-down-alt',
34  'text' => elgg_echo('sort:newest'),
36  'sort_by' => [
37  'property' => 'time_created',
38  'property_type' => 'attribute',
39  'direction' => 'desc',
40  ],
41  ]),
42  'parent_name' => 'sort:parent',
43  'priority' => 100,
44  ]);
45 
47  'name' => 'sort:time_created:asc',
48  'icon' => 'sort-numeric-down',
49  'text' => elgg_echo('sort:oldest'),
51  'sort_by' => [
52  'property' => 'time_created',
53  'property_type' => 'attribute',
54  'direction' => 'asc',
55  ],
56  ]),
57  'parent_name' => 'sort:parent',
58  'priority' => 110,
59  ]);
60 
61  return $result;
62  }
63 
71  public static function registerLastActionSorting(\Elgg\Event $event): ?MenuItems {
72 
73  if (!(bool) $event->getParam('filter_sorting', true)) {
74  // sorting is disabled for this menu
75  return null;
76  }
77 
78  /* @var $result MenuItems */
79  $result = $event->getValue();
80 
82  'name' => 'sort:last_action:desc',
83  'icon' => 'sort-numeric-down-alt',
84  'text' => elgg_echo('table_columns:fromView:last_action'),
86  'sort_by' => [
87  'property' => 'last_action',
88  'property_type' => 'attribute',
89  'direction' => 'desc',
90  ],
91  ]),
92  'parent_name' => 'sort:parent',
93  'priority' => 115,
94  ]);
95 
96  return $result;
97  }
98 
106  public static function registerLastLoginSorting(\Elgg\Event $event): ?MenuItems {
107 
108  if (!(bool) $event->getParam('filter_sorting', true)) {
109  // sorting is disabled for this menu
110  return null;
111  }
112 
113  /* @var $result MenuItems */
114  $result = $event->getValue();
115 
117  'name' => 'sort:last_login:asc',
118  'icon' => 'sort-numeric-down',
119  'text' => elgg_echo('table_columns:fromView:last_login'),
121  'sort_by' => [
122  'property' => 'last_login',
123  'direction' => 'asc',
124  ],
125  ]),
126  'parent_name' => 'sort:parent',
127  'priority' => 116,
128  ]);
129 
130  return $result;
131  }
132 
140  public static function registerNameSorting(\Elgg\Event $event): ?MenuItems {
141 
142  if (!(bool) $event->getParam('filter_sorting', true)) {
143  // sorting is disabled for this menu
144  return null;
145  }
146 
147  /* @var $result MenuItems */
148  $result = $event->getValue();
149 
151  'name' => 'sort:name:asc',
152  'icon' => 'sort-alpha-down',
153  'text' => elgg_echo('sort:az', [elgg_echo('table_columns:fromProperty:name')]),
155  'sort_by' => [
156  'property' => 'name',
157  'property_type' => 'metadata',
158  'direction' => 'asc',
159  ],
160  ]),
161  'title' => elgg_echo('sort:alpha'),
162  'parent_name' => 'sort:parent',
163  'priority' => 120,
164  ]);
165 
167  'name' => 'sort:name:desc',
168  'icon' => 'sort-alpha-down-alt',
169  'text' => elgg_echo('sort:za', [elgg_echo('table_columns:fromProperty:name')]),
171  'sort_by' => [
172  'property' => 'name',
173  'property_type' => 'metadata',
174  'direction' => 'desc',
175  ],
176  ]),
177  'title' => elgg_echo('sort:alpha'),
178  'parent_name' => 'sort:parent',
179  'priority' => 130,
180  ]);
181 
182  return $result;
183  }
184 
192  public static function registerTitleSorting(\Elgg\Event $event): ?MenuItems {
193 
194  if (!(bool) $event->getParam('filter_sorting', true)) {
195  // sorting is disabled for this menu
196  return null;
197  }
198 
199  /* @var $result MenuItems */
200  $result = $event->getValue();
201 
203  'name' => 'sort:title:asc',
204  'icon' => 'sort-alpha-down',
205  'text' => elgg_echo('sort:az', [elgg_echo('title')]),
207  'sort_by' => [
208  'property' => 'title',
209  'property_type' => 'metadata',
210  'direction' => 'asc',
211  ],
212  ]),
213  'title' => elgg_echo('sort:alpha'),
214  'parent_name' => 'sort:parent',
215  'priority' => 140,
216  ]);
217 
219  'name' => 'sort:title:desc',
220  'icon' => 'sort-alpha-down-alt',
221  'text' => elgg_echo('sort:za', [elgg_echo('title')]),
223  'sort_by' => [
224  'property' => 'title',
225  'property_type' => 'metadata',
226  'direction' => 'desc',
227  ],
228  ]),
229  'title' => elgg_echo('sort:alpha'),
230  'parent_name' => 'sort:parent',
231  'priority' => 150,
232  ]);
233 
234  return $result;
235  }
236 
245  public static function registerSortingDropdown(\Elgg\Event $event): ?MenuItems {
246 
247  if (!(bool) $event->getParam('filter_sorting', true)) {
248  // sorting is disabled for this menu
249  return null;
250  }
251 
252  /* @var $result MenuItems */
253  $result = $event->getValue();
254 
255  $first_menu_name = $event->getParam('filter_sorting_selected');
256  if (isset($first_menu_name) && !$result->has($first_menu_name)) {
257  $first_menu_name = null;
258  }
259 
260  if (!isset($first_menu_name)) {
261  $result->sort([\ElggMenuBuilder::class, 'compareByPriority']);
262 
263  /* @var $menu_item \ElggMenuItem */
264  foreach ($result as $menu_item) {
265  if ($menu_item->getParentName() !== 'sort:parent') {
266  continue;
267  }
268 
269  if (!isset($first_menu_name)) {
270  $first_menu_name = $menu_item->getID();
271  }
272 
273  if (!$menu_item->getSelected()) {
274  continue;
275  }
276 
277  $first_menu_name = $menu_item->getID();
278  break;
279  }
280  }
281 
282  if (!empty($first_menu_name)) {
283  $menu_item = $result->get($first_menu_name);
284 
285  $menu_item->setPriority(99999);
286  $menu_item->setName('sort:parent');
287  $menu_item->setParentName('');
288  $menu_item->setChildMenuOptions([
289  'display' => 'dropdown',
290  'data-position' => json_encode([
291  'at' => 'right bottom',
292  'my' => 'right top',
293  'collision' => 'fit fit',
294  ]),
295  ]);
296  $menu_item->setSelected(false);
297  $menu_item->setHref(false);
298  }
299 
300  return $result;
301  }
302 }
Add generic sort_by menu items to a filter menu.
elgg_echo(string $message_key, array $args=[], string $language= '')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
static factory(array $options)
Create an ElggMenuItem from an associative array.
static registerNameSorting(\Elgg\Event $event)
Register sorting menu items based on the name metadata.
static registerTimeCreatedSorting(\Elgg\Event $event)
Register sorting menu items based on the time_created attribute.
elgg_get_current_url()
Returns the current page&#39;s complete URL.
static registerTitleSorting(\Elgg\Event $event)
Register sorting menu items based on the title metadata.
elgg_http_add_url_query_elements(string $url, array $elements)
Sets elements in a URL&#39;s query string.
Definition: elgglib.php:183
static registerLastActionSorting(\Elgg\Event $event)
Register sorting menu items based on the last_action attribute.
static registerLastLoginSorting(\Elgg\Event $event)
Register sorting menu items based on the last_login metadata.
static registerSortingDropdown(\Elgg\Event $event)
Setup the sorting options in a dropdown menu, This should be used in combination with the other regis...
A collection of menu items.
Definition: MenuItems.php:10
Models an event passed to event handlers.
Definition: Event.php:11