Merge pull request #17008 from ivan-compucorp/CPS-70-fix-radio-value
[civicrm-core.git] / CRM / Core / BAO / PrevNextCache.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * BAO object for civicrm_prevnext_cache table.
20 */
21 class CRM_Core_BAO_PrevNextCache extends CRM_Core_DAO_PrevNextCache {
22
23 /**
24 * Get the previous and next keys.
25 *
26 * @param string $cacheKey
27 * @param int $id1
28 * @param int $id2
29 * @param int $mergeId
30 * @param string $join
31 * @param string $where
32 * @param bool $flip
33 *
34 * @return array
35 */
36 public static function getPositions($cacheKey, $id1, $id2, &$mergeId = NULL, $join = NULL, $where = NULL, $flip = FALSE) {
37 if ($flip) {
38 list($id1, $id2) = [$id2, $id1];
39 }
40
41 if ($mergeId == NULL) {
42 $query = "
43 SELECT id
44 FROM civicrm_prevnext_cache
45 WHERE cachekey = %3 AND
46 entity_id1 = %1 AND
47 entity_id2 = %2 AND
48 entity_table = 'civicrm_contact'
49 ";
50
51 $params = [
52 1 => [$id1, 'Integer'],
53 2 => [$id2, 'Integer'],
54 3 => [$cacheKey, 'String'],
55 ];
56
57 $mergeId = CRM_Core_DAO::singleValueQuery($query, $params);
58 }
59
60 $pos = ['foundEntry' => 0];
61 if ($mergeId) {
62 $pos['foundEntry'] = 1;
63
64 if ($where) {
65
66 $where = " AND {$where}";
67
68 }
69 $p = [
70 1 => [$mergeId, 'Integer'],
71 2 => [$cacheKey, 'String'],
72 ];
73 $sql = "SELECT pn.id, pn.entity_id1, pn.entity_id2, pn.data FROM civicrm_prevnext_cache pn {$join} ";
74 $wherePrev = " WHERE pn.id < %1 AND pn.cachekey = %2 {$where} ORDER BY ID DESC LIMIT 1";
75 $sqlPrev = $sql . $wherePrev;
76
77 $dao = CRM_Core_DAO::executeQuery($sqlPrev, $p);
78 if ($dao->fetch()) {
79 $pos['prev']['id1'] = $dao->entity_id1;
80 $pos['prev']['id2'] = $dao->entity_id2;
81 $pos['prev']['mergeId'] = $dao->id;
82 $pos['prev']['data'] = $dao->data;
83 }
84
85 $whereNext = " WHERE pn.id > %1 AND pn.cachekey = %2 {$where} ORDER BY ID ASC LIMIT 1";
86 $sqlNext = $sql . $whereNext;
87
88 $dao = CRM_Core_DAO::executeQuery($sqlNext, $p);
89 if ($dao->fetch()) {
90 $pos['next']['id1'] = $dao->entity_id1;
91 $pos['next']['id2'] = $dao->entity_id2;
92 $pos['next']['mergeId'] = $dao->id;
93 $pos['next']['data'] = $dao->data;
94 }
95 }
96 return $pos;
97 }
98
99 /**
100 * Delete an item from the prevnext cache table based on the entity.
101 *
102 * @param int $id
103 * @param string $cacheKey
104 * @param string $entityTable
105 */
106 public static function deleteItem($id = NULL, $cacheKey = NULL, $entityTable = 'civicrm_contact') {
107
108 //clear cache
109 $sql = "DELETE FROM civicrm_prevnext_cache WHERE entity_table = %1";
110 $params = [1 => [$entityTable, 'String']];
111
112 if (is_numeric($id)) {
113 $sql .= " AND ( entity_id1 = %2 OR entity_id2 = %2 )";
114 $params[2] = [$id, 'Integer'];
115 }
116
117 if (isset($cacheKey)) {
118 $sql .= " AND cachekey LIKE %3";
119 $params[3] = ["{$cacheKey}%", 'String'];
120 }
121 CRM_Core_DAO::executeQuery($sql, $params);
122 }
123
124 /**
125 * Delete pair from the previous next cache table to remove it from further merge consideration.
126 *
127 * The pair may have been flipped, so make sure we delete using both orders
128 *
129 * @param int $id1
130 * @param int $id2
131 * @param string $cacheKey
132 */
133 public static function deletePair($id1, $id2, $cacheKey = NULL) {
134 $sql = "DELETE FROM civicrm_prevnext_cache WHERE entity_table = 'civicrm_contact'";
135
136 $pair = "(entity_id1 = %2 AND entity_id2 = %3) OR (entity_id1 = %3 AND entity_id2 = %2)";
137 $sql .= " AND ( {$pair} )";
138 $params[2] = [$id1, 'Integer'];
139 $params[3] = [$id2, 'Integer'];
140
141 if (isset($cacheKey)) {
142 $sql .= " AND cachekey LIKE %4";
143 // used % to address any row with conflict-cacheKey e.g "merge Individual_8_0_conflicts"
144 $params[4] = ["{$cacheKey}%", 'String'];
145 }
146
147 CRM_Core_DAO::executeQuery($sql, $params);
148 }
149
150 /**
151 * Mark contacts as being in conflict.
152 *
153 * @param int $id1
154 * @param int $id2
155 * @param string $cacheKey
156 * @param array $conflicts
157 * @param string $mode
158 *
159 * @return bool
160 * @throws CRM_Core_Exception
161 */
162 public static function markConflict($id1, $id2, $cacheKey, $conflicts, $mode) {
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 = [
173 1 => [$id1, 'Integer'],
174 2 => [$id2, 'Integer'],
175 3 => ["{$cacheKey}", 'String'],
176 4 => ["{$cacheKey}_conflicts", 'String'],
177 ];
178 $pncFind = CRM_Core_DAO::executeQuery($sql, $params);
179
180 $conflictTexts = [];
181
182 foreach ($conflicts as $entity => $entityConflicts) {
183 if ($entity === 'contact') {
184 foreach ($entityConflicts as $conflict) {
185 $conflictTexts[] = "{$conflict['title']}: '{$conflict[$id1]}' vs. '{$conflict[$id2]}'";
186 }
187 }
188 else {
189 foreach ($entityConflicts as $locationConflict) {
190 if (!is_array($locationConflict)) {
191 continue;
192 }
193 $displayField = CRM_Dedupe_Merger::getLocationBlockInfo()[$entity]['displayField'];
194 $conflictTexts[] = "{$locationConflict['title']}: '{$locationConflict[$displayField][$id1]}' vs. '{$locationConflict[$displayField][$id2]}'";
195 }
196 }
197 }
198 $conflictString = implode(', ', $conflictTexts);
199
200 while ($pncFind->fetch()) {
201 $data = $pncFind->data;
202 if (!empty($data)) {
203 $data = CRM_Core_DAO::unSerializeField($data, CRM_Core_DAO::SERIALIZE_PHP);
204 $data['conflicts'] = $conflictString;
205 $data[$mode]['conflicts'] = $conflicts;
206
207 $pncUp = new CRM_Core_DAO_PrevNextCache();
208 $pncUp->id = $pncFind->id;
209 if ($pncUp->find(TRUE)) {
210 $pncUp->data = serialize($data);
211 $pncUp->cachekey = "{$cacheKey}_conflicts";
212 $pncUp->save();
213 }
214 }
215 }
216 return TRUE;
217 }
218
219 /**
220 * Retrieve from prev-next cache.
221 *
222 * This function is used from a variety of merge related functions, although
223 * it would probably be good to converge on calling CRM_Dedupe_Merger::getDuplicatePairs.
224 *
225 * We seem to currently be storing stats in this table too & they might make more sense in
226 * the main cache table.
227 *
228 * @param string $cacheKey
229 * @param string $join
230 * @param string $whereClause
231 * @param int $offset
232 * @param int $rowCount
233 * @param array $select
234 * @param string $orderByClause
235 * @param bool $includeConflicts
236 * Should we return rows that have already been idenfified as having a conflict.
237 * When this is TRUE you should be careful you do not set up a loop.
238 * @param array $params
239 *
240 * @return array
241 */
242 public static function retrieve($cacheKey, $join = NULL, $whereClause = NULL, $offset = 0, $rowCount = 0, $select = [], $orderByClause = '', $includeConflicts = TRUE, $params = []) {
243 $selectString = 'pn.*';
244
245 if (!empty($select)) {
246 $aliasArray = [];
247 foreach ($select as $column => $alias) {
248 $aliasArray[] = $column . ' as ' . $alias;
249 }
250 $selectString .= " , " . implode(' , ', $aliasArray);
251 }
252
253 $params = [
254 1 => [$cacheKey, 'String'],
255 ] + $params;
256
257 if (!empty($whereClause)) {
258 $whereClause = " AND " . $whereClause;
259 }
260 if ($includeConflicts) {
261 $where = ' WHERE (pn.cachekey = %1 OR pn.cachekey = %2)' . $whereClause;
262 $params[2] = ["{$cacheKey}_conflicts", 'String'];
263 }
264 else {
265 $where = ' WHERE (pn.cachekey = %1)' . $whereClause;
266 }
267
268 $query = "
269 SELECT SQL_CALC_FOUND_ROWS {$selectString}
270 FROM civicrm_prevnext_cache pn
271 {$join}
272 $where
273 $orderByClause
274 ";
275
276 if ($rowCount) {
277 $offset = CRM_Utils_Type::escape($offset, 'Int');
278 $rowCount = CRM_Utils_Type::escape($rowCount, 'Int');
279
280 $query .= " LIMIT {$offset}, {$rowCount}";
281 }
282
283 $dao = CRM_Core_DAO::executeQuery($query, $params);
284
285 $main = [];
286 $count = 0;
287 while ($dao->fetch()) {
288 if (self::is_serialized($dao->data)) {
289 $main[$count] = CRM_Utils_String::unserialize($dao->data);
290 }
291 else {
292 $main[$count] = $dao->data;
293 }
294
295 if (!empty($select)) {
296 $extraData = [];
297 foreach ($select as $sfield) {
298 $extraData[$sfield] = $dao->$sfield;
299 }
300 $main[$count] = [
301 'prevnext_id' => $dao->id,
302 'is_selected' => $dao->is_selected,
303 'entity_id1' => $dao->entity_id1,
304 'entity_id2' => $dao->entity_id2,
305 'data' => $main[$count],
306 ];
307 $main[$count] = array_merge($main[$count], $extraData);
308 }
309 $count++;
310 }
311
312 return $main;
313 }
314
315 /**
316 * @param $string
317 *
318 * @return bool
319 */
320 public static function is_serialized($string) {
321 return (@CRM_Utils_String::unserialize($string) !== FALSE);
322 }
323
324 /**
325 * @param string $sqlValues string of SQLValues to insert
326 * @return array
327 */
328 public static function convertSetItemValues($sqlValues) {
329 CRM_Core_Error::deprecatedFunctionWarning('Deprecated function');
330 $closingBrace = strpos($sqlValues, ')') - strlen($sqlValues);
331 $valueArray = array_map('trim', explode(', ', substr($sqlValues, strpos($sqlValues, '(') + 1, $closingBrace - 1)));
332 foreach ($valueArray as $key => &$value) {
333 // remove any quotes from values.
334 if (substr($value, 0, 1) == "'") {
335 $valueArray[$key] = substr($value, 1, -1);
336 }
337 }
338 return $valueArray;
339 }
340
341 /**
342 * @deprecated
343 *
344 * @param array|string $entity_table
345 * @param int $entity_id1
346 * @param int $entity_id2
347 * @param string $cacheKey
348 * @param string $data
349 */
350 public static function setItem($entity_table = NULL, $entity_id1 = NULL, $entity_id2 = NULL, $cacheKey = NULL, $data = NULL) {
351 CRM_Core_Error::deprecatedFunctionWarning('Deprecated function');
352 // If entity table is an array we are passing in an older format where this function only had 1 param $values. We put a deprecation warning.
353 if (!empty($entity_table) && is_array($entity_table)) {
354 Civi::log()->warning('Deprecated code path. Values should not be set this is going away in the future in favour of specific function params for each column.', array('civi.tag' => 'deprecated'));
355 foreach ($values as $value) {
356 $valueArray = self::convertSetItemValues($value);
357 self::setItem($valueArray[0], $valueArray[1], $valueArray[2], $valueArray[3], $valueArray[4]);
358 }
359 }
360 else {
361 CRM_Core_DAO::executeQuery("INSERT INTO civicrm_prevnext_cache (entity_table, entity_id1, entity_id2, cacheKey, data) VALUES
362 (%1, %2, %3, %4, '{$data}')", [
363 1 => [$entity_table, 'String'],
364 2 => [$entity_id1, 'Integer'],
365 3 => [$entity_id2, 'Integer'],
366 4 => [$cacheKey, 'String'],
367 ]);
368 }
369 }
370
371 /**
372 * Get count of matching rows.
373 *
374 * @param string $cacheKey
375 * @param string $join
376 * @param string $where
377 * @param string $op
378 * @param array $params
379 * Extra query params to parse into the query.
380 *
381 * @return int
382 */
383 public static function getCount($cacheKey, $join = NULL, $where = NULL, $op = "=", $params = []) {
384 $query = "
385 SELECT COUNT(*) FROM civicrm_prevnext_cache pn
386 {$join}
387 WHERE (pn.cachekey $op %1 OR pn.cachekey $op %2)
388 ";
389 if ($where) {
390 $query .= " AND {$where}";
391 }
392
393 $params = [
394 1 => [$cacheKey, 'String'],
395 2 => ["{$cacheKey}_conflicts", 'String'],
396 ] + $params;
397 return (int) CRM_Core_DAO::singleValueQuery($query, $params, TRUE, FALSE);
398 }
399
400 /**
401 * Repopulate the cache of merge prospects.
402 *
403 * @param int $rgid
404 * @param int $gid
405 * @param array $criteria
406 * Additional criteria to filter by.
407 *
408 * @param bool $checkPermissions
409 * Respect logged in user's permissions.
410 *
411 * @param int $searchLimit
412 * Limit for the number of contacts to be used for comparison.
413 * The search methodology finds all matches for the searchedContacts so this limits
414 * the number of searched contacts, not the matches found.
415 *
416 * @throws \CRM_Core_Exception
417 * @throws \CiviCRM_API3_Exception
418 */
419 public static function refillCache($rgid, $gid, $criteria, $checkPermissions, $searchLimit = 0) {
420 $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid, $criteria, $checkPermissions, $searchLimit);
421
422 // 1. Clear cache if any
423 $sql = "DELETE FROM civicrm_prevnext_cache WHERE cachekey LIKE %1";
424 CRM_Core_DAO::executeQuery($sql, [1 => ["{$cacheKeyString}%", 'String']]);
425
426 // FIXME: we need to start using temp tables / queries here instead of arrays.
427 // And cleanup code in CRM/Contact/Page/DedupeFind.php
428
429 // 2. FILL cache
430 $foundDupes = [];
431 if ($rgid && $gid) {
432 $foundDupes = CRM_Dedupe_Finder::dupesInGroup($rgid, $gid, $searchLimit);
433 }
434 elseif ($rgid) {
435 $contactIDs = [];
436 // The thing we really need to filter out is any chaining that would 'DO SOMETHING' to the DB.
437 // criteria could be passed in via url so we want to ensure nothing could be in that url that
438 // would chain to a delete. Limiting to getfields for 'get' limits us to declared fields,
439 // although we might wish to revisit later to allow joins.
440 $validFieldsForRetrieval = civicrm_api3('Contact', 'getfields', ['action' => 'get'])['values'];
441 $filteredCriteria = isset($criteria['contact']) ? array_intersect_key($criteria['contact'], $validFieldsForRetrieval) : [];
442
443 if (!empty($criteria) || !empty($searchLimit)) {
444 $contacts = civicrm_api3('Contact', 'get', array_merge([
445 'options' => ['limit' => $searchLimit],
446 'return' => 'id',
447 'check_permissions' => TRUE,
448 'contact_type' => civicrm_api3('RuleGroup', 'getvalue', ['id' => $rgid, 'return' => 'contact_type']),
449 ], $filteredCriteria));
450 $contactIDs = array_keys($contacts['values']);
451
452 if (empty($contactIDs)) {
453 // If there is criteria but no contacts were found then we should return now
454 // since we have no contacts to match.
455 return [];
456 }
457 }
458 $foundDupes = CRM_Dedupe_Finder::dupes($rgid, $contactIDs, $checkPermissions);
459 }
460
461 if (!empty($foundDupes)) {
462 CRM_Dedupe_Finder::parseAndStoreDupePairs($foundDupes, $cacheKeyString);
463 }
464 }
465
466 /**
467 * Old function to clean up he cache.
468 *
469 * @deprecated.
470 */
471 public static function cleanupCache() {
472 CRM_Core_Error::deprecatedFunctionWarning('Deprecated function');
473 Civi::service('prevnext')->cleanup();
474 }
475
476 /**
477 * Get the selections.
478 *
479 * NOTE: This stub has been preserved because one extension in `universe`
480 * was referencing the function.
481 *
482 * @deprecated
483 * @see CRM_Core_PrevNextCache_Sql::getSelection()
484 */
485 public static function getSelection($cacheKey, $action = 'get') {
486 CRM_Core_Error::deprecatedFunctionWarning('Deprecated function');
487 return Civi::service('prevnext')->getSelection($cacheKey, $action);
488 }
489
490 /**
491 * Flip 2 contacts in the prevNext cache.
492 *
493 * @param array $prevNextId
494 * @param bool $onlySelected
495 * Only flip those which have been marked as selected.
496 */
497 public static function flipPair(array $prevNextId, $onlySelected) {
498 $dao = new CRM_Core_DAO_PrevNextCache();
499 if ($onlySelected) {
500 $dao->is_selected = 1;
501 }
502 foreach ($prevNextId as $id) {
503 $dao->id = $id;
504 if ($dao->find(TRUE)) {
505 $originalData = CRM_Utils_String::unserialize($dao->data);
506 $srcFields = ['ID', 'Name'];
507 $swapFields = ['srcID', 'srcName', 'dstID', 'dstName'];
508 $data = array_diff_assoc($originalData, array_fill_keys($swapFields, 1));
509 foreach ($srcFields as $key) {
510 $data['src' . $key] = $originalData['dst' . $key];
511 $data['dst' . $key] = $originalData['src' . $key];
512 }
513 $dao->data = serialize($data);
514 $dao->entity_id1 = $data['dstID'];
515 $dao->entity_id2 = $data['srcID'];
516 $dao->save();
517 }
518 }
519 }
520
521 /**
522 * Get a list of available backend services.
523 *
524 * @return array
525 * Array(string $id => string $label).
526 */
527 public static function getPrevNextBackends() {
528 return [
529 'default' => ts('Default (Auto-detect)'),
530 'sql' => ts('SQL'),
531 'redis' => ts('Redis'),
532 ];
533 }
534
535 /**
536 * Generate and assign an arbitrary value to a field of a test object.
537 *
538 * This specifically supports testing the dedupe use case.
539 *
540 * @param string $fieldName
541 * @param array $fieldDef
542 * @param int $counter
543 * The globally-unique ID of the test object.
544 *
545 * @throws \CRM_Core_Exception
546 */
547 protected function assignTestValue($fieldName, &$fieldDef, $counter) {
548 if ($fieldName === 'cachekey') {
549 $this->cachekey = 'merge_' . rand();
550 return;
551 }
552 if ($fieldName === 'data') {
553 $this->data = serialize([]);
554 return;
555 }
556 parent::assignTestValue($fieldName, $fieldDef, $counter);
557 }
558
559 }