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

  • WhCountries
  • WhDatePickerHelper
  • WhDropDownInputWidget
  • WhFonts
  • WhFontSizes
  • WhGoogleFonts
  • WhInputWidget
  • WhLanguages
  • WhPhone
  • WhSelectBox
  • WhStates
  • WhTimePickerHelper
  • WhTimezones
  1 <?php
  2  /**
  3  * 
  4  * WhInputWidget.php
  5  *
  6  * Date: 06/09/14
  7  * Time: 13:48
  8  * @author Antonio Ramirez <amigo.cobos@gmail.com>
  9  * @link http://www.ramirezcobos.com/
 10  * @link http://www.2amigos.us/
 11  */
 12 
 13 Yii::import('bootstrap.helpers.TbArray');
 14 Yii::import('bootstrap.helpers.TbHtml');
 15 
 16 class WhInputWidget extends CInputWidget {
 17     /**
 18      * @var array the options for the Bootstrap FormHelper plugin.
 19      */
 20     public $pluginOptions = array();
 21     /**
 22      * @var array the event handlers for the underlying Bootstrap FormHelper input JS plugin.
 23      */
 24     public $clientEvents = array();
 25     /**
 26      * @var string the language code to use (default is english, no need to be set). Every plugin has its own available
 27      * languages.
 28      */
 29     public $language;
 30     /**
 31      * @var string in case you add your own language file.
 32      */
 33     public $languagePath;
 34     public $readOnly = false;
 35     /**
 36      * @inheritdoc
 37      */
 38     public function init()
 39     {
 40         parent::init();
 41 
 42         if (!$this->hasModel() && $this->name === null) {
 43             throw new CException("Either 'name', or 'model' and 'attribute' properties must be specified.");
 44         }
 45         if (!isset($this->htmlOptions['id'])) {
 46             $this->htmlOptions['id'] = $this->hasModel()
 47                 ? CHtml::activeId($this->model, $this->attribute)
 48                 : $this->getId();
 49         }
 50 
 51         $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
 52 
 53         $this->htmlOptions = TbArray::merge($this->asDataAttributes($this->pluginOptions), $this->htmlOptions);
 54         $this->pluginOptions = false;
 55 
 56         if ($this->hasModel()) {
 57             $this->htmlOptions['data-name'] = CHtml::activeId($this->model, $this->attribute);
 58             $this->htmlOptions['data-value'] = CHtml::value($this->model, $this->attribute);
 59         } else {
 60             $this->htmlOptions['data-name'] = $this->name;
 61             $this->htmlOptions['data-value'] = $this->value;
 62         }
 63 
 64     }
 65 
 66     /**
 67      * Converts client options to HTML5 data- attributes.
 68      * @param array $options the options to convert
 69      * @return array
 70      */
 71     protected function asDataAttributes($options)
 72     {
 73         $data = [];
 74         foreach ($options as $key => $value) {
 75             $data["data-$key"] = is_bool($value) ? ($value ? 'true' : 'false') : $value;
 76         }
 77         return $data;
 78     }
 79 
 80     /**
 81      * Registers a specific Bootstrap plugin and the related events
 82      * @param string $name the name of the Bootstrap helper plugin
 83      */
 84     protected function registerPlugin($name)
 85     {
 86         $path = __DIR__ . DIRECTORY_SEPARATOR . 'assets';
 87         $assetsUrl = $this->getAssetsUrl($path);
 88 
 89         /* @var $cs CClientScript */
 90         $cs = Yii::app()->getClientScript();
 91         $cs->registerCssFile($assetsUrl . "/css/bootstrap-formhelpers.min.css");
 92         $cs->registerScriptFile($assetsUrl . "/js/bootstrap-formhelpers.min.js", CClientScript::POS_END);
 93 
 94         if($this->language) {
 95             $fname = "bootstrap-formhelpers-" . (substr($name, 3)) . ".{$this->language}.js";
 96             $languageFile = $this->languagePath ? : $assetsUrl . "/i18n/{$this->language}/{$fname}";
 97             $cs->registerScriptFile($languageFile);
 98         }
 99 
100         $id = $this->htmlOptions['id'];
101         $js = array();
102         if ($this->pluginOptions !== false) {
103             $options = empty($this->pluginOptions) ? '' : CJavaScript::encode($this->pluginOptions);
104             $js[] = "jQuery('#$id').{$name}({$options});";
105         }
106 
107         if (!empty($this->clientEvents)) {
108             foreach ($this->clientEvents as $event => $handler) {
109                 $js[] = "jQuery('#$id').on('$event', $handler);";
110             }
111         }
112         if(count($js))
113         {
114             $cs->registerScript($id, implode("\n", $js));
115         }
116     }
117 
118 } 
YiiWheels API documentation generated by ApiGen 2.8.0