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  * WhCountries 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 WhCountries extends CInputWidget
 16 {
 17     /**
 18      * Editor options that will be passed to the editor.
 19      *
 20      * - country
 21      * - countryList
 22      * - flags
 23      * @see http://vincentlamanna.com/BootstrapFormHelpers/country.html
 24      */
 25     public $pluginOptions = array();
 26 
 27     /**
 28      * @var bool whether to display the language selection read only or not.
 29      */
 30     public $readOnly = false;
 31 
 32     /**
 33      * @var bool whether to use bootstrap helper select Box widget
 34      */
 35     public $useHelperSelectBox = false;
 36 
 37     /**
 38      * @var array extra config options for helper select box
 39      */
 40     public $helperOptions = array();
 41 
 42 
 43     /**
 44      * Widget's initialization method
 45      * @throws CException
 46      */
 47     public function init()
 48     {
 49 
 50         $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
 51 
 52         TbHtml::addCssClass('bfh-countries', $this->htmlOptions);
 53     }
 54 
 55     /**
 56      * Runs the widget.
 57      */
 58     public function run()
 59     {
 60         $this->renderField();
 61         $this->registerClientScript();
 62     }
 63 
 64     /**
 65      * Renders the input file field
 66      */
 67     public function renderField()
 68     {
 69         list($name, $id) = $this->resolveNameID();
 70 
 71         TbArray::defaultValue('id', $id, $this->htmlOptions);
 72         TbArray::defaultValue('name', $name, $this->htmlOptions);
 73 
 74         if ($this->useHelperSelectBox) {
 75             $select = Yii::createComponent(CMap::mergeArray($this->helperOptions, array(
 76                 'class' => 'yiiwheels.widgets.formhelpers.WhSelectBox',
 77                 'htmlOptions' => $this->htmlOptions,
 78                 'model' => $this->model,
 79                 'attribute' => $this->attribute,
 80                 'name' => $this->name,
 81                 'value' => $this->value,
 82                 'wrapperOptions' => array(
 83                     'class' => 'bfh-countries',
 84                     'data-country' => $this->hasModel() ? $this->model->{$this->attribute} : $this->value,
 85                     'data-flags' => isset($this->pluginOptions['flags']) && $this->pluginOptions['flags']
 86                         ? 'true'
 87                         : 'false',
 88                 )
 89             )));
 90             $select->init();
 91             $select->run();
 92         } else {
 93             $this->htmlOptions['data-country'] = $this->hasModel()
 94                 ? $this->model->{$this->attribute}
 95                 : $this->value;
 96 
 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         if (isset($this->pluginOptions['flags']) && $this->pluginOptions['flags'] == true) {
123             $cs->registerCssFile($assetsUrl . '/css/bootstrap-formhelpers-countries.flags.css');
124         }
125         $cs->registerScriptFile($assetsUrl . '/js/bootstrap-formhelpers-countries.en_US.js');
126         $cs->registerScriptFile($assetsUrl . '/js/bootstrap-formhelpers-countries.js');
127 
128         /* initialize plugin */
129         if (!$this->useHelperSelectBox) {
130             $selector = '#' . TbArray::getValue('id', $this->htmlOptions, $this->getId());
131             $this->getApi()->registerPlugin('bfhcountries', $selector, $this->pluginOptions);
132         }
133     }
134 }
YiiWheels API documentation generated by ApiGen 2.8.0