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