CRM-21754: Duplicate rows in Activity Details report when address fields are displayed
[civicrm-core.git] / CRM / Report / Form / Activity.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
0f03f337 31 * @copyright CiviCRM LLC (c) 2004-2017
6a488035
TO
32 */
33class CRM_Report_Form_Activity extends CRM_Report_Form {
1f220d30 34 protected $_selectAliasesTotal = array();
6a488035
TO
35
36 protected $_customGroupExtends = array(
21dfd5f5 37 'Activity',
430ae6dd
TO
38 );
39
09febbae 40 protected $_nonDisplayFields = array();
41
1728e9a0 42 /**
43 * This report has not been optimised for group filtering.
44 *
45 * The functionality for group filtering has been improved but not
46 * all reports have been adjusted to take care of it. This report has not
47 * and will run an inefficient query until fixed.
48 *
49 * CRM-19170
50 *
51 * @var bool
52 */
53 protected $groupFilterNotOptimised = TRUE;
54
74cf4551 55 /**
ab432335 56 * Class constructor.
74cf4551 57 */
00be9182 58 public function __construct() {
7116d719 59 // There could be multiple contacts. We not clear on which contact id to display.
83e74860
DS
60 // Lets hide it for now.
61 $this->_exposeContactID = FALSE;
8f621a01 62 // if navigated from count link of activity summary reports.
a3d827a7 63 $this->_resetDateFilter = CRM_Utils_Request::retrieve('resetDateFilter', 'Boolean');
83e74860 64
6a488035
TO
65 $config = CRM_Core_Config::singleton();
66 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents);
d6c3d40f 67 $caseEnabled = in_array("CiviCase", $config->enableComponents);
6a488035
TO
68 if ($campaignEnabled) {
69 $getCampaigns = CRM_Campaign_BAO_Campaign::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
70 $this->activeCampaigns = $getCampaigns['campaigns'];
71 asort($this->activeCampaigns);
72 $this->engagementLevels = CRM_Campaign_PseudoConstant::engagementLevel();
73 }
6a488035 74
1023b3bc 75 $components = CRM_Core_Component::getEnabledComponents();
76 foreach ($components as $componentName => $componentInfo) {
d3e6c71f 77 // CRM-19201: Add support for reporting CiviCampaign activities
44d17ec8
BS
78 // For CiviCase, "access all cases and activities" is required here
79 // rather than "access my cases and activities" to prevent those with
80 // only the later permission from seeing a list of all cases which might
81 // present a privacy issue.
82 if (CRM_Core_Permission::access($componentName, TRUE, TRUE)) {
1023b3bc 83 $accessAllowed[] = $componentInfo->componentID;
84 }
85 }
86
87 $include = '';
88 if (!empty($accessAllowed)) {
89 $include = 'OR v.component_id IN (' . implode(', ', $accessAllowed) . ')';
90 }
91 $condition = " AND ( v.component_id IS NULL {$include} )";
92 $this->activityTypes = CRM_Core_OptionGroup::values('activity_type', FALSE, FALSE, FALSE, $condition);
93 asort($this->activityTypes);
6a488035 94 $this->_columns = array(
af9b09df
TO
95 'civicrm_contact' => array(
96 'dao' => 'CRM_Contact_DAO_Contact',
97 'fields' => array(
98 'contact_source' => array(
99 'name' => 'sort_name',
8c0ff2a6 100 'title' => ts('Source Name'),
af9b09df
TO
101 'alias' => 'civicrm_contact_source',
102 'no_repeat' => TRUE,
6a488035 103 ),
af9b09df
TO
104 'contact_assignee' => array(
105 'name' => 'sort_name',
8c0ff2a6 106 'title' => ts('Assignee Name'),
af9b09df
TO
107 'alias' => 'civicrm_contact_assignee',
108 'dbAlias' => "civicrm_contact_assignee.sort_name",
109 'default' => TRUE,
110 ),
111 'contact_target' => array(
112 'name' => 'sort_name',
8c0ff2a6 113 'title' => ts('Target Name'),
af9b09df
TO
114 'alias' => 'civicrm_contact_target',
115 'dbAlias' => "civicrm_contact_target.sort_name",
116 'default' => TRUE,
117 ),
118 'contact_source_id' => array(
119 'name' => 'id',
120 'alias' => 'civicrm_contact_source',
121 'dbAlias' => "civicrm_contact_source.id",
122 'no_display' => TRUE,
123 'default' => TRUE,
124 'required' => TRUE,
125 ),
126 'contact_assignee_id' => array(
127 'name' => 'id',
128 'alias' => 'civicrm_contact_assignee',
129 'dbAlias' => "civicrm_contact_assignee.id",
130 'no_display' => TRUE,
131 'default' => TRUE,
132 'required' => TRUE,
133 ),
134 'contact_target_id' => array(
135 'name' => 'id',
136 'alias' => 'civicrm_contact_target',
137 'dbAlias' => "civicrm_contact_target.id",
138 'no_display' => TRUE,
139 'default' => TRUE,
140 'required' => TRUE,
482691e1 141 ),
6a488035 142 ),
af9b09df
TO
143 'filters' => array(
144 'contact_source' => array(
145 'name' => 'sort_name',
146 'alias' => 'civicrm_contact_source',
8c0ff2a6 147 'title' => ts('Source Name'),
af9b09df
TO
148 'operator' => 'like',
149 'type' => CRM_Report_Form::OP_STRING,
6a488035 150 ),
af9b09df
TO
151 'contact_assignee' => array(
152 'name' => 'sort_name',
153 'alias' => 'civicrm_contact_assignee',
8c0ff2a6 154 'title' => ts('Assignee Name'),
af9b09df
TO
155 'operator' => 'like',
156 'type' => CRM_Report_Form::OP_STRING,
157 ),
158 'contact_target' => array(
159 'name' => 'sort_name',
160 'alias' => 'civicrm_contact_target',
8c0ff2a6 161 'title' => ts('Target Name'),
af9b09df
TO
162 'operator' => 'like',
163 'type' => CRM_Report_Form::OP_STRING,
164 ),
165 'current_user' => array(
166 'name' => 'current_user',
167 'title' => ts('Limit To Current User'),
168 'type' => CRM_Utils_Type::T_INT,
169 'operatorType' => CRM_Report_Form::OP_SELECT,
170 'options' => array('0' => ts('No'), '1' => ts('Yes')),
6a488035
TO
171 ),
172 ),
af9b09df
TO
173 'grouping' => 'contact-fields',
174 ),
175 'civicrm_email' => array(
176 'dao' => 'CRM_Core_DAO_Email',
177 'fields' => array(
178 'contact_source_email' => array(
179 'name' => 'email',
8c0ff2a6 180 'title' => ts('Source Email'),
af9b09df 181 'alias' => 'civicrm_email_source',
6a488035 182 ),
af9b09df
TO
183 'contact_assignee_email' => array(
184 'name' => 'email',
8c0ff2a6 185 'title' => ts('Assignee Email'),
af9b09df 186 'alias' => 'civicrm_email_assignee',
6a488035 187 ),
af9b09df
TO
188 'contact_target_email' => array(
189 'name' => 'email',
8c0ff2a6 190 'title' => ts('Target Email'),
af9b09df 191 'alias' => 'civicrm_email_target',
6a488035
TO
192 ),
193 ),
af9b09df
TO
194 'order_bys' => array(
195 'source_contact_email' => array(
196 'name' => 'email',
8c0ff2a6 197 'title' => ts('Source Email'),
af9b09df 198 'dbAlias' => 'civicrm_email_contact_source_email',
6a488035
TO
199 ),
200 ),
af9b09df 201 ),
8c0ff2a6 202 'civicrm_phone' => array(
203 'dao' => 'CRM_Core_DAO_Phone',
204 'fields' => array(
205 'contact_source_phone' => array(
206 'name' => 'phone',
207 'title' => ts('Source Phone'),
208 'alias' => 'civicrm_phone_source',
209 ),
210 'contact_assignee_phone' => array(
211 'name' => 'phone',
212 'title' => ts('Assignee Phone'),
213 'alias' => 'civicrm_phone_assignee',
214 ),
215 'contact_target_phone' => array(
216 'name' => 'phone',
2afe8e0c 217 'title' => ts('Target Phone'),
8c0ff2a6 218 'alias' => 'civicrm_phone_target',
219 ),
220 ),
221 ),
af9b09df
TO
222 'civicrm_activity' => array(
223 'dao' => 'CRM_Activity_DAO_Activity',
224 'fields' => array(
225 'id' => array(
226 'no_display' => TRUE,
227 'title' => ts('Activity ID'),
228 'required' => TRUE,
229 ),
230 'source_record_id' => array(
231 'no_display' => TRUE,
232 'required' => TRUE,
233 ),
234 'activity_type_id' => array(
235 'title' => ts('Activity Type'),
236 'required' => TRUE,
237 'type' => CRM_Utils_Type::T_STRING,
238 ),
239 'activity_subject' => array(
240 'title' => ts('Subject'),
241 'default' => TRUE,
242 ),
243 'activity_date_time' => array(
244 'title' => ts('Activity Date'),
245 'required' => TRUE,
246 ),
247 'status_id' => array(
248 'title' => ts('Activity Status'),
249 'default' => TRUE,
250 'type' => CRM_Utils_Type::T_STRING,
251 ),
252 'duration' => array(
253 'title' => ts('Duration'),
254 'type' => CRM_Utils_Type::T_INT,
667abe3a 255 ),
c02f870b
JM
256 'location' => array(
257 'title' => ts('Location'),
258 'type' => CRM_Utils_Type::T_STRING,
259 ),
1efec7ff
DG
260 'details' => array(
261 'title' => ts('Activity Details'),
af9b09df 262 ),
da236f9a 263 'priority_id' => array(
264 'title' => ts('Priority'),
265 'default' => TRUE,
266 'type' => CRM_Utils_Type::T_STRING,
267 ),
6a488035 268 ),
1efec7ff
DG
269 'filters' => array(
270 'activity_date_time' => array(
1a6c8513 271 'default' => 'this.month',
6a488035
TO
272 'operatorType' => CRM_Report_Form::OP_DATE,
273 ),
af9b09df
TO
274 'activity_subject' => array('title' => ts('Activity Subject')),
275 'activity_type_id' => array(
276 'title' => ts('Activity Type'),
277 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
278 'options' => $this->activityTypes,
279 ),
280 'status_id' => array(
281 'title' => ts('Activity Status'),
8ee006e7 282 'type' => CRM_Utils_Type::T_STRING,
af9b09df
TO
283 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
284 'options' => CRM_Core_PseudoConstant::activityStatus(),
285 ),
c02f870b
JM
286 'location' => array(
287 'title' => ts('Location'),
288 'type' => CRM_Utils_Type::T_TEXT,
289 ),
1efec7ff
DG
290 'details' => array(
291 'title' => ts('Activity Details'),
292 'type' => CRM_Utils_Type::T_TEXT,
af9b09df 293 ),
71c30391 294 'priority_id' => array(
295 'title' => ts('Activity Priority'),
410e8105 296 'type' => CRM_Utils_Type::T_STRING,
71c30391 297 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
298 'options' => CRM_Core_PseudoConstant::get('CRM_Activity_DAO_Activity', 'priority_id'),
299 ),
af9b09df
TO
300 ),
301 'order_bys' => array(
302 'activity_date_time' => array(
303 'title' => ts('Activity Date'),
304 'default_weight' => '1',
305 'dbAlias' => 'civicrm_activity_activity_date_time',
306 ),
307 'activity_type_id' => array(
308 'title' => ts('Activity Type'),
309 'default_weight' => '2',
56bbb442 310 'dbAlias' => 'field(civicrm_activity_activity_type_id, ' . implode(', ', array_keys($this->activityTypes)) . ')',
af9b09df
TO
311 ),
312 ),
313 'grouping' => 'activity-fields',
314 'alias' => 'activity',
315 ),
56bbb442 316 // Hack to get $this->_alias populated for the table.
af9b09df
TO
317 'civicrm_activity_contact' => array(
318 'dao' => 'CRM_Activity_DAO_ActivityContact',
a0375fa3 319 'fields' => array(),
af9b09df
TO
320 ),
321 ) + $this->addressFields(TRUE);
6a488035 322
d6c3d40f 323 if ($caseEnabled && CRM_Core_Permission::check('access all cases and activities')) {
d6c3d40f 324 $this->_columns['civicrm_activity']['filters']['include_case_activities'] = array(
325 'name' => 'include_case_activities',
326 'title' => ts('Include Case Activities'),
327 'type' => CRM_Utils_Type::T_INT,
328 'operatorType' => CRM_Report_Form::OP_SELECT,
329 'options' => array('0' => ts('No'), '1' => ts('Yes')),
330 );
331 }
332
6a488035
TO
333 if ($campaignEnabled) {
334 // Add display column and filter for Survey Results, Campaign and Engagement Index if CiviCampaign is enabled
335
336 $this->_columns['civicrm_activity']['fields']['result'] = array(
fd6a6828 337 'title' => ts('Survey Result'),
6a488035
TO
338 'default' => 'false',
339 );
9d72cede
EM
340 $this->_columns['civicrm_activity']['filters']['result'] = array(
341 'title' => ts('Survey Result'),
6a488035
TO
342 'operator' => 'like',
343 'type' => CRM_Utils_Type::T_STRING,
344 );
345 if (!empty($this->activeCampaigns)) {
346 $this->_columns['civicrm_activity']['fields']['campaign_id'] = array(
fd6a6828 347 'title' => ts('Campaign'),
6a488035
TO
348 'default' => 'false',
349 );
9d72cede
EM
350 $this->_columns['civicrm_activity']['filters']['campaign_id'] = array(
351 'title' => ts('Campaign'),
09b5cdcf 352 'type' => CRM_Utils_Type::T_INT,
6a488035
TO
353 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
354 'options' => $this->activeCampaigns,
355 );
356 }
357 if (!empty($this->engagementLevels)) {
358 $this->_columns['civicrm_activity']['fields']['engagement_level'] = array(
fd6a6828 359 'title' => ts('Engagement Index'),
6a488035
TO
360 'default' => 'false',
361 );
9d72cede
EM
362 $this->_columns['civicrm_activity']['filters']['engagement_level'] = array(
363 'title' => ts('Engagement Index'),
09b5cdcf 364 'type' => CRM_Utils_Type::T_INT,
6a488035
TO
365 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
366 'options' => $this->engagementLevels,
367 );
368 }
369 }
370 $this->_groupFilter = TRUE;
371 $this->_tagFilter = TRUE;
ed795723 372 $this->_tagFilterTable = 'civicrm_activity';
6a488035
TO
373 parent::__construct();
374 }
375
be2e0c6a
TO
376 /**
377 * Adding address fields with dbAlias for order clause.
ab432335
EM
378 *
379 * @param bool $orderBy
380 *
a6c01b45 381 * @return array
ab432335 382 * Address fields
667abe3a 383 */
00be9182 384 public function addressFields($orderBy = FALSE) {
667abe3a 385 $address = parent::addAddressFields(FALSE, TRUE);
386 if ($orderBy) {
387 foreach ($address['civicrm_address']['order_bys'] as $fieldName => $field) {
388 $address['civicrm_address']['order_bys'][$fieldName]['dbAlias'] = "civicrm_address_{$fieldName}";
389 }
390 }
391 return $address;
392 }
393
74cf4551 394 /**
a0375fa3 395 * Build select clause.
396 *
74cf4551
EM
397 * @param null $recordType
398 */
00be9182 399 public function select($recordType = NULL) {
9d72cede
EM
400 if (!array_key_exists("contact_{$recordType}", $this->_params['fields']) &&
401 $recordType != 'final'
402 ) {
09febbae 403 $this->_nonDisplayFields[] = "civicrm_contact_contact_{$recordType}";
09febbae 404 }
1f220d30 405 parent::select();
1f220d30 406
09febbae 407 if ($recordType == 'final' && !empty($this->_nonDisplayFields)) {
408 foreach ($this->_nonDisplayFields as $fieldName) {
409 unset($this->_columnHeaders[$fieldName]);
410 }
411 }
412
1f220d30
DS
413 if (empty($this->_selectAliasesTotal)) {
414 $this->_selectAliasesTotal = $this->_selectAliases;
415 }
416
417 $removeKeys = array();
418 if ($recordType == 'target') {
419 foreach ($this->_selectClauses as $key => $clause) {
7116d719 420 if (strstr($clause, 'civicrm_contact_assignee.') ||
1f220d30
DS
421 strstr($clause, 'civicrm_contact_source.') ||
422 strstr($clause, 'civicrm_email_assignee.') ||
8c0ff2a6 423 strstr($clause, 'civicrm_email_source.') ||
424 strstr($clause, 'civicrm_phone_assignee.') ||
425 strstr($clause, 'civicrm_phone_source.')
1f220d30
DS
426 ) {
427 $removeKeys[] = $key;
428 unset($this->_selectClauses[$key]);
429 }
430 }
9d72cede 431 }
4c9b6178 432 elseif ($recordType == 'assignee') {
1f220d30 433 foreach ($this->_selectClauses as $key => $clause) {
7116d719 434 if (strstr($clause, 'civicrm_contact_target.') ||
1f220d30
DS
435 strstr($clause, 'civicrm_contact_source.') ||
436 strstr($clause, 'civicrm_email_target.') ||
8c0ff2a6 437 strstr($clause, 'civicrm_email_source.') ||
438 strstr($clause, 'civicrm_phone_target.') ||
439 strstr($clause, 'civicrm_phone_source.')
1f220d30
DS
440 ) {
441 $removeKeys[] = $key;
442 unset($this->_selectClauses[$key]);
443 }
444 }
9d72cede 445 }
4c9b6178 446 elseif ($recordType == 'source') {
1f220d30 447 foreach ($this->_selectClauses as $key => $clause) {
7116d719 448 if (strstr($clause, 'civicrm_contact_target.') ||
1f220d30
DS
449 strstr($clause, 'civicrm_contact_assignee.') ||
450 strstr($clause, 'civicrm_email_target.') ||
8c0ff2a6 451 strstr($clause, 'civicrm_email_assignee.') ||
452 strstr($clause, 'civicrm_phone_target.') ||
453 strstr($clause, 'civicrm_phone_assignee.')
1f220d30
DS
454 ) {
455 $removeKeys[] = $key;
456 unset($this->_selectClauses[$key]);
457 }
458 }
9d72cede 459 }
4c9b6178 460 elseif ($recordType == 'final') {
1f220d30
DS
461 $this->_selectClauses = $this->_selectAliasesTotal;
462 foreach ($this->_selectClauses as $key => $clause) {
7116d719 463 if (strstr($clause, 'civicrm_contact_contact_target') ||
1f220d30 464 strstr($clause, 'civicrm_contact_contact_assignee') ||
8c0ff2a6 465 strstr($clause, 'civicrm_contact_contact_source') ||
466 strstr($clause, 'civicrm_phone_contact_source_phone') ||
467 strstr($clause, 'civicrm_phone_contact_assignee_phone') ||
468 strstr($clause, 'civicrm_email_contact_source_email') ||
469 strstr($clause, 'civicrm_email_contact_assignee_email') ||
470 strstr($clause, 'civicrm_email_contact_target_email') ||
66ad82d6 471 strstr($clause, 'civicrm_phone_contact_target_phone') ||
472 strstr($clause, 'civicrm_address_')
9d72cede 473 ) {
66ad82d6 474 $this->_selectClauses[$key] = "GROUP_CONCAT(DISTINCT $clause SEPARATOR ';') as $clause";
6a488035
TO
475 }
476 }
477 }
478
1f220d30
DS
479 if ($recordType) {
480 foreach ($removeKeys as $key) {
481 unset($this->_selectAliases[$key]);
482 }
483
667abe3a 484 if ($recordType != 'final') {
485 foreach ($this->_columns['civicrm_address']['order_bys'] as $fieldName => $field) {
486 $orderByFld = $this->_columns['civicrm_address']['order_bys'][$fieldName];
487 $fldInfo = $this->_columns['civicrm_address']['fields'][$fieldName];
488 $this->_selectAliases[] = $orderByFld['dbAlias'];
489 $this->_selectClauses[] = "{$fldInfo['dbAlias']} as {$orderByFld['dbAlias']}";
490 }
667abe3a 491 $this->_selectAliases = array_unique($this->_selectAliases);
492 $this->_selectClauses = array_unique($this->_selectClauses);
493 }
1f220d30 494 $this->_select = "SELECT " . implode(', ', $this->_selectClauses) . " ";
1f220d30 495 }
6a488035
TO
496 }
497
74cf4551 498 /**
a0375fa3 499 * Build from clause.
500 *
501 * @param string $recordType
74cf4551 502 */
00be9182 503 public function from($recordType) {
44f817d4 504 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
667abe3a 505 $activityTypeId = CRM_Core_DAO::getFieldValue("CRM_Core_DAO_OptionGroup", 'activity_type', 'id', 'name');
9e74e3ce 506 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
9d72cede
EM
507 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
508 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
6a488035 509
2f4b4d54
DS
510 if ($recordType == 'target') {
511 $this->_from = "
512 FROM civicrm_activity {$this->_aliases['civicrm_activity']}
1f220d30
DS
513 INNER JOIN civicrm_activity_contact {$this->_aliases['civicrm_activity_contact']}
514 ON {$this->_aliases['civicrm_activity']}.id = {$this->_aliases['civicrm_activity_contact']}.activity_id AND
515 {$this->_aliases['civicrm_activity_contact']}.record_type_id = {$targetID}
2f4b4d54 516 INNER JOIN civicrm_contact civicrm_contact_target
1f220d30 517 ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_contact_target.id
2f4b4d54
DS
518 {$this->_aclFrom}";
519
520 if ($this->isTableSelected('civicrm_email')) {
521 $this->_from .= "
1f220d30
DS
522 LEFT JOIN civicrm_email civicrm_email_target
523 ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_email_target.contact_id AND
524 civicrm_email_target.is_primary = 1";
2f4b4d54 525 }
8c0ff2a6 526
527 if ($this->isTableSelected('civicrm_phone')) {
528 $this->_from .= "
529 LEFT JOIN civicrm_phone civicrm_phone_target
530 ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_phone_target.contact_id AND
531 civicrm_phone_target.is_primary = 1 ";
532 }
1f220d30 533 $this->_aliases['civicrm_contact'] = 'civicrm_contact_target';
2f4b4d54
DS
534 }
535
536 if ($recordType == 'assignee') {
537 $this->_from = "
538 FROM civicrm_activity {$this->_aliases['civicrm_activity']}
1f220d30
DS
539 INNER JOIN civicrm_activity_contact {$this->_aliases['civicrm_activity_contact']}
540 ON {$this->_aliases['civicrm_activity']}.id = {$this->_aliases['civicrm_activity_contact']}.activity_id AND
541 {$this->_aliases['civicrm_activity_contact']}.record_type_id = {$assigneeID}
2f4b4d54 542 INNER JOIN civicrm_contact civicrm_contact_assignee
1f220d30 543 ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_contact_assignee.id
2f4b4d54
DS
544 {$this->_aclFrom}";
545
546 if ($this->isTableSelected('civicrm_email')) {
547 $this->_from .= "
1f220d30
DS
548 LEFT JOIN civicrm_email civicrm_email_assignee
549 ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_email_assignee.contact_id AND
550 civicrm_email_assignee.is_primary = 1";
2f4b4d54 551 }
8c0ff2a6 552 if ($this->isTableSelected('civicrm_phone')) {
553 $this->_from .= "
554 LEFT JOIN civicrm_phone civicrm_phone_assignee
555 ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_phone_assignee.contact_id AND
556 civicrm_phone_assignee.is_primary = 1 ";
557 }
482691e1 558 $this->_aliases['civicrm_contact'] = 'civicrm_contact_assignee';
2f4b4d54
DS
559 }
560
561 if ($recordType == 'source') {
562 $this->_from = "
563 FROM civicrm_activity {$this->_aliases['civicrm_activity']}
1f220d30
DS
564 INNER JOIN civicrm_activity_contact {$this->_aliases['civicrm_activity_contact']}
565 ON {$this->_aliases['civicrm_activity']}.id = {$this->_aliases['civicrm_activity_contact']}.activity_id AND
566 {$this->_aliases['civicrm_activity_contact']}.record_type_id = {$sourceID}
2f4b4d54 567 INNER JOIN civicrm_contact civicrm_contact_source
1f220d30 568 ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_contact_source.id
2f4b4d54
DS
569 {$this->_aclFrom}";
570
571 if ($this->isTableSelected('civicrm_email')) {
572 $this->_from .= "
1f220d30
DS
573 LEFT JOIN civicrm_email civicrm_email_source
574 ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_email_source.contact_id AND
575 civicrm_email_source.is_primary = 1";
2f4b4d54 576 }
8c0ff2a6 577 if ($this->isTableSelected('civicrm_phone')) {
578 $this->_from .= "
579 LEFT JOIN civicrm_phone civicrm_phone_source
580 ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_phone_source.contact_id AND
581 civicrm_phone_source.is_primary = 1 ";
582 }
482691e1 583 $this->_aliases['civicrm_contact'] = 'civicrm_contact_source';
6a488035 584 }
56bbb442 585
482691e1 586 $this->addAddressFromClause();
6a488035
TO
587 }
588
74cf4551 589 /**
a0375fa3 590 * Build where clause.
591 *
592 * @param string $recordType
74cf4551 593 */
00be9182 594 public function where($recordType = NULL) {
2f4b4d54 595 $this->_where = " WHERE {$this->_aliases['civicrm_activity']}.is_test = 0 AND
6a488035
TO
596 {$this->_aliases['civicrm_activity']}.is_deleted = 0 AND
597 {$this->_aliases['civicrm_activity']}.is_current_revision = 1";
598
599 $clauses = array();
600 foreach ($this->_columns as $tableName => $table) {
601 if (array_key_exists('filters', $table)) {
602
603 foreach ($table['filters'] as $fieldName => $field) {
604 $clause = NULL;
7116d719 605 if ($fieldName != 'contact_' . $recordType &&
606 (strstr($fieldName, '_target') ||
607 strstr($fieldName, '_assignee') ||
608 strstr($fieldName, '_source')
609 )
610 ) {
7116d719 611 continue;
612 }
6a488035
TO
613 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
614 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
9d72cede
EM
615 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
616 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
6a488035
TO
617
618 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
619 }
620 else {
621 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
024311d5 622 if ($op && ($op != 'nnll' && $op != 'nll')) {
6a488035
TO
623 $clause = $this->whereClause($field,
624 $op,
625 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
626 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
627 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
628 );
d6c3d40f 629 if ($field['name'] == 'include_case_activities') {
630 $clause = NULL;
631 }
9d72cede
EM
632 if ($fieldName == 'activity_type_id' &&
633 empty($this->_params['activity_type_id_value'])
634 ) {
355b8a20 635 if (empty($this->_params['include_case_activities_value'])) {
636 $this->activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, FALSE, FALSE, 'label', TRUE);
637 }
1023b3bc 638 $actTypes = array_flip($this->activityTypes);
6c552737 639 $clause = "( {$this->_aliases['civicrm_activity']}.activity_type_id IN (" .
9d72cede 640 implode(',', $actTypes) . ") )";
42b3be07 641 }
6a488035
TO
642 }
643 }
644
645 if ($field['name'] == 'current_user') {
9d72cede
EM
646 if (CRM_Utils_Array::value("{$fieldName}_value", $this->_params) ==
647 1
648 ) {
6a488035
TO
649 // get current user
650 $session = CRM_Core_Session::singleton();
651 if ($contactID = $session->get('userID')) {
389cfee7 652 $clause = "{$this->_aliases['civicrm_activity_contact']}.activity_id IN
653 (SELECT activity_id FROM civicrm_activity_contact WHERE contact_id = {$contactID})";
6a488035
TO
654 }
655 else {
656 $clause = NULL;
657 }
658 }
659 else {
660 $clause = NULL;
661 }
662 }
663 if (!empty($clause)) {
664 $clauses[] = $clause;
665 }
666 }
667 }
668 }
669
670 if (empty($clauses)) {
b6cab294 671 $this->_where .= " ";
6a488035
TO
672 }
673 else {
b6cab294 674 $this->_where .= " AND " . implode(' AND ', $clauses);
6a488035
TO
675 }
676
677 if ($this->_aclWhere) {
678 $this->_where .= " AND {$this->_aclWhere} ";
679 }
680 }
681
a0375fa3 682 /**
683 * Override group by function.
684 */
00be9182 685 public function groupBy() {
b708c08d 686 $this->_groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, "{$this->_aliases['civicrm_activity']}.id");
6a488035
TO
687 }
688
74cf4551 689 /**
a0375fa3 690 * Build ACL clause.
691 *
74cf4551
EM
692 * @param string $tableAlias
693 */
00be9182 694 public function buildACLClause($tableAlias = 'contact_a') {
2a3c101f 695 //override for ACL( Since Contact may be source
6a488035
TO
696 //contact/assignee or target also it may be null )
697
698 if (CRM_Core_Permission::check('view all contacts')) {
699 $this->_aclFrom = $this->_aclWhere = NULL;
700 return;
701 }
702
703 $session = CRM_Core_Session::singleton();
704 $contactID = $session->get('userID');
705 if (!$contactID) {
706 $contactID = 0;
707 }
708 $contactID = CRM_Utils_Type::escape($contactID, 'Integer');
709
710 CRM_Contact_BAO_Contact_Permission::cache($contactID);
711 $clauses = array();
712 foreach ($tableAlias as $k => $alias) {
713 $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 ";
714 }
715
716 $this->_aclFrom = implode(" ", $clauses);
717 $this->_aclWhere = NULL;
718 }
719
74cf4551 720 /**
100fef9d 721 * @param int $groupID
74cf4551
EM
722 *
723 * @throws Exception
724 */
00be9182 725 public function add2group($groupID) {
9a2f601e 726 if (CRM_Utils_Array::value("contact_target_op", $this->_params) == 'nll') {
727 CRM_Core_Error::fatal(ts('Current filter criteria didn\'t have any target contact to add to group'));
728 }
729
7f6464a0
JM
730 $new_select = 'AS addtogroup_contact_id';
731 $select = str_ireplace('AS civicrm_contact_contact_target_id', $new_select, $this->_select);
732 $new_having = ' addtogroup_contact_id';
733 $having = str_ireplace(' civicrm_contact_contact_target_id', $new_having, $this->_having);
734 $query = "$select
9a2f601e 735FROM civireport_activity_temp_target tar
7f6464a0 736GROUP BY civicrm_activity_id $having {$this->_orderBy}";
9a2f601e 737 $select = 'AS addtogroup_contact_id';
738 $query = str_ireplace('AS civicrm_contact_contact_target_id', $select, $query);
f38e3c19 739 $dao = $this->executeReportQuery($query);
9a2f601e 740
741 $contactIDs = array();
742 // Add resulting contacts to group
743 while ($dao->fetch()) {
744 if ($dao->addtogroup_contact_id) {
745 $contact_id = explode(';', $dao->addtogroup_contact_id);
746 if ($contact_id[0]) {
7bb16323 747 $contactIDs[$contact_id[0]] = $contact_id[0];
9a2f601e 748 }
749 }
750 }
751
9d72cede 752 if (!empty($contactIDs)) {
9a2f601e 753 CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIDs, $groupID);
754 CRM_Core_Session::setStatus(ts("Listed contact(s) have been added to the selected group."), ts('Contacts Added'), 'success');
755 }
756 else {
757 CRM_Core_Session::setStatus(ts("The listed records(s) cannot be added to the group."));
758 }
759 }
760
d6c3d40f 761 /**
762 * @param $fields
763 * @param $files
764 * @param $self
765 *
766 * @return array
767 */
768 public static function formRule($fields, $files, $self) {
769 $errors = array();
355b8a20 770 $config = CRM_Core_Config::singleton();
771 if (in_array("CiviCase", $config->enableComponents)) {
772 $componentId = CRM_Core_Component::getComponentID('CiviCase');
773 $caseActivityTypes = CRM_Core_OptionGroup::values('activity_type', TRUE, FALSE, FALSE, " AND v.component_id={$componentId}");
774 if (!empty($fields['activity_type_id_value']) && is_array($fields['activity_type_id_value']) && empty($fields['include_case_activities_value'])) {
775 foreach ($fields['activity_type_id_value'] as $activityTypeId) {
776 if (in_array($activityTypeId, $caseActivityTypes)) {
777 $errors['fields'] = ts("Please enable 'Include Case Activities' to filter with Case Activity types.");
778 }
d6c3d40f 779 }
780 }
781 }
782 return $errors;
783 }
784
a1bf1058
ML
785 /**
786 * @param $applyLimit
787 *
788 * @return string
789 */
790 public function buildQuery($applyLimit = TRUE) {
44f817d4 791 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
52272965 792 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
2f4b4d54 793
389cfee7 794 //Assign those recordtype to array which have filter operator as 'Is not empty' or 'Is empty'
795 $nullFilters = array();
b6cab294 796 foreach (array('target', 'source', 'assignee') as $type) {
9d72cede
EM
797 if (CRM_Utils_Array::value("contact_{$type}_op", $this->_params) ==
798 'nnll' || !empty($this->_params["contact_{$type}_value"])
799 ) {
3ae89fd7 800 $nullFilters[] = " civicrm_contact_contact_{$type}_id IS NOT NULL ";
389cfee7 801 }
4c9b6178 802 elseif (CRM_Utils_Array::value("contact_{$type}_op", $this->_params) ==
9d72cede
EM
803 'nll'
804 ) {
3ae89fd7 805 $nullFilters[] = " civicrm_contact_contact_{$type}_id IS NULL ";
b6cab294 806 }
807 }
808
9d72cede 809 // 1. fill temp table with target results
6e5e50a9 810 $this->buildACLClause(array('civicrm_contact_target'));
1f220d30 811 $this->select('target');
2f4b4d54 812 $this->from('target');
5b1ba2b8 813 $this->customDataFrom();
389cfee7 814 $this->where('target');
1f220d30 815 $insertCols = implode(',', $this->_selectAliases);
d09afb3e 816 $tempQuery = "CREATE TEMPORARY TABLE civireport_activity_temp_target {$this->_databaseAttributes} AS
482691e1 817{$this->_select} {$this->_from} {$this->_where} ";
f38e3c19 818 $this->executeReportQuery($tempQuery);
2f4b4d54 819
1f220d30
DS
820 // 2. add new columns to hold assignee and source results
821 // fixme: add when required
822 $tempQuery = "
823 ALTER TABLE civireport_activity_temp_target
e7b3275b 824 MODIFY COLUMN civicrm_contact_contact_target_id VARCHAR(128),
1f220d30
DS
825 ADD COLUMN civicrm_contact_contact_assignee VARCHAR(128),
826 ADD COLUMN civicrm_contact_contact_source VARCHAR(128),
7116d719 827 ADD COLUMN civicrm_contact_contact_assignee_id VARCHAR(128),
482691e1 828 ADD COLUMN civicrm_contact_contact_source_id VARCHAR(128),
8c0ff2a6 829 ADD COLUMN civicrm_phone_contact_assignee_phone VARCHAR(128),
830 ADD COLUMN civicrm_phone_contact_source_phone VARCHAR(128),
1f220d30
DS
831 ADD COLUMN civicrm_email_contact_assignee_email VARCHAR(128),
832 ADD COLUMN civicrm_email_contact_source_email VARCHAR(128)";
f38e3c19 833 $this->executeReportQuery($tempQuery);
1f220d30
DS
834
835 // 3. fill temp table with assignee results
6e5e50a9 836 $this->buildACLClause(array('civicrm_contact_assignee'));
389cfee7 837 $this->select('assignee');
838 $this->from('assignee');
839 $this->customDataFrom();
840 $this->where('assignee');
841 $insertCols = implode(',', $this->_selectAliases);
9d72cede 842 $tempQuery = "INSERT INTO civireport_activity_temp_target ({$insertCols})
1f220d30 843{$this->_select}
83e74860 844{$this->_from} {$this->_where}";
f38e3c19 845 $this->executeReportQuery($tempQuery);
2f4b4d54 846
1f220d30 847 // 4. fill temp table with source results
6e5e50a9 848 $this->buildACLClause(array('civicrm_contact_source'));
389cfee7 849 $this->select('source');
850 $this->from('source');
851 $this->customDataFrom();
852 $this->where('source');
853 $insertCols = implode(',', $this->_selectAliases);
9d72cede 854 $tempQuery = "INSERT INTO civireport_activity_temp_target ({$insertCols})
1f220d30 855{$this->_select}
83e74860 856{$this->_from} {$this->_where}";
f38e3c19 857 $this->executeReportQuery($tempQuery);
2f4b4d54 858
1f220d30 859 // 5. show final result set from temp table
2f4b4d54 860 $rows = array();
1f220d30 861 $this->select('final');
389cfee7 862 $this->_having = "";
863 if (!empty($nullFilters)) {
864 $this->_having = "HAVING " . implode(' AND ', $nullFilters);
865 }
83e74860 866 $this->orderBy();
ecf1e543 867 foreach ($this->_sections as $alias => $section) {
868 if (!empty($section) && $section['name'] == 'activity_date_time') {
869 $this->alterSectionHeaderForDateTime('civireport_activity_temp_target', $section['tplField']);
870 }
e06df9af 871 }
a1bf1058
ML
872
873 if ($applyLimit) {
874 $this->limit();
875 }
876
d1641c51 877 $groupByFromSelect = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, 'civicrm_activity_id');
52272965 878
cbbc16ec 879 $this->_where = " WHERE (1)";
52272965 880 $this->buildPermissionClause();
cbbc16ec
TS
881 if ($this->_aclWhere) {
882 $this->_where .= " AND {$this->_aclWhere} ";
883 }
52272965 884
7116d719 885 $sql = "{$this->_select}
52272965
E
886 FROM civireport_activity_temp_target tar
887 INNER JOIN civicrm_activity {$this->_aliases['civicrm_activity']} ON {$this->_aliases['civicrm_activity']}.id = tar.civicrm_activity_id
888 INNER JOIN civicrm_activity_contact {$this->_aliases['civicrm_activity_contact']} ON {$this->_aliases['civicrm_activity_contact']}.activity_id = {$this->_aliases['civicrm_activity']}.id
889 AND {$this->_aliases['civicrm_activity_contact']}.record_type_id = {$sourceID}
cbbc16ec
TS
890 LEFT JOIN civicrm_contact contact_civireport ON contact_civireport.id = {$this->_aliases['civicrm_activity_contact']}.contact_id
891 {$this->_where} {$groupByFromSelect} {$this->_having} {$this->_orderBy} {$this->_limit}";
52272965 892
a1bf1058 893 CRM_Utils_Hook::alterReportVar('sql', $this, $this);
cbbc16ec 894 $this->addToDeveloperTab($sql);
a1bf1058
ML
895
896 return $sql;
897 }
898
899 public function postProcess() {
900 //reset value of activity_date
901 if (!empty($this->_resetDateFilter)) {
902 $this->_formValues["activity_date_time_relative"] = NULL;
903 }
904
905 $this->beginPostProcess();
906 $sql = $this->buildQuery(TRUE);
2f4b4d54
DS
907 $this->buildRows($sql, $rows);
908
909 // format result set.
1f220d30 910 $this->formatDisplay($rows);
2f4b4d54
DS
911
912 // assign variables to templates
913 $this->doTemplateAssignment($rows);
914
915 // do print / pdf / instance stuff if needed
916 $this->endPostProcess($rows);
6a488035
TO
917 }
918
74cf4551 919 /**
4b62bc4f
EM
920 * Alter display of rows.
921 *
922 * Iterate through the rows retrieved via SQL and make changes for display purposes,
923 * such as rendering contacts as links.
924 *
925 * @param array $rows
926 * Rows generated by SQL, with an array for each row.
74cf4551 927 */
00be9182 928 public function alterDisplay(&$rows) {
9d72cede
EM
929 $entryFound = FALSE;
930 $activityType = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE);
6a488035 931 $activityStatus = CRM_Core_PseudoConstant::activityStatus();
410e8105 932 $priority = CRM_Core_PseudoConstant::get('CRM_Activity_DAO_Activity', 'priority_id');
9d72cede 933 $viewLinks = FALSE;
9d72cede
EM
934 $context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'report');
935 $actUrl = '';
6a488035
TO
936
937 if (CRM_Core_Permission::check('access CiviCRM')) {
9d72cede
EM
938 $viewLinks = TRUE;
939 $onHover = ts('View Contact Summary for this Contact');
6a488035
TO
940 $onHoverAct = ts('View Activity Record');
941 }
38ba593b 942 foreach ($rows as $rowNum => $row) {
1efec7ff 943 // if we have an activity type, format the View Activity link for use in various columns
9d72cede
EM
944 if ($viewLinks &&
945 array_key_exists('civicrm_activity_activity_type_id', $row)
946 ) {
1efec7ff
DG
947 // Check for target contact id(s) and use the first contact id in that list for view activity link if found,
948 // else use source contact id
949 if (!empty($rows[$rowNum]['civicrm_contact_contact_target_id'])) {
950 $targets = explode(';', $rows[$rowNum]['civicrm_contact_contact_target_id']);
951 $cid = $targets[0];
952 }
953 else {
954 $cid = $rows[$rowNum]['civicrm_contact_contact_source_id'];
955 }
956
957 $actActionLinks = CRM_Activity_Selector_Activity::actionLinks($row['civicrm_activity_activity_type_id'],
958 CRM_Utils_Array::value('civicrm_activity_source_record_id', $rows[$rowNum]),
959 FALSE,
960 $rows[$rowNum]['civicrm_activity_id']
961 );
962
963 $actLinkValues = array(
964 'id' => $rows[$rowNum]['civicrm_activity_id'],
965 'cid' => $cid,
966 'cxt' => $context,
967 );
968 $actUrl = CRM_Utils_System::url($actActionLinks[CRM_Core_Action::VIEW]['url'],
969 CRM_Core_Action::replace($actActionLinks[CRM_Core_Action::VIEW]['qs'], $actLinkValues), TRUE
970 );
971 }
972
482691e1
RN
973 if (array_key_exists('civicrm_contact_contact_source', $row)) {
974 if ($value = $row['civicrm_contact_contact_source_id']) {
975 if ($viewLinks) {
976 $url = CRM_Utils_System::url("civicrm/contact/view",
977 'reset=1&cid=' . $value,
978 $this->_absoluteUrl
979 );
980 $rows[$rowNum]['civicrm_contact_contact_source_link'] = $url;
981 $rows[$rowNum]['civicrm_contact_contact_source_hover'] = $onHover;
982 }
983 $entryFound = TRUE;
984 }
985 }
986
987 if (array_key_exists('civicrm_contact_contact_assignee', $row)) {
988 $assigneeNames = explode(';', $row['civicrm_contact_contact_assignee']);
989 if ($value = $row['civicrm_contact_contact_assignee_id']) {
990 $assigneeContactIds = explode(';', $value);
991 $link = array();
992 if ($viewLinks) {
993 foreach ($assigneeContactIds as $id => $value) {
994 if (isset($value) && isset($assigneeNames[$id])) {
995 $url = CRM_Utils_System::url("civicrm/contact/view",
996 'reset=1&cid=' . $value,
997 $this->_absoluteUrl
998 );
9d72cede
EM
999 $link[] = "<a title='" . $onHover . "' href='" . $url .
1000 "'>{$assigneeNames[$id]}</a>";
482691e1
RN
1001 }
1002 }
1003 $rows[$rowNum]['civicrm_contact_contact_assignee'] = implode('; ', $link);
1004 }
1005 $entryFound = TRUE;
1006 }
1007 }
1008
1009 if (array_key_exists('civicrm_contact_contact_target', $row)) {
1010 $targetNames = explode(';', $row['civicrm_contact_contact_target']);
1011 if ($value = $row['civicrm_contact_contact_target_id']) {
1012 $targetContactIds = explode(';', $value);
1013 $link = array();
1014 if ($viewLinks) {
1015 foreach ($targetContactIds as $id => $value) {
1016 if (isset($value) && isset($targetNames[$id])) {
1017 $url = CRM_Utils_System::url("civicrm/contact/view",
1018 'reset=1&cid=' . $value,
1019 $this->_absoluteUrl
1020 );
9d72cede
EM
1021 $link[] = "<a title='" . $onHover . "' href='" . $url .
1022 "'>{$targetNames[$id]}</a>";
482691e1
RN
1023 }
1024 }
1025 $rows[$rowNum]['civicrm_contact_contact_target'] = implode('; ', $link);
1026 }
1027 $entryFound = TRUE;
1028 }
1029 }
1030
6a488035
TO
1031 if (array_key_exists('civicrm_activity_activity_type_id', $row)) {
1032 if ($value = $row['civicrm_activity_activity_type_id']) {
1033 $rows[$rowNum]['civicrm_activity_activity_type_id'] = $activityType[$value];
482691e1 1034 if ($viewLinks) {
1efec7ff 1035 $rows[$rowNum]['civicrm_activity_activity_type_id_link'] = $actUrl;
482691e1
RN
1036 $rows[$rowNum]['civicrm_activity_activity_type_id_hover'] = $onHoverAct;
1037 }
6a488035
TO
1038 $entryFound = TRUE;
1039 }
1040 }
1041
1042 if (array_key_exists('civicrm_activity_status_id', $row)) {
1043 if ($value = $row['civicrm_activity_status_id']) {
1044 $rows[$rowNum]['civicrm_activity_status_id'] = $activityStatus[$value];
1045 $entryFound = TRUE;
1046 }
1047 }
1048
410e8105 1049 if (array_key_exists('civicrm_activity_priority_id', $row)) {
1050 if ($value = $row['civicrm_activity_priority_id']) {
1051 $rows[$rowNum]['civicrm_activity_priority_id'] = $priority[$value];
1052 $entryFound = TRUE;
1053 }
1054 }
1055
34e0bdb6 1056 if (array_key_exists('civicrm_activity_details', $row) && $this->_outputMode == 'html') {
1efec7ff
DG
1057 if ($value = $row['civicrm_activity_details']) {
1058 $fullDetails = $rows[$rowNum]['civicrm_activity_details'];
1059 $rows[$rowNum]['civicrm_activity_details'] = substr($fullDetails, 0, strrpos(substr($fullDetails, 0, 80), ' '));
1060 if ($actUrl) {
6e5e50a9 1061 $rows[$rowNum]['civicrm_activity_details'] .= " <a href='{$actUrl}' title='{$onHoverAct}'>(more)</a>";
1efec7ff
DG
1062 }
1063 $entryFound = TRUE;
1064 }
1065 }
1066
6a488035
TO
1067 if (array_key_exists('civicrm_activity_campaign_id', $row)) {
1068 if ($value = $row['civicrm_activity_campaign_id']) {
1069 $rows[$rowNum]['civicrm_activity_campaign_id'] = $this->activeCampaigns[$value];
1070 $entryFound = TRUE;
1071 }
1072 }
1073
1074 if (array_key_exists('civicrm_activity_engagement_level', $row)) {
1075 if ($value = $row['civicrm_activity_engagement_level']) {
1076 $rows[$rowNum]['civicrm_activity_engagement_level'] = $this->engagementLevels[$value];
1077 $entryFound = TRUE;
1078 }
1079 }
1080
9d72cede
EM
1081 if (array_key_exists('civicrm_activity_activity_date_time', $row) &&
1082 array_key_exists('civicrm_activity_status_id', $row)
1083 ) {
6a488035
TO
1084 if (CRM_Utils_Date::overdue($rows[$rowNum]['civicrm_activity_activity_date_time']) &&
1085 $activityStatus[$row['civicrm_activity_status_id']] != 'Completed'
1086 ) {
1087 $rows[$rowNum]['class'] = "status-overdue";
1088 $entryFound = TRUE;
1089 }
1090 }
1091
66ad82d6 1092 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'activity', 'List all activities for this', ';') ? TRUE : $entryFound;
482691e1 1093
6a488035
TO
1094 if (!$entryFound) {
1095 break;
1096 }
1097 }
1098 }
667abe3a 1099
00be9182 1100 public function sectionTotals() {
667abe3a 1101 // Reports using order_bys with sections must populate $this->_selectAliases in select() method.
1102 if (empty($this->_selectAliases)) {
1103 return;
1104 }
1105
1106 if (!empty($this->_sections)) {
1107 // pull section aliases out of $this->_sections
1108 $sectionAliases = array_keys($this->_sections);
1109
1110 $ifnulls = array();
1111 foreach (array_merge($sectionAliases, $this->_selectAliases) as $alias) {
1112 $ifnulls[] = "ifnull($alias, '') as $alias";
1113 }
b708c08d 1114 $this->_select = "SELECT " . implode(", ", $ifnulls);
36d2f4d5 1115 $this->_select = CRM_Contact_BAO_Query::appendAnyValueToSelect($ifnulls, $sectionAliases);
667abe3a 1116
b708c08d 1117 $query = $this->_select .
e13b3d8e 1118 ", count(DISTINCT civicrm_activity_id) as ct from civireport_activity_temp_target group by " .
667abe3a 1119 implode(", ", $sectionAliases);
1120
1121 // initialize array of total counts
1122 $totals = array();
f38e3c19 1123 $dao = $this->executeReportQuery($query);
667abe3a 1124 while ($dao->fetch()) {
1125 // let $this->_alterDisplay translate any integer ids to human-readable values.
1126 $rows[0] = $dao->toArray();
1127 $this->alterDisplay($rows);
1128 $row = $rows[0];
1129
1130 // add totals for all permutations of section values
1131 $values = array();
1132 $i = 1;
1133 $aliasCount = count($sectionAliases);
1134 foreach ($sectionAliases as $alias) {
1135 $values[] = $row[$alias];
1136 $key = implode(CRM_Core_DAO::VALUE_SEPARATOR, $values);
1137 if ($i == $aliasCount) {
1138 // the last alias is the lowest-level section header; use count as-is
1139 $totals[$key] = $dao->ct;
1140 }
1141 else {
1142 // other aliases are higher level; roll count into their total
1143 $totals[$key] += $dao->ct;
1144 }
1145 }
1146 }
1147 $this->assign('sectionTotals', $totals);
1148 }
1149 }
96025800 1150
6a488035 1151}