YiiWheels
  • Package
  • Class
  • Tree

Packages

  • None
  • yiiwheels
    • behaviors
    • widgets
    • widgets
      • ace
      • box
      • datepicker
      • daterangepicker
      • datetimepicker
      • detail
      • fileupload
      • fileuploader
      • gallery
      • google
      • grid
        • behaviors
        • operations
      • highcharts
      • maskInput
      • maskmoney
      • modal
      • multiselect
      • rangeslider
      • redactor
      • select2
      • sparklines
      • switch
      • timeago
      • timepicker
      • toggle
      • typeahead

Classes

  • WhRedactor
  1 <?php
  2 /**
  3  * WhRedactor class
  4  *
  5  * @author Antonio Ramirez <amigo.cobos@gmail.com>
  6  * @copyright Copyright &copy; 2amigos.us 2013-
  7  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  8  * @package YiiWheels.widgets.redactor
  9  * @uses YiiStrap.helpers.TbArray
 10  */
 11 Yii::import('bootstrap.helpers.TbArray');
 12 
 13 class WhRedactor extends CInputWidget
 14 {
 15     /**
 16      * Editor options that will be passed to the editor
 17      * @see http://imperavi.com/redactor/docs/
 18      */
 19     public $pluginOptions = array();
 20 
 21     /**
 22      * Debug mode
 23      * Used to publish full js file instead of min version
 24      */
 25     public $debugMode = false;
 26 
 27     /**
 28      * Widget's init function
 29      */
 30     public function init()
 31     {
 32 
 33         $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
 34 
 35         if (!$style = TbArray::popValue('style', $this->htmlOptions, '')) {
 36             $this->htmlOptions['style'] = $style;
 37         }
 38 
 39         $width                      = TbArray::getValue('width', $this->htmlOptions, '100%');
 40         $height                     = TbArray::popValue('height', $this->htmlOptions, '450px');
 41         $this->htmlOptions['style'] = "width:{$width};height:{$height};" . $this->htmlOptions['style'];
 42     }
 43 
 44     /**
 45      * Runs the widget.
 46      */
 47     public function run()
 48     {
 49         $this->renderField();
 50         $this->registerClientScript();
 51     }
 52 
 53     /**
 54      * Renders field
 55      */
 56     public function renderField()
 57     {
 58         list($name, $id) = $this->resolveNameID();
 59 
 60         TbArray::defaultValue('id', $id, $this->htmlOptions);
 61         TbArray::defaultValue('name', $name, $this->htmlOptions);
 62 
 63         if ($this->hasModel()) {
 64             echo CHtml::activeTextArea($this->model, $this->attribute, $this->htmlOptions);
 65         } else {
 66             echo CHtml::textArea($name, $this->value, $this->htmlOptions);
 67         }
 68 
 69     }
 70 
 71     /**
 72      * Registers required client script for bootstrap select2. It is not used through bootstrap->registerPlugin
 73      * in order to attach events if any
 74      */
 75     public function registerClientScript()
 76     {
 77         /* publish assets dir */
 78         $path      = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
 79         $assetsUrl = $this->getAssetsUrl($path);
 80 
 81         /* @var $cs CClientScript */
 82         $cs = Yii::app()->getClientScript();
 83 
 84         $script = $this->debugMode
 85             ? 'redactor.js'
 86             : 'redactor.min.js';
 87 
 88         $cs->registerCssFile($assetsUrl . '/css/redactor.css');
 89         $cs->registerScriptFile($assetsUrl . '/js/' . $script, CClientScript::POS_END);
 90 
 91         /* register language */
 92         $language = TbArray::getValue('lang', $this->pluginOptions);
 93         if (!empty($language) && $language != 'en') {
 94             $cs->registerScriptFile($assetsUrl . '/js/langs/' . $language . '.js', CClientScript::POS_END);
 95         }
 96 
 97         /* register plugins (if any) */
 98         $this->registerPlugins($assetsUrl);
 99 
100         /* initialize plugin */
101         $selector = '#' . TbArray::getValue('id', $this->htmlOptions, $this->getId());
102 
103         $this->getApi()->registerPlugin('redactor', $selector, $this->pluginOptions);
104     }
105 
106     /**
107      * @param $assetsUrl
108      */
109     protected function registerPlugins($assetsUrl)
110     {
111         if (isset($this->pluginOptions['plugins'])) {
112             $ds          = DIRECTORY_SEPARATOR;
113             $pluginsPath = __DIR__ . $ds . 'assets' . $ds . 'js' . $ds . 'plugins' . $ds;
114             $pluginsUrl  = $assetsUrl . '/js/plugins/';
115             $scriptTypes = array('css', 'js');
116 
117             foreach ($this->pluginOptions['plugins'] as $pluginName) {
118                 foreach ($scriptTypes as $type) {
119                     if (@file_exists($pluginsPath . $pluginName . $ds . $pluginName . '.' . $type)) {
120                         Yii::app()->clientScript->registerScriptFile(
121                             $pluginsUrl . '/' .
122                                 $pluginName . '/' .
123                                 $pluginName . '.' .
124                                 $type
125                         );
126                     }
127                 }
128             }
129         }
130     }
131 }
132 
YiiWheels API documentation generated by ApiGen 2.8.0