5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
33 * @copyright CiviCRM LLC (c) 2004-2013
37 class CRM_Report_Form_ActivitySummary
extends CRM_Report_Form
{
39 protected $_emailField = FALSE;
40 protected $_phoneField = FALSE;
42 function __construct() {
43 $this->_columns
= array(
46 'dao' => 'CRM_Contact_DAO_Contact',
55 array('title' => ts('Contact Name'),
63 array('title' => ts('Contact Name'),
71 'title' => ts('Contact'),
78 array('title' => ts('Contact Name')),
80 'grouping' => 'contact-fields',
84 'dao' => 'CRM_Core_DAO_Email',
96 array('title' => ts('Email')),
98 'grouping' => 'contact-fields',
102 'dao' => 'CRM_Core_DAO_Email',
106 array('title' => 'Phone'),
108 'grouping' => 'contact-fields',
110 'civicrm_activity' =>
112 'dao' => 'CRM_Activity_DAO_Activity',
115 'activity_type_id' =>
116 array('title' => ts('Activity Type'),
118 'type' => CRM_Utils_Type
::T_STRING
,
122 'title' => 'Duration',
125 'sum' => ts('Total Duration'),
130 'title' => 'Total Activities',
134 'count' => ts('Activity Count'),
140 'activity_date_time' =>
141 array('operatorType' => CRM_Report_Form
::OP_DATE
),
142 'activity_type_id' =>
143 array('title' => ts('Activity Type'),
144 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
145 'options' => CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, FALSE, 'label', TRUE),
148 array('title' => ts('Activity Status'),
149 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
150 'options' => CRM_Core_PseudoConstant
::activityStatus(),
153 array('title' => ts('Priority'),
154 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
155 'options' => CRM_Core_PseudoConstant
::priority(),
160 'activity_date_time' =>
161 array('title' => ts('Activity Date'),
164 'activity_type_id' =>
165 array('title' => ts('Activity Type'),
171 'activity_date_time' =>
172 array('title' => ts('Activity Date')),
173 'activity_type_id' =>
174 array('title' => ts('Activity Type')),
176 'grouping' => 'activity-fields',
177 'alias' => 'activity',
181 parent
::__construct();
186 $this->_columnHeaders
= array();
187 foreach ($this->_columns
as $tableName => $table) {
188 if (array_key_exists('group_bys', $table)) {
189 foreach ($table['group_bys'] as $fieldName => $field) {
190 if (CRM_Utils_Array
::value($fieldName, $this->_params
['group_bys'])) {
192 switch (CRM_Utils_Array
::value($fieldName, $this->_params
['group_bys_freq'])) {
194 $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL WEEKDAY({$field['dbAlias']}) DAY) AS {$tableName}_{$fieldName}_start";
196 $select[] = "YEARWEEK({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
197 $select[] = "WEEKOFYEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
198 $field['title'] = 'Week';
202 $select[] = "MAKEDATE(YEAR({$field['dbAlias']}), 1) AS {$tableName}_{$fieldName}_start";
203 $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
204 $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
205 $field['title'] = 'Year';
209 $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL (DAYOFMONTH({$field['dbAlias']})-1) DAY) as {$tableName}_{$fieldName}_start";
210 $select[] = "MONTH({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
211 $select[] = "MONTHNAME({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
212 $field['title'] = 'Month';
216 $select[] = "STR_TO_DATE(CONCAT( 3 * QUARTER( {$field['dbAlias']} ) -2 , '/', '1', '/', YEAR( {$field['dbAlias']} ) ), '%m/%d/%Y') AS {$tableName}_{$fieldName}_start";
217 $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
218 $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
219 $field['title'] = 'Quarter';
222 if (CRM_Utils_Array
::value($fieldName, $this->_params
['group_bys_freq'])) {
223 $this->_interval
= $field['title'];
224 $this->_columnHeaders
["{$tableName}_{$fieldName}_start"]['title'] = $field['title'] . ' Beginning';
225 $this->_columnHeaders
["{$tableName}_{$fieldName}_start"]['type'] = $field['type'];
226 $this->_columnHeaders
["{$tableName}_{$fieldName}_start"]['group_by'] = $this->_params
['group_bys_freq'][$fieldName];
228 // just to make sure these values are transfered to rows.
229 // since we need that for calculation purpose,
230 // e.g making subtotals look nicer or graphs
231 $this->_columnHeaders
["{$tableName}_{$fieldName}_interval"] = array('no_display' => TRUE);
232 $this->_columnHeaders
["{$tableName}_{$fieldName}_subtotal"] = array('no_display' => TRUE);
237 if (array_key_exists('fields', $table)) {
238 foreach ($table['fields'] as $fieldName => $field) {
239 if (CRM_Utils_Array
::value('required', $field) ||
240 CRM_Utils_Array
::value($fieldName, $this->_params
['fields'])
242 if ($tableName == 'civicrm_email') {
243 $this->_emailField
= TRUE;
245 if ($tableName == 'civicrm_phone') {
246 $this->_phoneField
= TRUE;
248 if (CRM_Utils_Array
::value('statistics', $field)) {
249 foreach ($field['statistics'] as $stat => $label) {
250 switch (strtolower($stat)) {
252 $select[] = "COUNT(DISTINCT({$field['dbAlias']})) as {$tableName}_{$fieldName}_{$stat}";
253 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type
::T_INT
;
254 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
255 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
259 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
260 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type
::T_INT
;
261 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
262 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
267 elseif ($fieldName == 'activity_type_id') {
268 if (!CRM_Utils_Array
::value('activity_type_id', $this->_params
['group_bys'])) {
269 $select[] = "GROUP_CONCAT(DISTINCT {$field['dbAlias']} ORDER BY {$field['dbAlias']} ) as {$tableName}_{$fieldName}";
272 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
274 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
275 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
276 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['no_display'] = CRM_Utils_Array
::value('no_display', $field);
279 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
280 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
281 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
282 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['no_display'] = CRM_Utils_Array
::value('no_display', $field);
289 $this->_select
= "SELECT " . implode(', ', $select) . " ";
295 FROM civicrm_activity {$this->_aliases['civicrm_activity']}
297 LEFT JOIN civicrm_activity_target target_activity
298 ON {$this->_aliases['civicrm_activity']}.id = target_activity.activity_id
299 LEFT JOIN civicrm_activity_assignment assignment_activity
300 ON {$this->_aliases['civicrm_activity']}.id = assignment_activity.activity_id
301 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
302 ON ({$this->_aliases['civicrm_activity']}.source_contact_id = {$this->_aliases['civicrm_contact']}.id OR
303 target_activity.target_contact_id = {$this->_aliases['civicrm_contact']}.id OR
304 assignment_activity.assignee_contact_id = {$this->_aliases['civicrm_contact']}.id )
306 LEFT JOIN civicrm_option_value
307 ON ( {$this->_aliases['civicrm_activity']}.activity_type_id = civicrm_option_value.value )
308 LEFT JOIN civicrm_option_group
309 ON civicrm_option_group.id = civicrm_option_value.option_group_id
310 LEFT JOIN civicrm_case_activity
311 ON civicrm_case_activity.activity_id = {$this->_aliases['civicrm_activity']}.id
312 LEFT JOIN civicrm_case
313 ON civicrm_case_activity.case_id = civicrm_case.id
314 LEFT JOIN civicrm_case_contact
315 ON civicrm_case_contact.case_id = civicrm_case.id ";
317 if ($this->_emailField
) {
319 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
320 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
321 {$this->_aliases['civicrm_email']}.is_primary = 1 ";
324 if ($this->_phoneField
) {
326 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
327 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
328 {$this->_aliases['civicrm_phone']}.is_primary = 1 ";
333 $this->_where
= " WHERE civicrm_option_group.name = 'activity_type' AND
334 {$this->_aliases['civicrm_activity']}.is_test = 0 AND
335 {$this->_aliases['civicrm_activity']}.is_deleted = 0 AND
336 {$this->_aliases['civicrm_activity']}.is_current_revision = 1";
339 foreach ($this->_columns
as $tableName => $table) {
340 if (array_key_exists('filters', $table)) {
342 foreach ($table['filters'] as $fieldName => $field) {
344 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
345 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
346 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
347 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
349 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
352 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
354 $clause = $this->whereClause($field,
356 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
357 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
358 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
363 if (!empty($clause)) {
364 $clauses[] = $clause;
370 if (empty($clauses)) {
371 $this->_where
.= " ";
374 $this->_where
.= " AND " . implode(' AND ', $clauses);
377 if ($this->_aclWhere
) {
378 $this->_where
.= " AND {$this->_aclWhere} ";
383 $this->_groupBy
= array();
384 if (is_array($this->_params
['group_bys']) &&
385 !empty($this->_params
['group_bys'])
387 foreach ($this->_columns
as $tableName => $table) {
388 if (array_key_exists('group_bys', $table)) {
389 foreach ($table['group_bys'] as $fieldName => $field) {
390 if (CRM_Utils_Array
::value($fieldName, $this->_params
['group_bys'])) {
391 if (CRM_Utils_Array
::value('chart', $field)) {
392 $this->assign('chartSupported', TRUE);
394 if (CRM_Utils_Array
::value('frequency', $table['group_bys'][$fieldName]) &&
395 CRM_Utils_Array
::value($fieldName, $this->_params
['group_bys_freq'])
398 $append = "YEAR({$field['dbAlias']}),";
399 if (in_array(strtolower($this->_params
['group_bys_freq'][$fieldName]),
404 $this->_groupBy
[] = "$append {$this->_params['group_bys_freq'][$fieldName]}({$field['dbAlias']})";
408 $this->_groupBy
[] = $field['dbAlias'];
415 $this->_groupBy
= "GROUP BY " . implode(', ', $this->_groupBy
);
418 $this->_groupBy
= "GROUP BY {$this->_aliases['civicrm_contact']}.id ";
422 function formRule($fields, $files, $self) {
424 $contactFields = array('sort_name', 'email', 'phone');
425 if (CRM_Utils_Array
::value('group_bys', $fields)) {
427 if (CRM_Utils_Array
::value('activity_type_id', $fields['group_bys']) &&
428 !CRM_Utils_Array
::value('sort_name', $fields['group_bys'])
430 foreach ($fields['fields'] as $fieldName => $val) {
431 if (in_array($fieldName, $contactFields)) {
432 $errors['fields'] = ts("Please select GroupBy 'Contact' to display Contact Fields");
438 if (CRM_Utils_Array
::value('activity_date_time', $fields['group_bys'])) {
439 if (CRM_Utils_Array
::value('sort_name', $fields['group_bys'])) {
440 $errors['fields'] = ts("Please do not select GroupBy 'Activity Date' with GroupBy 'Contact'");
443 foreach ($fields['fields'] as $fieldName => $val) {
444 if (in_array($fieldName, $contactFields)) {
445 $errors['fields'] = ts("Please do not select any Contact Fields with GroupBy 'Activity Date'");
455 function postProcess() {
456 // get the acl clauses built before we assemble the query
457 $this->buildACLClause($this->_aliases
['civicrm_contact']);
458 parent
::postProcess();
461 function alterDisplay(&$rows) {
462 // custom code to alter rows
465 $activityType = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, FALSE, 'label', TRUE);
468 $onHover = ts('View Contact Summary for this Contact');
469 foreach ($rows as $rowNum => $row) {
471 if (array_key_exists('civicrm_contact_sort_name', $row) && $this->_outputMode
!= 'csv') {
472 if ($value = $row['civicrm_contact_id']) {
475 $priviousContact = $value;
478 if ($priviousContact == $value) {
480 $priviousContact = $value;
484 $priviousContact = $value;
488 if ($flagContact == 1) {
489 $rows[$rowNum]['civicrm_contact_sort_name'] = "";
491 if (array_key_exists('civicrm_email_email', $row)) {
492 $rows[$rowNum]['civicrm_email_email'] = "";
494 if (array_key_exists('civicrm_phone_phone', $row)) {
495 $rows[$rowNum]['civicrm_phone_phone'] = "";
499 $url = CRM_Utils_System
::url('civicrm/contact/view',
500 'reset=1&cid=' . $value,
504 $rows[$rowNum]['civicrm_contact_sort_name'] = "<a href='$url'>" . $row['civicrm_contact_sort_name'] . '</a>';
510 if (array_key_exists('civicrm_activity_activity_type_id', $row)) {
511 if ($value = $row['civicrm_activity_activity_type_id']) {
513 $value = explode(',', $value);
514 foreach ($value as $key => $id) {
515 $value[$key] = $activityType[$id];
518 $rows[$rowNum]['civicrm_activity_activity_type_id'] = implode(' , ', $value);