Commit | Line | Data |
---|---|---|
3406acde SH |
1 | <?php |
2 | ||
3 | class 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 | ||
13915f89 | 33 | $hasicon = ((!$isbranch || $item->type == navigation_node::TYPE_ACTIVITY || $item->type == navigation_node::TYPE_RESOURCE) && $item->icon instanceof renderable); |
7081714d SH |
34 | |
35 | if ($hasicon) { | |
3406acde | 36 | $icon = $this->output->render($item->icon); |
13915f89 SH |
37 | } else { |
38 | $icon = ''; | |
3406acde | 39 | } |
13915f89 | 40 | $content = $icon.$content; // use CSS for spacing of icons |
3406acde SH |
41 | if ($item->helpbutton !== null) { |
42 | $content = trim($item->helpbutton).html_writer::tag('span', $content, array('class'=>'clearhelpbutton')); | |
43 | } | |
44 | ||
45 | if ($content === '') { | |
46 | continue; | |
47 | } | |
48 | ||
480f906e SH |
49 | $attributes = array(); |
50 | if ($title !== '') { | |
51 | $attributes['title'] = $title; | |
52 | } | |
53 | if ($item->hidden) { | |
54 | $attributes['class'] = 'dimmed_text'; | |
55 | } | |
56 | if (is_string($item->action) || empty($item->action) || ($item->type === navigation_node::TYPE_CATEGORY && empty($options['linkcategories']))) { | |
32561caf | 57 | $attributes['tabindex'] = '0'; //add tab support to span but still maintain character stream sequence. |
480f906e SH |
58 | $content = html_writer::tag('span', $content, $attributes); |
59 | } else if ($item->action instanceof action_link) { | |
3406acde SH |
60 | //TODO: to be replaced with something else |
61 | $link = $item->action; | |
13915f89 | 62 | $link->text = $icon.$link->text; |
480f906e | 63 | $link->attributes = array_merge($link->attributes, $attributes); |
3406acde | 64 | $content = $this->output->render($link); |
480f906e | 65 | $linkrendered = true; |
3406acde | 66 | } else if ($item->action instanceof moodle_url) { |
3406acde | 67 | $content = html_writer::link($item->action, $content, $attributes); |
3406acde SH |
68 | } |
69 | ||
70 | // this applies to the li item which contains all child lists too | |
d2401694 | 71 | $liclasses = array($item->get_css_type(), 'depth_'.$depth); |
3406acde SH |
72 | if ($item->has_children() && (!$item->forceopen || $item->collapse)) { |
73 | $liclasses[] = 'collapsed'; | |
74 | } | |
7081714d SH |
75 | if ($isbranch) { |
76 | $liclasses[] = 'contains_branch'; | |
77 | } else if ($hasicon) { | |
78 | $liclasses[] = 'item_with_icon'; | |
79 | } | |
3406acde SH |
80 | if ($item->isactive === true) { |
81 | $liclasses[] = 'current_branch'; | |
82 | } | |
83 | $liattr = array('class'=>join(' ',$liclasses)); | |
84 | // class attribute on the div item which only contains the item content | |
85 | $divclasses = array('tree_item'); | |
7081714d | 86 | if ($isbranch) { |
3406acde SH |
87 | $divclasses[] = 'branch'; |
88 | } else { | |
89 | $divclasses[] = 'leaf'; | |
90 | } | |
7081714d SH |
91 | if ($hasicon) { |
92 | $divclasses[] = 'hasicon'; | |
93 | } | |
3406acde SH |
94 | if (!empty($item->classes) && count($item->classes)>0) { |
95 | $divclasses[] = join(' ', $item->classes); | |
96 | } | |
97 | $divattr = array('class'=>join(' ', $divclasses)); | |
98 | if (!empty($item->id)) { | |
99 | $divattr['id'] = $item->id; | |
100 | } | |
8e5c23e0 SH |
101 | $content = html_writer::tag('p', $content, $divattr); |
102 | if ($isexpandable) { | |
103 | $content .= $this->navigation_node($item->children, array(), $expansionlimit, $options, $depth+1); | |
104 | } | |
3406acde SH |
105 | if (!empty($item->preceedwithhr) && $item->preceedwithhr===true) { |
106 | $content = html_writer::empty_tag('hr') . $content; | |
107 | } | |
108 | $content = html_writer::tag('li', $content, $liattr); | |
109 | $lis[] = $content; | |
110 | } | |
6c721bbf SH |
111 | |
112 | if (count($lis)) { | |
113 | return html_writer::tag('ul', implode("\n", $lis), $attrs); | |
114 | } else { | |
115 | return ''; | |
116 | } | |
3406acde SH |
117 | } |
118 | ||
119 | } |