PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/editor/tinymce/adminlib.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 280 lines | 164 code | 36 blank | 80 comment | 26 complexity | 868838d2c2bb636426c28c76a02f5a3a MD5 | raw file
  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. * TinyMCE admin setting stuff.
  18. *
  19. * @package editor_tinymce
  20. * @copyright 2012 Petr Skoda {@link http://skodak.org}
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') || die();
  24. require_once("$CFG->libdir/pluginlib.php");
  25. /**
  26. * Editor subplugin info class.
  27. *
  28. * @package editor_tinymce
  29. * @copyright 2012 Petr Skoda {@link http://skodak.org}
  30. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31. */
  32. class plugininfo_tinymce extends plugininfo_base {
  33. public function is_uninstall_allowed() {
  34. return true;
  35. }
  36. public function get_uninstall_url() {
  37. return new moodle_url('/lib/editor/tinymce/subplugins.php', array('delete' => $this->name, 'sesskey' => sesskey()));
  38. }
  39. public function get_settings_section_name() {
  40. return 'tinymce'.$this->name.'settings';
  41. }
  42. public function load_settings(part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
  43. global $CFG, $USER, $DB, $OUTPUT, $PAGE; // in case settings.php wants to refer to them
  44. $ADMIN = $adminroot; // may be used in settings.php
  45. $settings = null;
  46. if ($hassiteconfig && file_exists($this->full_path('settings.php'))) {
  47. $section = $this->get_settings_section_name();
  48. $settings = new admin_settingpage($section, $this->displayname,
  49. 'moodle/site:config', $this->is_enabled() === false);
  50. include($this->full_path('settings.php')); // this may also set $settings to null
  51. }
  52. if ($settings) {
  53. $ADMIN->add($parentnodename, $settings);
  54. }
  55. }
  56. public function is_enabled() {
  57. static $disabledsubplugins = null; // TODO: MDL-34344 remove this once get_config() is cached via MUC!
  58. if (is_null($disabledsubplugins)) {
  59. $disabledsubplugins = array();
  60. $config = get_config('editor_tinymce', 'disabledsubplugins');
  61. if ($config) {
  62. $config = explode(',', $config);
  63. foreach ($config as $sp) {
  64. $sp = trim($sp);
  65. if ($sp !== '') {
  66. $disabledsubplugins[$sp] = $sp;
  67. }
  68. }
  69. }
  70. }
  71. return !isset($disabledsubplugins[$this->name]);
  72. }
  73. }
  74. /**
  75. * Special class for TinyMCE subplugin administration.
  76. *
  77. * @package editor_tinymce
  78. * @copyright 2012 Petr Skoda {@link http://skodak.org}
  79. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  80. */
  81. class tiynce_subplugins_settings extends admin_setting {
  82. public function __construct() {
  83. $this->nosave = true;
  84. parent::__construct('tinymcesubplugins', get_string('subplugintype_tinymce_plural', 'editor_tinymce'), '', '');
  85. }
  86. /**
  87. * Always returns true, does nothing.
  88. *
  89. * @return true
  90. */
  91. public function get_setting() {
  92. return true;
  93. }
  94. /**
  95. * Always returns true, does nothing.
  96. *
  97. * @return true
  98. */
  99. public function get_defaultsetting() {
  100. return true;
  101. }
  102. /**
  103. * Always returns '', does not write anything.
  104. *
  105. * @param string $data
  106. * @return string Always returns ''
  107. */
  108. public function write_setting($data) {
  109. // Do not write any setting.
  110. return '';
  111. }
  112. /**
  113. * Checks if $query is one of the available subplugins.
  114. *
  115. * @param string $query The string to search for
  116. * @return bool Returns true if found, false if not
  117. */
  118. public function is_related($query) {
  119. if (parent::is_related($query)) {
  120. return true;
  121. }
  122. $subplugins = get_plugin_list('tinymce');
  123. foreach ($subplugins as $name=>$dir) {
  124. if (stripos($name, $query) !== false) {
  125. return true;
  126. }
  127. $namestr = get_string('pluginname', 'tinymce_'.$name);
  128. if (strpos(textlib::strtolower($namestr), textlib::strtolower($query)) !== false) {
  129. return true;
  130. }
  131. }
  132. return false;
  133. }
  134. /**
  135. * Builds the XHTML to display the control.
  136. *
  137. * @param string $data Unused
  138. * @param string $query
  139. * @return string
  140. */
  141. public function output_html($data, $query='') {
  142. global $CFG, $OUTPUT, $PAGE;
  143. require_once("$CFG->libdir/editorlib.php");
  144. require_once("$CFG->libdir/pluginlib.php");
  145. require_once(__DIR__.'/lib.php');
  146. $tinymce = new tinymce_texteditor();
  147. $pluginmanager = plugin_manager::instance();
  148. // display strings
  149. $strbuttons = get_string('availablebuttons', 'editor_tinymce');
  150. $strdisable = get_string('disable');
  151. $strenable = get_string('enable');
  152. $strname = get_string('name');
  153. $strsettings = get_string('settings');
  154. $struninstall = get_string('uninstallplugin', 'admin');
  155. $strversion = get_string('version');
  156. $subplugins = get_plugin_list('tinymce');
  157. $return = $OUTPUT->heading(get_string('subplugintype_tinymce_plural', 'editor_tinymce'), 3, 'main', true);
  158. $return .= $OUTPUT->box_start('generalbox tinymcesubplugins');
  159. $table = new html_table();
  160. $table->head = array($strname, $strbuttons, $strversion, $strenable, $strsettings, $struninstall);
  161. $table->align = array('left', 'left', 'center', 'center', 'center', 'center');
  162. $table->data = array();
  163. $table->width = '100%';
  164. // Iterate through subplugins.
  165. foreach ($subplugins as $name => $dir) {
  166. $namestr = get_string('pluginname', 'tinymce_'.$name);
  167. $version = get_config('tinymce_'.$name, 'version');
  168. if ($version === false) {
  169. $version = '';
  170. }
  171. $plugin = $tinymce->get_plugin($name);
  172. $plugininfo = $pluginmanager->get_plugin_info('tinymce_'.$name);
  173. // Add hide/show link.
  174. if (!$version) {
  175. $hideshow = '';
  176. $displayname = html_writer::tag('span', $name, array('class'=>'error'));
  177. } else if ($plugininfo->is_enabled()) {
  178. $url = new moodle_url('/lib/editor/tinymce/subplugins.php', array('sesskey'=>sesskey(), 'return'=>'settings', 'disable'=>$name));
  179. $hideshow = html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/hide'), 'class'=>'iconsmall', 'alt'=>$strdisable));
  180. $hideshow = html_writer::link($url, $hideshow);
  181. $displayname = html_writer::tag('span', $namestr);
  182. } else {
  183. $url = new moodle_url('/lib/editor/tinymce/subplugins.php', array('sesskey'=>sesskey(), 'return'=>'settings', 'enable'=>$name));
  184. $hideshow = html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/show'), 'class'=>'iconsmall', 'alt'=>$strenable));
  185. $hideshow = html_writer::link($url, $hideshow);
  186. $displayname = html_writer::tag('span', $namestr, array('class'=>'dimmed_text'));
  187. }
  188. if ($PAGE->theme->resolve_image_location('icon', 'tinymce_' . $name, false)) {
  189. $icon = $OUTPUT->pix_icon('icon', '', 'tinymce_' . $name, array('class' => 'icon pluginicon'));
  190. } else {
  191. $icon = $OUTPUT->pix_icon('spacer', '', 'moodle', array('class' => 'icon pluginicon noicon'));
  192. }
  193. $displayname = $icon . ' ' . $displayname;
  194. // Add available buttons.
  195. $buttons = implode(', ', $plugin->get_buttons());
  196. $buttons = html_writer::tag('span', $buttons, array('class'=>'tinymcebuttons'));
  197. // Add settings link.
  198. if (!$version) {
  199. $settings = '';
  200. } else if ($url = $plugininfo->get_settings_url()) {
  201. $settings = html_writer::link($url, $strsettings);
  202. } else {
  203. $settings = '';
  204. }
  205. // Add uninstall info.
  206. if ($version) {
  207. $url = new moodle_url($plugininfo->get_uninstall_url(), array('return'=>'settings'));
  208. $uninstall = html_writer::link($url, $struninstall);
  209. } else {
  210. $uninstall = '';
  211. }
  212. // Add a row to the table.
  213. $table->data[] = array($displayname, $buttons, $version, $hideshow, $settings, $uninstall);
  214. }
  215. $return .= html_writer::table($table);
  216. $return .= html_writer::tag('p', get_string('tablenosave', 'admin'));
  217. $return .= $OUTPUT->box_end();
  218. return highlight($query, $return);
  219. }
  220. }
  221. class editor_tinymce_json_setting_textarea extends admin_setting_configtextarea {
  222. /**
  223. * Returns an XHTML string for the editor
  224. *
  225. * @param string $data
  226. * @param string $query
  227. * @return string XHTML string for the editor
  228. */
  229. public function output_html($data, $query='') {
  230. $result = parent::output_html($data, $query);
  231. $data = trim($data);
  232. if ($data) {
  233. $decoded = json_decode($data, true);
  234. // Note: it is not very nice to abuse these file classes, but anyway...
  235. if (is_array($decoded)) {
  236. $valid = '<span class="pathok">&#x2714;</span>';
  237. } else {
  238. $valid = '<span class="patherror">&#x2718;</span>';
  239. }
  240. $result = str_replace('</textarea>', '</textarea>'.$valid, $result);
  241. }
  242. return $result;
  243. }
  244. }