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