diff --git a/lib/editor/tinymce/classes/plugin.php b/lib/editor/tinymce/classes/plugin.php index 3c97c963ad9..ed33fa49efc 100644 --- a/lib/editor/tinymce/classes/plugin.php +++ b/lib/editor/tinymce/classes/plugin.php @@ -143,17 +143,30 @@ abstract class editor_tinymce_plugin { * plugins you can set $alwaysadd to false and check the return value * to see if it succeeded. * + * Note: button will not be added if it is already present in any row + * (separator is an exception). + * + * The following example will add the button 'newbutton' after the + * 'existingbutton' if it exists or in the end of the last row otherwise: + *
+ * if ($row = $this->find_button($params, 'existingbutton')) { + * $this->add_button_after($params, $row, 'newbutton', 'existingbutton'); + * } else { + * $this->add_button_after($params, $this->count_button_rows($params), 'newbutton'); + * } + *+ * * @param array $params TinyMCE init parameters array * @param int $row Row to add button to (1 to 3) * @param string $button Identifier of button/plugin * @param string $after Adds button directly after the named plugin * @param bool $alwaysadd If specified $after string not found, add at end - * @return bool True if added + * @return bool True if added or button already exists (in any row) */ protected function add_button_after(array &$params, $row, $button, $after = '', $alwaysadd = true) { - if ($this->is_button_present($params, $button)) { + if ($button !== '|' && $this->find_button($params, $button)) { return true; } @@ -194,17 +207,30 @@ abstract class editor_tinymce_plugin { * plugins you can set $alwaysadd to false and check the return value * to see if it succeeded. * + * Note: button will not be added if it is already present in any row + * (separator is an exception). + * + * The following example will add the button 'newbutton' before the + * 'existingbutton' if it exists or in the end of the last row otherwise: + *
+ * if ($row = $this->find_button($params, 'existingbutton')) { + * $this->add_button_before($params, $row, 'newbutton', 'existingbutton'); + * } else { + * $this->add_button_after($params, $this->count_button_rows($params), 'newbutton'); + * } + *+ * * @param array $params TinyMCE init parameters array * @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 $before string not found, add at start - * @return bool True if added + * @return bool True if added or button already exists (in any row) */ protected function add_button_before(array &$params, $row, $button, $before = '', $alwaysadd = true) { - if ($this->is_button_present($params, $button)) { + if ($button !== '|' && $this->find_button($params, $button)) { return true; } $row = $this->fix_row($params, $row); @@ -235,20 +261,17 @@ abstract class editor_tinymce_plugin { } /** - * Tests if button already present. - * @param array $params - * @param string $button - * @return bool + * Tests if button is already present. + * + * @param array $params TinyMCE init parameters array + * @param string $button button name + * @return false|int false if button is not found, row number otherwise (row numbers start from 1) */ - 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; + protected function find_button(array &$params, $button) { + foreach ($params as $key => $value) { + if (preg_match('/^theme_advanced_buttons(\d+)$/', $key, $matches) && + strpos(','. $value. ',', ','. $button. ',') !== false) { + return (int)$matches[1]; } } return false; @@ -262,21 +285,31 @@ abstract class editor_tinymce_plugin { * @return int requested row if exists, lower number if does not exist. */ 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])) { + if ($row <= 1) { + // Row 1 is always present. + return 1; + } else if (isset($params['theme_advanced_buttons' . $row])) { return $row; + } else { + return $this->count_button_rows($params); } - for($i=$row; $i>=1; $i--) { - $field = 'theme_advanced_buttons' . $i; - if (isset($params[$field])) { - return $i; + } + + /** + * Counts the number of rows in TinyMCE editor (row numbering starts with 1) + * + * @param array $params TinyMCE init parameters array + * @return int the maximum existing row number + */ + protected function count_button_rows(array &$params) { + $maxrow = 1; + foreach ($params as $key => $value) { + if (preg_match('/^theme_advanced_buttons(\d+)$/', $key, $matches) && + (int)$matches[1] > $maxrow) { + $maxrow = (int)$matches[1]; } } - // This should not happen. - return 1; + return $maxrow; } /** diff --git a/lib/editor/tinymce/plugins/dragmath/lib.php b/lib/editor/tinymce/plugins/dragmath/lib.php index d4b8ac70f46..84699f7ed80 100644 --- a/lib/editor/tinymce/plugins/dragmath/lib.php +++ b/lib/editor/tinymce/plugins/dragmath/lib.php @@ -38,8 +38,13 @@ class tinymce_dragmath extends editor_tinymce_plugin { } } - // Add button before 'nonbreaking' in advancedbuttons3. - $this->add_button_before($params, 3, 'dragmath', 'nonbreaking'); + if ($row = $this->find_button($params, 'nonbreaking')) { + // Add button before 'nonbreaking'. + $this->add_button_before($params, $row, 'dragmath', 'nonbreaking'); + } else { + // If 'nonbreaking' is not found, add button in the end of the last row: + $this->add_button_after($params, $this->count_button_rows($params), 'dragmath'); + } // Add JS file, which uses default name. $this->add_js_plugin($params); diff --git a/lib/editor/tinymce/plugins/moodleemoticon/lib.php b/lib/editor/tinymce/plugins/moodleemoticon/lib.php index c2751a52d00..047196a8a25 100644 --- a/lib/editor/tinymce/plugins/moodleemoticon/lib.php +++ b/lib/editor/tinymce/plugins/moodleemoticon/lib.php @@ -39,8 +39,13 @@ class tinymce_moodleemoticon extends editor_tinymce_plugin { } } - // Add button after 'image' in advancedbuttons3. - $this->add_button_after($params, 3, 'moodleemoticon', 'image'); + if ($row = $this->find_button($params, 'image')) { + // Add button after 'image'. + $this->add_button_after($params, $row, 'moodleemoticon', 'image'); + } else { + // If 'image' is not found, add button in the end of the last row. + $this->add_button_after($params, $this->count_button_rows($params), 'moodleemoticon'); + } // Add JS file, which uses default name. $this->add_js_plugin($params); diff --git a/lib/editor/tinymce/plugins/moodlemedia/lib.php b/lib/editor/tinymce/plugins/moodlemedia/lib.php index 83141fe15e8..05bce052f39 100644 --- a/lib/editor/tinymce/plugins/moodlemedia/lib.php +++ b/lib/editor/tinymce/plugins/moodlemedia/lib.php @@ -37,14 +37,17 @@ class tinymce_moodlemedia extends editor_tinymce_plugin { } } - // Add button after emoticon button in advancedbuttons1. - $added = $this->add_button_after($params, 1, 'moodlemedia', 'moodleemoticon', false); - - // Note: We know that the emoticon button has already been added, if it - // exists, because I set the sort order higher for this. So, if no - // emoticon, add after 'image'. - if (!$added) { - $this->add_button_after($params, 1, 'moodlemedia', 'image'); + if ($row = $this->find_button($params, 'moodleemoticon')) { + // Add button after 'moodleemoticon' icon. + $this->add_button_after($params, $row, 'moodlemedia', 'moodleemoticon'); + } else if ($row = $this->find_button($params, 'image')) { + // Note: We know that the plugin emoticon button has already been added + // if it is enabled because this plugin has higher sortorder. + // Otherwise add after 'image'. + $this->add_button_after($params, $row, 'moodlemedia', 'image'); + } else { + // Add this button in the end of the first row (by default 'image' button should be in the first row). + $this->add_button_after($params, 1, 'moodlemedia'); } // Add JS file, which uses default name. diff --git a/lib/editor/tinymce/plugins/moodlenolink/lib.php b/lib/editor/tinymce/plugins/moodlenolink/lib.php index b400f810d5e..d80f172eea2 100644 --- a/lib/editor/tinymce/plugins/moodlenolink/lib.php +++ b/lib/editor/tinymce/plugins/moodlenolink/lib.php @@ -30,8 +30,13 @@ class tinymce_moodlenolink extends editor_tinymce_plugin { protected function update_init_params(array &$params, context $context, array $options = null) { - // Add button after 'unlink' in advancedbuttons1. - $this->add_button_after($params, 1, 'moodlenolink', 'unlink'); + if ($row = $this->find_button($params, 'unlink')) { + // Add button after 'unlink'. + $this->add_button_after($params, $row, 'moodlenolink', 'unlink'); + } else { + // Add this button in the end of the first row (by default 'unlink' button should be in the first row). + $this->add_button_after($params, 1, 'moodlenolink'); + } // Add JS file, which uses default name. $this->add_js_plugin($params); diff --git a/lib/editor/tinymce/plugins/pdw/lib.php b/lib/editor/tinymce/plugins/pdw/lib.php index ad1eee3c530..e23f60affe2 100644 --- a/lib/editor/tinymce/plugins/pdw/lib.php +++ b/lib/editor/tinymce/plugins/pdw/lib.php @@ -34,10 +34,10 @@ class tinymce_pdw extends editor_tinymce_plugin { protected function update_init_params(array &$params, context $context, array $options = null) { - $rowsnumber = $this->count_rows($params); + $rowsnumber = $this->count_button_rows($params); if ($rowsnumber > 1) { // Add button before 'undo' in advancedbuttons1. - $this->add_button_before($params, 1, ' | ', ''); + $this->add_button_before($params, 1, '|', ''); $this->add_button_before($params, 1, 'pdw_toggle', ''); $params['pdw_toggle_on'] = 1; $params['pdw_toggle_toolbars'] = join(',', range(2, $rowsnumber)); @@ -48,19 +48,11 @@ class tinymce_pdw extends editor_tinymce_plugin { } /** - * Counts the number of rows in TinyMCE editor + * Gets the order in which to run this plugin * - * @param array $params TinyMCE init parameters array - * @return int the maximum existing row number + * We need pdw plugin to be added the last, so nothing is added before the button. */ - private function count_rows(array &$params) { - for($i = 10; $i >= 1; $i--) { - $field = 'theme_advanced_buttons' . $i; - if (isset($params[$field])) { - return $i; - } - } - // This should not happen. - return 1; + protected function get_sort_order() { + return 100000; } } diff --git a/lib/editor/tinymce/plugins/spellchecker/lib.php b/lib/editor/tinymce/plugins/spellchecker/lib.php index 8c14e08546d..72d63236c95 100644 --- a/lib/editor/tinymce/plugins/spellchecker/lib.php +++ b/lib/editor/tinymce/plugins/spellchecker/lib.php @@ -48,8 +48,10 @@ class tinymce_spellchecker extends editor_tinymce_plugin { // Prevent the built-in spell checker in Firefox, Safari and other sane browsers. unset($params['gecko_spellcheck']); - // Add button after code button in advancedbuttons3. - $added = $this->add_button_after($params, 3, 'spellchecker', 'code', false); + if ($row = $this->find_button($params, 'code')) { + // Add button after 'code'. + $this->add_button_after($params, $row, 'spellchecker', 'code'); + } // Add JS file, which uses default name. $this->add_js_plugin($params); diff --git a/lib/editor/tinymce/tests/editor_test.php b/lib/editor/tinymce/tests/editor_test.php index 7e49bbe0fed..331c2d559fc 100644 --- a/lib/editor/tinymce/tests/editor_test.php +++ b/lib/editor/tinymce/tests/editor_test.php @@ -60,4 +60,126 @@ class editor_tinymce_testcase extends advanced_testcase { $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); } + + public function test_add_button() { + global $CFG; + $plugin = new tinymce_testplugin(__DIR__); + $config = get_config('editor_tinymce'); + $params = array( + 'moodle_config' => $config, + 'entity_encoding' => "raw", + 'plugins' => 'lists,table,style,layer,advhr,advlink,emotions,inlinepopups,' . + 'searchreplace,paste,directionality,fullscreen,nonbreaking,contextmenu,' . + 'insertdatetime,save,iespell,preview,print,noneditable,visualchars,' . + 'xhtmlxtras,template,pagebreak', + 'gecko_spellcheck' => true, + 'theme_advanced_font_sizes' => "1,2,3,4,5,6,7", + 'moodle_plugin_base' => "$CFG->httpswwwroot/lib/editor/tinymce/plugins/", + 'theme_advanced_font_sizes' => "1,2,3,4,5,6,7", + 'theme_advanced_layout_manager' => "SimpleLayout", + 'theme_advanced_buttons1' => 'one,two,|,three,four', + 'theme_advanced_buttons2' => 'five,six', + 'theme_advanced_buttons3' => 'seven,eight,|', + 'theme_advanced_buttons4' => '|,nine', + 'theme_advanced_buttons5' => 'ten,eleven,twelve', + 'theme_advanced_buttons6' => 'thirteen,fourteen', + 'theme_advanced_buttons7' => 'fiveteen', + 'theme_advanced_buttons' => 'zero', // this is a fake entry, it is not a button row. + 'theme_something' => 123, + ); + + // Count number of rows. + $this->assertSame(7, $plugin->test_count_button_rows($params)); + + // Find button - first button in a row. + $this->assertSame(1, $plugin->test_find_button($params, 'one')); + // Find button - last button in a row. + $this->assertSame(4, $plugin->test_find_button($params, 'nine')); + // Find button - middle button in a row. + $this->assertSame(5, $plugin->test_find_button($params, 'eleven')); + // Find button - the only button in a row. + $this->assertSame(7, $plugin->test_find_button($params, 'fiveteen')); + // Find button - button not present. + $this->assertSame(false, $plugin->test_find_button($params, 'sixteen')); + // Find button - button not present. + $this->assertSame(false, $plugin->test_find_button($params, 'zero')); + + // Adding button in the beginning of the row. + $this->assertTrue($plugin->test_add_button_before($params, 1, 'new1', '', true)); + $this->assertSame('new1,one,two,|,three,four', $params['theme_advanced_buttons1']); + // Adding button that already exists (nothing changes). + $this->assertTrue($plugin->test_add_button_before($params, 1, 'new1', '', true)); + $this->assertSame('new1,one,two,|,three,four', $params['theme_advanced_buttons1']); + // Adding button before existing button. + $this->assertTrue($plugin->test_add_button_before($params, 1, 'new2', 'two', true)); + $this->assertSame('new1,one,new2,two,|,three,four', $params['theme_advanced_buttons1']); + // Adding button before another button that does not exist ($alwaysadd = false). + $this->assertTrue($plugin->test_add_button_before($params, 4, 'new3', 'fiveteen', true)); + $this->assertSame('new3,|,nine', $params['theme_advanced_buttons4']); + // Adding button before another button that does not exist ($alwaysadd = false). + $this->assertFalse($plugin->test_add_button_before($params, 4, 'new4', 'fiveteen', false)); + $this->assertSame('new3,|,nine', $params['theme_advanced_buttons4']); + // Adding button into non-existing 0 row. + $this->assertTrue($plugin->test_add_button_before($params, 0, 'new9')); + $this->assertSame('new9,new1,one,new2,two,|,three,four', $params['theme_advanced_buttons1']); + $this->assertFalse(isset($params['theme_advanced_buttons0'])); + // Adding button into non-existing 9 row. + $this->assertTrue($plugin->test_add_button_before($params, 9, 'new10')); + $this->assertSame('new10,fiveteen', $params['theme_advanced_buttons7']); + $this->assertFalse(isset($params['theme_advanced_buttons9'])); + + // Adding button in the end of the row. + $this->assertTrue($plugin->test_add_button_after($params, 5, 'new5', '', true)); + $this->assertSame('ten,eleven,twelve,new5', $params['theme_advanced_buttons5']); + // Adding button that already exists. + $this->assertTrue($plugin->test_add_button_after($params, 5, 'new5', '', true)); + $this->assertSame('ten,eleven,twelve,new5', $params['theme_advanced_buttons5']); + // Adding button after the existing button. + $this->assertTrue($plugin->test_add_button_after($params, 6, 'new6', 'thirteen', true)); + $this->assertSame('thirteen,new6,fourteen', $params['theme_advanced_buttons6']); + // Adding button after another button that does not exist ($alwaysadd = true). + $this->assertTrue($plugin->test_add_button_after($params, 6, 'new7', 'fiveteen', true)); + $this->assertSame('thirteen,new6,fourteen,new7', $params['theme_advanced_buttons6']); + // Adding button after another button that does not exist ($alwaysadd = false). + $this->assertFalse($plugin->test_add_button_after($params, 6, 'new8', 'fiveteen', false)); + $this->assertSame('thirteen,new6,fourteen,new7', $params['theme_advanced_buttons6']); + // Adding button into non-existing 0 row. + $this->assertTrue($plugin->test_add_button_after($params, 0, 'new11')); + $this->assertSame('new9,new1,one,new2,two,|,three,four,new11', $params['theme_advanced_buttons1']); + $this->assertFalse(isset($params['theme_advanced_buttons0'])); + // Adding button into non-existing 9 row. + $this->assertTrue($plugin->test_add_button_after($params, 9, 'new12')); + $this->assertSame('new10,fiveteen,new12', $params['theme_advanced_buttons7']); + $this->assertFalse(isset($params['theme_advanced_buttons9'])); + } } + +/** + * Pseudo plugin class for testing editor_tinymce_plugin protected methods + * + * @package editor_tinymce + * @category phpunit + * @copyright 2013 Marina Glancy + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class tinymce_testplugin extends editor_tinymce_plugin { + protected function update_init_params(array &$params, context $context, array $options = null) { + // Empty function just to make a class not abstract. + } + + public function test_count_button_rows(array &$params) { + return parent::count_button_rows($params); + } + + public function test_find_button(array &$params, $button) { + return parent::find_button($params, $button); + } + + public function test_add_button_after(array &$params, $row, $button, $after = '', $alwaysadd = true) { + return parent::add_button_after($params, $row, $button, $after, $alwaysadd); + } + + public function test_add_button_before(array &$params, $row, $button, $before = '', $alwaysadd = true) { + return parent::add_button_before($params, $row, $button, $before, $alwaysadd); + } +} \ No newline at end of file diff --git a/lib/editor/tinymce/upgrade.txt b/lib/editor/tinymce/upgrade.txt index a8b2c157424..a2551a276d0 100644 --- a/lib/editor/tinymce/upgrade.txt +++ b/lib/editor/tinymce/upgrade.txt @@ -7,6 +7,9 @@ information provided here is intended especially for developers. * added a new plugin to toggle the 2nd and 3rd toolbars * added a new plugin to wrap the toolbars on small screens. This plugin will be automatically added to existing toolbars by replacing the middle occurrence of | with "wrap". +* New functions to make adding of new buttons easier: + - editor_tinymce_plugin::find_button() + - editor_tinymce_plugin::count_button_rows() === 2.5 === diff --git a/version.php b/version.php index 09b985e4159..1310446ac82 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2013071900.02; // YYYYMMDD = weekly release date of this DEV branch +$version = 2013072300.00; // YYYYMMDD = weekly release date of this DEV branch // RR = release increments - 00 in DEV branches // .XX = incremental changes