4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.4 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2013
36 class CRM_Report_Form_ActivitySummary
extends CRM_Report_Form
{
38 protected $_emailField = FALSE;
39 protected $_phoneField = FALSE;
41 function __construct() {
42 $this->_columns
= array(
45 'dao' => 'CRM_Contact_DAO_Contact',
54 array('title' => ts('Contact Name'),
62 array('title' => ts('Contact Name'),
70 'title' => ts('Contact'),
77 array('title' => ts('Contact Name')),
79 'grouping' => 'contact-fields',
83 'dao' => 'CRM_Core_DAO_Email',
95 array('title' => ts('Email')),
97 'grouping' => 'contact-fields',
101 'dao' => 'CRM_Core_DAO_Email',
105 array('title' => 'Phone'),
107 'grouping' => 'contact-fields',
109 'civicrm_activity' =>
111 'dao' => 'CRM_Activity_DAO_Activity',
114 'activity_type_id' =>
115 array('title' => ts('Activity Type'),
117 'type' => CRM_Utils_Type
::T_STRING
,
121 'title' => 'Duration',
124 'sum' => ts('Total Duration'),
129 'title' => 'Total Activities',
133 'count' => ts('Activity Count'),
139 'activity_date_time' =>
140 array('operatorType' => CRM_Report_Form
::OP_DATE
),
141 'activity_type_id' =>
142 array('title' => ts('Activity Type'),
143 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
144 'options' => CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, FALSE, 'label', TRUE),
147 array('title' => ts('Activity Status'),
148 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
149 'options' => CRM_Core_PseudoConstant
::activityStatus(),
152 array('title' => ts('Priority'),
153 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
154 'options' => CRM_Core_PseudoConstant
::get('CRM_Activity_DAO_Activity', 'priority_id'),
159 'activity_date_time' =>
160 array('title' => ts('Activity Date'),
163 'activity_type_id' =>
164 array('title' => ts('Activity Type'),
170 'activity_date_time' =>
171 array('title' => ts('Activity Date')),
172 'activity_type_id' =>
173 array('title' => ts('Activity Type')),
175 'grouping' => 'activity-fields',
176 'alias' => 'activity',
180 parent
::__construct();
185 $this->_columnHeaders
= array();
186 foreach ($this->_columns
as $tableName => $table) {
187 if (array_key_exists('group_bys', $table)) {
188 foreach ($table['group_bys'] as $fieldName => $field) {
189 if (CRM_Utils_Array
::value($fieldName, $this->_params
['group_bys'])) {
191 switch (CRM_Utils_Array
::value($fieldName, $this->_params
['group_bys_freq'])) {
193 $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL WEEKDAY({$field['dbAlias']}) DAY) AS {$tableName}_{$fieldName}_start";
195 $select[] = "YEARWEEK({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
196 $select[] = "WEEKOFYEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
197 $field['title'] = 'Week';
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';
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';
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';
221 if (CRM_Utils_Array
::value($fieldName, $this->_params
['group_bys_freq'])) {
222 $this->_interval
= $field['title'];
223 $this->_columnHeaders
["{$tableName}_{$fieldName}_start"]['title'] = $field['title'] . ' Beginning';
224 $this->_columnHeaders
["{$tableName}_{$fieldName}_start"]['type'] = $field['type'];
225 $this->_columnHeaders
["{$tableName}_{$fieldName}_start"]['group_by'] = $this->_params
['group_bys_freq'][$fieldName];
227 // just to make sure these values are transfered to rows.
228 // since we need that for calculation purpose,
229 // e.g making subtotals look nicer or graphs
230 $this->_columnHeaders
["{$tableName}_{$fieldName}_interval"] = array('no_display' => TRUE);
231 $this->_columnHeaders
["{$tableName}_{$fieldName}_subtotal"] = array('no_display' => TRUE);
236 if (array_key_exists('fields', $table)) {
237 foreach ($table['fields'] as $fieldName => $field) {
238 if (CRM_Utils_Array
::value('required', $field) ||
239 CRM_Utils_Array
::value($fieldName, $this->_params
['fields'])
241 if ($tableName == 'civicrm_email') {
242 $this->_emailField
= TRUE;
244 if ($tableName == 'civicrm_phone') {
245 $this->_phoneField
= TRUE;
247 if (CRM_Utils_Array
::value('statistics', $field)) {
248 foreach ($field['statistics'] as $stat => $label) {
249 switch (strtolower($stat)) {
251 $select[] = "COUNT(DISTINCT({$field['dbAlias']})) as {$tableName}_{$fieldName}_{$stat}";
252 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type
::T_INT
;
253 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
254 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
258 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
259 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type
::T_INT
;
260 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
261 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
266 elseif ($fieldName == 'activity_type_id') {
267 if (!CRM_Utils_Array
::value('activity_type_id', $this->_params
['group_bys'])) {
268 $select[] = "GROUP_CONCAT(DISTINCT {$field['dbAlias']} ORDER BY {$field['dbAlias']} ) as {$tableName}_{$fieldName}";
271 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
273 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
274 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
275 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['no_display'] = CRM_Utils_Array
::value('no_display', $field);
278 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
279 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
280 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
281 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['no_display'] = CRM_Utils_Array
::value('no_display', $field);
288 $this->_select
= "SELECT " . implode(', ', $select) . " ";
292 $activityContacts = CRM_Core_OptionGroup
::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
293 $assigneeID = CRM_Utils_Array
::key('Activity Assignees', $activityContacts);
294 $targetID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
295 $sourceID = CRM_Utils_Array
::key('Activity Source', $activityContacts);
298 FROM civicrm_activity {$this->_aliases['civicrm_activity']}
300 LEFT JOIN civicrm_activity_contact target_activity
301 ON {$this->_aliases['civicrm_activity']}.id = target_activity.activity_id AND
302 target_activity.record_type_id = {$targetID}
303 LEFT JOIN civicrm_activity_contact assignment_activity
304 ON {$this->_aliases['civicrm_activity']}.id = assignment_activity.activity_id AND
305 assignment_activity.record_type_id = {$assigneeID}
306 LEFT JOIN civicrm_activity_contact source_activity
307 ON {$this->_aliases['civicrm_activity']}.id = source_activity.activity_id AND
308 source_activity.record_type_id = {$sourceID}
309 LEFT JOIN civicrm_contact contact_civireport
310 ON target_activity.contact_id = contact_civireport.id
311 LEFT JOIN civicrm_contact civicrm_contact_assignee
312 ON assignment_activity.contact_id = civicrm_contact_assignee.id
313 LEFT JOIN civicrm_contact civicrm_contact_source
314 ON source_activity.contact_id = civicrm_contact_source.id
316 LEFT JOIN civicrm_option_value
317 ON ( {$this->_aliases['civicrm_activity']}.activity_type_id = civicrm_option_value.value )
318 LEFT JOIN civicrm_option_group
319 ON civicrm_option_group.id = civicrm_option_value.option_group_id
320 LEFT JOIN civicrm_case_activity
321 ON civicrm_case_activity.activity_id = {$this->_aliases['civicrm_activity']}.id
322 LEFT JOIN civicrm_case
323 ON civicrm_case_activity.case_id = civicrm_case.id
324 LEFT JOIN civicrm_case_contact
325 ON civicrm_case_contact.case_id = civicrm_case.id ";
327 if ($this->_emailField
) {
329 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
330 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
331 {$this->_aliases['civicrm_email']}.is_primary = 1 ";
334 if ($this->_phoneField
) {
336 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
337 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
338 {$this->_aliases['civicrm_phone']}.is_primary = 1 ";
343 $this->_where
= " WHERE civicrm_option_group.name = 'activity_type' AND
344 {$this->_aliases['civicrm_activity']}.is_test = 0 AND
345 {$this->_aliases['civicrm_activity']}.is_deleted = 0 AND
346 {$this->_aliases['civicrm_activity']}.is_current_revision = 1";
349 foreach ($this->_columns
as $tableName => $table) {
350 if (array_key_exists('filters', $table)) {
352 foreach ($table['filters'] as $fieldName => $field) {
354 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
355 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
356 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
357 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
359 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
362 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
364 $clause = $this->whereClause($field,
366 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
367 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
368 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
373 if (!empty($clause)) {
374 $clauses[] = $clause;
380 if (empty($clauses)) {
381 $this->_where
.= " ";
384 $this->_where
.= " AND " . implode(' AND ', $clauses);
387 if ($this->_aclWhere
) {
388 $this->_where
.= " AND {$this->_aclWhere} ";
393 $this->_groupBy
= array();
394 if (is_array($this->_params
['group_bys']) &&
395 !empty($this->_params
['group_bys'])
397 foreach ($this->_columns
as $tableName => $table) {
398 if (array_key_exists('group_bys', $table)) {
399 foreach ($table['group_bys'] as $fieldName => $field) {
400 if (CRM_Utils_Array
::value($fieldName, $this->_params
['group_bys'])) {
401 if (CRM_Utils_Array
::value('chart', $field)) {
402 $this->assign('chartSupported', TRUE);
404 if (CRM_Utils_Array
::value('frequency', $table['group_bys'][$fieldName]) &&
405 CRM_Utils_Array
::value($fieldName, $this->_params
['group_bys_freq'])
408 $append = "YEAR({$field['dbAlias']}),";
409 if (in_array(strtolower($this->_params
['group_bys_freq'][$fieldName]),
414 $this->_groupBy
[] = "$append {$this->_params['group_bys_freq'][$fieldName]}({$field['dbAlias']})";
418 $this->_groupBy
[] = $field['dbAlias'];
425 $this->_groupBy
= "GROUP BY " . implode(', ', $this->_groupBy
);
428 $this->_groupBy
= "GROUP BY {$this->_aliases['civicrm_contact']}.id ";
432 function formRule($fields, $files, $self) {
434 $contactFields = array('sort_name', 'email', 'phone');
435 if (CRM_Utils_Array
::value('group_bys', $fields)) {
437 if (CRM_Utils_Array
::value('activity_type_id', $fields['group_bys']) &&
438 !CRM_Utils_Array
::value('sort_name', $fields['group_bys'])
440 foreach ($fields['fields'] as $fieldName => $val) {
441 if (in_array($fieldName, $contactFields)) {
442 $errors['fields'] = ts("Please select GroupBy 'Contact' to display Contact Fields");
448 if (CRM_Utils_Array
::value('activity_date_time', $fields['group_bys'])) {
449 if (CRM_Utils_Array
::value('sort_name', $fields['group_bys'])) {
450 $errors['fields'] = ts("Please do not select GroupBy 'Activity Date' with GroupBy 'Contact'");
453 foreach ($fields['fields'] as $fieldName => $val) {
454 if (in_array($fieldName, $contactFields)) {
455 $errors['fields'] = ts("Please do not select any Contact Fields with GroupBy 'Activity Date'");
465 function postProcess() {
466 // get the acl clauses built before we assemble the query
467 $this->buildACLClause($this->_aliases
['civicrm_contact']);
468 parent
::postProcess();
471 function alterDisplay(&$rows) {
472 // custom code to alter rows
475 $activityType = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, FALSE, 'label', TRUE);
478 $onHover = ts('View Contact Summary for this Contact');
479 foreach ($rows as $rowNum => $row) {
481 if (array_key_exists('civicrm_contact_sort_name', $row) && $this->_outputMode
!= 'csv') {
482 if ($value = $row['civicrm_contact_id']) {
485 $priviousContact = $value;
488 if ($priviousContact == $value) {
490 $priviousContact = $value;
494 $priviousContact = $value;
498 if ($flagContact == 1) {
499 $rows[$rowNum]['civicrm_contact_sort_name'] = "";
501 if (array_key_exists('civicrm_email_email', $row)) {
502 $rows[$rowNum]['civicrm_email_email'] = "";
504 if (array_key_exists('civicrm_phone_phone', $row)) {
505 $rows[$rowNum]['civicrm_phone_phone'] = "";
509 $url = CRM_Utils_System
::url('civicrm/contact/view',
510 'reset=1&cid=' . $value,
514 $rows[$rowNum]['civicrm_contact_sort_name'] = "<a href='$url'>" . $row['civicrm_contact_sort_name'] . '</a>';
520 if (array_key_exists('civicrm_activity_activity_type_id', $row)) {
521 if ($value = $row['civicrm_activity_activity_type_id']) {
523 $value = explode(',', $value);
524 foreach ($value as $key => $id) {
525 $value[$key] = $activityType[$id];
528 $rows[$rowNum]['civicrm_activity_activity_type_id'] = implode(' , ', $value);