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