diff --git a/include/class.dynamic_forms.php b/include/class.dynamic_forms.php index 580e631eb02b82a6e729c9edab0207e4d8b14dec..c58a22f4644e3d768caf172579fe5b2c2310ff4a 100644 --- a/include/class.dynamic_forms.php +++ b/include/class.dynamic_forms.php @@ -18,6 +18,7 @@ **********************************************************************/ require_once(INCLUDE_DIR . 'class.orm.php'); require_once(INCLUDE_DIR . 'class.forms.php'); +require_once(INCLUDE_DIR . 'class.filter.php'); require_once(INCLUDE_DIR . 'class.signal.php'); /** @@ -607,14 +608,23 @@ class DynamicFormEntry extends VerySimpleModel { // Add to list of answers $this->_values[] = $a; $this->_fields[] = $field; + $this->_form = null; + // Omit fields without data // For user entries, the name and email fields should not be // saved with the rest of the data - if (!($this->object_type == 'U' + if ($this->object_type == 'U' && in_array($field->get('name'), array('name','email'))) - && $field->hasData()) - $a->save(); - $this->_form = null; + continue; + + if ($this->object_type == 'O' + && in_array($field->get('name'), array('name'))) + continue; + + if (!$field->hasData()) + continue; + + $a->save(); } // Sort the form the way it is declared to be sorted if ($this->_fields) @@ -634,6 +644,11 @@ class DynamicFormEntry extends VerySimpleModel { if ($this->object_type == 'U' && in_array($field->get('name'), array('name','email'))) continue; + + if ($this->object_type == 'O' + && in_array($field->get('name'), array('name'))) + continue; + $val = $field->to_database($field->getClean()); if (is_array($val)) { $a->set('value', $val[0]); diff --git a/include/class.organization.php b/include/class.organization.php index 7cb66ce9f61e7fd8645765f164e866da1292b344..a37665291ae9981258ca32ce5510412513172b2d 100644 --- a/include/class.organization.php +++ b/include/class.organization.php @@ -13,6 +13,8 @@ vim: expandtab sw=4 ts=4 sts=4: **********************************************************************/ require_once(INCLUDE_DIR . 'class.orm.php'); +require_once(INCLUDE_DIR . 'class.forms.php'); +require_once(INCLUDE_DIR . 'class.dynamic_forms.php'); class OrganizationModel extends VerySimpleModel { static $meta = array( @@ -125,6 +127,41 @@ class Organization extends OrganizationModel { return parent::delete(); } + + function update($vars, &$errors) { + + $valid = true; + $forms = $this->getForms($vars); + foreach ($forms as $cd) { + if (!$cd->isValid()) + $valid = false; + if ($cd->get('type') == 'O' + && ($form= $cd->getForm($vars)) + && ($f=$form->getField('name')) + && $f->getClean() + && ($o=Organization::lookup(array('name'=>$f->getClean()))) + && $o->id != $this->getId()) { + $valid = false; + $f->addError('Organization with the same name already exists'); + } + } + + if (!$valid) + return false; + + foreach ($this->getDynamicData() as $cd) { + if (($f=$cd->getForm()) + && ($f->get('type') == 'O') + && ($name = $f->getField('name'))) { + $this->name = $name->getClean(); + $this->save(); + } + $cd->save(); + } + + return true; + } + static function fromVars($vars) { if (!($org = Organization::lookup(array('name' => $vars['name'])))) { @@ -134,6 +171,7 @@ class Organization extends OrganizationModel { 'updated' => new SqlFunction('NOW'), )); $org->save(true); + $org->addDynamicData($vars); } return $org; @@ -161,7 +199,7 @@ class Organization extends OrganizationModel { } -class OragnizationForm extends DynamicForm { +class OrganizationForm extends DynamicForm { static $instance; static $form; @@ -172,7 +210,7 @@ class OragnizationForm extends DynamicForm { static function getDefaultForm() { if (!isset(static::$form)) { - if ($o = static::objects()) + if (($o = static::objects()) && $o[0]) static::$form = $o[0]; else //TODO: Remove the code below and move it to task?? static::$form = self::__loadDefaultForm();