Merge pull request #15916 from civicrm/5.20
[civicrm-core.git] / CRM / Activity / Selector / Activity.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 activities for a contact.
6a488035
TO
20 */
21class CRM_Activity_Selector_Activity extends CRM_Core_Selector_Base implements CRM_Core_Selector_API {
22
23 /**
100fef9d 24 * We use desc to remind us what that column is, name is used in the tpl
6a488035
TO
25 *
26 * @var array
6a488035 27 */
62d3ee27 28 public static $_columnHeaders;
6a488035
TO
29
30 /**
100fef9d 31 * ContactId - contact id of contact whose activies are displayed
6a488035
TO
32 *
33 * @var int
6a488035
TO
34 */
35 protected $_contactId;
36
37 protected $_admin;
38
39 protected $_context;
40
41 protected $_activityTypeIDs;
42
43 protected $_viewOptions;
44
45 /**
fe482240 46 * Class constructor.
6a488035 47 *
041ab3d1
TO
48 * @param int $contactId
49 * Contact whose activities we want to display.
50 * @param int $permission
51 * The permission we have for this contact.
6a488035 52 *
dd244018
EM
53 * @param bool $admin
54 * @param string $context
55 * @param null $activityTypeIDs
56 *
57 * @return \CRM_Activity_Selector_Activity
6a488035 58 */
8d7a9d07 59 public function __construct(
9d5494f7
TO
60 $contactId,
61 $permission,
62 $admin = FALSE,
63 $context = 'activity',
64 $activityTypeIDs = NULL) {
6a488035
TO
65 $this->_contactId = $contactId;
66 $this->_permission = $permission;
67 $this->_admin = $admin;
68 $this->_context = $context;
69 $this->_activityTypeIDs = $activityTypeIDs;
70
71 // get all enabled view componentc (check if case is enabled)
72 $this->_viewOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
73 'contact_view_options', TRUE, NULL, TRUE
74 );
75 }
76
77 /**
78 * This method returns the action links that are given for each search row.
79 * currently the action links added for each row are
80 *
81 * - View
82 *
c490a46a 83 * @param int $activityTypeId
100fef9d 84 * @param int $sourceRecordId
77b97be7 85 * @param bool $accessMailingReport
100fef9d 86 * @param int $activityId
77b97be7
EM
87 * @param null $key
88 * @param null $compContext
89 *
6a488035 90 * @return array
6a488035 91 */
9d5494f7
TO
92 public static function actionLinks(
93 $activityTypeId,
94 $sourceRecordId = NULL,
95 $accessMailingReport = FALSE,
96 $activityId = NULL,
97 $key = NULL,
98 $compContext = NULL) {
b864360d
MWMC
99
100 //CRM-14277 added additional param to handle activity search
29571f63 101 $extraParams = "&searchContext=activity";
6a488035 102
29571f63 103 $extraParams .= ($key) ? "&key={$key}" : NULL;
6a488035
TO
104 if ($compContext) {
105 $extraParams .= "&compContext={$compContext}";
106 }
107
353ffa53 108 $showView = TRUE;
6a488035
TO
109 $showUpdate = $showDelete = FALSE;
110 $qsUpdate = NULL;
b864360d
MWMC
111 $url = NULL;
112 $qsView = NULL;
6a488035 113
b864360d 114 $activityTypeName = CRM_Core_PseudoConstant::getName('CRM_Activity_BAO_Activity', 'activity_type_id', $activityTypeId);
6a488035 115
7808aae6
SB
116 // CRM-7607
117 // Lets allow to have normal operation for only activity types.
118 // When activity type is disabled or no more exists give only delete.
6a488035
TO
119 switch ($activityTypeName) {
120 case 'Event Registration':
66526669 121 case 'Change Registration':
6a488035
TO
122 $url = 'civicrm/contact/view/participant';
123 $qsView = "action=view&reset=1&id={$sourceRecordId}&cid=%%cid%%&context=%%cxt%%{$extraParams}";
124 break;
125
126 case 'Contribution':
127 $url = 'civicrm/contact/view/contribution';
128 $qsView = "action=view&reset=1&id={$sourceRecordId}&cid=%%cid%%&context=%%cxt%%{$extraParams}";
129 break;
130
66526669
PJ
131 case 'Payment':
132 case 'Refund':
133 $participantId = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_ParticipantPayment', $sourceRecordId, 'participant_id', 'contribution_id');
134 if (!empty($participantId)) {
135 $url = 'civicrm/contact/view/participant';
136 $qsView = "action=view&reset=1&id={$participantId}&cid=%%cid%%&context=%%cxt%%{$extraParams}";
137 }
138 break;
139
6a488035
TO
140 case 'Membership Signup':
141 case 'Membership Renewal':
142 case 'Change Membership Status':
143 case 'Change Membership Type':
144 $url = 'civicrm/contact/view/membership';
145 $qsView = "action=view&reset=1&id={$sourceRecordId}&cid=%%cid%%&context=%%cxt%%{$extraParams}";
146 break;
147
148 case 'Pledge Reminder':
149 case 'Pledge Acknowledgment':
150 $url = 'civicrm/contact/view/activity';
151 $qsView = "atype={$activityTypeId}&action=view&reset=1&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}";
152 break;
153
154 case 'Email':
155 case 'Bulk Email':
353ffa53 156 $url = 'civicrm/activity/view';
6a488035
TO
157 $delUrl = 'civicrm/activity';
158 $qsView = "atype={$activityTypeId}&action=view&reset=1&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}";
159 if ($activityTypeName == 'Email') {
160 $showDelete = TRUE;
161 }
162 break;
163
164 case 'Inbound Email':
165 $url = 'civicrm/contact/view/activity';
166 $qsView = "atype={$activityTypeId}&action=view&reset=1&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}";
ee90a98c 167
426fe3c7 168 if (CRM_Activity_BAO_Activity::checkEditInboundEmailsPermissions()) {
ee90a98c
CR
169 $showDelete = $showUpdate = TRUE;
170 $qsUpdate = "atype={$activityTypeId}&action=update&reset=1&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}";
171 }
6a488035
TO
172 break;
173
174 case 'Open Case':
175 case 'Change Case Type':
176 case 'Change Case Status':
177 case 'Change Case Start Date':
178 $showUpdate = $showDelete = FALSE;
353ffa53
TO
179 $url = 'civicrm/activity';
180 $qsView = "atype={$activityTypeId}&action=view&reset=1&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}";
181 $qsUpdate = "atype={$activityTypeId}&action=update&reset=1&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}";
6a488035
TO
182 break;
183
184 default:
353ffa53 185 $url = 'civicrm/activity';
6a488035 186 $showView = $showDelete = $showUpdate = TRUE;
353ffa53 187 $qsView = "atype={$activityTypeId}&action=view&reset=1&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}";
6a488035
TO
188 $qsUpdate = "atype={$activityTypeId}&action=update&reset=1&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}";
189
7808aae6 190 // When type is not available lets hide view and update.
6a488035
TO
191 if (empty($activityTypeName)) {
192 $showView = $showUpdate = FALSE;
193 }
194 break;
195 }
196
197 $qsDelete = "atype={$activityTypeId}&action=delete&reset=1&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}";
198
be2fb01f 199 $actionLinks = [];
6a488035
TO
200
201 if ($showView) {
be2fb01f 202 $actionLinks += [
353ffa53 203 CRM_Core_Action::
be2fb01f 204 VIEW => [
353ffa53
TO
205 'name' => ts('View'),
206 'url' => $url,
207 'qs' => $qsView,
208 'title' => ts('View Activity'),
be2fb01f
CW
209 ],
210 ];
6a488035
TO
211 }
212
213 if ($showUpdate) {
214 $updateUrl = 'civicrm/activity/add';
215 if ($activityTypeName == 'Email') {
216 $updateUrl = 'civicrm/activity/email/add';
217 }
218 elseif ($activityTypeName == 'Print PDF Letter') {
219 $updateUrl = 'civicrm/activity/pdf/add';
220 }
481a74f4 221 if (CRM_Activity_BAO_Activity::checkPermission($activityId, CRM_Core_Action::UPDATE)) {
be2fb01f 222 $actionLinks += [
353ffa53 223 CRM_Core_Action::
be2fb01f 224 UPDATE => [
353ffa53
TO
225 'name' => ts('Edit'),
226 'url' => $updateUrl,
227 'qs' => $qsUpdate,
228 'title' => ts('Update Activity'),
be2fb01f
CW
229 ],
230 ];
bbfeec41 231 }
6a488035
TO
232 }
233
234 if (
235 $activityTypeName &&
236 CRM_Case_BAO_Case::checkPermission($activityId, 'File On Case', $activityTypeId)
237 ) {
be2fb01f 238 $actionLinks += [
353ffa53 239 CRM_Core_Action::
be2fb01f 240 ADD => [
353ffa53
TO
241 'name' => ts('File on Case'),
242 'url' => '#',
243 'extra' => 'onclick="javascript:fileOnCase( \'file\', \'%%id%%\', null, this ); return false;"',
244 'title' => ts('File on Case'),
be2fb01f
CW
245 ],
246 ];
6a488035
TO
247 }
248
249 if ($showDelete) {
250 if (!isset($delUrl) || !$delUrl) {
251 $delUrl = $url;
252 }
be2fb01f 253 $actionLinks += [
353ffa53 254 CRM_Core_Action::
be2fb01f 255 DELETE => [
353ffa53
TO
256 'name' => ts('Delete'),
257 'url' => $delUrl,
258 'qs' => $qsDelete,
259 'title' => ts('Delete Activity'),
be2fb01f
CW
260 ],
261 ];
6a488035
TO
262 }
263
264 if ($accessMailingReport) {
be2fb01f 265 $actionLinks += [
353ffa53 266 CRM_Core_Action::
be2fb01f 267 BROWSE => [
353ffa53
TO
268 'name' => ts('Mailing Report'),
269 'url' => 'civicrm/mailing/report',
270 'qs' => "mid={$sourceRecordId}&reset=1&cid=%%cid%%&context=activitySelector",
271 'title' => ts('View Mailing Report'),
be2fb01f
CW
272 ],
273 ];
6a488035
TO
274 }
275
276 return $actionLinks;
277 }
278
279 /**
100fef9d 280 * Getter for array of the parameters required for creating pager.
6a488035 281 *
da6b46f4 282 * @param $action
c490a46a 283 * @param array $params
6a488035 284 */
00be9182 285 public function getPagerParams($action, &$params) {
353ffa53 286 $params['status'] = ts('Activities %%StatusMessage%%');
6a488035 287 $params['csvString'] = NULL;
353ffa53 288 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
6a488035
TO
289
290 $params['buttonTop'] = 'PagerTopButton';
291 $params['buttonBottom'] = 'PagerBottomButton';
292 }
293
294 /**
100fef9d 295 * Returns the column headers as an array of tuples:
6a488035
TO
296 * (name, sortName (key to the sort array))
297 *
041ab3d1
TO
298 * @param string $action
299 * The action being performed.
3f8d2862 300 * @param string $output
041ab3d1 301 * What should the result set include (web/email/csv).
6a488035 302 *
a6c01b45
CW
303 * @return array
304 * the column headers that need to be displayed
6a488035 305 */
00be9182 306 public function &getColumnHeaders($action = NULL, $output = NULL) {
6a488035 307 if ($output == CRM_Core_Selector_Controller::EXPORT || $output == CRM_Core_Selector_Controller::SCREEN) {
be2fb01f 308 $csvHeaders = [ts('Activity Type'), ts('Description'), ts('Activity Date')];
6a488035
TO
309 foreach (self::_getColumnHeaders() as $column) {
310 if (array_key_exists('name', $column)) {
311 $csvHeaders[] = $column['name'];
312 }
313 }
314 return $csvHeaders;
315 }
316 else {
317 return self::_getColumnHeaders();
318 }
319 }
320
321 /**
322 * Returns total number of rows for the query.
323 *
041ab3d1
TO
324 * @param string $action
325 * Action being performed.
6a488035 326 *
fd31fa4c
EM
327 * @param null $case
328 *
a6c01b45
CW
329 * @return int
330 * Total number of rows
6a488035 331 */
00be9182 332 public function getTotalCount($action, $case = NULL) {
be2fb01f 333 $params = [
6a488035
TO
334 'contact_id' => $this->_contactId,
335 'admin' => $this->_admin,
336 'caseId' => $case,
337 'context' => $this->_context,
338 'activity_type_id' => $this->_activityTypeIDs,
339 'offset' => 0,
340 'rowCount' => 0,
341 'sort' => NULL,
be2fb01f 342 ];
6e793248 343 return CRM_Activity_BAO_Activity::getActivitiesCount($params);
6a488035
TO
344 }
345
346 /**
fe482240 347 * Returns all the rows in the given offset and rowCount.
6a488035 348 *
3f8d2862 349 * @param string $action
041ab3d1
TO
350 * The action being performed.
351 * @param int $offset
352 * The row number to start from.
353 * @param int $rowCount
354 * The number of rows to return.
355 * @param string $sort
356 * The sql string that describes the sort order.
3f8d2862 357 * @param string $output
041ab3d1 358 * What should the result set include (web/email/csv).
2a6da8d7
EM
359 *
360 * @param null $case
6a488035 361 *
a6c01b45
CW
362 * @return int
363 * the total number of rows for this action
6a488035 364 */
00be9182 365 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL, $case = NULL) {
be2fb01f 366 $params = [
6a488035
TO
367 'contact_id' => $this->_contactId,
368 'admin' => $this->_admin,
369 'caseId' => $case,
370 'context' => $this->_context,
371 'activity_type_id' => $this->_activityTypeIDs,
372 'offset' => $offset,
373 'rowCount' => $rowCount,
374 'sort' => $sort,
be2fb01f 375 ];
6a488035 376 $config = CRM_Core_Config::singleton();
6e793248 377 $rows = CRM_Activity_BAO_Activity::getActivities($params);
6a488035
TO
378
379 if (empty($rows)) {
380 return $rows;
381 }
382
383 $activityStatus = CRM_Core_PseudoConstant::activityStatus();
384
385 $engagementLevels = CRM_Campaign_PseudoConstant::engagementLevel();
386
7808aae6 387 // CRM-4418
be2fb01f 388 $permissions = [$this->_permission];
6a488035
TO
389 if (CRM_Core_Permission::check('delete activities')) {
390 $permissions[] = CRM_Core_Permission::DELETE;
391 }
392 $mask = CRM_Core_Action::mask($permissions);
393
394 foreach ($rows as $k => $row) {
395 $row = &$rows[$k];
396
397 // DRAFTING: provide a facility for db-stored strings
398 // localize the built-in activity names for display
399 // (these are not enums, so we can't use any automagic here)
400 switch ($row['activity_type']) {
401 case 'Meeting':
402 $row['activity_type'] = ts('Meeting');
403 break;
404
405 case 'Phone Call':
406 $row['activity_type'] = ts('Phone Call');
407 break;
408
409 case 'Email':
410 $row['activity_type'] = ts('Email');
411 break;
412
413 case 'SMS':
414 $row['activity_type'] = ts('SMS');
415 break;
416
417 case 'Event':
418 $row['activity_type'] = ts('Event');
419 break;
420 }
421
422 // add class to this row if overdue
423 if (CRM_Utils_Date::overdue(CRM_Utils_Array::value('activity_date_time', $row))
424 && CRM_Utils_Array::value('status_id', $row) == 1
425 ) {
426 $row['overdue'] = 1;
427 $row['class'] = 'status-overdue';
428 }
429 else {
430 $row['overdue'] = 0;
431 $row['class'] = 'status-ontime';
432 }
433
434 $row['status'] = $row['status_id'] ? $activityStatus[$row['status_id']] : NULL;
435
436 if ($engagementLevel = CRM_Utils_Array::value('engagement_level', $row)) {
437 $row['engagement_level'] = CRM_Utils_Array::value($engagementLevel, $engagementLevels, $engagementLevel);
438 }
439
7808aae6 440 // CRM-3553
6a488035 441 $accessMailingReport = FALSE;
a7488080 442 if (!empty($row['mailingId'])) {
6a488035
TO
443 $accessMailingReport = TRUE;
444 }
445
446 $actionLinks = $this->actionLinks(CRM_Utils_Array::value('activity_type_id', $row),
447 CRM_Utils_Array::value('source_record_id', $row),
448 $accessMailingReport,
449 CRM_Utils_Array::value('activity_id', $row),
450 $this->_key
451 );
452
453 $actionMask = array_sum(array_keys($actionLinks)) & $mask;
454
455 if ($output != CRM_Core_Selector_Controller::EXPORT && $output != CRM_Core_Selector_Controller::SCREEN) {
456 $row['action'] = CRM_Core_Action::formLink($actionLinks,
457 $actionMask,
be2fb01f 458 [
6a488035
TO
459 'id' => $row['activity_id'],
460 'cid' => $this->_contactId,
461 'cxt' => $this->_context,
462 'caseid' => CRM_Utils_Array::value('case_id', $row),
be2fb01f 463 ],
87dab4a4
AH
464 ts('more'),
465 FALSE,
466 'activity.selector.action',
467 'Activity',
468 $row['activity_id']
6a488035
TO
469 );
470 }
471
472 unset($row);
473 }
474
475 return $rows;
476 }
477
478 /**
100fef9d 479 * Name of export file.
6a488035 480 *
041ab3d1
TO
481 * @param string $output
482 * Type of output.
6a488035 483 *
a6c01b45
CW
484 * @return string
485 * name of the file
6a488035 486 */
00be9182 487 public function getExportFileName($output = 'csv') {
6a488035
TO
488 return ts('CiviCRM Activity');
489 }
490
491 /**
fe482240 492 * Get colunmn headers for search selector.
6a488035
TO
493 *
494 *
a6c01b45 495 * @return array
6a488035
TO
496 */
497 private static function &_getColumnHeaders() {
498 if (!isset(self::$_columnHeaders)) {
be2fb01f
CW
499 self::$_columnHeaders = [
500 [
353ffa53 501 'name' => ts('Type'),
6a488035
TO
502 'sort' => 'activity_type',
503 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
504 ],
505 [
353ffa53 506 'name' => ts('Subject'),
6a488035
TO
507 'sort' => 'subject',
508 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
509 ],
510 [
353ffa53 511 'name' => ts('Added By'),
6a488035
TO
512 'sort' => 'source_contact_name',
513 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
514 ],
515 ['name' => ts('With')],
516 ['name' => ts('Assigned')],
517 [
6a488035
TO
518 'name' => ts('Date'),
519 'sort' => 'activity_date_time',
520 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
521 ],
522 [
6a488035
TO
523 'name' => ts('Status'),
524 'sort' => 'status_id',
525 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
526 ],
527 ['desc' => ts('Actions')],
528 ];
6a488035
TO
529 }
530
531 return self::$_columnHeaders;
532 }
96025800 533
6a488035 534}