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