Files
pmgr/site/models/contact.php
Abijah a3148cc17d Fixed bug that was using an existing entry, even when the source had been selected as new.
git-svn-id: file:///svn-source/pmgr/branches/hosted_migration_20130215@1029 97e9348a-65ac-dc4b-aefc-98561f571b83
2013-05-30 03:38:17 +00:00

130 lines
3.8 KiB
PHP

<?php
class Contact extends AppModel {
var $displayField = 'display_name';
var $hasMany = array(
'ContactsMethod',
'ContactsCustomer',
);
var $hasAndBelongsToMany = array(
'Customer',
'ContactAddress' => array(
'joinTable' => 'contacts_methods',
'associationForeignKey' => 'method_id',
'unique' => true,
'conditions' => "method = 'ADDRESS'",
),
'ContactPhone' => array(
'joinTable' => 'contacts_methods',
'associationForeignKey' => 'method_id',
'unique' => true,
'conditions' => "method = 'PHONE'",
),
'ContactEmail' => array(
'joinTable' => 'contacts_methods',
'associationForeignKey' => 'method_id',
'unique' => true,
'conditions' => "method = 'EMAIL'",
),
);
/**************************************************************************
**************************************************************************
**************************************************************************
* 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']['first_name'] && $data['Contact']['last_name'])
$data['Contact']['display_name'] =
$data['Contact']['last_name'] . ', ' . $data['Contact']['first_name'];
foreach (array('last_name', 'first_name', 'company_name') AS $fld) {
if (!$data['Contact']['display_name'] && $data['Contact'][$fld])
$data['Contact']['display_name'] = $data['Contact'][$fld];
}
// 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']) || $item['source'] == 'new') {
$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'),
));
}
}
?>