Replace CRM_Utils_Array::value with ?? in variable assignments
[civicrm-core.git] / CRM / Campaign / Selector / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
67d19299 19 * This class is used to retrieve and display a range of contacts that match the given criteria.
6a488035
TO
20 */
21class CRM_Campaign_Selector_Search extends CRM_Core_Selector_Base implements CRM_Core_Selector_API {
22
23 /**
24 * This defines two actions- View and Edit.
25 *
26 * @var array
6a488035 27 */
f157740d 28 public static $_links = NULL;
6a488035
TO
29
30 /**
100fef9d 31 * We use desc to remind us what that column is, name is used in the tpl
6a488035
TO
32 *
33 * @var array
6a488035 34 */
f157740d 35 public static $_columnHeaders;
6a488035
TO
36
37 /**
38 * Properties of contact we're interested in displaying
39 * @var array
6a488035 40 */
f157740d 41 public static $_properties = [
6a488035
TO
42 'contact_id',
43 'sort_name',
44 'street_unit',
45 'street_name',
46 'street_number',
47 'street_address',
48 'city',
49 'postal_code',
50 'state_province',
51 'country',
52 'email',
53 'phone',
54 'campaign_id',
55 'survey_activity_id',
56 'survey_activity_target_id',
57 'survey_activity_target_contact_id',
be2fb01f 58 ];
6a488035
TO
59
60 /**
100fef9d 61 * Are we restricting ourselves to a single contact
6a488035 62 *
d51c6add 63 * @var bool
6a488035
TO
64 */
65 protected $_single = FALSE;
66
67 /**
100fef9d 68 * Are we restricting ourselves to a single contact
6a488035 69 *
d51c6add 70 * @var bool
6a488035
TO
71 */
72 protected $_limit = NULL;
73
74 /**
100fef9d 75 * What context are we being invoked from
6a488035 76 *
6a488035
TO
77 * @var string
78 */
79 protected $_context = NULL;
80
81 /**
100fef9d 82 * QueryParams is the array returned by exportValues called on
6a488035
TO
83 * the HTML_QuickForm_Controller for that page.
84 *
85 * @var array
6a488035
TO
86 */
87 public $_queryParams;
88
89 /**
fe482240 90 * Represent the type of selector.
6a488035
TO
91 *
92 * @var int
6a488035
TO
93 */
94 protected $_action;
95
96 /**
fe482240 97 * The additional clause that we restrict the search with.
6a488035
TO
98 *
99 * @var string
100 */
101 protected $_surveyClause = NULL;
102
103 /**
fe482240 104 * The query object.
6a488035
TO
105 *
106 * @var string
107 */
108 protected $_query;
109
110 /**
fe482240 111 * Class constructor.
6a488035 112 *
7aaf6db0
TO
113 * @param array $queryParams
114 * Array of parameters for query.
dd244018 115 * @param \const|int $action - action of search basic or advanced.
7aaf6db0
TO
116 * @param string $surveyClause
117 * If the caller wants to further restrict the search.
118 * @param bool $single
119 * Are we dealing only with one contact?.
120 * @param int $limit
121 * How many voters do we want returned.
6a488035 122 *
dd244018
EM
123 * @param string $context
124 *
125 * @return \CRM_Campaign_Selector_Search
dd244018 126 */
28a04ea9 127 public function __construct(
5c2ea586 128 &$queryParams,
94880218 129 $action = CRM_Core_Action::NONE,
6a488035 130 $surveyClause = NULL,
94880218
TO
131 $single = FALSE,
132 $limit = NULL,
133 $context = 'search'
6a488035
TO
134 ) {
135 // submitted form values
136 $this->_queryParams = &$queryParams;
137
353ffa53
TO
138 $this->_single = $single;
139 $this->_limit = $limit;
6a488035
TO
140 $this->_context = $context;
141
142 $this->_campaignClause = $surveyClause;
9c1bc317
CW
143 $this->_campaignFromClause = $surveyClause['fromClause'] ?? NULL;
144 $this->_campaignWhereClause = $surveyClause['whereClause'] ?? NULL;
6a488035
TO
145
146 // type of selector
147 $this->_action = $action;
148
149 $this->_query = new CRM_Contact_BAO_Query($this->_queryParams,
150 NULL, NULL, FALSE, FALSE,
151 CRM_Contact_BAO_Query::MODE_CAMPAIGN,
152 TRUE
153 );
154 }
6a488035
TO
155
156 /**
157 * This method returns the links that are given for each search row.
158 * currently the links added for each row are
159 *
160 * - View
161 * - Edit
162 *
163 * @return array
6a488035 164 */
f157740d 165 public static function &links() {
be2fb01f 166 return self::$_links = [];
6a488035
TO
167 }
168
169 /**
100fef9d 170 * Getter for array of the parameters required for creating pager.
6a488035 171 *
da6b46f4 172 * @param $action
c490a46a 173 * @param array $params
6a488035 174 */
00be9182 175 public function getPagerParams($action, &$params) {
6a488035
TO
176 $params['csvString'] = NULL;
177 $params['status'] = ts('Respondents') . ' %%StatusMessage%%';
178 $params['rowCount'] = ($this->_limit) ? $this->_limit : CRM_Utils_Pager::ROWCOUNT;
179 $params['buttonTop'] = 'PagerTopButton';
180 $params['buttonBottom'] = 'PagerBottomButton';
181 }
182
183 /**
184 * Returns total number of rows for the query.
185 *
54957108 186 * @param string $action
6a488035 187 *
a6c01b45
CW
188 * @return int
189 * Total number of rows
6a488035 190 */
00be9182 191 public function getTotalCount($action) {
6a488035
TO
192 return $this->_query->searchQuery(0, 0, NULL,
193 TRUE, FALSE,
194 FALSE, FALSE, FALSE,
195 $this->_campaignWhereClause,
196 NULL,
197 $this->_campaignFromClause
198 );
199 }
200
201 /**
fe482240 202 * Returns all the rows in the given offset and rowCount.
6a488035 203 *
3f8d2862 204 * @param string $action
7aaf6db0
TO
205 * The action being performed.
206 * @param int $offset
207 * The row number to start from.
208 * @param int $rowCount
209 * The number of rows to return.
210 * @param string $sort
211 * The sql string that describes the sort order.
3f8d2862 212 * @param string $output
7aaf6db0 213 * What should the result set include (web/email/csv).
6a488035 214 *
a6c01b45
CW
215 * @return int
216 * the total number of rows for this action
6a488035 217 */
00be9182 218 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
6a488035
TO
219 $result = $this->_query->searchQuery($offset, $rowCount, $sort,
220 FALSE, FALSE,
221 FALSE, FALSE,
222 FALSE, $this->_campaignWhereClause,
223 NULL,
224 $this->_campaignFromClause
225 );
226
6a488035 227 // process the result of the query
be2fb01f 228 $rows = [];
6a488035 229
94880218 230 while ($result->fetch()) {
d9ab802d 231 $this->_query->convertToPseudoNames($result);
be2fb01f 232 $row = [];
6a488035
TO
233 // the columns we are interested in
234 foreach (self::$_properties as $property) {
235 if (property_exists($result, $property)) {
236 $row[$property] = $result->$property;
237 }
238 }
239 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->contact_id;
240 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_type, FALSE, $result->contact_id);
241
242 $rows[] = $row;
243 }
244 $this->buildPrevNextCache($sort);
245
246 return $rows;
247 }
248
30c4e065
EM
249 /**
250 * @param $sort
251 */
00be9182 252 public function buildPrevNextCache($sort) {
6a488035 253 //for prev/next pagination
a3d827a7 254 $crmPID = CRM_Utils_Request::retrieve('crmPID', 'Integer');
6a488035
TO
255
256 if (!$crmPID) {
257 $cacheKey = "civicrm search {$this->_key}";
744b4e34 258 Civi::service('prevnext')->deleteItem(NULL, $cacheKey, 'civicrm_contact');
91da6cd5 259
0e7133be 260 $sql = $this->_query->getSearchSQLParts(0, 0, $sort,
6a488035
TO
261 FALSE, FALSE,
262 FALSE, FALSE,
89d7bb76 263 $this->_campaignWhereClause,
6a488035
TO
264 NULL,
265 $this->_campaignFromClause
266 );
0e7133be 267
9df3fff5 268 $selectSQL = "
2c8b42d5 269 SELECT %1, contact_a.id, contact_a.display_name
0e7133be 270FROM {$sql['from']}
6a488035 271";
6a488035 272
9df3fff5 273 try {
2c8b42d5 274 Civi::service('prevnext')->fillWithSql($cacheKey, $selectSQL, [1 => [$cacheKey, 'String']]);
9df3fff5
TO
275 }
276 catch (CRM_Core_Exception $e) {
277 // Heavy handed, no? Seems like this merits an explanation.
6a488035
TO
278 return;
279 }
9df3fff5 280
e37c68e1
TO
281 if (Civi::service('prevnext') instanceof CRM_Core_PrevNextCache_Sql) {
282 // SQL-backed prevnext cache uses an extra record for pruning the cache.
435fc552
SL
283 // Also ensure that caches stay alive for 2 days a per previous code.
284 Civi::cache('prevNextCache')->set($cacheKey, $cacheKey, 60 * 60 * 24 * CRM_Core_PrevNextCache_Sql::cacheDays);
e37c68e1 285 }
6a488035
TO
286 }
287 }
288
289 /**
a6c01b45
CW
290 * @return array
291 * which contains an array of strings
28a04ea9 292 */
6a488035
TO
293 public function getQILL() {
294 return $this->_query->qill();
295 }
296
297 /**
100fef9d 298 * Returns the column headers as an array of tuples:
6a488035
TO
299 * (name, sortName (key to the sort array))
300 *
7aaf6db0
TO
301 * @param string $action
302 * The action being performed.
3f8d2862 303 * @param string $output
7aaf6db0 304 * What should the result set include (web/email/csv).
6a488035 305 *
a6c01b45
CW
306 * @return array
307 * the column headers that need to be displayed
6a488035
TO
308 */
309 public function &getColumnHeaders($action = NULL, $output = NULL) {
be2fb01f 310 self::$_columnHeaders = [];
6a488035
TO
311
312 if (!$this->_single) {
be2fb01f
CW
313 $contactDetails = [
314 [
353ffa53 315 'name' => ts('Contact Name'),
6a488035
TO
316 'sort' => 'sort_name',
317 'direction' => CRM_Utils_Sort::ASCENDING,
be2fb01f
CW
318 ],
319 [
353ffa53 320 'name' => ts('Street Number'),
6a488035 321 'sort' => 'street_number',
be2fb01f
CW
322 ],
323 [
353ffa53 324 'name' => ts('Street Name'),
6a488035 325 'sort' => 'street_name',
be2fb01f
CW
326 ],
327 ['name' => ts('Street Address')],
328 [
353ffa53 329 'name' => ts('City'),
6a488035 330 'sort' => 'city',
be2fb01f
CW
331 ],
332 [
353ffa53 333 'name' => ts('Postal Code'),
6a488035 334 'sort' => 'postal_code',
be2fb01f
CW
335 ],
336 [
353ffa53 337 'name' => ts('State'),
6a488035 338 'sort' => 'state_province_name',
be2fb01f
CW
339 ],
340 ['name' => ts('Country')],
341 ['name' => ts('Email')],
342 ['name' => ts('Phone')],
343 ];
6a488035
TO
344 self::$_columnHeaders = array_merge($contactDetails, self::$_columnHeaders);
345 }
346
347 return self::$_columnHeaders;
348 }
349
30c4e065
EM
350 /**
351 * @return string
352 */
00be9182 353 public function &getQuery() {
6a488035
TO
354 return $this->_query;
355 }
356
357 /**
100fef9d 358 * Name of export file.
6a488035 359 *
7aaf6db0
TO
360 * @param string $output
361 * Type of output.
6a488035 362 *
a6c01b45
CW
363 * @return string
364 * name of the file
6a488035 365 */
00be9182 366 public function getExportFileName($output = 'csv') {
6a488035
TO
367 return ts('CiviCRM Respondent Search');
368 }
96025800 369
6a488035 370}