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

Organization Update

This commit addresses an issue where updating (editing) organization from
User's page resulted in ALL settings getting cleared. The change makes sure
the update is routed to the proper routine.
parent 306b7ef8
No related branches found
No related tags found
No related merge requests found
......@@ -92,8 +92,11 @@ class OrgsAjaxAPI extends AjaxController {
Http::response(404, 'Unknown organization');
$errors = array();
if($org->update($_POST, $errors))
Http::response(201, $org->to_json());
if ($profile) {
if ($org->updateProfile($_POST, $errors))
Http::response(201, $org->to_json(), 'application/json');
} elseif ($org->update($_POST, $errors))
Http::response(201, $org->to_json(), 'application/json');
$forms = $org->getForms();
......
......@@ -341,22 +341,7 @@ implements TemplateVariable {
return $base + $extra;
}
function update($vars, &$errors) {
$valid = true;
$forms = $this->getForms($vars);
foreach ($forms as $entry) {
if (!$entry->isValid())
$valid = false;
if ($entry->getDynamicForm()->get('type') == 'O'
&& ($f = $entry->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'));
}
}
function updateProfile($vars, &$errors) {
if ($vars['domain']) {
foreach (explode(',', $vars['domain']) as $d) {
......@@ -380,20 +365,13 @@ implements TemplateVariable {
}
}
if (!$valid || $errors)
return false;
// Attempt to valid & update dynamic data even on errors
if (!$this->update($vars, $errors))
$errors['error'] = __('Unable to update organization form');
foreach ($this->getDynamicData() as $entry) {
if ($entry->getDynamicForm()->get('type') == 'O'
&& ($name = $entry->getField('name'))
) {
$this->name = $name->getClean();
$this->save();
}
$entry->setSource($vars);
if ($entry->save())
$this->updated = SqlFunction::NOW();
}
if ($errors)
return false;
// Set flags
foreach (array(
......@@ -430,6 +408,43 @@ implements TemplateVariable {
return $this->save();
}
function update($vars, &$errors) {
$valid = true;
$forms = $this->getForms($vars);
foreach ($forms as $entry) {
if (!$entry->isValid())
$valid = false;
if ($entry->getDynamicForm()->get('type') == 'O'
&& ($f = $entry->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 || $errors)
return false;
// Save dynamic data.
foreach ($this->getDynamicData() as $entry) {
if ($entry->getDynamicForm()->get('type') == 'O'
&& ($name = $entry->getField('name'))
) {
$this->name = $name->getClean();
$this->save();
}
$entry->setSource($vars);
if ($entry->save())
$this->updated = SqlFunction::NOW();
}
return true;
}
function delete() {
if (!parent::delete())
return false;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment