You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
4.7 KiB

  1. <?php
  2. /*
  3. * The MIT License
  4. *
  5. * Copyright 2019 Blobt.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. namespace blobt\widgets;
  26. use yii\widgets\InputWidget;
  27. use Yii;
  28. use yii\base\InvalidConfigException;
  29. use yii\helpers\ArrayHelper;
  30. use yii\helpers\Html;
  31. use yii\web\JsExpression;
  32. use yii\helpers\Json;
  33. use blobt\web\DatepickBootstrapAsset;
  34. use blobt\helpers\DatetimeHelper;
  35. /**
  36. * 此DatePicker只适用于使用Unix时间戳保存时间的model
  37. *
  38. * @author Blobt
  39. * @email 380255922@qq.com
  40. * @created Aug 19, 2019
  41. */
  42. class DatePicker extends InputWidget {
  43. /**
  44. * @var string 日期显示格式
  45. */
  46. public $format = "Y-m-d";
  47. /**
  48. * @var string datepicker的日期显示input框的id,只用做显示
  49. */
  50. public $pickerInputId;
  51. /**
  52. * @var boll 是否自动填充当天日期
  53. */
  54. public $autoFill = false;
  55. /**
  56. * @inheritdoc
  57. * @throws InvalidConfigException
  58. * @throws \ReflectionException
  59. */
  60. public function run() {
  61. $this->initSetting();
  62. $this->registerAsset();
  63. echo $this->renderHiddenInput();
  64. echo $this->renderDatapickerInput();
  65. }
  66. /**
  67. * 初始化小物件的配置
  68. */
  69. protected function initSetting() {
  70. //初始化日期格式
  71. if (empty($this->format)) {
  72. $this->format = Yii::$app->formatter->dateFormat;
  73. }
  74. //关闭自动完成
  75. $this->options["autocomplete"] = "off";
  76. //生成一个picker input的 id
  77. $this->pickerInputId = $this->options['id'] . "_" . Yii::$app->security->generateRandomString(32);
  78. }
  79. /**
  80. * 注册js和css
  81. */
  82. protected function registerAsset() {
  83. $view = $this->getView();
  84. DatepickBootstrapAsset::register($view);
  85. $options = [
  86. "format" => $this->convertDateFormat($this->format),
  87. "language" => "zh-CN"
  88. ];
  89. $jsOptions = Json::encode($options);
  90. $js = <<< SCRIPT
  91. $('#{$this->pickerInputId}').datepicker({$jsOptions}).on('changeDate', function(e) {
  92. $('#{$this->options['id']}').val(String(e.date.getTime()).substring(0,10));
  93. });;
  94. SCRIPT;
  95. $view->registerJs($js);
  96. }
  97. /**
  98. * 渲染一个用作保存Datepicker Unix时间错的hidden input
  99. * @return string
  100. */
  101. protected function renderHiddenInput() {
  102. if ($this->hasModel()) {
  103. return Html::activeInput('hidden', $this->model, $this->attribute, $this->options);
  104. }
  105. return Html::input('hidden', $this->name, $this->value, $this->options);
  106. }
  107. /**
  108. * TODO:自动填充了今天为默认日期
  109. * 渲染一个用作显示日期的字符格式 text input
  110. * @return string
  111. */
  112. protected function renderDatapickerInput() {
  113. $value = '';
  114. $options = $this->options;
  115. $options['id'] = $this->pickerInputId;
  116. if ($this->hasModel()) {
  117. $timestamp = ArrayHelper::getValue($this->model, $this->attribute);
  118. if (!empty($timestamp)) {
  119. $value = date($this->format, $timestamp);
  120. }
  121. }
  122. if (empty($timestamp) && $this->autoFill) {
  123. $timestamp = time();
  124. $value = date($this->format, $timestamp);
  125. }
  126. return Html::input('text', $this->name, $value, $options);
  127. }
  128. /**
  129. * 转换php日期格式为bootstrap日期格式
  130. *
  131. * TODO:没有弄清所有格式的转换,后续添加
  132. *
  133. * @param string $format PHP日期格式字符串
  134. * @return string
  135. */
  136. protected static function convertDateFormat($format) {
  137. $conversions = [
  138. 'd' => 'dd',
  139. 'm' => 'mm',
  140. 'Y' => 'yyyy',
  141. ];
  142. return strtr($format, $conversions);
  143. }
  144. }