1 <?php
2 /**
3 * WhTimePicker widget class
4 *
5 * @author Antonio Ramirez <amigo.cobos@gmail.com>
6 * @copyright Copyright © 2amigos.us 2013-
7 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
8 * @package YiiWheels.widgets.timepicker
9 * @uses YiiStrap.helpers.TbArray
10 */
11 Yii::import('bootstrap.helpers.TbArray');
12
13 class WhTimePicker extends CInputWidget
14 {
15 /**
16 * @var array the options for the Bootstrap JavaScript plugin.
17 * Available options:
18 * template string
19 * 'dropdown' (default), Show picker in a dropdown
20 * 'modal', Show picker in a modal
21 * false, Don't show a widget
22 * minuteStep integer 15 Specify a step for the minute field.
23 * showSeconds boolean false Show the seconds field.
24 * secondStep integer 15 Specify a step for the second field.
25 * defaultTime string
26 * 'current' (default) Set to the current time.
27 * 'value' Set to inputs current value
28 * false Do not set a default time
29 * showMeridian boolean
30 * true (default) 12hr mode
31 * false24hr mode
32 * showInputs boolean
33 * true (default )Shows the text inputs in the widget.
34 * false Hide the text inputs in the widget
35 * disableFocus boolean false Disables the input from focusing. This is useful for touch screen devices that
36 * display a keyboard on input focus.
37 * modalBackdrop boolean false Show modal backdrop.
38 */
39 public $pluginOptions = array();
40
41 /**
42 * @var string[] the JavaScript event handlers.
43 */
44 public $events = array();
45
46 /**
47 * @var array the HTML attributes for the widget container.
48 */
49 public $htmlOptions = array();
50
51 /**
52 * Initializes the widget.
53 */
54 public function init()
55 {
56 $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
57
58 TbHtml::addCssClass('form-control', $this->htmlOptions);
59 }
60
61 /**
62 * Runs the widget.
63 */
64 public function run()
65 {
66 $this->renderField();
67
68 $this->registerClientScript();
69
70 }
71
72 /**
73 * Renders the field
74 */
75 public function renderField()
76 {
77 list($name, $id) = $this->resolveNameID();
78
79 TbArray::defaultValue('id', $id, $this->htmlOptions);
80 TbArray::defaultValue('name', $name, $this->htmlOptions);
81
82 echo '<div class="input-group">';
83 echo '<div class="input-group-addon"><span class="glyphicon glyphicon-time"></span></div>';
84 if ($this->hasModel()) {
85 echo CHtml::activeTextField($this->model, $this->attribute, $this->htmlOptions);
86 } else {
87 echo CHtml::textField($name, $this->value, $this->htmlOptions, array('style' => 'width:100%'));
88 }
89 echo '</div>';
90 }
91
92 /**
93 * Registers required javascript files
94 * @param $id
95 */
96 public function registerClientScript()
97 {
98 /* publish assets dir */
99 $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
100 $assetsUrl = $this->getAssetsUrl($path);
101
102 /* @var $cs CClientScript */
103 $cs = Yii::app()->getClientScript();
104
105 $cs->registerCssFile($assetsUrl . '/css/bootstrap-timepicker.min.css');
106 $cs->registerScriptFile($assetsUrl . '/js/bootstrap-timepicker.min.js');
107
108 /* initialize plugin */
109 $selector = '#' . TbArray::getValue('id', $this->htmlOptions, $this->getId());
110
111 $this->getApi()->registerPlugin('timepicker', $selector, $this->pluginOptions);
112 $this->getApi()->registerEvents($selector, $this->events);
113 }
114 }