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