EliteConf
 All Data Structures Namespaces Files Functions Variables Pages
EJuiTimePicker.php
Go to the documentation of this file.
1 <?php
16 Yii::import( 'zii.widgets.jui.CJuiDatePicker' );
17 class EJuiTimePicker extends CJuiDatePicker
18 {
22  const ASSETS_NAME = '/jquery-ui-timepicker-addon';
23 
27  const LOCALIZATION_NAME = '/localization/jquery-ui-timepicker-';
28 
32  public $mode = 'datetime';
33 
37  public $timeOptions = array();
38 
42  public $timeHtmlOptions = array();
43 
47  public function init()
48  {
49  if( !in_array( $this->mode, array( 'date', 'time', 'datetime' ) ) )
50  throw new CException( 'unknown mode "' . $this->mode . '"' );
51 
52  if( !isset( $this->language ) )
53  $this->language = Yii::app()->getLanguage();
54 
55  // Overwrite options for time picker
56  if( $this->mode === 'time' )
57  {
58  $this->options = array_merge( $this->options, $this->timeOptions );
59  $this->htmlOptions = array_merge( $this->htmlOptions, $this->timeHtmlOptions );
60  }
61 
62  return parent::init();
63  }
64 
68  public function run()
69  {
70  list( $name, $id ) = $this->resolveNameID();
71 
72  if( isset( $this->htmlOptions['id'] ) )
73  $id = $this->htmlOptions['id'];
74  else
75  $this->htmlOptions['id'] = $id;
76  if( isset( $this->htmlOptions['name'] ) )
77  $name = $this->htmlOptions['name'];
78  else
79  $this->htmlOptions['name'] = $name;
80 
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 );
85 
86  $options = CJavaScript::encode( $this->options );
87 
88  $js = "jQuery('#{$id}').{$this->mode}picker({$options});";
89 
90  if( isset( $this->language ) && $this->language != 'en_us' )
91  {
92  $this->registerScriptFile( $this->i18nScriptFile );
93  $js = "jQuery('#{$id}').{$this->mode}picker(jQuery.extend({showMonthAfterYear:false}, jQuery.datepicker.regional['{$this->language}'], {$options}));";
94  }
95 
96  // Publish extension assets
97  $assets = Yii::app()->getAssetManager()->publish( Yii::getPathOfAlias(
98  'ext.EJuiTimePicker' ) . '/assets' );
99  $cs = Yii::app()->getClientScript();
100  $cs->registerCssFile( $assets . self::ASSETS_NAME . '.css' );
101  $cs->registerScriptFile( $assets . self::ASSETS_NAME . '.js',
102  CClientScript::POS_END );
103 
104  // Run extension
105  $cs->registerScript( __CLASS__, $this->defaultOptions ? 'jQuery.{$this->mode}picker.setDefaults(' . CJavaScript::encode( $this->defaultOptions ) . ');' : '' );
106  $cs->registerScript( __CLASS__ . '#' . $id, $js );
107 
108  // // Add localization file if it exists
109  $localization = $assets . self::LOCALIZATION_NAME . $this->language . '.js';
110  if( file_exists( Yii::getPathOfAlias( 'webroot' ) . $localization ) )
111  $cs->registerScriptFile( $localization, CClientScript::POS_END );
112  }
113 }
114 ?>