4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
36 class CRM_Report_Form_ActivitySummary
extends CRM_Report_Form
{
38 protected $_emailField = FALSE;
39 protected $_phoneField = FALSE;
47 function __construct() {
48 $this->_columns
= array(
51 'dao' => 'CRM_Contact_DAO_Contact',
60 array('title' => ts('Contact Name'),
68 array('title' => ts('Contact Name'),
76 'title' => ts('Contact'),
83 array('title' => ts('Contact Name')),
85 'grouping' => 'contact-fields',
89 'dao' => 'CRM_Core_DAO_Email',
101 array('title' => ts('Email')),
103 'grouping' => 'contact-fields',
107 'dao' => 'CRM_Core_DAO_Email',
111 array('title' => 'Phone'),
113 'grouping' => 'contact-fields',
115 'civicrm_activity' =>
117 'dao' => 'CRM_Activity_DAO_Activity',
120 'activity_type_id' =>
121 array('title' => ts('Activity Type'),
123 'type' => CRM_Utils_Type
::T_STRING
,
127 'title' => 'Duration',
130 'sum' => ts('Total Duration'),
135 'title' => 'Total Activities',
139 'count' => ts('Activity Count'),
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),
153 array('title' => ts('Activity Status'),
154 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
155 'options' => CRM_Core_PseudoConstant
::activityStatus(),
158 array('title' => ts('Priority'),
159 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
160 'options' => CRM_Core_PseudoConstant
::get('CRM_Activity_DAO_Activity', 'priority_id'),
165 'activity_date_time' =>
166 array('title' => ts('Activity Date'),
169 'activity_type_id' =>
170 array('title' => ts('Activity Type'),
176 'activity_date_time' =>
177 array('title' => ts('Activity Date')),
178 'activity_type_id' =>
179 array('title' => ts('Activity Type')),
181 'grouping' => 'activity-fields',
182 'alias' => 'activity',
186 parent
::__construct();
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) {
195 if (!empty($this->_params
['group_bys'][$fieldName])) {
197 switch (CRM_Utils_Array
::value($fieldName, $this->_params
['group_bys_freq'])) {
199 $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL WEEKDAY({$field['dbAlias']}) DAY) AS {$tableName}_{$fieldName}_start";
201 $select[] = "YEARWEEK({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
202 $select[] = "WEEKOFYEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
203 $field['title'] = 'Week';
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';
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';
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';
227 if (!empty($this->_params
['group_bys_freq'][$fieldName])) {
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];
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);
242 if (array_key_exists('fields', $table)) {
243 foreach ($table['fields'] as $fieldName => $field) {
244 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
245 if ($tableName == 'civicrm_email') {
246 $this->_emailField
= TRUE;
248 if ($tableName == 'civicrm_phone') {
249 $this->_phoneField
= TRUE;
251 if (!empty($field['statistics'])) {
252 foreach ($field['statistics'] as $stat => $label) {
253 switch (strtolower($stat)) {
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}";
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}";
270 elseif ($fieldName == 'activity_type_id') {
271 if (empty($this->_params
['group_bys']['activity_type_id'])) {
272 $select[] = "GROUP_CONCAT(DISTINCT {$field['dbAlias']} ORDER BY {$field['dbAlias']} ) as {$tableName}_{$fieldName}";
275 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
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);
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);
292 $this->_select
= "SELECT " . implode(', ', $select) . " ";
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);
302 FROM civicrm_activity {$this->_aliases['civicrm_activity']}
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
320 LEFT JOIN civicrm_option_value
321 ON ( {$this->_aliases['civicrm_activity']}.activity_type_id = civicrm_option_value.value )
322 LEFT JOIN civicrm_option_group
323 ON civicrm_option_group.id = civicrm_option_value.option_group_id
324 LEFT JOIN civicrm_case_activity
325 ON civicrm_case_activity.activity_id = {$this->_aliases['civicrm_activity']}.id
326 LEFT JOIN civicrm_case
327 ON civicrm_case_activity.case_id = civicrm_case.id
328 LEFT JOIN civicrm_case_contact
329 ON civicrm_case_contact.case_id = civicrm_case.id ";
331 if ($this->_emailField
) {
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 ";
338 if ($this->_phoneField
) {
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 ";
347 $this->_where
= " WHERE civicrm_option_group.name = 'activity_type' AND
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";
353 foreach ($this->_columns
as $tableName => $table) {
354 if (array_key_exists('filters', $table)) {
356 foreach ($table['filters'] as $fieldName => $field) {
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
);
363 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
366 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
368 $clause = $this->whereClause($field,
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
)
377 if (!empty($clause)) {
378 $clauses[] = $clause;
384 if (empty($clauses)) {
385 $this->_where
.= " ";
388 $this->_where
.= " AND " . implode(' AND ', $clauses);
391 if ($this->_aclWhere
) {
392 $this->_where
.= " AND {$this->_aclWhere} ";
397 $this->_groupBy
= array();
398 if (is_array($this->_params
['group_bys']) &&
399 !empty($this->_params
['group_bys'])
401 foreach ($this->_columns
as $tableName => $table) {
402 if (array_key_exists('group_bys', $table)) {
403 foreach ($table['group_bys'] as $fieldName => $field) {
404 if (!empty($this->_params
['group_bys'][$fieldName])) {
405 if (!empty($field['chart'])) {
406 $this->assign('chartSupported', TRUE);
408 if (!empty($table['group_bys'][$fieldName]['frequency']) && !empty($this->_params
['group_bys_freq'][$fieldName])) {
410 $append = "YEAR({$field['dbAlias']}),";
411 if (in_array(strtolower($this->_params
['group_bys_freq'][$fieldName]),
416 $this->_groupBy
[] = "$append {$this->_params['group_bys_freq'][$fieldName]}({$field['dbAlias']})";
420 $this->_groupBy
[] = $field['dbAlias'];
427 $this->_groupBy
= "GROUP BY " . implode(', ', $this->_groupBy
);
430 $this->_groupBy
= "GROUP BY {$this->_aliases['civicrm_contact']}.id ";
441 function formRule($fields, $files, $self) {
443 $contactFields = array('sort_name', 'email', 'phone');
444 if (!empty($fields['group_bys'])) {
446 if (!empty($fields['group_bys']['activity_type_id']) && empty($fields['group_bys']['sort_name'])) {
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");
455 if (!empty($fields['group_bys']['activity_date_time'])) {
456 if (!empty($fields['group_bys']['sort_name'])) {
457 $errors['fields'] = ts("Please do not select GroupBy 'Activity Date' with GroupBy 'Contact'");
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'");
472 function postProcess() {
473 // get the acl clauses built before we assemble the query
474 $this->buildACLClause($this->_aliases
['civicrm_contact']);
475 parent
::postProcess();
481 function alterDisplay(&$rows) {
482 // custom code to alter rows
485 $activityType = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, FALSE, 'label', TRUE);
488 $onHover = ts('View Contact Summary for this Contact');
489 foreach ($rows as $rowNum => $row) {
491 if (array_key_exists('civicrm_contact_sort_name', $row) && $this->_outputMode
!= 'csv') {
492 if ($value = $row['civicrm_contact_id']) {
495 $priviousContact = $value;
498 if ($priviousContact == $value) {
500 $priviousContact = $value;
504 $priviousContact = $value;
508 if ($flagContact == 1) {
509 $rows[$rowNum]['civicrm_contact_sort_name'] = "";
511 if (array_key_exists('civicrm_email_email', $row)) {
512 $rows[$rowNum]['civicrm_email_email'] = "";
514 if (array_key_exists('civicrm_phone_phone', $row)) {
515 $rows[$rowNum]['civicrm_phone_phone'] = "";
519 $url = CRM_Utils_System
::url('civicrm/contact/view',
520 'reset=1&cid=' . $value,
524 $rows[$rowNum]['civicrm_contact_sort_name'] = "<a href='$url'>" . $row['civicrm_contact_sort_name'] . '</a>';
530 if (array_key_exists('civicrm_activity_activity_type_id', $row)) {
531 if ($value = $row['civicrm_activity_activity_type_id']) {
533 $value = explode(',', $value);
534 foreach ($value as $key => $id) {
535 $value[$key] = $activityType[$id];
538 $rows[$rowNum]['civicrm_activity_activity_type_id'] = implode(' , ', $value);