Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
e70a7fc0 | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
fa938177 | 31 | * @copyright CiviCRM LLC (c) 2004-2016 |
6a488035 TO |
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 | ||
b5c2afd0 EM |
41 | /** |
42 | * @param $cacheKey | |
43 | * @param $id1 | |
44 | * @param $id2 | |
100fef9d | 45 | * @param int $mergeId |
e60f24eb TO |
46 | * @param NULL $join |
47 | * @param NULL $where | |
b5c2afd0 EM |
48 | * @param bool $flip |
49 | * | |
50 | * @return array | |
51 | */ | |
00be9182 | 52 | public static function getPositions($cacheKey, $id1, $id2, &$mergeId = NULL, $join = NULL, $where = NULL, $flip = FALSE) { |
6a488035 TO |
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 | ||
6ea503d4 TO |
67 | $params = array( |
68 | 1 => array($id1, 'Integer'), | |
69 | 2 => array($id2, 'Integer'), | |
70 | 3 => array($cacheKey, 'String'), | |
6a488035 TO |
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 | } | |
6ea503d4 TO |
85 | $p = array( |
86 | 1 => array($mergeId, 'Integer'), | |
87 | 2 => array($cacheKey, 'String'), | |
6a488035 | 88 | ); |
353ffa53 | 89 | $sql = "SELECT pn.id, pn.entity_id1, pn.entity_id2, pn.data FROM civicrm_prevnext_cache pn {$join} "; |
6a488035 | 90 | $wherePrev = " WHERE pn.id < %1 AND pn.cacheKey = %2 {$where} ORDER BY ID DESC LIMIT 1"; |
353ffa53 | 91 | $sqlPrev = $sql . $wherePrev; |
6a488035 | 92 | |
6a488035 TO |
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 | ||
b5c2afd0 | 115 | /** |
100fef9d | 116 | * @param int $id |
e60f24eb | 117 | * @param NULL $cacheKey |
b5c2afd0 EM |
118 | * @param string $entityTable |
119 | */ | |
00be9182 | 120 | public static function deleteItem($id = NULL, $cacheKey = NULL, $entityTable = 'civicrm_contact') { |
6a488035 TO |
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 | ||
b5c2afd0 EM |
138 | /** |
139 | * @param $id1 | |
140 | * @param $id2 | |
e60f24eb | 141 | * @param NULL $cacheKey |
b5c2afd0 EM |
142 | * @param bool $isViceVersa |
143 | * @param string $entityTable | |
144 | */ | |
00be9182 | 145 | public static function deletePair($id1, $id2, $cacheKey = NULL, $isViceVersa = FALSE, $entityTable = 'civicrm_contact') { |
6a488035 TO |
146 | $sql = "DELETE FROM civicrm_prevnext_cache WHERE entity_table = %1"; |
147 | $params = array(1 => array($entityTable, 'String')); | |
148 | ||
ae5ffbb7 | 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)"; |
6a488035 TO |
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"; | |
63ef778e | 156 | $params[4] = array("{$cacheKey}%", 'String'); // used % to address any row with conflict-cacheKey e.g "merge Individual_8_0_conflicts" |
6a488035 TO |
157 | } |
158 | ||
159 | CRM_Core_DAO::executeQuery($sql, $params); | |
160 | } | |
161 | ||
f931b74c | 162 | public static function markConflict($id1, $id2, $cacheKey, $conflicts) { |
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 | |
63ef778e | 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); | |
fd630ef9 | 184 | $data['conflicts'] = implode(",", array_values($conflicts)); |
63ef778e | 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 | ||
b5c2afd0 | 198 | /** |
ad37ac8e | 199 | * Retrieve from prev-next cache. |
200 | * | |
66eceb0b | 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 | * | |
ad37ac8e | 207 | * @param string $cacheKey |
208 | * @param string $join | |
66eceb0b | 209 | * @param string $whereClause |
b5c2afd0 EM |
210 | * @param int $offset |
211 | * @param int $rowCount | |
66eceb0b | 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. | |
b5c2afd0 EM |
217 | * |
218 | * @return array | |
219 | */ | |
ed92673b | 220 | public static function retrieve($cacheKey, $join = NULL, $whereClause = NULL, $offset = 0, $rowCount = 0, $select = array(), $orderByClause = '', $includeConflicts = TRUE, $params = array()) { |
63ef778e | 221 | $selectString = 'pn.*'; |
66eceb0b | 222 | |
f931b74c | 223 | if (!empty($select)) { |
63ef778e | 224 | $aliasArray = array(); |
225 | foreach ($select as $column => $alias) { | |
f931b74c | 226 | $aliasArray[] = $column . ' as ' . $alias; |
63ef778e | 227 | } |
f931b74c | 228 | $selectString .= " , " . implode(' , ', $aliasArray); |
63ef778e | 229 | } |
66eceb0b | 230 | |
63ef778e | 231 | $params = array( |
232 | 1 => array($cacheKey, 'String'), | |
ed92673b | 233 | ) + $params; |
6a488035 | 234 | |
66eceb0b | 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; | |
6a488035 TO |
244 | } |
245 | ||
66eceb0b | 246 | $query = " |
247 | SELECT SQL_CALC_FOUND_ROWS {$selectString} | |
248 | FROM civicrm_prevnext_cache pn | |
249 | {$join} | |
250 | $where | |
251 | $orderByClause | |
252 | "; | |
253 | ||
6a488035 | 254 | if ($rowCount) { |
bf00d1b6 DL |
255 | $offset = CRM_Utils_Type::escape($offset, 'Int'); |
256 | $rowCount = CRM_Utils_Type::escape($rowCount, 'Int'); | |
257 | ||
6a488035 TO |
258 | $query .= " LIMIT {$offset}, {$rowCount}"; |
259 | } | |
260 | ||
261 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
262 | ||
63ef778e | 263 | $main = array(); |
264 | $count = 0; | |
6a488035 TO |
265 | while ($dao->fetch()) { |
266 | if (self::is_serialized($dao->data)) { | |
63ef778e | 267 | $main[$count] = unserialize($dao->data); |
6a488035 TO |
268 | } |
269 | else { | |
63ef778e | 270 | $main[$count] = $dao->data; |
6a488035 | 271 | } |
63ef778e | 272 | |
273 | if (!empty($select)) { | |
274 | $extraData = array(); | |
275 | foreach ($select as $dfield => $sfield) { | |
276 | $extraData[$sfield] = $dao->$sfield; | |
277 | } | |
278 | $main[$count] = array( | |
f931b74c | 279 | 'prevnext_id' => $dao->id, |
280 | 'is_selected' => $dao->is_selected, | |
281 | 'entity_id1' => $dao->entity_id1, | |
282 | 'entity_id2' => $dao->entity_id2, | |
63ef778e | 283 | 'data' => $main[$count], |
284 | ); | |
285 | $main[$count] = array_merge($main[$count], $extraData); | |
6a488035 | 286 | } |
63ef778e | 287 | $count++; |
6a488035 TO |
288 | } |
289 | ||
290 | return $main; | |
291 | } | |
292 | ||
b5c2afd0 EM |
293 | /** |
294 | * @param $string | |
295 | * | |
296 | * @return bool | |
297 | */ | |
6a488035 | 298 | public static function is_serialized($string) { |
ab8a593e | 299 | return (@unserialize($string) !== FALSE); |
6a488035 TO |
300 | } |
301 | ||
b5c2afd0 EM |
302 | /** |
303 | * @param $values | |
304 | */ | |
00be9182 | 305 | public static function setItem($values) { |
6a488035 TO |
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 | ||
b5c2afd0 EM |
313 | /** |
314 | * @param $cacheKey | |
ed92673b | 315 | * @param string $join |
316 | * @param string $where | |
b5c2afd0 | 317 | * @param string $op |
ed92673b | 318 | * @param array $params |
319 | * Extra query params to parse into the query. | |
b5c2afd0 EM |
320 | * |
321 | * @return int | |
322 | */ | |
ed92673b | 323 | public static function getCount($cacheKey, $join = NULL, $where = NULL, $op = "=", $params = array()) { |
6a488035 TO |
324 | $query = " |
325 | SELECT COUNT(*) FROM civicrm_prevnext_cache pn | |
326 | {$join} | |
63ef778e | 327 | WHERE (pn.cacheKey $op %1 OR pn.cacheKey $op %2) |
6a488035 TO |
328 | "; |
329 | if ($where) { | |
330 | $query .= " AND {$where}"; | |
331 | } | |
332 | ||
63ef778e | 333 | $params = array( |
334 | 1 => array($cacheKey, 'String'), | |
f931b74c | 335 | 2 => array("{$cacheKey}_conflicts", 'String'), |
ed92673b | 336 | ) + $params; |
64951b63 | 337 | return (int) CRM_Core_DAO::singleValueQuery($query, $params, TRUE, FALSE); |
6a488035 TO |
338 | } |
339 | ||
b5c2afd0 | 340 | /** |
e23e26ec | 341 | * Repopulate the cache of merge prospects. |
342 | * | |
100fef9d CW |
343 | * @param int $rgid |
344 | * @param int $gid | |
e60f24eb | 345 | * @param NULL $cacheKeyString |
e23e26ec | 346 | * @param array $criteria |
347 | * Additional criteria to filter by. | |
7a9ab499 | 348 | * |
3058f4d9 | 349 | * @param bool $checkPermissions |
350 | * Respect logged in user's permissions. | |
351 | * | |
7a9ab499 | 352 | * @return bool |
3058f4d9 | 353 | * @throws \CRM_Core_Exception |
354 | * @throws \CiviCRM_API3_Exception | |
b5c2afd0 | 355 | */ |
3058f4d9 | 356 | public static function refillCache($rgid, $gid, $cacheKeyString, $criteria, $checkPermissions) { |
6a488035 | 357 | if (!$cacheKeyString && $rgid) { |
3058f4d9 | 358 | $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid, $criteria, $checkPermissions); |
6a488035 TO |
359 | } |
360 | ||
361 | if (!$cacheKeyString) { | |
362 | return FALSE; | |
363 | } | |
364 | ||
365 | // 1. Clear cache if any | |
366 | $sql = "DELETE FROM civicrm_prevnext_cache WHERE cacheKey LIKE %1"; | |
367 | CRM_Core_DAO::executeQuery($sql, array(1 => array("{$cacheKeyString}%", 'String'))); | |
368 | ||
369 | // FIXME: we need to start using temp tables / queries here instead of arrays. | |
370 | // And cleanup code in CRM/Contact/Page/DedupeFind.php | |
371 | ||
372 | // 2. FILL cache | |
373 | $foundDupes = array(); | |
374 | if ($rgid && $gid) { | |
375 | $foundDupes = CRM_Dedupe_Finder::dupesInGroup($rgid, $gid); | |
376 | } | |
377 | elseif ($rgid) { | |
e23e26ec | 378 | $contactIDs = array(); |
379 | if (!empty($criteria)) { | |
380 | $contacts = civicrm_api3('Contact', 'get', array_merge(array('options' => array('limit' => 0), 'return' => 'id'), $criteria['contact'])); | |
381 | $contactIDs = array_keys($contacts['values']); | |
382 | } | |
3058f4d9 | 383 | $foundDupes = CRM_Dedupe_Finder::dupes($rgid, $contactIDs, $checkPermissions); |
6a488035 TO |
384 | } |
385 | ||
386 | if (!empty($foundDupes)) { | |
1719073d | 387 | CRM_Dedupe_Finder::parseAndStoreDupePairs($foundDupes, $cacheKeyString); |
6a488035 TO |
388 | } |
389 | } | |
390 | ||
00be9182 | 391 | public static function cleanupCache() { |
6a488035 TO |
392 | // clean up all prev next caches older than $cacheTimeIntervalDays days |
393 | $cacheTimeIntervalDays = 2; | |
394 | ||
395 | // first find all the cacheKeys that match this | |
396 | $sql = " | |
397 | DELETE pn, c | |
398 | FROM civicrm_cache c | |
399 | INNER JOIN civicrm_prevnext_cache pn ON c.path = pn.cacheKey | |
400 | WHERE c.group_name = %1 | |
401 | AND c.created_date < date_sub( NOW( ), INTERVAL %2 day ) | |
402 | "; | |
403 | $params = array( | |
404 | 1 => array('CiviCRM Search PrevNextCache', 'String'), | |
405 | 2 => array($cacheTimeIntervalDays, 'Integer'), | |
406 | ); | |
407 | CRM_Core_DAO::executeQuery($sql, $params); | |
408 | } | |
409 | ||
b5c2afd0 | 410 | /** |
fe482240 | 411 | * Save checkbox selections. |
c490a46a | 412 | * |
b5c2afd0 EM |
413 | * @param $cacheKey |
414 | * @param string $action | |
c490a46a | 415 | * @param array $cIds |
b5c2afd0 EM |
416 | * @param string $entity_table |
417 | */ | |
00be9182 | 418 | public static function markSelection($cacheKey, $action = 'unselect', $cIds = NULL, $entity_table = 'civicrm_contact') { |
6a488035 TO |
419 | if (!$cacheKey) { |
420 | return; | |
421 | } | |
422 | $params = array(); | |
423 | ||
424 | $entity_whereClause = " AND entity_table = '{$entity_table}'"; | |
425 | if ($cIds && $cacheKey && $action) { | |
426 | if (is_array($cIds)) { | |
427 | $cIdFilter = "(" . implode(',', $cIds) . ")"; | |
428 | $whereClause = " | |
429 | WHERE cacheKey LIKE %1 | |
430 | AND (entity_id1 IN {$cIdFilter} OR entity_id2 IN {$cIdFilter}) | |
431 | "; | |
432 | } | |
433 | else { | |
434 | $whereClause = " | |
435 | WHERE cacheKey LIKE %1 | |
436 | AND (entity_id1 = %2 OR entity_id2 = %2) | |
437 | "; | |
438 | $params[2] = array("{$cIds}", 'Integer'); | |
439 | } | |
440 | if ($action == 'select') { | |
441 | $whereClause .= "AND is_selected = 0"; | |
442 | $sql = "UPDATE civicrm_prevnext_cache SET is_selected = 1 {$whereClause} {$entity_whereClause}"; | |
55488e61 | 443 | $params[1] = array("{$cacheKey}%", 'String'); |
6a488035 TO |
444 | } |
445 | elseif ($action == 'unselect') { | |
446 | $whereClause .= "AND is_selected = 1"; | |
447 | $sql = "UPDATE civicrm_prevnext_cache SET is_selected = 0 {$whereClause} {$entity_whereClause}"; | |
448 | $params[1] = array("%{$cacheKey}%", 'String'); | |
449 | } | |
450 | // default action is reseting | |
451 | } | |
452 | elseif (!$cIds && $cacheKey && $action == 'unselect') { | |
453 | $sql = " | |
454 | UPDATE civicrm_prevnext_cache | |
455 | SET is_selected = 0 | |
456 | WHERE cacheKey LIKE %1 AND is_selected = 1 | |
457 | {$entity_whereClause} | |
458 | "; | |
55488e61 | 459 | $params[1] = array("{$cacheKey}%", 'String'); |
6a488035 TO |
460 | } |
461 | CRM_Core_DAO::executeQuery($sql, $params); | |
462 | } | |
463 | ||
464 | /** | |
fe482240 | 465 | * Get the selections. |
6a488035 | 466 | * |
6a0b768e TO |
467 | * @param string $cacheKey |
468 | * Cache key. | |
469 | * @param string $action | |
470 | * Action. | |
6a488035 TO |
471 | * $action : get - get only selection records |
472 | * getall - get all the records of the specified cache key | |
6a0b768e TO |
473 | * @param string $entity_table |
474 | * Entity table. | |
77b97be7 | 475 | * |
ae5ffbb7 | 476 | * @return array|NULL |
6a488035 | 477 | */ |
00be9182 | 478 | public static function getSelection($cacheKey, $action = 'get', $entity_table = 'civicrm_contact') { |
6a488035 | 479 | if (!$cacheKey) { |
ae5ffbb7 | 480 | return NULL; |
6a488035 TO |
481 | } |
482 | $params = array(); | |
483 | ||
484 | $entity_whereClause = " AND entity_table = '{$entity_table}'"; | |
485 | if ($cacheKey && ($action == 'get' || $action == 'getall')) { | |
486 | $actionGet = ($action == "get") ? " AND is_selected = 1 " : ""; | |
487 | $sql = " | |
488 | SELECT entity_id1, entity_id2 FROM civicrm_prevnext_cache | |
489 | WHERE cacheKey LIKE %1 | |
490 | $actionGet | |
491 | $entity_whereClause | |
492 | ORDER BY id | |
493 | "; | |
55488e61 | 494 | $params[1] = array("{$cacheKey}%", 'String'); |
6a488035 TO |
495 | |
496 | $contactIds = array($cacheKey => array()); | |
497 | $cIdDao = CRM_Core_DAO::executeQuery($sql, $params); | |
498 | while ($cIdDao->fetch()) { | |
499 | if ($cIdDao->entity_id1 == $cIdDao->entity_id2) { | |
500 | $contactIds[$cacheKey][$cIdDao->entity_id1] = 1; | |
501 | } | |
502 | } | |
503 | return $contactIds; | |
504 | } | |
505 | } | |
506 | ||
b5c2afd0 EM |
507 | /** |
508 | * @return array | |
509 | */ | |
00be9182 | 510 | public static function getSelectedContacts() { |
6a488035 TO |
511 | $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String'); |
512 | $cacheKey = "civicrm search {$qfKey}"; | |
513 | $query = " | |
514 | SELECT * | |
515 | FROM civicrm_prevnext_cache | |
516 | WHERE cacheKey LIKE %1 | |
517 | AND is_selected=1 | |
518 | AND cacheKey NOT LIKE %2 | |
519 | "; | |
55488e61 DL |
520 | $params1[1] = array("{$cacheKey}%", 'String'); |
521 | $params1[2] = array("{$cacheKey}_alphabet%", 'String'); | |
6a488035 TO |
522 | $dao = CRM_Core_DAO::executeQuery($query, $params1); |
523 | ||
481a74f4 | 524 | $val = array(); |
6a488035 TO |
525 | while ($dao->fetch()) { |
526 | $val[] = $dao->data; | |
527 | } | |
528 | return $val; | |
529 | } | |
530 | ||
b5c2afd0 | 531 | /** |
c490a46a CW |
532 | * @param CRM_Core_Form $form |
533 | * @param array $params | |
b5c2afd0 EM |
534 | * |
535 | * @return mixed | |
536 | */ | |
00be9182 | 537 | public static function buildSelectedContactPager(&$form, &$params) { |
6a488035 TO |
538 | $params['status'] = ts('Contacts %%StatusMessage%%'); |
539 | $params['csvString'] = NULL; | |
540 | $params['buttonTop'] = 'PagerTopButton'; | |
541 | $params['buttonBottom'] = 'PagerBottomButton'; | |
c490a46a | 542 | $params['rowCount'] = $form->get(CRM_Utils_Pager::PAGE_ROWCOUNT); |
6a488035 TO |
543 | |
544 | if (!$params['rowCount']) { | |
545 | $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT; | |
546 | } | |
547 | ||
353ffa53 | 548 | $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $form); |
6a488035 TO |
549 | $cacheKey = "civicrm search {$qfKey}"; |
550 | ||
551 | $query = " | |
552 | SELECT count(id) | |
553 | FROM civicrm_prevnext_cache | |
554 | WHERE cacheKey LIKE %1 | |
555 | AND is_selected = 1 | |
556 | AND cacheKey NOT LIKE %2 | |
557 | "; | |
55488e61 DL |
558 | $params1[1] = array("{$cacheKey}%", 'String'); |
559 | $params1[2] = array("{$cacheKey}_alphabet%", 'String'); | |
c490a46a | 560 | $paramsTotal = CRM_Core_DAO::singleValueQuery($query, $params1); |
6a488035 | 561 | $params['total'] = $paramsTotal; |
c490a46a CW |
562 | $form->_pager = new CRM_Utils_Pager($params); |
563 | $form->assign_by_ref('pager', $form->_pager); | |
564 | list($offset, $rowCount) = $form->_pager->getOffsetAndRowCount(); | |
6a488035 TO |
565 | $params['offset'] = $offset; |
566 | $params['rowCount1'] = $rowCount; | |
567 | return $params; | |
568 | } | |
96025800 | 569 | |
808c05a9 | 570 | /** |
571 | * Flip 2 contacts in the prevNext cache. | |
572 | * | |
573 | * @param array $prevNextId | |
574 | * @param bool $onlySelected | |
575 | * Only flip those which have been marked as selected. | |
576 | */ | |
577 | public static function flipPair(array $prevNextId, $onlySelected) { | |
578 | $dao = new CRM_Core_DAO_PrevNextCache(); | |
579 | if ($onlySelected) { | |
580 | $dao->is_selected = 1; | |
581 | } | |
582 | foreach ($prevNextId as $id) { | |
583 | $dao->id = $id; | |
584 | if ($dao->find(TRUE)) { | |
585 | $originalData = unserialize($dao->data); | |
586 | $srcFields = array('ID', 'Name'); | |
587 | $swapFields = array('srcID', 'srcName', 'dstID', 'dstName'); | |
588 | $data = array_diff_assoc($originalData, array_fill_keys($swapFields, 1)); | |
589 | foreach ($srcFields as $key) { | |
590 | $data['src' . $key] = $originalData['dst' . $key]; | |
591 | $data['dst' . $key] = $originalData['src' . $key]; | |
592 | } | |
593 | $dao->data = serialize($data); | |
e67dcaf8 | 594 | $dao->entity_id1 = $data['dstID']; |
595 | $dao->entity_id2 = $data['srcID']; | |
808c05a9 | 596 | $dao->save(); |
597 | } | |
598 | } | |
599 | } | |
600 | ||
6a488035 | 601 | } |