4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
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) . " ";
294 FROM civicrm_activity {$this->_aliases['civicrm_activity']}
296 LEFT JOIN civicrm_activity_target target_activity
297 ON {$this->_aliases['civicrm_activity']}.id = target_activity.activity_id
298 LEFT JOIN civicrm_activity_assignment assignment_activity
299 ON {$this->_aliases['civicrm_activity']}.id = assignment_activity.activity_id
300 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
301 ON ({$this->_aliases['civicrm_activity']}.source_contact_id = {$this->_aliases['civicrm_contact']}.id OR
302 target_activity.target_contact_id = {$this->_aliases['civicrm_contact']}.id OR
303 assignment_activity.assignee_contact_id = {$this->_aliases['civicrm_contact']}.id )
305 LEFT JOIN civicrm_option_value
306 ON ( {$this->_aliases['civicrm_activity']}.activity_type_id = civicrm_option_value.value )
307 LEFT JOIN civicrm_option_group
308 ON civicrm_option_group.id = civicrm_option_value.option_group_id
309 LEFT JOIN civicrm_case_activity
310 ON civicrm_case_activity.activity_id = {$this->_aliases['civicrm_activity']}.id
311 LEFT JOIN civicrm_case
312 ON civicrm_case_activity.case_id = civicrm_case.id
313 LEFT JOIN civicrm_case_contact
314 ON civicrm_case_contact.case_id = civicrm_case.id ";
316 if ($this->_emailField
) {
318 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
319 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
320 {$this->_aliases['civicrm_email']}.is_primary = 1 ";
323 if ($this->_phoneField
) {
325 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
326 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
327 {$this->_aliases['civicrm_phone']}.is_primary = 1 ";
332 $this->_where
= " WHERE civicrm_option_group.name = 'activity_type' AND
333 {$this->_aliases['civicrm_activity']}.is_test = 0 AND
334 {$this->_aliases['civicrm_activity']}.is_deleted = 0 AND
335 {$this->_aliases['civicrm_activity']}.is_current_revision = 1";
338 foreach ($this->_columns
as $tableName => $table) {
339 if (array_key_exists('filters', $table)) {
341 foreach ($table['filters'] as $fieldName => $field) {
343 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
344 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
345 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
346 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
348 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
351 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
353 $clause = $this->whereClause($field,
355 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
356 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
357 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
362 if (!empty($clause)) {
363 $clauses[] = $clause;
369 if (empty($clauses)) {
370 $this->_where
.= " ";
373 $this->_where
.= " AND " . implode(' AND ', $clauses);
376 if ($this->_aclWhere
) {
377 $this->_where
.= " AND {$this->_aclWhere} ";
382 $this->_groupBy
= array();
383 if (is_array($this->_params
['group_bys']) &&
384 !empty($this->_params
['group_bys'])
386 foreach ($this->_columns
as $tableName => $table) {
387 if (array_key_exists('group_bys', $table)) {
388 foreach ($table['group_bys'] as $fieldName => $field) {
389 if (CRM_Utils_Array
::value($fieldName, $this->_params
['group_bys'])) {
390 if (CRM_Utils_Array
::value('chart', $field)) {
391 $this->assign('chartSupported', TRUE);
393 if (CRM_Utils_Array
::value('frequency', $table['group_bys'][$fieldName]) &&
394 CRM_Utils_Array
::value($fieldName, $this->_params
['group_bys_freq'])
397 $append = "YEAR({$field['dbAlias']}),";
398 if (in_array(strtolower($this->_params
['group_bys_freq'][$fieldName]),
403 $this->_groupBy
[] = "$append {$this->_params['group_bys_freq'][$fieldName]}({$field['dbAlias']})";
407 $this->_groupBy
[] = $field['dbAlias'];
414 $this->_groupBy
= "GROUP BY " . implode(', ', $this->_groupBy
);
417 $this->_groupBy
= "GROUP BY {$this->_aliases['civicrm_contact']}.id ";
421 function formRule($fields, $files, $self) {
423 $contactFields = array('sort_name', 'email', 'phone');
424 if (CRM_Utils_Array
::value('group_bys', $fields)) {
426 if (CRM_Utils_Array
::value('activity_type_id', $fields['group_bys']) &&
427 !CRM_Utils_Array
::value('sort_name', $fields['group_bys'])
429 foreach ($fields['fields'] as $fieldName => $val) {
430 if (in_array($fieldName, $contactFields)) {
431 $errors['fields'] = ts("Please select GroupBy 'Contact' to display Contact Fields");
437 if (CRM_Utils_Array
::value('activity_date_time', $fields['group_bys'])) {
438 if (CRM_Utils_Array
::value('sort_name', $fields['group_bys'])) {
439 $errors['fields'] = ts("Please do not select GroupBy 'Activity Date' with GroupBy 'Contact'");
442 foreach ($fields['fields'] as $fieldName => $val) {
443 if (in_array($fieldName, $contactFields)) {
444 $errors['fields'] = ts("Please do not select any Contact Fields with GroupBy 'Activity Date'");
454 function postProcess() {
455 // get the acl clauses built before we assemble the query
456 $this->buildACLClause($this->_aliases
['civicrm_contact']);
457 parent
::postProcess();
460 function alterDisplay(&$rows) {
461 // custom code to alter rows
464 $activityType = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, FALSE, 'label', TRUE);
467 $onHover = ts('View Contact Summary for this Contact');
468 foreach ($rows as $rowNum => $row) {
470 if (array_key_exists('civicrm_contact_sort_name', $row) && $this->_outputMode
!= 'csv') {
471 if ($value = $row['civicrm_contact_id']) {
474 $priviousContact = $value;
477 if ($priviousContact == $value) {
479 $priviousContact = $value;
483 $priviousContact = $value;
487 if ($flagContact == 1) {
488 $rows[$rowNum]['civicrm_contact_sort_name'] = "";
490 if (array_key_exists('civicrm_email_email', $row)) {
491 $rows[$rowNum]['civicrm_email_email'] = "";
493 if (array_key_exists('civicrm_phone_phone', $row)) {
494 $rows[$rowNum]['civicrm_phone_phone'] = "";
498 $url = CRM_Utils_System
::url('civicrm/contact/view',
499 'reset=1&cid=' . $value,
503 $rows[$rowNum]['civicrm_contact_sort_name'] = "<a href='$url'>" . $row['civicrm_contact_sort_name'] . '</a>';
509 if (array_key_exists('civicrm_activity_activity_type_id', $row)) {
510 if ($value = $row['civicrm_activity_activity_type_id']) {
512 $value = explode(',', $value);
513 foreach ($value as $key => $id) {
514 $value[$key] = $activityType[$id];
517 $rows[$rowNum]['civicrm_activity_activity_type_id'] = implode(' , ', $value);