array('numeric'), 'display_name' => array('notempty'), 'id_federal' => array('ssn'), 'id_exp' => array('date') ); 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']['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 : 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'), )); } } ?>