Moved the contact save logic into the model and out of the controller.

git-svn-id: file:///svn-source/pmgr/branches/invoice_receipt_20090629/site@230 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-07-06 17:25:05 +00:00
parent 280c5dae55
commit 850d15eb50
3 changed files with 107 additions and 68 deletions

View File

@@ -110,72 +110,23 @@ class ContactsController extends AppController {
return;
}
if (!$this->data['Contact']['display_name'])
$this->data['Contact']['display_name'] =
(($this->data['Contact']['first_name'] &&
$this->data['Contact']['last_name'])
? $this->data['Contact']['last_name'] . ', ' . $this->data['Contact']['first_name']
: ($this->data['Contact']['first_name']
? $this->data['Contact']['first_name']
: $this->data['Contact']['last_name']));
$this->Contact->create();
if (!$this->Contact->save($this->data, false)) {
pr("CONTACT SAVE FAILED");
$this->Session->setFlash("CONTACT SAVE FAILED", true);
return;
}
$this->data['Contact']['id'] = $this->Contact->id;
// Remove all associated ContactMethods, as it ensures
// any entries deleted by the user actually get deleted
// in the system. We'll recreate the needed ones anyway.
$this->Contact->ContactsMethod->deleteAll
(array('contact_id' => $this->data['Contact']['id']), false);
// Iterate each type of contact method, adding them into
// the database as needed and associating with this contact.
foreach (array('phone', 'address', 'email') AS $type) {
$class = 'Contact' . ucfirst($type);
$enum = strtoupper($type);
// Nothing to do if this contact method isn't used
if (!isset($this->data[$class]))
continue;
// Go through each entry of this contact method
foreach ($this->data[$class] AS &$item) {
// If the user has entered all new data, we need to
// save that as a brand new entry.
if (isset($item['source']) && $item['source'] === 'new') {
unset($item['id']);
$I = new $class();
$I->create();
if (!$I->save($item, false)) {
pr("$enum SAVE FAILED");
$this->Session->setFlash("$enum SAVE FAILED", true);
continue;
}
$item['id'] = $I->id;
}
// Update the ContactsMethod to reflect the appropriate IDs
$item['ContactsMethod']['contact_id'] = $this->data['Contact']['id'];
$item['ContactsMethod']['method_id'] = $item['id'];
$item['ContactsMethod']['method'] = $enum;
// Save the relationship between contact and phone/email/address
$CM = new ContactsMethod();
if (!$CM->save($item['ContactsMethod'], false)) {
die("ContactsMethod Save FAILED!");
}
// Go through each contact method and strip the bogus ID if new
foreach (array_intersect_key($this->data,
array('ContactPhone'=>1,
'ContactAddress'=>1,
'ContactEmail'=>1)) AS $type => $arr) {
foreach ($arr AS $idx => $item) {
if (isset($item['source']) && $item['source'] === 'new')
unset($this->data[$type][$idx]['id']);
}
}
// Save the contact and all associated data
$this->Contact->saveContact($this->data['Contact']['id'], $this->data);
// Now that the work is done, let the user view the updated contact
$this->redirect(array('action'=>'view', $this->data['Contact']['id']));
//$this->autoRender = false;
//$this->render('/empty');
return;
}