diff --git a/include/class.dynamic_forms.php b/include/class.dynamic_forms.php index 1c9c44fb07063e747c102e9d18c86ed3f3bff63b..3bb5fcf7e1d67785ca1ebbb21da531061c841d57 100644 --- a/include/class.dynamic_forms.php +++ b/include/class.dynamic_forms.php @@ -137,9 +137,10 @@ class DynamicForm extends VerySimpleModel { return $this->get('deletable'); } - function instanciate($sort=1) { - return DynamicFormEntry::create(array( - 'form_id'=>$this->get('id'), 'sort'=>$sort)); + function instanciate($sort=1, $data=null) { + return DynamicFormEntry::create( + array('form_id'=>$this->get('id'), 'sort'=>$sort), + $data); } function data($data) { @@ -970,9 +971,8 @@ class DynamicFormEntry extends VerySimpleModel { if (!parent::save($refetch || count($this->dirty))) return false; - foreach ($this->getFields() as $field) { - if (!($a = $field->getAnswer())) - continue; + foreach ($this->getAnswers() as $a) { + $field = $a->getField(); if ($this->object_type == 'U' && in_array($field->get('name'), array('name','email'))) @@ -1005,10 +1005,13 @@ class DynamicFormEntry extends VerySimpleModel { return parent::delete(); } - static function create($ht=false) { + static function create($ht=false, $data=null) { $inst = parent::create($ht); $inst->set('created', new SqlFunction('NOW')); - foreach ($inst->getForm()->getFields() as $f) { + $form = $inst->getForm(); + if ($data) + $form->setSource($data); + foreach ($form->getFields() as $f) { if (!$f->hasData()) continue; $a = DynamicFormEntryAnswer::create( array('field_id'=>$f->get('id'))); diff --git a/include/class.organization.php b/include/class.organization.php index 9f1076c246072460bab90bc3ca37454de2176330..458a1f7eed9a71b2e0cc217153ab2c92845f6286 100644 --- a/include/class.organization.php +++ b/include/class.organization.php @@ -105,15 +105,10 @@ class Organization extends OrganizationModel { var $_forms; function addDynamicData($data) { + $entry = $this->addForm(OrganizationForm::objects()->one(), 1, $data); + $entry->save(); - $of = OrganizationForm::getInstance($this->id, true); - foreach ($of->getFields() as $f) - if (isset($data[$f->get('name')])) - $of->setAnswer($f->get('name'), $data[$f->get('name')]); - - $of->save(); - - return $of; + return $entry; } function getDynamicData($create=true) { @@ -193,12 +188,12 @@ class Organization extends OrganizationModel { } } - function addForm($form, $sort=1) { - $form = $form->instanciate(); - $form->set('sort', $sort); - $form->set('object_type', 'O'); - $form->set('object_id', $this->getId()); - $form->save(); + function addForm($form, $sort=1, $data) { + $entry = $form->instanciate($sort, $data); + $entry->set('object_type', 'O'); + $entry->set('object_id', $this->getId()); + $entry->save(); + return $entry; } function getFilterData() { @@ -358,14 +353,15 @@ class Organization extends OrganizationModel { static function fromForm($form) { - if(!$form) return null; + if (!$form) + return null; //Validate the form $valid = true; if (!$form->isValid()) $valid = false; - //Make sure the email is not in-use + // Make sure the name is not in-use if (($field=$form->getField('name')) && $field->getClean() && Organization::lookup(array('name' => $field->getClean()))) { @@ -411,9 +407,9 @@ class OrganizationForm extends DynamicForm { return static::$form; } - static function getInstance($object_id=0, $new=false) { + static function getInstance($object_id=0, $new=false, $data=null) { if ($new || !isset(static::$instance)) - static::$instance = static::getDefaultForm()->instanciate(); + static::$instance = static::getDefaultForm()->instanciate(1, $data); static::$instance->object_type = 'O';