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