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;
94 $dao = CRM_Core_DAO
::executeQuery($sqlPrev, $p);
96 $pos['prev']['id1'] = $dao->entity_id1
;
97 $pos['prev']['id2'] = $dao->entity_id2
;
98 $pos['prev']['mergeId'] = $dao->id
;
99 $pos['prev']['data'] = $dao->data
;
102 $whereNext = " WHERE pn.id > %1 AND pn.cacheKey = %2 {$where} ORDER BY ID ASC LIMIT 1";
103 $sqlNext = $sql . $whereNext;
105 $dao = CRM_Core_DAO
::executeQuery($sqlNext, $p);
107 $pos['next']['id1'] = $dao->entity_id1
;
108 $pos['next']['id2'] = $dao->entity_id2
;
109 $pos['next']['mergeId'] = $dao->id
;
110 $pos['next']['data'] = $dao->data
;
118 * @param NULL $cacheKey
119 * @param string $entityTable
121 public static function deleteItem($id = NULL, $cacheKey = NULL, $entityTable = 'civicrm_contact') {
124 $sql = "DELETE FROM civicrm_prevnext_cache WHERE entity_table = %1";
125 $params = array(1 => array($entityTable, 'String'));
127 if (is_numeric($id)) {
128 $sql .= " AND ( entity_id1 = %2 OR entity_id2 = %2 )";
129 $params[2] = array($id, 'Integer');
132 if (isset($cacheKey)) {
133 $sql .= " AND cacheKey LIKE %3";
134 $params[3] = array("{$cacheKey}%", 'String');
136 CRM_Core_DAO
::executeQuery($sql, $params);
142 * @param NULL $cacheKey
143 * @param bool $isViceVersa
144 * @param string $entityTable
146 public static function deletePair($id1, $id2, $cacheKey = NULL, $isViceVersa = FALSE, $entityTable = 'civicrm_contact') {
147 $sql = "DELETE FROM civicrm_prevnext_cache WHERE entity_table = %1";
148 $params = array(1 => array($entityTable, 'String'));
152 "entity_id1 = %2 AND entity_id2 = %3" :
153 "(entity_id1 = %2 AND entity_id2 = %3) OR (entity_id1 = %3 AND entity_id2 = %2)";
154 $sql .= " AND ( {$pair} )";
155 $params[2] = array($id1, 'Integer');
156 $params[3] = array($id2, 'Integer');
158 if (isset($cacheKey)) {
159 $sql .= " AND cacheKey LIKE %4";
160 $params[4] = array("{$cacheKey}%", 'String');
163 CRM_Core_DAO
::executeQuery($sql, $params);
171 * @param int $rowCount
175 public static function retrieve($cacheKey, $join = NULL, $where = NULL, $offset = 0, $rowCount = 0) {
178 FROM civicrm_prevnext_cache pn
182 $params = array(1 => array($cacheKey, 'String'));
185 $query .= " AND {$where}";
189 $offset = CRM_Utils_Type
::escape($offset, 'Int');
190 $rowCount = CRM_Utils_Type
::escape($rowCount, 'Int');
192 $query .= " LIMIT {$offset}, {$rowCount}";
195 $dao = CRM_Core_DAO
::executeQuery($query, $params);
198 while ($dao->fetch()) {
199 if (self
::is_serialized($dao->data
)) {
200 $main[] = unserialize($dao->data
);
203 $main[] = $dao->data
;
215 public static function is_serialized($string) {
216 return (@unserialize
($string) !== FALSE);
222 public static function setItem($values) {
223 $insert = "INSERT INTO civicrm_prevnext_cache ( entity_table, entity_id1, entity_id2, cacheKey, data ) VALUES \n";
224 $query = $insert . implode(",\n ", $values);
226 //dump the dedupe matches in the prevnext_cache table
227 CRM_Core_DAO
::executeQuery($query);
238 public static function getCount($cacheKey, $join = NULL, $where = NULL, $op = "=") {
240 SELECT COUNT(*) FROM civicrm_prevnext_cache pn
242 WHERE cacheKey $op %1
245 $query .= " AND {$where}";
248 $params = array(1 => array($cacheKey, 'String'));
249 return (int) CRM_Core_DAO
::singleValueQuery($query, $params, TRUE, FALSE);
255 * @param NULL $cacheKeyString
257 public static function refillCache($rgid = NULL, $gid = NULL, $cacheKeyString = NULL) {
258 if (!$cacheKeyString && $rgid) {
259 $contactType = CRM_Core_DAO
::getFieldValue('CRM_Dedupe_DAO_RuleGroup', $rgid, 'contact_type');
260 $cacheKeyString = "merge {$contactType}";
261 $cacheKeyString .= $rgid ?
"_{$rgid}" : '_0';
262 $cacheKeyString .= $gid ?
"_{$gid}" : '_0';
265 if (!$cacheKeyString) {
269 // 1. Clear cache if any
270 $sql = "DELETE FROM civicrm_prevnext_cache WHERE cacheKey LIKE %1";
271 CRM_Core_DAO
::executeQuery($sql, array(1 => array("{$cacheKeyString}%", 'String')));
273 // FIXME: we need to start using temp tables / queries here instead of arrays.
274 // And cleanup code in CRM/Contact/Page/DedupeFind.php
277 $foundDupes = array();
279 $foundDupes = CRM_Dedupe_Finder
::dupesInGroup($rgid, $gid);
282 $foundDupes = CRM_Dedupe_Finder
::dupes($rgid);
285 if (!empty($foundDupes)) {
286 $cids = $displayNames = $values = array();
287 foreach ($foundDupes as $dupe) {
291 $cidString = implode(', ', array_keys($cids));
292 $sql = "SELECT id, display_name FROM civicrm_contact WHERE id IN ($cidString) ORDER BY sort_name";
293 $dao = new CRM_Core_DAO();
295 while ($dao->fetch()) {
296 $displayNames[$dao->id
] = $dao->display_name
;
299 $session = CRM_Core_Session
::singleton();
300 $userId = $session->get('userID');
302 foreach ($foundDupes as $dupes) {
305 if ($dstID == $userId) {
312 'srcName' => $displayNames[$srcID],
314 'dstName' => $displayNames[$dstID],
315 'weight' => $dupes[2],
319 $data = CRM_Core_DAO
::escapeString(serialize($row));
320 $values[] = " ( 'civicrm_contact', $srcID, $dstID, '$cacheKeyString', '$data' ) ";
322 self
::setItem($values);
326 public static function cleanupCache() {
327 // clean up all prev next caches older than $cacheTimeIntervalDays days
328 $cacheTimeIntervalDays = 2;
330 // first find all the cacheKeys that match this
334 INNER JOIN civicrm_prevnext_cache pn ON c.path = pn.cacheKey
335 WHERE c.group_name = %1
336 AND c.created_date < date_sub( NOW( ), INTERVAL %2 day )
339 1 => array('CiviCRM Search PrevNextCache', 'String'),
340 2 => array($cacheTimeIntervalDays, 'Integer'),
342 CRM_Core_DAO
::executeQuery($sql, $params);
346 * Save checkbox selections
349 * @param string $action
351 * @param string $entity_table
353 public static function markSelection($cacheKey, $action = 'unselect', $cIds = NULL, $entity_table = 'civicrm_contact') {
359 $entity_whereClause = " AND entity_table = '{$entity_table}'";
360 if ($cIds && $cacheKey && $action) {
361 if (is_array($cIds)) {
362 $cIdFilter = "(" . implode(',', $cIds) . ")";
364 WHERE cacheKey LIKE %1
365 AND (entity_id1 IN {$cIdFilter} OR entity_id2 IN {$cIdFilter})
370 WHERE cacheKey LIKE %1
371 AND (entity_id1 = %2 OR entity_id2 = %2)
373 $params[2] = array("{$cIds}", 'Integer');
375 if ($action == 'select') {
376 $whereClause .= "AND is_selected = 0";
377 $sql = "UPDATE civicrm_prevnext_cache SET is_selected = 1 {$whereClause} {$entity_whereClause}";
378 $params[1] = array("{$cacheKey}%", 'String');
380 elseif ($action == 'unselect') {
381 $whereClause .= "AND is_selected = 1";
382 $sql = "UPDATE civicrm_prevnext_cache SET is_selected = 0 {$whereClause} {$entity_whereClause}";
383 $params[1] = array("%{$cacheKey}%", 'String');
385 // default action is reseting
387 elseif (!$cIds && $cacheKey && $action == 'unselect') {
389 UPDATE civicrm_prevnext_cache
391 WHERE cacheKey LIKE %1 AND is_selected = 1
392 {$entity_whereClause}
394 $params[1] = array("{$cacheKey}%", 'String');
396 CRM_Core_DAO
::executeQuery($sql, $params);
402 * @param string $cacheKey
404 * @param string $action
406 * $action : get - get only selection records
407 * getall - get all the records of the specified cache key
408 * @param string $entity_table
413 public static function getSelection($cacheKey, $action = 'get', $entity_table = 'civicrm_contact') {
419 $entity_whereClause = " AND entity_table = '{$entity_table}'";
420 if ($cacheKey && ($action == 'get' ||
$action == 'getall')) {
421 $actionGet = ($action == "get") ?
" AND is_selected = 1 " : "";
423 SELECT entity_id1, entity_id2 FROM civicrm_prevnext_cache
424 WHERE cacheKey LIKE %1
429 $params[1] = array("{$cacheKey}%", 'String');
431 $contactIds = array($cacheKey => array());
432 $cIdDao = CRM_Core_DAO
::executeQuery($sql, $params);
433 while ($cIdDao->fetch()) {
434 if ($cIdDao->entity_id1
== $cIdDao->entity_id2
) {
435 $contactIds[$cacheKey][$cIdDao->entity_id1
] = 1;
445 public static function getSelectedContacts() {
446 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String');
447 $cacheKey = "civicrm search {$qfKey}";
450 FROM civicrm_prevnext_cache
451 WHERE cacheKey LIKE %1
453 AND cacheKey NOT LIKE %2
455 $params1[1] = array("{$cacheKey}%", 'String');
456 $params1[2] = array("{$cacheKey}_alphabet%", 'String');
457 $dao = CRM_Core_DAO
::executeQuery($query, $params1);
460 while ($dao->fetch()) {
467 * @param CRM_Core_Form $form
468 * @param array $params
472 public static function buildSelectedContactPager(&$form, &$params) {
473 $params['status'] = ts('Contacts %%StatusMessage%%');
474 $params['csvString'] = NULL;
475 $params['buttonTop'] = 'PagerTopButton';
476 $params['buttonBottom'] = 'PagerBottomButton';
477 $params['rowCount'] = $form->get(CRM_Utils_Pager
::PAGE_ROWCOUNT
);
479 if (!$params['rowCount']) {
480 $params['rowCount'] = CRM_Utils_Pager
::ROWCOUNT
;
483 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', $form);
484 $cacheKey = "civicrm search {$qfKey}";
488 FROM civicrm_prevnext_cache
489 WHERE cacheKey LIKE %1
491 AND cacheKey NOT LIKE %2
493 $params1[1] = array("{$cacheKey}%", 'String');
494 $params1[2] = array("{$cacheKey}_alphabet%", 'String');
495 $paramsTotal = CRM_Core_DAO
::singleValueQuery($query, $params1);
496 $params['total'] = $paramsTotal;
497 $form->_pager
= new CRM_Utils_Pager($params);
498 $form->assign_by_ref('pager', $form->_pager
);
499 list($offset, $rowCount) = $form->_pager
->getOffsetAndRowCount();
500 $params['offset'] = $offset;
501 $params['rowCount1'] = $rowCount;