Merge pull request #15850 from civicrm/5.20
[civicrm-core.git] / CRM / Activity / Selector / Activity.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 activities for a contact.
20 */
21 class CRM_Activity_Selector_Activity extends CRM_Core_Selector_Base implements CRM_Core_Selector_API {
22
23 /**
24 * We use desc to remind us what that column is, name is used in the tpl
25 *
26 * @var array
27 */
28 public static $_columnHeaders;
29
30 /**
31 * ContactId - contact id of contact whose activies are displayed
32 *
33 * @var int
34 */
35 protected $_contactId;
36
37 protected $_admin;
38
39 protected $_context;
40
41 protected $_activityTypeIDs;
42
43 protected $_viewOptions;
44
45 /**
46 * Class constructor.
47 *
48 * @param int $contactId
49 * Contact whose activities we want to display.
50 * @param int $permission
51 * The permission we have for this contact.
52 *
53 * @param bool $admin
54 * @param string $context
55 * @param null $activityTypeIDs
56 *
57 * @return \CRM_Activity_Selector_Activity
58 */
59 public function __construct(
60 $contactId,
61 $permission,
62 $admin = FALSE,
63 $context = 'activity',
64 $activityTypeIDs = NULL) {
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 *
83 * @param int $activityTypeId
84 * @param int $sourceRecordId
85 * @param bool $accessMailingReport
86 * @param int $activityId
87 * @param null $key
88 * @param null $compContext
89 *
90 * @return array
91 */
92 public static function actionLinks(
93 $activityTypeId,
94 $sourceRecordId = NULL,
95 $accessMailingReport = FALSE,
96 $activityId = NULL,
97 $key = NULL,
98 $compContext = NULL) {
99
100 //CRM-14277 added additional param to handle activity search
101 $extraParams = "&searchContext=activity";
102
103 $extraParams .= ($key) ? "&key={$key}" : NULL;
104 if ($compContext) {
105 $extraParams .= "&compContext={$compContext}";
106 }
107
108 $showView = TRUE;
109 $showUpdate = $showDelete = FALSE;
110 $qsUpdate = NULL;
111 $url = NULL;
112 $qsView = NULL;
113
114 $activityTypeName = CRM_Core_PseudoConstant::getName('CRM_Activity_BAO_Activity', 'activity_type_id', $activityTypeId);
115
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.
119 switch ($activityTypeName) {
120 case 'Event Registration':
121 case 'Change Registration':
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
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
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':
156 $url = 'civicrm/activity/view';
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}";
167
168 if (CRM_Activity_BAO_Activity::checkEditInboundEmailsPermissions()) {
169 $showDelete = $showUpdate = TRUE;
170 $qsUpdate = "atype={$activityTypeId}&action=update&reset=1&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}";
171 }
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;
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}";
182 break;
183
184 default:
185 $url = 'civicrm/activity';
186 $showView = $showDelete = $showUpdate = TRUE;
187 $qsView = "atype={$activityTypeId}&action=view&reset=1&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}";
188 $qsUpdate = "atype={$activityTypeId}&action=update&reset=1&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}";
189
190 // When type is not available lets hide view and update.
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
199 $actionLinks = [];
200
201 if ($showView) {
202 $actionLinks += [
203 CRM_Core_Action::
204 VIEW => [
205 'name' => ts('View'),
206 'url' => $url,
207 'qs' => $qsView,
208 'title' => ts('View Activity'),
209 ],
210 ];
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 }
221 if (CRM_Activity_BAO_Activity::checkPermission($activityId, CRM_Core_Action::UPDATE)) {
222 $actionLinks += [
223 CRM_Core_Action::
224 UPDATE => [
225 'name' => ts('Edit'),
226 'url' => $updateUrl,
227 'qs' => $qsUpdate,
228 'title' => ts('Update Activity'),
229 ],
230 ];
231 }
232 }
233
234 if (
235 $activityTypeName &&
236 CRM_Case_BAO_Case::checkPermission($activityId, 'File On Case', $activityTypeId)
237 ) {
238 $actionLinks += [
239 CRM_Core_Action::
240 ADD => [
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'),
245 ],
246 ];
247 }
248
249 if ($showDelete) {
250 if (!isset($delUrl) || !$delUrl) {
251 $delUrl = $url;
252 }
253 $actionLinks += [
254 CRM_Core_Action::
255 DELETE => [
256 'name' => ts('Delete'),
257 'url' => $delUrl,
258 'qs' => $qsDelete,
259 'title' => ts('Delete Activity'),
260 ],
261 ];
262 }
263
264 if ($accessMailingReport) {
265 $actionLinks += [
266 CRM_Core_Action::
267 BROWSE => [
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'),
272 ],
273 ];
274 }
275
276 return $actionLinks;
277 }
278
279 /**
280 * Getter for array of the parameters required for creating pager.
281 *
282 * @param $action
283 * @param array $params
284 */
285 public function getPagerParams($action, &$params) {
286 $params['status'] = ts('Activities %%StatusMessage%%');
287 $params['csvString'] = NULL;
288 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
289
290 $params['buttonTop'] = 'PagerTopButton';
291 $params['buttonBottom'] = 'PagerBottomButton';
292 }
293
294 /**
295 * Returns the column headers as an array of tuples:
296 * (name, sortName (key to the sort array))
297 *
298 * @param string $action
299 * The action being performed.
300 * @param string $output
301 * What should the result set include (web/email/csv).
302 *
303 * @return array
304 * the column headers that need to be displayed
305 */
306 public function &getColumnHeaders($action = NULL, $output = NULL) {
307 if ($output == CRM_Core_Selector_Controller::EXPORT || $output == CRM_Core_Selector_Controller::SCREEN) {
308 $csvHeaders = [ts('Activity Type'), ts('Description'), ts('Activity Date')];
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 *
324 * @param string $action
325 * Action being performed.
326 *
327 * @param null $case
328 *
329 * @return int
330 * Total number of rows
331 */
332 public function getTotalCount($action, $case = NULL) {
333 $params = [
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,
342 ];
343 return CRM_Activity_BAO_Activity::getActivitiesCount($params);
344 }
345
346 /**
347 * Returns all the rows in the given offset and rowCount.
348 *
349 * @param string $action
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.
357 * @param string $output
358 * What should the result set include (web/email/csv).
359 *
360 * @param null $case
361 *
362 * @return int
363 * the total number of rows for this action
364 */
365 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL, $case = NULL) {
366 $params = [
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,
375 ];
376 $config = CRM_Core_Config::singleton();
377 $rows = CRM_Activity_BAO_Activity::getActivities($params);
378
379 if (empty($rows)) {
380 return $rows;
381 }
382
383 $activityStatus = CRM_Core_PseudoConstant::activityStatus();
384
385 $engagementLevels = CRM_Campaign_PseudoConstant::engagementLevel();
386
387 // CRM-4418
388 $permissions = [$this->_permission];
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
440 // CRM-3553
441 $accessMailingReport = FALSE;
442 if (!empty($row['mailingId'])) {
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,
458 [
459 'id' => $row['activity_id'],
460 'cid' => $this->_contactId,
461 'cxt' => $this->_context,
462 'caseid' => CRM_Utils_Array::value('case_id', $row),
463 ],
464 ts('more'),
465 FALSE,
466 'activity.selector.action',
467 'Activity',
468 $row['activity_id']
469 );
470 }
471
472 unset($row);
473 }
474
475 return $rows;
476 }
477
478 /**
479 * Name of export file.
480 *
481 * @param string $output
482 * Type of output.
483 *
484 * @return string
485 * name of the file
486 */
487 public function getExportFileName($output = 'csv') {
488 return ts('CiviCRM Activity');
489 }
490
491 /**
492 * Get colunmn headers for search selector.
493 *
494 *
495 * @return array
496 */
497 private static function &_getColumnHeaders() {
498 if (!isset(self::$_columnHeaders)) {
499 self::$_columnHeaders = [
500 [
501 'name' => ts('Type'),
502 'sort' => 'activity_type',
503 'direction' => CRM_Utils_Sort::DONTCARE,
504 ],
505 [
506 'name' => ts('Subject'),
507 'sort' => 'subject',
508 'direction' => CRM_Utils_Sort::DONTCARE,
509 ],
510 [
511 'name' => ts('Added By'),
512 'sort' => 'source_contact_name',
513 'direction' => CRM_Utils_Sort::DONTCARE,
514 ],
515 ['name' => ts('With')],
516 ['name' => ts('Assigned')],
517 [
518 'name' => ts('Date'),
519 'sort' => 'activity_date_time',
520 'direction' => CRM_Utils_Sort::DONTCARE,
521 ],
522 [
523 'name' => ts('Status'),
524 'sort' => 'status_id',
525 'direction' => CRM_Utils_Sort::DONTCARE,
526 ],
527 ['desc' => ts('Actions')],
528 ];
529 }
530
531 return self::$_columnHeaders;
532 }
533
534 }