Ckeditor.php

Go to the documentation of this file.
00001 <?php
00002 
00036 class Robo47_View_Helper_Ckeditor extends Zend_View_Helper_FormTextarea
00037 {
00041     const INIT_MODE_JQUERY = 'jquery';
00042 
00046     const INIT_MODE_SCRIPT = 'script';
00047 
00053     protected static $_defaultOptions = array();
00059     protected $_initMode = 'script';
00065     protected $_placement = 'append';
00071     protected $_editorOptions = '';
00072 
00076     public function __construct($options = array())
00077     {
00078         $this->setOptions(self::$_defaultOptions);
00079         $this->setOptions($options);
00080     }
00081 
00088     public function setOptions($options)
00089     {
00090         foreach ($options as $option => $value) {
00091             switch ($option) {
00092                 case 'initMode':
00093                     $this->setInitMode($value);
00094                     break;
00095                 case 'placement':
00096                     $this->setPlacement($value);
00097                     break;
00098                 case 'editorOptions':
00099                     $this->setEditorOptions($value);
00100                     break;
00101                 default:
00102                     break;
00103             }
00104         }
00105         return $this;
00106     }
00107 
00113     public function getOptions()
00114     {
00115         return array(
00116             'initMode' => $this->getInitMode(),
00117             'placement' => $this->getPlacement(),
00118             'editorOptions' => $this->getEditorOptions(),
00119         );
00120     }
00121 
00127     public function getInitMode()
00128     {
00129         return $this->_initMode;
00130     }
00131 
00137     public function getPlacement()
00138     {
00139         return $this->_placement;
00140     }
00141 
00147     public function getEditorOptions()
00148     {
00149         return $this->_editorOptions;
00150     }
00151 
00158     public function setInitMode($initMode)
00159     {
00160         $initMode = strtolower($initMode);
00161         switch ($initMode) {
00162             case self::INIT_MODE_SCRIPT:
00163             case self::INIT_MODE_JQUERY:
00164                 $this->_initMode = $initMode;
00165                 break;
00166             default:
00167                 $message = 'Invalid initMode: ' . $initMode;
00168                 throw new Robo47_View_Helper_Exception($message);
00169         }
00170         return $this;
00171     }
00172 
00179     public function setPlacement($placement)
00180     {
00181         $placement = strtolower($placement);
00182         switch ($placement) {
00183             case 'append':
00184             case 'prepend':
00185                 $this->_placement = $placement;
00186                 break;
00187             default:
00188                 $message = 'Invalid placement: ' . $placement;
00189                 throw new Robo47_View_Helper_Exception($message);
00190         }
00191         return $this;
00192     }
00193 
00200     public function setEditorOptions($options)
00201     {
00202         $this->_editorOptions = $options;
00203         return $this;
00204     }
00205 
00211     public static function setDefaultOptions($options = array())
00212     {
00213         if ($options instanceof Zend_Config) {
00214             $options = $options->toArray();
00215         }
00216         self::$_defaultOptions = $options;
00217     }
00218 
00228     public function ckeditor($name, $value = null, $attribs = null)
00229     {
00230         if (!isset($attribs['id'])) {
00231             $message = 'Cant create CDKEditor for textarea without an id';
00232             throw new Robo47_View_Helper_Exception($message);
00233         }
00234         $textArea = parent::formTextarea($name, $value, $attribs);
00235 
00236         $ckeditorOptions = $this->getEditorOptions();
00237         $id = $attribs['id'];
00238         if (!empty($ckeditorOptions)) {
00239             $ckeditorCode = "CKEDITOR.replace('{$id}', {$ckeditorOptions});";
00240         } else {
00241             $ckeditorCode = "CKEDITOR.replace('{$id}');";
00242         }
00243 
00244         $initMode = $this->getInitMode();
00245 
00246         switch ($initMode) {
00247             case self::INIT_MODE_SCRIPT:
00248                 $code = '<script type="text/javascript">' . PHP_EOL .
00249                     '//<![CDATA[' . PHP_EOL .
00250                     $ckeditorCode . PHP_EOL .
00251                     '//]]>' . PHP_EOL .
00252                     '</script>';
00253                 $returnCode = $textArea . $code;
00254                 break;
00255             case self::INIT_MODE_JQUERY:
00256                 $headScript = $this->view->HeadScript();
00257                 /* @var $headScript Zend_View_Helper_HeadScript */
00258                 $code = '$(document).ready( function() { ' . PHP_EOL .
00259                     $ckeditorCode . PHP_EOL .
00260                     '});' . PHP_EOL;
00261                 $placement = $this->getPlacement();
00262                 $headScript->headScript('script', $code, $placement);
00263                 $returnCode = $textArea;
00264                 break;
00265         }
00266         return $returnCode;
00267     }
00268 
00275     public static function setDefaultOption($option, $value)
00276     {
00277         self::$_defaultOptions[$option] = $value;
00278     }
00279 
00287     public static function getDefaultOption($option, $default = null)
00288     {
00289         if (isset(self::$_defaultOptions[$option])) {
00290             return self::$_defaultOptions[$option];
00291         } else {
00292             return $default;
00293         }
00294     }
00295 
00301     public static function getDefaultOptions()
00302     {
00303         return self::$_defaultOptions;
00304     }
00305 
00309     public static function unsetDefaultOptions()
00310     {
00311         self::$_defaultOptions = array();
00312     }
00313 }