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
59 /**
60 * Runs the widget.
61 */
62 public function run()
63 {
64 $this->renderField();
65
66 $this->registerClientScript();
67
68 }
69
70 /**
71 * Renders the field
72 */
73 public function renderField()
74 {
75 list($name, $id) = $this->resolveNameID();
76
77 TbArray::defaultValue('id', $id, $this->htmlOptions);
78 TbArray::defaultValue('name', $name, $this->htmlOptions);
79
80 echo '<span class="bootstrap-timepicker">';
81 if ($this->hasModel()) {
82 echo CHtml::activeTextField($this->model, $this->attribute, $this->htmlOptions);
83 } else {
84 echo CHtml::textField($name, $this->value, $this->htmlOptions, array('style' => 'width:100%'));
85 }
86 echo '</span>';
87 }
88
89 /**
90 * Registers required javascript files
91 * @param $id
92 */
93 public function registerClientScript()
94 {
95 /* publish assets dir */
96 $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
97 $assetsUrl = $this->getAssetsUrl($path);
98
99 /* @var $cs CClientScript */
100 $cs = Yii::app()->getClientScript();
101
102 $cs->registerCssFile($assetsUrl . '/css/bootstrap-timepicker.min.css');
103 $cs->registerScriptFile($assetsUrl . '/js/bootstrap-timepicker.min.js');
104
105 /* initialize plugin */
106 $selector = '#' . TbArray::getValue('id', $this->htmlOptions, $this->getId());
107
108 $this->getApi()->registerPlugin('timepicker', $selector, $this->pluginOptions);
109 $this->getApi()->registerEvents($selector, $this->events);
110 }
111 }