"MDL-12304, fix double text"
[moodle-linuxchix.git] / blocks / admin_tree / block_admin_tree.php
blobf05bdb937568ab946674277d175c8da4d93d5fe0
1 <?php // $Id$
3 class block_admin_tree extends block_base {
5 var $currentdepth;
6 var $divcounter;
7 var $tempcontent;
8 var $pathtosection;
9 var $expandjavascript;
10 var $destination;
12 function init() {
13 global $PAGE;
14 $this->title = get_string('administrationsite');
15 $this->version = 2007101509;
16 $this->currentdepth = 0;
17 $this->divcounter = 1;
18 $this->tempcontent = '';
19 $this->section = (isset($PAGE->section) ? $PAGE->section : '');
20 $this->pathtosection = array();
21 $this->expandjavascript = '';
24 function applicable_formats() {
25 if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
26 return array('site' => true, 'admin' => true, 'my' => true);
27 } else {
28 return array('site' => true, 'admin' => true);
32 function preferred_width() {
33 return 210;
36 function open_folder($visiblename) {
37 global $CFG;
38 $strfolderopened = s(get_string('folderopened'));
40 $this->tempcontent .= '<div class="depth'.$this->currentdepth.'"><a href="#" onclick="toggle(\''.$this->divcounter.'\');return false">';
41 $this->tempcontent .= '<span id="vh_div'.$this->divcounter.'indicator"><img src="'.$CFG->pixpath.'/i/open.gif" alt="'.$strfolderopened.'" /></span> ';
42 $this->tempcontent .= $visiblename.'</a></div><div id="vh_div'.$this->divcounter.'">'."\n";
43 $this->currentdepth++;
44 $this->divcounter++;
47 function close_folder() {
48 $this->currentdepth--;
49 $this->tempcontent .= "</div>\n";
52 function create_item($visiblename,$link,$icon,$class) {
53 global $CFG;
54 $this->tempcontent .= '<div class="depth'.$this->currentdepth.'"><a class="'.$class.'" href="'.$link.'"><img src="'.$icon.'" alt="" />'.
55 $visiblename.'</a></div>'."\n";
58 function build_tree (&$content) {
59 global $CFG;
60 if (is_a($content, 'admin_settingpage')) {
61 // show hidden pages in tree if hidden page active
62 if ($content->check_access() and (($content->name == $this->section) or !$content->is_hidden())) {
63 $class = ($content->name == $this->section) ? 'link current' : 'link';
64 if ($content->is_hidden()) {
65 $class .= ' hidden';
67 $this->create_item($content->visiblename, $CFG->wwwroot.'/'.$CFG->admin.'/settings.php?section='.$content->name,$CFG->pixpath.'/i/item.gif', $class);
69 } else if (is_a($content, 'admin_externalpage')) {
70 // show hidden pages in tree if hidden page active
71 if ($content->check_access() and (($content->name == $this->section) or !$content->is_hidden())) {
72 $class = ($content->name == $this->section) ? 'link current' : 'link';
73 if ($content->is_hidden()) {
74 $class .= ' hidden';
76 $this->create_item($content->visiblename, $content->url, $CFG->pixpath.'/i/item.gif', $class);
78 } else if (is_a($content, 'admin_category')) {
79 if ($content->check_access() and !$content->is_hidden()) {
81 // check if the category we're currently printing is a parent category for the current page; if it is, we
82 // make a note (in the javascript) that it has to be expanded after the page has loaded
83 if ($this->section != '' and $this->pathtosection[count($this->pathtosection) - 1] == $content->name) {
84 $this->expandjavascript .= 'expand('.$this->divcounter.');'."\n";
85 array_pop($this->pathtosection);
88 $this->open_folder($content->visiblename);
90 $entries = array_keys($content->children);
92 foreach ($entries as $entry) {
93 $this->build_tree($content->children[$entry]);
96 $this->close_folder();
101 function get_content() {
103 global $CFG;
105 if ($this->content !== NULL) {
106 return $this->content;
109 if (isguestuser() or !isloggedin()) {
110 // these users can not change any settings
111 $this->content = '';
112 return '';
115 require_once($CFG->libdir.'/adminlib.php');
116 $adminroot =& admin_get_root(false, false); // settings not required - only pages
118 if ($current = $adminroot->locate($this->section, true)) {
119 $this->pathtosection = $current->path;
120 array_pop($this->pathtosection);
123 // we need to do this instead of $this->build_tree($adminroot) because the top-level folder
124 // is redundant (and ideally ignored). (the top-level folder is "administration".)
126 $entries = array_keys($adminroot->children);
128 asort($entries);
130 foreach ($entries as $entry) {
131 $this->build_tree($adminroot->children[$entry]);
134 if ($this->tempcontent !== '') {
135 $closedimg = '<img src="'.$CFG->pixpath.'/i/closed.gif" alt="'.s(get_string('folderclosed')).'" />';
136 $openedimg = '<img src="'.$CFG->pixpath.'/i/open.gif" alt="'.s(get_string('folderopened')).'" />';
138 $this->content = new object();
139 $this->content->text = '
140 <script type="text/javascript">
141 //<![CDATA[
142 var vh_numdivs = '.($this->divcounter - 1).';
143 var parkplatz = new Array();
144 for (var i=1; i<=vh_numdivs; i++) {
145 parkplatz[i] = null;
148 function toggle(i) {
149 i = parseInt(i);
150 if (parkplatz[i] === null) {
151 collapse(i);
152 } else {
153 expand(i);
157 function collapse(i) {
158 if (parkplatz[i] !== null) {
159 return;
161 var obj = document.getElementById("vh_div"+String(i));
162 if (obj === null) {
163 return;
165 var nothing = document.createElement("span");
166 nothing.setAttribute("id", "vh_div"+String(i));
167 parkplatz[i] = obj;
168 obj.parentNode.replaceChild(nothing, obj);
169 var icon = document.getElementById("vh_div"+String(i)+"indicator");
170 icon.innerHTML = "'.addslashes_js($closedimg).'";
173 function expand(i) {
174 if (parkplatz[i] === null) {
175 return;
177 var nothing = document.getElementById("vh_div"+String(i));
178 var obj = parkplatz[i];
179 parkplatz[i] = null;
180 nothing.parentNode.replaceChild(obj, nothing);
181 var icon = document.getElementById("vh_div"+String(i)+"indicator");
182 icon.innerHTML = "'.addslashes_js($openedimg).'";
185 function expandall() {
186 for (i=1; i<=vh_numdivs; i++) {
187 expand(i);
191 function collapseall() {
192 for (var i=vh_numdivs; i>0; i--) {
193 collapse(i);
197 //]]>
198 </script>
199 <div class="admintree">
201 '.$this->tempcontent.'
203 </div>
204 <script type="text/javascript">
205 //<![CDATA[
206 collapseall();
207 '.$this->expandjavascript.';
208 //]]>
209 </script>';
211 $this->content->footer = '<div class="adminsearchform">'.
212 '<form action="'.$CFG->wwwroot.'/'.$CFG->admin.'/search.php" method="get"><div>'.
213 '<label for="query" class="accesshide">'.get_string('searchinsettings', 'admin').'</label>'.
214 '<input type="text" name="query" id="query" size="8" value="'.s($adminroot->search).'" />'.
215 '<input type="submit" value="'.get_string('search').'" /></div>'.
216 '</form></div>';
217 } else {
218 $this->content = new object();
219 $this->content->text = '';
222 return $this->content;