EJuiDateTimePicker.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * EJuiDateTimePicker displays a DateTimePicker or TimePicker.
  4. *
  5. * EJuiDateTimePicker encapsulates the {@link http://trentrichardson.com/examples/timepicker/} addon.
  6. *
  7. * To use this widget, you may insert the following code in a view:
  8. * <pre>
  9. * $this->widget('ext.jui.EJuiDateTimePicker', array(
  10. * 'model' => $model,
  11. * 'attribute' => 'publish_time',
  12. * // additional javascript options for the datetime picker plugin
  13. * 'options' => array(
  14. * 'dateFormat' => 'yy-mm-dd',
  15. * ),
  16. * 'htmlOptions' => array(
  17. * 'style' => 'height:20px;'
  18. * ),
  19. * ));
  20. * </pre>
  21. *
  22. * @author Fadeev Ruslan <fadeevr@gmail.com>
  23. */
  24. Yii::import('zii.widgets.jui.CJuiDatePicker');
  25. class EJuiDateTimePicker extends CJuiDatePicker
  26. {
  27. public $mode = 'datetime';
  28. public function init()
  29. {
  30. if (!in_array($this->mode, array('date', 'time', 'datetime'))) {
  31. throw new CException('CJuiDatePicker unknown mode "' . $this->mode . '". Use time, datetime or date!');
  32. }
  33. if (empty($this->language)) {
  34. $this->language = Yii::app()->language;
  35. }
  36. parent::init();
  37. }
  38. public function run()
  39. {
  40. if ($this->mode == 'date') {
  41. parent::run();
  42. }
  43. else {
  44. list($name, $id) = $this->resolveNameID();
  45. if (isset($this->htmlOptions['id'])) {
  46. $id = $this->htmlOptions['id'];
  47. } else {
  48. $this->htmlOptions['id'] = $id;
  49. }
  50. if (isset($this->htmlOptions['name'])) {
  51. $name = $this->htmlOptions['name'];
  52. }
  53. if ($this->flat === false) {
  54. if ($this->hasModel()) {
  55. echo CHtml::activeTextField($this->model, $this->attribute, $this->htmlOptions);
  56. } else {
  57. echo CHtml::textField($name, $this->value, $this->htmlOptions);
  58. }
  59. } else {
  60. if ($this->hasModel()) {
  61. echo CHtml::activeHiddenField($this->model, $this->attribute, $this->htmlOptions);
  62. $attribute = $this->attribute;
  63. $this->options['defaultDate'] = $this->model->$attribute;
  64. } else {
  65. echo CHtml::hiddenField($name, $this->value, $this->htmlOptions);
  66. $this->options['defaultDate'] = $this->value;
  67. }
  68. if (!isset($this->options['onSelect'])) {
  69. $this->options['onSelect'] = new CJavaScriptExpression("function( selectedDate ) { jQuery('#{$id}').val(selectedDate);}");
  70. }
  71. $id = $this->htmlOptions['id'] = $id . '_container';
  72. $this->htmlOptions['name'] = $name . '_container';
  73. echo CHtml::tag('div', $this->htmlOptions, '');
  74. }
  75. //set now time..
  76. $this->options['hour'] = date('H');
  77. $this->options['minute'] = date('i');
  78. $this->options['second'] = date('s');
  79. $options = CJavaScript::encode($this->options);
  80. $js = "jQuery('#{$id}').{$this->mode}picker($options);";
  81. $assetsDir = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
  82. $assets = Yii::app()->assetManager->publish($assetsDir);
  83. $cs = Yii::app()->clientScript;
  84. $min = YII_DEBUG ? '' : '.min';
  85. $cs->registerCssFile($assets . '/jquery-ui-timepicker-addon.css');
  86. $cs->registerScriptFile($assets . '/jquery-ui-timepicker-addon' . $min . '.js', CClientScript::POS_END);
  87. if ($this->language != 'en') {
  88. $this->registerScriptFile($this->i18nScriptFile);
  89. //TimePicker localization load..
  90. $i18nScriptFile = 'jquery-ui-timepicker-' . $this->language . '.js';
  91. $i18nScriptPath = $assetsDir . DIRECTORY_SEPARATOR . 'localization' . DIRECTORY_SEPARATOR . $i18nScriptFile;
  92. if (file_exists($i18nScriptPath)) {
  93. $cs->registerScriptFile($assets . '/localization/' . $i18nScriptFile, CClientScript::POS_END);
  94. }
  95. $js = "jQuery('#{$id}').{$this->mode}picker(jQuery.extend(jQuery.datepicker.regional['{$this->language}'], {$options}));";
  96. }
  97. if (isset($this->defaultOptions)) {
  98. $this->registerScriptFile($this->i18nScriptFile);
  99. $cs->registerScript(__CLASS__, $this->defaultOptions !== null ? "jQuery.{$this->mode}picker.setDefaults(" . CJavaScript::encode($this->defaultOptions) . ');' : '');
  100. }
  101. $cs->registerScript(__CLASS__ . '#' . $id, $js);
  102. }
  103. }
  104. }