3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
29 * Class CRM_Core_PrevNextCache_Sql
31 * Store the previous/next cache in a special-purpose SQL table.
33 class CRM_Core_PrevNextCache_Sql
implements CRM_Core_PrevNextCache_Interface
{
36 * Store the results of a SQL query in the cache.
39 * A SQL query. The query *MUST* be a SELECT statement which yields
40 * the following columns (in order): cacheKey, entity_id1, data
42 * @throws CRM_Core_Exception
44 public function fillWithSql($cacheKey, $sql) {
46 INSERT INTO civicrm_prevnext_cache (cacheKey, entity_id1, data)
48 $result = CRM_Core_DAO
::executeQuery($insertSQL . $sql, [], FALSE, NULL, FALSE, TRUE, TRUE);
49 if (is_a($result, 'DB_Error')) {
50 throw new CRM_Core_Exception($result->message
);
55 public function fillWithArray($cacheKey, $rows) {
60 $insert = CRM_Utils_SQL_Insert
::into('civicrm_prevnext_cache')
67 foreach ($rows as &$row) {
68 $insert->row($row +
['cacheKey' => $cacheKey]);
71 CRM_Core_DAO
::executeQuery($insert->toSQL());
76 * Save checkbox selections.
78 * @param string $cacheKey
79 * @param string $action
80 * Ex: 'select', 'unselect'.
81 * @param array|int|NULL $ids
82 * A list of contact IDs to (un)select.
83 * To unselect all contact IDs, use NULL.
85 public function markSelection($cacheKey, $action, $ids = NULL) {
91 if ($ids && $cacheKey && $action) {
93 $cIdFilter = "(" . implode(',', $ids) . ")";
96 AND (entity_id1 IN {$cIdFilter} OR entity_id2 IN {$cIdFilter})
102 AND (entity_id1 = %2 OR entity_id2 = %2)
104 $params[2] = array("{$ids}", 'Integer');
106 if ($action == 'select') {
107 $whereClause .= "AND is_selected = 0";
108 $sql = "UPDATE civicrm_prevnext_cache SET is_selected = 1 {$whereClause}";
109 $params[1] = array($cacheKey, 'String');
111 elseif ($action == 'unselect') {
112 $whereClause .= "AND is_selected = 1";
113 $sql = "UPDATE civicrm_prevnext_cache SET is_selected = 0 {$whereClause}";
114 $params[1] = array($cacheKey, 'String');
116 // default action is reseting
118 elseif (!$ids && $cacheKey && $action == 'unselect') {
120 UPDATE civicrm_prevnext_cache
122 WHERE cacheKey = %1 AND is_selected = 1
124 $params[1] = array($cacheKey, 'String');
126 CRM_Core_DAO
::executeQuery($sql, $params);
130 * Get the selections.
132 * @param string $cacheKey
134 * @param string $action
135 * One of the following:
136 * - 'get' - get only selection records
137 * - 'getall' - get all the records of the specified cache key
141 public function getSelection($cacheKey, $action = 'get') {
147 if ($cacheKey && ($action == 'get' ||
$action == 'getall')) {
148 $actionGet = ($action == "get") ?
" AND is_selected = 1 " : "";
150 SELECT entity_id1 FROM civicrm_prevnext_cache
155 $params[1] = array($cacheKey, 'String');
157 $contactIds = array($cacheKey => array());
158 $cIdDao = CRM_Core_DAO
::executeQuery($sql, $params);
159 while ($cIdDao->fetch()) {
160 $contactIds[$cacheKey][$cIdDao->entity_id1
] = 1;
167 * Get the previous and next keys.
169 * @param string $cacheKey
174 public function getPositions($cacheKey, $id1) {
175 $mergeId = CRM_Core_DAO
::singleValueQuery(
176 "SELECT id FROM civicrm_prevnext_cache WHERE cacheKey = %2 AND entity_id1 = %1",
178 1 => [$id1, 'Integer'],
179 2 => [$cacheKey, 'String'],
183 $pos = ['foundEntry' => 0];
185 $pos['foundEntry'] = 1;
187 $sql = "SELECT pn.id, pn.entity_id1, pn.entity_id2, pn.data FROM civicrm_prevnext_cache pn ";
188 $wherePrev = " WHERE pn.id < %1 AND pn.cacheKey = %2 ORDER BY ID DESC LIMIT 1";
189 $whereNext = " WHERE pn.id > %1 AND pn.cacheKey = %2 ORDER BY ID ASC LIMIT 1";
191 1 => [$mergeId, 'Integer'],
192 2 => [$cacheKey, 'String'],
195 $dao = CRM_Core_DAO
::executeQuery($sql . $wherePrev, $p);
197 $pos['prev']['id1'] = $dao->entity_id1
;
198 $pos['prev']['mergeId'] = $dao->id
;
199 $pos['prev']['data'] = $dao->data
;
202 $dao = CRM_Core_DAO
::executeQuery($sql . $whereNext, $p);
204 $pos['next']['id1'] = $dao->entity_id1
;
205 $pos['next']['mergeId'] = $dao->id
;
206 $pos['next']['data'] = $dao->data
;
214 * Delete an item from the prevnext cache table based on the entity.
217 * @param string $cacheKey
219 public function deleteItem($id = NULL, $cacheKey = NULL) {
220 $sql = "DELETE FROM civicrm_prevnext_cache WHERE (1)";
223 if (is_numeric($id)) {
224 $sql .= " AND ( entity_id1 = %2 OR entity_id2 = %2 )";
225 $params[2] = array($id, 'Integer');
228 if (isset($cacheKey)) {
229 $sql .= " AND cacheKey = %3";
230 $params[3] = array($cacheKey, 'String');
232 CRM_Core_DAO
::executeQuery($sql, $params);
236 * Get count of matching rows.
238 * @param string $cacheKey
241 public function getCount($cacheKey) {
242 $query = "SELECT COUNT(*) FROM civicrm_prevnext_cache pn WHERE pn.cacheKey = %1";
243 $params = [1 => [$cacheKey, 'String']];
244 return (int) CRM_Core_DAO
::singleValueQuery($query, $params, TRUE, FALSE);
248 * Fetch a list of contacts from the prev/next cache for displaying a search results page
250 * @param string $cacheKey
252 * @param int $rowCount
254 * List of contact IDs.
256 public function fetch($cacheKey, $offset, $rowCount) {
258 $dao = CRM_Utils_SQL_Select
::from('civicrm_prevnext_cache pnc')
259 ->where('pnc.cacheKey = @cacheKey', ['cacheKey' => $cacheKey])
260 ->select('pnc.entity_id1 as cid')
262 ->limit($rowCount, $offset)
264 while ($dao->fetch()) {