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