From 165a25921b7cf0abecfde2455f41fc4e630d8539 Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Sat, 10 Aug 2013 21:47:24 +0300 Subject: [PATCH] rework date widget. use custom formats --- composer.json | 1 + composer.lock | 67 +++++++++++++++++++++++------- ipf/admin/static/admin/js/admin.js | 8 +++- ipf/form/model.php | 3 ++ ipf/form/widget/dateinput.php | 44 +++++++++++++++++--- 5 files changed, 101 insertions(+), 22 deletions(-) diff --git a/composer.json b/composer.json index 8054e65..2cbf46a 100644 --- a/composer.json +++ b/composer.json @@ -18,6 +18,7 @@ "require": { "pear/archive_tar": "1.3.*", "mustangostang/spyc": "0.5.*", + "andy128k/missing-tools": "dev-master", "andy128k/routeexpression": "dev-master", "andy128k/ipf-template": "dev-master" }, diff --git a/composer.lock b/composer.lock index 145d463..274009c 100644 --- a/composer.lock +++ b/composer.lock @@ -3,7 +3,7 @@ "This file locks the dependencies of your project to a known state", "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file" ], - "hash": "e5ea80198a9aa0bc5288cbdbe34949e7", + "hash": "57c4d731e20fcf4857e1a79b7884e303", "packages": [ { "name": "andy128k/ipf-template", @@ -48,6 +48,35 @@ }, "time": "2013-07-30 22:57:26" }, + { + "name": "andy128k/missing-tools", + "version": "dev-master", + "source": { + "type": "git", + "url": "git://git.andy128k.net/missing-tools.git", + "reference": "2eadebd11488194e45d75b722bc0ebbabf15dcc3" + }, + "require-dev": { + "phpunit/phpunit": "3.7.*" + }, + "type": "library", + "autoload": { + "classmap": [ + "src" + ] + }, + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Andrey Kutejko", + "email": "andy128k@gmail.com" + } + ], + "description": "Miscellaneous utilities", + "time": "2013-08-10 18:31:29" + }, { "name": "andy128k/routeexpression", "version": "dev-master", @@ -381,16 +410,16 @@ }, { "name": "phpunit/php-token-stream", - "version": "1.1.8", + "version": "1.2.0", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-token-stream.git", - "reference": "1.1.8" + "reference": "1.2.0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/1.1.8", - "reference": "1.1.8", + "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/1.2.0", + "reference": "1.2.0", "shasum": "" }, "require": { @@ -398,6 +427,11 @@ "php": ">=5.3.3" }, "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.2-dev" + } + }, "autoload": { "classmap": [ "PHP/" @@ -422,20 +456,20 @@ "keywords": [ "tokenizer" ], - "time": "2013-08-02 19:10:55" + "time": "2013-08-04 05:57:48" }, { "name": "phpunit/phpunit", - "version": "3.7.23", + "version": "3.7.24", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "3.7.23" + "reference": "3.7.24" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/3.7.23", - "reference": "3.7.23", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/3.7.24", + "reference": "3.7.24", "shasum": "" }, "require": { @@ -496,7 +530,7 @@ "testing", "xunit" ], - "time": "2013-08-02 19:14:44" + "time": "2013-08-09 06:58:24" }, { "name": "phpunit/phpunit-mock-objects", @@ -549,17 +583,17 @@ }, { "name": "symfony/yaml", - "version": "v2.3.2", + "version": "v2.3.3", "target-dir": "Symfony/Component/Yaml", "source": { "type": "git", "url": "https://github.com/symfony/Yaml.git", - "reference": "v2.3.2" + "reference": "v2.3.3" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/Yaml/zipball/v2.3.2", - "reference": "v2.3.2", + "url": "https://api.github.com/repos/symfony/Yaml/zipball/v2.3.3", + "reference": "v2.3.3", "shasum": "" }, "require": { @@ -592,7 +626,7 @@ ], "description": "Symfony Yaml Component", "homepage": "http://symfony.com", - "time": "2013-07-11 19:36:36" + "time": "2013-07-21 12:12:18" } ], "aliases": [ @@ -600,6 +634,7 @@ ], "minimum-stability": "stable", "stability-flags": { + "andy128k/missing-tools": 20, "andy128k/routeexpression": 20, "andy128k/ipf-template": 20 }, diff --git a/ipf/admin/static/admin/js/admin.js b/ipf/admin/static/admin/js/admin.js index 2ec9d5e..2c070c4 100644 --- a/ipf/admin/static/admin/js/admin.js +++ b/ipf/admin/static/admin/js/admin.js @@ -1,5 +1,11 @@ $(function(){ - $('.dateinput').datepicker({dateFormat: 'yy-mm-dd'}); + $('.dateinput').each(function(){ + var opts = { + dateFormat: $(this).data('format') || 'yy-mm-dd' + }; + $(this).datepicker(opts); + }); + $('.datetimeinput').datetimepicker({dateFormat: 'yy-mm-dd'}); $('.checkgroup').closest('ul').each(function(){ diff --git a/ipf/form/model.php b/ipf/form/model.php index 763c107..5658002 100644 --- a/ipf/form/model.php +++ b/ipf/form/model.php @@ -99,6 +99,9 @@ class IPF_Form_Model extends IPF_Form $form_field = new IPF_Form_Field_Float($params); break; case 'date': + $format = IPF::get('date_format'); + if ($format) + $params['widget_attrs'] = array('format' => $format); $form_field = new IPF_Form_Field_Date($params); break; case 'datetime': diff --git a/ipf/form/widget/dateinput.php b/ipf/form/widget/dateinput.php index 5ca7852..549d1b2 100644 --- a/ipf/form/widget/dateinput.php +++ b/ipf/form/widget/dateinput.php @@ -3,14 +3,48 @@ class IPF_Form_Widget_DateInput extends IPF_Form_Widget_Input { public $input_type = 'text'; - public $format = 'Y-m-d'; + public $format = IPF_Format::DATE_ISO8601; + + public function __construct($attrs=array()) + { + $format = ArrayTools::pop($attrs, 'format'); + if ($format) + $this->format = is_string($format) ? IPF_Format::flagsFromString($format) : $format; + + parent::__construct($attrs); + } public function render($name, $value, $extra_attrs=array()) { - if (strlen($value) > 0) { - $value = date($this->format, strtotime($value)); - } $extra_attrs['class'] = 'dateinput'; + $extra_attrs['data-format'] = IPF_Format::makeFormat($this->format, array('yy', 'mm', 'dd')); return parent::render($name, $value, $extra_attrs); } -} \ No newline at end of file + + public function valueFromFormData($name, &$data) + { + if (!isset($data[$name]) || !$data[$name]) { + return null; + } + + $date = IPF_Format::parseDate($this->format, $data[$name]); + if ($date !== false) { + $day = str_pad($date['tm_mday'], 2, '0', STR_PAD_LEFT); + $month = str_pad($date['tm_mon']+1, 2, '0', STR_PAD_LEFT); + $year = str_pad($date['tm_year']+1900, 4, '0', STR_PAD_LEFT); + return $year.'-'.$month.'-'.$day; + } else { + return null; + } + } + + public function valueToFormData($name, $data) + { + if (isset($data[$name]) && $data[$name]) { + return IPF_Format::formatDate($this->format, strtotime($data[$name])); + } else { + return null; + } + } +} + -- 2.49.0