From 0b8038a60b80079c8fbda9422d2ac13437e286c0 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Mon, 2 Jul 2018 12:27:38 -0700 Subject: [PATCH] (dev/core#217) PrevNext - Migrate `markSelection()` from BAO to service interface --- CRM/Contact/Page/AJAX.php | 6 +-- CRM/Core/BAO/PrevNextCache.php | 54 -------------------------- CRM/Core/PrevNextCache/Interface.php | 14 +++++++ CRM/Core/PrevNextCache/Sql.php | 58 ++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 57 deletions(-) diff --git a/CRM/Contact/Page/AJAX.php b/CRM/Contact/Page/AJAX.php index 79fa957b6c..031e04fb69 100644 --- a/CRM/Contact/Page/AJAX.php +++ b/CRM/Contact/Page/AJAX.php @@ -966,17 +966,17 @@ LIMIT {$offset}, {$rowCount} $elements[$key] = self::_convertToId($element); } CRM_Utils_Type::escapeAll($elements, 'Integer'); - CRM_Core_BAO_PrevNextCache::markSelection($cacheKey, $actionToPerform, $elements); + Civi::service('prevnext')->markSelection($cacheKey, $actionToPerform, $elements); } else { - CRM_Core_BAO_PrevNextCache::markSelection($cacheKey, $actionToPerform); + Civi::service('prevnext')->markSelection($cacheKey, $actionToPerform); } } elseif ($variableType == 'single') { $cId = self::_convertToId($name); CRM_Utils_Type::escape($cId, 'Integer'); $action = ($state == 'checked') ? 'select' : 'unselect'; - CRM_Core_BAO_PrevNextCache::markSelection($cacheKey, $action, $cId); + Civi::service('prevnext')->markSelection($cacheKey, $action, $cId); } $contactIds = CRM_Core_BAO_PrevNextCache::getSelection($cacheKey); $countSelectionCids = count($contactIds[$cacheKey]); diff --git a/CRM/Core/BAO/PrevNextCache.php b/CRM/Core/BAO/PrevNextCache.php index 084f7709ce..3c330ef418 100644 --- a/CRM/Core/BAO/PrevNextCache.php +++ b/CRM/Core/BAO/PrevNextCache.php @@ -438,60 +438,6 @@ AND c.created_date < date_sub( NOW( ), INTERVAL %2 day ) CRM_Core_DAO::executeQuery($sql, $params); } - /** - * Save checkbox selections. - * - * @param $cacheKey - * @param string $action - * @param array $cIds - * @param string $entity_table - */ - public static function markSelection($cacheKey, $action = 'unselect', $cIds = NULL, $entity_table = 'civicrm_contact') { - if (!$cacheKey) { - return; - } - $params = array(); - - $entity_whereClause = " AND entity_table = '{$entity_table}'"; - if ($cIds && $cacheKey && $action) { - if (is_array($cIds)) { - $cIdFilter = "(" . implode(',', $cIds) . ")"; - $whereClause = " -WHERE cacheKey LIKE %1 -AND (entity_id1 IN {$cIdFilter} OR entity_id2 IN {$cIdFilter}) -"; - } - else { - $whereClause = " -WHERE cacheKey LIKE %1 -AND (entity_id1 = %2 OR entity_id2 = %2) -"; - $params[2] = array("{$cIds}", 'Integer'); - } - if ($action == 'select') { - $whereClause .= "AND is_selected = 0"; - $sql = "UPDATE civicrm_prevnext_cache SET is_selected = 1 {$whereClause} {$entity_whereClause}"; - $params[1] = array("{$cacheKey}%", 'String'); - } - elseif ($action == 'unselect') { - $whereClause .= "AND is_selected = 1"; - $sql = "UPDATE civicrm_prevnext_cache SET is_selected = 0 {$whereClause} {$entity_whereClause}"; - $params[1] = array("%{$cacheKey}%", 'String'); - } - // default action is reseting - } - elseif (!$cIds && $cacheKey && $action == 'unselect') { - $sql = " -UPDATE civicrm_prevnext_cache -SET is_selected = 0 -WHERE cacheKey LIKE %1 AND is_selected = 1 - {$entity_whereClause} -"; - $params[1] = array("{$cacheKey}%", 'String'); - } - CRM_Core_DAO::executeQuery($sql, $params); - } - /** * Get the selections. * diff --git a/CRM/Core/PrevNextCache/Interface.php b/CRM/Core/PrevNextCache/Interface.php index 5220ca205c..fbc937b8ce 100644 --- a/CRM/Core/PrevNextCache/Interface.php +++ b/CRM/Core/PrevNextCache/Interface.php @@ -58,4 +58,18 @@ interface CRM_Core_PrevNextCache_Interface { */ public function fillWithArray($cacheKey, $rows); + /** + * Save checkbox selections. + * + * @param string $cacheKey + * @param string $action + * Ex: 'select', 'unselect'. + * @param array|int|NULL $cIds + * A list of contact IDs to (un)select. + * To unselect all contact IDs, use NULL. + * @param string $entity_table + * Ex: 'civicrm_contact'. + */ + public function markSelection($cacheKey, $action = 'unselect', $cIds = NULL, $entity_table = 'civicrm_contact'); + } diff --git a/CRM/Core/PrevNextCache/Sql.php b/CRM/Core/PrevNextCache/Sql.php index 7882e9ad83..1f3e0a2c31 100644 --- a/CRM/Core/PrevNextCache/Sql.php +++ b/CRM/Core/PrevNextCache/Sql.php @@ -74,4 +74,62 @@ INSERT INTO civicrm_prevnext_cache ( entity_table, entity_id1, entity_id2, cache return TRUE; } + /** + * Save checkbox selections. + * + * @param string $cacheKey + * @param string $action + * Ex: 'select', 'unselect'. + * @param array|int|NULL $cIds + * A list of contact IDs to (un)select. + * To unselect all contact IDs, use NULL. + * @param string $entity_table + * Ex: 'civicrm_contact'. + */ + public function markSelection($cacheKey, $action = 'unselect', $cIds = NULL, $entity_table = 'civicrm_contact') { + if (!$cacheKey) { + return; + } + $params = array(); + + $entity_whereClause = " AND entity_table = '{$entity_table}'"; + if ($cIds && $cacheKey && $action) { + if (is_array($cIds)) { + $cIdFilter = "(" . implode(',', $cIds) . ")"; + $whereClause = " +WHERE cacheKey LIKE %1 +AND (entity_id1 IN {$cIdFilter} OR entity_id2 IN {$cIdFilter}) +"; + } + else { + $whereClause = " +WHERE cacheKey LIKE %1 +AND (entity_id1 = %2 OR entity_id2 = %2) +"; + $params[2] = array("{$cIds}", 'Integer'); + } + if ($action == 'select') { + $whereClause .= "AND is_selected = 0"; + $sql = "UPDATE civicrm_prevnext_cache SET is_selected = 1 {$whereClause} {$entity_whereClause}"; + $params[1] = array("{$cacheKey}%", 'String'); + } + elseif ($action == 'unselect') { + $whereClause .= "AND is_selected = 1"; + $sql = "UPDATE civicrm_prevnext_cache SET is_selected = 0 {$whereClause} {$entity_whereClause}"; + $params[1] = array("%{$cacheKey}%", 'String'); + } + // default action is reseting + } + elseif (!$cIds && $cacheKey && $action == 'unselect') { + $sql = " +UPDATE civicrm_prevnext_cache +SET is_selected = 0 +WHERE cacheKey LIKE %1 AND is_selected = 1 + {$entity_whereClause} +"; + $params[1] = array("{$cacheKey}%", 'String'); + } + CRM_Core_DAO::executeQuery($sql, $params); + } + } -- 2.25.1