Merge pull request #3880 from rohankatkar/webtestCRM-15108
[civicrm-core.git] / CRM / Report / Form / ActivitySummary.php
CommitLineData
6a488035 1<?php
6a488035
TO
2
3/*
4 +--------------------------------------------------------------------+
06b69b18 5 | CiviCRM version 4.5 |
6a488035 6 +--------------------------------------------------------------------+
06b69b18 7 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27*/
28
29/**
30 *
31 * @package CRM
06b69b18 32 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
33 * $Id$
34 *
35 */
36class CRM_Report_Form_ActivitySummary extends CRM_Report_Form {
37
38 protected $_emailField = FALSE;
430ae6dd
TO
39 protected $_phoneField = FALSE;
40
74cf4551
EM
41 /**
42 *
43 */
44 /**
45 *
46 */
430ae6dd 47 function __construct() {
6a488035
TO
48 $this->_columns = array(
49 'civicrm_contact' =>
50 array(
51 'dao' => 'CRM_Contact_DAO_Contact',
52 'fields' =>
53 array(
54 'id' =>
55 array(
56 'required' => TRUE,
57 'no_display' => TRUE,
58 ),
59 'sort_name' =>
60 array('title' => ts('Contact Name'),
61 'default' => TRUE,
62 'no_repeat' => TRUE,
63 ),
64 ),
65 'filters' =>
66 array(
67 'sort_name' =>
68 array('title' => ts('Contact Name'),
69 ),
70 ),
71 'group_bys' =>
72 array(
73 'sort_name' =>
74 array(
75 'name' => 'id',
76 'title' => ts('Contact'),
77 'default' => TRUE,
78 ),
79 ),
80 'order_bys' =>
81 array(
82 'sort_name' =>
83 array('title' => ts('Contact Name')),
84 ),
85 'grouping' => 'contact-fields',
86 ),
87 'civicrm_email' =>
88 array(
89 'dao' => 'CRM_Core_DAO_Email',
90 'fields' =>
91 array(
92 'email' =>
93 array(
94 'title' => 'Email',
95 'default' => TRUE,
96 ),
97 ),
98 'order_bys' =>
99 array(
100 'email' =>
101 array('title' => ts('Email')),
102 ),
103 'grouping' => 'contact-fields',
104 ),
105 'civicrm_phone' =>
106 array(
107 'dao' => 'CRM_Core_DAO_Email',
108 'fields' =>
109 array(
110 'phone' =>
111 array('title' => 'Phone'),
112 ),
113 'grouping' => 'contact-fields',
114 ),
115 'civicrm_activity' =>
116 array(
117 'dao' => 'CRM_Activity_DAO_Activity',
118 'fields' =>
119 array(
120 'activity_type_id' =>
121 array('title' => ts('Activity Type'),
122 'default' => TRUE,
123 'type' => CRM_Utils_Type::T_STRING,
124 ),
125 'duration' =>
126 array(
127 'title' => 'Duration',
128 'statistics' =>
129 array(
130 'sum' => ts('Total Duration'),
131 ),
132 ),
133 'id' =>
134 array(
135 'title' => 'Total Activities',
136 'required' => TRUE,
137 'statistics' =>
138 array(
139 'count' => ts('Activity Count'),
140 ),
141 ),
142 ),
143 'filters' =>
144 array(
145 'activity_date_time' =>
146 array('operatorType' => CRM_Report_Form::OP_DATE),
147 'activity_type_id' =>
148 array('title' => ts('Activity Type'),
149 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
150 'options' => CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE),
151 ),
152 'status_id' =>
153 array('title' => ts('Activity Status'),
154 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
155 'options' => CRM_Core_PseudoConstant::activityStatus(),
156 ),
157 'priority_id' =>
158 array('title' => ts('Priority'),
159 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
cbf48754 160 'options' => CRM_Core_PseudoConstant::get('CRM_Activity_DAO_Activity', 'priority_id'),
6a488035
TO
161 ),
162 ),
163 'group_bys' =>
164 array(
165 'activity_date_time' =>
166 array('title' => ts('Activity Date'),
167 'frequency' => TRUE,
168 ),
169 'activity_type_id' =>
170 array('title' => ts('Activity Type'),
171 'default' => TRUE,
172 ),
173 ),
174 'order_bys' =>
175 array(
176 'activity_date_time' =>
177 array('title' => ts('Activity Date')),
178 'activity_type_id' =>
179 array('title' => ts('Activity Type')),
180 ),
181 'grouping' => 'activity-fields',
182 'alias' => 'activity',
183 ),
184 );
185
186 parent::__construct();
187 }
188
189 function select() {
190 $select = array();
191 $this->_columnHeaders = array();
192 foreach ($this->_columns as $tableName => $table) {
193 if (array_key_exists('group_bys', $table)) {
194 foreach ($table['group_bys'] as $fieldName => $field) {
a7488080 195 if (!empty($this->_params['group_bys'][$fieldName])) {
6a488035
TO
196
197 switch (CRM_Utils_Array::value($fieldName, $this->_params['group_bys_freq'])) {
198 case 'YEARWEEK':
199 $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL WEEKDAY({$field['dbAlias']}) DAY) AS {$tableName}_{$fieldName}_start";
200
201 $select[] = "YEARWEEK({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
202 $select[] = "WEEKOFYEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
203 $field['title'] = 'Week';
204 break;
205
206 case 'YEAR':
207 $select[] = "MAKEDATE(YEAR({$field['dbAlias']}), 1) AS {$tableName}_{$fieldName}_start";
208 $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
209 $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
210 $field['title'] = 'Year';
211 break;
212
213 case 'MONTH':
214 $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL (DAYOFMONTH({$field['dbAlias']})-1) DAY) as {$tableName}_{$fieldName}_start";
215 $select[] = "MONTH({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
216 $select[] = "MONTHNAME({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
217 $field['title'] = 'Month';
218 break;
219
220 case 'QUARTER':
221 $select[] = "STR_TO_DATE(CONCAT( 3 * QUARTER( {$field['dbAlias']} ) -2 , '/', '1', '/', YEAR( {$field['dbAlias']} ) ), '%m/%d/%Y') AS {$tableName}_{$fieldName}_start";
222 $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
223 $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
224 $field['title'] = 'Quarter';
225 break;
226 }
a7488080 227 if (!empty($this->_params['group_bys_freq'][$fieldName])) {
6a488035
TO
228 $this->_interval = $field['title'];
229 $this->_columnHeaders["{$tableName}_{$fieldName}_start"]['title'] = $field['title'] . ' Beginning';
230 $this->_columnHeaders["{$tableName}_{$fieldName}_start"]['type'] = $field['type'];
231 $this->_columnHeaders["{$tableName}_{$fieldName}_start"]['group_by'] = $this->_params['group_bys_freq'][$fieldName];
232
233 // just to make sure these values are transfered to rows.
234 // since we need that for calculation purpose,
235 // e.g making subtotals look nicer or graphs
236 $this->_columnHeaders["{$tableName}_{$fieldName}_interval"] = array('no_display' => TRUE);
237 $this->_columnHeaders["{$tableName}_{$fieldName}_subtotal"] = array('no_display' => TRUE);
238 }
239 }
240 }
241 }
242 if (array_key_exists('fields', $table)) {
243 foreach ($table['fields'] as $fieldName => $field) {
8cc574cf 244 if (!empty($field['required']) || !empty($this->_params['fields'][$fieldName])) {
6a488035
TO
245 if ($tableName == 'civicrm_email') {
246 $this->_emailField = TRUE;
247 }
248 if ($tableName == 'civicrm_phone') {
249 $this->_phoneField = TRUE;
250 }
a7488080 251 if (!empty($field['statistics'])) {
6a488035
TO
252 foreach ($field['statistics'] as $stat => $label) {
253 switch (strtolower($stat)) {
254 case 'count':
255 $select[] = "COUNT(DISTINCT({$field['dbAlias']})) as {$tableName}_{$fieldName}_{$stat}";
256 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type::T_INT;
257 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
258 $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}";
259 break;
260
261 case 'sum':
262 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
263 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type::T_INT;
264 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
265 $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}";
266 break;
267 }
268 }
269 }
270 elseif ($fieldName == 'activity_type_id') {
a7488080 271 if (empty($this->_params['group_bys']['activity_type_id'])) {
6a488035
TO
272 $select[] = "GROUP_CONCAT(DISTINCT {$field['dbAlias']} ORDER BY {$field['dbAlias']} ) as {$tableName}_{$fieldName}";
273 }
274 else {
275 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
276 }
277 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
278 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
279 $this->_columnHeaders["{$tableName}_{$fieldName}"]['no_display'] = CRM_Utils_Array::value('no_display', $field);
280 }
281 else {
282 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
283 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
284 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
285 $this->_columnHeaders["{$tableName}_{$fieldName}"]['no_display'] = CRM_Utils_Array::value('no_display', $field);
286 }
287 }
288 }
289 }
290 }
291
292 $this->_select = "SELECT " . implode(', ', $select) . " ";
293 }
294
295 function from() {
ecaec004
RN
296 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
297 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
298 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
299 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
6a488035
TO
300
301 $this->_from = "
302 FROM civicrm_activity {$this->_aliases['civicrm_activity']}
2f4c2f5d 303
ecaec004
RN
304 LEFT JOIN civicrm_activity_contact target_activity
305 ON {$this->_aliases['civicrm_activity']}.id = target_activity.activity_id AND
306 target_activity.record_type_id = {$targetID}
307 LEFT JOIN civicrm_activity_contact assignment_activity
308 ON {$this->_aliases['civicrm_activity']}.id = assignment_activity.activity_id AND
309 assignment_activity.record_type_id = {$assigneeID}
310 LEFT JOIN civicrm_activity_contact source_activity
311 ON {$this->_aliases['civicrm_activity']}.id = source_activity.activity_id AND
312 source_activity.record_type_id = {$sourceID}
313 LEFT JOIN civicrm_contact contact_civireport
314 ON target_activity.contact_id = contact_civireport.id
315 LEFT JOIN civicrm_contact civicrm_contact_assignee
316 ON assignment_activity.contact_id = civicrm_contact_assignee.id
317 LEFT JOIN civicrm_contact civicrm_contact_source
318 ON source_activity.contact_id = civicrm_contact_source.id
6a488035 319 {$this->_aclFrom}
2f4c2f5d 320 LEFT JOIN civicrm_option_value
6a488035 321 ON ( {$this->_aliases['civicrm_activity']}.activity_type_id = civicrm_option_value.value )
2f4c2f5d 322 LEFT JOIN civicrm_option_group
6a488035 323 ON civicrm_option_group.id = civicrm_option_value.option_group_id
2f4c2f5d 324 LEFT JOIN civicrm_case_activity
6a488035 325 ON civicrm_case_activity.activity_id = {$this->_aliases['civicrm_activity']}.id
2f4c2f5d 326 LEFT JOIN civicrm_case
6a488035 327 ON civicrm_case_activity.case_id = civicrm_case.id
2f4c2f5d 328 LEFT JOIN civicrm_case_contact
6a488035
TO
329 ON civicrm_case_contact.case_id = civicrm_case.id ";
330
331 if ($this->_emailField) {
332 $this->_from .= "
333 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
334 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
335 {$this->_aliases['civicrm_email']}.is_primary = 1 ";
336 }
337
338 if ($this->_phoneField) {
339 $this->_from .= "
340 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
341 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
342 {$this->_aliases['civicrm_phone']}.is_primary = 1 ";
343 }
344 }
345
346 function where() {
2f4c2f5d 347 $this->_where = " WHERE civicrm_option_group.name = 'activity_type' AND
6a488035
TO
348 {$this->_aliases['civicrm_activity']}.is_test = 0 AND
349 {$this->_aliases['civicrm_activity']}.is_deleted = 0 AND
350 {$this->_aliases['civicrm_activity']}.is_current_revision = 1";
351
352 $clauses = array();
353 foreach ($this->_columns as $tableName => $table) {
354 if (array_key_exists('filters', $table)) {
355
356 foreach ($table['filters'] as $fieldName => $field) {
357 $clause = NULL;
358 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
359 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
360 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
361 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
362
363 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
364 }
365 else {
366 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
367 if ($op) {
368 $clause = $this->whereClause($field,
369 $op,
370 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
371 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
372 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
373 );
374 }
375 }
376
377 if (!empty($clause)) {
378 $clauses[] = $clause;
379 }
380 }
381 }
382 }
383
384 if (empty($clauses)) {
385 $this->_where .= " ";
386 }
387 else {
388 $this->_where .= " AND " . implode(' AND ', $clauses);
389 }
390
391 if ($this->_aclWhere) {
392 $this->_where .= " AND {$this->_aclWhere} ";
393 }
394 }
395
396 function groupBy() {
397 $this->_groupBy = array();
398 if (is_array($this->_params['group_bys']) &&
399 !empty($this->_params['group_bys'])
400 ) {
401 foreach ($this->_columns as $tableName => $table) {
402 if (array_key_exists('group_bys', $table)) {
403 foreach ($table['group_bys'] as $fieldName => $field) {
a7488080
CW
404 if (!empty($this->_params['group_bys'][$fieldName])) {
405 if (!empty($field['chart'])) {
6a488035
TO
406 $this->assign('chartSupported', TRUE);
407 }
8cc574cf 408 if (!empty($table['group_bys'][$fieldName]['frequency']) && !empty($this->_params['group_bys_freq'][$fieldName])) {
6a488035
TO
409
410 $append = "YEAR({$field['dbAlias']}),";
411 if (in_array(strtolower($this->_params['group_bys_freq'][$fieldName]),
412 array('year')
413 )) {
414 $append = '';
415 }
416 $this->_groupBy[] = "$append {$this->_params['group_bys_freq'][$fieldName]}({$field['dbAlias']})";
417 $append = TRUE;
418 }
419 else {
420 $this->_groupBy[] = $field['dbAlias'];
421 }
422 }
423 }
424 }
425 }
426
427 $this->_groupBy = "GROUP BY " . implode(', ', $this->_groupBy);
428 }
429 else {
430 $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_contact']}.id ";
431 }
432 }
433
74cf4551
EM
434 /**
435 * @param $fields
436 * @param $files
437 * @param $self
438 *
439 * @return array
440 */
6a488035
TO
441 function formRule($fields, $files, $self) {
442 $errors = array();
443 $contactFields = array('sort_name', 'email', 'phone');
a7488080 444 if (!empty($fields['group_bys'])) {
6a488035 445
8cc574cf 446 if (!empty($fields['group_bys']['activity_type_id']) && empty($fields['group_bys']['sort_name'])) {
6a488035
TO
447 foreach ($fields['fields'] as $fieldName => $val) {
448 if (in_array($fieldName, $contactFields)) {
449 $errors['fields'] = ts("Please select GroupBy 'Contact' to display Contact Fields");
450 break;
451 }
452 }
453 }
454
a7488080
CW
455 if (!empty($fields['group_bys']['activity_date_time'])) {
456 if (!empty($fields['group_bys']['sort_name'])) {
6a488035
TO
457 $errors['fields'] = ts("Please do not select GroupBy 'Activity Date' with GroupBy 'Contact'");
458 }
459 else {
460 foreach ($fields['fields'] as $fieldName => $val) {
461 if (in_array($fieldName, $contactFields)) {
462 $errors['fields'] = ts("Please do not select any Contact Fields with GroupBy 'Activity Date'");
463 break;
464 }
465 }
466 }
467 }
468 }
469 return $errors;
470 }
471
472 function postProcess() {
473 // get the acl clauses built before we assemble the query
474 $this->buildACLClause($this->_aliases['civicrm_contact']);
475 parent::postProcess();
476 }
477
74cf4551
EM
478 /**
479 * @param $rows
480 */
6a488035
TO
481 function alterDisplay(&$rows) {
482 // custom code to alter rows
483
484 $entryFound = FALSE;
485 $activityType = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE);
486 $flagContact = 0;
487
488 $onHover = ts('View Contact Summary for this Contact');
489 foreach ($rows as $rowNum => $row) {
490
491 if (array_key_exists('civicrm_contact_sort_name', $row) && $this->_outputMode != 'csv') {
492 if ($value = $row['civicrm_contact_id']) {
493
494 if ($rowNum == 0) {
495 $priviousContact = $value;
496 }
497 else {
498 if ($priviousContact == $value) {
499 $flagContact = 1;
500 $priviousContact = $value;
501 }
502 else {
503 $flagContact = 0;
504 $priviousContact = $value;
505 }
506 }
507
508 if ($flagContact == 1) {
509 $rows[$rowNum]['civicrm_contact_sort_name'] = "";
510
511 if (array_key_exists('civicrm_email_email', $row)) {
512 $rows[$rowNum]['civicrm_email_email'] = "";
513 }
514 if (array_key_exists('civicrm_phone_phone', $row)) {
515 $rows[$rowNum]['civicrm_phone_phone'] = "";
516 }
517 }
518 else {
519 $url = CRM_Utils_System::url('civicrm/contact/view',
520 'reset=1&cid=' . $value,
521 $this->_absoluteUrl
522 );
523
524 $rows[$rowNum]['civicrm_contact_sort_name'] = "<a href='$url'>" . $row['civicrm_contact_sort_name'] . '</a>';
525 }
526 $entryFound = TRUE;
527 }
528 }
529
530 if (array_key_exists('civicrm_activity_activity_type_id', $row)) {
531 if ($value = $row['civicrm_activity_activity_type_id']) {
532
533 $value = explode(',', $value);
534 foreach ($value as $key => $id) {
535 $value[$key] = $activityType[$id];
536 }
537
538 $rows[$rowNum]['civicrm_activity_activity_type_id'] = implode(' , ', $value);
539 $entryFound = TRUE;
540 }
541 }
542
543 if (!$entryFound) {
544 break;
545 }
546 }
547 }
548}
549