]> git.andy128k.dev Git - ipf.git/commitdiff
rework date widget. use custom formats
authorAndrey Kutejko <andy128k@gmail.com>
Sat, 10 Aug 2013 18:47:24 +0000 (21:47 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Sat, 10 Aug 2013 18:47:24 +0000 (21:47 +0300)
composer.json
composer.lock
ipf/admin/static/admin/js/admin.js
ipf/form/model.php
ipf/form/widget/dateinput.php

index 8054e6507cfcff8e5d1bdce1f2e3cbd45100ed27..2cbf46ae2322dfdce49795262870b3f6251ec7a0 100644 (file)
@@ -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"
   },
index 145d463b2deb0eb21a28bcc88a4d24191987e78d..274009c85e4cb3312d86056d41c3292f87a83f10 100644 (file)
@@ -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",
             },
             "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",
         },
         {
             "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": {
                 "php": ">=5.3.3"
             },
             "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-master": "1.2-dev"
+                }
+            },
             "autoload": {
                 "classmap": [
                     "PHP/"
             "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": {
                 "testing",
                 "xunit"
             ],
-            "time": "2013-08-02 19:14:44"
+            "time": "2013-08-09 06:58:24"
         },
         {
             "name": "phpunit/phpunit-mock-objects",
         },
         {
             "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": {
             ],
             "description": "Symfony Yaml Component",
             "homepage": "http://symfony.com",
-            "time": "2013-07-11 19:36:36"
+            "time": "2013-07-21 12:12:18"
         }
     ],
     "aliases": [
     ],
     "minimum-stability": "stable",
     "stability-flags": {
+        "andy128k/missing-tools": 20,
         "andy128k/routeexpression": 20,
         "andy128k/ipf-template": 20
     },
index 2ec9d5e7d94b5f8309c9309f04fdef575b5e017d..2c070c4cebc95e7a72ca28a806cef20754a0e2b1 100644 (file)
@@ -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(){
index 763c10715364c554b97ead7af2bf5bf54bce590a..56580027a0cdb735b1206f79d30f62bc1589415e 100644 (file)
@@ -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':
index 5ca7852c075240e03c014b34f278032bce58e7f2..549d1b2a4948f2cc5f2e8015ebbc6fe46e0dccf8 100644 (file)
@@ -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;
+        }
+    }
+}
+