CJuiDatePicker.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * CJuiDatePicker class file.
  4. *
  5. * @author Sebastian Thierer <sebathi@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. Yii::import('zii.widgets.jui.CJuiInputWidget');
  11. /**
  12. * CJuiDatePicker displays a datepicker.
  13. *
  14. * CJuiDatePicker encapsulates the {@link http://jqueryui.com/datepicker/ JUI
  15. * datepicker} plugin.
  16. *
  17. * To use this widget, you may insert the following code in a view:
  18. * <pre>
  19. * $this->widget('zii.widgets.jui.CJuiDatePicker',array(
  20. * 'name'=>'publishDate',
  21. * // additional javascript options for the date picker plugin
  22. * 'options'=>array(
  23. * 'showAnim'=>'fold',
  24. * ),
  25. * 'htmlOptions'=>array(
  26. * 'style'=>'height:20px;'
  27. * ),
  28. * ));
  29. * </pre>
  30. *
  31. * By configuring the {@link options} property, you may specify the options
  32. * that need to be passed to the JUI datepicker plugin. Please refer to
  33. * the {@link http://api.jqueryui.com/datepicker/ JUI DatePicker API}
  34. * documentation for possible options (name-value pairs) and
  35. * {@link http://jqueryui.com/datepicker/ JUI DatePicker page} for general
  36. * description and demo.
  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. * Run this widget.
  65. * This method registers necessary javascript and renders the needed HTML code.
  66. */
  67. public function run()
  68. {
  69. list($name,$id)=$this->resolveNameID();
  70. if(isset($this->htmlOptions['id']))
  71. $id=$this->htmlOptions['id'];
  72. else
  73. $this->htmlOptions['id']=$id;
  74. if(isset($this->htmlOptions['name']))
  75. $name=$this->htmlOptions['name'];
  76. if($this->flat===false)
  77. {
  78. if($this->hasModel())
  79. echo CHtml::activeTextField($this->model,$this->attribute,$this->htmlOptions);
  80. else
  81. echo CHtml::textField($name,$this->value,$this->htmlOptions);
  82. }
  83. else
  84. {
  85. if($this->hasModel())
  86. {
  87. echo CHtml::activeHiddenField($this->model,$this->attribute,$this->htmlOptions);
  88. $attribute=$this->attribute;
  89. $this->options['defaultDate']=$this->model->$attribute;
  90. }
  91. else
  92. {
  93. echo CHtml::hiddenField($name,$this->value,$this->htmlOptions);
  94. $this->options['defaultDate']=$this->value;
  95. }
  96. $this->options['altField']='#'.$id;
  97. $id=$this->htmlOptions['id']=$id.'_container';
  98. $this->htmlOptions['name']=$name.'_container';
  99. echo CHtml::tag('div',$this->htmlOptions,'');
  100. }
  101. $options=CJavaScript::encode($this->options);
  102. $js = "jQuery('#{$id}').datepicker($options);";
  103. if($this->language!='' && $this->language!='en')
  104. {
  105. $this->registerScriptFile($this->i18nScriptFile);
  106. $js = "jQuery('#{$id}').datepicker(jQuery.extend({showMonthAfterYear:false},jQuery.datepicker.regional['{$this->language}'],{$options}));";
  107. }
  108. $cs = Yii::app()->getClientScript();
  109. if(isset($this->defaultOptions))
  110. {
  111. $this->registerScriptFile($this->i18nScriptFile);
  112. $cs->registerScript(__CLASS__,$this->defaultOptions!==null?'jQuery.datepicker.setDefaults('.CJavaScript::encode($this->defaultOptions).');':'');
  113. }
  114. $cs->registerScript(__CLASS__.'#'.$id,$js);
  115. }
  116. }