Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
f47539f6 | 35 | * BAO object for civicrm_prevnext_cache table. |
6a488035 TO |
36 | */ |
37 | class CRM_Core_BAO_PrevNextCache extends CRM_Core_DAO_PrevNextCache { | |
38 | ||
b5c2afd0 | 39 | /** |
f47539f6 | 40 | * Get the previous and next keys. |
41 | * | |
42 | * @param string $cacheKey | |
43 | * @param int $id1 | |
44 | * @param int $id2 | |
100fef9d | 45 | * @param int $mergeId |
f47539f6 | 46 | * @param string $join |
47 | * @param string $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 | /** |
f47539f6 | 116 | * Delete an item from the prevnext cache table based on the entity. |
117 | * | |
100fef9d | 118 | * @param int $id |
f47539f6 | 119 | * @param string $cacheKey |
b5c2afd0 EM |
120 | * @param string $entityTable |
121 | */ | |
00be9182 | 122 | public static function deleteItem($id = NULL, $cacheKey = NULL, $entityTable = 'civicrm_contact') { |
6a488035 TO |
123 | |
124 | //clear cache | |
125 | $sql = "DELETE FROM civicrm_prevnext_cache WHERE entity_table = %1"; | |
126 | $params = array(1 => array($entityTable, 'String')); | |
127 | ||
128 | if (is_numeric($id)) { | |
129 | $sql .= " AND ( entity_id1 = %2 OR entity_id2 = %2 )"; | |
130 | $params[2] = array($id, 'Integer'); | |
131 | } | |
132 | ||
133 | if (isset($cacheKey)) { | |
134 | $sql .= " AND cacheKey LIKE %3"; | |
135 | $params[3] = array("{$cacheKey}%", 'String'); | |
136 | } | |
137 | CRM_Core_DAO::executeQuery($sql, $params); | |
138 | } | |
139 | ||
b5c2afd0 | 140 | /** |
f47539f6 | 141 | * Delete from the previous next cache table for a pair of ids. |
142 | * | |
143 | * @param int $id1 | |
144 | * @param int $id2 | |
145 | * @param string $cacheKey | |
b5c2afd0 EM |
146 | * @param bool $isViceVersa |
147 | * @param string $entityTable | |
148 | */ | |
00be9182 | 149 | public static function deletePair($id1, $id2, $cacheKey = NULL, $isViceVersa = FALSE, $entityTable = 'civicrm_contact') { |
6a488035 TO |
150 | $sql = "DELETE FROM civicrm_prevnext_cache WHERE entity_table = %1"; |
151 | $params = array(1 => array($entityTable, 'String')); | |
152 | ||
ae5ffbb7 | 153 | $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 |
154 | $sql .= " AND ( {$pair} )"; |
155 | $params[2] = array($id1, 'Integer'); | |
156 | $params[3] = array($id2, 'Integer'); | |
157 | ||
158 | if (isset($cacheKey)) { | |
159 | $sql .= " AND cacheKey LIKE %4"; | |
63ef778e | 160 | $params[4] = array("{$cacheKey}%", 'String'); // used % to address any row with conflict-cacheKey e.g "merge Individual_8_0_conflicts" |
6a488035 TO |
161 | } |
162 | ||
163 | CRM_Core_DAO::executeQuery($sql, $params); | |
164 | } | |
165 | ||
f2ac86d1 | 166 | /** |
167 | * Mark contacts as being in conflict. | |
168 | * | |
169 | * @param int $id1 | |
170 | * @param int $id2 | |
171 | * @param string $cacheKey | |
172 | * @param array $conflicts | |
173 | * | |
174 | * @return bool | |
175 | */ | |
f931b74c | 176 | public static function markConflict($id1, $id2, $cacheKey, $conflicts) { |
63ef778e | 177 | if (empty($cacheKey) || empty($conflicts)) { |
178 | return FALSE; | |
179 | } | |
180 | ||
ad37ac8e | 181 | $sql = "SELECT pn.* |
63ef778e | 182 | FROM civicrm_prevnext_cache pn |
ad37ac8e | 183 | WHERE |
184 | ((pn.entity_id1 = %1 AND pn.entity_id2 = %2) OR (pn.entity_id1 = %2 AND pn.entity_id2 = %1)) AND | |
63ef778e | 185 | (cacheKey = %3 OR cacheKey = %4)"; |
186 | $params = array( | |
187 | 1 => array($id1, 'Integer'), | |
188 | 2 => array($id2, 'Integer'), | |
189 | 3 => array("{$cacheKey}", 'String'), | |
190 | 4 => array("{$cacheKey}_conflicts", 'String'), | |
191 | ); | |
192 | $pncFind = CRM_Core_DAO::executeQuery($sql, $params); | |
193 | ||
194 | while ($pncFind->fetch()) { | |
195 | $data = $pncFind->data; | |
196 | if (!empty($data)) { | |
197 | $data = unserialize($data); | |
fd630ef9 | 198 | $data['conflicts'] = implode(",", array_values($conflicts)); |
63ef778e | 199 | |
200 | $pncUp = new CRM_Core_DAO_PrevNextCache(); | |
201 | $pncUp->id = $pncFind->id; | |
202 | if ($pncUp->find(TRUE)) { | |
203 | $pncUp->data = serialize($data); | |
204 | $pncUp->cacheKey = "{$cacheKey}_conflicts"; | |
205 | $pncUp->save(); | |
206 | } | |
207 | } | |
208 | } | |
209 | return TRUE; | |
210 | } | |
211 | ||
b5c2afd0 | 212 | /** |
ad37ac8e | 213 | * Retrieve from prev-next cache. |
214 | * | |
66eceb0b | 215 | * This function is used from a variety of merge related functions, although |
216 | * it would probably be good to converge on calling CRM_Dedupe_Merger::getDuplicatePairs. | |
217 | * | |
218 | * We seem to currently be storing stats in this table too & they might make more sense in | |
219 | * the main cache table. | |
220 | * | |
ad37ac8e | 221 | * @param string $cacheKey |
222 | * @param string $join | |
66eceb0b | 223 | * @param string $whereClause |
b5c2afd0 EM |
224 | * @param int $offset |
225 | * @param int $rowCount | |
66eceb0b | 226 | * @param array $select |
227 | * @param string $orderByClause | |
228 | * @param bool $includeConflicts | |
229 | * Should we return rows that have already been idenfified as having a conflict. | |
230 | * When this is TRUE you should be careful you do not set up a loop. | |
f47539f6 | 231 | * @param array $params |
b5c2afd0 EM |
232 | * |
233 | * @return array | |
234 | */ | |
ed92673b | 235 | public static function retrieve($cacheKey, $join = NULL, $whereClause = NULL, $offset = 0, $rowCount = 0, $select = array(), $orderByClause = '', $includeConflicts = TRUE, $params = array()) { |
63ef778e | 236 | $selectString = 'pn.*'; |
66eceb0b | 237 | |
f931b74c | 238 | if (!empty($select)) { |
63ef778e | 239 | $aliasArray = array(); |
240 | foreach ($select as $column => $alias) { | |
f931b74c | 241 | $aliasArray[] = $column . ' as ' . $alias; |
63ef778e | 242 | } |
f931b74c | 243 | $selectString .= " , " . implode(' , ', $aliasArray); |
63ef778e | 244 | } |
66eceb0b | 245 | |
63ef778e | 246 | $params = array( |
247 | 1 => array($cacheKey, 'String'), | |
ed92673b | 248 | ) + $params; |
6a488035 | 249 | |
66eceb0b | 250 | if (!empty($whereClause)) { |
251 | $whereClause = " AND " . $whereClause; | |
252 | } | |
253 | if ($includeConflicts) { | |
254 | $where = ' WHERE (pn.cacheKey = %1 OR pn.cacheKey = %2)' . $whereClause; | |
255 | $params[2] = array("{$cacheKey}_conflicts", 'String'); | |
256 | } | |
257 | else { | |
258 | $where = ' WHERE (pn.cacheKey = %1)' . $whereClause; | |
6a488035 TO |
259 | } |
260 | ||
66eceb0b | 261 | $query = " |
262 | SELECT SQL_CALC_FOUND_ROWS {$selectString} | |
263 | FROM civicrm_prevnext_cache pn | |
264 | {$join} | |
265 | $where | |
266 | $orderByClause | |
267 | "; | |
268 | ||
6a488035 | 269 | if ($rowCount) { |
bf00d1b6 DL |
270 | $offset = CRM_Utils_Type::escape($offset, 'Int'); |
271 | $rowCount = CRM_Utils_Type::escape($rowCount, 'Int'); | |
272 | ||
6a488035 TO |
273 | $query .= " LIMIT {$offset}, {$rowCount}"; |
274 | } | |
275 | ||
276 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
277 | ||
63ef778e | 278 | $main = array(); |
279 | $count = 0; | |
6a488035 TO |
280 | while ($dao->fetch()) { |
281 | if (self::is_serialized($dao->data)) { | |
63ef778e | 282 | $main[$count] = unserialize($dao->data); |
6a488035 TO |
283 | } |
284 | else { | |
63ef778e | 285 | $main[$count] = $dao->data; |
6a488035 | 286 | } |
63ef778e | 287 | |
288 | if (!empty($select)) { | |
289 | $extraData = array(); | |
bf588234 | 290 | foreach ($select as $sfield) { |
63ef778e | 291 | $extraData[$sfield] = $dao->$sfield; |
292 | } | |
293 | $main[$count] = array( | |
f931b74c | 294 | 'prevnext_id' => $dao->id, |
295 | 'is_selected' => $dao->is_selected, | |
296 | 'entity_id1' => $dao->entity_id1, | |
297 | 'entity_id2' => $dao->entity_id2, | |
63ef778e | 298 | 'data' => $main[$count], |
299 | ); | |
300 | $main[$count] = array_merge($main[$count], $extraData); | |
6a488035 | 301 | } |
63ef778e | 302 | $count++; |
6a488035 TO |
303 | } |
304 | ||
305 | return $main; | |
306 | } | |
307 | ||
b5c2afd0 EM |
308 | /** |
309 | * @param $string | |
310 | * | |
311 | * @return bool | |
312 | */ | |
6a488035 | 313 | public static function is_serialized($string) { |
ab8a593e | 314 | return (@unserialize($string) !== FALSE); |
6a488035 TO |
315 | } |
316 | ||
b5c2afd0 EM |
317 | /** |
318 | * @param $values | |
319 | */ | |
00be9182 | 320 | public static function setItem($values) { |
6a488035 TO |
321 | $insert = "INSERT INTO civicrm_prevnext_cache ( entity_table, entity_id1, entity_id2, cacheKey, data ) VALUES \n"; |
322 | $query = $insert . implode(",\n ", $values); | |
323 | ||
324 | //dump the dedupe matches in the prevnext_cache table | |
325 | CRM_Core_DAO::executeQuery($query); | |
326 | } | |
327 | ||
b5c2afd0 | 328 | /** |
f47539f6 | 329 | * Get count of matching rows. |
330 | * | |
331 | * @param string $cacheKey | |
ed92673b | 332 | * @param string $join |
333 | * @param string $where | |
b5c2afd0 | 334 | * @param string $op |
ed92673b | 335 | * @param array $params |
336 | * Extra query params to parse into the query. | |
b5c2afd0 EM |
337 | * |
338 | * @return int | |
339 | */ | |
ed92673b | 340 | public static function getCount($cacheKey, $join = NULL, $where = NULL, $op = "=", $params = array()) { |
6a488035 TO |
341 | $query = " |
342 | SELECT COUNT(*) FROM civicrm_prevnext_cache pn | |
343 | {$join} | |
63ef778e | 344 | WHERE (pn.cacheKey $op %1 OR pn.cacheKey $op %2) |
6a488035 TO |
345 | "; |
346 | if ($where) { | |
347 | $query .= " AND {$where}"; | |
348 | } | |
349 | ||
63ef778e | 350 | $params = array( |
351 | 1 => array($cacheKey, 'String'), | |
f931b74c | 352 | 2 => array("{$cacheKey}_conflicts", 'String'), |
ed92673b | 353 | ) + $params; |
64951b63 | 354 | return (int) CRM_Core_DAO::singleValueQuery($query, $params, TRUE, FALSE); |
6a488035 TO |
355 | } |
356 | ||
b5c2afd0 | 357 | /** |
e23e26ec | 358 | * Repopulate the cache of merge prospects. |
359 | * | |
100fef9d CW |
360 | * @param int $rgid |
361 | * @param int $gid | |
e60f24eb | 362 | * @param NULL $cacheKeyString |
e23e26ec | 363 | * @param array $criteria |
364 | * Additional criteria to filter by. | |
7a9ab499 | 365 | * |
3058f4d9 | 366 | * @param bool $checkPermissions |
367 | * Respect logged in user's permissions. | |
368 | * | |
7a9ab499 | 369 | * @return bool |
3058f4d9 | 370 | * @throws \CRM_Core_Exception |
371 | * @throws \CiviCRM_API3_Exception | |
b5c2afd0 | 372 | */ |
3058f4d9 | 373 | public static function refillCache($rgid, $gid, $cacheKeyString, $criteria, $checkPermissions) { |
6a488035 | 374 | if (!$cacheKeyString && $rgid) { |
3058f4d9 | 375 | $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid, $criteria, $checkPermissions); |
6a488035 TO |
376 | } |
377 | ||
378 | if (!$cacheKeyString) { | |
379 | return FALSE; | |
380 | } | |
381 | ||
382 | // 1. Clear cache if any | |
383 | $sql = "DELETE FROM civicrm_prevnext_cache WHERE cacheKey LIKE %1"; | |
384 | CRM_Core_DAO::executeQuery($sql, array(1 => array("{$cacheKeyString}%", 'String'))); | |
385 | ||
386 | // FIXME: we need to start using temp tables / queries here instead of arrays. | |
387 | // And cleanup code in CRM/Contact/Page/DedupeFind.php | |
388 | ||
389 | // 2. FILL cache | |
390 | $foundDupes = array(); | |
391 | if ($rgid && $gid) { | |
392 | $foundDupes = CRM_Dedupe_Finder::dupesInGroup($rgid, $gid); | |
393 | } | |
394 | elseif ($rgid) { | |
e23e26ec | 395 | $contactIDs = array(); |
396 | if (!empty($criteria)) { | |
397 | $contacts = civicrm_api3('Contact', 'get', array_merge(array('options' => array('limit' => 0), 'return' => 'id'), $criteria['contact'])); | |
398 | $contactIDs = array_keys($contacts['values']); | |
399 | } | |
3058f4d9 | 400 | $foundDupes = CRM_Dedupe_Finder::dupes($rgid, $contactIDs, $checkPermissions); |
6a488035 TO |
401 | } |
402 | ||
403 | if (!empty($foundDupes)) { | |
1719073d | 404 | CRM_Dedupe_Finder::parseAndStoreDupePairs($foundDupes, $cacheKeyString); |
6a488035 TO |
405 | } |
406 | } | |
407 | ||
00be9182 | 408 | public static function cleanupCache() { |
6a488035 TO |
409 | // clean up all prev next caches older than $cacheTimeIntervalDays days |
410 | $cacheTimeIntervalDays = 2; | |
411 | ||
412 | // first find all the cacheKeys that match this | |
413 | $sql = " | |
414 | DELETE pn, c | |
415 | FROM civicrm_cache c | |
416 | INNER JOIN civicrm_prevnext_cache pn ON c.path = pn.cacheKey | |
417 | WHERE c.group_name = %1 | |
418 | AND c.created_date < date_sub( NOW( ), INTERVAL %2 day ) | |
419 | "; | |
420 | $params = array( | |
421 | 1 => array('CiviCRM Search PrevNextCache', 'String'), | |
422 | 2 => array($cacheTimeIntervalDays, 'Integer'), | |
423 | ); | |
424 | CRM_Core_DAO::executeQuery($sql, $params); | |
425 | } | |
426 | ||
b5c2afd0 | 427 | /** |
fe482240 | 428 | * Save checkbox selections. |
c490a46a | 429 | * |
b5c2afd0 EM |
430 | * @param $cacheKey |
431 | * @param string $action | |
c490a46a | 432 | * @param array $cIds |
b5c2afd0 EM |
433 | * @param string $entity_table |
434 | */ | |
00be9182 | 435 | public static function markSelection($cacheKey, $action = 'unselect', $cIds = NULL, $entity_table = 'civicrm_contact') { |
6a488035 TO |
436 | if (!$cacheKey) { |
437 | return; | |
438 | } | |
439 | $params = array(); | |
440 | ||
441 | $entity_whereClause = " AND entity_table = '{$entity_table}'"; | |
442 | if ($cIds && $cacheKey && $action) { | |
443 | if (is_array($cIds)) { | |
444 | $cIdFilter = "(" . implode(',', $cIds) . ")"; | |
445 | $whereClause = " | |
446 | WHERE cacheKey LIKE %1 | |
447 | AND (entity_id1 IN {$cIdFilter} OR entity_id2 IN {$cIdFilter}) | |
448 | "; | |
449 | } | |
450 | else { | |
451 | $whereClause = " | |
452 | WHERE cacheKey LIKE %1 | |
453 | AND (entity_id1 = %2 OR entity_id2 = %2) | |
454 | "; | |
455 | $params[2] = array("{$cIds}", 'Integer'); | |
456 | } | |
457 | if ($action == 'select') { | |
458 | $whereClause .= "AND is_selected = 0"; | |
459 | $sql = "UPDATE civicrm_prevnext_cache SET is_selected = 1 {$whereClause} {$entity_whereClause}"; | |
55488e61 | 460 | $params[1] = array("{$cacheKey}%", 'String'); |
6a488035 TO |
461 | } |
462 | elseif ($action == 'unselect') { | |
463 | $whereClause .= "AND is_selected = 1"; | |
464 | $sql = "UPDATE civicrm_prevnext_cache SET is_selected = 0 {$whereClause} {$entity_whereClause}"; | |
465 | $params[1] = array("%{$cacheKey}%", 'String'); | |
466 | } | |
467 | // default action is reseting | |
468 | } | |
469 | elseif (!$cIds && $cacheKey && $action == 'unselect') { | |
470 | $sql = " | |
471 | UPDATE civicrm_prevnext_cache | |
472 | SET is_selected = 0 | |
473 | WHERE cacheKey LIKE %1 AND is_selected = 1 | |
474 | {$entity_whereClause} | |
475 | "; | |
55488e61 | 476 | $params[1] = array("{$cacheKey}%", 'String'); |
6a488035 TO |
477 | } |
478 | CRM_Core_DAO::executeQuery($sql, $params); | |
479 | } | |
480 | ||
481 | /** | |
fe482240 | 482 | * Get the selections. |
6a488035 | 483 | * |
6a0b768e TO |
484 | * @param string $cacheKey |
485 | * Cache key. | |
486 | * @param string $action | |
487 | * Action. | |
6a488035 TO |
488 | * $action : get - get only selection records |
489 | * getall - get all the records of the specified cache key | |
6a0b768e TO |
490 | * @param string $entity_table |
491 | * Entity table. | |
77b97be7 | 492 | * |
ae5ffbb7 | 493 | * @return array|NULL |
6a488035 | 494 | */ |
00be9182 | 495 | public static function getSelection($cacheKey, $action = 'get', $entity_table = 'civicrm_contact') { |
6a488035 | 496 | if (!$cacheKey) { |
ae5ffbb7 | 497 | return NULL; |
6a488035 TO |
498 | } |
499 | $params = array(); | |
500 | ||
501 | $entity_whereClause = " AND entity_table = '{$entity_table}'"; | |
502 | if ($cacheKey && ($action == 'get' || $action == 'getall')) { | |
503 | $actionGet = ($action == "get") ? " AND is_selected = 1 " : ""; | |
504 | $sql = " | |
505 | SELECT entity_id1, entity_id2 FROM civicrm_prevnext_cache | |
506 | WHERE cacheKey LIKE %1 | |
507 | $actionGet | |
508 | $entity_whereClause | |
9b5914f2 | 509 | ORDER BY id |
6a488035 | 510 | "; |
55488e61 | 511 | $params[1] = array("{$cacheKey}%", 'String'); |
6a488035 TO |
512 | |
513 | $contactIds = array($cacheKey => array()); | |
514 | $cIdDao = CRM_Core_DAO::executeQuery($sql, $params); | |
515 | while ($cIdDao->fetch()) { | |
516 | if ($cIdDao->entity_id1 == $cIdDao->entity_id2) { | |
517 | $contactIds[$cacheKey][$cIdDao->entity_id1] = 1; | |
518 | } | |
519 | } | |
520 | return $contactIds; | |
521 | } | |
522 | } | |
523 | ||
b5c2afd0 EM |
524 | /** |
525 | * @return array | |
526 | */ | |
00be9182 | 527 | public static function getSelectedContacts() { |
6a488035 TO |
528 | $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String'); |
529 | $cacheKey = "civicrm search {$qfKey}"; | |
530 | $query = " | |
531 | SELECT * | |
532 | FROM civicrm_prevnext_cache | |
533 | WHERE cacheKey LIKE %1 | |
534 | AND is_selected=1 | |
535 | AND cacheKey NOT LIKE %2 | |
536 | "; | |
55488e61 DL |
537 | $params1[1] = array("{$cacheKey}%", 'String'); |
538 | $params1[2] = array("{$cacheKey}_alphabet%", 'String'); | |
6a488035 TO |
539 | $dao = CRM_Core_DAO::executeQuery($query, $params1); |
540 | ||
481a74f4 | 541 | $val = array(); |
6a488035 TO |
542 | while ($dao->fetch()) { |
543 | $val[] = $dao->data; | |
544 | } | |
545 | return $val; | |
546 | } | |
547 | ||
b5c2afd0 | 548 | /** |
c490a46a CW |
549 | * @param CRM_Core_Form $form |
550 | * @param array $params | |
b5c2afd0 EM |
551 | * |
552 | * @return mixed | |
553 | */ | |
00be9182 | 554 | public static function buildSelectedContactPager(&$form, &$params) { |
6a488035 TO |
555 | $params['status'] = ts('Contacts %%StatusMessage%%'); |
556 | $params['csvString'] = NULL; | |
557 | $params['buttonTop'] = 'PagerTopButton'; | |
558 | $params['buttonBottom'] = 'PagerBottomButton'; | |
c490a46a | 559 | $params['rowCount'] = $form->get(CRM_Utils_Pager::PAGE_ROWCOUNT); |
6a488035 TO |
560 | |
561 | if (!$params['rowCount']) { | |
562 | $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT; | |
563 | } | |
564 | ||
353ffa53 | 565 | $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $form); |
6a488035 TO |
566 | $cacheKey = "civicrm search {$qfKey}"; |
567 | ||
568 | $query = " | |
21ca2cb6 | 569 | SELECT count(*) |
6a488035 TO |
570 | FROM civicrm_prevnext_cache |
571 | WHERE cacheKey LIKE %1 | |
572 | AND is_selected = 1 | |
573 | AND cacheKey NOT LIKE %2 | |
574 | "; | |
55488e61 DL |
575 | $params1[1] = array("{$cacheKey}%", 'String'); |
576 | $params1[2] = array("{$cacheKey}_alphabet%", 'String'); | |
c490a46a | 577 | $paramsTotal = CRM_Core_DAO::singleValueQuery($query, $params1); |
6a488035 | 578 | $params['total'] = $paramsTotal; |
c490a46a CW |
579 | $form->_pager = new CRM_Utils_Pager($params); |
580 | $form->assign_by_ref('pager', $form->_pager); | |
581 | list($offset, $rowCount) = $form->_pager->getOffsetAndRowCount(); | |
6a488035 TO |
582 | $params['offset'] = $offset; |
583 | $params['rowCount1'] = $rowCount; | |
584 | return $params; | |
585 | } | |
96025800 | 586 | |
808c05a9 | 587 | /** |
588 | * Flip 2 contacts in the prevNext cache. | |
589 | * | |
590 | * @param array $prevNextId | |
591 | * @param bool $onlySelected | |
592 | * Only flip those which have been marked as selected. | |
593 | */ | |
594 | public static function flipPair(array $prevNextId, $onlySelected) { | |
595 | $dao = new CRM_Core_DAO_PrevNextCache(); | |
596 | if ($onlySelected) { | |
597 | $dao->is_selected = 1; | |
598 | } | |
599 | foreach ($prevNextId as $id) { | |
600 | $dao->id = $id; | |
601 | if ($dao->find(TRUE)) { | |
602 | $originalData = unserialize($dao->data); | |
603 | $srcFields = array('ID', 'Name'); | |
604 | $swapFields = array('srcID', 'srcName', 'dstID', 'dstName'); | |
605 | $data = array_diff_assoc($originalData, array_fill_keys($swapFields, 1)); | |
606 | foreach ($srcFields as $key) { | |
607 | $data['src' . $key] = $originalData['dst' . $key]; | |
608 | $data['dst' . $key] = $originalData['src' . $key]; | |
609 | } | |
610 | $dao->data = serialize($data); | |
e67dcaf8 | 611 | $dao->entity_id1 = $data['dstID']; |
612 | $dao->entity_id2 = $data['srcID']; | |
808c05a9 | 613 | $dao->save(); |
614 | } | |
615 | } | |
616 | } | |
617 | ||
6a488035 | 618 | } |