3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
37 * BAO object for civicrm_prevnext_cache table
39 class CRM_Core_BAO_PrevNextCache
extends CRM_Core_DAO_PrevNextCache
{
41 static function getPositions($cacheKey, $id1, $id2, &$mergeId = NULL, $join = NULL, $where = NULL, $flip = FALSE) {
43 list($id1, $id2) = array($id2, $id1);
46 if ($mergeId == NULL) {
49 FROM civicrm_prevnext_cache
50 WHERE cacheKey = %3 AND
53 entity_table = 'civicrm_contact'
56 $params = array(1 => array($id1, 'Integer'),
57 2 => array($id2, 'Integer'),
58 3 => array($cacheKey, 'String'),
61 $mergeId = CRM_Core_DAO
::singleValueQuery($query, $params);
64 $pos = array('foundEntry' => 0);
66 $pos['foundEntry'] = 1;
70 $where = " AND {$where}";
73 $p = array(1 => array($mergeId, 'Integer'),
74 2 => array($cacheKey, 'String'),
76 $sql = "SELECT pn.id, pn.entity_id1, pn.entity_id2, pn.data FROM civicrm_prevnext_cache pn {$join} ";
77 $wherePrev = " WHERE pn.id < %1 AND pn.cacheKey = %2 {$where} ORDER BY ID DESC LIMIT 1";
78 $sqlPrev = $sql . $wherePrev;
81 $dao = CRM_Core_DAO
::executeQuery($sqlPrev, $p);
83 $pos['prev']['id1'] = $dao->entity_id1
;
84 $pos['prev']['id2'] = $dao->entity_id2
;
85 $pos['prev']['mergeId'] = $dao->id
;
86 $pos['prev']['data'] = $dao->data
;
89 $whereNext = " WHERE pn.id > %1 AND pn.cacheKey = %2 {$where} ORDER BY ID ASC LIMIT 1";
90 $sqlNext = $sql . $whereNext;
92 $dao = CRM_Core_DAO
::executeQuery($sqlNext, $p);
94 $pos['next']['id1'] = $dao->entity_id1
;
95 $pos['next']['id2'] = $dao->entity_id2
;
96 $pos['next']['mergeId'] = $dao->id
;
97 $pos['next']['data'] = $dao->data
;
103 static function deleteItem($id = NULL, $cacheKey = NULL, $entityTable = 'civicrm_contact') {
106 $sql = "DELETE FROM civicrm_prevnext_cache WHERE entity_table = %1";
107 $params = array(1 => array($entityTable, 'String'));
109 if (is_numeric($id)) {
110 $sql .= " AND ( entity_id1 = %2 OR entity_id2 = %2 )";
111 $params[2] = array($id, 'Integer');
114 if (isset($cacheKey)) {
115 $sql .= " AND cacheKey LIKE %3";
116 $params[3] = array("{$cacheKey}%", 'String');
118 CRM_Core_DAO
::executeQuery($sql, $params);
121 static function deletePair($id1, $id2, $cacheKey = NULL, $isViceVersa = FALSE, $entityTable = 'civicrm_contact') {
122 $sql = "DELETE FROM civicrm_prevnext_cache WHERE entity_table = %1";
123 $params = array(1 => array($entityTable, 'String'));
127 "entity_id1 = %2 AND entity_id2 = %3" :
128 "(entity_id1 = %2 AND entity_id2 = %3) OR (entity_id1 = %3 AND entity_id2 = %2)";
129 $sql .= " AND ( {$pair} )";
130 $params[2] = array($id1, 'Integer');
131 $params[3] = array($id2, 'Integer');
133 if (isset($cacheKey)) {
134 $sql .= " AND cacheKey LIKE %4";
135 $params[4] = array("{$cacheKey}%", 'String');
138 CRM_Core_DAO
::executeQuery($sql, $params);
141 static function retrieve($cacheKey, $join = NULL, $where = NULL, $offset = 0, $rowCount = 0) {
144 FROM civicrm_prevnext_cache pn
148 $params = array(1 => array($cacheKey, 'String'));
151 $query .= " AND {$where}";
155 $offset = CRM_Utils_Type
::escape($offset, 'Int');
156 $rowCount = CRM_Utils_Type
::escape($rowCount, 'Int');
158 $query .= " LIMIT {$offset}, {$rowCount}";
161 $dao = CRM_Core_DAO
::executeQuery($query, $params);
164 while ($dao->fetch()) {
165 if (self
::is_serialized($dao->data
)) {
166 $main[] = unserialize($dao->data
);
169 $main[] = $dao->data
;
176 public static function is_serialized($string) {
177 return (@unserialize
($string) !== false);
180 static function setItem($values) {
181 $insert = "INSERT INTO civicrm_prevnext_cache ( entity_table, entity_id1, entity_id2, cacheKey, data ) VALUES \n";
182 $query = $insert . implode(",\n ", $values);
184 //dump the dedupe matches in the prevnext_cache table
185 CRM_Core_DAO
::executeQuery($query);
188 static function getCount($cacheKey, $join = NULL, $where = NULL, $op = "=") {
190 SELECT COUNT(*) FROM civicrm_prevnext_cache pn
192 WHERE cacheKey $op %1
195 $query .= " AND {$where}";
198 $params = array(1 => array($cacheKey, 'String'));
199 return (int) CRM_Core_DAO
::singleValueQuery($query, $params, TRUE, FALSE);
202 static function refillCache($rgid = NULL, $gid = NULL, $cacheKeyString = NULL) {
203 if (!$cacheKeyString && $rgid) {
204 $contactType = CRM_Core_DAO
::getFieldValue('CRM_Dedupe_DAO_RuleGroup', $rgid, 'contact_type');
205 $cacheKeyString = "merge {$contactType}";
206 $cacheKeyString .= $rgid ?
"_{$rgid}" : '_0';
207 $cacheKeyString .= $gid ?
"_{$gid}" : '_0';
210 if (!$cacheKeyString) {
214 // 1. Clear cache if any
215 $sql = "DELETE FROM civicrm_prevnext_cache WHERE cacheKey LIKE %1";
216 CRM_Core_DAO
::executeQuery($sql, array(1 => array("{$cacheKeyString}%", 'String')));
218 // FIXME: we need to start using temp tables / queries here instead of arrays.
219 // And cleanup code in CRM/Contact/Page/DedupeFind.php
222 $foundDupes = array();
224 $foundDupes = CRM_Dedupe_Finder
::dupesInGroup($rgid, $gid);
227 $foundDupes = CRM_Dedupe_Finder
::dupes($rgid);
230 if (!empty($foundDupes)) {
231 $cids = $displayNames = $values = array();
232 foreach ($foundDupes as $dupe) {
236 $cidString = implode(', ', array_keys($cids));
237 $sql = "SELECT id, display_name FROM civicrm_contact WHERE id IN ($cidString) ORDER BY sort_name";
238 $dao = new CRM_Core_DAO();
240 while ($dao->fetch()) {
241 $displayNames[$dao->id
] = $dao->display_name
;
244 $session = CRM_Core_Session
::singleton();
245 $userId = $session->get('userID');
247 foreach ($foundDupes as $dupes) {
250 if ($dstID == $userId) {
257 'srcName' => $displayNames[$srcID],
259 'dstName' => $displayNames[$dstID],
260 'weight' => $dupes[2],
264 $data = CRM_Core_DAO
::escapeString(serialize($row));
265 $values[] = " ( 'civicrm_contact', $srcID, $dstID, '$cacheKeyString', '$data' ) ";
267 self
::setItem($values);
271 static function cleanupCache() {
272 // clean up all prev next caches older than $cacheTimeIntervalDays days
273 $cacheTimeIntervalDays = 2;
275 // first find all the cacheKeys that match this
279 INNER JOIN civicrm_prevnext_cache pn ON c.path = pn.cacheKey
280 WHERE c.group_name = %1
281 AND c.created_date < date_sub( NOW( ), INTERVAL %2 day )
284 1 => array('CiviCRM Search PrevNextCache', 'String'),
285 2 => array($cacheTimeIntervalDays, 'Integer'),
287 CRM_Core_DAO
::executeQuery($sql, $params);
291 /* function for saving the checkbox selections
292 * $action select - select a particular contact
293 * unselect - unselect a particular contact
295 static function markSelection($cacheKey, $action = 'unselect', $cIds = NULL, $entity_table = 'civicrm_contact') {
301 $entity_whereClause = " AND entity_table = '{$entity_table}'";
302 if ($cIds && $cacheKey && $action) {
303 if (is_array($cIds)) {
304 $cIdFilter = "(" . implode(',', $cIds) . ")";
306 WHERE cacheKey LIKE %1
307 AND (entity_id1 IN {$cIdFilter} OR entity_id2 IN {$cIdFilter})
312 WHERE cacheKey LIKE %1
313 AND (entity_id1 = %2 OR entity_id2 = %2)
315 $params[2] = array("{$cIds}", 'Integer');
317 if ($action == 'select') {
318 $whereClause .= "AND is_selected = 0";
319 $sql = "UPDATE civicrm_prevnext_cache SET is_selected = 1 {$whereClause} {$entity_whereClause}";
320 $params[1] = array("{$cacheKey}%", 'String');
322 elseif ($action == 'unselect') {
323 $whereClause .= "AND is_selected = 1";
324 $sql = "UPDATE civicrm_prevnext_cache SET is_selected = 0 {$whereClause} {$entity_whereClause}";
325 $params[1] = array("%{$cacheKey}%", 'String');
327 // default action is reseting
329 elseif (!$cIds && $cacheKey && $action == 'unselect') {
331 UPDATE civicrm_prevnext_cache
333 WHERE cacheKey LIKE %1 AND is_selected = 1
334 {$entity_whereClause}
336 $params[1] = array("{$cacheKey}%", 'String');
338 CRM_Core_DAO
::executeQuery($sql, $params);
342 * function to get the selections
344 * @param string $cacheKey cache key
345 * @param string $action action
346 * $action : get - get only selection records
347 * getall - get all the records of the specified cache key
348 * @param string $entity_table entity table
350 static function getSelection($cacheKey, $action = 'get', $entity_table = 'civicrm_contact') {
356 $entity_whereClause = " AND entity_table = '{$entity_table}'";
357 if ($cacheKey && ($action == 'get' ||
$action == 'getall')) {
358 $actionGet = ($action == "get") ?
" AND is_selected = 1 " : "";
360 SELECT entity_id1, entity_id2 FROM civicrm_prevnext_cache
361 WHERE cacheKey LIKE %1
366 $params[1] = array("{$cacheKey}%", 'String');
368 $contactIds = array($cacheKey => array());
369 $cIdDao = CRM_Core_DAO
::executeQuery($sql, $params);
370 while ($cIdDao->fetch()) {
371 if ($cIdDao->entity_id1
== $cIdDao->entity_id2
) {
372 $contactIds[$cacheKey][$cIdDao->entity_id1
] = 1;
379 static function getSelectedContacts() {
380 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String');
381 $cacheKey = "civicrm search {$qfKey}";
384 FROM civicrm_prevnext_cache
385 WHERE cacheKey LIKE %1
387 AND cacheKey NOT LIKE %2
389 $params1[1] = array("{$cacheKey}%", 'String');
390 $params1[2] = array("{$cacheKey}_alphabet%", 'String');
391 $dao = CRM_Core_DAO
::executeQuery($query, $params1);
394 while ($dao->fetch()) {
400 static function buildSelectedContactPager( &$obj, &$params) {
401 $params['status'] = ts('Contacts %%StatusMessage%%');
402 $params['csvString'] = NULL;
403 $params['buttonTop'] = 'PagerTopButton';
404 $params['buttonBottom'] = 'PagerBottomButton';
405 $params['rowCount'] = $obj->get(CRM_Utils_Pager
::PAGE_ROWCOUNT
);
407 if (!$params['rowCount']) {
408 $params['rowCount'] = CRM_Utils_Pager
::ROWCOUNT
;
411 $qfKey = CRM_Utils_Request
::retrieve('qfKey','String', $this);
412 $cacheKey = "civicrm search {$qfKey}";
416 FROM civicrm_prevnext_cache
417 WHERE cacheKey LIKE %1
419 AND cacheKey NOT LIKE %2
421 $params1[1] = array("{$cacheKey}%", 'String');
422 $params1[2] = array("{$cacheKey}_alphabet%", 'String');
423 $paramsTotal = CRM_Core_DAO
::singleValueQuery($query, $params1);
424 $params['total'] = $paramsTotal;
425 $obj->_pager
= new CRM_Utils_Pager($params);
426 $obj->assign_by_ref('pager', $obj->_pager
);
427 list($offset, $rowCount) = $obj->_pager
->getOffsetAndRowCount();
428 $params['offset'] = $offset;
429 $params['rowCount1'] = $rowCount;