(dev/core#217) PrevNext - Migrate `getSelection()` from BAO to service interface
[civicrm-core.git] / CRM / Core / PrevNextCache / Sql.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Class CRM_Core_PrevNextCache_Sql
30 *
31 * Store the previous/next cache in a special-purpose SQL table.
32 */
33 class CRM_Core_PrevNextCache_Sql implements CRM_Core_PrevNextCache_Interface {
34
35 /**
36 * Store the results of a SQL query in the cache.
37 *
38 * @param string $sql
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
41 * @return bool
42 * @throws CRM_Core_Exception
43 */
44 public function fillWithSql($cacheKey, $sql) {
45 $insertSQL = "
46 INSERT INTO civicrm_prevnext_cache ( entity_table, entity_id1, entity_id2, cacheKey, data )
47 ";
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);
51 }
52 return TRUE;
53 }
54
55 public function fillWithArray($cacheKey, $rows) {
56 if (empty($rows)) {
57 return;
58 }
59
60 $insert = CRM_Utils_SQL_Insert::into('civicrm_prevnext_cache')
61 ->columns([
62 'entity_table',
63 'entity_id1',
64 'entity_id2',
65 'cacheKey',
66 'data'
67 ]);
68
69 foreach ($rows as &$row) {
70 $insert->row($row + ['cacheKey' => $cacheKey]);
71 }
72
73 CRM_Core_DAO::executeQuery($insert->toSQL());
74 return TRUE;
75 }
76
77 /**
78 * Save checkbox selections.
79 *
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'.
88 */
89 public function markSelection($cacheKey, $action = 'unselect', $cIds = NULL, $entity_table = 'civicrm_contact') {
90 if (!$cacheKey) {
91 return;
92 }
93 $params = array();
94
95 $entity_whereClause = " AND entity_table = '{$entity_table}'";
96 if ($cIds && $cacheKey && $action) {
97 if (is_array($cIds)) {
98 $cIdFilter = "(" . implode(',', $cIds) . ")";
99 $whereClause = "
100 WHERE cacheKey LIKE %1
101 AND (entity_id1 IN {$cIdFilter} OR entity_id2 IN {$cIdFilter})
102 ";
103 }
104 else {
105 $whereClause = "
106 WHERE cacheKey LIKE %1
107 AND (entity_id1 = %2 OR entity_id2 = %2)
108 ";
109 $params[2] = array("{$cIds}", 'Integer');
110 }
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');
115 }
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');
120 }
121 // default action is reseting
122 }
123 elseif (!$cIds && $cacheKey && $action == 'unselect') {
124 $sql = "
125 UPDATE civicrm_prevnext_cache
126 SET is_selected = 0
127 WHERE cacheKey LIKE %1 AND is_selected = 1
128 {$entity_whereClause}
129 ";
130 $params[1] = array("{$cacheKey}%", 'String');
131 }
132 CRM_Core_DAO::executeQuery($sql, $params);
133 }
134
135 /**
136 * Get the selections.
137 *
138 * @param string $cacheKey
139 * Cache key.
140 * @param string $action
141 * Action.
142 * $action : get - get only selection records
143 * getall - get all the records of the specified cache key
144 * @param string $entity_table
145 * Entity table.
146 *
147 * @return array|NULL
148 */
149 public function getSelection($cacheKey, $action = 'get', $entity_table = 'civicrm_contact') {
150 if (!$cacheKey) {
151 return NULL;
152 }
153 $params = array();
154
155 $entity_whereClause = " AND entity_table = '{$entity_table}'";
156 if ($cacheKey && ($action == 'get' || $action == 'getall')) {
157 $actionGet = ($action == "get") ? " AND is_selected = 1 " : "";
158 $sql = "
159 SELECT entity_id1, entity_id2 FROM civicrm_prevnext_cache
160 WHERE cacheKey LIKE %1
161 $actionGet
162 $entity_whereClause
163 ORDER BY id
164 ";
165 $params[1] = array("{$cacheKey}%", 'String');
166
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;
172 }
173 }
174 return $contactIds;
175 }
176 }
177
178 }