Merge pull request #8347 from eileenmcnaughton/tidy-up
[civicrm-core.git] / CRM / Core / BAO / PrevNextCache.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2016
32 * $Id$
33 *
34 */
35
36 /**
37 * BAO object for civicrm_prevnext_cache table
38 */
39 class CRM_Core_BAO_PrevNextCache extends CRM_Core_DAO_PrevNextCache {
40
41 /**
42 * @param $cacheKey
43 * @param $id1
44 * @param $id2
45 * @param int $mergeId
46 * @param NULL $join
47 * @param NULL $where
48 * @param bool $flip
49 *
50 * @return array
51 */
52 public static function getPositions($cacheKey, $id1, $id2, &$mergeId = NULL, $join = NULL, $where = NULL, $flip = FALSE) {
53 if ($flip) {
54 list($id1, $id2) = array($id2, $id1);
55 }
56
57 if ($mergeId == NULL) {
58 $query = "
59 SELECT id
60 FROM civicrm_prevnext_cache
61 WHERE cacheKey = %3 AND
62 entity_id1 = %1 AND
63 entity_id2 = %2 AND
64 entity_table = 'civicrm_contact'
65 ";
66
67 $params = array(
68 1 => array($id1, 'Integer'),
69 2 => array($id2, 'Integer'),
70 3 => array($cacheKey, 'String'),
71 );
72
73 $mergeId = CRM_Core_DAO::singleValueQuery($query, $params);
74 }
75
76 $pos = array('foundEntry' => 0);
77 if ($mergeId) {
78 $pos['foundEntry'] = 1;
79
80 if ($where) {
81
82 $where = " AND {$where}";
83
84 }
85 $p = array(
86 1 => array($mergeId, 'Integer'),
87 2 => array($cacheKey, 'String'),
88 );
89 $sql = "SELECT pn.id, pn.entity_id1, pn.entity_id2, pn.data FROM civicrm_prevnext_cache pn {$join} ";
90 $wherePrev = " WHERE pn.id < %1 AND pn.cacheKey = %2 {$where} ORDER BY ID DESC LIMIT 1";
91 $sqlPrev = $sql . $wherePrev;
92
93 $dao = CRM_Core_DAO::executeQuery($sqlPrev, $p);
94 if ($dao->fetch()) {
95 $pos['prev']['id1'] = $dao->entity_id1;
96 $pos['prev']['id2'] = $dao->entity_id2;
97 $pos['prev']['mergeId'] = $dao->id;
98 $pos['prev']['data'] = $dao->data;
99 }
100
101 $whereNext = " WHERE pn.id > %1 AND pn.cacheKey = %2 {$where} ORDER BY ID ASC LIMIT 1";
102 $sqlNext = $sql . $whereNext;
103
104 $dao = CRM_Core_DAO::executeQuery($sqlNext, $p);
105 if ($dao->fetch()) {
106 $pos['next']['id1'] = $dao->entity_id1;
107 $pos['next']['id2'] = $dao->entity_id2;
108 $pos['next']['mergeId'] = $dao->id;
109 $pos['next']['data'] = $dao->data;
110 }
111 }
112 return $pos;
113 }
114
115 /**
116 * @param int $id
117 * @param NULL $cacheKey
118 * @param string $entityTable
119 */
120 public static function deleteItem($id = NULL, $cacheKey = NULL, $entityTable = 'civicrm_contact') {
121
122 //clear cache
123 $sql = "DELETE FROM civicrm_prevnext_cache WHERE entity_table = %1";
124 $params = array(1 => array($entityTable, 'String'));
125
126 if (is_numeric($id)) {
127 $sql .= " AND ( entity_id1 = %2 OR entity_id2 = %2 )";
128 $params[2] = array($id, 'Integer');
129 }
130
131 if (isset($cacheKey)) {
132 $sql .= " AND cacheKey LIKE %3";
133 $params[3] = array("{$cacheKey}%", 'String');
134 }
135 CRM_Core_DAO::executeQuery($sql, $params);
136 }
137
138 /**
139 * @param $id1
140 * @param $id2
141 * @param NULL $cacheKey
142 * @param bool $isViceVersa
143 * @param string $entityTable
144 */
145 public static function deletePair($id1, $id2, $cacheKey = NULL, $isViceVersa = FALSE, $entityTable = 'civicrm_contact') {
146 $sql = "DELETE FROM civicrm_prevnext_cache WHERE entity_table = %1";
147 $params = array(1 => array($entityTable, 'String'));
148
149 $pair = !$isViceVersa ? "entity_id1 = %2 AND entity_id2 = %3" : "(entity_id1 = %2 AND entity_id2 = %3) OR (entity_id1 = %3 AND entity_id2 = %2)";
150 $sql .= " AND ( {$pair} )";
151 $params[2] = array($id1, 'Integer');
152 $params[3] = array($id2, 'Integer');
153
154 if (isset($cacheKey)) {
155 $sql .= " AND cacheKey LIKE %4";
156 $params[4] = array("{$cacheKey}%", 'String'); // used % to address any row with conflict-cacheKey e.g "merge Individual_8_0_conflicts"
157 }
158
159 CRM_Core_DAO::executeQuery($sql, $params);
160 }
161
162 public static function markConflict($id1, $id2, $cacheKey, $conflicts) {
163 if (empty($cacheKey) || empty($conflicts)) {
164 return FALSE;
165 }
166
167 $sql = "SELECT pn.*
168 FROM civicrm_prevnext_cache pn
169 WHERE
170 ((pn.entity_id1 = %1 AND pn.entity_id2 = %2) OR (pn.entity_id1 = %2 AND pn.entity_id2 = %1)) AND
171 (cacheKey = %3 OR cacheKey = %4)";
172 $params = array(
173 1 => array($id1, 'Integer'),
174 2 => array($id2, 'Integer'),
175 3 => array("{$cacheKey}", 'String'),
176 4 => array("{$cacheKey}_conflicts", 'String'),
177 );
178 $pncFind = CRM_Core_DAO::executeQuery($sql, $params);
179
180 while ($pncFind->fetch()) {
181 $data = $pncFind->data;
182 if (!empty($data)) {
183 $data = unserialize($data);
184 $data['conflicts'] = implode(",", array_values($conflicts));
185
186 $pncUp = new CRM_Core_DAO_PrevNextCache();
187 $pncUp->id = $pncFind->id;
188 if ($pncUp->find(TRUE)) {
189 $pncUp->data = serialize($data);
190 $pncUp->cacheKey = "{$cacheKey}_conflicts";
191 $pncUp->save();
192 }
193 }
194 }
195 return TRUE;
196 }
197
198 /**
199 * Retrieve from prev-next cache.
200 *
201 * This function is used from a variety of merge related functions, although
202 * it would probably be good to converge on calling CRM_Dedupe_Merger::getDuplicatePairs.
203 *
204 * We seem to currently be storing stats in this table too & they might make more sense in
205 * the main cache table.
206 *
207 * @param string $cacheKey
208 * @param string $join
209 * @param string $whereClause
210 * @param int $offset
211 * @param int $rowCount
212 * @param array $select
213 * @param string $orderByClause
214 * @param bool $includeConflicts
215 * Should we return rows that have already been idenfified as having a conflict.
216 * When this is TRUE you should be careful you do not set up a loop.
217 *
218 * @return array
219 */
220 public static function retrieve($cacheKey, $join = NULL, $whereClause = NULL, $offset = 0, $rowCount = 0, $select = array(), $orderByClause = '', $includeConflicts = TRUE) {
221 $selectString = 'pn.*';
222
223 if (!empty($select)) {
224 $aliasArray = array();
225 foreach ($select as $column => $alias) {
226 $aliasArray[] = $column . ' as ' . $alias;
227 }
228 $selectString .= " , " . implode(' , ', $aliasArray);
229 }
230
231 $params = array(
232 1 => array($cacheKey, 'String'),
233 );
234
235 if (!empty($whereClause)) {
236 $whereClause = " AND " . $whereClause;
237 }
238 if ($includeConflicts) {
239 $where = ' WHERE (pn.cacheKey = %1 OR pn.cacheKey = %2)' . $whereClause;
240 $params[2] = array("{$cacheKey}_conflicts", 'String');
241 }
242 else {
243 $where = ' WHERE (pn.cacheKey = %1)' . $whereClause;
244 }
245
246 $query = "
247 SELECT SQL_CALC_FOUND_ROWS {$selectString}
248 FROM civicrm_prevnext_cache pn
249 {$join}
250 $where
251 $orderByClause
252 ";
253
254 if ($rowCount) {
255 $offset = CRM_Utils_Type::escape($offset, 'Int');
256 $rowCount = CRM_Utils_Type::escape($rowCount, 'Int');
257
258 $query .= " LIMIT {$offset}, {$rowCount}";
259 }
260
261 $dao = CRM_Core_DAO::executeQuery($query, $params);
262
263 $main = array();
264 $count = 0;
265 while ($dao->fetch()) {
266 if (self::is_serialized($dao->data)) {
267 $main[$count] = unserialize($dao->data);
268 }
269 else {
270 $main[$count] = $dao->data;
271 }
272
273 if (!empty($select)) {
274 $extraData = array();
275 foreach ($select as $dfield => $sfield) {
276 $extraData[$sfield] = $dao->$sfield;
277 }
278 $main[$count] = array(
279 'prevnext_id' => $dao->id,
280 'is_selected' => $dao->is_selected,
281 'entity_id1' => $dao->entity_id1,
282 'entity_id2' => $dao->entity_id2,
283 'data' => $main[$count],
284 );
285 $main[$count] = array_merge($main[$count], $extraData);
286 }
287 $count++;
288 }
289
290 return $main;
291 }
292
293 /**
294 * @param $string
295 *
296 * @return bool
297 */
298 public static function is_serialized($string) {
299 return (@unserialize($string) !== FALSE);
300 }
301
302 /**
303 * @param $values
304 */
305 public static function setItem($values) {
306 $insert = "INSERT INTO civicrm_prevnext_cache ( entity_table, entity_id1, entity_id2, cacheKey, data ) VALUES \n";
307 $query = $insert . implode(",\n ", $values);
308
309 //dump the dedupe matches in the prevnext_cache table
310 CRM_Core_DAO::executeQuery($query);
311 }
312
313 /**
314 * @param $cacheKey
315 * @param NULL $join
316 * @param NULL $where
317 * @param string $op
318 *
319 * @return int
320 */
321 public static function getCount($cacheKey, $join = NULL, $where = NULL, $op = "=") {
322 $query = "
323 SELECT COUNT(*) FROM civicrm_prevnext_cache pn
324 {$join}
325 WHERE (pn.cacheKey $op %1 OR pn.cacheKey $op %2)
326 ";
327 if ($where) {
328 $query .= " AND {$where}";
329 }
330
331 $params = array(
332 1 => array($cacheKey, 'String'),
333 2 => array("{$cacheKey}_conflicts", 'String'),
334 );
335 return (int) CRM_Core_DAO::singleValueQuery($query, $params, TRUE, FALSE);
336 }
337
338 /**
339 * Repopulate the cache of merge prospects.
340 *
341 * @param int $rgid
342 * @param int $gid
343 * @param NULL $cacheKeyString
344 * @param array $criteria
345 * Additional criteria to filter by.
346 *
347 * @return bool
348 */
349 public static function refillCache($rgid = NULL, $gid = NULL, $cacheKeyString = NULL, $criteria = array()) {
350 if (!$cacheKeyString && $rgid) {
351 $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid, $criteria);
352 }
353
354 if (!$cacheKeyString) {
355 return FALSE;
356 }
357
358 // 1. Clear cache if any
359 $sql = "DELETE FROM civicrm_prevnext_cache WHERE cacheKey LIKE %1";
360 CRM_Core_DAO::executeQuery($sql, array(1 => array("{$cacheKeyString}%", 'String')));
361
362 // FIXME: we need to start using temp tables / queries here instead of arrays.
363 // And cleanup code in CRM/Contact/Page/DedupeFind.php
364
365 // 2. FILL cache
366 $foundDupes = array();
367 if ($rgid && $gid) {
368 $foundDupes = CRM_Dedupe_Finder::dupesInGroup($rgid, $gid);
369 }
370 elseif ($rgid) {
371 $contactIDs = array();
372 if (!empty($criteria)) {
373 $contacts = civicrm_api3('Contact', 'get', array_merge(array('options' => array('limit' => 0), 'return' => 'id'), $criteria['contact']));
374 $contactIDs = array_keys($contacts['values']);
375 }
376 $foundDupes = CRM_Dedupe_Finder::dupes($rgid, $contactIDs);
377 }
378
379 if (!empty($foundDupes)) {
380 $cids = $displayNames = $values = array();
381 foreach ($foundDupes as $dupe) {
382 $cids[$dupe[0]] = 1;
383 $cids[$dupe[1]] = 1;
384 }
385 $cidString = implode(', ', array_keys($cids));
386 $sql = "SELECT id, display_name FROM civicrm_contact WHERE id IN ($cidString) ORDER BY sort_name";
387 $dao = new CRM_Core_DAO();
388 $dao->query($sql);
389 while ($dao->fetch()) {
390 $displayNames[$dao->id] = $dao->display_name;
391 }
392
393 $session = CRM_Core_Session::singleton();
394 $userId = $session->get('userID');
395
396 foreach ($foundDupes as $dupes) {
397 $srcID = $dupes[0];
398 $dstID = $dupes[1];
399 if ($dstID == $userId) {
400 $srcID = $dupes[1];
401 $dstID = $dupes[0];
402 }
403
404 $row = array(
405 'srcID' => $srcID,
406 'srcName' => $displayNames[$srcID],
407 'dstID' => $dstID,
408 'dstName' => $displayNames[$dstID],
409 'weight' => $dupes[2],
410 'canMerge' => TRUE,
411 );
412
413 $data = CRM_Core_DAO::escapeString(serialize($row));
414 $values[] = " ( 'civicrm_contact', $srcID, $dstID, '$cacheKeyString', '$data' ) ";
415 }
416 self::setItem($values);
417 }
418 }
419
420 public static function cleanupCache() {
421 // clean up all prev next caches older than $cacheTimeIntervalDays days
422 $cacheTimeIntervalDays = 2;
423
424 // first find all the cacheKeys that match this
425 $sql = "
426 DELETE pn, c
427 FROM civicrm_cache c
428 INNER JOIN civicrm_prevnext_cache pn ON c.path = pn.cacheKey
429 WHERE c.group_name = %1
430 AND c.created_date < date_sub( NOW( ), INTERVAL %2 day )
431 ";
432 $params = array(
433 1 => array('CiviCRM Search PrevNextCache', 'String'),
434 2 => array($cacheTimeIntervalDays, 'Integer'),
435 );
436 CRM_Core_DAO::executeQuery($sql, $params);
437 }
438
439 /**
440 * Save checkbox selections.
441 *
442 * @param $cacheKey
443 * @param string $action
444 * @param array $cIds
445 * @param string $entity_table
446 */
447 public static function markSelection($cacheKey, $action = 'unselect', $cIds = NULL, $entity_table = 'civicrm_contact') {
448 if (!$cacheKey) {
449 return;
450 }
451 $params = array();
452
453 $entity_whereClause = " AND entity_table = '{$entity_table}'";
454 if ($cIds && $cacheKey && $action) {
455 if (is_array($cIds)) {
456 $cIdFilter = "(" . implode(',', $cIds) . ")";
457 $whereClause = "
458 WHERE cacheKey LIKE %1
459 AND (entity_id1 IN {$cIdFilter} OR entity_id2 IN {$cIdFilter})
460 ";
461 }
462 else {
463 $whereClause = "
464 WHERE cacheKey LIKE %1
465 AND (entity_id1 = %2 OR entity_id2 = %2)
466 ";
467 $params[2] = array("{$cIds}", 'Integer');
468 }
469 if ($action == 'select') {
470 $whereClause .= "AND is_selected = 0";
471 $sql = "UPDATE civicrm_prevnext_cache SET is_selected = 1 {$whereClause} {$entity_whereClause}";
472 $params[1] = array("{$cacheKey}%", 'String');
473 }
474 elseif ($action == 'unselect') {
475 $whereClause .= "AND is_selected = 1";
476 $sql = "UPDATE civicrm_prevnext_cache SET is_selected = 0 {$whereClause} {$entity_whereClause}";
477 $params[1] = array("%{$cacheKey}%", 'String');
478 }
479 // default action is reseting
480 }
481 elseif (!$cIds && $cacheKey && $action == 'unselect') {
482 $sql = "
483 UPDATE civicrm_prevnext_cache
484 SET is_selected = 0
485 WHERE cacheKey LIKE %1 AND is_selected = 1
486 {$entity_whereClause}
487 ";
488 $params[1] = array("{$cacheKey}%", 'String');
489 }
490 CRM_Core_DAO::executeQuery($sql, $params);
491 }
492
493 /**
494 * Get the selections.
495 *
496 * @param string $cacheKey
497 * Cache key.
498 * @param string $action
499 * Action.
500 * $action : get - get only selection records
501 * getall - get all the records of the specified cache key
502 * @param string $entity_table
503 * Entity table.
504 *
505 * @return array|NULL
506 */
507 public static function getSelection($cacheKey, $action = 'get', $entity_table = 'civicrm_contact') {
508 if (!$cacheKey) {
509 return NULL;
510 }
511 $params = array();
512
513 $entity_whereClause = " AND entity_table = '{$entity_table}'";
514 if ($cacheKey && ($action == 'get' || $action == 'getall')) {
515 $actionGet = ($action == "get") ? " AND is_selected = 1 " : "";
516 $sql = "
517 SELECT entity_id1, entity_id2 FROM civicrm_prevnext_cache
518 WHERE cacheKey LIKE %1
519 $actionGet
520 $entity_whereClause
521 ORDER BY id
522 ";
523 $params[1] = array("{$cacheKey}%", 'String');
524
525 $contactIds = array($cacheKey => array());
526 $cIdDao = CRM_Core_DAO::executeQuery($sql, $params);
527 while ($cIdDao->fetch()) {
528 if ($cIdDao->entity_id1 == $cIdDao->entity_id2) {
529 $contactIds[$cacheKey][$cIdDao->entity_id1] = 1;
530 }
531 }
532 return $contactIds;
533 }
534 }
535
536 /**
537 * @return array
538 */
539 public static function getSelectedContacts() {
540 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String');
541 $cacheKey = "civicrm search {$qfKey}";
542 $query = "
543 SELECT *
544 FROM civicrm_prevnext_cache
545 WHERE cacheKey LIKE %1
546 AND is_selected=1
547 AND cacheKey NOT LIKE %2
548 ";
549 $params1[1] = array("{$cacheKey}%", 'String');
550 $params1[2] = array("{$cacheKey}_alphabet%", 'String');
551 $dao = CRM_Core_DAO::executeQuery($query, $params1);
552
553 $val = array();
554 while ($dao->fetch()) {
555 $val[] = $dao->data;
556 }
557 return $val;
558 }
559
560 /**
561 * @param CRM_Core_Form $form
562 * @param array $params
563 *
564 * @return mixed
565 */
566 public static function buildSelectedContactPager(&$form, &$params) {
567 $params['status'] = ts('Contacts %%StatusMessage%%');
568 $params['csvString'] = NULL;
569 $params['buttonTop'] = 'PagerTopButton';
570 $params['buttonBottom'] = 'PagerBottomButton';
571 $params['rowCount'] = $form->get(CRM_Utils_Pager::PAGE_ROWCOUNT);
572
573 if (!$params['rowCount']) {
574 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
575 }
576
577 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $form);
578 $cacheKey = "civicrm search {$qfKey}";
579
580 $query = "
581 SELECT count(id)
582 FROM civicrm_prevnext_cache
583 WHERE cacheKey LIKE %1
584 AND is_selected = 1
585 AND cacheKey NOT LIKE %2
586 ";
587 $params1[1] = array("{$cacheKey}%", 'String');
588 $params1[2] = array("{$cacheKey}_alphabet%", 'String');
589 $paramsTotal = CRM_Core_DAO::singleValueQuery($query, $params1);
590 $params['total'] = $paramsTotal;
591 $form->_pager = new CRM_Utils_Pager($params);
592 $form->assign_by_ref('pager', $form->_pager);
593 list($offset, $rowCount) = $form->_pager->getOffsetAndRowCount();
594 $params['offset'] = $offset;
595 $params['rowCount1'] = $rowCount;
596 return $params;
597 }
598
599 }