]> git.andy128k.dev Git - ipf.git/commitdiff
refactor model form field suggestion
authorAndrey Kutejko <andy128k@gmail.com>
Sat, 10 Aug 2013 13:27:50 +0000 (16:27 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Sat, 10 Aug 2013 13:27:50 +0000 (16:27 +0300)
12 files changed:
ipf/form/db/date.php [deleted file]
ipf/form/db/datetime.php [deleted file]
ipf/form/db/decimal.php [deleted file]
ipf/form/db/double.php [deleted file]
ipf/form/db/email.php [deleted file]
ipf/form/db/file.php [deleted file]
ipf/form/db/html.php [deleted file]
ipf/form/db/image.php [deleted file]
ipf/form/db/integer.php [deleted file]
ipf/form/db/string.php [deleted file]
ipf/form/db/timestamp.php [deleted file]
ipf/form/model.php

diff --git a/ipf/form/db/date.php b/ipf/form/db/date.php
deleted file mode 100644 (file)
index e0a315a..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-<?php
-
-class IPF_Form_DB_Date extends IPF_Form_DB{
-    public $type = 'date';
-    function formField($def, $form_field='IPF_Form_Field_Date'){
-        return parent::formField($def, $form_field);
-    }
-}
diff --git a/ipf/form/db/datetime.php b/ipf/form/db/datetime.php
deleted file mode 100644 (file)
index 0e04e12..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-<?php
-
-class IPF_Form_DB_Datetime extends IPF_Form_DB{
-    public $type = 'datetime';
-    function formField($def, $form_field='IPF_Form_Field_Datetime'){
-        return parent::formField($def, $form_field);
-    }
-}
diff --git a/ipf/form/db/decimal.php b/ipf/form/db/decimal.php
deleted file mode 100644 (file)
index 4d5624b..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-<?php
-
-class IPF_Form_DB_Decimal extends IPF_Form_DB
-{
-    function formField($def, $form_field='IPF_Form_Field_Float')
-    {
-        $def['widget_attrs'] = array('style' => 'width:140px;');
-        return parent::formField($def, $form_field);
-    }
-}
-
diff --git a/ipf/form/db/double.php b/ipf/form/db/double.php
deleted file mode 100644 (file)
index 11a38e3..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-<?php
-
-class IPF_Form_DB_Double extends IPF_Form_DB
-{
-    function formField($def, $form_field='IPF_Form_Field_Float')
-    {
-        $def['widget_attrs'] = array('style' => 'width:140px;');
-        return parent::formField($def, $form_field);
-    }
-}
-
diff --git a/ipf/form/db/email.php b/ipf/form/db/email.php
deleted file mode 100644 (file)
index 73f030d..0000000
+++ /dev/null
@@ -1,13 +0,0 @@
-<?php
-
-class IPF_Form_DB_Email extends IPF_Form_DB
-{
-    public $type = 'varchar';
-    public $extra = array('size' => 200);
-
-    function formField($def, $form_field='IPF_Form_Field_Email')
-    {
-        return parent::formField($def, $form_field);
-    }
-}
-
diff --git a/ipf/form/db/file.php b/ipf/form/db/file.php
deleted file mode 100644 (file)
index 520344a..0000000
+++ /dev/null
@@ -1,12 +0,0 @@
-<?php
-
-class IPF_Form_DB_File extends IPF_Form_DB
-{
-    function formField($def, $form_field='IPF_Form_Field_File')
-    {
-        $field = parent::formField($def, $form_field);
-        $field->uploadTo = @$this->extra['uploadTo'];
-        return $field;
-    }
-}
-
diff --git a/ipf/form/db/html.php b/ipf/form/db/html.php
deleted file mode 100644 (file)
index 6c253f6..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-<?php
-
-class IPF_Form_DB_Html extends IPF_Form_DB{
-    function formField($def, $form_field='IPF_Form_Field_Html'){
-        return parent::formField($def, $form_field);
-    }
-}
diff --git a/ipf/form/db/image.php b/ipf/form/db/image.php
deleted file mode 100644 (file)
index fb741bf..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-<?php
-
-class IPF_Form_DB_Image extends IPF_Form_DB_File
-{
-    function formField($def, $form_field='IPF_Form_Field_Image')
-    {
-        return parent::formField($def, $form_field);
-    }
-}
-
diff --git a/ipf/form/db/integer.php b/ipf/form/db/integer.php
deleted file mode 100644 (file)
index a01663e..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-<?php
-
-class IPF_Form_DB_Integer extends IPF_Form_DB
-{
-    function formField($def, $form_field='IPF_Form_Field_Integer')
-    {
-        $def['widget_attrs'] = array('style' => 'width:140px;');
-        return parent::formField($def, $form_field);
-    }
-}
-
diff --git a/ipf/form/db/string.php b/ipf/form/db/string.php
deleted file mode 100644 (file)
index 90299e2..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-<?php
-
-class IPF_Form_DB_String extends IPF_Form_DB
-{
-    public $type = 'varchar';
-}
diff --git a/ipf/form/db/timestamp.php b/ipf/form/db/timestamp.php
deleted file mode 100644 (file)
index f881650..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-<?php
-
-class IPF_Form_DB_Timestamp extends IPF_Form_DB{
-    public $type = 'datetime';
-    function formField($def, $form_field='IPF_Form_Field_Datetime'){
-        return parent::formField($def, $form_field);
-    }
-}
index 1c72f74383a49f2ef606426e4ad4977c6d32d8c6..763c10715364c554b97ead7af2bf5bf54bce590a 100644 (file)
@@ -61,41 +61,54 @@ class IPF_Form_Model extends IPF_Form
         if ($name == $this->model->getTable()->getIdentifier())
             return;
 
-        $defaults = array(
-            'blank'     => true,
-            'help_text' => '',
+        $required = isset($col['notblank']) && $col['notblank'];
+        $label = isset($col['verbose']) ? $col['verbose'] : IPF_Utils::humanTitle($name);
+
+        $params = array(
+            'required'  => $required,
             'editable'  => true,
-            'verbose'   => isset($col['verbose']) ? $col['verbose'] : $name,
+            'label'     => $label,
+            'help_text' => '',
         );
 
-        $type = $col['type'];
-
-        if (isset($col['notblank'])) {
-            if ($col['notblank'])
-                $defaults['blank'] = false;
-            else
-                $defaults['blank'] = true;
+        switch ($col['type']) {
+            case 'string':
+                if (isset($col['length']))
+                    $params['max_length'] = (int)($col['length']);
+
+                if (isset($col['uploadTo']))
+                    $params['uploadTo'] = $col['uploadTo'];
+
+                if     (isset($col['email']) && $col['email'])  { $form_field = new IPF_Form_Field_Email($params); }
+                elseif (isset($col['file'])  && $col['file'])   { $form_field = new IPF_Form_Field_File($params); }
+                elseif (isset($col['image']) && $col['image'])  { $form_field = new IPF_Form_Field_Image($params); }
+                elseif (isset($col['html'])  && $col['html'])   { $form_field = new IPF_Form_Field_Html($params); }
+                else                                            { $form_field = new IPF_Form_Field_Varchar($params); }
+
+                break;
+            case 'boolean':
+                $form_field = new IPF_Form_Field_Boolean($params);
+                break;
+            case 'integer':
+                $params['widget_attrs'] = array('style' => 'width:140px;');
+                $form_field = new IPF_Form_Field_Integer($params);
+                break;
+            case 'double':
+            case 'decimal':
+                $params['widget_attrs'] = array('style' => 'width:140px;');
+                $form_field = new IPF_Form_Field_Float($params);
+                break;
+            case 'date':
+                $form_field = new IPF_Form_Field_Date($params);
+                break;
+            case 'datetime':
+            case 'timestamp':
+                $form_field = new IPF_Form_Field_Datetime($params);
+                break;
+            default:
+                throw new IPF_Exception_Form(__('Unsupported column type \''.$col['type'].'\'.'));
         }
-        if (isset($col['length']))
-            $defaults['max_length'] = (int)($col['length']);
-
-        if (isset($col['email']))
-            $type = 'email';
-
-        if (isset($col['file']))
-            $type = 'file';
-
-        if (isset($col['image']))
-            $type = 'image';
-
-        if (isset($col['html']))
-            $type = 'html';
-
-        $cn = 'IPF_Form_DB_'.ucfirst($type);
-
-        $db_field = new $cn('', $name, $col);
 
-        $form_field = $db_field->formField($defaults);
         if ($form_field !== null)
             $this->fields[$name] = $form_field;
     }