From 97659bc1901e4c245bdcf6a8d2678b304c328b91 Mon Sep 17 00:00:00 2001 From: scriptua Date: Fri, 3 Jun 2016 19:35:02 +0300 Subject: [PATCH] init --- README.md | 40 +++++++++ composer.json | 22 +++++ src/DateTime.php | 185 ++++++++++++++++++++++++++++++++++++++++++ src/DateTimeAsset.php | 40 +++++++++ src/MomentAsset.php | 32 ++++++++ 5 files changed, 319 insertions(+) create mode 100644 README.md create mode 100644 composer.json create mode 100644 src/DateTime.php create mode 100644 src/DateTimeAsset.php create mode 100644 src/MomentAsset.php diff --git a/README.md b/README.md new file mode 100644 index 0000000..bc1db41 --- /dev/null +++ b/README.md @@ -0,0 +1,40 @@ +Yii2 DateTime Widget +==================== +Yii2 DateTime Widget + +Installation +------------ + +The preferred way to install this extension is through [composer](http://getcomposer.org/download/). + +Either run + +``` +php composer.phar require --prefer-dist oakcms/yii2-datetimepicker "*" +``` + +or add + +``` +"oakcms/yii2-datetimepicker": "*" +``` + +to the require section of your `composer.json` file. + + +Usage +----- + +Once the extension is installed, simply use it in your code by : + +```php +use oakcms\yii2-datetimepicker\DateTimeWidget; + +field($model, 'field')->widget( + DateTimeWidget::className(), + [ + 'phpDatetimeFormat' => 'dd.MM.yyyy, HH:mm' + ] + ); +?> +``` diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..c419a96 --- /dev/null +++ b/composer.json @@ -0,0 +1,22 @@ +{ + "name": "oakcms/yii2-datetimepicker", + "description": "Yii2 DateTime Widget", + "type": "yii2-extension", + "keywords": ["yii2","extension"," datetimepicker"," oakcms"], + "license": "GPL-3.0+", + "authors": [ + { + "name": "scriptua", + "email": "script@email.ua" + } + ], + "require": { + "yiisoft/yii2": "*", + "bower-asset/eonasdan-bootstrap-datetimepicker": "*" + }, + "autoload": { + "psr-4": { + "oakcms\\datetimepicker\\": "src" + } + } +} diff --git a/src/DateTime.php b/src/DateTime.php new file mode 100644 index 0000000..4b8c4af --- /dev/null +++ b/src/DateTime.php @@ -0,0 +1,185 @@ + 'YYYY-MM-DDTHH:mm:ssZZ', // 2014-05-14T13:55:01+02:00 + "dd-MM-yyyy'T'HH:mm:ssZZZZZ" => 'DD-MM-YYYYTHH:mm:ssZZ', // 14-05-2014T13:55:01+02:00 + "yyyy-MM-dd" => 'YYYY-MM-DD', // 2014-05-14 + "dd.MM.yyyy, HH:mm" => 'DD.MM.YYYY, HH:mm', // 14.05.2014, 13:55, German format without seconds + "dd.MM.yyyy, HH:mm:ss" => 'DD.MM.YYYY, HH:mm:ss', // 14.05.2014, 13:55:01, German format with seconds + "dd/MM/yyyy" => 'DD/MM/YYYY', // 14/05/2014, British ascending format + "dd/MM/yyyy HH:mm" => 'DD/MM/YYYY HH:mm', // 14/05/2014 13:55, British ascending format with time + "EE, dd/MM/yyyy HH:mm" => 'ddd, DD/MM/YYYY HH:mm', // Wed, 14/05/2014 13:55, includes day of week in British format + ]; + /** + * @throws \yii\base\InvalidConfigException + */ + public function init() + { + parent::init(); + $value = $this->hasModel() ? Html::getAttributeValue($this->model, $this->attribute) : $this->value; + $this->momentDatetimeFormat = $this->momentDatetimeFormat ?: ArrayHelper::getValue( + $this->getPhpMomentMappings(), + $this->phpDatetimeFormat + ); + if (!$this->momentDatetimeFormat) { + throw new InvalidConfigException('Please set momentjs datetime format'); + } + // Init default clientOptions + $this->clientOptions = ArrayHelper::merge([ + 'useCurrent' => true, + 'locale' => $this->locale ?: substr(Yii::$app->language, 0, 2), + 'format' => $this->momentDatetimeFormat, + ], $this->clientOptions); + // Init default options + $this->options = ArrayHelper::merge([ + 'class' => 'form-control', + ], $this->options); + if ($value !== null) { + $this->options['value'] = array_key_exists('value', $this->options) + ? $this->options['value'] + : Yii::$app->formatter->asDatetime($value, $this->phpDatetimeFormat); + } + if (!isset($this->containerOptions['id'])) { + $this->containerOptions['id'] = $this->getId(); + } + $this->registerJs(); + } + protected function registerJs() + { + DateTimeAsset::register($this->getView()); + $clientOptions = Json::encode($this->clientOptions); + $this->getView()->registerJs("$('#{$this->containerOptions['id']}').datetimepicker({$clientOptions})"); + if (!empty($this->clientEvents)) { + $js = []; + foreach ($this->clientEvents as $event => $handler) { + $js[] = "jQuery('#{$this->containerOptions['id']}').on('$event', $handler);"; + } + $this->getView()->registerJs(implode("\n", $js)); + } + } + /** + * @return string + */ + public function run() + { + $content = []; + if ($this->showInputAddon) { + Html::addCssClass($this->containerOptions, 'input-group'); + } + Html::addCssStyle($this->containerOptions, 'position: relative'); + $content[] = Html::beginTag('div', $this->containerOptions); + $content[] = $this->renderInput(); + if ($this->showInputAddon) { + $content[] = $this->renderInputAddon(); + } + $content[] = Html::endTag('div'); + return implode("\n", $content); + } + /** + * @return string + */ + protected function renderInput() + { + if ($this->hasModel()) { + $content = Html::activeTextInput($this->model, $this->attribute, $this->options); + } else { + $content = Html::textInput($this->name, $this->value, $this->options); + } + return $content; + } + /** + * @return string + */ + protected function renderInputAddon() + { + $content = []; + if (!array_key_exists('class', $this->inputAddonOptions)) { + Html::addCssClass($this->inputAddonOptions, 'input-group-addon'); + } + if (!array_key_exists('style', $this->inputAddonOptions)) { + Html::addCssStyle($this->inputAddonOptions, ['cursor' => 'pointer']); + } + $content[] = Html::beginTag('span', $this->inputAddonOptions); + if ($this->inputAddonContent) { + $content[] = $this->inputAddonContent; + } else { + $content[] = Html::tag('span', '', ['class' => 'glyphicon glyphicon-calendar']); + } + $content[] = Html::endTag('span'); + return implode("\n", $content); + } + /** + * @return array + */ + protected function getPhpMomentMappings() + { + return array_merge($this->defaultPhpMomentMapping, $this->phpMomentMapping); + } +} diff --git a/src/DateTimeAsset.php b/src/DateTimeAsset.php new file mode 100644 index 0000000..06c2d30 --- /dev/null +++ b/src/DateTimeAsset.php @@ -0,0 +1,40 @@ +