Merge pull request #4627 from colemanw/docblocks
[civicrm-core.git] / CRM / Activity / Selector / Activity.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
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 /**
43 * we use desc to remind us what that column is, name is used in the tpl
44 *
45 * @var array
46 * @static
47 */
48 static $_columnHeaders;
49
50 /**
51 * contactId - contact id of contact whose activies are displayed
52 *
53 * @var int
54 * @access protected
55 */
56 protected $_contactId;
57
58 protected $_admin;
59
60 protected $_context;
61
62 protected $_activityTypeIDs;
63
64 protected $_viewOptions;
65
66 /**
67 * Class constructor
68 *
69 * @param int $contactId - contact whose activities we want to display
70 * @param int $permission - the permission we have for this contact
71 *
dd244018
EM
72 * @param bool $admin
73 * @param string $context
74 * @param null $activityTypeIDs
75 *
76 * @return \CRM_Activity_Selector_Activity
77 @access public
6a488035
TO
78 */
79 function __construct($contactId,
80 $permission,
81 $admin = FALSE,
82 $context = 'activity',
83 $activityTypeIDs = NULL) {
84 $this->_contactId = $contactId;
85 $this->_permission = $permission;
86 $this->_admin = $admin;
87 $this->_context = $context;
88 $this->_activityTypeIDs = $activityTypeIDs;
89
90 // get all enabled view componentc (check if case is enabled)
91 $this->_viewOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
92 'contact_view_options', TRUE, NULL, TRUE
93 );
94 }
95
96 /**
97 * This method returns the action links that are given for each search row.
98 * currently the action links added for each row are
99 *
100 * - View
101 *
c490a46a 102 * @param int $activityTypeId
77b97be7
EM
103 * @param null $sourceRecordId
104 * @param bool $accessMailingReport
105 * @param null $activityId
106 * @param null $key
107 * @param null $compContext
108 *
6a488035
TO
109 * @return array
110 * @access public
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 /**
275 * getter for array of the parameters required for creating pager.
276 *
da6b46f4 277 * @param $action
c490a46a 278 * @param array $params
da6b46f4 279 *
6a488035
TO
280 * @access public
281 */
282 function getPagerParams($action, &$params) {
283 $params['status'] = ts('Activities %%StatusMessage%%');
284 $params['csvString'] = NULL;
285 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
286
287 $params['buttonTop'] = 'PagerTopButton';
288 $params['buttonBottom'] = 'PagerBottomButton';
289 }
290
291 /**
292 * returns the column headers as an array of tuples:
293 * (name, sortName (key to the sort array))
294 *
295 * @param string $action the action being performed
296 * @param enum $output what should the result set include (web/email/csv)
297 *
298 * @return array the column headers that need to be displayed
299 * @access public
300 */
301 function &getColumnHeaders($action = NULL, $output = NULL) {
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 *
319 * @param string $action - action being performed
320 *
fd31fa4c
EM
321 * @param null $case
322 *
6a488035
TO
323 * @return int Total number of rows
324 * @access public
325 */
326 function getTotalCount($action, $case = NULL) {
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 /**
341 * returns all the rows in the given offset and rowCount
342 *
2a6da8d7
EM
343 * @param enum $action the action being performed
344 * @param int $offset the row number to start from
345 * @param int $rowCount the number of rows to return
346 * @param string $sort the sql string that describes the sort order
347 * @param enum $output what should the result set include (web/email/csv)
348 *
349 * @param null $case
6a488035
TO
350 *
351 * @return int the total number of rows for this action
352 */
353 function &getRows($action, $offset, $rowCount, $sort, $output = NULL, $case = NULL) {
354 $params = array(
355 'contact_id' => $this->_contactId,
356 'admin' => $this->_admin,
357 'caseId' => $case,
358 'context' => $this->_context,
359 'activity_type_id' => $this->_activityTypeIDs,
360 'offset' => $offset,
361 'rowCount' => $rowCount,
362 'sort' => $sort,
363 );
364 $config = CRM_Core_Config::singleton();
365 $rows = CRM_Activity_BAO_Activity::getActivities($params);
366
367 if (empty($rows)) {
368 return $rows;
369 }
370
371 $activityStatus = CRM_Core_PseudoConstant::activityStatus();
372
373 $engagementLevels = CRM_Campaign_PseudoConstant::engagementLevel();
374
375 //CRM-4418
376 $permissions = array($this->_permission);
377 if (CRM_Core_Permission::check('delete activities')) {
378 $permissions[] = CRM_Core_Permission::DELETE;
379 }
380 $mask = CRM_Core_Action::mask($permissions);
381
382 foreach ($rows as $k => $row) {
383 $row = &$rows[$k];
384
385 // DRAFTING: provide a facility for db-stored strings
386 // localize the built-in activity names for display
387 // (these are not enums, so we can't use any automagic here)
388 switch ($row['activity_type']) {
389 case 'Meeting':
390 $row['activity_type'] = ts('Meeting');
391 break;
392
393 case 'Phone Call':
394 $row['activity_type'] = ts('Phone Call');
395 break;
396
397 case 'Email':
398 $row['activity_type'] = ts('Email');
399 break;
400
401 case 'SMS':
402 $row['activity_type'] = ts('SMS');
403 break;
404
405 case 'Event':
406 $row['activity_type'] = ts('Event');
407 break;
408 }
409
410 // add class to this row if overdue
411 if (CRM_Utils_Date::overdue(CRM_Utils_Array::value('activity_date_time', $row))
412 && CRM_Utils_Array::value('status_id', $row) == 1
413 ) {
414 $row['overdue'] = 1;
415 $row['class'] = 'status-overdue';
416 }
417 else {
418 $row['overdue'] = 0;
419 $row['class'] = 'status-ontime';
420 }
421
422 $row['status'] = $row['status_id'] ? $activityStatus[$row['status_id']] : NULL;
423
424 if ($engagementLevel = CRM_Utils_Array::value('engagement_level', $row)) {
425 $row['engagement_level'] = CRM_Utils_Array::value($engagementLevel, $engagementLevels, $engagementLevel);
426 }
427
428 //CRM-3553
429 $accessMailingReport = FALSE;
a7488080 430 if (!empty($row['mailingId'])) {
6a488035
TO
431 $accessMailingReport = TRUE;
432 }
433
434 $actionLinks = $this->actionLinks(CRM_Utils_Array::value('activity_type_id', $row),
435 CRM_Utils_Array::value('source_record_id', $row),
436 $accessMailingReport,
437 CRM_Utils_Array::value('activity_id', $row),
438 $this->_key
439 );
440
441 $actionMask = array_sum(array_keys($actionLinks)) & $mask;
442
443 if ($output != CRM_Core_Selector_Controller::EXPORT && $output != CRM_Core_Selector_Controller::SCREEN) {
444 $row['action'] = CRM_Core_Action::formLink($actionLinks,
445 $actionMask,
446 array(
447 'id' => $row['activity_id'],
448 'cid' => $this->_contactId,
449 'cxt' => $this->_context,
450 'caseid' => CRM_Utils_Array::value('case_id', $row),
87dab4a4
AH
451 ),
452 ts('more'),
453 FALSE,
454 'activity.selector.action',
455 'Activity',
456 $row['activity_id']
6a488035
TO
457 );
458 }
459
460 unset($row);
461 }
462
463 return $rows;
464 }
465
466 /**
467 * name of export file.
468 *
469 * @param string $output type of output
470 *
471 * @return string name of the file
472 */
473 function getExportFileName($output = 'csv') {
474 return ts('CiviCRM Activity');
475 }
476
477 /**
478 * get colunmn headers for search selector
479 *
480 *
481 * @return array $_columnHeaders
482 * @access private
483 */
484 private static function &_getColumnHeaders() {
485 if (!isset(self::$_columnHeaders)) {
486 self::$_columnHeaders = array(
487 array('name' => ts('Type'),
488 'sort' => 'activity_type',
489 'direction' => CRM_Utils_Sort::DONTCARE,
490 ),
491 array('name' => ts('Subject'),
492 'sort' => 'subject',
493 'direction' => CRM_Utils_Sort::DONTCARE,
494 ),
495 array('name' => ts('Added By'),
496 'sort' => 'source_contact_name',
497 'direction' => CRM_Utils_Sort::DONTCARE,
498 ),
499 array('name' => ts('With')),
500 array('name' => ts('Assigned')),
501 array(
502 'name' => ts('Date'),
503 'sort' => 'activity_date_time',
504 'direction' => CRM_Utils_Sort::DONTCARE,
505 ),
506 array(
507 'name' => ts('Status'),
508 'sort' => 'status_id',
509 'direction' => CRM_Utils_Sort::DONTCARE,
510 ),
511 array('desc' => ts('Actions')),
512 );
513 }
514
515 return self::$_columnHeaders;
516 }
517}
518