Merge pull request #15168 from MegaphoneJon/class-fixes
[civicrm-core.git] / CRM / Campaign / Selector / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33
34 /**
35 * This class is used to retrieve and display a range of contacts that match the given criteria.
36 */
37 class 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
43 */
44 public static $_links = NULL;
45
46 /**
47 * We use desc to remind us what that column is, name is used in the tpl
48 *
49 * @var array
50 */
51 public static $_columnHeaders;
52
53 /**
54 * Properties of contact we're interested in displaying
55 * @var array
56 */
57 public static $_properties = [
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',
74 ];
75
76 /**
77 * Are we restricting ourselves to a single contact
78 *
79 * @var bool
80 */
81 protected $_single = FALSE;
82
83 /**
84 * Are we restricting ourselves to a single contact
85 *
86 * @var bool
87 */
88 protected $_limit = NULL;
89
90 /**
91 * What context are we being invoked from
92 *
93 * @var string
94 */
95 protected $_context = NULL;
96
97 /**
98 * QueryParams is the array returned by exportValues called on
99 * the HTML_QuickForm_Controller for that page.
100 *
101 * @var array
102 */
103 public $_queryParams;
104
105 /**
106 * Represent the type of selector.
107 *
108 * @var int
109 */
110 protected $_action;
111
112 /**
113 * The additional clause that we restrict the search with.
114 *
115 * @var string
116 */
117 protected $_surveyClause = NULL;
118
119 /**
120 * The query object.
121 *
122 * @var string
123 */
124 protected $_query;
125
126 /**
127 * Class constructor.
128 *
129 * @param array $queryParams
130 * Array of parameters for query.
131 * @param \const|int $action - action of search basic or advanced.
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.
138 *
139 * @param string $context
140 *
141 * @return \CRM_Campaign_Selector_Search
142 */
143 public function __construct(
144 &$queryParams,
145 $action = CRM_Core_Action::NONE,
146 $surveyClause = NULL,
147 $single = FALSE,
148 $limit = NULL,
149 $context = 'search'
150 ) {
151 // submitted form values
152 $this->_queryParams = &$queryParams;
153
154 $this->_single = $single;
155 $this->_limit = $limit;
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 }
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
180 */
181 public static function &links() {
182 return self::$_links = [];
183 }
184
185 /**
186 * Getter for array of the parameters required for creating pager.
187 *
188 * @param $action
189 * @param array $params
190 */
191 public function getPagerParams($action, &$params) {
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 *
202 * @param string $action
203 *
204 * @return int
205 * Total number of rows
206 */
207 public function getTotalCount($action) {
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 /**
218 * Returns all the rows in the given offset and rowCount.
219 *
220 * @param string $action
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.
228 * @param string $output
229 * What should the result set include (web/email/csv).
230 *
231 * @return int
232 * the total number of rows for this action
233 */
234 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
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
243 // process the result of the query
244 $rows = [];
245
246 while ($result->fetch()) {
247 $this->_query->convertToPseudoNames($result);
248 $row = [];
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
265 /**
266 * @param $sort
267 */
268 public function buildPrevNextCache($sort) {
269 //for prev/next pagination
270 $crmPID = CRM_Utils_Request::retrieve('crmPID', 'Integer');
271
272 if (!$crmPID) {
273 $cacheKey = "civicrm search {$this->_key}";
274 Civi::service('prevnext')->deleteItem(NULL, $cacheKey, 'civicrm_contact');
275
276 $sql = $this->_query->getSearchSQLParts(0, 0, $sort,
277 FALSE, FALSE,
278 FALSE, FALSE,
279 $this->_campaignWhereClause,
280 NULL,
281 $this->_campaignFromClause
282 );
283
284 $selectSQL = "
285 SELECT %1, contact_a.id, contact_a.display_name
286 FROM {$sql['from']}
287 ";
288
289 try {
290 Civi::service('prevnext')->fillWithSql($cacheKey, $selectSQL, [1 => [$cacheKey, 'String']]);
291 }
292 catch (CRM_Core_Exception $e) {
293 // Heavy handed, no? Seems like this merits an explanation.
294 return;
295 }
296
297 if (Civi::service('prevnext') instanceof CRM_Core_PrevNextCache_Sql) {
298 // SQL-backed prevnext cache uses an extra record for pruning the cache.
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);
301 }
302 }
303 }
304
305 /**
306 * @return array
307 * which contains an array of strings
308 */
309 public function getQILL() {
310 return $this->_query->qill();
311 }
312
313 /**
314 * Returns the column headers as an array of tuples:
315 * (name, sortName (key to the sort array))
316 *
317 * @param string $action
318 * The action being performed.
319 * @param string $output
320 * What should the result set include (web/email/csv).
321 *
322 * @return array
323 * the column headers that need to be displayed
324 */
325 public function &getColumnHeaders($action = NULL, $output = NULL) {
326 self::$_columnHeaders = [];
327
328 if (!$this->_single) {
329 $contactDetails = [
330 [
331 'name' => ts('Contact Name'),
332 'sort' => 'sort_name',
333 'direction' => CRM_Utils_Sort::ASCENDING,
334 ],
335 [
336 'name' => ts('Street Number'),
337 'sort' => 'street_number',
338 ],
339 [
340 'name' => ts('Street Name'),
341 'sort' => 'street_name',
342 ],
343 ['name' => ts('Street Address')],
344 [
345 'name' => ts('City'),
346 'sort' => 'city',
347 ],
348 [
349 'name' => ts('Postal Code'),
350 'sort' => 'postal_code',
351 ],
352 [
353 'name' => ts('State'),
354 'sort' => 'state_province_name',
355 ],
356 ['name' => ts('Country')],
357 ['name' => ts('Email')],
358 ['name' => ts('Phone')],
359 ];
360 self::$_columnHeaders = array_merge($contactDetails, self::$_columnHeaders);
361 }
362
363 return self::$_columnHeaders;
364 }
365
366 /**
367 * @return string
368 */
369 public function &getQuery() {
370 return $this->_query;
371 }
372
373 /**
374 * Name of export file.
375 *
376 * @param string $output
377 * Type of output.
378 *
379 * @return string
380 * name of the file
381 */
382 public function getExportFileName($output = 'csv') {
383 return ts('CiviCRM Respondent Search');
384 }
385
386 }