X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FContact%2FBAO%2FQuery.php;h=7cc40b691b3cc73b0fc5f3f3807f3b00063610d0;hb=4243847f916f125a8b2e4671a7a7eae762546577;hp=756e672c263639ab42b7ea76a999f58cf5147cdf;hpb=e166ff79ba67538eca2ef6d32bafb7e078850299;p=civicrm-core.git diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index 756e672c26..7cc40b691b 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -4066,13 +4066,6 @@ civicrm_relationship.start_date > {$today} } $this->generatePermissionClause($onlyDeleted, $count); - list($select, $from, $where, $having) = $this->query($count, $sortByChar, $groupContacts); - - //additional from clause should be w/ proper joins. - if ($additionalFromClause) { - $from .= "\n" . $additionalFromClause; - } - if (empty($where)) { $where = "WHERE $this->_permissionWhereClause"; } @@ -4139,7 +4132,6 @@ civicrm_relationship.start_date > {$today} } } - $doOpt = TRUE; // hack for order clause if ($order) { $fieldStr = trim(str_replace('ORDER BY', '', $order)); @@ -4170,72 +4162,22 @@ civicrm_relationship.start_date > {$today} $this->_whereTables["civicrm_email"] = 1; $order = str_replace($field, "civicrm_email.{$field}", $order); break; - - default: - $doOpt = FALSE; } + $this->_fromClause = self::fromClause($this->_tables, NULL, NULL, $this->_primaryLocation, $this->_mode); + $this->_simpleFromClause = self::fromClause($this->_whereTables, NULL, NULL, $this->_primaryLocation, $this->_mode); } } - if ($rowCount > 0 && $offset >= 0) { $limit = " LIMIT $offset, $rowCount "; + } + } - // ok here is a first hack at an optimization, lets get all the contact ids - // that are restricted and we'll then do the final clause with it - // CRM-5954 - if (isset($this->_distinctComponentClause)) { - if (strpos($this->_distinctComponentClause, 'DISTINCT') == FALSE) { - $limitSelect = "SELECT DISTINCT {$this->_distinctComponentClause}"; - } - else { - $limitSelect = "SELECT {$this->_distinctComponentClause}"; - } - } - else { - $limitSelect = 'SELECT DISTINCT contact_a.id as id'; - } - - if ($doOpt) { - $this->_simpleFromClause = self::fromClause($this->_whereTables, NULL, NULL, - $this->_primaryLocation, $this->_mode - ); - - if ($additionalFromClause) { - $this->_simpleFromClause .= "\n" . $additionalFromClause; - } - // if we are doing a transform, do it here - // CRM-7969 - $having = NULL; - if ($this->_displayRelationshipType) { - $this->filterRelatedContacts($this->_simpleFromClause, $where, $having); - } + list($select, $from, $where, $having) = $this->query($count, $sortByChar, $groupContacts); - $limitQuery = "$limitSelect {$this->_simpleFromClause} $where $groupBy $order $limit"; - $limitDAO = CRM_Core_DAO::executeQuery($limitQuery); - $limitIDs = array(); - while ($limitDAO->fetch()) { - if ($limitDAO->id) { - $limitIDs[] = $limitDAO->id; - } - } - if (empty($limitIDs)) { - $limitClause = ' AND ( 0 ) '; - } - else { - if (isset($this->_distinctComponentClause)) { - $limitClause = " AND {$this->_distinctComponentClause} IN ( "; - } - else { - $limitClause = ' AND contact_a.id IN ( '; - } - $limitClause .= implode(',', $limitIDs) . ' ) '; - } - $where .= $limitClause; - // reset limit clause since we already restrict what records we want - $limit = NULL; - } - } + //additional from clause should be w/ proper joins. + if ($additionalFromClause) { + $from .= "\n" . $additionalFromClause; } // if we are doing a transform, do it here @@ -4259,8 +4201,6 @@ civicrm_relationship.start_date > {$today} return CRM_Core_DAO::singleValueQuery($query); } - // crm_core_error::debug('$query', $query); exit; - $dao = CRM_Core_DAO::executeQuery($query); if ($groupContacts) { $ids = array(); @@ -4273,6 +4213,25 @@ civicrm_relationship.start_date > {$today} return $dao; } + /** + * Fetch a list of contacts from the prev/next cache for displaying a search results page + * + * @param string $cacheKey + * @param int $offset + * @param int $rowCount + * @param bool $includeContactIds + * @return CRM_Core_DAO + */ + function getCachedContacts($cacheKey, $offset, $rowCount, $includeContactIds) { + $this->_includeContactIds = $includeContactIds; + list($select, $from, ) = $this->query(); + $from = " FROM civicrm_prevnext_cache pnc INNER JOIN civicrm_contact contact_a ON contact_a.id = pnc.entity_id1 AND pnc.cacheKey = '$cacheKey' " . substr($from, 31); + $order = " ORDER BY pnc.id"; + $limit = " LIMIT $offset, $rowCount"; + $query = "$select $from $order $limit"; + return CRM_Core_DAO::executeQuery($query); + } + /** * Populate $this->_permissionWhereClause with permission related clause and update other * query related properties.