Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
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"; | |
156 | $params[4] = array("{$cacheKey}%", 'String'); | |
157 | } | |
158 | ||
159 | CRM_Core_DAO::executeQuery($sql, $params); | |
160 | } | |
161 | ||
b5c2afd0 EM |
162 | /** |
163 | * @param $cacheKey | |
e60f24eb TO |
164 | * @param NULL $join |
165 | * @param NULL $where | |
b5c2afd0 EM |
166 | * @param int $offset |
167 | * @param int $rowCount | |
168 | * | |
169 | * @return array | |
170 | */ | |
00be9182 | 171 | public static function retrieve($cacheKey, $join = NULL, $where = NULL, $offset = 0, $rowCount = 0) { |
6a488035 TO |
172 | $query = " |
173 | SELECT data | |
174 | FROM civicrm_prevnext_cache pn | |
175 | {$join} | |
176 | WHERE cacheKey = %1 | |
177 | "; | |
178 | $params = array(1 => array($cacheKey, 'String')); | |
179 | ||
180 | if ($where) { | |
181 | $query .= " AND {$where}"; | |
182 | } | |
183 | ||
184 | if ($rowCount) { | |
bf00d1b6 DL |
185 | $offset = CRM_Utils_Type::escape($offset, 'Int'); |
186 | $rowCount = CRM_Utils_Type::escape($rowCount, 'Int'); | |
187 | ||
6a488035 TO |
188 | $query .= " LIMIT {$offset}, {$rowCount}"; |
189 | } | |
190 | ||
191 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
192 | ||
193 | $main = array(); | |
194 | while ($dao->fetch()) { | |
195 | if (self::is_serialized($dao->data)) { | |
196 | $main[] = unserialize($dao->data); | |
197 | } | |
198 | else { | |
199 | $main[] = $dao->data; | |
200 | } | |
201 | } | |
202 | ||
203 | return $main; | |
204 | } | |
205 | ||
b5c2afd0 EM |
206 | /** |
207 | * @param $string | |
208 | * | |
209 | * @return bool | |
210 | */ | |
6a488035 | 211 | public static function is_serialized($string) { |
ab8a593e | 212 | return (@unserialize($string) !== FALSE); |
6a488035 TO |
213 | } |
214 | ||
b5c2afd0 EM |
215 | /** |
216 | * @param $values | |
217 | */ | |
00be9182 | 218 | public static function setItem($values) { |
6a488035 TO |
219 | $insert = "INSERT INTO civicrm_prevnext_cache ( entity_table, entity_id1, entity_id2, cacheKey, data ) VALUES \n"; |
220 | $query = $insert . implode(",\n ", $values); | |
221 | ||
222 | //dump the dedupe matches in the prevnext_cache table | |
223 | CRM_Core_DAO::executeQuery($query); | |
224 | } | |
225 | ||
b5c2afd0 EM |
226 | /** |
227 | * @param $cacheKey | |
e60f24eb TO |
228 | * @param NULL $join |
229 | * @param NULL $where | |
b5c2afd0 EM |
230 | * @param string $op |
231 | * | |
232 | * @return int | |
233 | */ | |
00be9182 | 234 | public static function getCount($cacheKey, $join = NULL, $where = NULL, $op = "=") { |
6a488035 TO |
235 | $query = " |
236 | SELECT COUNT(*) FROM civicrm_prevnext_cache pn | |
237 | {$join} | |
238 | WHERE cacheKey $op %1 | |
239 | "; | |
240 | if ($where) { | |
241 | $query .= " AND {$where}"; | |
242 | } | |
243 | ||
244 | $params = array(1 => array($cacheKey, 'String')); | |
64951b63 | 245 | return (int) CRM_Core_DAO::singleValueQuery($query, $params, TRUE, FALSE); |
6a488035 TO |
246 | } |
247 | ||
b5c2afd0 | 248 | /** |
100fef9d CW |
249 | * @param int $rgid |
250 | * @param int $gid | |
e60f24eb | 251 | * @param NULL $cacheKeyString |
7a9ab499 EM |
252 | * |
253 | * @return bool | |
b5c2afd0 | 254 | */ |
00be9182 | 255 | public static function refillCache($rgid = NULL, $gid = NULL, $cacheKeyString = NULL) { |
6a488035 TO |
256 | if (!$cacheKeyString && $rgid) { |
257 | $contactType = CRM_Core_DAO::getFieldValue('CRM_Dedupe_DAO_RuleGroup', $rgid, 'contact_type'); | |
258 | $cacheKeyString = "merge {$contactType}"; | |
259 | $cacheKeyString .= $rgid ? "_{$rgid}" : '_0'; | |
260 | $cacheKeyString .= $gid ? "_{$gid}" : '_0'; | |
261 | } | |
262 | ||
263 | if (!$cacheKeyString) { | |
264 | return FALSE; | |
265 | } | |
266 | ||
267 | // 1. Clear cache if any | |
268 | $sql = "DELETE FROM civicrm_prevnext_cache WHERE cacheKey LIKE %1"; | |
269 | CRM_Core_DAO::executeQuery($sql, array(1 => array("{$cacheKeyString}%", 'String'))); | |
270 | ||
271 | // FIXME: we need to start using temp tables / queries here instead of arrays. | |
272 | // And cleanup code in CRM/Contact/Page/DedupeFind.php | |
273 | ||
274 | // 2. FILL cache | |
275 | $foundDupes = array(); | |
276 | if ($rgid && $gid) { | |
277 | $foundDupes = CRM_Dedupe_Finder::dupesInGroup($rgid, $gid); | |
278 | } | |
279 | elseif ($rgid) { | |
280 | $foundDupes = CRM_Dedupe_Finder::dupes($rgid); | |
281 | } | |
282 | ||
283 | if (!empty($foundDupes)) { | |
284 | $cids = $displayNames = $values = array(); | |
285 | foreach ($foundDupes as $dupe) { | |
286 | $cids[$dupe[0]] = 1; | |
287 | $cids[$dupe[1]] = 1; | |
288 | } | |
289 | $cidString = implode(', ', array_keys($cids)); | |
353ffa53 TO |
290 | $sql = "SELECT id, display_name FROM civicrm_contact WHERE id IN ($cidString) ORDER BY sort_name"; |
291 | $dao = new CRM_Core_DAO(); | |
6a488035 TO |
292 | $dao->query($sql); |
293 | while ($dao->fetch()) { | |
294 | $displayNames[$dao->id] = $dao->display_name; | |
295 | } | |
296 | ||
297 | $session = CRM_Core_Session::singleton(); | |
298 | $userId = $session->get('userID'); | |
299 | ||
300 | foreach ($foundDupes as $dupes) { | |
301 | $srcID = $dupes[0]; | |
302 | $dstID = $dupes[1]; | |
303 | if ($dstID == $userId) { | |
304 | $srcID = $dupes[1]; | |
305 | $dstID = $dupes[0]; | |
306 | } | |
307 | ||
308 | $row = array( | |
309 | 'srcID' => $srcID, | |
310 | 'srcName' => $displayNames[$srcID], | |
311 | 'dstID' => $dstID, | |
312 | 'dstName' => $displayNames[$dstID], | |
313 | 'weight' => $dupes[2], | |
314 | 'canMerge' => TRUE, | |
315 | ); | |
316 | ||
317 | $data = CRM_Core_DAO::escapeString(serialize($row)); | |
318 | $values[] = " ( 'civicrm_contact', $srcID, $dstID, '$cacheKeyString', '$data' ) "; | |
319 | } | |
320 | self::setItem($values); | |
321 | } | |
322 | } | |
323 | ||
00be9182 | 324 | public static function cleanupCache() { |
6a488035 TO |
325 | // clean up all prev next caches older than $cacheTimeIntervalDays days |
326 | $cacheTimeIntervalDays = 2; | |
327 | ||
328 | // first find all the cacheKeys that match this | |
329 | $sql = " | |
330 | DELETE pn, c | |
331 | FROM civicrm_cache c | |
332 | INNER JOIN civicrm_prevnext_cache pn ON c.path = pn.cacheKey | |
333 | WHERE c.group_name = %1 | |
334 | AND c.created_date < date_sub( NOW( ), INTERVAL %2 day ) | |
335 | "; | |
336 | $params = array( | |
337 | 1 => array('CiviCRM Search PrevNextCache', 'String'), | |
338 | 2 => array($cacheTimeIntervalDays, 'Integer'), | |
339 | ); | |
340 | CRM_Core_DAO::executeQuery($sql, $params); | |
341 | } | |
342 | ||
b5c2afd0 | 343 | /** |
fe482240 | 344 | * Save checkbox selections. |
c490a46a | 345 | * |
b5c2afd0 EM |
346 | * @param $cacheKey |
347 | * @param string $action | |
c490a46a | 348 | * @param array $cIds |
b5c2afd0 EM |
349 | * @param string $entity_table |
350 | */ | |
00be9182 | 351 | public static function markSelection($cacheKey, $action = 'unselect', $cIds = NULL, $entity_table = 'civicrm_contact') { |
6a488035 TO |
352 | if (!$cacheKey) { |
353 | return; | |
354 | } | |
355 | $params = array(); | |
356 | ||
357 | $entity_whereClause = " AND entity_table = '{$entity_table}'"; | |
358 | if ($cIds && $cacheKey && $action) { | |
359 | if (is_array($cIds)) { | |
360 | $cIdFilter = "(" . implode(',', $cIds) . ")"; | |
361 | $whereClause = " | |
362 | WHERE cacheKey LIKE %1 | |
363 | AND (entity_id1 IN {$cIdFilter} OR entity_id2 IN {$cIdFilter}) | |
364 | "; | |
365 | } | |
366 | else { | |
367 | $whereClause = " | |
368 | WHERE cacheKey LIKE %1 | |
369 | AND (entity_id1 = %2 OR entity_id2 = %2) | |
370 | "; | |
371 | $params[2] = array("{$cIds}", 'Integer'); | |
372 | } | |
373 | if ($action == 'select') { | |
374 | $whereClause .= "AND is_selected = 0"; | |
375 | $sql = "UPDATE civicrm_prevnext_cache SET is_selected = 1 {$whereClause} {$entity_whereClause}"; | |
55488e61 | 376 | $params[1] = array("{$cacheKey}%", 'String'); |
6a488035 TO |
377 | } |
378 | elseif ($action == 'unselect') { | |
379 | $whereClause .= "AND is_selected = 1"; | |
380 | $sql = "UPDATE civicrm_prevnext_cache SET is_selected = 0 {$whereClause} {$entity_whereClause}"; | |
381 | $params[1] = array("%{$cacheKey}%", 'String'); | |
382 | } | |
383 | // default action is reseting | |
384 | } | |
385 | elseif (!$cIds && $cacheKey && $action == 'unselect') { | |
386 | $sql = " | |
387 | UPDATE civicrm_prevnext_cache | |
388 | SET is_selected = 0 | |
389 | WHERE cacheKey LIKE %1 AND is_selected = 1 | |
390 | {$entity_whereClause} | |
391 | "; | |
55488e61 | 392 | $params[1] = array("{$cacheKey}%", 'String'); |
6a488035 TO |
393 | } |
394 | CRM_Core_DAO::executeQuery($sql, $params); | |
395 | } | |
396 | ||
397 | /** | |
fe482240 | 398 | * Get the selections. |
6a488035 | 399 | * |
6a0b768e TO |
400 | * @param string $cacheKey |
401 | * Cache key. | |
402 | * @param string $action | |
403 | * Action. | |
6a488035 TO |
404 | * $action : get - get only selection records |
405 | * getall - get all the records of the specified cache key | |
6a0b768e TO |
406 | * @param string $entity_table |
407 | * Entity table. | |
77b97be7 | 408 | * |
ae5ffbb7 | 409 | * @return array|NULL |
6a488035 | 410 | */ |
00be9182 | 411 | public static function getSelection($cacheKey, $action = 'get', $entity_table = 'civicrm_contact') { |
6a488035 | 412 | if (!$cacheKey) { |
ae5ffbb7 | 413 | return NULL; |
6a488035 TO |
414 | } |
415 | $params = array(); | |
416 | ||
417 | $entity_whereClause = " AND entity_table = '{$entity_table}'"; | |
418 | if ($cacheKey && ($action == 'get' || $action == 'getall')) { | |
419 | $actionGet = ($action == "get") ? " AND is_selected = 1 " : ""; | |
420 | $sql = " | |
421 | SELECT entity_id1, entity_id2 FROM civicrm_prevnext_cache | |
422 | WHERE cacheKey LIKE %1 | |
423 | $actionGet | |
424 | $entity_whereClause | |
425 | ORDER BY id | |
426 | "; | |
55488e61 | 427 | $params[1] = array("{$cacheKey}%", 'String'); |
6a488035 TO |
428 | |
429 | $contactIds = array($cacheKey => array()); | |
430 | $cIdDao = CRM_Core_DAO::executeQuery($sql, $params); | |
431 | while ($cIdDao->fetch()) { | |
432 | if ($cIdDao->entity_id1 == $cIdDao->entity_id2) { | |
433 | $contactIds[$cacheKey][$cIdDao->entity_id1] = 1; | |
434 | } | |
435 | } | |
436 | return $contactIds; | |
437 | } | |
438 | } | |
439 | ||
b5c2afd0 EM |
440 | /** |
441 | * @return array | |
442 | */ | |
00be9182 | 443 | public static function getSelectedContacts() { |
6a488035 TO |
444 | $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String'); |
445 | $cacheKey = "civicrm search {$qfKey}"; | |
446 | $query = " | |
447 | SELECT * | |
448 | FROM civicrm_prevnext_cache | |
449 | WHERE cacheKey LIKE %1 | |
450 | AND is_selected=1 | |
451 | AND cacheKey NOT LIKE %2 | |
452 | "; | |
55488e61 DL |
453 | $params1[1] = array("{$cacheKey}%", 'String'); |
454 | $params1[2] = array("{$cacheKey}_alphabet%", 'String'); | |
6a488035 TO |
455 | $dao = CRM_Core_DAO::executeQuery($query, $params1); |
456 | ||
481a74f4 | 457 | $val = array(); |
6a488035 TO |
458 | while ($dao->fetch()) { |
459 | $val[] = $dao->data; | |
460 | } | |
461 | return $val; | |
462 | } | |
463 | ||
b5c2afd0 | 464 | /** |
c490a46a CW |
465 | * @param CRM_Core_Form $form |
466 | * @param array $params | |
b5c2afd0 EM |
467 | * |
468 | * @return mixed | |
469 | */ | |
00be9182 | 470 | public static function buildSelectedContactPager(&$form, &$params) { |
6a488035 TO |
471 | $params['status'] = ts('Contacts %%StatusMessage%%'); |
472 | $params['csvString'] = NULL; | |
473 | $params['buttonTop'] = 'PagerTopButton'; | |
474 | $params['buttonBottom'] = 'PagerBottomButton'; | |
c490a46a | 475 | $params['rowCount'] = $form->get(CRM_Utils_Pager::PAGE_ROWCOUNT); |
6a488035 TO |
476 | |
477 | if (!$params['rowCount']) { | |
478 | $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT; | |
479 | } | |
480 | ||
353ffa53 | 481 | $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $form); |
6a488035 TO |
482 | $cacheKey = "civicrm search {$qfKey}"; |
483 | ||
484 | $query = " | |
485 | SELECT count(id) | |
486 | FROM civicrm_prevnext_cache | |
487 | WHERE cacheKey LIKE %1 | |
488 | AND is_selected = 1 | |
489 | AND cacheKey NOT LIKE %2 | |
490 | "; | |
55488e61 DL |
491 | $params1[1] = array("{$cacheKey}%", 'String'); |
492 | $params1[2] = array("{$cacheKey}_alphabet%", 'String'); | |
c490a46a | 493 | $paramsTotal = CRM_Core_DAO::singleValueQuery($query, $params1); |
6a488035 | 494 | $params['total'] = $paramsTotal; |
c490a46a CW |
495 | $form->_pager = new CRM_Utils_Pager($params); |
496 | $form->assign_by_ref('pager', $form->_pager); | |
497 | list($offset, $rowCount) = $form->_pager->getOffsetAndRowCount(); | |
6a488035 TO |
498 | $params['offset'] = $offset; |
499 | $params['rowCount1'] = $rowCount; | |
500 | return $params; | |
501 | } | |
96025800 | 502 | |
6a488035 | 503 | } |