Finished implementation of contact edit. This is now saving everything to the database. I hope to now leverage it for a new contact. This checkin includes a bit of code in the sitelink2pmgr script that sets the customer display name. It should have been checked in several revisions ago.

git-svn-id: file:///svn-source/pmgr/branches/invoice_receipt_20090629/site@217 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-07-05 23:43:35 +00:00
parent 61af7693cd
commit dc787bd9e7
6 changed files with 124 additions and 21 deletions

View File

@@ -100,8 +100,61 @@ class ContactsController extends AppController {
function edit($id = null) {
if (isset($this->data)) {
pr($this->data);
$this->autoRender = false;
if (isset($this->params['form']['cancel'])) {
$this->redirect(array('action'=>'view', $this->data['Contact']['id']));
return;
}
// 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!");
}
}
}
// 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;
return;
}
@@ -124,16 +177,22 @@ class ContactsController extends AppController {
$phone_types = array_flip($this->Contact->ContactPhone->getEnumValues('type'));
unset($phone_types[0]);
//$phone_types = array_combine($phone_types, array_map('ucfirst', array_map('strtolower', $phone_types)));
// REVISIT <AP> 20090705
// Use this to have a mixed case enum
// array_map('ucfirst', array_map('strtolower', $phone_types))
$phone_types = array_combine($phone_types, $phone_types);
$this->set(compact('phone_types'));
$method_types = array_flip($this->Contact->getEnumValues('type', 'pmgr_contacts_methods'));
$method_types = array_flip($this->Contact->getEnumValues
('type',
$this->Contact->tablePrefix . 'contacts_methods'));
unset($method_types[0]);
$method_types = array_combine($method_types, $method_types);
$this->set(compact('method_types'));
$method_preferences = array_flip($this->Contact->getEnumValues('preference', 'pmgr_contacts_methods'));
$method_preferences = array_flip($this->Contact->getEnumValues
('preference',
$this->Contact->tablePrefix . 'contacts_methods'));
unset($method_preferences[0]);
$method_preferences = array_combine($method_preferences, $method_preferences);
$this->set(compact('method_preferences'));