From a57f714cd2a795ec29f085e10b679eaec524b991 Mon Sep 17 00:00:00 2001 From: Antonio de la Rosa Date: Tue, 26 Jan 2016 17:59:02 +0100 Subject: [PATCH] Simple fix in foreignkeyfields and addded new features for pass arguments to forms via model field --- src/CoreFields/ForeignKeyField.php | 9 +++++---- src/CoreFields/PhangoField.php | 2 +- src/Forms/BaseForm.php | 2 +- src/Forms/SelectModelForm.php | 17 +++++++++++++++-- src/Webmodel.php | 7 ++++++- 5 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/CoreFields/ForeignKeyField.php b/src/CoreFields/ForeignKeyField.php index beb49fc..7d4dee8 100644 --- a/src/CoreFields/ForeignKeyField.php +++ b/src/CoreFields/ForeignKeyField.php @@ -25,21 +25,22 @@ class ForeignKeyField extends IntegerField{ public $fields_related_model; public $name_field_to_field; - function __construct($related_model, $size=11, $default=0) + function __construct($related_model, $size=11, $default_id=0, $name_field='', $name_value='') { $this->size=$size; - $this->form='PhangoApp\PhaModels\Forms\SelectForm'; + $this->form='PhangoApp\PhaModels\Forms\SelectModelForm'; $this->related_model=&$related_model; $this->container_model=$this->related_model->name; //Fields obtained from related_model if you make a query... $this->fields_related_model=array(); //Representative field for related model... - $this->name_field_to_field=''; - $this->default_id=$default; + $this->name_field_to_field=$name_field; + $this->default_id=$default_id; $this->quot_open=''; $this->quot_close=''; $this->protected=0; + $this->parameters=array(&$this->related_model, $name_field, $name_value); } diff --git a/src/CoreFields/PhangoField.php b/src/CoreFields/PhangoField.php index e362b18..9405c53 100644 --- a/src/CoreFields/PhangoField.php +++ b/src/CoreFields/PhangoField.php @@ -103,7 +103,7 @@ class PhangoField { public $form_loaded; /** - * Array for create initial parameters for form.. + * Array for create initial extra parameters for form. */ public $parameters=array(); diff --git a/src/Forms/BaseForm.php b/src/Forms/BaseForm.php index c92475f..3bfb62c 100644 --- a/src/Forms/BaseForm.php +++ b/src/Forms/BaseForm.php @@ -25,7 +25,7 @@ class BaseForm { public $enctype=0; - public function __construct($name, $value) + public function __construct($name, $value, $extra_parameters=array()) { $this->label=$name; $this->name=$name; diff --git a/src/Forms/SelectModelForm.php b/src/Forms/SelectModelForm.php index 228ddf5..b90f5e6 100644 --- a/src/Forms/SelectModelForm.php +++ b/src/Forms/SelectModelForm.php @@ -13,13 +13,26 @@ class SelectModelForm extends SelectForm{ public $model; - public $conditions='WHERE 1=1'; + public $conditions=['WHERE 1=1', []]; public $field_value=''; public $field_name=''; public $raw_query=0; + + public function __construct($name, $value, $model, $field_name, $field_value) + { + + parent::__construct($name, $value); + + $this->model=&$model; + + $this->field_name=$field_name; + + $this->field_value=$field_value; + + } public function form() { @@ -32,7 +45,7 @@ class SelectModelForm extends SelectForm{ } - $this->model->set_conditions($this->conditions); + $this->model->set_conditions($this->conditions[0], $this->conditions[1]); $query=$this->model->select(array($this->field_name, $this->field_value), $this->raw_query); diff --git a/src/Webmodel.php b/src/Webmodel.php index d171e8d..a990a20 100644 --- a/src/Webmodel.php +++ b/src/Webmodel.php @@ -2032,7 +2032,12 @@ class Webmodel { $form_class=$component->form; - $this->forms[$component_name]=new $form_class($component_name, $component->value); + //$this->forms[$component_name]=new $form_class($component_name, $component->value, $component->parameters); + $rc=new \ReflectionClass($form_class); + + array_unshift($component->parameters, $component_name, $component->value); + + $this->forms[$component_name]=$rc->newInstanceArgs($component->parameters); $type_class=get_class($this->forms[$component_name]);