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