-
Notifications
You must be signed in to change notification settings - Fork 2
/
EJuiTimePicker.php
114 lines (96 loc) · 3.4 KB
/
EJuiTimePicker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/**
* EJuiTimePicker class file.
*
* @author Andrew M. <[email protected]>
* @copyright Copyright © 2011 Andrew M.
* @license Licensed under MIT license. http://ifdattic.com/MIT-license.txt
* @version 1.0
*/
/**
* EJuiTimePicker displays a datepicker, timepicker or both.
*
* @author Andrew M. <[email protected]>
*/
Yii::import( 'zii.widgets.jui.CJuiDatePicker' );
class EJuiTimePicker extends CJuiDatePicker
{
/**
* Filename of extensions assets.
*/
const ASSETS_NAME = '/jquery-ui-timepicker-addon';
/**
* Filename of localization assets.
*/
const LOCALIZATION_NAME = '/localization/jquery-ui-timepicker-';
/**
* @var string default mode of timepicker addon.
*/
public $mode = 'datetime';
/**
* @var array options to overwrite when using time picker.
*/
public $timeOptions = array();
/**
* @var array html options to overwrite when using time picker.
*/
public $timeHtmlOptions = array();
/**
* Initialize widget.
*/
public function init()
{
if( !in_array( $this->mode, array( 'date', 'time', 'datetime' ) ) )
throw new CException( 'unknown mode "' . $this->mode . '"' );
if( !isset( $this->language ) )
$this->language = Yii::app()->getLanguage();
// Overwrite options for time picker
if( $this->mode === 'time' )
{
$this->options = array_merge( $this->options, $this->timeOptions );
$this->htmlOptions = array_merge( $this->htmlOptions, $this->timeHtmlOptions );
}
return parent::init();
}
/**
* Generate timepicker input.
*/
public function run()
{
list( $name, $id ) = $this->resolveNameID();
if( isset( $this->htmlOptions['id'] ) )
$id = $this->htmlOptions['id'];
else
$this->htmlOptions['id'] = $id;
if( isset( $this->htmlOptions['name'] ) )
$name = $this->htmlOptions['name'];
else
$this->htmlOptions['name'] = $name;
if( $this->hasModel() )
echo CHtml::activeTextField( $this->model, $this->attribute, $this->htmlOptions );
else
echo CHtml::textField( $name, $this->value, $this->htmlOptions );
$options = CJavaScript::encode( $this->options );
$js = "jQuery('#{$id}').{$this->mode}picker({$options});";
if( isset( $this->language ) && $this->language != 'en_us' )
{
$this->registerScriptFile( $this->i18nScriptFile );
$js = "jQuery('#{$id}').{$this->mode}picker(jQuery.extend({showMonthAfterYear:false}, jQuery.datepicker.regional['{$this->language}'], {$options}));";
}
// Publish extension assets
$assets = Yii::app()->getAssetManager()->publish( Yii::getPathOfAlias(
'ext.EJuiTimePicker' ) . '/assets' );
$cs = Yii::app()->getClientScript();
$cs->registerCssFile( $assets . self::ASSETS_NAME . '.css' );
$cs->registerScriptFile( $assets . self::ASSETS_NAME . '.js',
CClientScript::POS_END );
// Run extension
$cs->registerScript( __CLASS__, $this->defaultOptions ? 'jQuery.{$this->mode}picker.setDefaults(' . CJavaScript::encode( $this->defaultOptions ) . ');' : '' );
$cs->registerScript( __CLASS__ . '#' . $id, $js );
// // Add localization file if it exists
$localization = $assets . self::LOCALIZATION_NAME . $this->language . '.js';
if( file_exists( Yii::getPathOfAlias( 'webroot' ) . $localization ) )
$cs->registerScriptFile( $localization, CClientScript::POS_END );
}
}
?>