]> git.andy128k.dev Git - ipf.git/commitdiff
Model Choice Field
authoravl <alex.litovchenko@gmail.com>
Tue, 19 Aug 2008 04:54:20 +0000 (07:54 +0300)
committeravl <alex.litovchenko@gmail.com>
Tue, 19 Aug 2008 04:54:20 +0000 (07:54 +0300)
ipf/form/db.php
ipf/form/db/integer.php
ipf/form/field/integer.php
ipf/form/model.php
ipf/orm/export.php
ipf/orm/record.php
ipf/orm/table.php

index 5a4fb16e2fe8e22a0e4f00a931134b65fcc38191..ba72c24dd6bb6ac1047f0e6296cb835ccf109981 100644 (file)
@@ -17,14 +17,19 @@ class IPF_Form_DB
 
     function formField($def, $form_field='IPF_Form_Field_Varchar')
     {
-        $defaults = array('required' => !$def['blank'], 
-                          'label' => IPF_Utils::humanTitle($def['verbose']), 
-                          'help_text' => $def['help_text']);
+        $defaults = array(
+            'required' => !$def['blank'], 
+            'label' => IPF_Utils::humanTitle($def['verbose']), 
+            'help_text' => $def['help_text'],
+            'type'=>$this->type,
+        );
+                          
         unset($def['blank'], $def['verbose'], $def['help_text']);
         if (isset($def['default'])) {
             $defaults['initial'] = $def['default'];
             unset($def['default']);
         }
+        /*
         if (isset($def['choices'])) {
             $defaults['widget'] = 'IPF_Form_Widget_SelectInput';
             if (isset($def['widget_attrs'])) {
@@ -39,6 +44,7 @@ class IPF_Form_DB
                 unset($def[$key]);
             }
         }
+        */
         $params = array_merge($defaults, $def);
         return new $form_field($params);
     }
index 0d9d002e9f68e01894d90674d9c89d9769d9012a..d38b3fb58bfb0d1d41c62a5e5ee8470d473d91ab 100644 (file)
@@ -1,44 +1,8 @@
 <?php
 
 class IPF_Form_DB_Integer extends IPF_Form_DB{
-
-    public $widget = 'IPF_Form_Widget_TextInput';
-    public $max = null;
-    public $min = null;
-
-    public function clean($value)
-    {
-        parent::clean($value);
-        if (in_array($value, $this->empty_values)) {
-            $value = '';
-        }
-        if (is_array($value)) {
-            reset($value);
-            while (list($i, $val) = each($value)) {
-                if (!preg_match('/[0-9]+/', $val)) {
-                    throw new IPF_Exception_Form(__('The value must be an integer.'));
-                }
-                $this->checkMinMax($val);
-                $value[$i] = (int) $val;
-            }
-            reset($value);
-            return $value;
-        } else {
-            if (!preg_match('/[0-9]+/', $value)) {
-                throw new IPF_Exception_Form(__('The value must be an integer.'));
-            }
-            $this->checkMinMax($value);
-        }
-        return (int) $value;
-    }
-
-    protected function checkMinMax($value)
-    {
-        if ($this->max !== null and $value > $this->max) {
-            throw new IPF_Exception_Form(sprintf(__('Ensure that this value is not greater than %1$d.'), $this->max));
-        }
-        if ($this->min !== null and $value < $this->min) {
-            throw new IPF_Exception_Form(sprintf(__('Ensure that this value is not lower than %1$d.'), $this->min));
-        }
+    function formField($def, $form_field='IPF_Form_Field_Integer'){
+        $def['widget_attrs'] = array('style'=>'width:40px;');
+        return parent::formField($def, $form_field);
     }
 }
\ No newline at end of file
index e7909dc5c1320af1dd4404cfb8a1bfe402f90702..7ae4e8d8d9ae580b5da715738576be150d9e2e61 100644 (file)
@@ -9,6 +9,11 @@ class IPF_Form_Field_Integer extends IPF_Form_Field
     public function clean($value)
     {
         parent::clean($value);
+
+        if (($this->required==false) and in_array($value, $this->empty_values)) {
+            return null;
+        }
+        
         if (in_array($value, $this->empty_values)) {
             $value = '';
         }
index 1c5d2a2518400f7b67a66b70aca4ae78df14acf8..7cfd8747d1db1b8c0026f0ef66c489b75b770e7e 100644 (file)
@@ -14,17 +14,23 @@ class IPF_Form_Model extends IPF_Form
         
         $user_fields = $this->fields();
         $db_columns = $this->model->getTable()->getColumns();
+        $db_relations = $this->model->getTable()->getRelations();
         
         
         if ($user_fields===null){
             foreach($db_columns as $name=>$col){
                 $this->addDBField($name,$col);
             }
+            foreach($db_relations as $name => $relation){
+                $this->addDBRelation($name,$relation);
+                   }
         }
         else{
             foreach($user_fields as $uname){
                 if (array_key_exists($uname,$db_columns))
                     $this->addDBField($uname,$db_columns[$uname]);
+                elseif (array_key_exists($uname,$db_relations))
+                    $this->addDBRelation($uname,$db_relations[$uname]);
                 else{
                     $add_method = 'add__'.$uname.'__field';
                     $this->$add_method();
@@ -39,15 +45,19 @@ class IPF_Form_Model extends IPF_Form
             
         $defaults = array('blank' => true, 'verbose' => $name, 'help_text' => '', 'editable' => true);
         $type = $col['type'];
+        
         if (isset($col['notblank']))
-            $defaults['blank'] = false;
+            if ($col['notblank'])
+                $defaults['blank'] = false;
+            else
+                $defaults['blank'] = true;
         if (isset($col['length']))
             $defaults['max_length'] = (int)($col['length']);
         if (isset($col['email']))
             $type = 'email';
         $cn = 'IPF_Form_DB_'.$type;
-        $db_field = new $cn('', $name);
         
+        $db_field = new $cn('', $name);
         //echo $name;
         //print_r($defaults);
         
@@ -55,16 +65,30 @@ class IPF_Form_Model extends IPF_Form
             $this->fields[$name] = $form_field;
         }
     }
-    
+
+    function addDBRelation($name,$relation){
+        if ($relation->getType()==IPF_ORM_Relation::ONE_AGGREGATE){
+            //$name .= "_id";
+            $db_field = new IPF_Form_DB_Foreignkey('',$name);
+            $defaults = array('blank' => true, 'verbose' => $name, 'help_text' => '', 'editable' => true, 'model'=>$relation->getClass());
+            $form_field = $db_field->formField($defaults);
+            $this->fields[$name] = $form_field;
+        }
+    }
+
     function fields(){ return $this->user_fields; }
 
     function save($commit=true)
     {
         if ($this->isValid()) {
+            
+            //print_r($this->cleaned_data);
+            
+            //print ($this->cleaned_data['category']);
+            
             $this->model->SetFromFormData($this->cleaned_data);
             
             //print_r($this->model->data);
-            
             /*
             if ($commit && $this->model->id) {
                 $this->model->update();
@@ -72,7 +96,8 @@ class IPF_Form_Model extends IPF_Form
                 $this->model->create();
             }
             */
-            print_r($this->model->save());
+            
+            $this->model->save();
             return $this->model;
         }
         throw new Exception(__('Cannot save the model from an invalid form.'));
index 7f5911b710c7bd1ff25e7e5285e9d58ac8106f5c..27f4dd34c98a85dd3880fb28c85b3670546c44e1 100644 (file)
@@ -363,7 +363,6 @@ class IPF_ORM_Export extends IPF_ORM_Connection_Module
     {
         $sql  = $this->getForeignKeyBaseDeclaration($definition);
         $sql .= $this->getAdvancedForeignKeyOptions($definition);
-
         return $sql;
     }
 
index 325e42d4c481e1ffa104c0ba8f76a373b368c66e..7b2b03c4f390d733e351eaf899ca802085c7c5b4 100644 (file)
@@ -1259,8 +1259,14 @@ abstract class IPF_ORM_Record extends IPF_ORM_Record_Abstract implements Countab
 
     function SetFromFormData($cleaned_values)
     {
+        //$relations = $this->getTable()->getRelations();
         foreach ($cleaned_values as $key=>$val) {
-            $this->$key = $val;
+            /*
+            if (array_key_exists($key,$relations)){
+                
+                
+            }else*/
+                $this->$key = $val;
         }
     }
 }
\ No newline at end of file
index 05ea8d5464892bfadd9a3c22265a01390cf53a72..6403fa819d0eb1233c54b1cf6a277088991f296f 100644 (file)
@@ -408,7 +408,7 @@ class IPF_ORM_Table extends IPF_ORM_Configurable implements Countable
         }
 
         $options['primary'] = $primary;
-
+        
         return array('tableName' => $this->getOption('tableName'),
                      'columns'   => $columns,
                      'options'   => array_merge($this->getOptions(), $options));