3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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): entity_table, entity_id1, entity_id2, cacheKey, data
42 * @throws CRM_Core_Exception
44 public function fillWithSql($cacheKey, $sql) {
46 INSERT INTO civicrm_prevnext_cache ( entity_table, entity_id1, entity_id2, cacheKey, 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')
69 foreach ($rows as &$row) {
70 $insert->row($row +
['cacheKey' => $cacheKey]);
73 CRM_Core_DAO
::executeQuery($insert->toSQL());
78 * Save checkbox selections.
80 * @param string $cacheKey
81 * @param string $action
82 * Ex: 'select', 'unselect'.
83 * @param array|int|NULL $cIds
84 * A list of contact IDs to (un)select.
85 * To unselect all contact IDs, use NULL.
86 * @param string $entity_table
87 * Ex: 'civicrm_contact'.
89 public function markSelection($cacheKey, $action = 'unselect', $cIds = NULL, $entity_table = 'civicrm_contact') {
95 $entity_whereClause = " AND entity_table = '{$entity_table}'";
96 if ($cIds && $cacheKey && $action) {
97 if (is_array($cIds)) {
98 $cIdFilter = "(" . implode(',', $cIds) . ")";
100 WHERE cacheKey LIKE %1
101 AND (entity_id1 IN {$cIdFilter} OR entity_id2 IN {$cIdFilter})
106 WHERE cacheKey LIKE %1
107 AND (entity_id1 = %2 OR entity_id2 = %2)
109 $params[2] = array("{$cIds}", 'Integer');
111 if ($action == 'select') {
112 $whereClause .= "AND is_selected = 0";
113 $sql = "UPDATE civicrm_prevnext_cache SET is_selected = 1 {$whereClause} {$entity_whereClause}";
114 $params[1] = array("{$cacheKey}%", 'String');
116 elseif ($action == 'unselect') {
117 $whereClause .= "AND is_selected = 1";
118 $sql = "UPDATE civicrm_prevnext_cache SET is_selected = 0 {$whereClause} {$entity_whereClause}";
119 $params[1] = array("%{$cacheKey}%", 'String');
121 // default action is reseting
123 elseif (!$cIds && $cacheKey && $action == 'unselect') {
125 UPDATE civicrm_prevnext_cache
127 WHERE cacheKey LIKE %1 AND is_selected = 1
128 {$entity_whereClause}
130 $params[1] = array("{$cacheKey}%", 'String');
132 CRM_Core_DAO
::executeQuery($sql, $params);
136 * Get the selections.
138 * @param string $cacheKey
140 * @param string $action
142 * $action : get - get only selection records
143 * getall - get all the records of the specified cache key
144 * @param string $entity_table
149 public function getSelection($cacheKey, $action = 'get', $entity_table = 'civicrm_contact') {
155 $entity_whereClause = " AND entity_table = '{$entity_table}'";
156 if ($cacheKey && ($action == 'get' ||
$action == 'getall')) {
157 $actionGet = ($action == "get") ?
" AND is_selected = 1 " : "";
159 SELECT entity_id1, entity_id2 FROM civicrm_prevnext_cache
160 WHERE cacheKey LIKE %1
165 $params[1] = array("{$cacheKey}%", 'String');
167 $contactIds = array($cacheKey => array());
168 $cIdDao = CRM_Core_DAO
::executeQuery($sql, $params);
169 while ($cIdDao->fetch()) {
170 if ($cIdDao->entity_id1
== $cIdDao->entity_id2
) {
171 $contactIds[$cacheKey][$cIdDao->entity_id1
] = 1;