Skip to content

Commit

Permalink
Refactoring the helper a little
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Krämer committed Nov 18, 2013
1 parent 299baed commit 12c5169
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Test/Case/View/Helper/TinyMceHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function testEditor() {
$this->TinyMCE->configs = $this->configs;
$this->TinyMCE->editor('simple');

$this->expectException('OutOfBoundsException');
$this->expectException('RuntimeException');
$this->TinyMCE->editor('invalid-config');
}

Expand Down
18 changes: 14 additions & 4 deletions View/Helper/TinyMCEHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class TinyMCEHelper extends AppHelper {
*
* @var array
*/
public $helpers = array('Html');
public $helpers = array(
'Html'
);

/**
* Configuration
Expand All @@ -37,7 +39,10 @@ class TinyMCEHelper extends AppHelper {
*
* @var array
*/
protected $_defaults = array();
protected $_defaults = array(
'script' => '/TinyMCE/js/tiny_mce/tiny_mce.js',
'loadScript' => true,
);

/**
* Constructor
Expand All @@ -51,12 +56,14 @@ public function __construct(View $View, $settings = array()) {
if (!empty($configs) && is_array($configs)) {
$this->configs = $configs;
}
$this->settings = array_merge($this->_defaults, $settings);
}

/**
* Adds a new editor to the script block in the head
*
* @see http://www.tinymce.com/wiki.php/Configuration for a list of keys
* @throws RuntimeException
* @param mixed If array camel cased TinyMCE Init config keys, if string it checks if a config with that name exists
* @return void
*/
Expand All @@ -65,7 +72,7 @@ public function editor($options = array()) {
if (isset($this->configs[$options])) {
$options = $this->configs[$options];
} else {
throw new OutOfBoundsException(sprintf(__('Invalid TinyMCE configuration preset %s'), $options));
throw new RuntimeException(sprintf(__('Invalid TinyMCE configuration preset %s'), $options));
}
}
$options = array_merge($this->_defaults, $options);
Expand All @@ -74,6 +81,7 @@ public function editor($options = array()) {
foreach ($options as $option => $value) {
$lines .= Inflector::underscore($option) . ' : "' . $value . '",' . "\n";
}

// remove last comma from lines to avoid the editor breaking in Internet Explorer
$lines = rtrim($lines);
$lines = rtrim($lines, ',');
Expand All @@ -92,7 +100,9 @@ public function beforeRender($viewFile) {
if ($appOptions !== false && is_array($appOptions)) {
$this->_defaults = $appOptions;
}
$this->Html->script('/TinyMCE/js/tiny_mce/tiny_mce.js', array('inline' => false));
if ($this->settings['loadScript'] === true) {
$this->Html->script($this->settings['script'], array('inline' => false));
}
}

}

0 comments on commit 12c5169

Please sign in to comment.