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

  • WhDateRangePicker
  1 <?php
  2 /**
  3  * WhDateRangePicker widget class
  4  * A simple implementation for date range picker for Twitter Bootstrap
  5  * @see <http://www.dangrossman.info/2012/08/20/a-date-range-picker-for-twitter-bootstrap/>
  6  *
  7  * @author Antonio Ramirez <amigo.cobos@gmail.com>
  8  * @copyright Copyright &copy; 2amigos.us 2013-
  9  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 10  * @package YiiWheels.widgets.daterangepicker
 11  * @uses YiiStrap.helpers.TbArray
 12  */
 13 Yii::import('bootstrap.helpers.TbArray');
 14 
 15 class WhDateRangePicker extends CInputWidget
 16 {
 17 
 18     /**
 19      * @var string $selector if provided, then no input field will be rendered. It will write the JS code for the
 20      * specified selector.
 21      */
 22     public $selector;
 23 
 24     /**
 25      * @var string JS Callback for Daterange picker
 26      */
 27     public $callback;
 28 
 29     /**
 30      * @var array pluginOptions to be passed to daterange picker plugin
 31      */
 32     public $pluginOptions = array();
 33 
 34     /**
 35      * Initializes the widget.
 36      */
 37     public function init()
 38     {
 39         $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
 40         $this->htmlOptions['id'] = TbArray::getValue('id', $this->htmlOptions, $this->getId());
 41     }
 42 
 43     /**
 44      * Runs the widget.
 45      */
 46     public function run()
 47     {
 48         $this->renderField();
 49         $this->registerClientScript();
 50     }
 51 
 52     /**
 53      * Renders the field if no selector has been provided
 54      */
 55     public function renderField()
 56     {
 57         if (null === $this->selector) {
 58             list($name, $id) = $this->resolveNameID();
 59 
 60             if ($this->hasModel()) {
 61                 echo TbHtml::activeTextField($this->model, $this->attribute, $this->htmlOptions);
 62             } else {
 63                 echo TbHtml::textField($name, $this->value, $this->htmlOptions);
 64             }
 65 
 66             $this->setLocaleSettings();
 67         }
 68     }
 69 
 70     /**
 71      *
 72      * If user did not provided the names of weekdays and months in $this->pluginOptions['locale']
 73      *  (which he should not care about anyway)
 74      *  then we populate this names from Yii's locales database.
 75      *
 76      * <strong>Heads up!</strong> This method works with the local properties directly.
 77      */
 78     private function setLocaleSettings()
 79     {
 80         $this->setDaysOfWeekNames();
 81         $this->setMonthNames();
 82     }
 83 
 84     /**
 85      * Sets days of week names if no locale settings were made to the plugin options.
 86      */
 87     private function setDaysOfWeekNames()
 88     {
 89         if (empty($this->pluginOptions['locale']['daysOfWeek'])) {
 90             $this->pluginOptions['locale']['daysOfWeek'] = Yii::app()->locale->getWeekDayNames('narrow', true);
 91         }
 92     }
 93 
 94     /**
 95      * Sets month names if no locale settings were made to the plugin options.
 96      */
 97     private function setMonthNames()
 98     {
 99         if (empty($this->pluginOptions['locale']['monthNames'])) {
100             $this->pluginOptions['locale']['monthNames'] = array_values(
101                 Yii::app()->locale->getMonthNames('wide', true)
102             );
103         }
104     }
105 
106     /**
107      *
108      * Registers required css js files
109      */
110     public function registerClientScript()
111     {
112         /* publish assets dir */
113         $path      = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
114         $assetsUrl = $this->getAssetsUrl($path);
115 
116         /* register required moment.js */
117         $this->getYiiWheels()->registerAssetJs('moment.min.js');
118 
119         /* @var $cs CClientScript */
120         $cs = Yii::app()->getClientScript();
121 
122         $cs->registerCssFile($assetsUrl . '/css/daterangepicker.css');
123         $cs->registerScriptFile($assetsUrl . '/js/daterangepicker.js', CClientScript::POS_END);
124 
125         /* initialize plugin */
126         $selector = null === $this->selector
127             ? '#' . TbArray::getValue('id', $this->htmlOptions, $this->getId())
128             : $this->selector;
129 
130         $callback = ($this->callback instanceof CJavaScriptExpression)
131             ? $this->callback
132             : ($this->callback === null ? '' : new CJavaScriptExpression($this->callback));
133 
134         $cs->registerScript(
135             __CLASS__ . '#' . $this->getId(),
136             '$("' . $selector . '").daterangepicker(' .
137                 CJavaScript::encode($this->pluginOptions) .
138                 ($callback ? ', ' . CJavaScript::encode($callback) : '') .
139                 ');'
140         );
141     }
142 }
143 
YiiWheels API documentation generated by ApiGen 2.8.0