1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Definition of views_handler_sort_menu_hierarchy.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
|
|
* Sort in menu hierarchy order.
|
10 |
|
|
*
|
11 |
|
|
* Given a field name of 'p' this produces an ORDER BY on p1, p2, ..., p9;
|
12 |
|
|
* and optionally injects multiple joins to {menu_links} to sort by weight
|
13 |
|
|
* and title as well.
|
14 |
|
|
*
|
15 |
|
|
* This is only really useful for the {menu_links} table.
|
16 |
|
|
*
|
17 |
|
|
* @ingroup views_sort_handlers
|
18 |
|
|
*/
|
19 |
|
|
class views_handler_sort_menu_hierarchy extends views_handler_sort {
|
20 |
|
|
function option_definition() {
|
21 |
|
|
$options = parent::option_definition();
|
22 |
|
|
$options['sort_within_level'] = array('default' => FALSE);
|
23 |
|
|
return $options;
|
24 |
|
|
}
|
25 |
|
|
|
26 |
|
|
function options_form(&$form, &$form_state) {
|
27 |
|
|
parent::options_form($form, $form_state);
|
28 |
|
|
$form['sort_within_level'] = array(
|
29 |
|
|
'#type' => 'checkbox',
|
30 |
|
|
'#title' => t('Sort within each hierarchy level'),
|
31 |
|
|
'#description' => t('Enable this to sort the items within each level of the hierarchy by weight and title. Warning: this may produce a slow query.'),
|
32 |
|
|
'#default_value' => $this->options['sort_within_level'],
|
33 |
|
|
);
|
34 |
|
|
}
|
35 |
|
|
|
36 |
|
|
function query() {
|
37 |
|
|
$this->ensure_my_table();
|
38 |
|
|
$max_depth = isset($this->definition['max depth']) ? $this->definition['max depth'] : MENU_MAX_DEPTH;
|
39 |
|
|
for ($i = 1; $i <= $max_depth; ++$i) {
|
40 |
|
|
if ($this->options['sort_within_level']) {
|
41 |
|
|
$join = new views_join();
|
42 |
|
|
$join->construct('menu_links', $this->table_alias, $this->field . $i, 'mlid');
|
43 |
|
|
$menu_links = $this->query->add_table('menu_links', NULL, $join);
|
44 |
|
|
$this->query->add_orderby($menu_links, 'weight', $this->options['order']);
|
45 |
|
|
$this->query->add_orderby($menu_links, 'link_title', $this->options['order']);
|
46 |
|
|
}
|
47 |
|
|
|
48 |
|
|
// We need this even when also sorting by weight and title, to make sure
|
49 |
|
|
// that children of two parents with the same weight and title are
|
50 |
|
|
// correctly separated.
|
51 |
|
|
$this->query->add_orderby($this->table_alias, $this->field . $i, $this->options['order']);
|
52 |
|
|
}
|
53 |
|
|
}
|
54 |
|
|
} |