From: monishdeb Date: Mon, 26 May 2014 10:35:39 +0000 (+0530) Subject: CRM-14107 fix - Implement Previous / Next navigation on contact summary pages when... X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=7c34ab115a261e5d6b0534118e8e34761eda06d4;p=civicrm-core.git CRM-14107 fix - Implement Previous / Next navigation on contact summary pages when accessed from a custom search https://issues.civicrm.org/jira/browse/CRM-14107 --- diff --git a/CRM/Contact/Form/Search/Custom/ActivitySearch.php b/CRM/Contact/Form/Search/Custom/ActivitySearch.php index aa46e53207..cefe9d2d41 100644 --- a/CRM/Contact/Form/Search/Custom/ActivitySearch.php +++ b/CRM/Contact/Form/Search/Custom/ActivitySearch.php @@ -196,7 +196,7 @@ class CRM_Contact_Form_Search_Custom_ActivitySearch implements CRM_Contact_Form_ $groupTree = CRM_Core_BAO_CustomGroup::getTree('Activity', $form, NULL, NULL, '', NULL); foreach ($groupTree as $key) { - if ($key['extends'] == 'Activity') { + if (!empty($key['extends']) && $key['extends'] == 'Activity') { $select .= ", " . $key['table_name'] . ".*"; $from .= " LEFT JOIN " . $key['table_name'] . " ON " . $key['table_name'] . ".entity_id = activity.id"; } @@ -221,6 +221,9 @@ class CRM_Contact_Form_Search_Custom_ActivitySearch implements CRM_Contact_Form_ $sql .= 'ORDER BY contact_a.sort_name, activity.activity_date_time DESC, activity.activity_type_id, activity.status_id, activity.subject'; } } + else { + $sql .= ' ORDER BY contact_a.sort_name'; + } if ($rowcount > 0 && $offset >= 0) { $offset = CRM_Utils_Type::escape($offset, 'Int'); diff --git a/CRM/Contact/Form/Search/Custom/ContribSYBNT.php b/CRM/Contact/Form/Search/Custom/ContribSYBNT.php index 5eee5ea65b..6b5a196b4c 100644 --- a/CRM/Contact/Form/Search/Custom/ContribSYBNT.php +++ b/CRM/Contact/Form/Search/Custom/ContribSYBNT.php @@ -163,7 +163,8 @@ class CRM_Contact_Form_Search_Custom_ContribSYBNT implements CRM_Contact_Form_Se $from = $this->from(); if ($justIDs) { - $select = 'contact_a.id as contact_id'; + $select = $this->select(); + $select .= ', contact.id as contact_id, sort_name'; } else { $select = $this->select(); @@ -188,7 +189,14 @@ GROUP BY contact.id ORDER BY donation_amount desc "; - // CRM_Core_Error::debug('sql',$sql); exit(); + + if ($justIDs) { + CRM_Core_DAO::executeQuery("DROP TEMPORARY TABLE IF EXISTS CustomSearch_SYBNT_temp"); + $query = "CREATE TEMPORARY TABLE CustomSearch_SYBNT_temp AS ({$sql})"; + $dao = CRM_Core_DAO::executeQuery($query); + $sql = "SELECT contact_id FROM CustomSearch_SYBNT_temp"; + } + return $sql; } @@ -196,7 +204,7 @@ ORDER BY donation_amount desc * @return string */ function select() { - if ($this->start_date_2 || $this->end_date_2) { + if (!empty($this->start_date_2) || !empty($this->end_date_2)) { return " sum(contrib_1.total_amount) + sum(contrib_2.total_amount) AS donation_amount, count(contrib_1.id) + count(contrib_1.id) AS donation_count @@ -215,13 +223,13 @@ count(contrib_1.id) AS donation_count */ function from() { $from = NULL; - if ($this->start_date_2 || $this->end_date_2) { + if (!empty($this->start_date_2) || !empty($this->end_date_2)) { $from .= " LEFT JOIN civicrm_contribution contrib_2 ON contrib_2.contact_id = contact.id "; } - if ($this->exclude_start_date || - $this->exclude_end_date || - $this->is_first_amount + if (!empty($this->exclude_start_date) || + !empty($this->exclude_end_date) || + !empty($this->is_first_amount) ) { $from .= " LEFT JOIN XG_CustomSearch_SYBNT xg ON xg.contact_id = contact.id "; } @@ -237,31 +245,29 @@ count(contrib_1.id) AS donation_count function where($includeContactIDs = FALSE) { $clauses = array(); - if ($this->start_date_1) { + if (!empty($this->start_date_1)) { $clauses[] = "contrib_1.receive_date >= {$this->start_date_1}"; } - if ($this->end_date_1) { + if (!empty($this->end_date_1)) { $clauses[] = "contrib_1.receive_date <= {$this->end_date_1}"; } - if ($this->start_date_2 || - $this->end_date_2 - ) { + if (!empty($this->start_date_2) || !empty($this->end_date_2)) { $clauses[] = "contrib_2.is_test = 0"; - if ($this->start_date_2) { + if (!empty($this->start_date_2)) { $clauses[] = "contrib_2.receive_date >= {$this->start_date_2}"; } - if ($this->end_date_2) { + if (!empty($this->end_date_2)) { $clauses[] = "contrib_2.receive_date <= {$this->end_date_2}"; } } - if ($this->exclude_start_date || - $this->exclude_end_date || - $this->is_first_amount + if (!empty($this->exclude_start_date) || + !empty($this->exclude_end_date) || + !empty($this->is_first_amount) ) { // first create temp table to store contact ids diff --git a/CRM/Contact/Form/Search/Custom/Group.php b/CRM/Contact/Form/Search/Custom/Group.php index 2343c2d897..f4f4aed558 100644 --- a/CRM/Contact/Form/Search/Custom/Group.php +++ b/CRM/Contact/Form/Search/Custom/Group.php @@ -259,6 +259,9 @@ class CRM_Contact_Form_Search_Custom_Group $sql .= " ORDER BY contact_id ASC"; } } + else { + $sql .= " ORDER BY contact_a.id ASC"; + } if ($offset >= 0 && $rowcount > 0) { $sql .= " LIMIT $offset, $rowcount "; diff --git a/CRM/Contact/Form/Search/Custom/PostalMailing.php b/CRM/Contact/Form/Search/Custom/PostalMailing.php index 3318185728..3bc66a380c 100644 --- a/CRM/Contact/Form/Search/Custom/PostalMailing.php +++ b/CRM/Contact/Form/Search/Custom/PostalMailing.php @@ -62,6 +62,10 @@ class CRM_Contact_Form_Search_Custom_PostalMailing extends CRM_Contact_Form_Sear $form->assign('elements', array('group_id')); } + function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = FALSE) { + return $this->all($offset, $rowcount, $sort, FALSE, TRUE); + } + /** * @param int $offset * @param int $rowcount @@ -76,6 +80,7 @@ class CRM_Contact_Form_Search_Custom_PostalMailing extends CRM_Contact_Form_Sear ) { if ($justIDs) { $selectClause = "contact_a.id as contact_id"; + $sort = 'contact_a.id'; } else { $selectClause = " diff --git a/CRM/Contact/Form/Search/Custom/Sample.php b/CRM/Contact/Form/Search/Custom/Sample.php index bb8f6af6fc..f811a58f81 100644 --- a/CRM/Contact/Form/Search/Custom/Sample.php +++ b/CRM/Contact/Form/Search/Custom/Sample.php @@ -93,6 +93,10 @@ class CRM_Contact_Form_Search_Custom_Sample extends CRM_Contact_Form_Search_Cust return $summary; } + function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = FALSE) { + return $this->all($offset, $rowcount, $sort, FALSE, TRUE); + } + /** * @param int $offset * @param int $rowcount @@ -107,6 +111,7 @@ class CRM_Contact_Form_Search_Custom_Sample extends CRM_Contact_Form_Search_Cust ) { if ($justIDs) { $selectClause = "contact_a.id as contact_id"; + $sort = 'contact_a.id'; } else { $selectClause = " diff --git a/CRM/Contact/Form/Search/Custom/TagContributions.php b/CRM/Contact/Form/Search/Custom/TagContributions.php index 297867a5ee..ac6f6bdfc3 100644 --- a/CRM/Contact/Form/Search/Custom/TagContributions.php +++ b/CRM/Contact/Form/Search/Custom/TagContributions.php @@ -122,23 +122,21 @@ SELECT $select FROM $from WHERE $where "; - //for only contact ids ignore order and group by. - if (!$onlyIDs) { - $sql .= " GROUP BY contact_a.id"; - // Define ORDER BY for query in $sort, with default value - if (!empty($sort)) { - if (is_string($sort)) { - $sort = CRM_Utils_Type::escape($sort, 'String'); - $sql .= " ORDER BY $sort "; - } - else { - $sql .= " ORDER BY " . trim($sort->orderBy()); - } + + $sql .= " GROUP BY contact_a.id"; + // Define ORDER BY for query in $sort, with default value + if (!empty($sort)) { + if (is_string($sort)) { + $sort = CRM_Utils_Type::escape($sort, 'String'); + $sql .= " ORDER BY $sort "; } else { - $sql .= ""; + $sql .= " ORDER BY " . trim($sort->orderBy()); } } + else { + $sql .= ""; + } return $sql; } diff --git a/CRM/Contact/Form/Search/Custom/ZipCodeRange.php b/CRM/Contact/Form/Search/Custom/ZipCodeRange.php index 5f61345b34..788dbaa474 100644 --- a/CRM/Contact/Form/Search/Custom/ZipCodeRange.php +++ b/CRM/Contact/Form/Search/Custom/ZipCodeRange.php @@ -83,6 +83,10 @@ class CRM_Contact_Form_Search_Custom_ZipCodeRange extends CRM_Contact_Form_Searc return $summary; } + function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = FALSE) { + return $this->all($offset, $rowcount, $sort, FALSE, TRUE); + } + /** * @param int $offset * @param int $rowcount @@ -97,6 +101,7 @@ class CRM_Contact_Form_Search_Custom_ZipCodeRange extends CRM_Contact_Form_Searc ) { if ($justIDs) { $selectClause = "contact_a.id as contact_id"; + $sort = "contact_a.id"; } else { $selectClause = " diff --git a/CRM/Contact/Selector.php b/CRM/Contact/Selector.php index 7cda6262c2..51b5a87710 100644 --- a/CRM/Contact/Selector.php +++ b/CRM/Contact/Selector.php @@ -977,6 +977,13 @@ class CRM_Contact_Selector extends CRM_Core_Selector_Base implements CRM_Core_Se if (is_a($this, 'CRM_Contact_Selector_Custom')) { $sql = $this->_search->contactIDs($start, $end, $sort, TRUE); $replaceSQL = "SELECT contact_a.id as contact_id"; + + if (is_a($this->_search, 'CRM_Contact_Form_Search_Custom_FullText') || + is_a($this->_search, 'CRM_Contact_Form_Search_Custom_ContribSYBNT') + ) { + $replaceSQL = "SELECT contact_id"; + } + $coreSearch = FALSE; } // For core searches use the searchQuery method @@ -994,12 +1001,21 @@ class CRM_Contact_Selector extends CRM_Core_Selector_Base implements CRM_Core_Se // the prev next cache in this situation // the other alternative of running the FULL query will just be incredibly inefficient // and slow things down way too much on large data sets / complex queries - $insertSQL = " INSERT INTO civicrm_prevnext_cache ( entity_table, entity_id1, entity_id2, cacheKey, data ) SELECT 'civicrm_contact', contact_a.id, contact_a.id, '$cacheKey', contact_a.display_name "; + if (property_exists($this, '_search') && + (is_a($this->_search, 'CRM_Contact_Form_Search_Custom_FullText') || + is_a($this->_search, 'CRM_Contact_Form_Search_Custom_ContribSYBNT')) + ) { + $insertSQL = " +INSERT INTO civicrm_prevnext_cache ( entity_table, entity_id1, entity_id2, cacheKey, data ) +SELECT 'civicrm_contact', contact_id, contact_id, '$cacheKey', sort_name +"; + } + $sql = str_replace($replaceSQL, $insertSQL, $sql); $errorScope = CRM_Core_TemporaryErrorScope::ignoreException(); diff --git a/CRM/Contact/Selector/Custom.php b/CRM/Contact/Selector/Custom.php index d5fc147ba1..95a773c0ba 100644 --- a/CRM/Contact/Selector/Custom.php +++ b/CRM/Contact/Selector/Custom.php @@ -171,12 +171,15 @@ class CRM_Contact_Selector_Custom extends CRM_Contact_Selector { * */ static function &links() { + list($key) = func_get_args(); + $extraParams = ($key) ? "&key={$key}" : NULL; + if (!(self::$_links)) { self::$_links = array( CRM_Core_Action::VIEW => array( 'name' => ts('View'), 'url' => 'civicrm/contact/view', - 'qs' => 'reset=1&cid=%%id%%', + 'qs' => "reset=1&cid=%%id%%{$extraParams}", 'title' => ts('View Contact Details'), ), CRM_Core_Action::UPDATE => array( @@ -299,7 +302,7 @@ class CRM_Contact_Selector_Custom extends CRM_Contact_Selector { $columns = $this->_search->columns(); $columnNames = array_values($columns); - $links = self::links(); + $links = self::links($this->_key); $permissions = array(CRM_Core_Permission::getPermission()); if (CRM_Core_Permission::check('delete contacts')) { @@ -428,7 +431,7 @@ class CRM_Contact_Selector_Custom extends CRM_Contact_Selector { * @param $rows */ function addActions(&$rows) { - $links = self::links(); + $links = self::links($this->_key); $permissions = array(CRM_Core_Permission::getPermission()); if (CRM_Core_Permission::check('delete contacts')) {