search MDL-25153 fixed performance problem
[moodle.git] / blocks / navigation / renderer.php
CommitLineData
3406acde
SH
1<?php
2
3class block_navigation_renderer extends plugin_renderer_base {
4
88f77c3c 5 public function navigation_tree(global_navigation $navigation, $expansionlimit) {
d2401694 6 $navigation->add_class('navigation_node');
88f77c3c 7 $content = $this->navigation_node(array($navigation), array('class'=>'block_tree list'), $expansionlimit);
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
d2401694 14 protected function navigation_node($items, $attrs=array(), $expansionlimit=null, $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) {
24 if (!$item->display) {
25 continue;
26 }
27 $content = $item->get_content();
28 $title = $item->get_title();
31513462 29 $isbranch = ($item->type !== $expansionlimit && ($item->children->count() > 0 || ($item->nodetype == navigation_node::NODETYPE_BRANCH && $item->children->count()==0 && (isloggedin() || $item->type <= navigation_node::TYPE_CATEGORY))));
ea2bc359 30 $hasicon = ((!$isbranch || $item->type == navigation_node::TYPE_ACTIVITY)&& $item->icon instanceof renderable);
7081714d
SH
31
32 if ($hasicon) {
3406acde 33 $icon = $this->output->render($item->icon);
7081714d 34 $content = $icon.$content; // use CSS for spacing of icons
3406acde
SH
35 }
36 if ($item->helpbutton !== null) {
37 $content = trim($item->helpbutton).html_writer::tag('span', $content, array('class'=>'clearhelpbutton'));
38 }
39
40 if ($content === '') {
41 continue;
42 }
43
44 if ($item->action instanceof action_link) {
45 //TODO: to be replaced with something else
46 $link = $item->action;
47 if ($item->hidden) {
48 $link->add_class('dimmed');
49 }
50 $content = $this->output->render($link);
51 } else if ($item->action instanceof moodle_url) {
52 $attributes = array();
53 if ($title !== '') {
54 $attributes['title'] = $title;
55 }
56 if ($item->hidden) {
57 $attributes['class'] = 'dimmed_text';
58 }
59 $content = html_writer::link($item->action, $content, $attributes);
60
61 } else if (is_string($item->action) || empty($item->action)) {
62 $attributes = array();
63 if ($title !== '') {
64 $attributes['title'] = $title;
65 }
66 if ($item->hidden) {
67 $attributes['class'] = 'dimmed_text';
68 }
69 $content = html_writer::tag('span', $content, $attributes);
70 }
71
72 // this applies to the li item which contains all child lists too
d2401694 73 $liclasses = array($item->get_css_type(), 'depth_'.$depth);
3406acde
SH
74 if ($item->has_children() && (!$item->forceopen || $item->collapse)) {
75 $liclasses[] = 'collapsed';
76 }
7081714d
SH
77 if ($isbranch) {
78 $liclasses[] = 'contains_branch';
79 } else if ($hasicon) {
80 $liclasses[] = 'item_with_icon';
81 }
3406acde
SH
82 if ($item->isactive === true) {
83 $liclasses[] = 'current_branch';
84 }
85 $liattr = array('class'=>join(' ',$liclasses));
86 // class attribute on the div item which only contains the item content
87 $divclasses = array('tree_item');
7081714d 88 if ($isbranch) {
3406acde
SH
89 $divclasses[] = 'branch';
90 } else {
91 $divclasses[] = 'leaf';
92 }
7081714d
SH
93 if ($hasicon) {
94 $divclasses[] = 'hasicon';
95 }
3406acde
SH
96 if (!empty($item->classes) && count($item->classes)>0) {
97 $divclasses[] = join(' ', $item->classes);
98 }
99 $divattr = array('class'=>join(' ', $divclasses));
100 if (!empty($item->id)) {
101 $divattr['id'] = $item->id;
102 }
d2401694 103 $content = html_writer::tag('p', $content, $divattr) . $this->navigation_node($item->children, array(), $expansionlimit, $depth+1);
3406acde
SH
104 if (!empty($item->preceedwithhr) && $item->preceedwithhr===true) {
105 $content = html_writer::empty_tag('hr') . $content;
106 }
107 $content = html_writer::tag('li', $content, $liattr);
108 $lis[] = $content;
109 }
6c721bbf
SH
110
111 if (count($lis)) {
112 return html_writer::tag('ul', implode("\n", $lis), $attrs);
113 } else {
114 return '';
115 }
3406acde
SH
116 }
117
118}