Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
f299f7db | 6 | | Copyright CiviCRM LLC (c) 2004-2020 | |
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 | |
f299f7db | 31 | * @copyright CiviCRM LLC (c) 2004-2020 |
6a488035 TO |
32 | */ |
33 | class CRM_Report_Form_ActivitySummary extends CRM_Report_Form { | |
34 | ||
35 | protected $_emailField = FALSE; | |
430ae6dd | 36 | protected $_phoneField = FALSE; |
3832d330 KJ |
37 | protected $_tempTableName; |
38 | protected $_tempDurationSumTableName; | |
430ae6dd | 39 | |
74cf4551 | 40 | /** |
1728e9a0 | 41 | * This report has not been optimised for group filtering. |
42 | * | |
43 | * The functionality for group filtering has been improved but not | |
44 | * all reports have been adjusted to take care of it. This report has not | |
45 | * and will run an inefficient query until fixed. | |
46 | * | |
47 | * CRM-19170 | |
48 | * | |
49 | * @var bool | |
74cf4551 | 50 | */ |
1728e9a0 | 51 | protected $groupFilterNotOptimised = TRUE; |
52 | ||
74cf4551 | 53 | /** |
73b448bf | 54 | * Class constructor. |
74cf4551 | 55 | */ |
00be9182 | 56 | public function __construct() { |
be2fb01f CW |
57 | $this->_columns = [ |
58 | 'civicrm_contact' => [ | |
6a488035 | 59 | 'dao' => 'CRM_Contact_DAO_Contact', |
be2fb01f CW |
60 | 'fields' => [ |
61 | 'id' => [ | |
6a488035 TO |
62 | 'required' => TRUE, |
63 | 'no_display' => TRUE, | |
be2fb01f CW |
64 | ], |
65 | 'sort_name' => [ | |
a8c87952 | 66 | 'title' => ts('Contact Name'), |
6a488035 | 67 | 'no_repeat' => TRUE, |
be2fb01f CW |
68 | ], |
69 | ], | |
70 | 'filters' => [ | |
71 | 'sort_name' => [ | |
a8c87952 | 72 | 'title' => ts('Contact Name'), |
be2fb01f CW |
73 | ], |
74 | ], | |
75 | 'group_bys' => [ | |
76 | 'sort_name' => [ | |
6a488035 TO |
77 | 'name' => 'id', |
78 | 'title' => ts('Contact'), | |
be2fb01f CW |
79 | ], |
80 | ], | |
81 | 'order_bys' => [ | |
82 | 'sort_name' => [ | |
21dfd5f5 | 83 | 'title' => ts('Contact Name'), |
be2fb01f CW |
84 | ], |
85 | ], | |
6a488035 | 86 | 'grouping' => 'contact-fields', |
be2fb01f CW |
87 | ], |
88 | 'civicrm_email' => [ | |
6a488035 | 89 | 'dao' => 'CRM_Core_DAO_Email', |
be2fb01f CW |
90 | 'fields' => [ |
91 | 'email' => [ | |
ccc29f8e | 92 | 'title' => ts('Email'), |
be2fb01f CW |
93 | ], |
94 | ], | |
95 | 'order_bys' => [ | |
96 | 'email' => [ | |
21dfd5f5 | 97 | 'title' => ts('Email'), |
be2fb01f CW |
98 | ], |
99 | ], | |
6a488035 | 100 | 'grouping' => 'contact-fields', |
be2fb01f CW |
101 | ], |
102 | 'civicrm_phone' => [ | |
6a488035 | 103 | 'dao' => 'CRM_Core_DAO_Email', |
be2fb01f CW |
104 | 'fields' => [ |
105 | 'phone' => [ | |
ccc29f8e | 106 | 'title' => ts('Phone'), |
be2fb01f CW |
107 | ], |
108 | ], | |
6a488035 | 109 | 'grouping' => 'contact-fields', |
be2fb01f CW |
110 | ], |
111 | 'civicrm_activity' => [ | |
6a488035 | 112 | 'dao' => 'CRM_Activity_DAO_Activity', |
be2fb01f CW |
113 | 'fields' => [ |
114 | 'activity_type_id' => [ | |
a8c87952 | 115 | 'title' => ts('Activity Type'), |
96a1ef05 SB |
116 | 'required' => TRUE, |
117 | 'type' => CRM_Utils_Type::T_STRING, | |
be2fb01f CW |
118 | ], |
119 | 'status_id' => [ | |
fdee8d8d | 120 | 'title' => ts('Activity Status'), |
6a488035 TO |
121 | 'default' => TRUE, |
122 | 'type' => CRM_Utils_Type::T_STRING, | |
be2fb01f CW |
123 | ], |
124 | 'duration' => [ | |
96495cc0 | 125 | 'title' => ts('Duration'), |
d17ad0af | 126 | 'default' => TRUE, |
be2fb01f CW |
127 | ], |
128 | 'priority_id' => [ | |
da236f9a | 129 | 'title' => ts('Priority'), |
130 | 'default' => TRUE, | |
131 | 'type' => CRM_Utils_Type::T_STRING, | |
be2fb01f CW |
132 | ], |
133 | 'id' => [ | |
96495cc0 | 134 | 'title' => ts('Total Activities'), |
6a488035 | 135 | 'required' => TRUE, |
be2fb01f | 136 | 'statistics' => [ |
96a1ef05 | 137 | 'count' => ts('Count'), |
be2fb01f CW |
138 | ], |
139 | ], | |
140 | ], | |
141 | 'filters' => [ | |
142 | 'activity_date_time' => [ | |
21dfd5f5 | 143 | 'operatorType' => CRM_Report_Form::OP_DATE, |
be2fb01f CW |
144 | ], |
145 | 'activity_type_id' => [ | |
a8c87952 | 146 | 'title' => ts('Activity Type'), |
96495cc0 | 147 | 'default' => 0, |
6a488035 TO |
148 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
149 | 'options' => CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE), | |
be2fb01f CW |
150 | ], |
151 | 'status_id' => [ | |
a8c87952 | 152 | 'title' => ts('Activity Status'), |
8ee006e7 | 153 | 'type' => CRM_Utils_Type::T_STRING, |
6a488035 TO |
154 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
155 | 'options' => CRM_Core_PseudoConstant::activityStatus(), | |
be2fb01f CW |
156 | ], |
157 | 'priority_id' => [ | |
71c30391 | 158 | 'title' => ts('Activity Priority'), |
8ee006e7 | 159 | 'type' => CRM_Utils_Type::T_INT, |
6a488035 | 160 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
cbf48754 | 161 | 'options' => CRM_Core_PseudoConstant::get('CRM_Activity_DAO_Activity', 'priority_id'), |
be2fb01f CW |
162 | ], |
163 | ], | |
164 | 'group_bys' => [ | |
165 | 'activity_date_time' => [ | |
353ffa53 TO |
166 | 'title' => ts('Activity Date'), |
167 | 'frequency' => TRUE, | |
be2fb01f CW |
168 | ], |
169 | 'activity_type_id' => [ | |
a8c87952 | 170 | 'title' => ts('Activity Type'), |
6a488035 | 171 | 'default' => TRUE, |
be2fb01f CW |
172 | ], |
173 | 'status_id' => [ | |
fdee8d8d | 174 | 'title' => ts('Activity Status'), |
96a1ef05 | 175 | 'default' => TRUE, |
be2fb01f CW |
176 | ], |
177 | ], | |
178 | 'order_bys' => [ | |
179 | 'activity_date_time' => [ | |
21dfd5f5 | 180 | 'title' => ts('Activity Date'), |
be2fb01f CW |
181 | ], |
182 | 'activity_type_id' => [ | |
21dfd5f5 | 183 | 'title' => ts('Activity Type'), |
be2fb01f CW |
184 | ], |
185 | ], | |
6a488035 TO |
186 | 'grouping' => 'activity-fields', |
187 | 'alias' => 'activity', | |
be2fb01f CW |
188 | ], |
189 | ]; | |
96a1ef05 | 190 | $this->_groupFilter = TRUE; |
6a488035 TO |
191 | |
192 | parent::__construct(); | |
193 | } | |
194 | ||
00be9182 | 195 | public function select() { |
be2fb01f CW |
196 | $select = []; |
197 | $this->_columnHeaders = []; | |
6a488035 TO |
198 | foreach ($this->_columns as $tableName => $table) { |
199 | if (array_key_exists('group_bys', $table)) { | |
200 | foreach ($table['group_bys'] as $fieldName => $field) { | |
a7488080 | 201 | if (!empty($this->_params['group_bys'][$fieldName])) { |
96a1ef05 SB |
202 | //include column in report when selected in group by but not in column section. |
203 | if (empty($this->_params['fields'][$fieldName])) { | |
204 | $this->_params['fields'][$fieldName] = TRUE; | |
205 | } | |
6221462f | 206 | if (isset($this->_params['group_bys_freq']) && !empty($this->_params['group_bys_freq'][$fieldName])) { |
a8c87952 EM |
207 | switch ($this->_params['group_bys_freq'][$fieldName]) { |
208 | case 'YEARWEEK': | |
209 | $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL WEEKDAY({$field['dbAlias']}) DAY) AS {$tableName}_{$fieldName}_start"; | |
210 | ||
211 | $select[] = "YEARWEEK({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal"; | |
212 | $select[] = "WEEKOFYEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval"; | |
213 | $field['title'] = 'Week'; | |
214 | break; | |
215 | ||
216 | case 'YEAR': | |
217 | $select[] = "MAKEDATE(YEAR({$field['dbAlias']}), 1) AS {$tableName}_{$fieldName}_start"; | |
218 | $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal"; | |
219 | $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval"; | |
220 | $field['title'] = 'Year'; | |
221 | break; | |
222 | ||
223 | case 'MONTH': | |
224 | $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL (DAYOFMONTH({$field['dbAlias']})-1) DAY) as {$tableName}_{$fieldName}_start"; | |
225 | $select[] = "MONTH({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal"; | |
226 | $select[] = "MONTHNAME({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval"; | |
227 | $field['title'] = 'Month'; | |
228 | break; | |
229 | ||
230 | case 'QUARTER': | |
231 | $select[] = "STR_TO_DATE(CONCAT( 3 * QUARTER( {$field['dbAlias']} ) -2 , '/', '1', '/', YEAR( {$field['dbAlias']} ) ), '%m/%d/%Y') AS {$tableName}_{$fieldName}_start"; | |
232 | $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal"; | |
233 | $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval"; | |
234 | $field['title'] = 'Quarter'; | |
235 | break; | |
236 | } | |
6a488035 TO |
237 | $this->_interval = $field['title']; |
238 | $this->_columnHeaders["{$tableName}_{$fieldName}_start"]['title'] = $field['title'] . ' Beginning'; | |
239 | $this->_columnHeaders["{$tableName}_{$fieldName}_start"]['type'] = $field['type']; | |
240 | $this->_columnHeaders["{$tableName}_{$fieldName}_start"]['group_by'] = $this->_params['group_bys_freq'][$fieldName]; | |
241 | ||
b44e3f84 | 242 | // just to make sure these values are transferred to rows. |
6a488035 TO |
243 | // since we need that for calculation purpose, |
244 | // e.g making subtotals look nicer or graphs | |
be2fb01f CW |
245 | $this->_columnHeaders["{$tableName}_{$fieldName}_interval"] = ['no_display' => TRUE]; |
246 | $this->_columnHeaders["{$tableName}_{$fieldName}_subtotal"] = ['no_display' => TRUE]; | |
6a488035 TO |
247 | } |
248 | } | |
249 | } | |
250 | } | |
251 | if (array_key_exists('fields', $table)) { | |
252 | foreach ($table['fields'] as $fieldName => $field) { | |
8cc574cf | 253 | if (!empty($field['required']) || !empty($this->_params['fields'][$fieldName])) { |
657ee972 | 254 | if ($tableName == 'civicrm_email' || in_array('email', CRM_Utils_Array::collect('column', $this->_params['order_bys']))) { |
6a488035 TO |
255 | $this->_emailField = TRUE; |
256 | } | |
257 | if ($tableName == 'civicrm_phone') { | |
258 | $this->_phoneField = TRUE; | |
259 | } | |
a7488080 | 260 | if (!empty($field['statistics'])) { |
6a488035 TO |
261 | foreach ($field['statistics'] as $stat => $label) { |
262 | switch (strtolower($stat)) { | |
263 | case 'count': | |
264 | $select[] = "COUNT(DISTINCT({$field['dbAlias']})) as {$tableName}_{$fieldName}_{$stat}"; | |
265 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type::T_INT; | |
266 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label; | |
267 | $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}"; | |
268 | break; | |
6a488035 TO |
269 | } |
270 | } | |
271 | } | |
272 | elseif ($fieldName == 'activity_type_id') { | |
a7488080 | 273 | if (empty($this->_params['group_bys']['activity_type_id'])) { |
6a488035 TO |
274 | $select[] = "GROUP_CONCAT(DISTINCT {$field['dbAlias']} ORDER BY {$field['dbAlias']} ) as {$tableName}_{$fieldName}"; |
275 | } | |
276 | else { | |
277 | $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}"; | |
278 | } | |
279 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field); | |
280 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field); | |
281 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['no_display'] = CRM_Utils_Array::value('no_display', $field); | |
282 | } | |
283 | else { | |
284 | $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}"; | |
285 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field); | |
286 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field); | |
287 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['no_display'] = CRM_Utils_Array::value('no_display', $field); | |
288 | } | |
289 | } | |
290 | } | |
291 | } | |
292 | } | |
d1641c51 | 293 | $this->_selectClauses = $select; |
6a488035 TO |
294 | |
295 | $this->_select = "SELECT " . implode(', ', $select) . " "; | |
296 | } | |
297 | ||
70599df6 | 298 | /** |
299 | * Generate from clause. | |
70599df6 | 300 | */ |
1ad3437a | 301 | public function from() { |
44f817d4 | 302 | $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); |
ecaec004 RN |
303 | $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); |
304 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); | |
305 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); | |
6a488035 | 306 | |
1ad3437a | 307 | $this->_from = " |
308 | FROM civicrm_activity {$this->_aliases['civicrm_activity']} | |
309 | ||
310 | LEFT JOIN civicrm_activity_contact target_activity | |
311 | ON {$this->_aliases['civicrm_activity']}.id = target_activity.activity_id AND | |
312 | target_activity.record_type_id = {$targetID} | |
313 | LEFT JOIN civicrm_activity_contact assignment_activity | |
314 | ON {$this->_aliases['civicrm_activity']}.id = assignment_activity.activity_id AND | |
315 | assignment_activity.record_type_id = {$assigneeID} | |
316 | LEFT JOIN civicrm_activity_contact source_activity | |
317 | ON {$this->_aliases['civicrm_activity']}.id = source_activity.activity_id AND | |
318 | source_activity.record_type_id = {$sourceID} | |
319 | LEFT JOIN civicrm_contact contact_civireport | |
320 | ON target_activity.contact_id = contact_civireport.id | |
321 | LEFT JOIN civicrm_contact civicrm_contact_assignee | |
322 | ON assignment_activity.contact_id = civicrm_contact_assignee.id | |
323 | LEFT JOIN civicrm_contact civicrm_contact_source | |
324 | ON source_activity.contact_id = civicrm_contact_source.id | |
325 | {$this->_aclFrom} | |
326 | LEFT JOIN civicrm_option_value | |
327 | ON ( {$this->_aliases['civicrm_activity']}.activity_type_id = civicrm_option_value.value ) | |
328 | LEFT JOIN civicrm_option_group | |
329 | ON civicrm_option_group.id = civicrm_option_value.option_group_id | |
330 | LEFT JOIN civicrm_case_activity | |
331 | ON civicrm_case_activity.activity_id = {$this->_aliases['civicrm_activity']}.id | |
332 | LEFT JOIN civicrm_case | |
333 | ON civicrm_case_activity.case_id = civicrm_case.id | |
334 | LEFT JOIN civicrm_case_contact | |
335 | ON civicrm_case_contact.case_id = civicrm_case.id "; | |
336 | ||
337 | $this->joinPhoneFromContact(); | |
338 | ||
339 | $this->joinEmailFromContact(); | |
340 | } | |
341 | ||
342 | /** | |
343 | * Generate from clause for when calculating activity durations. | |
344 | */ | |
345 | public function activityDurationFrom() { | |
346 | $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); | |
347 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); | |
348 | $this->_from = " | |
9099266e WM |
349 | FROM civicrm_activity {$this->_aliases['civicrm_activity']} |
350 | LEFT JOIN civicrm_activity_contact target_activity | |
351 | ON {$this->_aliases['civicrm_activity']}.id = target_activity.activity_id AND | |
352 | target_activity.record_type_id = {$targetID} | |
353 | LEFT JOIN civicrm_contact contact_civireport | |
354 | ON target_activity.contact_id = contact_civireport.id | |
355 | {$this->_aclFrom}"; | |
a6d5eae9 DJ |
356 | |
357 | // Email table is needed if sorting by Email. | |
358 | $this->joinEmailFromContact(); | |
6a488035 TO |
359 | } |
360 | ||
70599df6 | 361 | /** |
362 | * Generate where clause. | |
363 | * | |
041ecc95 | 364 | * @param bool $durationMode |
70599df6 | 365 | */ |
96495cc0 KJ |
366 | public function where($durationMode = FALSE) { |
367 | $optionGroupClause = ''; | |
368 | if (!$durationMode) { | |
369 | $optionGroupClause = 'civicrm_option_group.name = "activity_type" AND '; | |
370 | } | |
371 | $this->_where = " WHERE {$optionGroupClause} | |
372 | {$this->_aliases['civicrm_activity']}.is_test = 0 AND | |
373 | {$this->_aliases['civicrm_activity']}.is_deleted = 0 AND | |
374 | {$this->_aliases['civicrm_activity']}.is_current_revision = 1"; | |
6a488035 | 375 | |
be2fb01f | 376 | $clauses = []; |
6a488035 TO |
377 | foreach ($this->_columns as $tableName => $table) { |
378 | if (array_key_exists('filters', $table)) { | |
379 | ||
380 | foreach ($table['filters'] as $fieldName => $field) { | |
381 | $clause = NULL; | |
382 | if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) { | |
383 | $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params); | |
a8c87952 EM |
384 | $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params); |
385 | $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params); | |
6a488035 TO |
386 | |
387 | $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']); | |
388 | } | |
389 | else { | |
390 | $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params); | |
391 | if ($op) { | |
392 | $clause = $this->whereClause($field, | |
393 | $op, | |
394 | CRM_Utils_Array::value("{$fieldName}_value", $this->_params), | |
395 | CRM_Utils_Array::value("{$fieldName}_min", $this->_params), | |
396 | CRM_Utils_Array::value("{$fieldName}_max", $this->_params) | |
397 | ); | |
398 | } | |
399 | } | |
400 | ||
401 | if (!empty($clause)) { | |
402 | $clauses[] = $clause; | |
403 | } | |
404 | } | |
405 | } | |
406 | } | |
407 | ||
408 | if (empty($clauses)) { | |
409 | $this->_where .= " "; | |
410 | } | |
411 | else { | |
412 | $this->_where .= " AND " . implode(' AND ', $clauses); | |
413 | } | |
414 | ||
96495cc0 | 415 | if ($this->_aclWhere && !$durationMode) { |
3f1f2db0 | 416 | $this->_where .= " AND ({$this->_aclWhere} OR civicrm_contact_source.is_deleted=0 OR civicrm_contact_assignee.is_deleted=0)"; |
6a488035 TO |
417 | } |
418 | } | |
419 | ||
d5625bc9 DJ |
420 | /** |
421 | * Build the report query. | |
422 | * | |
423 | * @param bool $applyLimit | |
424 | * | |
425 | * @return string | |
426 | */ | |
427 | public function buildQuery($applyLimit = TRUE) { | |
428 | $this->buildGroupTempTable(); | |
429 | $this->select(); | |
430 | $this->from(); | |
431 | $this->customDataFrom(); | |
432 | $this->buildPermissionClause(); | |
433 | $this->where(); | |
434 | $this->groupBy(); | |
435 | $this->orderBy(); | |
436 | ||
a6d5eae9 DJ |
437 | // Order by & Section columns not selected for display need to be included in SELECT. |
438 | $unselectedColumns = array_merge($this->unselectedOrderByColumns(), $this->unselectedSectionColumns()); | |
439 | foreach ($unselectedColumns as $alias => $field) { | |
d5625bc9 DJ |
440 | $clause = $this->getSelectClauseWithGroupConcatIfNotGroupedBy($field['table_name'], $field['name'], $field); |
441 | if (!$clause) { | |
442 | $clause = "{$field['dbAlias']} as {$alias}"; | |
443 | } | |
444 | $this->_select .= ", $clause "; | |
445 | } | |
446 | ||
447 | if ($applyLimit && empty($this->_params['charts'])) { | |
448 | $this->limit(); | |
449 | } | |
450 | CRM_Utils_Hook::alterReportVar('sql', $this, $this); | |
451 | ||
d5625bc9 DJ |
452 | // build temporary table column names base on column headers of result |
453 | $dbColumns = []; | |
454 | foreach ($this->_columnHeaders as $fieldName => $dontCare) { | |
455 | $dbColumns[] = $fieldName . ' VARCHAR(128)'; | |
456 | } | |
457 | ||
a6d5eae9 DJ |
458 | // Order by & Section columns not selected for display need to be included in temp table. |
459 | foreach ($unselectedColumns as $alias => $section) { | |
460 | $dbColumns[] = $alias . ' VARCHAR(128)'; | |
461 | } | |
462 | ||
d5625bc9 DJ |
463 | // create temp table to store main result |
464 | $this->_tempTableName = $this->createTemporaryTable('tempTable', " | |
465 | id int unsigned NOT NULL AUTO_INCREMENT, " . implode(', ', $dbColumns) . ' , PRIMARY KEY (id)', | |
466 | TRUE); | |
467 | ||
468 | // build main report query | |
469 | $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy} {$this->_limit}"; | |
470 | $this->addToDeveloperTab($sql); | |
471 | ||
472 | // store the result in temporary table | |
473 | $insertCols = ''; | |
a6d5eae9 | 474 | $insertQuery = "INSERT INTO {$this->_tempTableName} ( " . implode(',', array_merge(array_keys($this->_columnHeaders), array_keys($unselectedColumns))) . " ) |
d5625bc9 DJ |
475 | {$sql}"; |
476 | CRM_Core_DAO::executeQuery($insertQuery); | |
477 | ||
478 | // now build the query for duration sum | |
479 | $this->activityDurationFrom(); | |
480 | $this->where(TRUE); | |
481 | $this->groupBy(FALSE); | |
482 | ||
483 | // build the query to calulate duration sum | |
484 | $sql = "SELECT SUM(activity_civireport.duration) as civicrm_activity_duration_total {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy} {$this->_limit}"; | |
485 | ||
486 | // create temp table to store duration | |
487 | $this->_tempDurationSumTableName = $this->createTemporaryTable('tempDurationSumTable', " | |
488 | id int unsigned NOT NULL AUTO_INCREMENT, civicrm_activity_duration_total VARCHAR(128), PRIMARY KEY (id)", | |
489 | TRUE); | |
490 | ||
491 | // store the result in temporary table | |
492 | $insertQuery = "INSERT INTO {$this->_tempDurationSumTableName} (civicrm_activity_duration_total) | |
493 | {$sql}"; | |
6cc7af15 | 494 | CRM_Core_DAO::disableFullGroupByMode(); |
d5625bc9 | 495 | CRM_Core_DAO::executeQuery($insertQuery); |
6cc7af15 | 496 | CRM_Core_DAO::reenableFullGroupByMode(); |
d5625bc9 DJ |
497 | |
498 | $sql = "SELECT {$this->_tempTableName}.*, {$this->_tempDurationSumTableName}.civicrm_activity_duration_total | |
499 | FROM {$this->_tempTableName} INNER JOIN {$this->_tempDurationSumTableName} | |
500 | ON ({$this->_tempTableName}.id = {$this->_tempDurationSumTableName}.id)"; | |
501 | ||
502 | // finally add duration total to column headers | |
503 | $this->_columnHeaders['civicrm_activity_duration_total'] = ['no_display' => 1]; | |
504 | ||
505 | // reset the sql building to default, which is used / called during other actions like "add to group" | |
506 | $this->from(); | |
507 | $this->where(); | |
508 | ||
509 | return $sql; | |
510 | } | |
511 | ||
bc854509 | 512 | /** |
513 | * Group the fields. | |
514 | * | |
515 | * @param bool $includeSelectCol | |
516 | */ | |
d1641c51 | 517 | public function groupBy($includeSelectCol = TRUE) { |
be2fb01f | 518 | $this->_groupBy = []; |
229d3c71 | 519 | if (!empty($this->_params['group_bys']) && |
96495cc0 | 520 | is_array($this->_params['group_bys'])) { |
6a488035 TO |
521 | foreach ($this->_columns as $tableName => $table) { |
522 | if (array_key_exists('group_bys', $table)) { | |
523 | foreach ($table['group_bys'] as $fieldName => $field) { | |
a7488080 CW |
524 | if (!empty($this->_params['group_bys'][$fieldName])) { |
525 | if (!empty($field['chart'])) { | |
6a488035 TO |
526 | $this->assign('chartSupported', TRUE); |
527 | } | |
a8c87952 EM |
528 | if (!empty($table['group_bys'][$fieldName]['frequency']) && |
529 | !empty($this->_params['group_bys_freq'][$fieldName]) | |
530 | ) { | |
6a488035 TO |
531 | |
532 | $append = "YEAR({$field['dbAlias']}),"; | |
533 | if (in_array(strtolower($this->_params['group_bys_freq'][$fieldName]), | |
be2fb01f | 534 | ['year'] |
a8c87952 | 535 | )) { |
6a488035 TO |
536 | $append = ''; |
537 | } | |
538 | $this->_groupBy[] = "$append {$this->_params['group_bys_freq'][$fieldName]}({$field['dbAlias']})"; | |
539 | $append = TRUE; | |
540 | } | |
541 | else { | |
542 | $this->_groupBy[] = $field['dbAlias']; | |
543 | } | |
544 | } | |
545 | } | |
546 | } | |
547 | } | |
d1641c51 | 548 | $groupBy = $this->_groupBy; |
6a488035 TO |
549 | $this->_groupBy = "GROUP BY " . implode(', ', $this->_groupBy); |
550 | } | |
551 | else { | |
d1641c51 | 552 | $groupBy = "{$this->_aliases['civicrm_activity']}.id"; |
96495cc0 | 553 | $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_activity']}.id "; |
6a488035 | 554 | } |
d1641c51 | 555 | if ($includeSelectCol) { |
b708c08d | 556 | $this->_groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, $groupBy); |
d1641c51 | 557 | } |
6a488035 TO |
558 | } |
559 | ||
74cf4551 EM |
560 | /** |
561 | * @param $fields | |
562 | * @param $files | |
563 | * @param $self | |
564 | * | |
565 | * @return array | |
566 | */ | |
52628c99 | 567 | public static function formRule($fields, $files, $self) { |
be2fb01f CW |
568 | $errors = []; |
569 | $contactFields = ['sort_name', 'email', 'phone']; | |
a7488080 | 570 | if (!empty($fields['group_bys'])) { |
a7488080 CW |
571 | if (!empty($fields['group_bys']['activity_date_time'])) { |
572 | if (!empty($fields['group_bys']['sort_name'])) { | |
6a488035 TO |
573 | $errors['fields'] = ts("Please do not select GroupBy 'Activity Date' with GroupBy 'Contact'"); |
574 | } | |
575 | else { | |
576 | foreach ($fields['fields'] as $fieldName => $val) { | |
577 | if (in_array($fieldName, $contactFields)) { | |
578 | $errors['fields'] = ts("Please do not select any Contact Fields with GroupBy 'Activity Date'"); | |
579 | break; | |
580 | } | |
581 | } | |
582 | } | |
583 | } | |
584 | } | |
d5c61232 KJ |
585 | |
586 | // don't allow add to group action unless contact fields are selected. | |
587 | if (isset($fields['_qf_ActivitySummary_submit_group'])) { | |
588 | $contactFieldSelected = FALSE; | |
589 | foreach ($fields['fields'] as $fieldName => $val) { | |
590 | if (in_array($fieldName, $contactFields)) { | |
591 | $contactFieldSelected = TRUE; | |
592 | break; | |
593 | } | |
594 | } | |
595 | ||
596 | if (!$contactFieldSelected) { | |
597 | $errors['fields'] = ts('You cannot use "Add Contacts to Group" action unless contacts fields are selected.'); | |
598 | } | |
599 | } | |
6a488035 TO |
600 | return $errors; |
601 | } | |
602 | ||
96a1ef05 SB |
603 | /** |
604 | * @param $rows | |
605 | * | |
606 | * @return array | |
607 | */ | |
608 | public function statistics(&$rows) { | |
609 | $statistics = parent::statistics($rows); | |
610 | $totalType = $totalActivity = $totalDuration = 0; | |
611 | ||
96495cc0 KJ |
612 | $query = "SELECT {$this->_tempTableName}.civicrm_activity_activity_type_id, |
613 | {$this->_tempTableName}.civicrm_activity_id_count, | |
614 | {$this->_tempDurationSumTableName}.civicrm_activity_duration_total | |
615 | FROM {$this->_tempTableName} INNER JOIN {$this->_tempDurationSumTableName} | |
616 | ON ({$this->_tempTableName}.id = {$this->_tempDurationSumTableName}.id)"; | |
96a1ef05 | 617 | |
96495cc0 | 618 | $actDAO = CRM_Core_DAO::executeQuery($query); |
028df0a9 | 619 | |
be2fb01f | 620 | $activityTypesCount = []; |
d5c61232 | 621 | while ($actDAO->fetch()) { |
96495cc0 KJ |
622 | if (!in_array($actDAO->civicrm_activity_activity_type_id, $activityTypesCount)) { |
623 | $activityTypesCount[] = $actDAO->civicrm_activity_activity_type_id; | |
624 | } | |
625 | ||
626 | $totalActivity += $actDAO->civicrm_activity_id_count; | |
627 | $totalDuration += $actDAO->civicrm_activity_duration_total; | |
96a1ef05 SB |
628 | } |
629 | ||
96495cc0 KJ |
630 | $totalType = count($activityTypesCount); |
631 | ||
be2fb01f | 632 | $statistics['counts']['type'] = [ |
96a1ef05 SB |
633 | 'title' => ts('Total Types'), |
634 | 'value' => $totalType, | |
be2fb01f CW |
635 | ]; |
636 | $statistics['counts']['activities'] = [ | |
96a1ef05 SB |
637 | 'title' => ts('Total Number of Activities'), |
638 | 'value' => $totalActivity, | |
be2fb01f CW |
639 | ]; |
640 | $statistics['counts']['duration'] = [ | |
96a1ef05 SB |
641 | 'title' => ts('Total Duration (in Minutes)'), |
642 | 'value' => $totalDuration, | |
be2fb01f | 643 | ]; |
96a1ef05 SB |
644 | return $statistics; |
645 | } | |
646 | ||
fdee8d8d SB |
647 | public function modifyColumnHeaders() { |
648 | //CRM-16719 modify name of column | |
649 | if (!empty($this->_columnHeaders['civicrm_activity_status_id'])) { | |
ccc29f8e | 650 | $this->_columnHeaders['civicrm_activity_status_id']['title'] = ts('Status'); |
fdee8d8d SB |
651 | } |
652 | } | |
653 | ||
74cf4551 | 654 | /** |
4b62bc4f EM |
655 | * Alter display of rows. |
656 | * | |
657 | * Iterate through the rows retrieved via SQL and make changes for display purposes, | |
658 | * such as rendering contacts as links. | |
659 | * | |
660 | * @param array $rows | |
661 | * Rows generated by SQL, with an array for each row. | |
74cf4551 | 662 | */ |
00be9182 | 663 | public function alterDisplay(&$rows) { |
a8c87952 | 664 | $entryFound = FALSE; |
6a488035 | 665 | $activityType = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE); |
96a1ef05 | 666 | $activityStatus = CRM_Core_PseudoConstant::activityStatus(); |
da236f9a | 667 | $priority = CRM_Core_PseudoConstant::get('CRM_Activity_DAO_Activity', 'priority_id'); |
6a488035 TO |
668 | $onHover = ts('View Contact Summary for this Contact'); |
669 | foreach ($rows as $rowNum => $row) { | |
96a1ef05 SB |
670 | // make count columns point to activity detail report |
671 | if (!empty($row['civicrm_activity_id_count'])) { | |
be2fb01f CW |
672 | $url = []; |
673 | $urlParams = ['activity_type_id', 'gid', 'status_id', 'contact_id']; | |
fdee8d8d | 674 | foreach ($urlParams as $field) { |
3ff6c3c0 | 675 | if (!empty($row['civicrm_activity_' . $field])) { |
96a1ef05 SB |
676 | $url[] = "{$field}_op=in&{$field}_value={$row['civicrm_activity_'.$field]}"; |
677 | } | |
3ff6c3c0 SB |
678 | elseif (!empty($this->_params[$field . '_value'])) { |
679 | $val = implode(",", $this->_params[$field . '_value']); | |
fdee8d8d SB |
680 | $url[] = "{$field}_op=in&{$field}_value={$val}"; |
681 | } | |
682 | } | |
be2fb01f | 683 | $date_suffixes = ['relative', 'from', 'to']; |
201312d6 | 684 | foreach ($date_suffixes as $suffix) { |
fdee8d8d | 685 | if (!empty($this->_params['activity_date_time_' . $suffix])) { |
96a1ef05 SB |
686 | list($from, $to) |
687 | = $this->getFromTo( | |
688 | CRM_Utils_Array::value("activity_date_time_relative", $this->_params), | |
689 | CRM_Utils_Array::value("activity_date_time_from", $this->_params), | |
690 | CRM_Utils_Array::value("activity_date_time_to", $this->_params) | |
3ff6c3c0 | 691 | ); |
96a1ef05 | 692 | $url[] = "activity_date_time_from={$from}&activity_date_time_to={$to}"; |
fdee8d8d | 693 | break; |
96a1ef05 SB |
694 | } |
695 | } | |
8f621a01 | 696 | // reset date filter on activity reports. |
697 | $url[] = "resetDateFilter=1"; | |
3ff6c3c0 SB |
698 | $url = implode('&', $url); |
699 | $url = CRM_Report_Utils_Report::getNextUrl('activity', "reset=1&force=1&{$url}", | |
96a1ef05 SB |
700 | $this->_absoluteUrl, |
701 | $this->_id, | |
702 | $this->_drilldownReport); | |
703 | $rows[$rowNum]['civicrm_activity_id_count_link'] = $url; | |
704 | $rows[$rowNum]['civicrm_activity_id_count_hover'] = ts('List all activity(s) for this row.'); | |
705 | $entryFound = TRUE; | |
706 | } | |
6a488035 TO |
707 | |
708 | if (array_key_exists('civicrm_contact_sort_name', $row) && $this->_outputMode != 'csv') { | |
709 | if ($value = $row['civicrm_contact_id']) { | |
710 | ||
0555ce4f WM |
711 | // unset the name, email and phone fields if the contact is the same as the previous contact |
712 | if (isset($previousContact) && $previousContact == $value) { | |
6a488035 TO |
713 | $rows[$rowNum]['civicrm_contact_sort_name'] = ""; |
714 | ||
715 | if (array_key_exists('civicrm_email_email', $row)) { | |
716 | $rows[$rowNum]['civicrm_email_email'] = ""; | |
717 | } | |
718 | if (array_key_exists('civicrm_phone_phone', $row)) { | |
719 | $rows[$rowNum]['civicrm_phone_phone'] = ""; | |
720 | } | |
721 | } | |
722 | else { | |
723 | $url = CRM_Utils_System::url('civicrm/contact/view', | |
724 | 'reset=1&cid=' . $value, | |
725 | $this->_absoluteUrl | |
726 | ); | |
727 | ||
a8c87952 EM |
728 | $rows[$rowNum]['civicrm_contact_sort_name'] = "<a href='$url'>" . $row['civicrm_contact_sort_name'] . |
729 | '</a>'; | |
6a488035 | 730 | } |
0555ce4f WM |
731 | |
732 | // store the contact ID of this contact | |
733 | $previousContact = $value; | |
6a488035 TO |
734 | $entryFound = TRUE; |
735 | } | |
736 | } | |
737 | ||
738 | if (array_key_exists('civicrm_activity_activity_type_id', $row)) { | |
739 | if ($value = $row['civicrm_activity_activity_type_id']) { | |
740 | ||
741 | $value = explode(',', $value); | |
742 | foreach ($value as $key => $id) { | |
743 | $value[$key] = $activityType[$id]; | |
744 | } | |
745 | ||
746 | $rows[$rowNum]['civicrm_activity_activity_type_id'] = implode(' , ', $value); | |
747 | $entryFound = TRUE; | |
748 | } | |
749 | } | |
750 | ||
96a1ef05 SB |
751 | if (array_key_exists('civicrm_activity_status_id', $row)) { |
752 | if ($value = $row['civicrm_activity_status_id']) { | |
753 | $rows[$rowNum]['civicrm_activity_status_id'] = $activityStatus[$value]; | |
754 | $entryFound = TRUE; | |
755 | } | |
756 | } | |
757 | ||
da236f9a | 758 | if (array_key_exists('civicrm_activity_priority_id', $row)) { |
759 | if ($value = $row['civicrm_activity_priority_id']) { | |
760 | $rows[$rowNum]['civicrm_activity_priority_id'] = $priority[$value]; | |
761 | $entryFound = TRUE; | |
762 | } | |
763 | } | |
764 | ||
e6143637 KJ |
765 | if (array_key_exists('civicrm_activity_duration', $row)) { |
766 | if ($value = $row['civicrm_activity_duration']) { | |
96495cc0 | 767 | $rows[$rowNum]['civicrm_activity_duration'] = $rows[$rowNum]['civicrm_activity_duration_total']; |
e6143637 KJ |
768 | $entryFound = TRUE; |
769 | } | |
770 | } | |
771 | ||
6a488035 TO |
772 | if (!$entryFound) { |
773 | break; | |
774 | } | |
775 | } | |
776 | } | |
96025800 | 777 | |
6a488035 | 778 | } |