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;
}

View File

@@ -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'),
));
}
}
?>

View File

@@ -239,7 +239,7 @@ class Customer extends AppModel {
/* $this->ContactsCustomer->deleteAll */
/* (array('customer_id' => $id), false); */
// At this point, since we've saved data to contact,
// At this point, since we've saved data to customer,
// we'll proceed forward as much as possible, even
// if we encounter an error. For now, we'll assume
// the operation will succeed.