INFRA-132 - Fix spacing of @return tag in comments
[civicrm-core.git] / CRM / Activity / Selector / Activity.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class is used to retrieve and display activities for a contact
38 *
39 */
40 class 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 */
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 *
68 * @param int $contactId
69 * Contact whose activities we want to display.
70 * @param int $permission
71 * The permission we have for this contact.
72 *
73 * @param bool $admin
74 * @param string $context
75 * @param null $activityTypeIDs
76 *
77 * @return \CRM_Activity_Selector_Activity
78 @access public
79 */
80 function __construct(
81 $contactId,
82 $permission,
83 $admin = FALSE,
84 $context = 'activity',
85 $activityTypeIDs = NULL) {
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 *
104 * @param int $activityTypeId
105 * @param int $sourceRecordId
106 * @param bool $accessMailingReport
107 * @param int $activityId
108 * @param null $key
109 * @param null $compContext
110 *
111 * @return array
112 */
113 public static function actionLinks(
114 $activityTypeId,
115 $sourceRecordId = NULL,
116 $accessMailingReport = FALSE,
117 $activityId = NULL,
118 $key = NULL,
119 $compContext = NULL) {
120 static $activityActTypes = NULL;
121 //CRM-14277 added addtitional param to handle activity search
122 $extraParams = "&searchContext=activity";
123
124 $extraParams .= ($key) ? "&key={$key}" : NULL;
125 if ($compContext) {
126 $extraParams .= "&compContext={$compContext}";
127 }
128
129 $showView = TRUE;
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':
143 case 'Change Registration':
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
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
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':
178 $url = 'civicrm/activity/view';
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;
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}";
199 break;
200
201 default:
202 $url = 'civicrm/activity';
203 $showView = $showDelete = $showUpdate = TRUE;
204 $qsView = "atype={$activityTypeId}&action=view&reset=1&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}";
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) {
219 $actionLinks += array(CRM_Core_Action::
220 VIEW => array(
221 'name' => ts('View'),
222 'url' => $url,
223 'qs' => $qsView,
224 'title' => ts('View Activity'),
225 ));
226 }
227
228 if ($showUpdate) {
229 $updateUrl = 'civicrm/activity/add';
230 if ($activityTypeName == 'Email') {
231 $updateUrl = 'civicrm/activity/email/add';
232 }
233 elseif ($activityTypeName == 'Print PDF Letter') {
234 $updateUrl = 'civicrm/activity/pdf/add';
235 }
236 if (CRM_Activity_BAO_Activity::checkPermission($activityId, CRM_Core_Action::UPDATE)) {
237 $actionLinks += array(CRM_Core_Action::
238 UPDATE => array(
239 'name' => ts('Edit'),
240 'url' => $updateUrl,
241 'qs' => $qsUpdate,
242 'title' => ts('Update Activity'),
243 ));
244 }
245 }
246
247 if (
248 $activityTypeName &&
249 CRM_Case_BAO_Case::checkPermission($activityId, 'File On Case', $activityTypeId)
250 ) {
251 $actionLinks += array(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'),
257 ));
258 }
259
260 if ($showDelete) {
261 if (!isset($delUrl) || !$delUrl) {
262 $delUrl = $url;
263 }
264 $actionLinks += array(CRM_Core_Action::
265 DELETE => array(
266 'name' => ts('Delete'),
267 'url' => $delUrl,
268 'qs' => $qsDelete,
269 'title' => ts('Delete Activity'),
270 ));
271 }
272
273 if ($accessMailingReport) {
274 $actionLinks += array(CRM_Core_Action::
275 BROWSE => array(
276 'name' => ts('Mailing Report'),
277 'url' => 'civicrm/mailing/report',
278 'qs' => "mid={$sourceRecordId}&reset=1&cid=%%cid%%&context=activitySelector",
279 'title' => ts('View Mailing Report'),
280 ));
281 }
282
283 return $actionLinks;
284 }
285
286 /**
287 * Getter for array of the parameters required for creating pager.
288 *
289 * @param $action
290 * @param array $params
291 *
292 */
293 public function getPagerParams($action, &$params) {
294 $params['status'] = ts('Activities %%StatusMessage%%');
295 $params['csvString'] = NULL;
296 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
297
298 $params['buttonTop'] = 'PagerTopButton';
299 $params['buttonBottom'] = 'PagerBottomButton';
300 }
301
302 /**
303 * Returns the column headers as an array of tuples:
304 * (name, sortName (key to the sort array))
305 *
306 * @param string $action
307 * The action being performed.
308 * @param enum $output
309 * What should the result set include (web/email/csv).
310 *
311 * @return array
312 * the column headers that need to be displayed
313 */
314 public function &getColumnHeaders($action = NULL, $output = NULL) {
315 if ($output == CRM_Core_Selector_Controller::EXPORT || $output == CRM_Core_Selector_Controller::SCREEN) {
316 $csvHeaders = array(ts('Activity Type'), ts('Description'), ts('Activity Date'));
317 foreach (self::_getColumnHeaders() as $column) {
318 if (array_key_exists('name', $column)) {
319 $csvHeaders[] = $column['name'];
320 }
321 }
322 return $csvHeaders;
323 }
324 else {
325 return self::_getColumnHeaders();
326 }
327 }
328
329 /**
330 * Returns total number of rows for the query.
331 *
332 * @param string $action
333 * Action being performed.
334 *
335 * @param null $case
336 *
337 * @return int
338 * Total number of rows
339 */
340 public function getTotalCount($action, $case = NULL) {
341 $params = array(
342 'contact_id' => $this->_contactId,
343 'admin' => $this->_admin,
344 'caseId' => $case,
345 'context' => $this->_context,
346 'activity_type_id' => $this->_activityTypeIDs,
347 'offset' => 0,
348 'rowCount' => 0,
349 'sort' => NULL,
350 );
351 return CRM_Activity_BAO_Activity::getActivitiesCount($params);
352 }
353
354 /**
355 * Returns all the rows in the given offset and rowCount
356 *
357 * @param enum $action
358 * The action being performed.
359 * @param int $offset
360 * The row number to start from.
361 * @param int $rowCount
362 * The number of rows to return.
363 * @param string $sort
364 * The sql string that describes the sort order.
365 * @param enum $output
366 * What should the result set include (web/email/csv).
367 *
368 * @param null $case
369 *
370 * @return int
371 * the total number of rows for this action
372 */
373 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL, $case = NULL) {
374 $params = array(
375 'contact_id' => $this->_contactId,
376 'admin' => $this->_admin,
377 'caseId' => $case,
378 'context' => $this->_context,
379 'activity_type_id' => $this->_activityTypeIDs,
380 'offset' => $offset,
381 'rowCount' => $rowCount,
382 'sort' => $sort,
383 );
384 $config = CRM_Core_Config::singleton();
385 $rows = CRM_Activity_BAO_Activity::getActivities($params);
386
387 if (empty($rows)) {
388 return $rows;
389 }
390
391 $activityStatus = CRM_Core_PseudoConstant::activityStatus();
392
393 $engagementLevels = CRM_Campaign_PseudoConstant::engagementLevel();
394
395 //CRM-4418
396 $permissions = array($this->_permission);
397 if (CRM_Core_Permission::check('delete activities')) {
398 $permissions[] = CRM_Core_Permission::DELETE;
399 }
400 $mask = CRM_Core_Action::mask($permissions);
401
402 foreach ($rows as $k => $row) {
403 $row = &$rows[$k];
404
405 // DRAFTING: provide a facility for db-stored strings
406 // localize the built-in activity names for display
407 // (these are not enums, so we can't use any automagic here)
408 switch ($row['activity_type']) {
409 case 'Meeting':
410 $row['activity_type'] = ts('Meeting');
411 break;
412
413 case 'Phone Call':
414 $row['activity_type'] = ts('Phone Call');
415 break;
416
417 case 'Email':
418 $row['activity_type'] = ts('Email');
419 break;
420
421 case 'SMS':
422 $row['activity_type'] = ts('SMS');
423 break;
424
425 case 'Event':
426 $row['activity_type'] = ts('Event');
427 break;
428 }
429
430 // add class to this row if overdue
431 if (CRM_Utils_Date::overdue(CRM_Utils_Array::value('activity_date_time', $row))
432 && CRM_Utils_Array::value('status_id', $row) == 1
433 ) {
434 $row['overdue'] = 1;
435 $row['class'] = 'status-overdue';
436 }
437 else {
438 $row['overdue'] = 0;
439 $row['class'] = 'status-ontime';
440 }
441
442 $row['status'] = $row['status_id'] ? $activityStatus[$row['status_id']] : NULL;
443
444 if ($engagementLevel = CRM_Utils_Array::value('engagement_level', $row)) {
445 $row['engagement_level'] = CRM_Utils_Array::value($engagementLevel, $engagementLevels, $engagementLevel);
446 }
447
448 //CRM-3553
449 $accessMailingReport = FALSE;
450 if (!empty($row['mailingId'])) {
451 $accessMailingReport = TRUE;
452 }
453
454 $actionLinks = $this->actionLinks(CRM_Utils_Array::value('activity_type_id', $row),
455 CRM_Utils_Array::value('source_record_id', $row),
456 $accessMailingReport,
457 CRM_Utils_Array::value('activity_id', $row),
458 $this->_key
459 );
460
461 $actionMask = array_sum(array_keys($actionLinks)) & $mask;
462
463 if ($output != CRM_Core_Selector_Controller::EXPORT && $output != CRM_Core_Selector_Controller::SCREEN) {
464 $row['action'] = CRM_Core_Action::formLink($actionLinks,
465 $actionMask,
466 array(
467 'id' => $row['activity_id'],
468 'cid' => $this->_contactId,
469 'cxt' => $this->_context,
470 'caseid' => CRM_Utils_Array::value('case_id', $row),
471 ),
472 ts('more'),
473 FALSE,
474 'activity.selector.action',
475 'Activity',
476 $row['activity_id']
477 );
478 }
479
480 unset($row);
481 }
482
483 return $rows;
484 }
485
486 /**
487 * Name of export file.
488 *
489 * @param string $output
490 * Type of output.
491 *
492 * @return string
493 * name of the file
494 */
495 public function getExportFileName($output = 'csv') {
496 return ts('CiviCRM Activity');
497 }
498
499 /**
500 * Get colunmn headers for search selector
501 *
502 *
503 * @return array
504 */
505 private static function &_getColumnHeaders() {
506 if (!isset(self::$_columnHeaders)) {
507 self::$_columnHeaders = array(
508 array(
509 'name' => ts('Type'),
510 'sort' => 'activity_type',
511 'direction' => CRM_Utils_Sort::DONTCARE,
512 ),
513 array(
514 'name' => ts('Subject'),
515 'sort' => 'subject',
516 'direction' => CRM_Utils_Sort::DONTCARE,
517 ),
518 array(
519 'name' => ts('Added By'),
520 'sort' => 'source_contact_name',
521 'direction' => CRM_Utils_Sort::DONTCARE,
522 ),
523 array('name' => ts('With')),
524 array('name' => ts('Assigned')),
525 array(
526 'name' => ts('Date'),
527 'sort' => 'activity_date_time',
528 'direction' => CRM_Utils_Sort::DONTCARE,
529 ),
530 array(
531 'name' => ts('Status'),
532 'sort' => 'status_id',
533 'direction' => CRM_Utils_Sort::DONTCARE,
534 ),
535 array('desc' => ts('Actions')),
536 );
537 }
538
539 return self::$_columnHeaders;
540 }
541 }