Guest User

Untitled

a guest
Nov 17th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. <?php
  2. namespace common\widgets;
  3. use Yii;
  4. use yii\helpers\Html;
  5. use yii\helpers\FormatConverter;
  6. use yii\base\InvalidParamException;
  7. /**
  8. * Extended DatePicker, allows to set different formats for sending and displaying value
  9. */
  10. class DatePicker extends \kartik\date\DatePicker
  11. {
  12. public $saveDateFormat = 'php:Y-m-d';
  13. private $savedValueInputID = '';
  14. private $attributeValue = null;
  15. public function __construct($config = [])
  16. {
  17. // $defaultOptions = [
  18. // 'type' => static::TYPE_COMPONENT_APPEND,
  19. // 'convertFormat' => true,
  20. // 'pluginOptions' => [
  21. // 'autoclose' => true,
  22. // 'format' => Yii::$app->formatter->dateFormat,
  23. // ],
  24. // ];
  25. $defaultOptions = [
  26. 'type' => static::TYPE_COMPONENT_APPEND,
  27. 'convertFormat' => true,
  28. 'pluginOptions' => [
  29. 'displayFormat' => 'php:d-F-Y',
  30. 'todayHighlight' => true,
  31. 'todayBtn' => true,
  32. 'autoclose' => true,
  33. 'endDate'=> date('d-m-Y', time()),
  34. 'format' => 'php:d F Y',
  35. // 'format' => Yii::$app->formatter->dateFormat,
  36. ],
  37. ];
  38. $config = array_replace_recursive($defaultOptions, $config);
  39. parent::__construct($config);
  40. }
  41. public function init()
  42. {
  43. if ($this->hasModel()) {
  44. $model = $this->model;
  45. $attribute = $this->attribute;
  46. $value = $model->$attribute;
  47. $this->model = null;
  48. $this->attribute = null;
  49. $this->name = Html::getInputName($model, $attribute);
  50. $this->attributeValue = $value;
  51. if ($value) {
  52. try {
  53. $this->value = Yii::$app->formatter->asDateTime($value, $this->pluginOptions['format']);
  54. // $this->value = $value;
  55. } catch (InvalidParamException $e) {
  56. $this->value = null;
  57. }
  58. }
  59. }
  60. return parent::init();
  61. }
  62. protected function parseMarkup($input)
  63. {
  64. $res = parent::parseMarkup($input);
  65. $res .= $this->renderSavedValueInput();
  66. $this->registerScript();
  67. return $res;
  68. }
  69. protected function renderSavedValueInput()
  70. {
  71. $value = $this->attributeValue;
  72. if ($value !== null && $value !== '') {
  73. // format value according to saveDateFormat
  74. try {
  75. $value = Yii::$app->formatter->asDate($value, $this->saveDateFormat);
  76. } catch(InvalidParamException $e) {
  77. // ignore exception and keep original value if it is not a valid date
  78. }
  79. }
  80. $this->savedValueInputID = $this->options['id'].'-saved-value';
  81. $options = $this->options;
  82. $options['id'] = $this->savedValueInputID;
  83. $options['value'] = $value;
  84. // render hidden input
  85. if ($this->hasModel()) {
  86. $contents = Html::activeHiddenInput($this->model, $this->attribute, $options);
  87. } else {
  88. $contents = Html::hiddenInput($this->name, $value, $options);
  89. }
  90. return $contents;
  91. }
  92. protected function registerScript()
  93. {
  94. $language = $this->language ?: Yii::$app->language;
  95. $format = $this->saveDateFormat;
  96. $format = strncmp($format, 'php:', 4) === 0 ? substr($format, 4) :
  97. FormatConverter::convertDateIcuToPhp($format, $this->type);
  98. $saveDateFormatJs = static::convertDateFormat($format);
  99. $containerID = $this->options['data-datepicker-source'];
  100. $hiddenInputID = $this->savedValueInputID;
  101. $script = "
  102. $('#{$containerID}').on('changeDate', function(e) {
  103. var savedValue = e.format(0, '{$saveDateFormatJs}');
  104. $('#{$hiddenInputID}').val(savedValue).trigger('change');
  105. }).on('clearDate', function(e) {
  106. var savedValue = e.format(0, '{$saveDateFormatJs}');
  107. $('#{$hiddenInputID}').val(savedValue).trigger('change');
  108. });
  109. $('#{$containerID}').data('datepicker').update();
  110. ";
  111. $view = $this->getView();
  112. $view->registerJs($script);
  113. }
  114.  
  115. /**
  116. * Renders the source input for the DatePicker plugin.
  117. *
  118. * @return string
  119. */
  120. protected function renderInput()
  121. {
  122. // Html::addCssClass($this->options, 'form-control');
  123. if ($this->type == self::TYPE_INLINE) {
  124. if (empty($this->options['readonly'])) {
  125. $this->options['readonly'] = true;
  126. }
  127. $this->options['class'] .= ' input-sm text-center';
  128. }
  129. if (isset($this->form) && ($this->type !== self::TYPE_RANGE)) {
  130. $vars = call_user_func('get_object_vars', $this);
  131. unset($vars['form']);
  132. return $this->form->field($this->model, $this->attribute)->widget(self::classname(), $vars);
  133. }
  134. $input = $this->type == self::TYPE_BUTTON ? 'hiddenInput' : 'textInput';
  135. return $this->parseMarkup($this->getInput($input));
  136. }
  137. }
Add Comment
Please, Sign In to add comment