CRM-14106 - Regex targeting the first part of if statements
[civicrm-core.git] / CRM / Activity / Selector / Activity.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
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 }
bbfeec41
BS
212 if ( CRM_Activity_BAO_Activity::checkPermission($activityId, CRM_Core_Action::UPDATE) ) {
213 $actionLinks += array(CRM_Core_Action::UPDATE => array('name' => ts('Edit'),
214 'url' => $updateUrl,
215 'qs' => $qsUpdate,
216 'title' => ts('Update Activity'),
217 ));
218 }
6a488035
TO
219 }
220
221 if (
222 $activityTypeName &&
223 CRM_Case_BAO_Case::checkPermission($activityId, 'File On Case', $activityTypeId)
224 ) {
225 $actionLinks += array(CRM_Core_Action::ADD => array('name' => ts('File On Case'),
226 'url' => '#',
227 'extra' => 'onclick="javascript:fileOnCase( \'file\', \'%%id%%\' ); return false;"',
228 'title' => ts('File On Case'),
229 ));
230 }
231
232 if ($showDelete) {
233 if (!isset($delUrl) || !$delUrl) {
234 $delUrl = $url;
235 }
236 $actionLinks += array(CRM_Core_Action::DELETE => array('name' => ts('Delete'),
237 'url' => $delUrl,
238 'qs' => $qsDelete,
239 'title' => ts('Delete Activity'),
240 ));
241 }
242
243 if ($accessMailingReport) {
244 $actionLinks += array(CRM_Core_Action::BROWSE => array('name' => ts('Mailing Report'),
245 'url' => 'civicrm/mailing/report',
246 'qs' => "mid={$sourceRecordId}&reset=1&cid=%%cid%%&context=activitySelector",
247 'title' => ts('View Mailing Report'),
248 ));
249 }
250
251 return $actionLinks;
252 }
253
254 /**
255 * getter for array of the parameters required for creating pager.
256 *
257 * @param
258 * @access public
259 */
260 function getPagerParams($action, &$params) {
261 $params['status'] = ts('Activities %%StatusMessage%%');
262 $params['csvString'] = NULL;
263 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
264
265 $params['buttonTop'] = 'PagerTopButton';
266 $params['buttonBottom'] = 'PagerBottomButton';
267 }
268
269 /**
270 * returns the column headers as an array of tuples:
271 * (name, sortName (key to the sort array))
272 *
273 * @param string $action the action being performed
274 * @param enum $output what should the result set include (web/email/csv)
275 *
276 * @return array the column headers that need to be displayed
277 * @access public
278 */
279 function &getColumnHeaders($action = NULL, $output = NULL) {
280 if ($output == CRM_Core_Selector_Controller::EXPORT || $output == CRM_Core_Selector_Controller::SCREEN) {
281 $csvHeaders = array(ts('Activity Type'), ts('Description'), ts('Activity Date'));
282 foreach (self::_getColumnHeaders() as $column) {
283 if (array_key_exists('name', $column)) {
284 $csvHeaders[] = $column['name'];
285 }
286 }
287 return $csvHeaders;
288 }
289 else {
290 return self::_getColumnHeaders();
291 }
292 }
293
294 /**
295 * Returns total number of rows for the query.
296 *
297 * @param string $action - action being performed
298 *
299 * @return int Total number of rows
300 * @access public
301 */
302 function getTotalCount($action, $case = NULL) {
303 $params = array(
304 'contact_id' => $this->_contactId,
305 'admin' => $this->_admin,
306 'caseId' => $case,
307 'context' => $this->_context,
308 'activity_type_id' => $this->_activityTypeIDs,
309 'offset' => 0,
310 'rowCount' => 0,
311 'sort' => NULL,
312 );
313 return CRM_Activity_BAO_Activity::getActivitiesCount($params);
314 }
315
316 /**
317 * returns all the rows in the given offset and rowCount
318 *
319 * @param enum $action the action being performed
320 * @param int $offset the row number to start from
321 * @param int $rowCount the number of rows to return
322 * @param string $sort the sql string that describes the sort order
323 * @param enum $output what should the result set include (web/email/csv)
324 *
325 * @return int the total number of rows for this action
326 */
327 function &getRows($action, $offset, $rowCount, $sort, $output = NULL, $case = NULL) {
328 $params = array(
329 'contact_id' => $this->_contactId,
330 'admin' => $this->_admin,
331 'caseId' => $case,
332 'context' => $this->_context,
333 'activity_type_id' => $this->_activityTypeIDs,
334 'offset' => $offset,
335 'rowCount' => $rowCount,
336 'sort' => $sort,
337 );
338 $config = CRM_Core_Config::singleton();
339 $rows = CRM_Activity_BAO_Activity::getActivities($params);
340
341 if (empty($rows)) {
342 return $rows;
343 }
344
345 $activityStatus = CRM_Core_PseudoConstant::activityStatus();
346
347 $engagementLevels = CRM_Campaign_PseudoConstant::engagementLevel();
348
349 //CRM-4418
350 $permissions = array($this->_permission);
351 if (CRM_Core_Permission::check('delete activities')) {
352 $permissions[] = CRM_Core_Permission::DELETE;
353 }
354 $mask = CRM_Core_Action::mask($permissions);
355
356 foreach ($rows as $k => $row) {
357 $row = &$rows[$k];
358
359 // DRAFTING: provide a facility for db-stored strings
360 // localize the built-in activity names for display
361 // (these are not enums, so we can't use any automagic here)
362 switch ($row['activity_type']) {
363 case 'Meeting':
364 $row['activity_type'] = ts('Meeting');
365 break;
366
367 case 'Phone Call':
368 $row['activity_type'] = ts('Phone Call');
369 break;
370
371 case 'Email':
372 $row['activity_type'] = ts('Email');
373 break;
374
375 case 'SMS':
376 $row['activity_type'] = ts('SMS');
377 break;
378
379 case 'Event':
380 $row['activity_type'] = ts('Event');
381 break;
382 }
383
384 // add class to this row if overdue
385 if (CRM_Utils_Date::overdue(CRM_Utils_Array::value('activity_date_time', $row))
386 && CRM_Utils_Array::value('status_id', $row) == 1
387 ) {
388 $row['overdue'] = 1;
389 $row['class'] = 'status-overdue';
390 }
391 else {
392 $row['overdue'] = 0;
393 $row['class'] = 'status-ontime';
394 }
395
396 $row['status'] = $row['status_id'] ? $activityStatus[$row['status_id']] : NULL;
397
398 if ($engagementLevel = CRM_Utils_Array::value('engagement_level', $row)) {
399 $row['engagement_level'] = CRM_Utils_Array::value($engagementLevel, $engagementLevels, $engagementLevel);
400 }
401
402 //CRM-3553
403 $accessMailingReport = FALSE;
a7488080 404 if (!empty($row['mailingId'])) {
6a488035
TO
405 $accessMailingReport = TRUE;
406 }
407
408 $actionLinks = $this->actionLinks(CRM_Utils_Array::value('activity_type_id', $row),
409 CRM_Utils_Array::value('source_record_id', $row),
410 $accessMailingReport,
411 CRM_Utils_Array::value('activity_id', $row),
412 $this->_key
413 );
414
415 $actionMask = array_sum(array_keys($actionLinks)) & $mask;
416
417 if ($output != CRM_Core_Selector_Controller::EXPORT && $output != CRM_Core_Selector_Controller::SCREEN) {
418 $row['action'] = CRM_Core_Action::formLink($actionLinks,
419 $actionMask,
420 array(
421 'id' => $row['activity_id'],
422 'cid' => $this->_contactId,
423 'cxt' => $this->_context,
424 'caseid' => CRM_Utils_Array::value('case_id', $row),
87dab4a4
AH
425 ),
426 ts('more'),
427 FALSE,
428 'activity.selector.action',
429 'Activity',
430 $row['activity_id']
6a488035
TO
431 );
432 }
433
434 unset($row);
435 }
436
437 return $rows;
438 }
439
440 /**
441 * name of export file.
442 *
443 * @param string $output type of output
444 *
445 * @return string name of the file
446 */
447 function getExportFileName($output = 'csv') {
448 return ts('CiviCRM Activity');
449 }
450
451 /**
452 * get colunmn headers for search selector
453 *
454 *
455 * @return array $_columnHeaders
456 * @access private
457 */
458 private static function &_getColumnHeaders() {
459 if (!isset(self::$_columnHeaders)) {
460 self::$_columnHeaders = array(
461 array('name' => ts('Type'),
462 'sort' => 'activity_type',
463 'direction' => CRM_Utils_Sort::DONTCARE,
464 ),
465 array('name' => ts('Subject'),
466 'sort' => 'subject',
467 'direction' => CRM_Utils_Sort::DONTCARE,
468 ),
469 array('name' => ts('Added By'),
470 'sort' => 'source_contact_name',
471 'direction' => CRM_Utils_Sort::DONTCARE,
472 ),
473 array('name' => ts('With')),
474 array('name' => ts('Assigned')),
475 array(
476 'name' => ts('Date'),
477 'sort' => 'activity_date_time',
478 'direction' => CRM_Utils_Sort::DONTCARE,
479 ),
480 array(
481 'name' => ts('Status'),
482 'sort' => 'status_id',
483 'direction' => CRM_Utils_Sort::DONTCARE,
484 ),
485 array('desc' => ts('Actions')),
486 );
487 }
488
489 return self::$_columnHeaders;
490 }
491}
492