Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
bc77d7c0 TO |
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 | +--------------------------------------------------------------------+ | |
e70a7fc0 | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
f47539f6 | 19 | * BAO object for civicrm_prevnext_cache table. |
6a488035 TO |
20 | */ |
21 | class CRM_Core_BAO_PrevNextCache extends CRM_Core_DAO_PrevNextCache { | |
22 | ||
b5c2afd0 | 23 | /** |
f47539f6 | 24 | * Get the previous and next keys. |
25 | * | |
26 | * @param string $cacheKey | |
27 | * @param int $id1 | |
28 | * @param int $id2 | |
100fef9d | 29 | * @param int $mergeId |
f47539f6 | 30 | * @param string $join |
31 | * @param string $where | |
b5c2afd0 EM |
32 | * @param bool $flip |
33 | * | |
34 | * @return array | |
35 | */ | |
00be9182 | 36 | public static function getPositions($cacheKey, $id1, $id2, &$mergeId = NULL, $join = NULL, $where = NULL, $flip = FALSE) { |
6a488035 | 37 | if ($flip) { |
be2fb01f | 38 | list($id1, $id2) = [$id2, $id1]; |
6a488035 TO |
39 | } |
40 | ||
41 | if ($mergeId == NULL) { | |
42 | $query = " | |
43 | SELECT id | |
44 | FROM civicrm_prevnext_cache | |
783b4b21 | 45 | WHERE cachekey = %3 AND |
6a488035 TO |
46 | entity_id1 = %1 AND |
47 | entity_id2 = %2 AND | |
48 | entity_table = 'civicrm_contact' | |
49 | "; | |
50 | ||
be2fb01f CW |
51 | $params = [ |
52 | 1 => [$id1, 'Integer'], | |
53 | 2 => [$id2, 'Integer'], | |
54 | 3 => [$cacheKey, 'String'], | |
55 | ]; | |
6a488035 TO |
56 | |
57 | $mergeId = CRM_Core_DAO::singleValueQuery($query, $params); | |
58 | } | |
59 | ||
be2fb01f | 60 | $pos = ['foundEntry' => 0]; |
6a488035 TO |
61 | if ($mergeId) { |
62 | $pos['foundEntry'] = 1; | |
63 | ||
64 | if ($where) { | |
65 | ||
66 | $where = " AND {$where}"; | |
67 | ||
68 | } | |
be2fb01f CW |
69 | $p = [ |
70 | 1 => [$mergeId, 'Integer'], | |
71 | 2 => [$cacheKey, 'String'], | |
72 | ]; | |
353ffa53 | 73 | $sql = "SELECT pn.id, pn.entity_id1, pn.entity_id2, pn.data FROM civicrm_prevnext_cache pn {$join} "; |
783b4b21 | 74 | $wherePrev = " WHERE pn.id < %1 AND pn.cachekey = %2 {$where} ORDER BY ID DESC LIMIT 1"; |
353ffa53 | 75 | $sqlPrev = $sql . $wherePrev; |
6a488035 | 76 | |
6a488035 TO |
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 | ||
783b4b21 | 85 | $whereNext = " WHERE pn.id > %1 AND pn.cachekey = %2 {$where} ORDER BY ID ASC LIMIT 1"; |
6a488035 TO |
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 | ||
b5c2afd0 | 99 | /** |
f47539f6 | 100 | * Delete an item from the prevnext cache table based on the entity. |
101 | * | |
100fef9d | 102 | * @param int $id |
f47539f6 | 103 | * @param string $cacheKey |
b5c2afd0 EM |
104 | * @param string $entityTable |
105 | */ | |
00be9182 | 106 | public static function deleteItem($id = NULL, $cacheKey = NULL, $entityTable = 'civicrm_contact') { |
6a488035 TO |
107 | |
108 | //clear cache | |
109 | $sql = "DELETE FROM civicrm_prevnext_cache WHERE entity_table = %1"; | |
be2fb01f | 110 | $params = [1 => [$entityTable, 'String']]; |
6a488035 TO |
111 | |
112 | if (is_numeric($id)) { | |
113 | $sql .= " AND ( entity_id1 = %2 OR entity_id2 = %2 )"; | |
be2fb01f | 114 | $params[2] = [$id, 'Integer']; |
6a488035 TO |
115 | } |
116 | ||
117 | if (isset($cacheKey)) { | |
783b4b21 | 118 | $sql .= " AND cachekey LIKE %3"; |
be2fb01f | 119 | $params[3] = ["{$cacheKey}%", 'String']; |
6a488035 TO |
120 | } |
121 | CRM_Core_DAO::executeQuery($sql, $params); | |
122 | } | |
123 | ||
b5c2afd0 | 124 | /** |
c131d228 | 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 | |
f47539f6 | 128 | * |
129 | * @param int $id1 | |
130 | * @param int $id2 | |
131 | * @param string $cacheKey | |
b5c2afd0 | 132 | */ |
c131d228 | 133 | public static function deletePair($id1, $id2, $cacheKey = NULL) { |
134 | $sql = "DELETE FROM civicrm_prevnext_cache WHERE entity_table = 'civicrm_contact'"; | |
6a488035 | 135 | |
c131d228 | 136 | $pair = "(entity_id1 = %2 AND entity_id2 = %3) OR (entity_id1 = %3 AND entity_id2 = %2)"; |
6a488035 | 137 | $sql .= " AND ( {$pair} )"; |
be2fb01f CW |
138 | $params[2] = [$id1, 'Integer']; |
139 | $params[3] = [$id2, 'Integer']; | |
6a488035 TO |
140 | |
141 | if (isset($cacheKey)) { | |
783b4b21 | 142 | $sql .= " AND cachekey LIKE %4"; |
518fa0ee SL |
143 | // used % to address any row with conflict-cacheKey e.g "merge Individual_8_0_conflicts" |
144 | $params[4] = ["{$cacheKey}%", 'String']; | |
6a488035 TO |
145 | } |
146 | ||
147 | CRM_Core_DAO::executeQuery($sql, $params); | |
148 | } | |
149 | ||
f2ac86d1 | 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 | |
ffa59d18 | 157 | * @param string $mode |
f2ac86d1 | 158 | * |
159 | * @return bool | |
8cec96dc | 160 | * @throws CRM_Core_Exception |
f2ac86d1 | 161 | */ |
ffa59d18 | 162 | public static function markConflict($id1, $id2, $cacheKey, $conflicts, $mode) { |
63ef778e | 163 | if (empty($cacheKey) || empty($conflicts)) { |
164 | return FALSE; | |
165 | } | |
166 | ||
ad37ac8e | 167 | $sql = "SELECT pn.* |
63ef778e | 168 | FROM civicrm_prevnext_cache pn |
ad37ac8e | 169 | WHERE |
170 | ((pn.entity_id1 = %1 AND pn.entity_id2 = %2) OR (pn.entity_id1 = %2 AND pn.entity_id2 = %1)) AND | |
783b4b21 | 171 | (cachekey = %3 OR cachekey = %4)"; |
be2fb01f CW |
172 | $params = [ |
173 | 1 => [$id1, 'Integer'], | |
174 | 2 => [$id2, 'Integer'], | |
175 | 3 => ["{$cacheKey}", 'String'], | |
176 | 4 => ["{$cacheKey}_conflicts", 'String'], | |
177 | ]; | |
63ef778e | 178 | $pncFind = CRM_Core_DAO::executeQuery($sql, $params); |
179 | ||
5214f03b | 180 | $conflictTexts = []; |
ffa59d18 | 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 | } | |
5214f03b | 197 | } |
198 | $conflictString = implode(', ', $conflictTexts); | |
ffa59d18 | 199 | |
63ef778e | 200 | while ($pncFind->fetch()) { |
201 | $data = $pncFind->data; | |
202 | if (!empty($data)) { | |
8cec96dc | 203 | $data = CRM_Core_DAO::unSerializeField($data, CRM_Core_DAO::SERIALIZE_PHP); |
5214f03b | 204 | $data['conflicts'] = $conflictString; |
ffa59d18 | 205 | $data[$mode]['conflicts'] = $conflicts; |
63ef778e | 206 | |
207 | $pncUp = new CRM_Core_DAO_PrevNextCache(); | |
208 | $pncUp->id = $pncFind->id; | |
209 | if ($pncUp->find(TRUE)) { | |
210 | $pncUp->data = serialize($data); | |
783b4b21 | 211 | $pncUp->cachekey = "{$cacheKey}_conflicts"; |
63ef778e | 212 | $pncUp->save(); |
213 | } | |
214 | } | |
215 | } | |
216 | return TRUE; | |
217 | } | |
218 | ||
b5c2afd0 | 219 | /** |
ad37ac8e | 220 | * Retrieve from prev-next cache. |
221 | * | |
66eceb0b | 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 | * | |
ad37ac8e | 228 | * @param string $cacheKey |
229 | * @param string $join | |
66eceb0b | 230 | * @param string $whereClause |
b5c2afd0 EM |
231 | * @param int $offset |
232 | * @param int $rowCount | |
66eceb0b | 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. | |
f47539f6 | 238 | * @param array $params |
b5c2afd0 EM |
239 | * |
240 | * @return array | |
241 | */ | |
be2fb01f | 242 | public static function retrieve($cacheKey, $join = NULL, $whereClause = NULL, $offset = 0, $rowCount = 0, $select = [], $orderByClause = '', $includeConflicts = TRUE, $params = []) { |
63ef778e | 243 | $selectString = 'pn.*'; |
66eceb0b | 244 | |
f931b74c | 245 | if (!empty($select)) { |
be2fb01f | 246 | $aliasArray = []; |
63ef778e | 247 | foreach ($select as $column => $alias) { |
f931b74c | 248 | $aliasArray[] = $column . ' as ' . $alias; |
63ef778e | 249 | } |
f931b74c | 250 | $selectString .= " , " . implode(' , ', $aliasArray); |
63ef778e | 251 | } |
66eceb0b | 252 | |
be2fb01f CW |
253 | $params = [ |
254 | 1 => [$cacheKey, 'String'], | |
255 | ] + $params; | |
6a488035 | 256 | |
66eceb0b | 257 | if (!empty($whereClause)) { |
258 | $whereClause = " AND " . $whereClause; | |
259 | } | |
260 | if ($includeConflicts) { | |
783b4b21 | 261 | $where = ' WHERE (pn.cachekey = %1 OR pn.cachekey = %2)' . $whereClause; |
be2fb01f | 262 | $params[2] = ["{$cacheKey}_conflicts", 'String']; |
66eceb0b | 263 | } |
264 | else { | |
783b4b21 | 265 | $where = ' WHERE (pn.cachekey = %1)' . $whereClause; |
6a488035 TO |
266 | } |
267 | ||
66eceb0b | 268 | $query = " |
269 | SELECT SQL_CALC_FOUND_ROWS {$selectString} | |
270 | FROM civicrm_prevnext_cache pn | |
271 | {$join} | |
272 | $where | |
273 | $orderByClause | |
274 | "; | |
275 | ||
6a488035 | 276 | if ($rowCount) { |
bf00d1b6 DL |
277 | $offset = CRM_Utils_Type::escape($offset, 'Int'); |
278 | $rowCount = CRM_Utils_Type::escape($rowCount, 'Int'); | |
279 | ||
6a488035 TO |
280 | $query .= " LIMIT {$offset}, {$rowCount}"; |
281 | } | |
282 | ||
283 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
284 | ||
be2fb01f | 285 | $main = []; |
63ef778e | 286 | $count = 0; |
6a488035 TO |
287 | while ($dao->fetch()) { |
288 | if (self::is_serialized($dao->data)) { | |
63ef778e | 289 | $main[$count] = unserialize($dao->data); |
6a488035 TO |
290 | } |
291 | else { | |
63ef778e | 292 | $main[$count] = $dao->data; |
6a488035 | 293 | } |
63ef778e | 294 | |
295 | if (!empty($select)) { | |
be2fb01f | 296 | $extraData = []; |
bf588234 | 297 | foreach ($select as $sfield) { |
518fa0ee | 298 | $extraData[$sfield] = $dao->$sfield; |
63ef778e | 299 | } |
be2fb01f | 300 | $main[$count] = [ |
f931b74c | 301 | 'prevnext_id' => $dao->id, |
302 | 'is_selected' => $dao->is_selected, | |
303 | 'entity_id1' => $dao->entity_id1, | |
304 | 'entity_id2' => $dao->entity_id2, | |
63ef778e | 305 | 'data' => $main[$count], |
be2fb01f | 306 | ]; |
63ef778e | 307 | $main[$count] = array_merge($main[$count], $extraData); |
6a488035 | 308 | } |
63ef778e | 309 | $count++; |
6a488035 TO |
310 | } |
311 | ||
312 | return $main; | |
313 | } | |
314 | ||
b5c2afd0 EM |
315 | /** |
316 | * @param $string | |
317 | * | |
318 | * @return bool | |
319 | */ | |
6a488035 | 320 | public static function is_serialized($string) { |
ab8a593e | 321 | return (@unserialize($string) !== FALSE); |
6a488035 TO |
322 | } |
323 | ||
b5c2afd0 | 324 | /** |
e1c519d7 SL |
325 | * @param string $sqlValues string of SQLValues to insert |
326 | * @return array | |
b5c2afd0 | 327 | */ |
e1c519d7 SL |
328 | public static function convertSetItemValues($sqlValues) { |
329 | $closingBrace = strpos($sqlValues, ')') - strlen($sqlValues); | |
330 | $valueArray = array_map('trim', explode(', ', substr($sqlValues, strpos($sqlValues, '(') + 1, $closingBrace - 1))); | |
331 | foreach ($valueArray as $key => &$value) { | |
332 | // remove any quotes from values. | |
333 | if (substr($value, 0, 1) == "'") { | |
334 | $valueArray[$key] = substr($value, 1, -1); | |
335 | } | |
336 | } | |
337 | return $valueArray; | |
338 | } | |
6a488035 | 339 | |
e1c519d7 SL |
340 | /** |
341 | * @param array|string $entity_table | |
342 | * @param int $entity_id1 | |
343 | * @param int $entity_id2 | |
344 | * @param string $cacheKey | |
345 | * @param string $data | |
346 | */ | |
347 | public static function setItem($entity_table = NULL, $entity_id1 = NULL, $entity_id2 = NULL, $cacheKey = NULL, $data = NULL) { | |
348 | // 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. | |
349 | if (!empty($entity_table) && is_array($entity_table)) { | |
350 | 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')); | |
351 | foreach ($values as $value) { | |
352 | $valueArray = self::convertSetItemValues($value); | |
353 | self::setItem($valueArray[0], $valueArray[1], $valueArray[2], $valueArray[3], $valueArray[4]); | |
354 | } | |
355 | } | |
356 | else { | |
357 | CRM_Core_DAO::executeQuery("INSERT INTO civicrm_prevnext_cache (entity_table, entity_id1, entity_id2, cacheKey, data) VALUES | |
358 | (%1, %2, %3, %4, '{$data}')", [ | |
359 | 1 => [$entity_table, 'String'], | |
360 | 2 => [$entity_id1, 'Integer'], | |
361 | 3 => [$entity_id2, 'Integer'], | |
362 | 4 => [$cacheKey, 'String'], | |
363 | ]); | |
364 | } | |
6a488035 TO |
365 | } |
366 | ||
b5c2afd0 | 367 | /** |
f47539f6 | 368 | * Get count of matching rows. |
369 | * | |
370 | * @param string $cacheKey | |
ed92673b | 371 | * @param string $join |
372 | * @param string $where | |
b5c2afd0 | 373 | * @param string $op |
ed92673b | 374 | * @param array $params |
375 | * Extra query params to parse into the query. | |
b5c2afd0 EM |
376 | * |
377 | * @return int | |
378 | */ | |
be2fb01f | 379 | public static function getCount($cacheKey, $join = NULL, $where = NULL, $op = "=", $params = []) { |
6a488035 TO |
380 | $query = " |
381 | SELECT COUNT(*) FROM civicrm_prevnext_cache pn | |
382 | {$join} | |
783b4b21 | 383 | WHERE (pn.cachekey $op %1 OR pn.cachekey $op %2) |
6a488035 TO |
384 | "; |
385 | if ($where) { | |
386 | $query .= " AND {$where}"; | |
387 | } | |
388 | ||
be2fb01f CW |
389 | $params = [ |
390 | 1 => [$cacheKey, 'String'], | |
391 | 2 => ["{$cacheKey}_conflicts", 'String'], | |
392 | ] + $params; | |
64951b63 | 393 | return (int) CRM_Core_DAO::singleValueQuery($query, $params, TRUE, FALSE); |
6a488035 TO |
394 | } |
395 | ||
b5c2afd0 | 396 | /** |
e23e26ec | 397 | * Repopulate the cache of merge prospects. |
398 | * | |
100fef9d CW |
399 | * @param int $rgid |
400 | * @param int $gid | |
e23e26ec | 401 | * @param array $criteria |
402 | * Additional criteria to filter by. | |
7a9ab499 | 403 | * |
3058f4d9 | 404 | * @param bool $checkPermissions |
405 | * Respect logged in user's permissions. | |
406 | * | |
21a95d83 | 407 | * @param int $searchLimit |
408 | * Limit for the number of contacts to be used for comparison. | |
409 | * The search methodology finds all matches for the searchedContacts so this limits | |
410 | * the number of searched contacts, not the matches found. | |
411 | * | |
3058f4d9 | 412 | * @throws \CRM_Core_Exception |
413 | * @throws \CiviCRM_API3_Exception | |
b5c2afd0 | 414 | */ |
e1e24a57 | 415 | public static function refillCache($rgid, $gid, $criteria, $checkPermissions, $searchLimit = 0) { |
997a03fe | 416 | $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid, $criteria, $checkPermissions, $searchLimit); |
6a488035 TO |
417 | |
418 | // 1. Clear cache if any | |
783b4b21 | 419 | $sql = "DELETE FROM civicrm_prevnext_cache WHERE cachekey LIKE %1"; |
be2fb01f | 420 | CRM_Core_DAO::executeQuery($sql, [1 => ["{$cacheKeyString}%", 'String']]); |
6a488035 TO |
421 | |
422 | // FIXME: we need to start using temp tables / queries here instead of arrays. | |
423 | // And cleanup code in CRM/Contact/Page/DedupeFind.php | |
424 | ||
425 | // 2. FILL cache | |
be2fb01f | 426 | $foundDupes = []; |
6a488035 | 427 | if ($rgid && $gid) { |
21a95d83 | 428 | $foundDupes = CRM_Dedupe_Finder::dupesInGroup($rgid, $gid, $searchLimit); |
6a488035 TO |
429 | } |
430 | elseif ($rgid) { | |
be2fb01f | 431 | $contactIDs = []; |
88251439 | 432 | // The thing we really need to filter out is any chaining that would 'DO SOMETHING' to the DB. |
433 | // criteria could be passed in via url so we want to ensure nothing could be in that url that | |
434 | // would chain to a delete. Limiting to getfields for 'get' limits us to declared fields, | |
435 | // although we might wish to revisit later to allow joins. | |
436 | $validFieldsForRetrieval = civicrm_api3('Contact', 'getfields', ['action' => 'get'])['values']; | |
22912bef | 437 | $filteredCriteria = isset($criteria['contact']) ? array_intersect_key($criteria['contact'], $validFieldsForRetrieval) : []; |
438 | ||
439 | if (!empty($criteria) || !empty($searchLimit)) { | |
88251439 | 440 | $contacts = civicrm_api3('Contact', 'get', array_merge([ |
22912bef | 441 | 'options' => ['limit' => $searchLimit], |
88251439 | 442 | 'return' => 'id', |
443 | 'check_permissions' => TRUE, | |
22912bef | 444 | 'contact_type' => civicrm_api3('RuleGroup', 'getvalue', ['id' => $rgid, 'return' => 'contact_type']), |
445 | ], $filteredCriteria)); | |
e23e26ec | 446 | $contactIDs = array_keys($contacts['values']); |
22912bef | 447 | |
448 | if (empty($contactIDs)) { | |
449 | // If there is criteria but no contacts were found then we should return now | |
450 | // since we have no contacts to match. | |
451 | return []; | |
452 | } | |
e23e26ec | 453 | } |
22912bef | 454 | $foundDupes = CRM_Dedupe_Finder::dupes($rgid, $contactIDs, $checkPermissions); |
6a488035 TO |
455 | } |
456 | ||
457 | if (!empty($foundDupes)) { | |
1719073d | 458 | CRM_Dedupe_Finder::parseAndStoreDupePairs($foundDupes, $cacheKeyString); |
6a488035 TO |
459 | } |
460 | } | |
461 | ||
00be9182 | 462 | public static function cleanupCache() { |
435fc552 | 463 | Civi::service('prevnext')->cleanup(); |
6a488035 TO |
464 | } |
465 | ||
6a488035 | 466 | /** |
fe482240 | 467 | * Get the selections. |
6a488035 | 468 | * |
b7994703 TO |
469 | * NOTE: This stub has been preserved because one extension in `universe` |
470 | * was referencing the function. | |
77b97be7 | 471 | * |
b7994703 TO |
472 | * @deprecated |
473 | * @see CRM_Core_PrevNextCache_Sql::getSelection() | |
6a488035 | 474 | */ |
40ddbe99 TO |
475 | public static function getSelection($cacheKey, $action = 'get') { |
476 | return Civi::service('prevnext')->getSelection($cacheKey, $action); | |
6a488035 TO |
477 | } |
478 | ||
b5c2afd0 | 479 | /** |
808c05a9 | 480 | * Flip 2 contacts in the prevNext cache. |
481 | * | |
482 | * @param array $prevNextId | |
483 | * @param bool $onlySelected | |
484 | * Only flip those which have been marked as selected. | |
485 | */ | |
486 | public static function flipPair(array $prevNextId, $onlySelected) { | |
487 | $dao = new CRM_Core_DAO_PrevNextCache(); | |
488 | if ($onlySelected) { | |
489 | $dao->is_selected = 1; | |
490 | } | |
491 | foreach ($prevNextId as $id) { | |
492 | $dao->id = $id; | |
493 | if ($dao->find(TRUE)) { | |
494 | $originalData = unserialize($dao->data); | |
be2fb01f CW |
495 | $srcFields = ['ID', 'Name']; |
496 | $swapFields = ['srcID', 'srcName', 'dstID', 'dstName']; | |
808c05a9 | 497 | $data = array_diff_assoc($originalData, array_fill_keys($swapFields, 1)); |
498 | foreach ($srcFields as $key) { | |
499 | $data['src' . $key] = $originalData['dst' . $key]; | |
500 | $data['dst' . $key] = $originalData['src' . $key]; | |
501 | } | |
502 | $dao->data = serialize($data); | |
e67dcaf8 | 503 | $dao->entity_id1 = $data['dstID']; |
504 | $dao->entity_id2 = $data['srcID']; | |
808c05a9 | 505 | $dao->save(); |
506 | } | |
507 | } | |
508 | } | |
509 | ||
e28bf654 TO |
510 | /** |
511 | * Get a list of available backend services. | |
512 | * | |
513 | * @return array | |
514 | * Array(string $id => string $label). | |
515 | */ | |
516 | public static function getPrevNextBackends() { | |
517 | return [ | |
518 | 'default' => ts('Default (Auto-detect)'), | |
519 | 'sql' => ts('SQL'), | |
520 | 'redis' => ts('Redis'), | |
521 | ]; | |
522 | } | |
523 | ||
e13fa54b | 524 | /** |
525 | * Generate and assign an arbitrary value to a field of a test object. | |
526 | * | |
527 | * This specifically supports testing the dedupe use case. | |
528 | * | |
529 | * @param string $fieldName | |
530 | * @param array $fieldDef | |
531 | * @param int $counter | |
532 | * The globally-unique ID of the test object. | |
533 | */ | |
534 | protected function assignTestValue($fieldName, &$fieldDef, $counter) { | |
783b4b21 SL |
535 | if ($fieldName === 'cachekey') { |
536 | $this->cachekey = 'merge_' . rand(); | |
e13fa54b | 537 | return; |
538 | } | |
539 | if ($fieldName === 'data') { | |
540 | $this->data = serialize([]); | |
541 | return; | |
542 | } | |
543 | parent::assignTestValue($fieldName, $fieldDef, $counter); | |
544 | } | |
545 | ||
6a488035 | 546 | } |