blocks-navigation MDL-25596 Fixed up redundant AJAX calls when navigation was limited...
[moodle.git] / blocks / navigation / renderer.php
CommitLineData
3406acde
SH
1<?php
2
3class block_navigation_renderer extends plugin_renderer_base {
480f906e
SH
4
5 public function navigation_tree(global_navigation $navigation, $expansionlimit, array $options = array()) {
d2401694 6 $navigation->add_class('navigation_node');
480f906e 7 $content = $this->navigation_node(array($navigation), array('class'=>'block_tree list'), $expansionlimit, $options);
3406acde
SH
8 if (isset($navigation->id) && !is_numeric($navigation->id) && !empty($content)) {
9 $content = $this->output->box($content, 'block_tree_box', $navigation->id);
10 }
11 return $content;
12 }
13
480f906e 14 protected function navigation_node($items, $attrs=array(), $expansionlimit=null, array $options = array(), $depth=1) {
3406acde
SH
15
16 // exit if empty, we don't want an empty ul element
17 if (count($items)==0) {
18 return '';
19 }
20
21 // array of nested li elements
22 $lis = array();
23 foreach ($items as $item) {
8e5c23e0 24 if (!$item->display && !$item->contains_active_node()) {
3406acde
SH
25 continue;
26 }
27 $content = $item->get_content();
28 $title = $item->get_title();
8e5c23e0
SH
29
30 $isexpandable = (empty($expansionlimit) || ($item->type > navigation_node::TYPE_ACTIVITY || $item->type < $expansionlimit) || ($item->contains_active_node() && $item->children->count() > 0));
31 $isbranch = $isexpandable && ($item->children->count() > 0 || ($item->has_children() && (isloggedin() || $item->type <= navigation_node::TYPE_CATEGORY)));
32
33 $hasicon = ((!$isbranch || $item->type == navigation_node::TYPE_ACTIVITY )&& $item->icon instanceof renderable);
7081714d
SH
34
35 if ($hasicon) {
3406acde 36 $icon = $this->output->render($item->icon);
7081714d 37 $content = $icon.$content; // use CSS for spacing of icons
3406acde
SH
38 }
39 if ($item->helpbutton !== null) {
40 $content = trim($item->helpbutton).html_writer::tag('span', $content, array('class'=>'clearhelpbutton'));
41 }
42
43 if ($content === '') {
44 continue;
45 }
46
480f906e
SH
47 $attributes = array();
48 if ($title !== '') {
49 $attributes['title'] = $title;
50 }
51 if ($item->hidden) {
52 $attributes['class'] = 'dimmed_text';
53 }
54 if (is_string($item->action) || empty($item->action) || ($item->type === navigation_node::TYPE_CATEGORY && empty($options['linkcategories']))) {
55 $content = html_writer::tag('span', $content, $attributes);
56 } else if ($item->action instanceof action_link) {
3406acde
SH
57 //TODO: to be replaced with something else
58 $link = $item->action;
480f906e 59 $link->attributes = array_merge($link->attributes, $attributes);
3406acde 60 $content = $this->output->render($link);
480f906e 61 $linkrendered = true;
3406acde 62 } else if ($item->action instanceof moodle_url) {
3406acde 63 $content = html_writer::link($item->action, $content, $attributes);
3406acde
SH
64 }
65
66 // this applies to the li item which contains all child lists too
d2401694 67 $liclasses = array($item->get_css_type(), 'depth_'.$depth);
3406acde
SH
68 if ($item->has_children() && (!$item->forceopen || $item->collapse)) {
69 $liclasses[] = 'collapsed';
70 }
7081714d
SH
71 if ($isbranch) {
72 $liclasses[] = 'contains_branch';
73 } else if ($hasicon) {
74 $liclasses[] = 'item_with_icon';
75 }
3406acde
SH
76 if ($item->isactive === true) {
77 $liclasses[] = 'current_branch';
78 }
79 $liattr = array('class'=>join(' ',$liclasses));
80 // class attribute on the div item which only contains the item content
81 $divclasses = array('tree_item');
7081714d 82 if ($isbranch) {
3406acde
SH
83 $divclasses[] = 'branch';
84 } else {
85 $divclasses[] = 'leaf';
86 }
7081714d
SH
87 if ($hasicon) {
88 $divclasses[] = 'hasicon';
89 }
3406acde
SH
90 if (!empty($item->classes) && count($item->classes)>0) {
91 $divclasses[] = join(' ', $item->classes);
92 }
93 $divattr = array('class'=>join(' ', $divclasses));
94 if (!empty($item->id)) {
95 $divattr['id'] = $item->id;
96 }
8e5c23e0
SH
97 $content = html_writer::tag('p', $content, $divattr);
98 if ($isexpandable) {
99 $content .= $this->navigation_node($item->children, array(), $expansionlimit, $options, $depth+1);
100 }
3406acde
SH
101 if (!empty($item->preceedwithhr) && $item->preceedwithhr===true) {
102 $content = html_writer::empty_tag('hr') . $content;
103 }
104 $content = html_writer::tag('li', $content, $liattr);
105 $lis[] = $content;
106 }
6c721bbf
SH
107
108 if (count($lis)) {
109 return html_writer::tag('ul', implode("\n", $lis), $attrs);
110 } else {
111 return '';
112 }
3406acde
SH
113 }
114
115}