Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
CJuiDatePicker.php
1 <?php
2 /**
3  * CJuiDatePicker class file.
4  *
5  * @author Sebastian Thierer <sebathi@gmail.com>
6  * @link http://www.yiiframework.com/
7  * @copyright Copyright &copy; 2008-2011 Yii Software LLC
8  * @license http://www.yiiframework.com/license/
9  */
10 
11 Yii::import('zii.widgets.jui.CJuiInputWidget');
12 
13 /**
14  * CJuiDatePicker displays a datepicker.
15  *
16  * CJuiDatePicker encapsulates the {@link http://jqueryui.com/demos/datepicker/ JUI
17  * datepicker} plugin.
18  *
19  * To use this widget, you may insert the following code in a view:
20  * <pre>
21  * $this->widget('zii.widgets.jui.CJuiDatePicker',array(
22  * 'name'=>'publishDate',
23  * // additional javascript options for the date picker plugin
24  * 'options'=>array(
25  * 'showAnim'=>'fold',
26  * ),
27  * 'htmlOptions'=>array(
28  * 'style'=>'height:20px;'
29  * ),
30  * ));
31  * </pre>
32  *
33  * By configuring the {@link options} property, you may specify the options
34  * that need to be passed to the JUI datepicker plugin. Please refer to
35  * the {@link http://jqueryui.com/demos/datepicker/ JUI datepicker} documentation
36  * for possible options (name-value pairs).
37  *
38  * @author Sebastian Thierer <sebathi@gmail.com>
39  * @package zii.widgets.jui
40  * @since 1.1
41  */
42 class CJuiDatePicker extends CJuiInputWidget
43 {
44  /**
45  * @var string the locale ID (eg 'fr', 'de') for the language to be used by the date picker.
46  * If this property is not set, I18N will not be involved. That is, the date picker will show in English.
47  * You can force English language by setting the language attribute as '' (empty string)
48  */
49  public $language;
50  /**
51  * @var string The i18n Jquery UI script file. It uses scriptUrl property as base url.
52  */
53  public $i18nScriptFile='jquery-ui-i18n.min.js';
54  /**
55  * @var array The default options called just one time per request. This options will alter every other CJuiDatePicker instance in the page.
56  * It has to be set at the first call of CJuiDatePicker widget in the request.
57  */
58  public $defaultOptions;
59  /**
60  * @var boolean If true, shows the widget as an inline calendar and the input as a hidden field.
61  */
62  public $flat=false;
63 
64  /**
65  * Run this widget.
66  * This method registers necessary javascript and renders the needed HTML code.
67  */
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 
79  if($this->flat===false)
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  else
87  {
88  if($this->hasModel())
89  {
90  echo CHtml::activeHiddenField($this->model,$this->attribute,$this->htmlOptions);
91  $attribute=$this->attribute;
92  $this->options['defaultDate']=$this->model->$attribute;
93  }
94  else
95  {
96  echo CHtml::hiddenField($name,$this->value,$this->htmlOptions);
97  $this->options['defaultDate']=$this->value;
98  }
99 
100  $this->options['altField']='#'.$id;
101 
102  $id=$this->htmlOptions['id']=$id.'_container';
103  $this->htmlOptions['name']=$name.'_container';
104 
105  echo CHtml::tag('div',$this->htmlOptions,'');
106  }
107 
108  $options=CJavaScript::encode($this->options);
109  $js = "jQuery('#{$id}').datepicker($options);";
110 
111  if($this->language!='' && $this->language!='en')
112  {
113  $this->registerScriptFile($this->i18nScriptFile);
114  $js = "jQuery('#{$id}').datepicker(jQuery.extend({showMonthAfterYear:false},jQuery.datepicker.regional['{$this->language}'],{$options}));";
115  }
116 
117  $cs = Yii::app()->getClientScript();
118 
119  if(isset($this->defaultOptions))
120  {
121  $this->registerScriptFile($this->i18nScriptFile);
122  $cs->registerScript(__CLASS__,$this->defaultOptions!==null?'jQuery.datepicker.setDefaults('.CJavaScript::encode($this->defaultOptions).');':'');
123  }
124  $cs->registerScript(__CLASS__.'#'.$id,$js);
125  }
126 }