all(); } function all() { $this->gridView('All Contacts', 'all'); } /************************************************************************** ************************************************************************** ************************************************************************** * virtuals: gridData * - With the application controller handling the gridData action, * these virtual functions ensure that the correct data is passed * to jqGrid. */ function gridDataFilterTablesConfig(&$params, &$model, $table) { $config = parent::gridDataFilterTablesConfig($params, $model, $table); // Special case for Customer; We need the Contact/Customer relationship if ($table == 'Customer') $config = array('fields' => array('ContactsCustomer.type', 'ContactsCustomer.active'), 'conditions' => array('ContactsCustomer.active' => true), ); return $config; } function gridDataOrder(&$params, &$model, $index, $direction) { $order = parent::gridDataOrder($params, $model, $index, $direction); // After sorting by whatever the user wants, add these // defaults into the sort mechanism. If we're already // sorting by one of them, it will only be redundant, // and should cause no harm (possible a longer query?) $order[] = 'Contact.last_name ' . $direction; $order[] = 'Contact.first_name ' . $direction; return $order; } function gridDataPostProcessLinks(&$params, &$model, &$records, $links) { $links['Contact'] = array('display_name'); return parent::gridDataPostProcessLinks($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->addSideMenuLink('Edit', array('action' => 'edit', $id), null, 'ACTION'); // Prepare to render. $title = 'Contact: ' . $contact['Contact']['display_name']; $this->set(compact('contact', 'title')); } /************************************************************************** ************************************************************************** ************************************************************************** * action: edit */ function edit($id = null, $customer_id = null) { if (isset($this->data)) { if (isset($this->params['form']['cancel'])) { 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; } // Go through each contact method and strip the bogus ID if new foreach (array_intersect_key($this->data, array('ContactPhone'=>1, 'ContactAddress'=>1, 'ContactEmail'=>1)) AS $type => $arr) { foreach ($arr AS $idx => $item) { if (isset($item['source']) && $item['source'] === 'new') unset($this->data[$type][$idx]['id']); } } // Save the contact and all associated data $this->Contact->saveContact($this->data['Contact']['id'], $this->data); // Now that the work is done, let the user view the updated contact $this->redirect(array('action'=>'view', $this->data['Contact']['id'])); } if ($id) { $this->data = $this->Contact->find ('first', array ('contain' => array (// Models 'ContactPhone', 'ContactEmail', 'ContactAddress', 'Customer'), 'conditions' => array('Contact.id' => $id), )); $title = 'Contact: ' . $this->data['Contact']['display_name'] . " : Edit"; } else { $title = "Enter New Contact"; $this->data = array('ContactPhone' => array(), 'ContactAddress' => array(), 'ContactEmail' => array()); } $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); $this->set(compact('title')); $this->render('edit'); } /************************************************************************** ************************************************************************** ************************************************************************** * action: add * - Adds a new contact */ function add($customer_id = null) { $this->edit(null, $customer_id); } }