+++ /dev/null
-<?php
-
-class IPF_Form_DB
-{
- public $type = '';
- public $column = '';
- public $value;
- public $extra = array();
- public $methods = array();
-
- function __construct($value='', $column='', $extra=array())
- {
- $this->value = $value;
- $this->column = $column;
- $this->extra = array_merge($this->extra, $extra);
- }
-
- 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'],
- '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'])) {
- $def['widget_attrs']['choices'] = $def['choices'];
- } else {
- $def['widget_attrs'] = array('choices' => $def['choices']);
- }
- }
- foreach (array_keys($def) as $key) {
- if (!in_array($key, array('max_length','widget', 'label', 'required',
- 'initial', 'choices', 'widget_attrs'))) {
- unset($def[$key]);
- }
- }
- */
- $params = array_merge($defaults, $def);
- return new $form_field($params);
- }
-}
-
+++ /dev/null
-<?php
-
-class IPF_Form_DB_Boolean extends IPF_Form_DB
-{
- public $type = 'boolean';
-
- function formField($def, $form_field='IPF_Form_Field_Boolean')
- {
- return parent::formField($def, $form_field);
- }
-}
-
+++ /dev/null
-<?php
-
-class IPF_Form_DB_Foreignkey extends IPF_Form_DB
-{
- function formField($def, $form_field='IPF_Form_Field_ModelChoice'){
- $gmodel = new $def['model']();
- $def['queryset'] = $gmodel->getTable()->findAll();
- $def['model'] = $gmodel;
- if ($def['blank']==1)
- $def['required'] = false;
- else
- $def['required'] = true;
- return parent::formField($def, $form_field);
- }
-}
+++ /dev/null
-<?php
-
-class IPF_Form_DB_Manytomany extends IPF_Form_DB
-{
- public $type = 'manytomany';
-
- function formField($def, $form_field='IPF_Form_Field_ModelMultipleChoice')
- {
- $list_objects = IPF_ORM::getTable($def['model'])->findAll();
- $pk = IPF_ORM::getTable($def['model'])->getIdentifier();
- $choices = array();
- foreach($list_objects as $o){
- $choices[$o->__toString()] = $o->$pk;
- }
- $def['choices'] = $choices;
- if (!isset($def['widget'])) {
- $def['widget'] = 'IPF_Form_Widget_SelectMultipleInput';
- }
- return parent::formField($def, $form_field);
- }
-}
-
<?php
-class IPF_Form_Field_ModelChoice extends IPF_Form_Field_Choice{
+class IPF_Form_Field_ModelChoice extends IPF_Form_Field_Choice
+{
+ protected $table;
- protected $_model;
-
- function __construct($params=array()){
+ function __construct($params=array())
+ {
parent::__construct($params);
- $this->_model = $params['model'];
- if (isset($params['queryset'])){
- $choices = array('--------'=>'');
+ $this->table = $params['table'];
+ if (isset($params['queryset'])) {
+ $choices = array('--------' => '');
foreach ($params['queryset'] as $item) {
$choices[(string)$item] = $item->id;
}
}
}
- public function clean($value){
+ public function clean($value)
+ {
parent::clean($value);
- if (in_array($value, $this->empty_values)) {
+ if (in_array($value, $this->empty_values))
return null;
- }
- $o = $this->_model->getTable()->find($value);
- return $o;
+ return $this->table->find($value);
}
}
+
$params = array(
'required' => $required,
- 'editable' => true,
'label' => $label,
'help_text' => '',
);
$defaults = array(
'blank' => !isset($col['notblank']),
'help_text' => '',
- 'editable' => true,
'model' => $relation->getClass(),
'verbose' => isset($col['verbose']) ? $col['verbose'] : $name,
);
+ $table = IPF_ORM::getTable($relation->getClass());
+
+ $params = array(
+ 'required' => isset($col['notblank']),
+ 'label' => isset($col['verbose']) ? $col['verbose'] : IPF_Utils::humanTitle($name),
+ 'help_text' => '',
+ 'table' => $table,
+ );
+
if ($rt === IPF_ORM_Relation::ONE_AGGREGATE) {
- $name .= "_id";
- $db_field = new IPF_Form_DB_Foreignkey('',$name);
- return array($name, $db_field->formField($defaults));
+ $params['queryset'] = $table->findAll();
+ return array($name.'_id', new IPF_Form_Field_ModelChoice($params));
} elseif ($rt === IPF_ORM_Relation::MANY_AGGREGATE) {
- $db_field = new IPF_Form_DB_Manytomany('',$name);
- return array($name, $db_field->formField($defaults));
+
+ $pk = $table->getIdentifier();
+ $choices = array();
+ foreach ($table->findAll() as $o){
+ $choices[$o->__toString()] = $o->$pk;
+ }
+
+ $params['choices'] = $choices;
+ $params['widget'] = 'IPF_Form_Widget_SelectMultipleInput';
+ return array($name, new IPF_Form_Field_ModelMultipleChoice($params));
} else {
return null;
}