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.

164 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\DatetimepickBootstrapAsset;
  34. use blobt\helpers\DatetimeHelper;
  35. /**
  36. * 这是一个Bootstrap的DatetimePicker 参考https://www.malot.fr/bootstrap-datetimepicker/
  37. *
  38. * @author Blobt
  39. * @email 380255922@qq.com
  40. * @created Aug 21, 2019
  41. */
  42. class DatetimePicker extends InputWidget {
  43. /**
  44. * @var string 时间日期显示格式
  45. */
  46. public $format = "Y-m-d h:i";
  47. /**
  48. * @var string datetimepicker的日期显示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. echo $this->renderDatetimePickerInput();
  63. $this->registerAsset();
  64. }
  65. /**
  66. * 初始化小物件的配置
  67. */
  68. protected function initSetting() {
  69. //初始化日期格式
  70. if (empty($this->format)) {
  71. $this->format = Yii::$app->formatter->dateFormat;
  72. }
  73. //关闭自动完成
  74. $this->options["autocomplete"] = "off";
  75. //生成一个picker input的 id
  76. $this->pickerInputId = $this->options['id'] . "_" . Yii::$app->security->generateRandomString(4);
  77. }
  78. /**
  79. * 注册js和css
  80. */
  81. protected function registerAsset() {
  82. $view = $this->getView();
  83. DatetimepickBootstrapAsset::register($view);
  84. $options = [
  85. "format" => $this->convertDateFormat($this->format),
  86. "language" => "zh-CN"
  87. ];
  88. $jsOptions = Json::encode($options);
  89. $js = <<< SCRIPT
  90. $('#{$this->pickerInputId}').datetimepicker({$jsOptions}).on('changeDate', function(e) {
  91. $('#{$this->options['id']}').val(String(e.date.getTime()).substring(0,10));
  92. });;
  93. SCRIPT;
  94. $view->registerJs($js);
  95. }
  96. /**
  97. * 渲染一个用作保存Datepicker Unix时间错的hidden input
  98. * @return string
  99. */
  100. protected function renderHiddenInput() {
  101. if ($this->hasModel()) {
  102. return Html::activeInput('hidden', $this->model, $this->attribute, $this->options);
  103. }
  104. return Html::input('hidden', $this->name, $this->value, $this->options);
  105. }
  106. /**
  107. * TODO:自动填充了今天为默认日期
  108. * 渲染一个用作显示日期的字符格式 text input
  109. * @return string
  110. */
  111. protected function renderDatetimepickerInput() {
  112. $value = '';
  113. $options = $this->options;
  114. $options['id'] = $this->pickerInputId;
  115. if ($this->hasModel()) {
  116. $timestamp = ArrayHelper::getValue($this->model, $this->attribute);
  117. }
  118. if (empty($timestamp) && $this->autoFill) {
  119. $timestamp = time();
  120. $value = date($this->format, $timestamp);
  121. }
  122. return Html::input('text', $this->name, $value, $options);
  123. }
  124. /**
  125. * 转换php日期格式为bootstrap日期格式
  126. *
  127. * TODO:没有弄清所有格式的转换,后续添加
  128. *
  129. * @param string $format PHP日期格式字符串
  130. * @return string
  131. */
  132. protected static function convertDateFormat($format) {
  133. $conversions = [
  134. 'd' => 'dd',
  135. 'm' => 'mm',
  136. 'Y' => 'yyyy',
  137. 'h' => 'hh',
  138. 'i' => 'ii'
  139. ];
  140. return strtr($format, $conversions);
  141. }
  142. }