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