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; | |
430ae6dd | 23 | |
74cf4551 | 24 | /** |
1728e9a0 | 25 | * This report has not been optimised for group filtering. |
26 | * | |
27 | * The functionality for group filtering has been improved but not | |
28 | * all reports have been adjusted to take care of it. This report has not | |
29 | * and will run an inefficient query until fixed. | |
30 | * | |
31 | * CRM-19170 | |
32 | * | |
33 | * @var bool | |
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 | ||
431 | if ($applyLimit && empty($this->_params['charts'])) { | |
432 | $this->limit(); | |
433 | } | |
434 | CRM_Utils_Hook::alterReportVar('sql', $this, $this); | |
435 | ||
d5625bc9 DJ |
436 | // build temporary table column names base on column headers of result |
437 | $dbColumns = []; | |
438 | foreach ($this->_columnHeaders as $fieldName => $dontCare) { | |
439 | $dbColumns[] = $fieldName . ' VARCHAR(128)'; | |
440 | } | |
441 | ||
a6d5eae9 DJ |
442 | // Order by & Section columns not selected for display need to be included in temp table. |
443 | foreach ($unselectedColumns as $alias => $section) { | |
444 | $dbColumns[] = $alias . ' VARCHAR(128)'; | |
445 | } | |
446 | ||
d5625bc9 DJ |
447 | // create temp table to store main result |
448 | $this->_tempTableName = $this->createTemporaryTable('tempTable', " | |
449 | id int unsigned NOT NULL AUTO_INCREMENT, " . implode(', ', $dbColumns) . ' , PRIMARY KEY (id)', | |
450 | TRUE); | |
451 | ||
452 | // build main report query | |
453 | $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy} {$this->_limit}"; | |
454 | $this->addToDeveloperTab($sql); | |
455 | ||
456 | // store the result in temporary table | |
457 | $insertCols = ''; | |
a6d5eae9 | 458 | $insertQuery = "INSERT INTO {$this->_tempTableName} ( " . implode(',', array_merge(array_keys($this->_columnHeaders), array_keys($unselectedColumns))) . " ) |
d5625bc9 | 459 | {$sql}"; |
04076b1b | 460 | CRM_Core_DAO::disableFullGroupByMode(); |
d5625bc9 | 461 | CRM_Core_DAO::executeQuery($insertQuery); |
04076b1b | 462 | CRM_Core_DAO::reenableFullGroupByMode(); |
d5625bc9 DJ |
463 | |
464 | // now build the query for duration sum | |
465 | $this->activityDurationFrom(); | |
466 | $this->where(TRUE); | |
467 | $this->groupBy(FALSE); | |
468 | ||
469 | // build the query to calulate duration sum | |
470 | $sql = "SELECT SUM(activity_civireport.duration) as civicrm_activity_duration_total {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy} {$this->_limit}"; | |
471 | ||
472 | // create temp table to store duration | |
473 | $this->_tempDurationSumTableName = $this->createTemporaryTable('tempDurationSumTable', " | |
474 | id int unsigned NOT NULL AUTO_INCREMENT, civicrm_activity_duration_total VARCHAR(128), PRIMARY KEY (id)", | |
475 | TRUE); | |
476 | ||
477 | // store the result in temporary table | |
478 | $insertQuery = "INSERT INTO {$this->_tempDurationSumTableName} (civicrm_activity_duration_total) | |
479 | {$sql}"; | |
6cc7af15 | 480 | CRM_Core_DAO::disableFullGroupByMode(); |
d5625bc9 | 481 | CRM_Core_DAO::executeQuery($insertQuery); |
6cc7af15 | 482 | CRM_Core_DAO::reenableFullGroupByMode(); |
d5625bc9 DJ |
483 | |
484 | $sql = "SELECT {$this->_tempTableName}.*, {$this->_tempDurationSumTableName}.civicrm_activity_duration_total | |
485 | FROM {$this->_tempTableName} INNER JOIN {$this->_tempDurationSumTableName} | |
486 | ON ({$this->_tempTableName}.id = {$this->_tempDurationSumTableName}.id)"; | |
487 | ||
488 | // finally add duration total to column headers | |
489 | $this->_columnHeaders['civicrm_activity_duration_total'] = ['no_display' => 1]; | |
490 | ||
491 | // reset the sql building to default, which is used / called during other actions like "add to group" | |
492 | $this->from(); | |
493 | $this->where(); | |
494 | ||
495 | return $sql; | |
496 | } | |
497 | ||
bc854509 | 498 | /** |
499 | * Group the fields. | |
500 | * | |
501 | * @param bool $includeSelectCol | |
502 | */ | |
d1641c51 | 503 | public function groupBy($includeSelectCol = TRUE) { |
be2fb01f | 504 | $this->_groupBy = []; |
229d3c71 | 505 | if (!empty($this->_params['group_bys']) && |
96495cc0 | 506 | is_array($this->_params['group_bys'])) { |
6a488035 TO |
507 | foreach ($this->_columns as $tableName => $table) { |
508 | if (array_key_exists('group_bys', $table)) { | |
509 | foreach ($table['group_bys'] as $fieldName => $field) { | |
a7488080 CW |
510 | if (!empty($this->_params['group_bys'][$fieldName])) { |
511 | if (!empty($field['chart'])) { | |
6a488035 TO |
512 | $this->assign('chartSupported', TRUE); |
513 | } | |
a8c87952 EM |
514 | if (!empty($table['group_bys'][$fieldName]['frequency']) && |
515 | !empty($this->_params['group_bys_freq'][$fieldName]) | |
516 | ) { | |
6a488035 TO |
517 | |
518 | $append = "YEAR({$field['dbAlias']}),"; | |
519 | if (in_array(strtolower($this->_params['group_bys_freq'][$fieldName]), | |
be2fb01f | 520 | ['year'] |
a8c87952 | 521 | )) { |
6a488035 TO |
522 | $append = ''; |
523 | } | |
524 | $this->_groupBy[] = "$append {$this->_params['group_bys_freq'][$fieldName]}({$field['dbAlias']})"; | |
525 | $append = TRUE; | |
526 | } | |
527 | else { | |
528 | $this->_groupBy[] = $field['dbAlias']; | |
529 | } | |
530 | } | |
531 | } | |
532 | } | |
533 | } | |
d1641c51 | 534 | $groupBy = $this->_groupBy; |
6a488035 TO |
535 | $this->_groupBy = "GROUP BY " . implode(', ', $this->_groupBy); |
536 | } | |
537 | else { | |
d1641c51 | 538 | $groupBy = "{$this->_aliases['civicrm_activity']}.id"; |
96495cc0 | 539 | $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_activity']}.id "; |
6a488035 | 540 | } |
d1641c51 | 541 | if ($includeSelectCol) { |
b708c08d | 542 | $this->_groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, $groupBy); |
d1641c51 | 543 | } |
6a488035 TO |
544 | } |
545 | ||
74cf4551 EM |
546 | /** |
547 | * @param $fields | |
548 | * @param $files | |
549 | * @param $self | |
550 | * | |
551 | * @return array | |
552 | */ | |
52628c99 | 553 | public static function formRule($fields, $files, $self) { |
be2fb01f CW |
554 | $errors = []; |
555 | $contactFields = ['sort_name', 'email', 'phone']; | |
a7488080 | 556 | if (!empty($fields['group_bys'])) { |
a7488080 CW |
557 | if (!empty($fields['group_bys']['activity_date_time'])) { |
558 | if (!empty($fields['group_bys']['sort_name'])) { | |
6a488035 TO |
559 | $errors['fields'] = ts("Please do not select GroupBy 'Activity Date' with GroupBy 'Contact'"); |
560 | } | |
561 | else { | |
562 | foreach ($fields['fields'] as $fieldName => $val) { | |
563 | if (in_array($fieldName, $contactFields)) { | |
564 | $errors['fields'] = ts("Please do not select any Contact Fields with GroupBy 'Activity Date'"); | |
565 | break; | |
566 | } | |
567 | } | |
568 | } | |
569 | } | |
570 | } | |
d5c61232 KJ |
571 | |
572 | // don't allow add to group action unless contact fields are selected. | |
573 | if (isset($fields['_qf_ActivitySummary_submit_group'])) { | |
574 | $contactFieldSelected = FALSE; | |
575 | foreach ($fields['fields'] as $fieldName => $val) { | |
576 | if (in_array($fieldName, $contactFields)) { | |
577 | $contactFieldSelected = TRUE; | |
578 | break; | |
579 | } | |
580 | } | |
581 | ||
582 | if (!$contactFieldSelected) { | |
583 | $errors['fields'] = ts('You cannot use "Add Contacts to Group" action unless contacts fields are selected.'); | |
584 | } | |
585 | } | |
6a488035 TO |
586 | return $errors; |
587 | } | |
588 | ||
96a1ef05 SB |
589 | /** |
590 | * @param $rows | |
591 | * | |
592 | * @return array | |
593 | */ | |
594 | public function statistics(&$rows) { | |
595 | $statistics = parent::statistics($rows); | |
596 | $totalType = $totalActivity = $totalDuration = 0; | |
597 | ||
96495cc0 KJ |
598 | $query = "SELECT {$this->_tempTableName}.civicrm_activity_activity_type_id, |
599 | {$this->_tempTableName}.civicrm_activity_id_count, | |
600 | {$this->_tempDurationSumTableName}.civicrm_activity_duration_total | |
601 | FROM {$this->_tempTableName} INNER JOIN {$this->_tempDurationSumTableName} | |
602 | ON ({$this->_tempTableName}.id = {$this->_tempDurationSumTableName}.id)"; | |
96a1ef05 | 603 | |
96495cc0 | 604 | $actDAO = CRM_Core_DAO::executeQuery($query); |
028df0a9 | 605 | |
be2fb01f | 606 | $activityTypesCount = []; |
d5c61232 | 607 | while ($actDAO->fetch()) { |
96495cc0 KJ |
608 | if (!in_array($actDAO->civicrm_activity_activity_type_id, $activityTypesCount)) { |
609 | $activityTypesCount[] = $actDAO->civicrm_activity_activity_type_id; | |
610 | } | |
611 | ||
612 | $totalActivity += $actDAO->civicrm_activity_id_count; | |
613 | $totalDuration += $actDAO->civicrm_activity_duration_total; | |
96a1ef05 SB |
614 | } |
615 | ||
96495cc0 KJ |
616 | $totalType = count($activityTypesCount); |
617 | ||
be2fb01f | 618 | $statistics['counts']['type'] = [ |
96a1ef05 SB |
619 | 'title' => ts('Total Types'), |
620 | 'value' => $totalType, | |
be2fb01f CW |
621 | ]; |
622 | $statistics['counts']['activities'] = [ | |
96a1ef05 SB |
623 | 'title' => ts('Total Number of Activities'), |
624 | 'value' => $totalActivity, | |
be2fb01f CW |
625 | ]; |
626 | $statistics['counts']['duration'] = [ | |
96a1ef05 SB |
627 | 'title' => ts('Total Duration (in Minutes)'), |
628 | 'value' => $totalDuration, | |
be2fb01f | 629 | ]; |
96a1ef05 SB |
630 | return $statistics; |
631 | } | |
632 | ||
fdee8d8d SB |
633 | public function modifyColumnHeaders() { |
634 | //CRM-16719 modify name of column | |
635 | if (!empty($this->_columnHeaders['civicrm_activity_status_id'])) { | |
ccc29f8e | 636 | $this->_columnHeaders['civicrm_activity_status_id']['title'] = ts('Status'); |
fdee8d8d SB |
637 | } |
638 | } | |
639 | ||
74cf4551 | 640 | /** |
4b62bc4f EM |
641 | * Alter display of rows. |
642 | * | |
643 | * Iterate through the rows retrieved via SQL and make changes for display purposes, | |
644 | * such as rendering contacts as links. | |
645 | * | |
646 | * @param array $rows | |
647 | * Rows generated by SQL, with an array for each row. | |
74cf4551 | 648 | */ |
00be9182 | 649 | public function alterDisplay(&$rows) { |
a8c87952 | 650 | $entryFound = FALSE; |
6a488035 | 651 | $activityType = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE); |
96a1ef05 | 652 | $activityStatus = CRM_Core_PseudoConstant::activityStatus(); |
da236f9a | 653 | $priority = CRM_Core_PseudoConstant::get('CRM_Activity_DAO_Activity', 'priority_id'); |
6a488035 TO |
654 | $onHover = ts('View Contact Summary for this Contact'); |
655 | foreach ($rows as $rowNum => $row) { | |
96a1ef05 SB |
656 | // make count columns point to activity detail report |
657 | if (!empty($row['civicrm_activity_id_count'])) { | |
be2fb01f CW |
658 | $url = []; |
659 | $urlParams = ['activity_type_id', 'gid', 'status_id', 'contact_id']; | |
fdee8d8d | 660 | foreach ($urlParams as $field) { |
3ff6c3c0 | 661 | if (!empty($row['civicrm_activity_' . $field])) { |
96a1ef05 SB |
662 | $url[] = "{$field}_op=in&{$field}_value={$row['civicrm_activity_'.$field]}"; |
663 | } | |
3ff6c3c0 SB |
664 | elseif (!empty($this->_params[$field . '_value'])) { |
665 | $val = implode(",", $this->_params[$field . '_value']); | |
fdee8d8d SB |
666 | $url[] = "{$field}_op=in&{$field}_value={$val}"; |
667 | } | |
668 | } | |
be2fb01f | 669 | $date_suffixes = ['relative', 'from', 'to']; |
201312d6 | 670 | foreach ($date_suffixes as $suffix) { |
fdee8d8d | 671 | if (!empty($this->_params['activity_date_time_' . $suffix])) { |
96a1ef05 SB |
672 | list($from, $to) |
673 | = $this->getFromTo( | |
674 | CRM_Utils_Array::value("activity_date_time_relative", $this->_params), | |
675 | CRM_Utils_Array::value("activity_date_time_from", $this->_params), | |
676 | CRM_Utils_Array::value("activity_date_time_to", $this->_params) | |
3ff6c3c0 | 677 | ); |
96a1ef05 | 678 | $url[] = "activity_date_time_from={$from}&activity_date_time_to={$to}"; |
fdee8d8d | 679 | break; |
96a1ef05 SB |
680 | } |
681 | } | |
8f621a01 | 682 | // reset date filter on activity reports. |
683 | $url[] = "resetDateFilter=1"; | |
3ff6c3c0 SB |
684 | $url = implode('&', $url); |
685 | $url = CRM_Report_Utils_Report::getNextUrl('activity', "reset=1&force=1&{$url}", | |
96a1ef05 SB |
686 | $this->_absoluteUrl, |
687 | $this->_id, | |
688 | $this->_drilldownReport); | |
689 | $rows[$rowNum]['civicrm_activity_id_count_link'] = $url; | |
690 | $rows[$rowNum]['civicrm_activity_id_count_hover'] = ts('List all activity(s) for this row.'); | |
691 | $entryFound = TRUE; | |
692 | } | |
6a488035 TO |
693 | |
694 | if (array_key_exists('civicrm_contact_sort_name', $row) && $this->_outputMode != 'csv') { | |
695 | if ($value = $row['civicrm_contact_id']) { | |
696 | ||
0555ce4f WM |
697 | // unset the name, email and phone fields if the contact is the same as the previous contact |
698 | if (isset($previousContact) && $previousContact == $value) { | |
6a488035 TO |
699 | $rows[$rowNum]['civicrm_contact_sort_name'] = ""; |
700 | ||
701 | if (array_key_exists('civicrm_email_email', $row)) { | |
702 | $rows[$rowNum]['civicrm_email_email'] = ""; | |
703 | } | |
704 | if (array_key_exists('civicrm_phone_phone', $row)) { | |
705 | $rows[$rowNum]['civicrm_phone_phone'] = ""; | |
706 | } | |
707 | } | |
708 | else { | |
709 | $url = CRM_Utils_System::url('civicrm/contact/view', | |
710 | 'reset=1&cid=' . $value, | |
711 | $this->_absoluteUrl | |
712 | ); | |
713 | ||
a8c87952 EM |
714 | $rows[$rowNum]['civicrm_contact_sort_name'] = "<a href='$url'>" . $row['civicrm_contact_sort_name'] . |
715 | '</a>'; | |
6a488035 | 716 | } |
0555ce4f WM |
717 | |
718 | // store the contact ID of this contact | |
719 | $previousContact = $value; | |
6a488035 TO |
720 | $entryFound = TRUE; |
721 | } | |
722 | } | |
723 | ||
724 | if (array_key_exists('civicrm_activity_activity_type_id', $row)) { | |
725 | if ($value = $row['civicrm_activity_activity_type_id']) { | |
726 | ||
727 | $value = explode(',', $value); | |
728 | foreach ($value as $key => $id) { | |
729 | $value[$key] = $activityType[$id]; | |
730 | } | |
731 | ||
732 | $rows[$rowNum]['civicrm_activity_activity_type_id'] = implode(' , ', $value); | |
733 | $entryFound = TRUE; | |
734 | } | |
735 | } | |
736 | ||
96a1ef05 SB |
737 | if (array_key_exists('civicrm_activity_status_id', $row)) { |
738 | if ($value = $row['civicrm_activity_status_id']) { | |
739 | $rows[$rowNum]['civicrm_activity_status_id'] = $activityStatus[$value]; | |
740 | $entryFound = TRUE; | |
741 | } | |
742 | } | |
743 | ||
da236f9a | 744 | if (array_key_exists('civicrm_activity_priority_id', $row)) { |
745 | if ($value = $row['civicrm_activity_priority_id']) { | |
746 | $rows[$rowNum]['civicrm_activity_priority_id'] = $priority[$value]; | |
747 | $entryFound = TRUE; | |
748 | } | |
749 | } | |
750 | ||
e6143637 KJ |
751 | if (array_key_exists('civicrm_activity_duration', $row)) { |
752 | if ($value = $row['civicrm_activity_duration']) { | |
96495cc0 | 753 | $rows[$rowNum]['civicrm_activity_duration'] = $rows[$rowNum]['civicrm_activity_duration_total']; |
e6143637 KJ |
754 | $entryFound = TRUE; |
755 | } | |
756 | } | |
757 | ||
6a488035 TO |
758 | if (!$entryFound) { |
759 | break; | |
760 | } | |
761 | } | |
762 | } | |
96025800 | 763 | |
6a488035 | 764 | } |