From 207808d6d55c2b0353bc04fe780348b2622004e2 Mon Sep 17 00:00:00 2001 From: abijah Date: Mon, 6 Jul 2009 00:42:57 +0000 Subject: [PATCH] Added ability to add a new contact, which also fixes the glaring omission of saving changes to contact details on the edit page (previously we were only saving contact methods, not any contact details. git-svn-id: file:///svn-source/pmgr/branches/invoice_receipt_20090629/site@218 97e9348a-65ac-dc4b-aefc-98561f571b83 --- controllers/contacts_controller.php | 75 +++++++++++++++++++++------- controllers/customers_controller.php | 11 ++++ views/contacts/edit.ctp | 6 ++- 3 files changed, 72 insertions(+), 20 deletions(-) diff --git a/controllers/contacts_controller.php b/controllers/contacts_controller.php index 38c9330..845ac5c 100644 --- a/controllers/contacts_controller.php +++ b/controllers/contacts_controller.php @@ -98,14 +98,35 @@ class ContactsController extends AppController { * action: edit */ - function edit($id = null) { + function edit($id = null, $customer_id = null) { if (isset($this->data)) { if (isset($this->params['form']['cancel'])) { - $this->redirect(array('action'=>'view', $this->data['Contact']['id'])); + if (isset($this->data['Contact']['id'])) + $this->redirect(array('action'=>'view', $this->data['Contact']['id'])); +/* else */ +/* $this->redirect(array('controller' => 'customers', */ +/* 'action'=>'add', $this->data['Customer']['id'])); */ 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. @@ -158,22 +179,27 @@ class ContactsController extends AppController { return; } - if (!$id) { - $this->Session->setFlash(__('Invalid Item.', true)); - $this->redirect(array('action'=>'index')); + if ($id) { + $this->data = $this->Contact->find + ('first', array + ('contain' => array + (// Models + 'ContactPhone', + 'ContactEmail', + 'ContactAddress', + 'Customer'), + + 'conditions' => array('Contact.id' => $id), + )); + + $title = $this->data['Contact']['display_name'] . " : Edit"; + } + else { + $title = "Enter New Contact"; + $this->data = array('ContactPhone' => array(), + 'ContactAddress' => array(), + 'ContactEmail' => array()); } - - $this->data = $this->Contact->find - ('first', array - ('contain' => array - (// Models - 'ContactPhone', - 'ContactEmail', - 'ContactAddress', - 'Customer'), - - 'conditions' => array('Contact.id' => $id), - )); $phone_types = array_flip($this->Contact->ContactPhone->getEnumValues('type')); unset($phone_types[0]); @@ -208,7 +234,20 @@ class ContactsController extends AppController { // Prepare to render. //pr($this->data); - $title = $this->data['Contact']['display_name'] . " : Edit"; $this->set(compact('title')); + $this->render('edit'); } + + + /************************************************************************** + ************************************************************************** + ************************************************************************** + * action: add + * - Adds a new contact + */ + + function add($customer_id = null) { + $this->edit(null, $customer_id); + } + } diff --git a/controllers/customers_controller.php b/controllers/customers_controller.php index 997f9c5..24f7e3b 100644 --- a/controllers/customers_controller.php +++ b/controllers/customers_controller.php @@ -230,6 +230,17 @@ class CustomersController extends AppController { $this->sidemenu_links[] = array('name' => 'Operations', 'header' => true); + $this->sidemenu_links[] = + array('name' => 'Edit', + 'url' => array('action' => 'edit', + $id)); + + $this->sidemenu_links[] = + array('name' => 'Add Contact', + 'url' => array('controller' => 'contacts', + 'action' => 'add', + $id)); + $this->sidemenu_links[] = array('name' => 'Move-In', 'url' => array('action' => 'move_in', diff --git a/views/contacts/edit.ctp b/views/contacts/edit.ctp index 87b36d2..86e0592 100644 --- a/views/contacts/edit.ctp +++ b/views/contacts/edit.ctp @@ -342,13 +342,13 @@ function contactMethodTypeDiv($obj, $type, $stype, $values = null) { echo '
' . "\n"; -echo $form->create('Contact') . "\n"; +echo $form->create('Contact', array('action' => 'edit')) . "\n"; echo $form->input('id') . "\n"; echo($this->element ('form_table', array('class' => 'item contact detail', - 'caption' => 'Edit Contact', + 'caption' => $this->data ? 'Edit Contact' : 'New Contact', 'fields' => array ('first_name' => null, 'last_name' => null, @@ -364,6 +364,8 @@ echo($this->element //'' => null, //'' => '', ))) . "\n"); + +echo $form->submit('Update') . "\n"; ?>