From 37990ecd7971b72d94f72d136f1fd69d86c765e9 Mon Sep 17 00:00:00 2001 From: Johan Vervloet Date: Tue, 15 Dec 2015 15:05:36 +0100 Subject: [PATCH] CRM-16587 - workaround selected records custom search. This is a workaround, and it will probably not work 100% in these cases: * if you need to select columns with identical names from different tables * if you need expressions in your SELECT statement, instead of just selecting a column. You also have to adapt your custom searches for this to work. I changed the PostalMailing custom search so that you now can apply actions on a selection of the retuned records. ---------------------------------------- * CRM-16587: Action menu is not available on selected records on a Custom search https://issues.civicrm.org/jira/browse/CRM-16587 --- .../Form/Search/Custom/PostalMailing.php | 33 ++++++++++++++----- CRM/Contact/Selector/Custom.php | 6 ++-- CRM/Export/BAO/Export.php | 3 +- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/CRM/Contact/Form/Search/Custom/PostalMailing.php b/CRM/Contact/Form/Search/Custom/PostalMailing.php index dc19943c87..02c9486a40 100644 --- a/CRM/Contact/Form/Search/Custom/PostalMailing.php +++ b/CRM/Contact/Form/Search/Custom/PostalMailing.php @@ -42,11 +42,26 @@ class CRM_Contact_Form_Search_Custom_PostalMailing extends CRM_Contact_Form_Sear parent::__construct($formValues); $this->_columns = array( + // The only alias you can (and should!) use in the + // columns, is contact_id. Because contact_id is used + // in a lot of places, and it seems to be important that + // it is called contact_id_a. + // + // For the other fields, if possible, you should prefix + // their names with the table alias you are selecting them + // from. This way, you can work around CRM-16587. + // + // This approach wil not work if you want to select fields + // with the same name from different tables, this will generate + // an invalid query somewhere. In that case, you can + // use column aliases in your SELECT clause and in the array + // below, but you will still hit CRM-16587 when sorting on these + // fields. ts('Contact ID') => 'contact_id', - ts('Address') => 'address', - ts('Contact Type') => 'contact_type', - ts('Name') => 'sort_name', - ts('State') => 'state_province', + ts('Address') => 'address.street_address', + ts('Contact Type') => 'contact_a.contact_type', + ts('Name') => 'contact_a.sort_name', + ts('State') => 'state_province.name', ); } @@ -95,11 +110,11 @@ class CRM_Contact_Form_Search_Custom_PostalMailing extends CRM_Contact_Form_Sear } else { $selectClause = " -DISTINCT contact_a.id as contact_id , -contact_a.contact_type as contact_type, -contact_a.sort_name as sort_name, -address.street_address as address, -state_province.name as state_province +DISTINCT contact_a.id as contact_id, +contact_a.contact_type, +contact_a.sort_name, +address.street_address, +state_province.name "; } diff --git a/CRM/Contact/Selector/Custom.php b/CRM/Contact/Selector/Custom.php index 3364a3a691..f529f76e97 100644 --- a/CRM/Contact/Selector/Custom.php +++ b/CRM/Contact/Selector/Custom.php @@ -333,8 +333,10 @@ class CRM_Contact_Selector_Custom extends CRM_Contact_Selector { // the columns we are interested in foreach ($columnNames as $property) { - $row[$property] = $dao->$property; - if (!empty($dao->$property)) { + // Get part of name after last . (if any) + $unqualified_property = CRM_Utils_Array::First(array_slice(explode('.', $property), -1)); + $row[$property] = $dao->$unqualified_property; + if (!empty($dao->$unqualified_property)) { $empty = FALSE; } } diff --git a/CRM/Export/BAO/Export.php b/CRM/Export/BAO/Export.php index 44e150f172..6169adc621 100644 --- a/CRM/Export/BAO/Export.php +++ b/CRM/Export/BAO/Export.php @@ -1330,7 +1330,8 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c $row = array(); foreach ($fields as $field) { - $row[$field] = $dao->$field; + $unqualified_field = CRM_Utils_Array::First(array_slice(explode('.', $field), -1)); + $row[$field] = $dao->$unqualified_field; } if ($alterRow) { $search->alterRow($row); -- 2.25.1