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