1441 |
ariadna |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace core\output;
|
|
|
18 |
|
|
|
19 |
use core\exception\moodle_exception;
|
|
|
20 |
use moodle_url;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Custom menu class
|
|
|
24 |
*
|
|
|
25 |
* This class is used to operate a custom menu that can be rendered for the page.
|
|
|
26 |
* The custom menu is built using $CFG->custommenuitems and is a structured collection
|
|
|
27 |
* of custom_menu_item nodes that can be rendered by the core renderer.
|
|
|
28 |
*
|
|
|
29 |
* To configure the custom menu:
|
|
|
30 |
* Settings: Administration > Appearance > Advanced theme settings
|
|
|
31 |
*
|
|
|
32 |
* @copyright 2010 Sam Hemelryk
|
|
|
33 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
34 |
* @since Moodle 2.0
|
|
|
35 |
* @package core
|
|
|
36 |
* @category output
|
|
|
37 |
*/
|
|
|
38 |
class custom_menu extends custom_menu_item {
|
|
|
39 |
/**
|
|
|
40 |
* @var string The language we should render for, null disables multilang support.
|
|
|
41 |
*/
|
|
|
42 |
protected $currentlanguage = null;
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Creates the custom menu
|
|
|
46 |
*
|
|
|
47 |
* @param string $definition the menu items definition in syntax required by {@see convert_text_to_menu_nodes()}
|
|
|
48 |
* @param string $currentlanguage the current language code, null disables multilang support
|
|
|
49 |
*/
|
|
|
50 |
public function __construct($definition = '', $currentlanguage = null) {
|
|
|
51 |
$this->currentlanguage = $currentlanguage;
|
|
|
52 |
parent::__construct('root'); // create virtual root element of the menu
|
|
|
53 |
if (!empty($definition)) {
|
|
|
54 |
$this->override_children(self::convert_text_to_menu_nodes($definition, $currentlanguage));
|
|
|
55 |
}
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* Overrides the children of this custom menu. Useful when getting children
|
|
|
60 |
* from $CFG->custommenuitems
|
|
|
61 |
*
|
|
|
62 |
* @param array $children
|
|
|
63 |
*/
|
|
|
64 |
public function override_children(array $children) {
|
|
|
65 |
$this->children = [];
|
|
|
66 |
foreach ($children as $child) {
|
|
|
67 |
if ($child instanceof custom_menu_item) {
|
|
|
68 |
$this->children[] = $child;
|
|
|
69 |
}
|
|
|
70 |
}
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* Converts a string into a structured array of custom_menu_items which can
|
|
|
75 |
* then be added to a custom menu.
|
|
|
76 |
*
|
|
|
77 |
* Structure:
|
|
|
78 |
* text|url|title|langs
|
|
|
79 |
* The number of hyphens at the start determines the depth of the item. The
|
|
|
80 |
* languages are optional, comma separated list of languages the line is for.
|
|
|
81 |
*
|
|
|
82 |
* Example structure:
|
|
|
83 |
* First level first item|http://www.moodle.com/
|
|
|
84 |
* -Second level first item|http://www.moodle.com/partners/
|
|
|
85 |
* -Second level second item|http://www.moodle.com/hq/
|
|
|
86 |
* --Third level first item|http://www.moodle.com/jobs/
|
|
|
87 |
* -Second level third item|http://www.moodle.com/development/
|
|
|
88 |
* First level second item|http://www.moodle.com/feedback/
|
|
|
89 |
* First level third item
|
|
|
90 |
* English only|http://moodle.com|English only item|en
|
|
|
91 |
* German only|http://moodle.de|Deutsch|de,de_du,de_kids
|
|
|
92 |
*
|
|
|
93 |
*
|
|
|
94 |
* @static
|
|
|
95 |
* @param string $text the menu items definition
|
|
|
96 |
* @param string $language the language code, null disables multilang support
|
|
|
97 |
* @return array
|
|
|
98 |
*/
|
|
|
99 |
public static function convert_text_to_menu_nodes($text, $language = null) {
|
|
|
100 |
$root = new custom_menu();
|
|
|
101 |
$lastitem = $root;
|
|
|
102 |
$lastdepth = 0;
|
|
|
103 |
$hiddenitems = [];
|
|
|
104 |
$lines = explode("\n", $text);
|
|
|
105 |
foreach ($lines as $linenumber => $line) {
|
|
|
106 |
$line = trim($line);
|
|
|
107 |
if (strlen($line) == 0) {
|
|
|
108 |
continue;
|
|
|
109 |
}
|
|
|
110 |
// Parse item settings.
|
|
|
111 |
$itemtext = null;
|
|
|
112 |
$itemurl = null;
|
|
|
113 |
$itemtitle = null;
|
|
|
114 |
$itemvisible = true;
|
|
|
115 |
$settings = explode('|', $line);
|
|
|
116 |
foreach ($settings as $i => $setting) {
|
|
|
117 |
$setting = trim($setting);
|
|
|
118 |
if ($setting !== '') {
|
|
|
119 |
switch ($i) {
|
|
|
120 |
case 0: // Menu text.
|
|
|
121 |
$itemtext = ltrim($setting, '-');
|
|
|
122 |
break;
|
|
|
123 |
case 1: // URL.
|
|
|
124 |
try {
|
|
|
125 |
$itemurl = new moodle_url($setting);
|
|
|
126 |
} catch (moodle_exception $exception) {
|
|
|
127 |
// We're not actually worried about this, we don't want to mess up the display
|
|
|
128 |
// just for a wrongly entered URL.
|
|
|
129 |
$itemurl = null;
|
|
|
130 |
}
|
|
|
131 |
break;
|
|
|
132 |
case 2: // Title attribute.
|
|
|
133 |
$itemtitle = $setting;
|
|
|
134 |
break;
|
|
|
135 |
case 3: // Language.
|
|
|
136 |
if (!empty($language)) {
|
|
|
137 |
$itemlanguages = array_map('trim', explode(',', $setting));
|
|
|
138 |
$itemvisible &= in_array($language, $itemlanguages);
|
|
|
139 |
}
|
|
|
140 |
break;
|
|
|
141 |
}
|
|
|
142 |
}
|
|
|
143 |
}
|
|
|
144 |
// Get depth of new item.
|
|
|
145 |
preg_match('/^(\-*)/', $line, $match);
|
|
|
146 |
$itemdepth = strlen($match[1]) + 1;
|
|
|
147 |
// Find parent item for new item.
|
|
|
148 |
while (($lastdepth - $itemdepth) >= 0) {
|
|
|
149 |
$lastitem = $lastitem->get_parent();
|
|
|
150 |
$lastdepth--;
|
|
|
151 |
}
|
|
|
152 |
$lastitem = $lastitem->add($itemtext, $itemurl, $itemtitle, $linenumber + 1);
|
|
|
153 |
$lastdepth++;
|
|
|
154 |
if (!$itemvisible) {
|
|
|
155 |
$hiddenitems[] = $lastitem;
|
|
|
156 |
}
|
|
|
157 |
}
|
|
|
158 |
foreach ($hiddenitems as $item) {
|
|
|
159 |
$item->parent->remove_child($item);
|
|
|
160 |
}
|
|
|
161 |
return $root->get_children();
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
/**
|
|
|
165 |
* Sorts two custom menu items
|
|
|
166 |
*
|
|
|
167 |
* This function is designed to be used with the usort method
|
|
|
168 |
* usort($this->children, array('custom_menu','sort_custom_menu_items'));
|
|
|
169 |
*
|
|
|
170 |
* @static
|
|
|
171 |
* @param custom_menu_item $itema
|
|
|
172 |
* @param custom_menu_item $itemb
|
|
|
173 |
* @return int
|
|
|
174 |
*/
|
|
|
175 |
public static function sort_custom_menu_items(custom_menu_item $itema, custom_menu_item $itemb) {
|
|
|
176 |
$itema = $itema->get_sort_order();
|
|
|
177 |
$itemb = $itemb->get_sort_order();
|
|
|
178 |
if ($itema == $itemb) {
|
|
|
179 |
return 0;
|
|
|
180 |
}
|
|
|
181 |
return ($itema > $itemb) ? +1 : -1;
|
|
|
182 |
}
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
// Alias this class to the old name.
|
|
|
186 |
// This file will be autoloaded by the legacyclasses autoload system.
|
|
|
187 |
// In future all uses of this class will be corrected and the legacy references will be removed.
|
|
|
188 |
class_alias(custom_menu::class, \custom_menu::class);
|