weekly back-to-dev release 5.0dev
[moodle.git] / lib / editor / atto / adminlib.php
bloba1b9bd7e1d82bb5f2cab7d4d178627526a8e4f85
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.
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/>.
17 /**
18 * Atto admin setting stuff.
20 * @package editor_atto
21 * @copyright 2014 Jerome Mouneyrac
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 /**
28 * Admin setting for toolbar.
30 * @package editor_atto
31 * @copyright 2014 Frédéric Massart
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class editor_atto_toolbar_setting extends admin_setting_configtextarea {
36 /**
37 * Validate data.
39 * This ensures that:
40 * - Plugins are only used once,
41 * - Group names are unique,
42 * - Lines match: group = plugin[, plugin[, plugin ...]],
43 * - There are some groups and plugins defined,
44 * - The plugins used are installed.
46 * @param string $data
47 * @return mixed True on success, else error message.
49 public function validate($data) {
50 $result = parent::validate($data);
51 if ($result !== true) {
52 return $result;
55 $lines = explode("\n", $data);
56 $groups = array();
57 $plugins = array();
59 foreach ($lines as $line) {
60 if (!trim($line)) {
61 continue;
64 $matches = array();
65 if (!preg_match('/^\s*([a-z0-9]+)\s*=\s*([a-z0-9]+(\s*,\s*[a-z0-9]+)*)+\s*$/', $line, $matches)) {
66 $result = get_string('errorcannotparseline', 'editor_atto', $line);
67 break;
70 $group = $matches[1];
71 if (isset($groups[$group])) {
72 $result = get_string('errorgroupisusedtwice', 'editor_atto', $group);
73 break;
75 $groups[$group] = true;
77 $lineplugins = array_map('trim', explode(',', $matches[2]));
78 foreach ($lineplugins as $plugin) {
79 if (isset($plugins[$plugin])) {
80 $result = get_string('errorpluginisusedtwice', 'editor_atto', $plugin);
81 break 2;
82 } else if (!core_component::get_component_directory('atto_' . $plugin)) {
83 $result = get_string('errorpluginnotfound', 'editor_atto', $plugin);
84 break 2;
86 $plugins[$plugin] = true;
90 // We did not find any groups or plugins.
91 if (empty($groups) || empty($plugins)) {
92 $result = get_string('errornopluginsorgroupsfound', 'editor_atto');
95 return $result;
101 * Special class for Atto plugins administration.
103 * @package editor_atto
104 * @copyright 2014 Jerome Mouneyrac
105 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
107 class editor_atto_subplugins_setting extends admin_setting {
110 * Constructor.
112 public function __construct() {
113 $this->nosave = true;
114 parent::__construct('attosubplugins', get_string('subplugintype_atto_plural', 'editor_atto'), '', '');
118 * Returns current value of this setting.
119 * Always returns true, does nothing.
121 * @return true
123 public function get_setting() {
124 return true;
128 * Returns default setting if exists.
129 * Always returns true, does nothing.
131 * @return true
133 public function get_defaultsetting() {
134 return true;
138 * Store new setting.
139 * Always returns '', does not write anything.
141 * @param string $data string or array, must not be NULL.
142 * @return string Always returns ''.
144 public function write_setting($data) {
145 // Do not write any setting.
146 return '';
150 * Checks if $query is one of the available subplugins.
152 * @param string $query The string to search for.
153 * @return bool Returns true if found, false if not.
155 public function is_related($query) {
156 if (parent::is_related($query)) {
157 return true;
160 $subplugins = core_component::get_plugin_list('atto');
161 foreach ($subplugins as $name => $dir) {
162 if (stripos($name, $query) !== false) {
163 return true;
166 $namestr = get_string('pluginname', 'atto_' . $name);
167 if (strpos(core_text::strtolower($namestr), core_text::strtolower($query)) !== false) {
168 return true;
171 return false;
175 * Builds the XHTML to display the control.
177 * @param mixed $data Unused.
178 * @param string $query
179 * @return string highlight.
181 public function output_html($data, $query = '') {
182 global $CFG, $OUTPUT, $PAGE;
183 require_once($CFG->libdir . "/editorlib.php");
184 require_once(__DIR__ . '/lib.php');
185 $pluginmanager = core_plugin_manager::instance();
187 // Display strings.
188 $strtoolbarconfig = get_string('toolbarconfig', 'editor_atto');
189 $strname = get_string('name');
190 $strsettings = get_string('settings');
191 $struninstall = get_string('uninstallplugin', 'core_admin');
192 $strversion = get_string('version');
194 $subplugins = core_component::get_plugin_list('atto');
196 $return = $OUTPUT->heading(get_string('subplugintype_atto_plural', 'editor_atto'), 3, 'main', true);
197 $return .= $OUTPUT->box_start('generalbox attosubplugins');
199 $table = new html_table();
200 $table->head = array($strname, $strversion, $strtoolbarconfig, $strsettings, $struninstall);
201 $table->align = array('left', 'left', 'center', 'center', 'center', 'center');
202 $table->data = array();
203 $table->attributes['class'] = 'admintable generaltable';
205 // Iterate through subplugins.
206 foreach ($subplugins as $name => $dir) {
207 $namestr = get_string('pluginname', 'atto_' . $name);
208 $version = get_config('atto_' . $name, 'version');
209 if ($version === false) {
210 $version = '';
212 $plugininfo = $pluginmanager->get_plugin_info('atto_' . $name);
214 $toolbarconfig = $name;
216 $displayname = $namestr;
218 // Check if there is an icon in the atto plugin pix/ folder.
219 if ($PAGE->theme->resolve_image_location('icon', 'atto_' . $name, false)) {
220 $icon = $OUTPUT->pix_icon('icon', '', 'atto_' . $name, array('class' => 'icon pluginicon'));
221 } else {
222 // No icon found.
223 $icon = $OUTPUT->pix_icon('spacer', '', 'moodle', array('class' => 'icon pluginicon noicon'));
225 $displayname = $icon . $displayname;
227 // Add settings link.
228 if (!$version) {
229 $settings = '';
230 } else if ($url = $plugininfo->get_settings_url()) {
231 $settings = html_writer::link($url, $strsettings);
232 } else {
233 $settings = '';
236 // Add uninstall info.
237 $uninstall = '';
238 if ($uninstallurl = core_plugin_manager::instance()->get_uninstall_url('atto_' . $name, 'manage')) {
239 $uninstall = html_writer::link($uninstallurl, $struninstall);
242 // Add a row to the table.
243 $row = new html_table_row(array($displayname, $version, $toolbarconfig, $settings, $uninstall));
244 $table->data[] = $row;
246 $return .= html_writer::table($table);
247 $return .= html_writer::tag('p', get_string('tablenosave', 'admin'));
248 $return .= $OUTPUT->box_end();
249 return highlight($query, $return);