sidemenu_links); } /************************************************************************** ************************************************************************** ************************************************************************** * action: index / all * - Generate a listing of contacts */ function index() { $this->all(); } function all() { $this->jqGridView('All Contacts', 'all'); } /************************************************************************** ************************************************************************** ************************************************************************** * virtuals: jqGridData * - With the application controller handling the jqGridData action, * these virtual functions ensure that the correct data is passed * to jqGrid. */ function jqGridDataOrder(&$params, &$model, $index, $direction) { $order = parent::jqGridDataOrder($params, $model, $index, $direction); if ($index === 'Contact.last_name') { $order[] = 'Contact.first_name ' . $direction; } if ($index === 'Contact.first_name') { $order[] = 'Contact.last_name ' . $direction; } return $order; } function jqGridRecordLinks(&$params, &$model, &$records, $links) { $links['Contact'] = array('id'); return parent::jqGridRecordLinks($params, $model, $records, $links); } /************************************************************************** ************************************************************************** ************************************************************************** * action: view * - Displays information about a specific contact */ function view($id = null) { if (!$id) { $this->Session->setFlash(__('Invalid Item.', true)); $this->redirect(array('action'=>'index')); } $contact = $this->Contact->find ('first', array ('contain' => array (// Models 'ContactPhone', 'ContactEmail', 'ContactAddress', 'Customer'), 'conditions' => array('Contact.id' => $id), )); // Set up dynamic menu items $this->sidemenu_links[] = array('name' => 'Operations', 'header' => true); $this->sidemenu_links[] = array('name' => 'Edit', 'url' => array('action' => 'edit', $id)); // Prepare to render. $title = $contact['Contact']['display_name']; $this->set(compact('contact', 'title')); } /************************************************************************** ************************************************************************** ************************************************************************** * action: edit */ function edit($id = null) { if (isset($this->data)) { 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; } if (!$id) { $this->Session->setFlash(__('Invalid Item.', true)); $this->redirect(array('action'=>'index')); } $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]); // REVISIT 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', $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', $this->Contact->tablePrefix . 'contacts_methods')); unset($method_preferences[0]); $method_preferences = array_combine($method_preferences, $method_preferences); $this->set(compact('method_preferences')); $contact_phones = $this->Contact->ContactPhone->phoneList(); $this->set(compact('contact_phones')); $contact_addresses = $this->Contact->ContactAddress->addressList(); $this->set(compact('contact_addresses')); $contact_emails = $this->Contact->ContactEmail->emailList(); $this->set(compact('contact_emails')); // Prepare to render. //pr($this->data); $title = $this->data['Contact']['display_name'] . " : Edit"; $this->set(compact('title')); } }