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 | } | |
6a488035 | 123 | |
f2ac86d1 | 124 | /** |
125 | * Mark contacts as being in conflict. | |
126 | * | |
127 | * @param int $id1 | |
128 | * @param int $id2 | |
129 | * @param string $cacheKey | |
130 | * @param array $conflicts | |
ffa59d18 | 131 | * @param string $mode |
f2ac86d1 | 132 | * |
133 | * @return bool | |
8cec96dc | 134 | * @throws CRM_Core_Exception |
f2ac86d1 | 135 | */ |
ffa59d18 | 136 | public static function markConflict($id1, $id2, $cacheKey, $conflicts, $mode) { |
63ef778e | 137 | if (empty($cacheKey) || empty($conflicts)) { |
138 | return FALSE; | |
139 | } | |
140 | ||
ad37ac8e | 141 | $sql = "SELECT pn.* |
63ef778e | 142 | FROM civicrm_prevnext_cache pn |
ad37ac8e | 143 | WHERE |
144 | ((pn.entity_id1 = %1 AND pn.entity_id2 = %2) OR (pn.entity_id1 = %2 AND pn.entity_id2 = %1)) AND | |
783b4b21 | 145 | (cachekey = %3 OR cachekey = %4)"; |
be2fb01f CW |
146 | $params = [ |
147 | 1 => [$id1, 'Integer'], | |
148 | 2 => [$id2, 'Integer'], | |
149 | 3 => ["{$cacheKey}", 'String'], | |
150 | 4 => ["{$cacheKey}_conflicts", 'String'], | |
151 | ]; | |
63ef778e | 152 | $pncFind = CRM_Core_DAO::executeQuery($sql, $params); |
153 | ||
5214f03b | 154 | $conflictTexts = []; |
ffa59d18 | 155 | |
156 | foreach ($conflicts as $entity => $entityConflicts) { | |
157 | if ($entity === 'contact') { | |
158 | foreach ($entityConflicts as $conflict) { | |
159 | $conflictTexts[] = "{$conflict['title']}: '{$conflict[$id1]}' vs. '{$conflict[$id2]}'"; | |
160 | } | |
161 | } | |
162 | else { | |
163 | foreach ($entityConflicts as $locationConflict) { | |
164 | if (!is_array($locationConflict)) { | |
165 | continue; | |
166 | } | |
167 | $displayField = CRM_Dedupe_Merger::getLocationBlockInfo()[$entity]['displayField']; | |
168 | $conflictTexts[] = "{$locationConflict['title']}: '{$locationConflict[$displayField][$id1]}' vs. '{$locationConflict[$displayField][$id2]}'"; | |
169 | } | |
170 | } | |
5214f03b | 171 | } |
172 | $conflictString = implode(', ', $conflictTexts); | |
ffa59d18 | 173 | |
63ef778e | 174 | while ($pncFind->fetch()) { |
175 | $data = $pncFind->data; | |
176 | if (!empty($data)) { | |
8cec96dc | 177 | $data = CRM_Core_DAO::unSerializeField($data, CRM_Core_DAO::SERIALIZE_PHP); |
5214f03b | 178 | $data['conflicts'] = $conflictString; |
ffa59d18 | 179 | $data[$mode]['conflicts'] = $conflicts; |
63ef778e | 180 | |
181 | $pncUp = new CRM_Core_DAO_PrevNextCache(); | |
182 | $pncUp->id = $pncFind->id; | |
183 | if ($pncUp->find(TRUE)) { | |
184 | $pncUp->data = serialize($data); | |
783b4b21 | 185 | $pncUp->cachekey = "{$cacheKey}_conflicts"; |
63ef778e | 186 | $pncUp->save(); |
187 | } | |
188 | } | |
189 | } | |
190 | return TRUE; | |
191 | } | |
192 | ||
b5c2afd0 | 193 | /** |
ad37ac8e | 194 | * Retrieve from prev-next cache. |
195 | * | |
66eceb0b | 196 | * This function is used from a variety of merge related functions, although |
197 | * it would probably be good to converge on calling CRM_Dedupe_Merger::getDuplicatePairs. | |
198 | * | |
199 | * We seem to currently be storing stats in this table too & they might make more sense in | |
200 | * the main cache table. | |
201 | * | |
ad37ac8e | 202 | * @param string $cacheKey |
203 | * @param string $join | |
66eceb0b | 204 | * @param string $whereClause |
b5c2afd0 EM |
205 | * @param int $offset |
206 | * @param int $rowCount | |
66eceb0b | 207 | * @param array $select |
208 | * @param string $orderByClause | |
209 | * @param bool $includeConflicts | |
210 | * Should we return rows that have already been idenfified as having a conflict. | |
211 | * When this is TRUE you should be careful you do not set up a loop. | |
f47539f6 | 212 | * @param array $params |
b5c2afd0 EM |
213 | * |
214 | * @return array | |
215 | */ | |
be2fb01f | 216 | public static function retrieve($cacheKey, $join = NULL, $whereClause = NULL, $offset = 0, $rowCount = 0, $select = [], $orderByClause = '', $includeConflicts = TRUE, $params = []) { |
63ef778e | 217 | $selectString = 'pn.*'; |
66eceb0b | 218 | |
f931b74c | 219 | if (!empty($select)) { |
be2fb01f | 220 | $aliasArray = []; |
63ef778e | 221 | foreach ($select as $column => $alias) { |
f931b74c | 222 | $aliasArray[] = $column . ' as ' . $alias; |
63ef778e | 223 | } |
f931b74c | 224 | $selectString .= " , " . implode(' , ', $aliasArray); |
63ef778e | 225 | } |
66eceb0b | 226 | |
be2fb01f CW |
227 | $params = [ |
228 | 1 => [$cacheKey, 'String'], | |
229 | ] + $params; | |
6a488035 | 230 | |
66eceb0b | 231 | if (!empty($whereClause)) { |
232 | $whereClause = " AND " . $whereClause; | |
233 | } | |
234 | if ($includeConflicts) { | |
783b4b21 | 235 | $where = ' WHERE (pn.cachekey = %1 OR pn.cachekey = %2)' . $whereClause; |
be2fb01f | 236 | $params[2] = ["{$cacheKey}_conflicts", 'String']; |
66eceb0b | 237 | } |
238 | else { | |
783b4b21 | 239 | $where = ' WHERE (pn.cachekey = %1)' . $whereClause; |
6a488035 TO |
240 | } |
241 | ||
66eceb0b | 242 | $query = " |
243 | SELECT SQL_CALC_FOUND_ROWS {$selectString} | |
244 | FROM civicrm_prevnext_cache pn | |
245 | {$join} | |
246 | $where | |
247 | $orderByClause | |
248 | "; | |
249 | ||
6a488035 | 250 | if ($rowCount) { |
bf00d1b6 DL |
251 | $offset = CRM_Utils_Type::escape($offset, 'Int'); |
252 | $rowCount = CRM_Utils_Type::escape($rowCount, 'Int'); | |
253 | ||
6a488035 TO |
254 | $query .= " LIMIT {$offset}, {$rowCount}"; |
255 | } | |
256 | ||
257 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
258 | ||
be2fb01f | 259 | $main = []; |
63ef778e | 260 | $count = 0; |
6a488035 TO |
261 | while ($dao->fetch()) { |
262 | if (self::is_serialized($dao->data)) { | |
f24846d5 | 263 | $main[$count] = CRM_Utils_String::unserialize($dao->data); |
6a488035 TO |
264 | } |
265 | else { | |
63ef778e | 266 | $main[$count] = $dao->data; |
6a488035 | 267 | } |
63ef778e | 268 | |
269 | if (!empty($select)) { | |
be2fb01f | 270 | $extraData = []; |
bf588234 | 271 | foreach ($select as $sfield) { |
518fa0ee | 272 | $extraData[$sfield] = $dao->$sfield; |
63ef778e | 273 | } |
be2fb01f | 274 | $main[$count] = [ |
f931b74c | 275 | 'prevnext_id' => $dao->id, |
276 | 'is_selected' => $dao->is_selected, | |
277 | 'entity_id1' => $dao->entity_id1, | |
278 | 'entity_id2' => $dao->entity_id2, | |
63ef778e | 279 | 'data' => $main[$count], |
be2fb01f | 280 | ]; |
63ef778e | 281 | $main[$count] = array_merge($main[$count], $extraData); |
6a488035 | 282 | } |
63ef778e | 283 | $count++; |
6a488035 TO |
284 | } |
285 | ||
286 | return $main; | |
287 | } | |
288 | ||
b5c2afd0 EM |
289 | /** |
290 | * @param $string | |
291 | * | |
292 | * @return bool | |
293 | */ | |
6a488035 | 294 | public static function is_serialized($string) { |
f24846d5 | 295 | return (@CRM_Utils_String::unserialize($string) !== FALSE); |
6a488035 TO |
296 | } |
297 | ||
b5c2afd0 | 298 | /** |
e1c519d7 SL |
299 | * @param string $sqlValues string of SQLValues to insert |
300 | * @return array | |
b5c2afd0 | 301 | */ |
e1c519d7 | 302 | public static function convertSetItemValues($sqlValues) { |
b3e4463a | 303 | CRM_Core_Error::deprecatedFunctionWarning('Deprecated function'); |
e1c519d7 SL |
304 | $closingBrace = strpos($sqlValues, ')') - strlen($sqlValues); |
305 | $valueArray = array_map('trim', explode(', ', substr($sqlValues, strpos($sqlValues, '(') + 1, $closingBrace - 1))); | |
306 | foreach ($valueArray as $key => &$value) { | |
307 | // remove any quotes from values. | |
308 | if (substr($value, 0, 1) == "'") { | |
309 | $valueArray[$key] = substr($value, 1, -1); | |
310 | } | |
311 | } | |
312 | return $valueArray; | |
313 | } | |
6a488035 | 314 | |
e1c519d7 | 315 | /** |
3bcde7f1 | 316 | * @deprecated |
317 | * | |
e1c519d7 SL |
318 | * @param array|string $entity_table |
319 | * @param int $entity_id1 | |
320 | * @param int $entity_id2 | |
321 | * @param string $cacheKey | |
322 | * @param string $data | |
323 | */ | |
324 | public static function setItem($entity_table = NULL, $entity_id1 = NULL, $entity_id2 = NULL, $cacheKey = NULL, $data = NULL) { | |
3bcde7f1 | 325 | CRM_Core_Error::deprecatedFunctionWarning('Deprecated function'); |
e1c519d7 SL |
326 | // 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. |
327 | if (!empty($entity_table) && is_array($entity_table)) { | |
328 | 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')); | |
329 | foreach ($values as $value) { | |
330 | $valueArray = self::convertSetItemValues($value); | |
331 | self::setItem($valueArray[0], $valueArray[1], $valueArray[2], $valueArray[3], $valueArray[4]); | |
332 | } | |
333 | } | |
334 | else { | |
335 | CRM_Core_DAO::executeQuery("INSERT INTO civicrm_prevnext_cache (entity_table, entity_id1, entity_id2, cacheKey, data) VALUES | |
336 | (%1, %2, %3, %4, '{$data}')", [ | |
337 | 1 => [$entity_table, 'String'], | |
338 | 2 => [$entity_id1, 'Integer'], | |
339 | 3 => [$entity_id2, 'Integer'], | |
340 | 4 => [$cacheKey, 'String'], | |
341 | ]); | |
342 | } | |
6a488035 TO |
343 | } |
344 | ||
b5c2afd0 | 345 | /** |
f47539f6 | 346 | * Get count of matching rows. |
347 | * | |
348 | * @param string $cacheKey | |
ed92673b | 349 | * @param string $join |
350 | * @param string $where | |
b5c2afd0 | 351 | * @param string $op |
ed92673b | 352 | * @param array $params |
353 | * Extra query params to parse into the query. | |
b5c2afd0 EM |
354 | * |
355 | * @return int | |
356 | */ | |
be2fb01f | 357 | public static function getCount($cacheKey, $join = NULL, $where = NULL, $op = "=", $params = []) { |
6a488035 TO |
358 | $query = " |
359 | SELECT COUNT(*) FROM civicrm_prevnext_cache pn | |
360 | {$join} | |
783b4b21 | 361 | WHERE (pn.cachekey $op %1 OR pn.cachekey $op %2) |
6a488035 TO |
362 | "; |
363 | if ($where) { | |
364 | $query .= " AND {$where}"; | |
365 | } | |
366 | ||
be2fb01f CW |
367 | $params = [ |
368 | 1 => [$cacheKey, 'String'], | |
369 | 2 => ["{$cacheKey}_conflicts", 'String'], | |
370 | ] + $params; | |
64951b63 | 371 | return (int) CRM_Core_DAO::singleValueQuery($query, $params, TRUE, FALSE); |
6a488035 TO |
372 | } |
373 | ||
b5c2afd0 | 374 | /** |
e23e26ec | 375 | * Repopulate the cache of merge prospects. |
376 | * | |
100fef9d CW |
377 | * @param int $rgid |
378 | * @param int $gid | |
e23e26ec | 379 | * @param array $criteria |
380 | * Additional criteria to filter by. | |
7a9ab499 | 381 | * |
3058f4d9 | 382 | * @param bool $checkPermissions |
383 | * Respect logged in user's permissions. | |
384 | * | |
21a95d83 | 385 | * @param int $searchLimit |
386 | * Limit for the number of contacts to be used for comparison. | |
387 | * The search methodology finds all matches for the searchedContacts so this limits | |
388 | * the number of searched contacts, not the matches found. | |
389 | * | |
3058f4d9 | 390 | * @throws \CRM_Core_Exception |
391 | * @throws \CiviCRM_API3_Exception | |
b5c2afd0 | 392 | */ |
e1e24a57 | 393 | public static function refillCache($rgid, $gid, $criteria, $checkPermissions, $searchLimit = 0) { |
997a03fe | 394 | $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid, $criteria, $checkPermissions, $searchLimit); |
6a488035 TO |
395 | |
396 | // 1. Clear cache if any | |
783b4b21 | 397 | $sql = "DELETE FROM civicrm_prevnext_cache WHERE cachekey LIKE %1"; |
be2fb01f | 398 | CRM_Core_DAO::executeQuery($sql, [1 => ["{$cacheKeyString}%", 'String']]); |
6a488035 TO |
399 | |
400 | // FIXME: we need to start using temp tables / queries here instead of arrays. | |
401 | // And cleanup code in CRM/Contact/Page/DedupeFind.php | |
402 | ||
403 | // 2. FILL cache | |
be2fb01f | 404 | $foundDupes = []; |
6a488035 | 405 | if ($rgid && $gid) { |
21a95d83 | 406 | $foundDupes = CRM_Dedupe_Finder::dupesInGroup($rgid, $gid, $searchLimit); |
6a488035 TO |
407 | } |
408 | elseif ($rgid) { | |
be2fb01f | 409 | $contactIDs = []; |
88251439 | 410 | // The thing we really need to filter out is any chaining that would 'DO SOMETHING' to the DB. |
411 | // criteria could be passed in via url so we want to ensure nothing could be in that url that | |
412 | // would chain to a delete. Limiting to getfields for 'get' limits us to declared fields, | |
413 | // although we might wish to revisit later to allow joins. | |
414 | $validFieldsForRetrieval = civicrm_api3('Contact', 'getfields', ['action' => 'get'])['values']; | |
22912bef | 415 | $filteredCriteria = isset($criteria['contact']) ? array_intersect_key($criteria['contact'], $validFieldsForRetrieval) : []; |
416 | ||
417 | if (!empty($criteria) || !empty($searchLimit)) { | |
88251439 | 418 | $contacts = civicrm_api3('Contact', 'get', array_merge([ |
22912bef | 419 | 'options' => ['limit' => $searchLimit], |
88251439 | 420 | 'return' => 'id', |
421 | 'check_permissions' => TRUE, | |
22912bef | 422 | 'contact_type' => civicrm_api3('RuleGroup', 'getvalue', ['id' => $rgid, 'return' => 'contact_type']), |
423 | ], $filteredCriteria)); | |
e23e26ec | 424 | $contactIDs = array_keys($contacts['values']); |
22912bef | 425 | |
426 | if (empty($contactIDs)) { | |
427 | // If there is criteria but no contacts were found then we should return now | |
428 | // since we have no contacts to match. | |
429 | return []; | |
430 | } | |
e23e26ec | 431 | } |
22912bef | 432 | $foundDupes = CRM_Dedupe_Finder::dupes($rgid, $contactIDs, $checkPermissions); |
6a488035 TO |
433 | } |
434 | ||
435 | if (!empty($foundDupes)) { | |
1719073d | 436 | CRM_Dedupe_Finder::parseAndStoreDupePairs($foundDupes, $cacheKeyString); |
6a488035 TO |
437 | } |
438 | } | |
439 | ||
b3e4463a | 440 | /** |
441 | * Old function to clean up he cache. | |
442 | * | |
443 | * @deprecated. | |
444 | */ | |
00be9182 | 445 | public static function cleanupCache() { |
b3e4463a | 446 | CRM_Core_Error::deprecatedFunctionWarning('Deprecated function'); |
435fc552 | 447 | Civi::service('prevnext')->cleanup(); |
6a488035 TO |
448 | } |
449 | ||
6a488035 | 450 | /** |
fe482240 | 451 | * Get the selections. |
6a488035 | 452 | * |
b7994703 TO |
453 | * NOTE: This stub has been preserved because one extension in `universe` |
454 | * was referencing the function. | |
77b97be7 | 455 | * |
b7994703 TO |
456 | * @deprecated |
457 | * @see CRM_Core_PrevNextCache_Sql::getSelection() | |
6a488035 | 458 | */ |
40ddbe99 | 459 | public static function getSelection($cacheKey, $action = 'get') { |
b3e4463a | 460 | CRM_Core_Error::deprecatedFunctionWarning('Deprecated function'); |
40ddbe99 | 461 | return Civi::service('prevnext')->getSelection($cacheKey, $action); |
6a488035 TO |
462 | } |
463 | ||
b5c2afd0 | 464 | /** |
808c05a9 | 465 | * Flip 2 contacts in the prevNext cache. |
466 | * | |
467 | * @param array $prevNextId | |
468 | * @param bool $onlySelected | |
469 | * Only flip those which have been marked as selected. | |
470 | */ | |
471 | public static function flipPair(array $prevNextId, $onlySelected) { | |
472 | $dao = new CRM_Core_DAO_PrevNextCache(); | |
473 | if ($onlySelected) { | |
474 | $dao->is_selected = 1; | |
475 | } | |
476 | foreach ($prevNextId as $id) { | |
477 | $dao->id = $id; | |
478 | if ($dao->find(TRUE)) { | |
f24846d5 | 479 | $originalData = CRM_Utils_String::unserialize($dao->data); |
be2fb01f CW |
480 | $srcFields = ['ID', 'Name']; |
481 | $swapFields = ['srcID', 'srcName', 'dstID', 'dstName']; | |
808c05a9 | 482 | $data = array_diff_assoc($originalData, array_fill_keys($swapFields, 1)); |
483 | foreach ($srcFields as $key) { | |
484 | $data['src' . $key] = $originalData['dst' . $key]; | |
485 | $data['dst' . $key] = $originalData['src' . $key]; | |
486 | } | |
487 | $dao->data = serialize($data); | |
e67dcaf8 | 488 | $dao->entity_id1 = $data['dstID']; |
489 | $dao->entity_id2 = $data['srcID']; | |
808c05a9 | 490 | $dao->save(); |
491 | } | |
492 | } | |
493 | } | |
494 | ||
e28bf654 TO |
495 | /** |
496 | * Get a list of available backend services. | |
497 | * | |
498 | * @return array | |
499 | * Array(string $id => string $label). | |
500 | */ | |
501 | public static function getPrevNextBackends() { | |
502 | return [ | |
503 | 'default' => ts('Default (Auto-detect)'), | |
504 | 'sql' => ts('SQL'), | |
505 | 'redis' => ts('Redis'), | |
506 | ]; | |
507 | } | |
508 | ||
e13fa54b | 509 | /** |
510 | * Generate and assign an arbitrary value to a field of a test object. | |
511 | * | |
512 | * This specifically supports testing the dedupe use case. | |
513 | * | |
514 | * @param string $fieldName | |
515 | * @param array $fieldDef | |
516 | * @param int $counter | |
517 | * The globally-unique ID of the test object. | |
b3e4463a | 518 | * |
519 | * @throws \CRM_Core_Exception | |
e13fa54b | 520 | */ |
521 | protected function assignTestValue($fieldName, &$fieldDef, $counter) { | |
783b4b21 SL |
522 | if ($fieldName === 'cachekey') { |
523 | $this->cachekey = 'merge_' . rand(); | |
e13fa54b | 524 | return; |
525 | } | |
526 | if ($fieldName === 'data') { | |
527 | $this->data = serialize([]); | |
528 | return; | |
529 | } | |
530 | parent::assignTestValue($fieldName, $fieldDef, $counter); | |
531 | } | |
532 | ||
6a488035 | 533 | } |