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