3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * BAO object for civicrm_prevnext_cache table
39 class CRM_Core_BAO_PrevNextCache
extends CRM_Core_DAO_PrevNextCache
{
52 public static function getPositions($cacheKey, $id1, $id2, &$mergeId = NULL, $join = NULL, $where = NULL, $flip = FALSE) {
54 list($id1, $id2) = array($id2, $id1);
57 if ($mergeId == NULL) {
60 FROM civicrm_prevnext_cache
61 WHERE cacheKey = %3 AND
64 entity_table = 'civicrm_contact'
68 1 => array($id1, 'Integer'),
69 2 => array($id2, 'Integer'),
70 3 => array($cacheKey, 'String'),
73 $mergeId = CRM_Core_DAO
::singleValueQuery($query, $params);
76 $pos = array('foundEntry' => 0);
78 $pos['foundEntry'] = 1;
82 $where = " AND {$where}";
86 1 => array($mergeId, 'Integer'),
87 2 => array($cacheKey, 'String'),
89 $sql = "SELECT pn.id, pn.entity_id1, pn.entity_id2, pn.data FROM civicrm_prevnext_cache pn {$join} ";
90 $wherePrev = " WHERE pn.id < %1 AND pn.cacheKey = %2 {$where} ORDER BY ID DESC LIMIT 1";
91 $sqlPrev = $sql . $wherePrev;
93 $dao = CRM_Core_DAO
::executeQuery($sqlPrev, $p);
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
;
101 $whereNext = " WHERE pn.id > %1 AND pn.cacheKey = %2 {$where} ORDER BY ID ASC LIMIT 1";
102 $sqlNext = $sql . $whereNext;
104 $dao = CRM_Core_DAO
::executeQuery($sqlNext, $p);
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
;
117 * @param NULL $cacheKey
118 * @param string $entityTable
120 public static function deleteItem($id = NULL, $cacheKey = NULL, $entityTable = 'civicrm_contact') {
123 $sql = "DELETE FROM civicrm_prevnext_cache WHERE entity_table = %1";
124 $params = array(1 => array($entityTable, 'String'));
126 if (is_numeric($id)) {
127 $sql .= " AND ( entity_id1 = %2 OR entity_id2 = %2 )";
128 $params[2] = array($id, 'Integer');
131 if (isset($cacheKey)) {
132 $sql .= " AND cacheKey LIKE %3";
133 $params[3] = array("{$cacheKey}%", 'String');
135 CRM_Core_DAO
::executeQuery($sql, $params);
141 * @param NULL $cacheKey
142 * @param bool $isViceVersa
143 * @param string $entityTable
145 public static function deletePair($id1, $id2, $cacheKey = NULL, $isViceVersa = FALSE, $entityTable = 'civicrm_contact') {
146 $sql = "DELETE FROM civicrm_prevnext_cache WHERE entity_table = %1";
147 $params = array(1 => array($entityTable, 'String'));
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)";
150 $sql .= " AND ( {$pair} )";
151 $params[2] = array($id1, 'Integer');
152 $params[3] = array($id2, 'Integer');
154 if (isset($cacheKey)) {
155 $sql .= " AND cacheKey LIKE %4";
156 $params[4] = array("{$cacheKey}%", 'String');
159 CRM_Core_DAO
::executeQuery($sql, $params);
167 * @param int $rowCount
171 public static function retrieve($cacheKey, $join = NULL, $where = NULL, $offset = 0, $rowCount = 0) {
174 FROM civicrm_prevnext_cache pn
178 $params = array(1 => array($cacheKey, 'String'));
181 $query .= " AND {$where}";
185 $offset = CRM_Utils_Type
::escape($offset, 'Int');
186 $rowCount = CRM_Utils_Type
::escape($rowCount, 'Int');
188 $query .= " LIMIT {$offset}, {$rowCount}";
191 $dao = CRM_Core_DAO
::executeQuery($query, $params);
194 while ($dao->fetch()) {
195 if (self
::is_serialized($dao->data
)) {
196 $main[] = unserialize($dao->data
);
199 $main[] = $dao->data
;
211 public static function is_serialized($string) {
212 return (@unserialize
($string) !== FALSE);
218 public static function setItem($values) {
219 $insert = "INSERT INTO civicrm_prevnext_cache ( entity_table, entity_id1, entity_id2, cacheKey, data ) VALUES \n";
220 $query = $insert . implode(",\n ", $values);
222 //dump the dedupe matches in the prevnext_cache table
223 CRM_Core_DAO
::executeQuery($query);
234 public static function getCount($cacheKey, $join = NULL, $where = NULL, $op = "=") {
236 SELECT COUNT(*) FROM civicrm_prevnext_cache pn
238 WHERE cacheKey $op %1
241 $query .= " AND {$where}";
244 $params = array(1 => array($cacheKey, 'String'));
245 return (int) CRM_Core_DAO
::singleValueQuery($query, $params, TRUE, FALSE);
251 * @param NULL $cacheKeyString
253 public static function refillCache($rgid = NULL, $gid = NULL, $cacheKeyString = NULL) {
254 if (!$cacheKeyString && $rgid) {
255 $contactType = CRM_Core_DAO
::getFieldValue('CRM_Dedupe_DAO_RuleGroup', $rgid, 'contact_type');
256 $cacheKeyString = "merge {$contactType}";
257 $cacheKeyString .= $rgid ?
"_{$rgid}" : '_0';
258 $cacheKeyString .= $gid ?
"_{$gid}" : '_0';
261 if (!$cacheKeyString) {
265 // 1. Clear cache if any
266 $sql = "DELETE FROM civicrm_prevnext_cache WHERE cacheKey LIKE %1";
267 CRM_Core_DAO
::executeQuery($sql, array(1 => array("{$cacheKeyString}%", 'String')));
269 // FIXME: we need to start using temp tables / queries here instead of arrays.
270 // And cleanup code in CRM/Contact/Page/DedupeFind.php
273 $foundDupes = array();
275 $foundDupes = CRM_Dedupe_Finder
::dupesInGroup($rgid, $gid);
278 $foundDupes = CRM_Dedupe_Finder
::dupes($rgid);
281 if (!empty($foundDupes)) {
282 $cids = $displayNames = $values = array();
283 foreach ($foundDupes as $dupe) {
287 $cidString = implode(', ', array_keys($cids));
288 $sql = "SELECT id, display_name FROM civicrm_contact WHERE id IN ($cidString) ORDER BY sort_name";
289 $dao = new CRM_Core_DAO();
291 while ($dao->fetch()) {
292 $displayNames[$dao->id
] = $dao->display_name
;
295 $session = CRM_Core_Session
::singleton();
296 $userId = $session->get('userID');
298 foreach ($foundDupes as $dupes) {
301 if ($dstID == $userId) {
308 'srcName' => $displayNames[$srcID],
310 'dstName' => $displayNames[$dstID],
311 'weight' => $dupes[2],
315 $data = CRM_Core_DAO
::escapeString(serialize($row));
316 $values[] = " ( 'civicrm_contact', $srcID, $dstID, '$cacheKeyString', '$data' ) ";
318 self
::setItem($values);
322 public static function cleanupCache() {
323 // clean up all prev next caches older than $cacheTimeIntervalDays days
324 $cacheTimeIntervalDays = 2;
326 // first find all the cacheKeys that match this
330 INNER JOIN civicrm_prevnext_cache pn ON c.path = pn.cacheKey
331 WHERE c.group_name = %1
332 AND c.created_date < date_sub( NOW( ), INTERVAL %2 day )
335 1 => array('CiviCRM Search PrevNextCache', 'String'),
336 2 => array($cacheTimeIntervalDays, 'Integer'),
338 CRM_Core_DAO
::executeQuery($sql, $params);
342 * Save checkbox selections
345 * @param string $action
347 * @param string $entity_table
349 public static function markSelection($cacheKey, $action = 'unselect', $cIds = NULL, $entity_table = 'civicrm_contact') {
355 $entity_whereClause = " AND entity_table = '{$entity_table}'";
356 if ($cIds && $cacheKey && $action) {
357 if (is_array($cIds)) {
358 $cIdFilter = "(" . implode(',', $cIds) . ")";
360 WHERE cacheKey LIKE %1
361 AND (entity_id1 IN {$cIdFilter} OR entity_id2 IN {$cIdFilter})
366 WHERE cacheKey LIKE %1
367 AND (entity_id1 = %2 OR entity_id2 = %2)
369 $params[2] = array("{$cIds}", 'Integer');
371 if ($action == 'select') {
372 $whereClause .= "AND is_selected = 0";
373 $sql = "UPDATE civicrm_prevnext_cache SET is_selected = 1 {$whereClause} {$entity_whereClause}";
374 $params[1] = array("{$cacheKey}%", 'String');
376 elseif ($action == 'unselect') {
377 $whereClause .= "AND is_selected = 1";
378 $sql = "UPDATE civicrm_prevnext_cache SET is_selected = 0 {$whereClause} {$entity_whereClause}";
379 $params[1] = array("%{$cacheKey}%", 'String');
381 // default action is reseting
383 elseif (!$cIds && $cacheKey && $action == 'unselect') {
385 UPDATE civicrm_prevnext_cache
387 WHERE cacheKey LIKE %1 AND is_selected = 1
388 {$entity_whereClause}
390 $params[1] = array("{$cacheKey}%", 'String');
392 CRM_Core_DAO
::executeQuery($sql, $params);
398 * @param string $cacheKey
400 * @param string $action
402 * $action : get - get only selection records
403 * getall - get all the records of the specified cache key
404 * @param string $entity_table
409 public static function getSelection($cacheKey, $action = 'get', $entity_table = 'civicrm_contact') {
415 $entity_whereClause = " AND entity_table = '{$entity_table}'";
416 if ($cacheKey && ($action == 'get' ||
$action == 'getall')) {
417 $actionGet = ($action == "get") ?
" AND is_selected = 1 " : "";
419 SELECT entity_id1, entity_id2 FROM civicrm_prevnext_cache
420 WHERE cacheKey LIKE %1
425 $params[1] = array("{$cacheKey}%", 'String');
427 $contactIds = array($cacheKey => array());
428 $cIdDao = CRM_Core_DAO
::executeQuery($sql, $params);
429 while ($cIdDao->fetch()) {
430 if ($cIdDao->entity_id1
== $cIdDao->entity_id2
) {
431 $contactIds[$cacheKey][$cIdDao->entity_id1
] = 1;
441 public static function getSelectedContacts() {
442 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String');
443 $cacheKey = "civicrm search {$qfKey}";
446 FROM civicrm_prevnext_cache
447 WHERE cacheKey LIKE %1
449 AND cacheKey NOT LIKE %2
451 $params1[1] = array("{$cacheKey}%", 'String');
452 $params1[2] = array("{$cacheKey}_alphabet%", 'String');
453 $dao = CRM_Core_DAO
::executeQuery($query, $params1);
456 while ($dao->fetch()) {
463 * @param CRM_Core_Form $form
464 * @param array $params
468 public static function buildSelectedContactPager(&$form, &$params) {
469 $params['status'] = ts('Contacts %%StatusMessage%%');
470 $params['csvString'] = NULL;
471 $params['buttonTop'] = 'PagerTopButton';
472 $params['buttonBottom'] = 'PagerBottomButton';
473 $params['rowCount'] = $form->get(CRM_Utils_Pager
::PAGE_ROWCOUNT
);
475 if (!$params['rowCount']) {
476 $params['rowCount'] = CRM_Utils_Pager
::ROWCOUNT
;
479 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', $form);
480 $cacheKey = "civicrm search {$qfKey}";
484 FROM civicrm_prevnext_cache
485 WHERE cacheKey LIKE %1
487 AND cacheKey NOT LIKE %2
489 $params1[1] = array("{$cacheKey}%", 'String');
490 $params1[2] = array("{$cacheKey}_alphabet%", 'String');
491 $paramsTotal = CRM_Core_DAO
::singleValueQuery($query, $params1);
492 $params['total'] = $paramsTotal;
493 $form->_pager
= new CRM_Utils_Pager($params);
494 $form->assign_by_ref('pager', $form->_pager
);
495 list($offset, $rowCount) = $form->_pager
->getOffsetAndRowCount();
496 $params['offset'] = $offset;
497 $params['rowCount1'] = $rowCount;