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(
49 'civicrm_contact' => array(
50 'dao' => 'CRM_Contact_DAO_Contact',
57 'title' => ts('Contact Name'),
64 'title' => ts('Contact Name'),
70 'title' => ts('Contact'),
76 'title' => ts('Contact Name')
79 'grouping' => 'contact-fields',
81 'civicrm_email' => array(
82 'dao' => 'CRM_Core_DAO_Email',
91 'title' => ts('Email')
94 'grouping' => 'contact-fields',
96 'civicrm_phone' => array(
97 'dao' => 'CRM_Core_DAO_Email',
103 'grouping' => 'contact-fields',
105 'civicrm_activity' => array(
106 'dao' => 'CRM_Activity_DAO_Activity',
108 'activity_type_id' => array(
109 'title' => ts('Activity Type'),
111 'type' => CRM_Utils_Type
::T_STRING
,
114 'title' => 'Duration',
115 'statistics' => array(
116 'sum' => ts('Total Duration'),
120 'title' => 'Total Activities',
122 'statistics' => array(
123 'count' => ts('Activity Count'),
128 'activity_date_time' => array(
129 'operatorType' => CRM_Report_Form
::OP_DATE
131 'activity_type_id' => array(
132 'title' => ts('Activity Type'),
133 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
134 'options' => CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, FALSE, 'label', TRUE),
136 'status_id' => array(
137 'title' => ts('Activity Status'),
138 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
139 'options' => CRM_Core_PseudoConstant
::activityStatus(),
141 'priority_id' => array(
142 'title' => ts('Priority'),
143 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
144 'options' => CRM_Core_PseudoConstant
::get('CRM_Activity_DAO_Activity', 'priority_id'),
147 'group_bys' => array(
148 'activity_date_time' => array(
149 'title' => ts('Activity Date'),
152 'activity_type_id' => array(
153 'title' => ts('Activity Type'),
157 'order_bys' => array(
158 'activity_date_time' => array(
159 'title' => ts('Activity Date')
161 'activity_type_id' => array(
162 'title' => ts('Activity Type')
165 'grouping' => 'activity-fields',
166 'alias' => 'activity',
170 parent
::__construct();
175 $this->_columnHeaders
= array();
176 foreach ($this->_columns
as $tableName => $table) {
177 if (array_key_exists('group_bys', $table)) {
178 foreach ($table['group_bys'] as $fieldName => $field) {
179 if (!empty($this->_params
['group_bys'][$fieldName])) {
180 if (isset($this->_params
['group_bys_freq']) && !empty($this->_params
['group_bys_freq'][$fieldName])) {
181 switch ($this->_params
['group_bys_freq'][$fieldName]) {
183 $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL WEEKDAY({$field['dbAlias']}) DAY) AS {$tableName}_{$fieldName}_start";
185 $select[] = "YEARWEEK({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
186 $select[] = "WEEKOFYEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
187 $field['title'] = 'Week';
191 $select[] = "MAKEDATE(YEAR({$field['dbAlias']}), 1) AS {$tableName}_{$fieldName}_start";
192 $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
193 $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
194 $field['title'] = 'Year';
198 $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL (DAYOFMONTH({$field['dbAlias']})-1) DAY) as {$tableName}_{$fieldName}_start";
199 $select[] = "MONTH({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
200 $select[] = "MONTHNAME({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
201 $field['title'] = 'Month';
205 $select[] = "STR_TO_DATE(CONCAT( 3 * QUARTER( {$field['dbAlias']} ) -2 , '/', '1', '/', YEAR( {$field['dbAlias']} ) ), '%m/%d/%Y') AS {$tableName}_{$fieldName}_start";
206 $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
207 $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
208 $field['title'] = 'Quarter';
211 $this->_interval
= $field['title'];
212 $this->_columnHeaders
["{$tableName}_{$fieldName}_start"]['title'] = $field['title'] . ' Beginning';
213 $this->_columnHeaders
["{$tableName}_{$fieldName}_start"]['type'] = $field['type'];
214 $this->_columnHeaders
["{$tableName}_{$fieldName}_start"]['group_by'] = $this->_params
['group_bys_freq'][$fieldName];
216 // just to make sure these values are transfered to rows.
217 // since we need that for calculation purpose,
218 // e.g making subtotals look nicer or graphs
219 $this->_columnHeaders
["{$tableName}_{$fieldName}_interval"] = array('no_display' => TRUE);
220 $this->_columnHeaders
["{$tableName}_{$fieldName}_subtotal"] = array('no_display' => TRUE);
225 if (array_key_exists('fields', $table)) {
226 foreach ($table['fields'] as $fieldName => $field) {
227 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
228 if ($tableName == 'civicrm_email') {
229 $this->_emailField
= TRUE;
231 if ($tableName == 'civicrm_phone') {
232 $this->_phoneField
= TRUE;
234 if (!empty($field['statistics'])) {
235 foreach ($field['statistics'] as $stat => $label) {
236 switch (strtolower($stat)) {
238 $select[] = "COUNT(DISTINCT({$field['dbAlias']})) as {$tableName}_{$fieldName}_{$stat}";
239 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type
::T_INT
;
240 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
241 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
245 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
246 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type
::T_INT
;
247 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
248 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
253 elseif ($fieldName == 'activity_type_id') {
254 if (empty($this->_params
['group_bys']['activity_type_id'])) {
255 $select[] = "GROUP_CONCAT(DISTINCT {$field['dbAlias']} ORDER BY {$field['dbAlias']} ) as {$tableName}_{$fieldName}";
258 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
260 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
261 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
262 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['no_display'] = CRM_Utils_Array
::value('no_display', $field);
265 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
266 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
267 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
268 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['no_display'] = CRM_Utils_Array
::value('no_display', $field);
275 $this->_select
= "SELECT " . implode(', ', $select) . " ";
279 $activityContacts = CRM_Core_OptionGroup
::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
280 $assigneeID = CRM_Utils_Array
::key('Activity Assignees', $activityContacts);
281 $targetID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
282 $sourceID = CRM_Utils_Array
::key('Activity Source', $activityContacts);
285 FROM civicrm_activity {$this->_aliases['civicrm_activity']}
287 LEFT JOIN civicrm_activity_contact target_activity
288 ON {$this->_aliases['civicrm_activity']}.id = target_activity.activity_id AND
289 target_activity.record_type_id = {$targetID}
290 LEFT JOIN civicrm_activity_contact assignment_activity
291 ON {$this->_aliases['civicrm_activity']}.id = assignment_activity.activity_id AND
292 assignment_activity.record_type_id = {$assigneeID}
293 LEFT JOIN civicrm_activity_contact source_activity
294 ON {$this->_aliases['civicrm_activity']}.id = source_activity.activity_id AND
295 source_activity.record_type_id = {$sourceID}
296 LEFT JOIN civicrm_contact contact_civireport
297 ON target_activity.contact_id = contact_civireport.id
298 LEFT JOIN civicrm_contact civicrm_contact_assignee
299 ON assignment_activity.contact_id = civicrm_contact_assignee.id
300 LEFT JOIN civicrm_contact civicrm_contact_source
301 ON source_activity.contact_id = civicrm_contact_source.id
303 LEFT JOIN civicrm_option_value
304 ON ( {$this->_aliases['civicrm_activity']}.activity_type_id = civicrm_option_value.value )
305 LEFT JOIN civicrm_option_group
306 ON civicrm_option_group.id = civicrm_option_value.option_group_id
307 LEFT JOIN civicrm_case_activity
308 ON civicrm_case_activity.activity_id = {$this->_aliases['civicrm_activity']}.id
309 LEFT JOIN civicrm_case
310 ON civicrm_case_activity.case_id = civicrm_case.id
311 LEFT JOIN civicrm_case_contact
312 ON civicrm_case_contact.case_id = civicrm_case.id ";
314 if ($this->_emailField
) {
316 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
317 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
318 {$this->_aliases['civicrm_email']}.is_primary = 1 ";
321 if ($this->_phoneField
) {
323 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
324 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
325 {$this->_aliases['civicrm_phone']}.is_primary = 1 ";
330 $this->_where
= " WHERE civicrm_option_group.name = 'activity_type' AND
331 {$this->_aliases['civicrm_activity']}.is_test = 0 AND
332 {$this->_aliases['civicrm_activity']}.is_deleted = 0 AND
333 {$this->_aliases['civicrm_activity']}.is_current_revision = 1";
336 foreach ($this->_columns
as $tableName => $table) {
337 if (array_key_exists('filters', $table)) {
339 foreach ($table['filters'] as $fieldName => $field) {
341 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
342 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
343 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
344 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
346 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
349 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
351 $clause = $this->whereClause($field,
353 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
354 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
355 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
360 if (!empty($clause)) {
361 $clauses[] = $clause;
367 if (empty($clauses)) {
368 $this->_where
.= " ";
371 $this->_where
.= " AND " . implode(' AND ', $clauses);
374 if ($this->_aclWhere
) {
375 $this->_where
.= " AND {$this->_aclWhere} ";
380 $this->_groupBy
= array();
381 if (is_array($this->_params
['group_bys']) &&
382 !empty($this->_params
['group_bys'])
384 foreach ($this->_columns
as $tableName => $table) {
385 if (array_key_exists('group_bys', $table)) {
386 foreach ($table['group_bys'] as $fieldName => $field) {
387 if (!empty($this->_params
['group_bys'][$fieldName])) {
388 if (!empty($field['chart'])) {
389 $this->assign('chartSupported', TRUE);
391 if (!empty($table['group_bys'][$fieldName]['frequency']) &&
392 !empty($this->_params
['group_bys_freq'][$fieldName])
395 $append = "YEAR({$field['dbAlias']}),";
396 if (in_array(strtolower($this->_params
['group_bys_freq'][$fieldName]),
401 $this->_groupBy
[] = "$append {$this->_params['group_bys_freq'][$fieldName]}({$field['dbAlias']})";
405 $this->_groupBy
[] = $field['dbAlias'];
412 $this->_groupBy
= "GROUP BY " . implode(', ', $this->_groupBy
);
415 $this->_groupBy
= "GROUP BY {$this->_aliases['civicrm_contact']}.id ";
426 function formRule($fields, $files, $self) {
428 $contactFields = array('sort_name', 'email', 'phone');
429 if (!empty($fields['group_bys'])) {
431 if (!empty($fields['group_bys']['activity_type_id']) && empty($fields['group_bys']['sort_name'])) {
432 foreach ($fields['fields'] as $fieldName => $val) {
433 if (in_array($fieldName, $contactFields)) {
434 $errors['fields'] = ts("Please select GroupBy 'Contact' to display Contact Fields");
440 if (!empty($fields['group_bys']['activity_date_time'])) {
441 if (!empty($fields['group_bys']['sort_name'])) {
442 $errors['fields'] = ts("Please do not select GroupBy 'Activity Date' with GroupBy 'Contact'");
445 foreach ($fields['fields'] as $fieldName => $val) {
446 if (in_array($fieldName, $contactFields)) {
447 $errors['fields'] = ts("Please do not select any Contact Fields with GroupBy 'Activity Date'");
457 function postProcess() {
458 // get the acl clauses built before we assemble the query
459 $this->buildACLClause($this->_aliases
['civicrm_contact']);
460 parent
::postProcess();
466 function alterDisplay(&$rows) {
467 // custom code to alter rows
470 $activityType = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, FALSE, 'label', TRUE);
473 $onHover = ts('View Contact Summary for this Contact');
474 foreach ($rows as $rowNum => $row) {
476 if (array_key_exists('civicrm_contact_sort_name', $row) && $this->_outputMode
!= 'csv') {
477 if ($value = $row['civicrm_contact_id']) {
480 $priviousContact = $value;
483 if ($priviousContact == $value) {
485 $priviousContact = $value;
489 $priviousContact = $value;
493 if ($flagContact == 1) {
494 $rows[$rowNum]['civicrm_contact_sort_name'] = "";
496 if (array_key_exists('civicrm_email_email', $row)) {
497 $rows[$rowNum]['civicrm_email_email'] = "";
499 if (array_key_exists('civicrm_phone_phone', $row)) {
500 $rows[$rowNum]['civicrm_phone_phone'] = "";
504 $url = CRM_Utils_System
::url('civicrm/contact/view',
505 'reset=1&cid=' . $value,
509 $rows[$rowNum]['civicrm_contact_sort_name'] = "<a href='$url'>" . $row['civicrm_contact_sort_name'] .
516 if (array_key_exists('civicrm_activity_activity_type_id', $row)) {
517 if ($value = $row['civicrm_activity_activity_type_id']) {
519 $value = explode(',', $value);
520 foreach ($value as $key => $id) {
521 $value[$key] = $activityType[$id];
524 $rows[$rowNum]['civicrm_activity_activity_type_id'] = implode(' , ', $value);