From 5ad28c809b2da4224f0ce46a299ccfa18a03f170 Mon Sep 17 00:00:00 2001 From: abijah Date: Sun, 23 Aug 2009 19:23:49 +0000 Subject: [PATCH] Fixed the relationship field for customer and contact grids. git-svn-id: file:///svn-source/pmgr/branches/pre_0.1_work_20090819@750 97e9348a-65ac-dc4b-aefc-98561f571b83 --- site/controllers/contacts_controller.php | 13 +++++++++ site/controllers/customers_controller.php | 34 +++++++++++++++-------- site/views/contacts/view.ctp | 1 + site/views/customers/view.ctp | 2 +- site/views/elements/contacts.ctp | 13 ++++----- site/views/elements/customers.ctp | 7 +---- 6 files changed, 44 insertions(+), 26 deletions(-) diff --git a/site/controllers/contacts_controller.php b/site/controllers/contacts_controller.php index 8d24e38..10cd928 100644 --- a/site/controllers/contacts_controller.php +++ b/site/controllers/contacts_controller.php @@ -23,6 +23,19 @@ class ContactsController extends AppController { * 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); diff --git a/site/controllers/customers_controller.php b/site/controllers/customers_controller.php index b5d423c..fca37d9 100644 --- a/site/controllers/customers_controller.php +++ b/site/controllers/customers_controller.php @@ -92,19 +92,29 @@ class CustomersController extends AppController { return $conditions; } - function gridDataOrder(&$params, &$model, $index, $direction) { - $order = array(); - $order[] = parent::gridDataOrder($params, $model, $index, $direction); + function gridDataFilterTablesConfig(&$params, &$model, $table) { + $config = parent::gridDataFilterTablesConfig($params, $model, $table); - if ($index !== 'PrimaryContact.last_name') - $order[] = parent::gridDataOrder($params, $model, - 'PrimaryContact.last_name', $direction); - if ($index !== 'PrimaryContact.first_name') - $order[] = parent::gridDataOrder($params, $model, - 'PrimaryContact.first_name', $direction); - if ($index !== 'Customer.id') - $order[] = parent::gridDataOrder($params, $model, - 'Customer.id', $direction); + // Special case for Contact; We need the Contact/Customer relationship + if ($table == 'Contact') + $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[] = 'PrimaryContact.last_name ' . $direction; + $order[] = 'PrimaryContact.first_name ' . $direction; + $order[] = 'Customer.id ' . $direction; return $order; } diff --git a/site/views/contacts/view.ctp b/site/views/contacts/view.ctp index 0803999..55ac199 100644 --- a/site/views/contacts/view.ctp +++ b/site/views/contacts/view.ctp @@ -135,6 +135,7 @@ echo $this->element('customers', array 'config' => array ('caption' => 'Related Customers', 'filter' => array('Contact.id' => $contact['id']), + 'include' => array('Relationship'), ))); diff --git a/site/views/customers/view.ctp b/site/views/customers/view.ctp index 8a0bb48..a5d9456 100644 --- a/site/views/customers/view.ctp +++ b/site/views/customers/view.ctp @@ -61,7 +61,7 @@ echo $this->element('contacts', array 'config' => array ('caption' => 'Customer Contacts', 'filter' => array('Customer.id' => $customer['Customer']['id']), - 'include' => array('Type', 'Active'), + 'include' => array('Relationship'), ))); diff --git a/site/views/elements/contacts.ctp b/site/views/elements/contacts.ctp index 92c36a7..7267642 100644 --- a/site/views/elements/contacts.ctp +++ b/site/views/elements/contacts.ctp @@ -2,12 +2,11 @@ // Define the table columns $cols = array(); -$cols['Last Name'] = array('index' => 'Contact.last_name', 'formatter' => 'name'); -$cols['First Name'] = array('index' => 'Contact.first_name', 'formatter' => 'name'); -$cols['Company'] = array('index' => 'Contact.company_name', 'formatter' => 'longname'); -$cols['Type'] = array('index' => 'ContactsCustomer.type', 'formatter' => 'enum'); -$cols['Active'] = array('index' => 'ContactsCustomer.active', 'formatter' => 'enum'); -$cols['Comment'] = array('index' => 'Contact.comment', 'formatter' => 'comment'); +$cols['Relationship'] = array('index' => 'ContactsCustomer.type', 'formatter' => 'enum'); +$cols['Last Name'] = array('index' => 'Contact.last_name', 'formatter' => 'longname'); +$cols['First Name'] = array('index' => 'Contact.first_name', 'formatter' => 'longname'); +$cols['Company'] = array('index' => 'Contact.company_name', 'formatter' => 'longname'); +$cols['Comment'] = array('index' => 'Contact.comment', 'formatter' => 'comment'); // Render the grid $grid @@ -16,4 +15,4 @@ $grid ->defaultFields(array('Last Name', 'First Name')) ->searchFields(array('Last Name', 'First Name', 'Company')) ->render($this, isset($config) ? $config : null, - array_diff(array_keys($cols), array('Type', 'Active', 'Comment'))); + array_diff(array_keys($cols), array('Relationship', 'Comment'))); diff --git a/site/views/elements/customers.ctp b/site/views/elements/customers.ctp index b0d2702..2dbfb86 100644 --- a/site/views/elements/customers.ctp +++ b/site/views/elements/customers.ctp @@ -10,11 +10,6 @@ $cols['Leases'] = array('index' => 'current_lease_count', 'formatt $cols['Balance'] = array('index' => 'balance', 'formatter' => 'currency'); $cols['Comment'] = array('index' => 'Customer.comment', 'formatter' => 'comment'); - -// Certain fields are only valid with a particular context -if (!isset($config['filter']['Contact.id'])) - $grid->invalidFields('Relationship'); - // Render the grid $grid ->columns($cols) @@ -22,4 +17,4 @@ $grid ->defaultFields(array('Name')) ->searchFields(array('Name', 'Last Name', 'First Name')) ->render($this, isset($config) ? $config : null, - array_diff(array_keys($cols), array('Comment'))); + array_diff(array_keys($cols), array('Relationship', 'Comment')));