YiiWheels
  • Package
  • Class
  • Tree

Packages

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

Classes

  • WhCountries
  • WhDatePickerHelper
  • WhFonts
  • WhFontSizes
  • WhGoogleFonts
  • WhLanguages
  • WhPhone
  • WhSelectBox
  • WhStates
  • WhTimePickerHelper
  • WhTimezones
  1 <?php
  2 /**
  3  * WhStates widget 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.formhelpers
  9  * @uses YiiStrap.helpers.TbArray
 10  * @uses YiiStrap.helpers.TbHtml
 11  */
 12 Yii::import('bootstrap.helpers.TbArray');
 13 Yii::import('bootstrap.helpers.TbHtml');
 14 
 15 class WhStates extends CInputWidget
 16 {
 17     /**
 18      * Editor options that will be passed to the editor.
 19      *
 20      * - country
 21      * - state
 22      * @see http://vincentlamanna.com/BootstrapFormHelpers/state.html
 23      */
 24     public $pluginOptions = array();
 25 
 26     /**
 27      * @var bool whether to display the language selection read only or not.
 28      */
 29     public $readOnly = false;
 30 
 31     /**
 32      * @var bool whether to use bootstrap helper select Box widget
 33      */
 34     public $useHelperSelectBox = false;
 35 
 36     /**
 37      * @var array extra config options for helper select box
 38      */
 39     public $helperOptions = array();
 40 
 41 
 42     /**
 43      * Widget's initialization method
 44      * @throws CException
 45      */
 46     public function init()
 47     {
 48         if (!isset($this->pluginOptions['country'])) {
 49             throw new CException(Yii::t('zii', '$pluginOptions["country"] cannot be blank.'));
 50         }
 51         $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
 52 
 53         TbHtml::addCssClass('bfh-states', $this->htmlOptions);
 54     }
 55 
 56     /**
 57      * Runs the widget.
 58      */
 59     public function run()
 60     {
 61         $this->renderField();
 62         $this->registerClientScript();
 63     }
 64 
 65     /**
 66      * Renders the input file field
 67      */
 68     public function renderField()
 69     {
 70         list($name, $id) = $this->resolveNameID();
 71 
 72         TbArray::defaultValue('id', $id, $this->htmlOptions);
 73         TbArray::defaultValue('name', $name, $this->htmlOptions);
 74 
 75         $this->htmlOptions['data-country'] = $this->pluginOptions['country'];
 76         $this->pluginOptions['state'] = $this->htmlOptions['data-state'] = $this->hasModel()
 77             ? $this->model->{$this->attribute}
 78             : $this->value;
 79 
 80         if ($this->useHelperSelectBox) {
 81             $select = Yii::createComponent(CMap::mergeArray($this->helperOptions, array(
 82                 'class' => 'yiiwheels.widgets.formhelpers.WhSelectBox',
 83                 'htmlOptions' => $this->htmlOptions,
 84                 'model' => $this->model,
 85                 'attribute' => $this->attribute,
 86                 'name' => $this->name,
 87                 'value' => $this->value,
 88                 'wrapperOptions' => array(
 89                     'class' => 'bfh-countries',
 90                     'data-country' => $this->hasModel() ? $this->model->{$this->attribute} : $this->value,
 91                     'data-flags' => isset($this->pluginOptions['flags']) ? 'true' : 'false'
 92                 )
 93             )));
 94             $select->init();
 95             $select->run();
 96         } else {
 97             if (!$this->readOnly) {
 98                 if ($this->hasModel()) {
 99                     echo CHtml::activeDropDownList($this->model, $this->attribute, array(), $this->htmlOptions);
100                 } else {
101                     echo CHtml::dropDownList($name, $this->value, array(), $this->htmlOptions);
102                 }
103             } else {
104                 echo CHtml::tag('span', $this->htmlOptions);
105             }
106         }
107     }
108 
109     /**
110      * Registers client script
111      */
112     public function registerClientScript()
113     {
114         /* publish assets dir */
115         $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
116         $assetsUrl = $this->getAssetsUrl($path);
117 
118         /* @var $cs CClientScript */
119         $cs = Yii::app()->getClientScript();
120 
121         $cs->registerCssFile($assetsUrl . '/css/bootstrap-formhelpers.css');
122 
123         $cs->registerScriptFile($assetsUrl . '/js/bootstrap-formhelpers-states.en_US.js');
124         $cs->registerScriptFile($assetsUrl . '/js/bootstrap-formhelpers-states.js');
125 
126         /* initialize plugin */
127         if (!$this->useHelperSelectBox) {
128             $selector = '#' . TbArray::getValue('id', $this->htmlOptions, $this->getId());
129             $this->getApi()->registerPlugin('bfhstates', $selector, $this->pluginOptions);
130         }
131     }
132 }
YiiWheels API documentation generated by ApiGen 2.8.0