示例#1
0
 /**
  * @inheritdoc
  */
 public function init()
 {
     if ($this->logicalFormat === null) {
         $format = $this->type . 'Format';
         $pattern = Yii::$app->formatter->{$format};
     } else {
         $pattern = $this->logicalFormat;
     }
     if (substr($pattern, 0, 4) === 'php:') {
         $this->_phpLogicalFormat = substr($pattern, 4);
     } else {
         $this->_phpLogicalFormat = FormatConverter::convertDateIcuToPhp($pattern, $this->type);
     }
     if ($this->physicalFormat === null) {
         $driverName = Yii::$app->db->driverName;
         if (isset(static::$dbDatetimeFormat[$driverName])) {
             $pattern = static::$dbDatetimeFormat[$driverName][$this->type];
         } else {
             $pattern = static::$dbDatetimeFormat['default'][$this->type];
         }
     } else {
         $pattern = $this->physicalFormat;
     }
     if (substr($pattern, 0, 4) === 'php:') {
         $this->_phpPhysicalFormat = substr($pattern, 4);
     } else {
         $this->_phpPhysicalFormat = FormatConverter::convertDateIcuToPhp($pattern, $this->type);
     }
     parent::init();
 }
 public function run()
 {
     $inputId = $this->options['id'];
     $altInputId = $inputId . '-alt';
     $hasModel = $this->hasModel();
     if (array_key_exists('value', $this->options)) {
         $value = $this->options['value'];
     } elseif ($hasModel) {
         $value = Html::getAttributeValue($this->model, $this->attribute);
     } else {
         $value = $this->value;
     }
     $altOptions = ['id' => $altInputId, 'name' => $altInputId];
     if (!is_null($value) && $value !== '') {
         $formatter = Yii::$app->getFormatter();
         try {
             $this->options['value'] = $formatter->asDate($value, $this->dateFormat) . self::SEPARATOR . $formatter->asTime($value, $this->timeFormat);
             $altOptions['value'] = $formatter->asDate($value, $this->altDateFormat) . self::SEPARATOR . $formatter->asTime($value, $this->altTimeFormat);
         } catch (InvalidParamException $e) {
             // ignore exception and keep original value if it is not a valid date
         }
     }
     if ($hasModel) {
         $output = Html::activeTextInput($this->model, $this->attribute, $this->options) . (!$this->disableAlt ? Html::activeHiddenInput($this->model, $this->attribute, $altOptions) : null);
     } else {
         $output = Html::textInput($this->name, $this->value, $this->options) . (!$this->disableAlt ? Html::hiddenInput($this->name, $this->value, $altOptions) : null);
     }
     $this->clientOptions = array_merge(['numberOfMonths' => $this->numberOfMonths, 'showButtonPanel' => $this->showButtonPanel], $this->clientOptions, ['dateFormat' => FormatConverter::convertDatePhpOrIcuToJui($this->dateFormat), 'separator' => self::SEPARATOR, 'timeFormat' => FormatConverter::convertTimePhpOrIcuToJui($this->timeFormat), 'altFormat' => FormatConverter::convertDatePhpOrIcuToJui($this->altDateFormat), 'altSeparator' => self::SEPARATOR, 'altTimeFormat' => FormatConverter::convertTimePhpOrIcuToJui($this->altTimeFormat), 'altField' => '#' . $altInputId, 'altFieldTimeOnly' => false]);
     if (!$this->ignoreReadonly && array_key_exists('readonly', $this->options) && $this->options['readonly']) {
         $this->clientOptions['beforeShow'] = new JsExpression('function (input, inst) { return false; }');
     }
     if ($this->disableAlt) {
         foreach ($this->clientOptions as $keyCO => $valueCO) {
             if (strrpos($keyCO, 'alt') !== false) {
                 unset($this->clientOptions[$keyCO]);
             }
         }
     }
     $js = 'jQuery(\'#' . $inputId . '\').datetimepicker(' . Json::htmlEncode($this->clientOptions) . ');';
     if (Yii::$app->getRequest()->getIsAjax()) {
         $output .= Html::script($js);
     } else {
         $view = $this->getView();
         JuiDatetimePickerAsset::register($view);
         JuiDatetimePickerLanguageAsset::register($view);
         $view->registerJs($js);
     }
     return $output;
 }