Merge pull request #15135 from mattwire/case_links_refactor_report
[civicrm-core.git] / CRM / Activity / 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/**
3819f101 19 * This class is used to retrieve and display a range of contacts that match the given criteria.
6a488035 20 *
3819f101 21 * Specifically for results of advanced search options.
6a488035
TO
22 */
23class 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
6a488035 29 */
62d3ee27 30 public static $_links = NULL;
6a488035
TO
31
32 /**
100fef9d 33 * We use desc to remind us what that column is, name is used in the tpl
6a488035
TO
34 *
35 * @var array
6a488035 36 */
62d3ee27 37 public static $_columnHeaders;
6a488035
TO
38
39 /**
40 * Properties of contact we're interested in displaying
41 * @var array
6a488035
TO
42 */
43
62d3ee27 44 public static $_properties = [
6a488035
TO
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',
6a488035 55 'source_record_id',
6a488035
TO
56 'activity_type_id',
57 'activity_type',
58 'activity_is_test',
59 'activity_campaign_id',
60 'activity_engagement_level',
be2fb01f 61 ];
6a488035
TO
62
63 /**
fe482240 64 * Are we restricting ourselves to a single contact.
6a488035 65 *
d51c6add 66 * @var bool
6a488035
TO
67 */
68 protected $_single = FALSE;
69
70 /**
fe482240 71 * Are we restricting ourselves to a single contact.
6a488035 72 *
d51c6add 73 * @var bool
6a488035
TO
74 */
75 protected $_limit = NULL;
76
77 /**
fe482240 78 * What context are we being invoked from.
6a488035 79 *
6a488035
TO
80 * @var string
81 */
82 protected $_context = NULL;
83
84 /**
fe482240 85 * What component context are we being invoked from.
6a488035 86 *
6a488035
TO
87 * @var string
88 */
89 protected $_compContext = NULL;
90
91 /**
fe482240 92 * QueryParams is the array returned by exportValues called on.
6a488035
TO
93 * the HTML_QuickForm_Controller for that page.
94 *
95 * @var array
6a488035
TO
96 */
97 public $_queryParams;
98
99 /**
fe482240 100 * Represent the type of selector.
6a488035
TO
101 *
102 * @var int
6a488035
TO
103 */
104 protected $_action;
105
106 /**
fe482240 107 * The additional clause that we restrict the search with.
6a488035
TO
108 *
109 * @var string
110 */
111 protected $_activityClause = NULL;
112
113 /**
fe482240 114 * The query object.
6a488035 115 *
c2a377b1 116 * @var \CRM_Contact_BAO_Query
6a488035
TO
117 */
118 protected $_query;
119
120 /**
fe482240 121 * Class constructor.
6a488035 122 *
041ab3d1
TO
123 * @param array $queryParams
124 * Array of parameters for query.
dd244018 125 * @param \const|int $action - action of search basic or advanced.
041ab3d1
TO
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.
6a488035 132 *
dd244018
EM
133 * @param string $context
134 * @param null $compContext
135 *
136 * @return \CRM_Activity_Selector_Search
6a488035 137 */
2da40d21 138 public function __construct(
9d5494f7 139 &$queryParams,
32864ccf 140 $action = CRM_Core_Action::NONE,
6a488035 141 $activityClause = NULL,
32864ccf
TO
142 $single = FALSE,
143 $limit = NULL,
144 $context = 'search',
145 $compContext = NULL
6a488035
TO
146 ) {
147 // submitted form values
148 $this->_queryParams = &$queryParams;
149
353ffa53
TO
150 $this->_single = $single;
151 $this->_limit = $limit;
152 $this->_context = $context;
6a488035
TO
153 $this->_compContext = $compContext;
154
155 $this->_activityClause = $activityClause;
156
87767abb 157 // CRM-12675
2f9320ea 158 $components = CRM_Core_Component::getNames();
be2fb01f 159 $componentClause = [];
2f9320ea 160 foreach ($components as $componentID => $componentName) {
f8f5515a
BS
161 // CRM-19201: Add support for searching CiviCampaign and CiviCase
162 // activities. For CiviCase, "access all cases and activities" is
163 // required here rather than "access my cases and activities" to
164 // prevent those with only the later permission from seeing a list
165 // of all cases which might present a privacy issue.
c2a377b1 166 // @todo this is the cause of the current devastatingly bad performance on
167 // activity search - it involves a bad join.
168 // The correct fix is to use the permission infrastrucutre - ie. add in the
169 // clause generated by CRM_Activity_BAO_Query::addSelectWhere
170 // but some testing needs to check that before making the change
171 // see https://github.com/civicrm/civicrm-core/blob/be2fb01f90f5f299dd07402a41fed7c7c7567f00/CRM/Utils/SQL.php#L48
172 // for how it's done in the api kernel.
44d17ec8 173 if (!CRM_Core_Permission::access($componentName, TRUE, TRUE)) {
2f9320ea 174 $componentClause[] = " (activity_type.component_id IS NULL OR activity_type.component_id <> {$componentID}) ";
87767abb 175 }
176 }
177
2f9320ea 178 if (!empty($componentClause)) {
179 $componentRestriction = implode(' AND ', $componentClause);
945c2048 180 if (empty($this->_activityClause)) {
181 $this->_activityClause = $componentRestriction;
182 }
183 else {
184 $this->_activityClause .= ' AND ' . $componentRestriction;
185 }
2f9320ea 186 }
187
6a488035
TO
188 // type of selector
189 $this->_action = $action;
190 $this->_query = new CRM_Contact_BAO_Query($this->_queryParams,
7bd16b05 191 CRM_Activity_BAO_Query::selectorReturnProperties(),
6a488035
TO
192 NULL, FALSE, FALSE,
193 CRM_Contact_BAO_Query::MODE_ACTIVITY
194 );
195 $this->_query->_distinctComponentClause = '( civicrm_activity.id )';
196 $this->_query->_groupByComponentClause = " GROUP BY civicrm_activity.id ";
6a488035 197 }
6a488035
TO
198
199 /**
100fef9d 200 * Getter for array of the parameters required for creating pager.
6a488035 201 *
da6b46f4 202 * @param $action
c490a46a 203 * @param array $params
6a488035 204 */
00be9182 205 public function getPagerParams($action, &$params) {
6a488035
TO
206 $params['status'] = ts('Activities %%StatusMessage%%');
207 $params['csvString'] = NULL;
208 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
209 $params['buttonTop'] = 'PagerTopButton';
210 $params['buttonBottom'] = 'PagerBottomButton';
211 }
6a488035
TO
212
213 /**
214 * Returns total number of rows for the query.
215 *
2e2605fe 216 * @param string $action
6a488035 217 *
a6c01b45
CW
218 * @return int
219 * Total number of rows
6a488035 220 */
00be9182 221 public function getTotalCount($action) {
6a488035
TO
222 return $this->_query->searchQuery(0, 0, NULL,
223 TRUE, FALSE,
224 FALSE, FALSE,
225 FALSE,
226 $this->_activityClause
227 );
228 }
229
230 /**
2e2605fe 231 * Returns all the rows in the given offset and rowCount.
6a488035 232 *
3f8d2862 233 * @param string $action
041ab3d1
TO
234 * The action being performed.
235 * @param int $offset
236 * The row number to start from.
237 * @param int $rowCount
238 * The number of rows to return.
239 * @param string $sort
240 * The sql string that describes the sort order.
3f8d2862 241 * @param string $output
041ab3d1 242 * What should the result set include (web/email/csv).
6a488035 243 *
a6c01b45
CW
244 * @return array
245 * rows in the given offset and rowCount
6a488035 246 */
00be9182 247 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
f04255e4
DL
248 $result = $this->_query->searchQuery(
249 $offset, $rowCount, $sort,
6a488035
TO
250 FALSE, FALSE,
251 FALSE, FALSE,
252 FALSE,
253 $this->_activityClause
254 );
be2fb01f 255 $rows = [];
353ffa53 256 $mailingIDs = CRM_Mailing_BAO_Mailing::mailingACLIDs();
6a488035
TO
257 $accessCiviMail = CRM_Core_Permission::check('access CiviMail');
258
7808aae6 259 // Get all campaigns.
6a488035
TO
260 $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
261
262 $engagementLevels = CRM_Campaign_PseudoConstant::engagementLevel();
44f817d4 263 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
a24b3694 264 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
265 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
266 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
b864360d 267 $bulkActivityTypeID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Bulk Email');
f813f78e 268
6a488035 269 while ($result->fetch()) {
be2fb01f 270 $row = [];
6a488035 271
7808aae6 272 // Ignore rows where we dont have an activity id.
6a488035
TO
273 if (empty($result->activity_id)) {
274 continue;
275 }
7bd16b05 276 $this->_query->convertToPseudoNames($result);
6a488035
TO
277
278 // the columns we are interested in
279 foreach (self::$_properties as $property) {
280 if (isset($result->$property)) {
281 $row[$property] = $result->$property;
282 }
283 }
284
285 $contactId = CRM_Utils_Array::value('contact_id', $row);
286 if (!$contactId) {
287 $contactId = CRM_Utils_Array::value('source_contact_id', $row);
288 }
289
353ffa53 290 $row['target_contact_name'] = CRM_Activity_BAO_ActivityContact::getNames($row['activity_id'], $targetID);
a24b3694 291 $row['assignee_contact_name'] = CRM_Activity_BAO_ActivityContact::getNames($row['activity_id'], $assigneeID);
292 list($row['source_contact_name'], $row['source_contact_id']) = CRM_Activity_BAO_ActivityContact::getNames($row['activity_id'], $sourceID, TRUE);
f04255e4
DL
293 $row['source_contact_name'] = implode(',', array_values($row['source_contact_name']));
294 $row['source_contact_id'] = implode(',', $row['source_contact_id']);
6a488035 295
6a488035
TO
296 if ($this->_context == 'search') {
297 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->activity_id;
298 }
32864ccf 299 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? $result->contact_sub_type : $result->contact_type, FALSE, $result->contact_id
6a488035
TO
300 );
301 $accessMailingReport = FALSE;
353ffa53 302 $activityTypeId = $row['activity_type_id'];
6a488035 303 if ($row['activity_is_test']) {
cd355351 304 $row['activity_type'] = CRM_Core_TestEntity::appendTestText($row['activity_type']);
6a488035 305 }
6a488035
TO
306 $row['mailingId'] = '';
307 if (
308 $accessCiviMail &&
309 ($mailingIDs === TRUE || in_array($result->source_record_id, $mailingIDs)) &&
310 ($bulkActivityTypeID == $activityTypeId)
311 ) {
312 $row['mailingId'] = CRM_Utils_System::url('civicrm/mailing/report',
c79f662a 313 "mid={$result->source_record_id}&reset=1&cid={$contactId}&context=activitySelector"
6a488035
TO
314 );
315 $row['recipients'] = ts('(recipients)');
316 $row['target_contact_name'] = '';
317 $row['assignee_contact_name'] = '';
318 $accessMailingReport = TRUE;
319 }
320 $activityActions = new CRM_Activity_Selector_Activity($result->contact_id, NULL);
321 $actionLinks = $activityActions->actionLinks($activityTypeId,
322 CRM_Utils_Array::value('source_record_id', $row),
323 $accessMailingReport,
324 CRM_Utils_Array::value('activity_id', $row),
325 $this->_key,
326 $this->_compContext
327 );
328 $row['action'] = CRM_Core_Action::formLink($actionLinks, NULL,
be2fb01f 329 [
6a488035
TO
330 'id' => $result->activity_id,
331 'cid' => $contactId,
332 'cxt' => $this->_context,
be2fb01f 333 ],
87dab4a4
AH
334 ts('more'),
335 FALSE,
336 'activity.selector.row',
337 'Activity',
338 $result->activity_id
6a488035
TO
339 );
340
7808aae6 341 // Carry campaign to selector.
6a488035
TO
342 $row['campaign'] = CRM_Utils_Array::value($result->activity_campaign_id, $allCampaigns);
343 $row['campaign_id'] = $result->activity_campaign_id;
344
345 if ($engagementLevel = CRM_Utils_Array::value('activity_engagement_level', $row)) {
346 $row['activity_engagement_level'] = CRM_Utils_Array::value($engagementLevel,
347 $engagementLevels, $engagementLevel
348 );
349 }
350
7808aae6 351 // Check if recurring activity.
04374d9d 352 $repeat = CRM_Core_BAO_RecurringEntity::getPositionAndCount($row['activity_id'], 'civicrm_activity');
d8786c71 353 $row['repeat'] = '';
04374d9d 354 if ($repeat) {
be2fb01f 355 $row['repeat'] = ts('Repeating (%1 of %2)', [1 => $repeat[0], 2 => $repeat[1]]);
d8786c71 356 }
6a488035
TO
357 $rows[] = $row;
358 }
359
360 return $rows;
361 }
362
363 /**
a6c01b45
CW
364 * @return array
365 * which contains an array of strings
6a488035
TO
366 */
367 public function getQILL() {
368 return $this->_query->qill();
369 }
370
371 /**
100fef9d 372 * Returns the column headers as an array of tuples:
6a488035
TO
373 * (name, sortName (key to the sort array))
374 *
041ab3d1
TO
375 * @param string $action
376 * The action being performed.
3f8d2862 377 * @param string $output
041ab3d1 378 * What should the result set include (web/email/csv).
6a488035 379 *
a6c01b45
CW
380 * @return array
381 * the column headers that need to be displayed
6a488035
TO
382 */
383 public function &getColumnHeaders($action = NULL, $output = NULL) {
384 if (!isset(self::$_columnHeaders)) {
be2fb01f
CW
385 self::$_columnHeaders = [
386 [
6a488035
TO
387 'name' => ts('Type'),
388 'sort' => 'activity_type_id',
389 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
390 ],
391 [
6a488035 392 'name' => ts('Subject'),
5fa296f7 393 'sort' => 'activity_subject',
6a488035 394 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
395 ],
396 [
6a488035 397 'name' => ts('Added By'),
37eb6ff9 398 'sort' => 'source_contact',
6a488035 399 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
400 ],
401 ['name' => ts('With')],
402 ['name' => ts('Assigned')],
403 [
6a488035
TO
404 'name' => ts('Date'),
405 'sort' => 'activity_date_time',
406 'direction' => CRM_Utils_Sort::DESCENDING,
be2fb01f
CW
407 ],
408 [
6a488035 409 'name' => ts('Status'),
33a5a53d 410 'sort' => 'activity_status',
6a488035 411 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
412 ],
413 [
6a488035 414 'desc' => ts('Actions'),
be2fb01f
CW
415 ],
416 ];
6a488035
TO
417 }
418 return self::$_columnHeaders;
419 }
420
ffd93213
EM
421 /**
422 * @return mixed
423 */
00be9182 424 public function alphabetQuery() {
52cda5dc 425 return $this->_query->alphabetQuery();
6a488035
TO
426 }
427
ffd93213 428 /**
c2a377b1 429 * @return \CRM_Contact_BAO_Query
ffd93213 430 */
00be9182 431 public function &getQuery() {
6a488035
TO
432 return $this->_query;
433 }
434
435 /**
100fef9d 436 * Name of export file.
6a488035 437 *
041ab3d1
TO
438 * @param string $output
439 * Type of output.
6a488035 440 *
a6c01b45
CW
441 * @return string
442 * name of the file
6a488035 443 */
00be9182 444 public function getExportFileName($output = 'csv') {
6a488035
TO
445 return ts('CiviCRM Activity Search');
446 }
96025800 447
6a488035 448}