Skip to content

Commit

Permalink
Merge branch 'w36_MDL-35172_m24_editortoolbar' of git://github.com/sk…
Browse files Browse the repository at this point in the history
…odak/moodle
  • Loading branch information
stronk7 committed Sep 6, 2012
2 parents 7183a1f + 717a993 commit 0cbf023
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 37 deletions.
59 changes: 50 additions & 9 deletions lib/editor/tinymce/classes/plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,12 @@ protected function get_sort_order() {
*/
protected function add_button_after(array &$params, $row, $button,
$after = '', $alwaysadd = true) {
$this->check_row($row);

if ($this->is_button_present($params, $button)) {
return true;
}

$row = $this->fix_row($params, $row);

$field = 'theme_advanced_buttons' . $row;
$old = $params[$field];
Expand Down Expand Up @@ -190,15 +195,19 @@ protected function add_button_after(array &$params, $row, $button,
* to see if it succeeded.
*
* @param array $params TinyMCE init parameters array
* @param int $row Row to add button to (1 to 3)
* @param int $row Row to add button to (1 to 10)
* @param string $button Identifier of button/plugin
* @param string $before Adds button directly before the named plugin
* @param bool $alwaysadd If specified $after string not found, add at start
* @return bool True if added
*/
protected function add_button_before(array &$params, $row, $button,
$before = '', $alwaysadd = true) {
$this->check_row($row);

if ($this->is_button_present($params, $button)) {
return true;
}
$row = $this->fix_row($params, $row);

$field = 'theme_advanced_buttons' . $row;
$old = $params[$field];
Expand Down Expand Up @@ -226,15 +235,47 @@ protected function add_button_before(array &$params, $row, $button,
}

/**
* Checks the row value is valid.
* Tests if button already present.
* @param array $params
* @param string $button
* @return bool
*/
private function is_button_present(array $params, $button) {
for($i=1; $i<=10; $i++) {
$field = 'theme_advanced_buttons' . $i;
if (!isset($params[$field])) {
continue;
}
$buttons = explode(',', $params[$field]);
if (in_array($button, $buttons)) {
return true;
}
}
return false;
}

/**
* Checks the row value is valid, fix if necessary.
*
* @param int $row Row to add button to (1 to 3)
* @throws coding_exception If row value is outside the range 1-3
* @param array $params TinyMCE init parameters array
* @param int $row Row to add button if exists
* @return int requested row if exists, lower number if does not exist.
*/
private function check_row($row) {
if ($row < 1 || $row > 3) {
throw new coding_exception("Invalid row option: $row");
private function fix_row(array &$params, $row) {
$row = ($row < 1) ? 1 : (int)$row;
$row = ($row > 10) ? 10 : $row;

$field = 'theme_advanced_buttons' . $row;
if (isset($params[$field])) {
return $row;
}
for($i=$row; $i>=1; $i--) {
if (isset($params[$field])) {
return $row;
}
}
// This should not happen.
return 1;
}

/**
Expand Down
41 changes: 41 additions & 0 deletions lib/editor/tinymce/db/upgrade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* TinyMCE editor integration upgrade.
*
* @package editor_tinymce
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

function xmldb_editor_tinymce_upgrade($oldversion) {
global $CFG, $DB;

$dbman = $DB->get_manager();


if ($oldversion < 2012083100) {
// Reset redesigned editor toolbar setting.
unset_config('customtoolbar', 'editor_tinymce');
upgrade_plugin_savepoint(true, 2012083100, 'editor', 'tinymce');
}


return true;
}
4 changes: 2 additions & 2 deletions lib/editor/tinymce/lang/en/editor_tinymce.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
$string['availablebuttons'] = 'Available buttons';
$string['common:browseimage'] = 'Find or upload an image...';
$string['common:browsemedia'] = 'Find or upload a sound, video or applet...';
$string['customtoolbar'] = 'Custom editor toolbar';
$string['customtoolbar_desc'] = 'Each line contains a list of comma separated button names, use "|" as a group separator. Leave empty if you want standard toolbar. See <a href="{$a}" target="_blank">{$a}</a> for the list of default TinyMCE buttons.';
$string['customtoolbar'] = 'Editor toolbar';
$string['customtoolbar_desc'] = 'Each line contains a list of comma separated button names, use "|" as a group separator, empty lines are ignored. See <a href="{$a}" target="_blank">{$a}</a> for the list of default TinyMCE buttons.';
$string['fontselectlist'] = 'Available fonts list';
$string['media_dlg:filename'] = 'Filename';
$string['pluginname'] = 'TinyMCE HTML editor';
Expand Down
44 changes: 21 additions & 23 deletions lib/editor/tinymce/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ protected function get_init_params($elementid, array $options=null) {
}

$fontselectlist = empty($config->fontselectlist) ? '' : $config->fontselectlist;
$fontbutton = ($fontselectlist === '') ? '' : 'fontselect,';

$params = array(
'moodle_config' => $config,
Expand All @@ -154,13 +153,6 @@ protected function get_init_params($elementid, array $options=null) {
'theme_advanced_font_sizes' => "1,2,3,4,5,6,7",
'theme_advanced_layout_manager' => "SimpleLayout",
'theme_advanced_toolbar_align' => "left",
'theme_advanced_buttons1' => $fontbutton . 'fontsizeselect,formatselect,|,' .
'undo,redo,|,search,replace,|,fullscreen',
'theme_advanced_buttons2' => 'bold,italic,underline,strikethrough,sub,sup,|,' .
'justifyleft,justifycenter,justifyright,|,' .
'cleanup,removeformat,pastetext,pasteword,|,forecolor,backcolor,|,ltr,rtl',
'theme_advanced_buttons3' => 'bullist,numlist,outdent,indent,|,' .
'link,unlink,|,image,nonbreaking,charmap,table,|,code',
'theme_advanced_fonts' => $fontselectlist,
'theme_advanced_resize_horizontal' => true,
'theme_advanced_resizing' => true,
Expand All @@ -170,6 +162,19 @@ protected function get_init_params($elementid, array $options=null) {
'theme_advanced_statusbar_location' => "bottom",
);

// Should we override the default toolbar layout unconditionally?
$customtoolbar = self::parse_toolbar_setting($config->customtoolbar);
if ($customtoolbar) {
$i = 1;
foreach ($customtoolbar as $line) {
$params['theme_advanced_buttons'.$i] = $line;
$i++;
}
} else {
// At least one line is required.
$params['theme_advanced_buttons1'] = '';
}

if (!empty($options['legacy']) or !empty($options['noclean']) or !empty($options['trusted'])) {
// now deal somehow with non-standard tags, people scream when we do not make moodle code xtml strict,
// but they scream even more when we strip all tags that are not strict :-(
Expand All @@ -188,20 +193,6 @@ protected function get_init_params($elementid, array $options=null) {
// Allow plugins to adjust parameters.
editor_tinymce_plugin::all_update_init_params($params, $context, $options);

// Should we override the default toolbar layout unconditionally?
$customtoolbar = self::parse_toolbar_setting($config->customtoolbar);
if ($customtoolbar) {
unset($params['theme_advanced_buttons1']);
unset($params['theme_advanced_buttons2']);
unset($params['theme_advanced_buttons3']);
unset($params['theme_advanced_buttons4']);
$i = 1;
foreach ($customtoolbar as $line) {
$params['theme_advanced_buttons'.$i] = $line;
$i++;
}
}

// Remove temporary parameters.
unset($params['moodle_config']);

Expand All @@ -221,6 +212,7 @@ public static function parse_toolbar_setting($customtoolbar) {
}
$customtoolbar = str_replace("\r", "\n", $customtoolbar);
$customtoolbar = strtolower($customtoolbar);
$i = 0;
foreach (explode("\n", $customtoolbar) as $line) {
$line = preg_replace('/[^a-z0-9_,\|\-]/', ',', $line);
$line = str_replace('|', ',|,', $line);
Expand All @@ -229,7 +221,13 @@ public static function parse_toolbar_setting($customtoolbar) {
if ($line === '') {
continue;
}
$result[] = $line;
if ($i == 10) {
// Maximum is ten lines, merge the rest to the last line.
$result[9] = $result[9].','.$line;
} else {
$result[] = $line;
$i++;
}
}
return $result;
}
Expand Down
7 changes: 6 additions & 1 deletion lib/editor/tinymce/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@
require_once(__DIR__.'/adminlib.php');
$settings->add(new tiynce_subplugins_settings());
$settings->add(new admin_setting_heading('tinymcegeneralheader', new lang_string('settings'), ''));
$default = "fontselect,fontsizeselect,formatselect,|,undo,redo,|,search,replace,|,fullscreen
bold,italic,underline,strikethrough,sub,sup,|,justifyleft,justifycenter,justifyright,|,cleanup,removeformat,pastetext,pasteword,|,forecolor,backcolor,|,ltr,rtl
bullist,numlist,outdent,indent,|,link,unlink,|,image,nonbreaking,charmap,table,|,code";
$settings->add(new admin_setting_configtextarea('editor_tinymce/customtoolbar',
get_string('customtoolbar', 'editor_tinymce'), get_string('customtoolbar_desc', 'editor_tinymce', 'http://www.tinymce.com/wiki.php/Buttons/controls'), '', PARAM_RAW, 100, 6));
get_string('customtoolbar', 'editor_tinymce'), get_string('customtoolbar_desc', 'editor_tinymce', 'http://www.tinymce.com/wiki.php/Buttons/controls'), $default, PARAM_RAW, 100, 8));
$settings->add(new admin_setting_configtextarea('editor_tinymce/fontselectlist',
get_string('fontselectlist', 'editor_tinymce'), '',
'Trebuchet=Trebuchet MS,Verdana,Arial,Helvetica,sans-serif;Arial=arial,helvetica,sans-serif;Courier New=courier new,courier,monospace;Georgia=georgia,times new roman,times,serif;Tahoma=tahoma,arial,helvetica,sans-serif;Times New Roman=times new roman,times,serif;Verdana=verdana,arial,helvetica,sans-serif;Impact=impact;Wingdings=wingdings', PARAM_RAW));
Expand Down
3 changes: 3 additions & 0 deletions lib/editor/tinymce/tests/editor_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,8 @@ public function test_toolbar_parsing() {

$result = tinymce_texteditor::parse_toolbar_setting("| \n\n| \n \r");
$this->assertSame(array(), $result);

$result = tinymce_texteditor::parse_toolbar_setting("one\ntwo\n\nthree\nfour\nfive\nsix\nseven\neight\nnine\nten");
$this->assertSame(array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten'), $result);
}
}
4 changes: 2 additions & 2 deletions lib/editor/tinymce/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2012081000; // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires = 2012061700; // Requires this Moodle version
$plugin->version = 2012083100; // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires = 2012083100; // Requires this Moodle version
$plugin->component = 'editor_tinymce'; // Full name of the plugin (used for diagnostics)
$plugin->release = '3.6.0'; // This is NOT a directory name, see lib.php if you need to know where is the editor code!

0 comments on commit 0cbf023

Please sign in to comment.