MDL-52996 Atto: Allow plugins to customise toolbar
[moodle.git] / lib / editor / atto / lib.php
CommitLineData
adca7326
DW
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/**
18 * YUI text editor integration.
19 *
20 * @package editor_atto
21 * @copyright 2013 Damyon Wiese <damyon@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27/**
28 * This is the texteditor implementation.
29 * @copyright 2013 Damyon Wiese <damyon@moodle.com>
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 */
32class atto_texteditor extends texteditor {
33
34 /**
35 * Is the current browser supported by this editor?
36 *
37 * Of course!
38 * @return bool
39 */
40 public function supported_by_browser() {
41 return true;
42 }
43
44 /**
45 * Returns array of supported text formats.
46 * @return array
47 */
48 public function get_supported_formats() {
49 // FORMAT_MOODLE is not supported here, sorry.
50 return array(FORMAT_HTML => FORMAT_HTML);
51 }
52
53 /**
54 * Returns text format preferred by this editor.
55 * @return int
56 */
57 public function get_preferred_format() {
58 return FORMAT_HTML;
59 }
60
61 /**
62 * Does this editor support picking from repositories?
63 * @return bool
64 */
65 public function supports_repositories() {
66 return true;
67 }
68
69 /**
55c0403c 70 * Use this editor for given element.
adca7326 71 *
f66da45f 72 * Available Atto-specific options:
73 * atto:toolbar - set to a string to override the system config editor_atto/toolbar
74 *
75 * Available general options:
76 * context - set to the current context object
77 * enable_filemanagement - set false to get rid of the managefiles plugin
78 * autosave - true/false to control autosave
79 *
80 * Options are also passed through to the plugins.
81 *
adca7326
DW
82 * @param string $elementid
83 * @param array $options
84 * @param null $fpoptions
85 */
86 public function use_editor($elementid, array $options=null, $fpoptions=null) {
87 global $PAGE;
adca7326 88
f66da45f 89 if (array_key_exists('atto:toolbar', $options)) {
90 $configstr = $options['atto:toolbar'];
91 } else {
92 $configstr = get_config('editor_atto', 'toolbar');
93 }
55c0403c
DW
94
95 $grouplines = explode("\n", $configstr);
adca7326 96
55c0403c
DW
97 $groups = array();
98
99 foreach ($grouplines as $groupline) {
100 $line = explode('=', $groupline);
101 if (count($line) > 1) {
102 $group = trim(array_shift($line));
103 $plugins = array_map('trim', explode(',', array_shift($line)));
104 $groups[$group] = $plugins;
105 }
adca7326
DW
106 }
107
55c0403c 108 $modules = array('moodle-editor_atto-editor');
45698f77 109 $options['context'] = empty($options['context']) ? context_system::instance() : $options['context'];
55c0403c
DW
110
111 $jsplugins = array();
112 foreach ($groups as $group => $plugins) {
113 $groupplugins = array();
114 foreach ($plugins as $plugin) {
60a051d4
FM
115 // Do not die on missing plugin.
116 if (!core_component::get_component_directory('atto_' . $plugin)) {
60a051d4 117 continue;
40a15455
AG
118 }
119
120 // Remove manage files if requested.
121 if ($plugin == 'managefiles' && isset($options['enable_filemanagement']) && !$options['enable_filemanagement']) {
122 continue;
60a051d4
FM
123 }
124
55c0403c
DW
125 $jsplugin = array();
126 $jsplugin['name'] = $plugin;
127 $jsplugin['params'] = array();
128 $modules[] = 'moodle-atto_' . $plugin . '-button';
129
130 component_callback('atto_' . $plugin, 'strings_for_js');
45698f77 131 $extra = component_callback('atto_' . $plugin, 'params_for_js', array($elementid, $options, $fpoptions));
55c0403c
DW
132
133 if ($extra) {
62467795 134 $jsplugin = array_merge($jsplugin, $extra);
55c0403c
DW
135 }
136 // We always need the plugin name.
137 $PAGE->requires->string_for_js('pluginname', 'atto_' . $plugin);
138 $groupplugins[] = $jsplugin;
139 }
140 $jsplugins[] = array('group'=>$group, 'plugins'=>$groupplugins);
adca7326
DW
141 }
142
d86e45f0
AN
143 $PAGE->requires->strings_for_js(array(
144 'editor_command_keycode',
145 'editor_control_keycode',
146 'plugin_title_shortcut',
6bfd450a 147 'textrecovered',
a108fbee 148 'autosavefailed',
b6da971b
DC
149 'autosavesucceeded',
150 'errortextrecovery'
d86e45f0 151 ), 'editor_atto');
8a5db547
DW
152 $PAGE->requires->strings_for_js(array(
153 'warning',
154 'info'
155 ), 'moodle');
55c0403c 156 $PAGE->requires->yui_module($modules,
62467795 157 'Y.M.editor_atto.Editor.init',
55c0403c
DW
158 array($this->get_init_params($elementid, $options, $fpoptions, $jsplugins)));
159
adca7326
DW
160 }
161
162 /**
163 * Create a params array to init the editor.
164 *
165 * @param string $elementid
166 * @param array $options
167 * @param array $fpoptions
168 */
55c0403c 169 protected function get_init_params($elementid, array $options = null, array $fpoptions = null, $plugins = null) {
adca7326
DW
170 global $PAGE;
171
172 $directionality = get_string('thisdirection', 'langconfig');
173 $strtime = get_string('strftimetime');
174 $strdate = get_string('strftimedaydate');
175 $lang = current_language();
2ba6706d 176 $autosave = true;
6bfd450a 177 $autosavefrequency = get_config('editor_atto', 'autosavefrequency');
2ba6706d
DW
178 if (isset($options['autosave'])) {
179 $autosave = $options['autosave'];
180 }
adca7326
DW
181 $contentcss = $PAGE->theme->editor_css_url()->out(false);
182
3f08e9b9
DM
183 // Autosave disabled for guests.
184 if (isguestuser()) {
185 $autosave = false;
186 }
988592c5
DW
187 // Note <> is a safe separator, because it will not appear in the output of s().
188 $pagehash = sha1($PAGE->url . '<>' . s($this->get_text()));
adca7326
DW
189 $params = array(
190 'elementid' => $elementid,
191 'content_css' => $contentcss,
2ba6706d
DW
192 'contextid' => $options['context']->id,
193 'autosaveEnabled' => $autosave,
6bfd450a 194 'autosaveFrequency' => $autosavefrequency,
adca7326
DW
195 'language' => $lang,
196 'directionality' => $directionality,
55c0403c 197 'filepickeroptions' => array(),
c07f86ce 198 'plugins' => $plugins,
988592c5 199 'pageHash' => $pagehash,
adca7326
DW
200 );
201 if ($fpoptions) {
202 $params['filepickeroptions'] = $fpoptions;
203 }
204 return $params;
205 }
206}