Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
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 | |
45 | * @param null $mergeId | |
46 | * @param null $join | |
47 | * @param null $where | |
48 | * @param bool $flip | |
49 | * | |
50 | * @return array | |
51 | */ | |
6a488035 TO |
52 | static function getPositions($cacheKey, $id1, $id2, &$mergeId = NULL, $join = NULL, $where = NULL, $flip = FALSE) { |
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 | ||
67 | $params = array(1 => array($id1, 'Integer'), | |
68 | 2 => array($id2, 'Integer'), | |
69 | 3 => array($cacheKey, 'String'), | |
70 | ); | |
71 | ||
72 | $mergeId = CRM_Core_DAO::singleValueQuery($query, $params); | |
73 | } | |
74 | ||
75 | $pos = array('foundEntry' => 0); | |
76 | if ($mergeId) { | |
77 | $pos['foundEntry'] = 1; | |
78 | ||
79 | if ($where) { | |
80 | ||
81 | $where = " AND {$where}"; | |
82 | ||
83 | } | |
84 | $p = array(1 => array($mergeId, 'Integer'), | |
85 | 2 => array($cacheKey, 'String'), | |
86 | ); | |
87 | $sql = "SELECT pn.id, pn.entity_id1, pn.entity_id2, pn.data FROM civicrm_prevnext_cache pn {$join} "; | |
88 | $wherePrev = " WHERE pn.id < %1 AND pn.cacheKey = %2 {$where} ORDER BY ID DESC LIMIT 1"; | |
89 | $sqlPrev = $sql . $wherePrev; | |
90 | ||
91 | ||
92 | $dao = CRM_Core_DAO::executeQuery($sqlPrev, $p); | |
93 | if ($dao->fetch()) { | |
94 | $pos['prev']['id1'] = $dao->entity_id1; | |
95 | $pos['prev']['id2'] = $dao->entity_id2; | |
96 | $pos['prev']['mergeId'] = $dao->id; | |
97 | $pos['prev']['data'] = $dao->data; | |
98 | } | |
99 | ||
100 | $whereNext = " WHERE pn.id > %1 AND pn.cacheKey = %2 {$where} ORDER BY ID ASC LIMIT 1"; | |
101 | $sqlNext = $sql . $whereNext; | |
102 | ||
103 | $dao = CRM_Core_DAO::executeQuery($sqlNext, $p); | |
104 | if ($dao->fetch()) { | |
105 | $pos['next']['id1'] = $dao->entity_id1; | |
106 | $pos['next']['id2'] = $dao->entity_id2; | |
107 | $pos['next']['mergeId'] = $dao->id; | |
108 | $pos['next']['data'] = $dao->data; | |
109 | } | |
110 | } | |
111 | return $pos; | |
112 | } | |
113 | ||
b5c2afd0 EM |
114 | /** |
115 | * @param null $id | |
116 | * @param null $cacheKey | |
117 | * @param string $entityTable | |
118 | */ | |
6a488035 TO |
119 | static function deleteItem($id = NULL, $cacheKey = NULL, $entityTable = 'civicrm_contact') { |
120 | ||
121 | //clear cache | |
122 | $sql = "DELETE FROM civicrm_prevnext_cache WHERE entity_table = %1"; | |
123 | $params = array(1 => array($entityTable, 'String')); | |
124 | ||
125 | if (is_numeric($id)) { | |
126 | $sql .= " AND ( entity_id1 = %2 OR entity_id2 = %2 )"; | |
127 | $params[2] = array($id, 'Integer'); | |
128 | } | |
129 | ||
130 | if (isset($cacheKey)) { | |
131 | $sql .= " AND cacheKey LIKE %3"; | |
132 | $params[3] = array("{$cacheKey}%", 'String'); | |
133 | } | |
134 | CRM_Core_DAO::executeQuery($sql, $params); | |
135 | } | |
136 | ||
b5c2afd0 EM |
137 | /** |
138 | * @param $id1 | |
139 | * @param $id2 | |
140 | * @param null $cacheKey | |
141 | * @param bool $isViceVersa | |
142 | * @param string $entityTable | |
143 | */ | |
6a488035 TO |
144 | static function deletePair($id1, $id2, $cacheKey = NULL, $isViceVersa = FALSE, $entityTable = 'civicrm_contact') { |
145 | $sql = "DELETE FROM civicrm_prevnext_cache WHERE entity_table = %1"; | |
146 | $params = array(1 => array($entityTable, 'String')); | |
147 | ||
148 | $pair = | |
149 | ! $isViceVersa ? | |
150 | "entity_id1 = %2 AND entity_id2 = %3" : | |
151 | "(entity_id1 = %2 AND entity_id2 = %3) OR (entity_id1 = %3 AND entity_id2 = %2)"; | |
152 | $sql .= " AND ( {$pair} )"; | |
153 | $params[2] = array($id1, 'Integer'); | |
154 | $params[3] = array($id2, 'Integer'); | |
155 | ||
156 | if (isset($cacheKey)) { | |
157 | $sql .= " AND cacheKey LIKE %4"; | |
158 | $params[4] = array("{$cacheKey}%", 'String'); | |
159 | } | |
160 | ||
161 | CRM_Core_DAO::executeQuery($sql, $params); | |
162 | } | |
163 | ||
b5c2afd0 EM |
164 | /** |
165 | * @param $cacheKey | |
166 | * @param null $join | |
167 | * @param null $where | |
168 | * @param int $offset | |
169 | * @param int $rowCount | |
170 | * | |
171 | * @return array | |
172 | */ | |
00c461ec | 173 | static function retrieve($cacheKey, $join = NULL, $where = NULL, $offset = 0, $rowCount = 0) { |
6a488035 TO |
174 | $query = " |
175 | SELECT data | |
176 | FROM civicrm_prevnext_cache pn | |
177 | {$join} | |
178 | WHERE cacheKey = %1 | |
179 | "; | |
180 | $params = array(1 => array($cacheKey, 'String')); | |
181 | ||
182 | if ($where) { | |
183 | $query .= " AND {$where}"; | |
184 | } | |
185 | ||
186 | if ($rowCount) { | |
bf00d1b6 DL |
187 | $offset = CRM_Utils_Type::escape($offset, 'Int'); |
188 | $rowCount = CRM_Utils_Type::escape($rowCount, 'Int'); | |
189 | ||
6a488035 TO |
190 | $query .= " LIMIT {$offset}, {$rowCount}"; |
191 | } | |
192 | ||
193 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
194 | ||
195 | $main = array(); | |
196 | while ($dao->fetch()) { | |
197 | if (self::is_serialized($dao->data)) { | |
198 | $main[] = unserialize($dao->data); | |
199 | } | |
200 | else { | |
201 | $main[] = $dao->data; | |
202 | } | |
203 | } | |
204 | ||
205 | return $main; | |
206 | } | |
207 | ||
b5c2afd0 EM |
208 | /** |
209 | * @param $string | |
210 | * | |
211 | * @return bool | |
212 | */ | |
6a488035 TO |
213 | public static function is_serialized($string) { |
214 | return (@unserialize($string) !== false); | |
215 | } | |
216 | ||
b5c2afd0 EM |
217 | /** |
218 | * @param $values | |
219 | */ | |
6a488035 TO |
220 | static function setItem($values) { |
221 | $insert = "INSERT INTO civicrm_prevnext_cache ( entity_table, entity_id1, entity_id2, cacheKey, data ) VALUES \n"; | |
222 | $query = $insert . implode(",\n ", $values); | |
223 | ||
224 | //dump the dedupe matches in the prevnext_cache table | |
225 | CRM_Core_DAO::executeQuery($query); | |
226 | } | |
227 | ||
b5c2afd0 EM |
228 | /** |
229 | * @param $cacheKey | |
230 | * @param null $join | |
231 | * @param null $where | |
232 | * @param string $op | |
233 | * | |
234 | * @return int | |
235 | */ | |
6a488035 TO |
236 | static function getCount($cacheKey, $join = NULL, $where = NULL, $op = "=") { |
237 | $query = " | |
238 | SELECT COUNT(*) FROM civicrm_prevnext_cache pn | |
239 | {$join} | |
240 | WHERE cacheKey $op %1 | |
241 | "; | |
242 | if ($where) { | |
243 | $query .= " AND {$where}"; | |
244 | } | |
245 | ||
246 | $params = array(1 => array($cacheKey, 'String')); | |
64951b63 | 247 | return (int) CRM_Core_DAO::singleValueQuery($query, $params, TRUE, FALSE); |
6a488035 TO |
248 | } |
249 | ||
b5c2afd0 EM |
250 | /** |
251 | * @param null $rgid | |
252 | * @param null $gid | |
253 | * @param null $cacheKeyString | |
254 | */ | |
6a488035 TO |
255 | static function refillCache($rgid = NULL, $gid = NULL, $cacheKeyString = NULL) { |
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)); | |
290 | $sql = "SELECT id, display_name FROM civicrm_contact WHERE id IN ($cidString) ORDER BY sort_name"; | |
291 | $dao = new CRM_Core_DAO(); | |
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 | ||
324 | static function cleanupCache() { | |
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 | ||
343 | ||
344 | /* function for saving the checkbox selections | |
345 | * $action select - select a particular contact | |
346 | * unselect - unselect a particular contact | |
347 | */ | |
b5c2afd0 EM |
348 | /** |
349 | * @param $cacheKey | |
350 | * @param string $action | |
351 | * @param null $cIds | |
352 | * @param string $entity_table | |
353 | */ | |
6a488035 TO |
354 | static function markSelection($cacheKey, $action = 'unselect', $cIds = NULL, $entity_table = 'civicrm_contact') { |
355 | if (!$cacheKey) { | |
356 | return; | |
357 | } | |
358 | $params = array(); | |
359 | ||
360 | $entity_whereClause = " AND entity_table = '{$entity_table}'"; | |
361 | if ($cIds && $cacheKey && $action) { | |
362 | if (is_array($cIds)) { | |
363 | $cIdFilter = "(" . implode(',', $cIds) . ")"; | |
364 | $whereClause = " | |
365 | WHERE cacheKey LIKE %1 | |
366 | AND (entity_id1 IN {$cIdFilter} OR entity_id2 IN {$cIdFilter}) | |
367 | "; | |
368 | } | |
369 | else { | |
370 | $whereClause = " | |
371 | WHERE cacheKey LIKE %1 | |
372 | AND (entity_id1 = %2 OR entity_id2 = %2) | |
373 | "; | |
374 | $params[2] = array("{$cIds}", 'Integer'); | |
375 | } | |
376 | if ($action == 'select') { | |
377 | $whereClause .= "AND is_selected = 0"; | |
378 | $sql = "UPDATE civicrm_prevnext_cache SET is_selected = 1 {$whereClause} {$entity_whereClause}"; | |
55488e61 | 379 | $params[1] = array("{$cacheKey}%", 'String'); |
6a488035 TO |
380 | } |
381 | elseif ($action == 'unselect') { | |
382 | $whereClause .= "AND is_selected = 1"; | |
383 | $sql = "UPDATE civicrm_prevnext_cache SET is_selected = 0 {$whereClause} {$entity_whereClause}"; | |
384 | $params[1] = array("%{$cacheKey}%", 'String'); | |
385 | } | |
386 | // default action is reseting | |
387 | } | |
388 | elseif (!$cIds && $cacheKey && $action == 'unselect') { | |
389 | $sql = " | |
390 | UPDATE civicrm_prevnext_cache | |
391 | SET is_selected = 0 | |
392 | WHERE cacheKey LIKE %1 AND is_selected = 1 | |
393 | {$entity_whereClause} | |
394 | "; | |
55488e61 | 395 | $params[1] = array("{$cacheKey}%", 'String'); |
6a488035 TO |
396 | } |
397 | CRM_Core_DAO::executeQuery($sql, $params); | |
398 | } | |
399 | ||
400 | /** | |
401 | * function to get the selections | |
402 | * | |
77b97be7 EM |
403 | * @param string $cacheKey cache key |
404 | * @param string $action action | |
6a488035 TO |
405 | * $action : get - get only selection records |
406 | * getall - get all the records of the specified cache key | |
407 | * @param string $entity_table entity table | |
77b97be7 EM |
408 | * |
409 | * @return array | |
6a488035 TO |
410 | */ |
411 | static function getSelection($cacheKey, $action = 'get', $entity_table = 'civicrm_contact') { | |
412 | if (!$cacheKey) { | |
413 | return; | |
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 | */ | |
6a488035 TO |
443 | static function getSelectedContacts() { |
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 | ||
457 | $val = array( ); | |
458 | while ($dao->fetch()) { | |
459 | $val[] = $dao->data; | |
460 | } | |
461 | return $val; | |
462 | } | |
463 | ||
b5c2afd0 EM |
464 | /** |
465 | * @param $obj | |
466 | * @param $params | |
467 | * | |
468 | * @return mixed | |
469 | */ | |
6a488035 TO |
470 | static function buildSelectedContactPager( &$obj, &$params) { |
471 | $params['status'] = ts('Contacts %%StatusMessage%%'); | |
472 | $params['csvString'] = NULL; | |
473 | $params['buttonTop'] = 'PagerTopButton'; | |
474 | $params['buttonBottom'] = 'PagerBottomButton'; | |
475 | $params['rowCount'] = $obj->get(CRM_Utils_Pager::PAGE_ROWCOUNT); | |
476 | ||
477 | if (!$params['rowCount']) { | |
478 | $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT; | |
479 | } | |
480 | ||
481 | $qfKey = CRM_Utils_Request::retrieve('qfKey','String', $this); | |
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'); | |
6a488035 TO |
493 | $paramsTotal = CRM_Core_DAO::singleValueQuery($query, $params1); |
494 | $params['total'] = $paramsTotal; | |
495 | $obj->_pager = new CRM_Utils_Pager($params); | |
496 | $obj->assign_by_ref('pager', $obj->_pager); | |
497 | list($offset, $rowCount) = $obj->_pager->getOffsetAndRowCount(); | |
498 | $params['offset'] = $offset; | |
499 | $params['rowCount1'] = $rowCount; | |
500 | return $params; | |
501 | } | |
502 | } | |
503 |