5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
12 | CiviCRM is free software; you can copy, modify, and distribute it |
13 | under the terms of the GNU Affero General Public License |
14 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
16 | CiviCRM is distributed in the hope that it will be useful, but |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | See the GNU Affero General Public License for more details. |
21 | You should have received a copy of the GNU Affero General Public |
22 | License and the CiviCRM Licensing Exception along |
23 | with this program; if not, contact CiviCRM LLC |
24 | at info[AT]civicrm[DOT]org. If you have questions about the |
25 | GNU Affero General Public License or the licensing of CiviCRM, |
26 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
27 +--------------------------------------------------------------------+
33 * @copyright CiviCRM LLC (c) 2004-2013
37 class CRM_Report_Form_Activity
extends CRM_Report_Form
{
39 protected $_customGroupExtends = array(
40 'Activity'); function __construct() {
41 $config = CRM_Core_Config
::singleton();
42 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents
);
43 if ($campaignEnabled) {
44 $getCampaigns = CRM_Campaign_BAO_Campaign
::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
45 $this->activeCampaigns
= $getCampaigns['campaigns'];
46 asort($this->activeCampaigns
);
47 $this->engagementLevels
= CRM_Campaign_PseudoConstant
::engagementLevel();
49 $this->activityTypes
= CRM_Core_PseudoConstant
::activityType(TRUE, FALSE, FALSE, 'label', TRUE);
50 asort($this->activityTypes
);
52 $this->_columns
= array(
55 'dao' => 'CRM_Contact_DAO_Contact',
58 'source_contact_id' =>
61 'alias' => 'civicrm_contact_source',
66 'name' => 'sort_name',
67 'title' => ts('Source Contact Name'),
68 'alias' => 'civicrm_contact_source',
73 'name' => 'sort_name',
74 'title' => ts('Assignee Contact Name'),
75 'alias' => 'civicrm_contact_assignee',
80 'name' => 'sort_name',
81 'title' => ts('Target Contact Name'),
82 'alias' => 'contact_civireport',
90 'name' => 'sort_name',
91 'alias' => 'civicrm_contact_source',
92 'title' => ts('Source Contact Name'),
94 'type' => CRM_Report_Form
::OP_STRING
,
98 'name' => 'sort_name',
99 'alias' => 'civicrm_contact_assignee',
100 'title' => ts('Assignee Contact Name'),
101 'operator' => 'like',
102 'type' => CRM_Report_Form
::OP_STRING
,
106 'name' => 'sort_name',
107 'alias' => 'contact_civireport',
108 'title' => ts('Target Contact Name'),
109 'operator' => 'like',
110 'type' => CRM_Report_Form
::OP_STRING
,
114 'name' => 'current_user',
115 'title' => ts('Limit To Current User'),
116 'type' => CRM_Utils_Type
::T_INT
,
117 'operatorType' => CRM_Report_Form
::OP_SELECT
,
118 'options' => array('0' => ts('No'), '1' => ts('Yes')),
121 'grouping' => 'contact-fields',
125 'dao' => 'CRM_Core_DAO_Email',
128 'contact_source_email' =>
131 'title' => ts('Source Contact Email'),
132 'alias' => 'civicrm_email_source',
134 'contact_assignee_email' =>
137 'title' => ts('Assignee Contact Email'),
138 'alias' => 'civicrm_email_assignee',
140 'contact_target_email' =>
143 'title' => ts('Target Contact Email'),
144 'alias' => 'civicrm_email_target',
149 'source_contact_email' =>
152 'title' => ts('Source Contact Email'),
153 'alias' => 'civicrm_email_source',
157 'civicrm_activity' =>
159 'dao' => 'CRM_Activity_DAO_Activity',
164 'no_display' => TRUE,
167 'source_record_id' =>
169 'no_display' => TRUE,
172 'activity_type_id' =>
173 array('title' => ts('Activity Type'),
175 'type' => CRM_Utils_Type
::T_STRING
,
177 'activity_subject' =>
178 array('title' => ts('Subject'),
181 'source_contact_id' =>
183 'no_display' => TRUE,
186 'activity_date_time' =>
187 array('title' => ts('Activity Date'),
191 array('title' => ts('Activity Status'),
193 'type' => CRM_Utils_Type
::T_STRING
,
196 array('title' => ts('Duration'),
197 'type' => CRM_Utils_Type
::T_INT
,
202 'activity_date_time' =>
204 'default' => 'this.month',
205 'operatorType' => CRM_Report_Form
::OP_DATE
,
207 'activity_subject' =>
208 array('title' => ts('Activity Subject')),
209 'activity_type_id' =>
210 array('title' => ts('Activity Type'),
211 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
212 'options' => $this->activityTypes
,
215 array('title' => ts('Activity Status'),
216 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
217 'options' => CRM_Core_PseudoConstant
::activityStatus(),
222 'source_contact_id' =>
223 array('title' => ts('Source Contact'), 'default_weight' => '0'),
224 'activity_date_time' =>
225 array('title' => ts('Activity Date'), 'default_weight' => '1'),
226 'activity_type_id' =>
227 array('title' => ts('Activity Type'), 'default_weight' => '2'),
229 'grouping' => 'activity-fields',
230 'alias' => 'activity',
232 'civicrm_activity_assignment' =>
234 'dao' => 'CRM_Activity_DAO_ActivityAssignment',
237 'assignee_contact_id' =>
239 'no_display' => TRUE,
243 'alias' => 'activity_assignment',
245 'civicrm_activity_target' =>
247 'dao' => 'CRM_Activity_DAO_ActivityTarget',
250 'target_contact_id' =>
252 'no_display' => TRUE,
256 'alias' => 'activity_target',
258 'civicrm_case_activity' =>
260 'dao' => 'CRM_Case_DAO_CaseActivity',
266 'no_display' => TRUE,
270 'alias' => 'case_activity',
272 ) +
$this->addAddressFields(FALSE, TRUE);
274 if ($campaignEnabled) {
275 // Add display column and filter for Survey Results, Campaign and Engagement Index if CiviCampaign is enabled
277 $this->_columns
['civicrm_activity']['fields']['result'] = array(
278 'title' => 'Survey Result',
279 'default' => 'false',
281 $this->_columns
['civicrm_activity']['filters']['result'] = array('title' => ts('Survey Result'),
282 'operator' => 'like',
283 'type' => CRM_Utils_Type
::T_STRING
,
285 if (!empty($this->activeCampaigns
)) {
286 $this->_columns
['civicrm_activity']['fields']['campaign_id'] = array(
287 'title' => 'Campaign',
288 'default' => 'false',
290 $this->_columns
['civicrm_activity']['filters']['campaign_id'] = array('title' => ts('Campaign'),
291 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
292 'options' => $this->activeCampaigns
,
295 if (!empty($this->engagementLevels
)) {
296 $this->_columns
['civicrm_activity']['fields']['engagement_level'] = array(
297 'title' => 'Engagement Index',
298 'default' => 'false',
300 $this->_columns
['civicrm_activity']['filters']['engagement_level'] = array('title' => ts('Engagement Index'),
301 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
302 'options' => $this->engagementLevels
,
306 $this->_groupFilter
= TRUE;
307 $this->_tagFilter
= TRUE;
308 parent
::__construct();
313 $seperator = CRM_CORE_DAO
::VALUE_SEPARATOR
;
314 $this->_columnHeaders
= array();
315 foreach ($this->_columns
as $tableName => $table) {
316 if (array_key_exists('fields', $table)) {
317 foreach ($table['fields'] as $fieldName => $field) {
318 if (CRM_Utils_Array
::value('required', $field) ||
319 CRM_Utils_Array
::value($fieldName, $this->_params
['fields'])
322 if (isset($this->_params
['group_bys']) &&
323 !CRM_Utils_Array
::value('activity_type_id', $this->_params
['group_bys']) &&
324 (in_array($fieldName, array(
325 'contact_assignee', 'assignee_contact_id')) ||
326 in_array($fieldName, array('contact_target', 'target_contact_id'))
329 $orderByRef = "activity_assignment_civireport.assignee_contact_id";
330 if (in_array($fieldName, array(
331 'contact_target', 'target_contact_id'))) {
332 $orderByRef = "activity_target_civireport.target_contact_id";
334 $select[] = "GROUP_CONCAT(DISTINCT {$field['dbAlias']} ORDER BY {$orderByRef} SEPARATOR '{$seperator}') as {$tableName}_{$fieldName}";
337 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
339 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
340 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
341 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['no_display'] = CRM_Utils_Array
::value('no_display', $field);
347 $this->_select
= "SELECT " . implode(', ', $select) . " ";
353 FROM civicrm_activity {$this->_aliases['civicrm_activity']}
355 LEFT JOIN civicrm_activity_target {$this->_aliases['civicrm_activity_target']}
356 ON {$this->_aliases['civicrm_activity']}.id = {$this->_aliases['civicrm_activity_target']}.activity_id
357 LEFT JOIN civicrm_activity_assignment {$this->_aliases['civicrm_activity_assignment']}
358 ON {$this->_aliases['civicrm_activity']}.id = {$this->_aliases['civicrm_activity_assignment']}.activity_id
359 LEFT JOIN civicrm_contact civicrm_contact_source
360 ON {$this->_aliases['civicrm_activity']}.source_contact_id = civicrm_contact_source.id
361 LEFT JOIN civicrm_contact contact_civireport
362 ON {$this->_aliases['civicrm_activity_target']}.target_contact_id = contact_civireport.id
363 LEFT JOIN civicrm_contact civicrm_contact_assignee
364 ON {$this->_aliases['civicrm_activity_assignment']}.assignee_contact_id = civicrm_contact_assignee.id
367 LEFT JOIN civicrm_option_value
368 ON ( {$this->_aliases['civicrm_activity']}.activity_type_id = civicrm_option_value.value )
369 LEFT JOIN civicrm_option_group
370 ON civicrm_option_group.id = civicrm_option_value.option_group_id
371 LEFT JOIN civicrm_case_activity case_activity_civireport
372 ON case_activity_civireport.activity_id = {$this->_aliases['civicrm_activity']}.id
373 LEFT JOIN civicrm_case
374 ON case_activity_civireport.case_id = civicrm_case.id
375 LEFT JOIN civicrm_case_contact
376 ON civicrm_case_contact.case_id = civicrm_case.id ";
378 if ($this->isTableSelected('civicrm_email')) {
380 LEFT JOIN civicrm_email civicrm_email_source
381 ON {$this->_aliases['civicrm_activity']}.source_contact_id = civicrm_email_source.contact_id AND
382 civicrm_email_source.is_primary = 1
384 LEFT JOIN civicrm_email civicrm_email_target
385 ON {$this->_aliases['civicrm_activity_target']}.target_contact_id = civicrm_email_target.contact_id AND
386 civicrm_email_target.is_primary = 1
388 LEFT JOIN civicrm_email civicrm_email_assignee
389 ON {$this->_aliases['civicrm_activity_assignment']}.assignee_contact_id = civicrm_email_assignee.contact_id AND
390 civicrm_email_assignee.is_primary = 1 ";
392 $this->addAddressFromClause();
396 $this->_where
= " WHERE civicrm_option_group.name = 'activity_type' AND
397 {$this->_aliases['civicrm_activity']}.is_test = 0 AND
398 {$this->_aliases['civicrm_activity']}.is_deleted = 0 AND
399 {$this->_aliases['civicrm_activity']}.is_current_revision = 1";
402 foreach ($this->_columns
as $tableName => $table) {
403 if (array_key_exists('filters', $table)) {
405 foreach ($table['filters'] as $fieldName => $field) {
407 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
408 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
409 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
410 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
412 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
415 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
417 $clause = $this->whereClause($field,
419 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
420 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
421 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
426 if ($field['name'] == 'current_user') {
427 if (CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
) == 1) {
429 $session = CRM_Core_Session
::singleton();
430 if ($contactID = $session->get('userID')) {
431 $clause = "( civicrm_contact_source.id = " . $contactID . " OR civicrm_contact_assignee.id = " . $contactID . " OR contact_civireport.id = " . $contactID . " )";
441 if (!empty($clause)) {
442 $clauses[] = $clause;
448 if (empty($clauses)) {
449 $this->_where
.= " ";
452 $this->_where
.= " AND " . implode(' AND ', $clauses);
455 if ($this->_aclWhere
) {
456 $this->_where
.= " AND {$this->_aclWhere} ";
461 $this->_groupBy
= "GROUP BY {$this->_aliases['civicrm_activity']}.id";
464 function buildACLClause($tableAlias = 'contact_a') {
465 //override for ACL( Since Cotact may be source
466 //contact/assignee or target also it may be null )
468 if (CRM_Core_Permission
::check('view all contacts')) {
469 $this->_aclFrom
= $this->_aclWhere
= NULL;
473 $session = CRM_Core_Session
::singleton();
474 $contactID = $session->get('userID');
478 $contactID = CRM_Utils_Type
::escape($contactID, 'Integer');
480 CRM_Contact_BAO_Contact_Permission
::cache($contactID);
482 foreach ($tableAlias as $k => $alias) {
483 $clauses[] = " INNER JOIN civicrm_acl_contact_cache aclContactCache_{$k} ON ( {$alias}.id = aclContactCache_{$k}.contact_id OR {$alias}.id IS NULL ) AND aclContactCache_{$k}.user_id = $contactID ";
486 $this->_aclFrom
= implode(" ", $clauses);
487 $this->_aclWhere
= NULL;
490 function postProcess() {
492 $this->buildACLClause(array('civicrm_contact_source', 'contact_civireport', 'civicrm_contact_assignee'));
493 parent
::postProcess();
496 function alterDisplay(&$rows) {
497 // custom code to alter rows
500 $activityType = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, FALSE, 'label', TRUE);
501 $activityStatus = CRM_Core_PseudoConstant
::activityStatus();
503 $seperator = CRM_CORE_DAO
::VALUE_SEPARATOR
;
504 $context = CRM_Utils_Request
::retrieve('context', 'String', $this, FALSE, 'report');
506 if (CRM_Core_Permission
::check('access CiviCRM')) {
508 $onHover = ts('View Contact Summary for this Contact');
509 $onHoverAct = ts('View Activity Record');
511 foreach ($rows as $rowNum => $row) {
513 if (array_key_exists('civicrm_contact_contact_source', $row)) {
514 if ($value = $row['civicrm_activity_source_contact_id']) {
516 $url = CRM_Utils_System
::url("civicrm/contact/view",
517 'reset=1&cid=' . $value,
520 $rows[$rowNum]['civicrm_contact_contact_source_link'] = $url;
521 $rows[$rowNum]['civicrm_contact_contact_source_hover'] = $onHover;
527 if (array_key_exists('civicrm_contact_contact_assignee', $row)) {
528 $assigneeNames = explode($seperator, $row['civicrm_contact_contact_assignee']);
529 if ($value = $row['civicrm_activity_assignment_assignee_contact_id']) {
530 $assigneeContactIds = explode($seperator, $value);
533 foreach ($assigneeContactIds as $id => $value) {
534 $url = CRM_Utils_System
::url("civicrm/contact/view",
535 'reset=1&cid=' . $value,
538 $link[] = "<a title='" . $onHover . "' href='" . $url . "'>{$assigneeNames[$id]}</a>";
540 $rows[$rowNum]['civicrm_contact_contact_assignee'] = implode('; ', $link);
546 if (array_key_exists('civicrm_contact_contact_target', $row)) {
547 $targetNames = explode($seperator, $row['civicrm_contact_contact_target']);
548 if ($value = $row['civicrm_activity_target_target_contact_id']) {
549 $targetContactIds = explode($seperator, $value);
552 foreach ($targetContactIds as $id => $value) {
553 $url = CRM_Utils_System
::url("civicrm/contact/view",
554 'reset=1&cid=' . $value,
557 $link[] = "<a title='" . $onHover . "' href='" . $url . "'>{$targetNames[$id]}</a>";
559 $rows[$rowNum]['civicrm_contact_contact_target'] = implode('; ', $link);
565 if (array_key_exists('civicrm_activity_activity_type_id', $row)) {
566 if ($value = $row['civicrm_activity_activity_type_id']) {
567 $rows[$rowNum]['civicrm_activity_activity_type_id'] = $activityType[$value];
569 // Check for target contact id(s) and use the first contact id in that list for view activity link if found,
570 // else use source contact id
571 if (!empty($rows[$rowNum]['civicrm_activity_target_target_contact_id'])) {
572 $targets = explode($seperator, $rows[$rowNum]['civicrm_activity_target_target_contact_id']);
576 $cid = $rows[$rowNum]['civicrm_activity_source_contact_id'];
579 $actionLinks = CRM_Activity_Selector_Activity
::actionLinks($row['civicrm_activity_activity_type_id'],
580 CRM_Utils_Array
::value('civicrm_activity_source_record_id', $rows[$rowNum]),
582 $rows[$rowNum]['civicrm_activity_id']
586 'id' => $rows[$rowNum]['civicrm_activity_id'],
590 $url = CRM_Utils_System
::url($actionLinks[CRM_Core_Action
::VIEW
]['url'],
591 CRM_Core_Action
::replace($actionLinks[CRM_Core_Action
::VIEW
]['qs'], $linkValues), TRUE
593 $rows[$rowNum]['civicrm_activity_activity_type_id_link'] = $url;
594 $rows[$rowNum]['civicrm_activity_activity_type_id_hover'] = $onHoverAct;
600 if (array_key_exists('civicrm_activity_status_id', $row)) {
601 if ($value = $row['civicrm_activity_status_id']) {
602 $rows[$rowNum]['civicrm_activity_status_id'] = $activityStatus[$value];
607 if (array_key_exists('civicrm_activity_campaign_id', $row)) {
608 if ($value = $row['civicrm_activity_campaign_id']) {
609 $rows[$rowNum]['civicrm_activity_campaign_id'] = $this->activeCampaigns
[$value];
614 if (array_key_exists('civicrm_activity_engagement_level', $row)) {
615 if ($value = $row['civicrm_activity_engagement_level']) {
616 $rows[$rowNum]['civicrm_activity_engagement_level'] = $this->engagementLevels
[$value];
621 if (array_key_exists('civicrm_activity_activity_date_time', $row) && array_key_exists('civicrm_activity_status_id', $row)) {
622 if (CRM_Utils_Date
::overdue($rows[$rowNum]['civicrm_activity_activity_date_time']) &&
623 $activityStatus[$row['civicrm_activity_status_id']] != 'Completed'
625 $rows[$rowNum]['class'] = "status-overdue";
630 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'activity', 'List all activities for this ') ?
TRUE : $entryFound;
640 * Add Target Contact Address into From Table if required
642 function addAddressFromClause() {
643 // include address field if address column is to be included
644 if ((isset($this->_addressField
) &&
647 $this->isTableSelected('civicrm_address')
650 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
651 ON ({$this->_aliases['civicrm_activity_target']}.target_contact_id =
652 {$this->_aliases['civicrm_address']}.contact_id) AND
653 {$this->_aliases['civicrm_address']}.is_primary = 1\n";