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'));

View File

@@ -9,13 +9,17 @@ class Contact extends AppModel {
'id_exp' => array('date')
);
var $hasMany = array(
'ContactsMethod',
);
var $hasAndBelongsToMany = array(
'Customer',
'ContactAddress' => array(
'joinTable' => 'contacts_methods',
'associationForeignKey' => 'method_id',
'unique' => true,
'conditions' => "method = 'POST'",
'conditions' => "method = 'ADDRESS'",
),
'ContactPhone' => array(
'joinTable' => 'contacts_methods',

View File

@@ -7,6 +7,13 @@ class ContactAddress extends AppModel {
'postcode' => array('postal')
);
var $hasMany = array(
'ContactsMethod' => array(
'foreignKey' => 'method_id',
'conditions' => "method = 'ADDRESS'",
)
);
var $hasAndBelongsToMany = array(
'Contact' => array(
'className' => 'Contact',
@@ -14,7 +21,7 @@ class ContactAddress extends AppModel {
'foreignKey' => 'method_id',
'associationForeignKey' => 'contact_id',
'unique' => true,
'conditions' => "method = 'POST'",
'conditions' => "method = 'ADDRESS'",
)
);

View File

@@ -8,6 +8,13 @@ class ContactPhone extends AppModel {
'ext' => array('numeric')
);
var $hasMany = array(
'ContactsMethod' => array(
'foreignKey' => 'method_id',
'conditions' => "method = 'PHONE'",
)
);
var $hasAndBelongsToMany = array(
'Contact' => array(
'className' => 'Contact',

View File

@@ -0,0 +1,25 @@
<?php
class ContactsMethod extends AppModel {
var $primaryKey = false;
var $belongsTo = array(
'Contact',
'ContactAddress' => array(
'foreignKey' => 'method_id',
'conditions' => "method = 'ADDRESS'",
//'unique' => true,
),
'ContactPhone' => array(
'foreignKey' => 'method_id',
'conditions' => "method = 'PHONE'",
//'unique' => true,
),
'ContactEmail' => array(
'foreignKey' => 'method_id',
'conditions' => "method = 'EMAIL'",
//'unique' => true,
),
);
}
?>

View File

@@ -40,7 +40,7 @@ function contactMethodDiv($obj, $type, $legend, $values = null) {
$stype = strtolower($sname);
$div .=
'<INPUT TYPE="radio" ' . "\n" .
//' NAME="data[Contact'.ucfirst($type).'][%{id}][source]"' . "\n" .
' NAME="data[Contact'.ucfirst($type).'][%{id}][source]"' . "\n" .
' ONCLICK="switchMethodSource(%{id}, '."'{$type}', '{$stype}'".')"' . "\n" .
' CLASS="'.$type.'-method-%{id}-source" ' . "\n" .
' ID="'.$type.'-method-%{id}-source-'.$stype.'"' . "\n" .
@@ -70,24 +70,28 @@ function contactMethodDiv($obj, $type, $legend, $values = null) {
// BEGIN method-div
'<div id="'.$type.'-%{id}-method-div"' . "\n" .
/* (isset($values) */
/* ? '<input type="hidden" name="data[Contact'.ucfirst($type).'][%{id}][ContactsMethod][id]" value="'.$values['ContactsMethod']['id'].'"/>' . "\n" */
/* : '') . */
$obj->element
('form_table',
array('class' => "item contact-{$type} entry",
'field_prefix' => 'Contact'.ucfirst($type).'.%{id}.Method',
'field_prefix' => 'Contact'.ucfirst($type).'.%{id}.ContactsMethod',
'fields' => array
('type' => array
('opts' => array
('options' => $obj->varstore['methodTypes'],
'selected' => (isset($values) ? $values['ContactsMethod']['type'] : null),
)),
'preferences' => array
(
'preference' => array
('opts' => array
('options' => $obj->varstore['methodPreferences'],
'selected' => (isset($values) ? $values['ContactsMethod']['preference'] : null),
)),
'type' => array
('opts' => array
('options' => $obj->varstore['methodTypes'],
'selected' => (isset($values) ? $values['ContactsMethod']['type'] : null),
)),
'comment' => array
('opts' => array
('value' => (isset($values) ? $values['ContactsMethod']['comment'] : null),
@@ -175,9 +179,6 @@ function contactMethodTypeDiv($obj, $type, $stype, $values = null) {
}
}
elseif ($type === 'email') {
/* [email] => abijah@PerkinsHouse.com */
/* [comment] => */
if ($stype === 'existing') {
$fields = array
('id' => array('name' => 'Email',
@@ -402,6 +403,6 @@ echo($this->element
; // Alignment
echo $form->submit('Update') . "\n";
echo $form->submit('Cancel') . "\n";
echo $form->submit('Cancel', array('name' => 'cancel')) . "\n";
echo $form->end() . "\n";
echo '</div>' . "\n";