From f1f3bb85e8bec79dd7219e795c1e3b830f30ac93 Mon Sep 17 00:00:00 2001 From: avl Date: Tue, 19 Aug 2008 07:54:20 +0300 Subject: [PATCH] Model Choice Field --- ipf/form/db.php | 12 ++++++++--- ipf/form/db/integer.php | 42 +++----------------------------------- ipf/form/field/integer.php | 5 +++++ ipf/form/model.php | 35 ++++++++++++++++++++++++++----- ipf/orm/export.php | 1 - ipf/orm/record.php | 8 +++++++- ipf/orm/table.php | 2 +- 7 files changed, 55 insertions(+), 50 deletions(-) diff --git a/ipf/form/db.php b/ipf/form/db.php index 5a4fb16..ba72c24 100644 --- a/ipf/form/db.php +++ b/ipf/form/db.php @@ -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); } diff --git a/ipf/form/db/integer.php b/ipf/form/db/integer.php index 0d9d002..d38b3fb 100644 --- a/ipf/form/db/integer.php +++ b/ipf/form/db/integer.php @@ -1,44 +1,8 @@ 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 diff --git a/ipf/form/field/integer.php b/ipf/form/field/integer.php index e7909dc..7ae4e8d 100644 --- a/ipf/form/field/integer.php +++ b/ipf/form/field/integer.php @@ -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 = ''; } diff --git a/ipf/form/model.php b/ipf/form/model.php index 1c5d2a2..7cfd874 100644 --- a/ipf/form/model.php +++ b/ipf/form/model.php @@ -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.')); diff --git a/ipf/orm/export.php b/ipf/orm/export.php index 7f5911b..27f4dd3 100644 --- a/ipf/orm/export.php +++ b/ipf/orm/export.php @@ -363,7 +363,6 @@ class IPF_ORM_Export extends IPF_ORM_Connection_Module { $sql = $this->getForeignKeyBaseDeclaration($definition); $sql .= $this->getAdvancedForeignKeyOptions($definition); - return $sql; } diff --git a/ipf/orm/record.php b/ipf/orm/record.php index 325e42d..7b2b03c 100644 --- a/ipf/orm/record.php +++ b/ipf/orm/record.php @@ -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 diff --git a/ipf/orm/table.php b/ipf/orm/table.php index 05ea8d5..6403fa8 100644 --- a/ipf/orm/table.php +++ b/ipf/orm/table.php @@ -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)); -- 2.49.0