Fix phpdoc types for $_query in CRM_Core_Selector_Base subclasses
[civicrm-core.git] / CRM / Activity / Selector / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class is used to retrieve and display a range of contacts that match the given criteria.
20 *
21 * Specifically for results of advanced search options.
22 */
23 class CRM_Activity_Selector_Search extends CRM_Core_Selector_Base implements CRM_Core_Selector_API {
24
25 /**
26 * This defines two actions- View and Edit.
27 *
28 * @var array
29 */
30 public static $_links = NULL;
31
32 /**
33 * We use desc to remind us what that column is, name is used in the tpl
34 *
35 * @var array
36 */
37 public static $_columnHeaders;
38
39 /**
40 * Properties of contact we're interested in displaying
41 * @var array
42 */
43
44 public static $_properties = [
45 'contact_id',
46 'contact_type',
47 'contact_sub_type',
48 'sort_name',
49 'display_name',
50 'activity_id',
51 'activity_date_time',
52 'activity_status_id',
53 'activity_status',
54 'activity_subject',
55 'source_record_id',
56 'activity_type_id',
57 'activity_type',
58 'activity_is_test',
59 'activity_campaign_id',
60 'activity_engagement_level',
61 ];
62
63 /**
64 * Are we restricting ourselves to a single contact.
65 *
66 * @var bool
67 */
68 protected $_single = FALSE;
69
70 /**
71 * Are we restricting ourselves to a single contact.
72 *
73 * @var bool
74 */
75 protected $_limit = NULL;
76
77 /**
78 * What context are we being invoked from.
79 *
80 * @var string
81 */
82 protected $_context = NULL;
83
84 /**
85 * What component context are we being invoked from.
86 *
87 * @var string
88 */
89 protected $_compContext = NULL;
90
91 /**
92 * QueryParams is the array returned by exportValues called on.
93 * the HTML_QuickForm_Controller for that page.
94 *
95 * @var array
96 */
97 public $_queryParams;
98
99 /**
100 * Represent the type of selector.
101 *
102 * @var int
103 */
104 protected $_action;
105
106 /**
107 * The additional clause that we restrict the search with.
108 *
109 * @var string
110 */
111 protected $_activityClause = NULL;
112
113 /**
114 * The query object.
115 *
116 * @var CRM_Contact_BAO_Query
117 */
118 protected $_query;
119
120 /**
121 * Class constructor.
122 *
123 * @param array $queryParams
124 * Array of parameters for query.
125 * @param \const|int $action - action of search basic or advanced.
126 * @param string $activityClause
127 * If the caller wants to further restrict the search (used in activities).
128 * @param bool $single
129 * Are we dealing only with one contact?.
130 * @param int $limit
131 * How many activities do we want returned.
132 *
133 * @param string $context
134 * @param null $compContext
135 *
136 * @return \CRM_Activity_Selector_Search
137 */
138 public function __construct(
139 &$queryParams,
140 $action = CRM_Core_Action::NONE,
141 $activityClause = NULL,
142 $single = FALSE,
143 $limit = NULL,
144 $context = 'search',
145 $compContext = NULL
146 ) {
147 // submitted form values
148 $this->_queryParams = &$queryParams;
149
150 $this->_single = $single;
151 $this->_limit = $limit;
152 $this->_context = $context;
153 $this->_compContext = $compContext;
154
155 $this->_activityClause = $activityClause;
156
157 // type of selector
158 $this->_action = $action;
159 $this->_query = new CRM_Contact_BAO_Query($this->_queryParams,
160 CRM_Activity_BAO_Query::selectorReturnProperties(),
161 NULL, FALSE, FALSE,
162 CRM_Contact_BAO_Query::MODE_ACTIVITY
163 );
164 $this->_query->_distinctComponentClause = '( civicrm_activity.id )';
165 $this->_query->_groupByComponentClause = " GROUP BY civicrm_activity.id ";
166 }
167
168 /**
169 * Getter for array of the parameters required for creating pager.
170 *
171 * @param $action
172 * @param array $params
173 */
174 public function getPagerParams($action, &$params) {
175 $params['status'] = ts('Activities %%StatusMessage%%');
176 $params['csvString'] = NULL;
177 $params['rowCount'] = Civi::settings()->get('default_pager_size');
178 $params['buttonTop'] = 'PagerTopButton';
179 $params['buttonBottom'] = 'PagerBottomButton';
180 }
181
182 /**
183 * Returns total number of rows for the query.
184 *
185 * @param string $action
186 *
187 * @return int
188 * Total number of rows
189 */
190 public function getTotalCount($action) {
191 return $this->_query->searchQuery(0, 0, NULL,
192 TRUE, FALSE,
193 FALSE, FALSE,
194 FALSE,
195 $this->_activityClause
196 );
197 }
198
199 /**
200 * Returns all the rows in the given offset and rowCount.
201 *
202 * @param string $action
203 * The action being performed.
204 * @param int $offset
205 * The row number to start from.
206 * @param int $rowCount
207 * The number of rows to return.
208 * @param string $sort
209 * The sql string that describes the sort order.
210 * @param string $output
211 * What should the result set include (web/email/csv).
212 *
213 * @return array
214 * rows in the given offset and rowCount
215 */
216 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
217 $result = $this->_query->searchQuery(
218 $offset, $rowCount, $sort,
219 FALSE, FALSE,
220 FALSE, FALSE,
221 FALSE,
222 $this->_activityClause
223 );
224 $rows = [];
225 $mailingIDs = CRM_Mailing_BAO_Mailing::mailingACLIDs();
226 $accessCiviMail = CRM_Core_Permission::check('access CiviMail');
227
228 // Get all campaigns.
229 $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
230
231 $engagementLevels = CRM_Campaign_PseudoConstant::engagementLevel();
232 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
233 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
234 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
235 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
236 $bulkActivityTypeID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Bulk Email');
237
238 while ($result->fetch()) {
239 $row = [];
240
241 // Ignore rows where we dont have an activity id.
242 if (empty($result->activity_id)) {
243 continue;
244 }
245 $this->_query->convertToPseudoNames($result);
246
247 // the columns we are interested in
248 foreach (self::$_properties as $property) {
249 if (isset($result->$property)) {
250 $row[$property] = $result->$property;
251 }
252 }
253
254 $contactId = $row['contact_id'] ?? NULL;
255 if (!$contactId) {
256 $contactId = $row['source_contact_id'] ?? NULL;
257 }
258
259 $row['target_contact_name'] = CRM_Activity_BAO_ActivityContact::getNames($row['activity_id'], $targetID);
260 $row['assignee_contact_name'] = CRM_Activity_BAO_ActivityContact::getNames($row['activity_id'], $assigneeID);
261 list($row['source_contact_name'], $row['source_contact_id']) = CRM_Activity_BAO_ActivityContact::getNames($row['activity_id'], $sourceID, TRUE);
262 $row['source_contact_name'] = implode(',', array_values($row['source_contact_name']));
263 $row['source_contact_id'] = implode(',', $row['source_contact_id']);
264
265 if ($this->_context == 'search') {
266 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->activity_id;
267 }
268 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? $result->contact_sub_type : $result->contact_type, FALSE, $result->contact_id
269 );
270 $accessMailingReport = FALSE;
271 $activityTypeId = $row['activity_type_id'];
272 if ($row['activity_is_test']) {
273 $row['activity_type'] = CRM_Core_TestEntity::appendTestText($row['activity_type']);
274 }
275 $row['mailingId'] = '';
276 if (
277 $accessCiviMail &&
278 ($mailingIDs === TRUE || in_array($result->source_record_id, $mailingIDs)) &&
279 ($bulkActivityTypeID == $activityTypeId)
280 ) {
281 $row['mailingId'] = CRM_Utils_System::url('civicrm/mailing/report',
282 "mid={$result->source_record_id}&reset=1&cid={$contactId}&context=activitySelector"
283 );
284 $row['recipients'] = ts('(recipients)');
285 $row['target_contact_name'] = '';
286 $row['assignee_contact_name'] = '';
287 $accessMailingReport = TRUE;
288 }
289 $activityActions = new CRM_Activity_Selector_Activity($result->contact_id, NULL);
290 $actionLinks = $activityActions->actionLinks($activityTypeId,
291 CRM_Utils_Array::value('source_record_id', $row),
292 $accessMailingReport,
293 CRM_Utils_Array::value('activity_id', $row),
294 $this->_key,
295 $this->_compContext
296 );
297 $row['action'] = CRM_Core_Action::formLink($actionLinks, NULL,
298 [
299 'id' => $result->activity_id,
300 'cid' => $contactId,
301 'cxt' => $this->_context,
302 ],
303 ts('more'),
304 FALSE,
305 'activity.selector.row',
306 'Activity',
307 $result->activity_id
308 );
309
310 // Carry campaign to selector.
311 $row['campaign'] = $allCampaigns[$result->activity_campaign_id] ?? NULL;
312 $row['campaign_id'] = $result->activity_campaign_id;
313
314 if ($engagementLevel = CRM_Utils_Array::value('activity_engagement_level', $row)) {
315 $row['activity_engagement_level'] = CRM_Utils_Array::value($engagementLevel,
316 $engagementLevels, $engagementLevel
317 );
318 }
319
320 // Check if recurring activity.
321 $repeat = CRM_Core_BAO_RecurringEntity::getPositionAndCount($row['activity_id'], 'civicrm_activity');
322 $row['repeat'] = '';
323 if ($repeat) {
324 $row['repeat'] = ts('Repeating (%1 of %2)', [1 => $repeat[0], 2 => $repeat[1]]);
325 }
326 $rows[] = $row;
327 }
328
329 return $rows;
330 }
331
332 /**
333 * @return array
334 * which contains an array of strings
335 */
336 public function getQILL() {
337 return $this->_query->qill();
338 }
339
340 /**
341 * Returns the column headers as an array of tuples:
342 * (name, sortName (key to the sort array))
343 *
344 * @param string $action
345 * The action being performed.
346 * @param string $output
347 * What should the result set include (web/email/csv).
348 *
349 * @return array
350 * the column headers that need to be displayed
351 */
352 public function &getColumnHeaders($action = NULL, $output = NULL) {
353 if (!isset(self::$_columnHeaders)) {
354 self::$_columnHeaders = [
355 [
356 'name' => ts('Type'),
357 'sort' => 'activity_type_id',
358 'direction' => CRM_Utils_Sort::DONTCARE,
359 ],
360 [
361 'name' => ts('Subject'),
362 'sort' => 'activity_subject',
363 'direction' => CRM_Utils_Sort::DONTCARE,
364 ],
365 [
366 'name' => ts('Added by'),
367 'sort' => 'source_contact',
368 'direction' => CRM_Utils_Sort::DONTCARE,
369 ],
370 ['name' => ts('With')],
371 ['name' => ts('Assigned')],
372 [
373 'name' => ts('Date'),
374 'sort' => 'activity_date_time',
375 'direction' => CRM_Utils_Sort::DESCENDING,
376 ],
377 [
378 'name' => ts('Status'),
379 'sort' => 'activity_status',
380 'direction' => CRM_Utils_Sort::DONTCARE,
381 ],
382 [
383 'desc' => ts('Actions'),
384 ],
385 ];
386 }
387 return self::$_columnHeaders;
388 }
389
390 /**
391 * @return mixed
392 */
393 public function alphabetQuery() {
394 return $this->_query->alphabetQuery();
395 }
396
397 /**
398 * @return CRM_Contact_BAO_Query
399 */
400 public function &getQuery() {
401 return $this->_query;
402 }
403
404 /**
405 * Name of export file.
406 *
407 * @param string $output
408 * Type of output.
409 *
410 * @return string
411 * name of the file
412 */
413 public function getExportFileName($output = 'csv') {
414 return ts('CiviCRM Activity Search');
415 }
416
417 }