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
{
37 * Days for cache to llast forr
42 * Store the results of a SQL query in the cache.
43 * @param string $cacheKey
45 * A SQL query. The query *MUST* be a SELECT statement which yields
46 * the following columns (in order): cachekey, entity_id1, data
47 * @param array $sqlParams
48 * An array of parameters to be used with $sql.
49 * Use the same interpolation format as CRM_Core_DAO (composeQuery/executeQuery).
50 * Ex: [1 => ['foo', 'String']]
52 * @throws CRM_Core_Exception
53 * @see CRM_Core_DAO::composeQuery
55 public function fillWithSql($cacheKey, $sql, $sqlParams = []) {
57 INSERT INTO civicrm_prevnext_cache (cachekey, entity_id1, data)
59 $result = CRM_Core_DAO
::executeQuery($insertSQL . $sql, $sqlParams, FALSE, NULL, FALSE, TRUE, TRUE);
60 if (is_a($result, 'DB_Error')) {
61 throw new CRM_Core_Exception($result->message
);
66 public function fillWithArray($cacheKey, $rows) {
71 $insert = CRM_Utils_SQL_Insert
::into('civicrm_prevnext_cache')
78 foreach ($rows as &$row) {
79 $insert->row($row +
['cachekey' => $cacheKey]);
82 CRM_Core_DAO
::executeQuery($insert->toSQL());
87 * Save checkbox selections.
89 * @param string $cacheKey
90 * @param string $action
91 * Ex: 'select', 'unselect'.
92 * @param array|int|null $ids
93 * A list of contact IDs to (un)select.
94 * To unselect all contact IDs, use NULL.
96 public function markSelection($cacheKey, $action, $ids = NULL) {
102 if ($ids && $cacheKey && $action) {
103 if (is_array($ids)) {
104 $cIdFilter = "(" . implode(',', $ids) . ")";
107 AND (entity_id1 IN {$cIdFilter} OR entity_id2 IN {$cIdFilter})
113 AND (entity_id1 = %2 OR entity_id2 = %2)
115 $params[2] = ["{$ids}", 'Integer'];
117 if ($action == 'select') {
118 $whereClause .= "AND is_selected = 0";
119 $sql = "UPDATE civicrm_prevnext_cache SET is_selected = 1 {$whereClause}";
120 $params[1] = [$cacheKey, 'String'];
122 elseif ($action == 'unselect') {
123 $whereClause .= "AND is_selected = 1";
124 $sql = "UPDATE civicrm_prevnext_cache SET is_selected = 0 {$whereClause}";
125 $params[1] = [$cacheKey, 'String'];
127 // default action is reseting
129 elseif (!$ids && $cacheKey && $action == 'unselect') {
131 UPDATE civicrm_prevnext_cache
133 WHERE cachekey = %1 AND is_selected = 1
135 $params[1] = [$cacheKey, 'String'];
137 CRM_Core_DAO
::executeQuery($sql, $params);
141 * Get the selections.
143 * @param string $cacheKey
145 * @param string $action
146 * One of the following:
147 * - 'get' - get only selection records
148 * - 'getall' - get all the records of the specified cache key
152 public function getSelection($cacheKey, $action = 'get') {
158 if ($cacheKey && ($action == 'get' ||
$action == 'getall')) {
159 $actionGet = ($action == "get") ?
" AND is_selected = 1 " : "";
161 SELECT entity_id1 FROM civicrm_prevnext_cache
166 $params[1] = [$cacheKey, 'String'];
168 $contactIds = [$cacheKey => []];
169 $cIdDao = CRM_Core_DAO
::executeQuery($sql, $params);
170 while ($cIdDao->fetch()) {
171 $contactIds[$cacheKey][$cIdDao->entity_id1
] = 1;
178 * Get the previous and next keys.
180 * @param string $cacheKey
185 public function getPositions($cacheKey, $id1) {
186 $mergeId = CRM_Core_DAO
::singleValueQuery(
187 "SELECT id FROM civicrm_prevnext_cache WHERE cachekey = %2 AND entity_id1 = %1",
189 1 => [$id1, 'Integer'],
190 2 => [$cacheKey, 'String'],
194 $pos = ['foundEntry' => 0];
196 $pos['foundEntry'] = 1;
198 $sql = "SELECT pn.id, pn.entity_id1, pn.entity_id2, pn.data FROM civicrm_prevnext_cache pn ";
199 $wherePrev = " WHERE pn.id < %1 AND pn.cachekey = %2 ORDER BY ID DESC LIMIT 1";
200 $whereNext = " WHERE pn.id > %1 AND pn.cachekey = %2 ORDER BY ID ASC LIMIT 1";
202 1 => [$mergeId, 'Integer'],
203 2 => [$cacheKey, 'String'],
206 $dao = CRM_Core_DAO
::executeQuery($sql . $wherePrev, $p);
208 $pos['prev']['id1'] = $dao->entity_id1
;
209 $pos['prev']['mergeId'] = $dao->id
;
210 $pos['prev']['data'] = $dao->data
;
213 $dao = CRM_Core_DAO
::executeQuery($sql . $whereNext, $p);
215 $pos['next']['id1'] = $dao->entity_id1
;
216 $pos['next']['mergeId'] = $dao->id
;
217 $pos['next']['data'] = $dao->data
;
225 * Delete an item from the prevnext cache table based on the entity.
228 * @param string $cacheKey
230 public function deleteItem($id = NULL, $cacheKey = NULL) {
231 $sql = "DELETE FROM civicrm_prevnext_cache WHERE (1)";
234 if (is_numeric($id)) {
235 $sql .= " AND ( entity_id1 = %2 OR entity_id2 = %2 )";
236 $params[2] = [$id, 'Integer'];
239 if (isset($cacheKey)) {
240 $sql .= " AND cachekey = %3";
241 $params[3] = [$cacheKey, 'String'];
243 CRM_Core_DAO
::executeQuery($sql, $params);
247 * Get count of matching rows.
249 * @param string $cacheKey
252 public function getCount($cacheKey) {
253 $query = "SELECT COUNT(*) FROM civicrm_prevnext_cache pn WHERE pn.cachekey = %1";
254 $params = [1 => [$cacheKey, 'String']];
255 return (int) CRM_Core_DAO
::singleValueQuery($query, $params, TRUE, FALSE);
259 * Fetch a list of contacts from the prev/next cache for displaying a search results page
261 * @param string $cacheKey
263 * @param int $rowCount
265 * List of contact IDs.
267 public function fetch($cacheKey, $offset, $rowCount) {
269 $dao = CRM_Utils_SQL_Select
::from('civicrm_prevnext_cache pnc')
270 ->where('pnc.cachekey = @cacheKey', ['cacheKey' => $cacheKey])
271 ->select('pnc.entity_id1 as cid')
273 ->limit($rowCount, $offset)
275 while ($dao->fetch()) {
284 public function cleanup() {
285 // clean up all prev next caches older than $cacheTimeIntervalDays days
286 // first find all the cacheKeys that match this
290 INNER JOIN civicrm_prevnext_cache pn ON c.path = pn.cachekey
291 WHERE c.group_name = %1
292 AND c.created_date < date_sub( NOW( ), INTERVAL %2 day )
295 1 => [CRM_Core_BAO_Cache
::cleanKey('CiviCRM Search PrevNextCache'), 'String'],
296 2 => [self
::cacheDays
, 'Integer'],
298 CRM_Core_DAO
::executeQuery($sql, $params);