3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
35 class CRM_Report_Form_ActivitySummary
extends CRM_Report_Form
{
37 protected $_emailField = FALSE;
38 protected $_phoneField = FALSE;
44 public function __construct() {
45 $this->_columns
= array(
46 'civicrm_contact' => array(
47 'dao' => 'CRM_Contact_DAO_Contact',
54 'title' => ts('Contact Name'),
61 'title' => ts('Contact Name'),
67 'title' => ts('Contact'),
73 'title' => ts('Contact Name'),
76 'grouping' => 'contact-fields',
78 'civicrm_email' => array(
79 'dao' => 'CRM_Core_DAO_Email',
88 'title' => ts('Email'),
91 'grouping' => 'contact-fields',
93 'civicrm_phone' => array(
94 'dao' => 'CRM_Core_DAO_Email',
100 'grouping' => 'contact-fields',
102 'civicrm_activity' => array(
103 'dao' => 'CRM_Activity_DAO_Activity',
105 'activity_type_id' => array(
106 'title' => ts('Activity Type'),
108 'type' => CRM_Utils_Type
::T_STRING
,
111 'title' => 'Duration',
112 'statistics' => array(
113 'sum' => ts('Total Duration'),
117 'title' => 'Total Activities',
119 'statistics' => array(
120 'count' => ts('Activity Count'),
125 'activity_date_time' => array(
126 'operatorType' => CRM_Report_Form
::OP_DATE
,
128 'activity_type_id' => array(
129 'title' => ts('Activity Type'),
130 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
131 'options' => CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, FALSE, 'label', TRUE),
133 'status_id' => array(
134 'title' => ts('Activity Status'),
135 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
136 'options' => CRM_Core_PseudoConstant
::activityStatus(),
138 'priority_id' => array(
139 'title' => ts('Priority'),
140 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
141 'options' => CRM_Core_PseudoConstant
::get('CRM_Activity_DAO_Activity', 'priority_id'),
144 'group_bys' => array(
145 'activity_date_time' => array(
146 'title' => ts('Activity Date'),
149 'activity_type_id' => array(
150 'title' => ts('Activity Type'),
154 'order_bys' => array(
155 'activity_date_time' => array(
156 'title' => ts('Activity Date'),
158 'activity_type_id' => array(
159 'title' => ts('Activity Type'),
162 'grouping' => 'activity-fields',
163 'alias' => 'activity',
167 parent
::__construct();
170 public function select() {
172 $this->_columnHeaders
= array();
173 foreach ($this->_columns
as $tableName => $table) {
174 if (array_key_exists('group_bys', $table)) {
175 foreach ($table['group_bys'] as $fieldName => $field) {
176 if (!empty($this->_params
['group_bys'][$fieldName])) {
177 if (isset($this->_params
['group_bys_freq']) && !empty($this->_params
['group_bys_freq'][$fieldName])) {
178 switch ($this->_params
['group_bys_freq'][$fieldName]) {
180 $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL WEEKDAY({$field['dbAlias']}) DAY) AS {$tableName}_{$fieldName}_start";
182 $select[] = "YEARWEEK({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
183 $select[] = "WEEKOFYEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
184 $field['title'] = 'Week';
188 $select[] = "MAKEDATE(YEAR({$field['dbAlias']}), 1) AS {$tableName}_{$fieldName}_start";
189 $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
190 $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
191 $field['title'] = 'Year';
195 $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL (DAYOFMONTH({$field['dbAlias']})-1) DAY) as {$tableName}_{$fieldName}_start";
196 $select[] = "MONTH({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
197 $select[] = "MONTHNAME({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
198 $field['title'] = 'Month';
202 $select[] = "STR_TO_DATE(CONCAT( 3 * QUARTER( {$field['dbAlias']} ) -2 , '/', '1', '/', YEAR( {$field['dbAlias']} ) ), '%m/%d/%Y') AS {$tableName}_{$fieldName}_start";
203 $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
204 $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
205 $field['title'] = 'Quarter';
208 $this->_interval
= $field['title'];
209 $this->_columnHeaders
["{$tableName}_{$fieldName}_start"]['title'] = $field['title'] . ' Beginning';
210 $this->_columnHeaders
["{$tableName}_{$fieldName}_start"]['type'] = $field['type'];
211 $this->_columnHeaders
["{$tableName}_{$fieldName}_start"]['group_by'] = $this->_params
['group_bys_freq'][$fieldName];
213 // just to make sure these values are transfered to rows.
214 // since we need that for calculation purpose,
215 // e.g making subtotals look nicer or graphs
216 $this->_columnHeaders
["{$tableName}_{$fieldName}_interval"] = array('no_display' => TRUE);
217 $this->_columnHeaders
["{$tableName}_{$fieldName}_subtotal"] = array('no_display' => TRUE);
222 if (array_key_exists('fields', $table)) {
223 foreach ($table['fields'] as $fieldName => $field) {
224 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
225 if ($tableName == 'civicrm_email') {
226 $this->_emailField
= TRUE;
228 if ($tableName == 'civicrm_phone') {
229 $this->_phoneField
= TRUE;
231 if (!empty($field['statistics'])) {
232 foreach ($field['statistics'] as $stat => $label) {
233 switch (strtolower($stat)) {
235 $select[] = "COUNT(DISTINCT({$field['dbAlias']})) as {$tableName}_{$fieldName}_{$stat}";
236 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type
::T_INT
;
237 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
238 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
242 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
243 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type
::T_INT
;
244 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
245 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
250 elseif ($fieldName == 'activity_type_id') {
251 if (empty($this->_params
['group_bys']['activity_type_id'])) {
252 $select[] = "GROUP_CONCAT(DISTINCT {$field['dbAlias']} ORDER BY {$field['dbAlias']} ) as {$tableName}_{$fieldName}";
255 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
257 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
258 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
259 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['no_display'] = CRM_Utils_Array
::value('no_display', $field);
262 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
263 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
264 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
265 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['no_display'] = CRM_Utils_Array
::value('no_display', $field);
272 $this->_select
= "SELECT " . implode(', ', $select) . " ";
275 public function from() {
276 $activityContacts = CRM_Core_OptionGroup
::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
277 $assigneeID = CRM_Utils_Array
::key('Activity Assignees', $activityContacts);
278 $targetID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
279 $sourceID = CRM_Utils_Array
::key('Activity Source', $activityContacts);
282 FROM civicrm_activity {$this->_aliases['civicrm_activity']}
284 LEFT JOIN civicrm_activity_contact target_activity
285 ON {$this->_aliases['civicrm_activity']}.id = target_activity.activity_id AND
286 target_activity.record_type_id = {$targetID}
287 LEFT JOIN civicrm_activity_contact assignment_activity
288 ON {$this->_aliases['civicrm_activity']}.id = assignment_activity.activity_id AND
289 assignment_activity.record_type_id = {$assigneeID}
290 LEFT JOIN civicrm_activity_contact source_activity
291 ON {$this->_aliases['civicrm_activity']}.id = source_activity.activity_id AND
292 source_activity.record_type_id = {$sourceID}
293 LEFT JOIN civicrm_contact contact_civireport
294 ON target_activity.contact_id = contact_civireport.id
295 LEFT JOIN civicrm_contact civicrm_contact_assignee
296 ON assignment_activity.contact_id = civicrm_contact_assignee.id
297 LEFT JOIN civicrm_contact civicrm_contact_source
298 ON source_activity.contact_id = civicrm_contact_source.id
300 LEFT JOIN civicrm_option_value
301 ON ( {$this->_aliases['civicrm_activity']}.activity_type_id = civicrm_option_value.value )
302 LEFT JOIN civicrm_option_group
303 ON civicrm_option_group.id = civicrm_option_value.option_group_id
304 LEFT JOIN civicrm_case_activity
305 ON civicrm_case_activity.activity_id = {$this->_aliases['civicrm_activity']}.id
306 LEFT JOIN civicrm_case
307 ON civicrm_case_activity.case_id = civicrm_case.id
308 LEFT JOIN civicrm_case_contact
309 ON civicrm_case_contact.case_id = civicrm_case.id ";
311 if ($this->_emailField
) {
313 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
314 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
315 {$this->_aliases['civicrm_email']}.is_primary = 1 ";
318 if ($this->_phoneField
) {
320 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
321 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
322 {$this->_aliases['civicrm_phone']}.is_primary = 1 ";
326 public function where() {
327 $this->_where
= " WHERE civicrm_option_group.name = 'activity_type' AND
328 {$this->_aliases['civicrm_activity']}.is_test = 0 AND
329 {$this->_aliases['civicrm_activity']}.is_deleted = 0 AND
330 {$this->_aliases['civicrm_activity']}.is_current_revision = 1";
333 foreach ($this->_columns
as $tableName => $table) {
334 if (array_key_exists('filters', $table)) {
336 foreach ($table['filters'] as $fieldName => $field) {
338 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
339 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
340 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
341 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
343 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
346 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
348 $clause = $this->whereClause($field,
350 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
351 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
352 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
357 if (!empty($clause)) {
358 $clauses[] = $clause;
364 if (empty($clauses)) {
365 $this->_where
.= " ";
368 $this->_where
.= " AND " . implode(' AND ', $clauses);
371 if ($this->_aclWhere
) {
372 $this->_where
.= " AND {$this->_aclWhere} ";
376 public function groupBy() {
377 $this->_groupBy
= array();
378 if (is_array($this->_params
['group_bys']) &&
379 !empty($this->_params
['group_bys'])
381 foreach ($this->_columns
as $tableName => $table) {
382 if (array_key_exists('group_bys', $table)) {
383 foreach ($table['group_bys'] as $fieldName => $field) {
384 if (!empty($this->_params
['group_bys'][$fieldName])) {
385 if (!empty($field['chart'])) {
386 $this->assign('chartSupported', TRUE);
388 if (!empty($table['group_bys'][$fieldName]['frequency']) &&
389 !empty($this->_params
['group_bys_freq'][$fieldName])
392 $append = "YEAR({$field['dbAlias']}),";
393 if (in_array(strtolower($this->_params
['group_bys_freq'][$fieldName]),
398 $this->_groupBy
[] = "$append {$this->_params['group_bys_freq'][$fieldName]}({$field['dbAlias']})";
402 $this->_groupBy
[] = $field['dbAlias'];
409 $this->_groupBy
= "GROUP BY " . implode(', ', $this->_groupBy
);
412 $this->_groupBy
= "GROUP BY {$this->_aliases['civicrm_contact']}.id ";
423 public function formRule($fields, $files, $self) {
425 $contactFields = array('sort_name', 'email', 'phone');
426 if (!empty($fields['group_bys'])) {
428 if (!empty($fields['group_bys']['activity_type_id']) && empty($fields['group_bys']['sort_name'])) {
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 (!empty($fields['group_bys']['activity_date_time'])) {
438 if (!empty($fields['group_bys']['sort_name'])) {
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 public function postProcess() {
455 // get the acl clauses built before we assemble the query
456 $this->buildACLClause($this->_aliases
['civicrm_contact']);
457 parent
::postProcess();
461 * Alter display of rows.
463 * Iterate through the rows retrieved via SQL and make changes for display purposes,
464 * such as rendering contacts as links.
467 * Rows generated by SQL, with an array for each row.
469 public function alterDisplay(&$rows) {
472 $activityType = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, FALSE, 'label', TRUE);
475 $onHover = ts('View Contact Summary for this Contact');
476 foreach ($rows as $rowNum => $row) {
478 if (array_key_exists('civicrm_contact_sort_name', $row) && $this->_outputMode
!= 'csv') {
479 if ($value = $row['civicrm_contact_id']) {
482 $priviousContact = $value;
485 if ($priviousContact == $value) {
487 $priviousContact = $value;
491 $priviousContact = $value;
495 if ($flagContact == 1) {
496 $rows[$rowNum]['civicrm_contact_sort_name'] = "";
498 if (array_key_exists('civicrm_email_email', $row)) {
499 $rows[$rowNum]['civicrm_email_email'] = "";
501 if (array_key_exists('civicrm_phone_phone', $row)) {
502 $rows[$rowNum]['civicrm_phone_phone'] = "";
506 $url = CRM_Utils_System
::url('civicrm/contact/view',
507 'reset=1&cid=' . $value,
511 $rows[$rowNum]['civicrm_contact_sort_name'] = "<a href='$url'>" . $row['civicrm_contact_sort_name'] .
518 if (array_key_exists('civicrm_activity_activity_type_id', $row)) {
519 if ($value = $row['civicrm_activity_activity_type_id']) {
521 $value = explode(',', $value);
522 foreach ($value as $key => $id) {
523 $value[$key] = $activityType[$id];
526 $rows[$rowNum]['civicrm_activity_activity_type_id'] = implode(' , ', $value);