Skip to content
Snippets Groups Projects
Commit 0f5b95f0 authored by Peter Rotich's avatar Peter Rotich
Browse files

Attach dynamic data to organization form

Don't save 'name' as dynamic data for organization form.
parent c3bb3831
Branches
Tags
No related merge requests found
......@@ -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]);
......
......@@ -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();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment