Moved the contact save logic into the model and out of the controller.
git-svn-id: file:///svn-source/pmgr/branches/invoice_receipt_20090629@230 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
@@ -37,12 +37,100 @@ class Contact extends AppModel {
|
||||
),
|
||||
);
|
||||
|
||||
function contactList() {
|
||||
return $this->find('list',
|
||||
array('order' =>
|
||||
//array('last_name', 'first_name', 'middle_name'),
|
||||
array('display_name'),
|
||||
));
|
||||
|
||||
/**************************************************************************
|
||||
**************************************************************************
|
||||
**************************************************************************
|
||||
* function: saveContact
|
||||
* - Saves the contact and related data
|
||||
*/
|
||||
|
||||
function saveContact($id, $data) {
|
||||
|
||||
// Establish a display name if not already given
|
||||
if (!$data['Contact']['display_name'])
|
||||
$data['Contact']['display_name'] =
|
||||
(($data['Contact']['first_name'] &&
|
||||
$data['Contact']['last_name'])
|
||||
? $data['Contact']['last_name'] . ', ' . $data['Contact']['first_name']
|
||||
: ($data['Contact']['first_name']
|
||||
? $data['Contact']['first_name']
|
||||
: $data['Contact']['last_name']));
|
||||
|
||||
// Save the contact data
|
||||
$this->create();
|
||||
if ($id)
|
||||
$this->id = $id;
|
||||
if (!$this->save($data, false)) {
|
||||
return false;
|
||||
}
|
||||
$id = $this->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.
|
||||
// REVISIT <AP>: 20090706
|
||||
// Appears that $this->save() is already doing the
|
||||
// delete. I would have thought this would only happen
|
||||
// on a saveAll??
|
||||
/* $this->ContactsMethod->deleteAll */
|
||||
/* (array('contact_id' => $id), false); */
|
||||
|
||||
// At this point, since we've saved data to contact,
|
||||
// we'll proceed forward as much as possible, even
|
||||
// if we encounter an error. For now, we'll assume
|
||||
// the operation will succeed.
|
||||
$ret = true;
|
||||
|
||||
// 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($data[$class]))
|
||||
continue;
|
||||
|
||||
// Go through each entry of this contact method
|
||||
foreach ($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['id'])) {
|
||||
$I = new $class();
|
||||
$I->create();
|
||||
if (!$I->save($item, false)) {
|
||||
$ret = false;
|
||||
continue;
|
||||
}
|
||||
$item['id'] = $I->id;
|
||||
}
|
||||
|
||||
// Update the ContactsMethod to reflect the appropriate IDs
|
||||
$item['ContactsMethod']['contact_id'] = $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)) {
|
||||
$ret = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Return the result
|
||||
return $ret;
|
||||
}
|
||||
|
||||
function contactList() {
|
||||
return $this->find('list',
|
||||
array('order' =>
|
||||
//array('last_name', 'first_name', 'middle_name'),
|
||||
array('display_name'),
|
||||
));
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user