3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
35 class CRM_Report_Form_Case_Detail
extends CRM_Report_Form
{
37 protected $_relField = FALSE;
39 protected $_addressField = FALSE;
41 protected $_emailField = FALSE;
43 protected $_phoneField = FALSE;
45 protected $_worldRegionField = FALSE;
47 protected $_activityLast = FALSE;
49 protected $_activityLastCompleted = FALSE;
51 protected $_includeCaseDetailExtra = FALSE;
53 protected $_caseDetailExtra = array();
55 protected $_customGroupExtends = array('Case');
59 public function __construct() {
60 $this->case_statuses
= CRM_Core_OptionGroup
::values('case_status');
61 $this->case_types
= CRM_Case_PseudoConstant
::caseType();
62 $rels = CRM_Core_PseudoConstant
::relationshipType();
63 foreach ($rels as $relid => $v) {
64 $this->rel_types
[$relid] = $v['label_b_a'];
67 $this->deleted_labels
= array(
68 '' => ts('- select -'),
73 $this->caseActivityTypes
= array();
74 foreach (CRM_Case_PseudoConstant
::caseActivityType() as $typeDetail) {
75 $this->caseActivityTypes
[$typeDetail['id']] = $typeDetail['label'];
78 $this->_columns
= array(
79 'civicrm_case' => array(
80 'dao' => 'CRM_Case_DAO_Case',
83 'title' => ts('Case ID'),
88 'title' => ts('Subject'),
91 'start_date' => array(
92 'title' => ts('Start Date'),
93 'type' => CRM_Utils_Type
::T_DATE
,
96 'title' => ts('End Date'),
97 'type' => CRM_Utils_Type
::T_DATE
,
99 'status_id' => array('title' => ts('Case Status')),
100 'case_type_id' => array('title' => ts('Case Type')),
101 'is_deleted' => array(
102 'title' => ts('Deleted?'),
104 'type' => CRM_Utils_Type
::T_INT
,
108 'start_date' => array(
109 'title' => ts('Start Date'),
110 'operatorType' => CRM_Report_Form
::OP_DATE
,
111 'type' => CRM_Utils_Type
::T_DATE
,
114 'title' => ts('End Date'),
115 'operatorType' => CRM_Report_Form
::OP_DATE
,
116 'type' => CRM_Utils_Type
::T_DATE
,
118 'status_id' => array(
119 'title' => ts('Case Status'),
120 'type' => CRM_Utils_Type
::T_INT
,
121 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
122 'options' => CRM_Case_BAO_Case
::buildOptions('status_id', 'search'),
124 'case_type_id' => array(
125 'title' => ts('Case Type'),
126 'type' => CRM_Utils_Type
::T_INT
,
127 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
128 'options' => CRM_Case_BAO_Case
::buildOptions('case_type_id', 'search'),
130 'is_deleted' => array(
131 'title' => ts('Deleted?'),
132 'type' => CRM_Utils_Type
::T_INT
,
133 'operatorType' => CRM_Report_Form
::OP_SELECT
,
134 'options' => $this->deleted_labels
,
139 'civicrm_contact' => array(
140 'dao' => 'CRM_Contact_DAO_Contact',
142 'client_sort_name' => array(
143 'name' => 'sort_name',
144 'title' => ts('Client Name'),
148 'no_display' => TRUE,
153 'sort_name' => array('title' => ts('Client Name')),
156 'civicrm_relationship' => array(
157 'dao' => 'CRM_Contact_DAO_Relationship',
159 'case_role' => array(
160 'name' => 'relationship_type_id',
161 'title' => ts('Case Role(s)'),
165 'case_role' => array(
166 'name' => 'relationship_type_id',
167 'title' => ts('Case Role(s)'),
168 'type' => CRM_Utils_Type
::T_INT
,
169 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
170 'options' => $this->rel_types
,
174 'civicrm_email' => array(
175 'dao' => 'CRM_Core_DAO_Email',
178 'title' => ts('Email'),
182 'grouping' => 'contact-fields',
184 'civicrm_phone' => array(
185 'dao' => 'CRM_Core_DAO_Phone',
188 'title' => ts('Phone'),
192 'grouping' => 'contact-fields',
194 'civicrm_address' => array(
195 'dao' => 'CRM_Core_DAO_Address',
197 'street_address' => NULL,
198 'state_province_id' => array(
199 'title' => ts('State/Province'),
201 'country_id' => array('title' => ts('Country')),
203 'grouping' => 'contact-fields',
205 'country_id' => array(
206 'title' => ts('Country'),
207 'type' => CRM_Utils_Type
::T_INT
,
208 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
209 'options' => CRM_Core_PseudoConstant
::country(),
211 'state_province_id' => array(
212 'title' => ts('State/Province'),
213 'type' => CRM_Utils_Type
::T_INT
,
214 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
215 'options' => CRM_Core_PseudoConstant
::stateProvince(),
219 'civicrm_worldregion' => array(
220 'dao' => 'CRM_Core_DAO_Worldregion',
222 'worldregion_id' => array(
224 'title' => ts('World Region'),
225 'type' => CRM_Utils_Type
::T_INT
,
226 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
227 'options' => CRM_Core_PseudoConstant
::worldRegion(),
231 'civicrm_country' => array(
232 'dao' => 'CRM_Core_DAO_Country',
234 'civicrm_activity_last' => array(
235 'dao' => 'CRM_Activity_DAO_Activity',
237 'last_activity_date_time' => array(
238 'name' => 'activity_date_time',
239 'title' => ts('Last Action Date'),
240 'operatorType' => CRM_Report_Form
::OP_DATE
,
243 'alias' => 'civireport_activity_last',
245 'civicrm_activity_last_completed' => array(
246 'dao' => 'CRM_Activity_DAO_Activity',
248 'last_completed_activity_subject' => array(
250 'title' => ts('Subject of the last completed activity in the case'),
252 'last_completed_activity_type' => array(
253 'name' => 'activity_type_id',
254 'title' => ts('Activity type of the last completed activity'),
260 $this->_options
= array(
262 'title' => ts('My Cases'),
263 'type' => 'checkbox',
266 parent
::__construct();
269 public function preProcess() {
270 parent
::preProcess();
273 public function buildQuickForm() {
274 parent
::buildQuickForm();
275 $this->caseDetailSpecialColumnsAdd();
278 public function caseDetailSpecialColumnsAdd() {
280 $elements[] = &$this->createElement('select', 'case_activity_all_dates', NULL,
282 '' => ts('- select -'),
283 ) +
$this->caseActivityTypes
285 $this->addGroup($elements, 'case_detail_extra');
287 $this->_caseDetailExtra
= array(
288 'case_activity_all_dates' => array(
289 'title' => ts('List of all dates of activities of Type'),
290 'name' => 'activity_date_time',
294 $this->assign('caseDetailExtra', $this->_caseDetailExtra
);
297 public function select() {
299 $this->_columnHeaders
= array();
300 foreach ($this->_columns
as $tableName => $table) {
301 if (array_key_exists('fields', $table)) {
302 foreach ($table['fields'] as $fieldName => $field) {
303 if ($tableName == 'civicrm_address') {
304 $this->_addressField
= TRUE;
306 if (!empty($field['required']) ||
307 !empty($this->_params
['fields'][$fieldName])
309 if ($tableName == 'civicrm_email') {
310 $this->_emailField
= TRUE;
312 elseif ($tableName == 'civicrm_phone') {
313 $this->_phoneField
= TRUE;
315 elseif ($tableName == 'civicrm_relationship') {
316 $this->_relField
= TRUE;
318 if ($fieldName == 'sort_name') {
319 $select[] = "GROUP_CONCAT({$field['dbAlias']} ORDER BY {$field['dbAlias']} )
320 as {$tableName}_{$fieldName}";
322 if ($tableName == 'civicrm_activity_last_completed') {
323 $this->_activityLastCompleted
= TRUE;
326 if ($fieldName == 'case_role') {
327 $select[] = "GROUP_CONCAT(DISTINCT({$field['dbAlias']}) ORDER BY {$field['dbAlias']}) as {$tableName}_{$fieldName}";
330 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
333 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
334 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
339 $this->_selectClauses
= $select;
341 $this->_select
= 'SELECT ' . implode(', ', $select) . ' ';
344 public function from() {
346 $case = $this->_aliases
['civicrm_case'];
347 $conact = $this->_aliases
['civicrm_contact'];
350 FROM civicrm_case $case
351 LEFT JOIN civicrm_case_contact civireport_case_contact on civireport_case_contact.case_id = {$case}.id
352 LEFT JOIN civicrm_contact $conact ON {$conact}.id = civireport_case_contact.contact_id
354 if ($this->_relField
) {
356 LEFT JOIN civicrm_relationship {$this->_aliases['civicrm_relationship']} ON {$this->_aliases['civicrm_relationship']}.case_id = {$case}.id
360 if ($this->_addressField
) {
362 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
363 ON {$conact}.id = {$this->_aliases['civicrm_address']}.contact_id AND
364 {$this->_aliases['civicrm_address']}.is_primary = 1 ";
366 if ($this->_emailField
) {
368 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
369 ON {$conact}.id = {$this->_aliases['civicrm_email']}.contact_id AND
370 {$this->_aliases['civicrm_email']}.is_primary = 1 ";
372 if ($this->_phoneField
) {
374 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
375 ON ( {$conact}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
376 {$this->_aliases['civicrm_phone']}.is_primary = 1) ";
378 if ($this->_worldRegionField
) {
380 LEFT JOIN civicrm_country {$this->_aliases['civicrm_country']}
381 ON {$this->_aliases['civicrm_country']}.id ={$this->_aliases['civicrm_address']}.country_id
382 LEFT JOIN civicrm_worldregion {$this->_aliases['civicrm_worldregion']}
383 ON {$this->_aliases['civicrm_country']}.region_id = {$this->_aliases['civicrm_worldregion']}.id ";
386 // Include clause for last activity of the case
387 if ($this->_activityLast
) {
388 $this->_from
.= " LEFT JOIN civicrm_activity {$this->_aliases['civicrm_activity_last']} ON ( {$this->_aliases['civicrm_activity_last']}.id = ( SELECT max(activity_id) FROM civicrm_case_activity WHERE case_id = {$case}.id) )";
391 // Include clause for last completed activity of the case
392 if ($this->_activityLastCompleted
) {
393 $this->_from
.= " LEFT JOIN civicrm_activity {$this->_aliases['civicrm_activity_last_completed']} ON ( {$this->_aliases['civicrm_activity_last_completed']}.id = ( SELECT max(activity_id) FROM civicrm_case_activity cca, civicrm_activity ca WHERE ca.id = cca.activity_id AND cca.case_id = {$case}.id AND ca.status_id = 2 ) )";
397 public function where() {
400 foreach ($this->_columns
as $tableName => $table) {
401 if (array_key_exists('filters', $table)) {
402 foreach ($table['filters'] as $fieldName => $field) {
405 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
406 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
407 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
408 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
410 $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to, $field['type']);
414 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
415 if ($fieldName == 'case_type_id' &&
416 !empty($this->_params
['case_type_id_value'])
418 foreach ($this->_params
['case_type_id_value'] as $key => $value) {
419 $this->_params
['case_type_id_value'][$key] = $value;
424 $clause = $this->whereClause($field,
426 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
427 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
428 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
433 if (!empty($clause)) {
434 $clauses[] = $clause;
440 if (isset($this->_params
['options']['my_cases'])) {
441 $session = CRM_Core_Session
::singleton();
442 $userID = $session->get('userID');
443 $clauses[] = "{$this->_aliases['civicrm_contact']}.id = {$userID}";
446 if (empty($clauses)) {
447 $this->_where
= 'WHERE ( 1 ) ';
450 $this->_where
= 'WHERE ' . implode(' AND ', $clauses);
454 public function groupBy() {
455 $this->_groupBy
= " GROUP BY {$this->_aliases['civicrm_case']}.id";
456 $this->_groupBy
.= CRM_Contact_BAO_Query
::getGroupByFromSelectColumns($this->_selectClauses
, "{$this->_aliases['civicrm_case']}.id");
464 public function statistics(&$rows) {
465 $statistics = parent
::statistics($rows);
467 $select = "select COUNT( DISTINCT( {$this->_aliases['civicrm_address']}.country_id))";
468 $sql = "{$select} {$this->_from} {$this->_where}";
469 $countryCount = CRM_Core_DAO
::singleValueQuery($sql);
471 $statistics['counts']['case'] = array(
472 'title' => ts('Total Number of Cases '),
473 'value' => isset($statistics['counts']['rowsFound']) ?
$statistics['counts']['rowsFound']['value'] : count($rows),
475 $statistics['counts']['country'] = array(
476 'title' => ts('Total Number of Countries '),
477 'value' => $countryCount,
483 public function orderBy() {
484 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_case']}.start_date DESC ";
487 public function caseDetailSpecialColumnProcess() {
488 if (!$this->_includeCaseDetailExtra
) {
492 $from = $select = array();
493 $case = $this->_aliases
['civicrm_case'];
495 if ($activityType = CRM_Utils_Array
::value('case_activity_all_dates', $this->_params
['case_detail_extra'])) {
496 $select[] = "GROUP_CONCAT(DISTINCT(civireport_activity_all_{$activityType}.{$this->_caseDetailExtra['case_activity_all_dates']['name']}) ORDER BY civireport_activity_all_{$activityType}.{$this->_caseDetailExtra['case_activity_all_dates']['name']}) as case_activity_all_dates";
498 $from[] = " LEFT JOIN civicrm_case_activity civireport_case_activity_all_{$activityType} ON ( civireport_case_activity_all_{$activityType}.case_id = {$case}.id)
499 LEFT JOIN civicrm_activity civireport_activity_all_{$activityType} ON ( civireport_activity_all_{$activityType}.id = civireport_case_activity_all_{$activityType}.activity_id AND civireport_activity_all_{$activityType}.activity_type_id = {$activityType})";
501 $this->_columnHeaders
['case_activity_all_dates'] = array(
502 'title' => $this->_caseDetailExtra
['case_activity_all_dates']['title'] . ": {$this->caseActivityTypes[$activityType]}",
503 'type' => CRM_Utils_Array
::value('type', $this->_caseDetailExtra
['case_activity_all_dates']),
507 $this->_select
.= ', ' . implode(', ', $select) . ' ';
508 $this->_from
.= ' ' . implode(' ', $from) . ' ';
511 public function postProcess() {
513 $this->beginPostProcess();
515 $this->checkEnabledFields();
517 $this->buildQuery(TRUE);
519 $this->caseDetailSpecialColumnProcess();
521 $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy} {$this->_limit}";
523 $rows = $graphRows = array();
524 $this->buildRows($sql, $rows);
526 $this->formatDisplay($rows);
528 $this->doTemplateAssignment($rows);
529 $this->endPostProcess($rows);
532 public function checkEnabledFields() {
533 if (isset($this->_params
['worldregion_id_value']) &&
534 !empty($this->_params
['worldregion_id_value'])
536 $this->_addressField
= TRUE;
537 $this->_worldRegionField
= TRUE;
540 if (isset($this->_params
['case_role_value'])
541 && !empty($this->_params
['case_role_value'])
543 $this->_relField
= TRUE;
546 if (!empty($this->_params
['activity_date_time_relative']) ||
547 !empty($this->_params
['activity_date_time_from']) ||
548 CRM_Utils_Array
::value('activity_date_time_to', $this->_params
)
550 $this->_activityLast
= TRUE;
553 foreach (array_keys($this->_caseDetailExtra
) as $field) {
554 if (!empty($this->_params
['case_detail_extra'][$field])) {
555 $this->_includeCaseDetailExtra
= TRUE;
562 * Alter display of rows.
564 * Iterate through the rows retrieved via SQL and make changes for display purposes,
565 * such as rendering contacts as links.
568 * Rows generated by SQL, with an array for each row.
570 public function alterDisplay(&$rows) {
572 $activityTypes = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE);
574 foreach ($rows as $rowNum => $row) {
575 if (array_key_exists('civicrm_case_status_id', $row)) {
576 if ($value = $row['civicrm_case_status_id']) {
577 $rows[$rowNum]['civicrm_case_status_id'] = $this->case_statuses
[$value];
582 if (array_key_exists('civicrm_case_case_type_id', $row)) {
583 if ($value = str_replace(CRM_Core_DAO
::VALUE_SEPARATOR
, '', $row['civicrm_case_case_type_id'])) {
584 $rows[$rowNum]['civicrm_case_case_type_id'] = $this->case_types
[$value];
589 if (array_key_exists('civicrm_case_subject', $row)) {
590 if ($value = $row['civicrm_case_subject']) {
591 $url = CRM_Utils_System
::url("civicrm/case/ajax/details",
592 "caseId={$row['civicrm_case_id']}&contactId={$row['civicrm_contact_id']}",
595 $rows[$rowNum]['civicrm_case_subject'] = "<a class=\"crm-popup\" href=\"$url\">$value</a>";
596 $rows[$rowNum]['civicrm_case_subject_hover'] = ts('View Details of Case.');
601 if (array_key_exists('civicrm_relationship_case_role', $row)) {
602 if ($value = $row['civicrm_relationship_case_role']) {
603 $caseRoles = explode(',', $value);
604 foreach ($caseRoles as $num => $caseRole) {
605 $caseRoles[$num] = $this->rel_types
[$caseRole];
607 $rows[$rowNum]['civicrm_relationship_case_role'] = implode('; ', $caseRoles);
611 if (array_key_exists('civicrm_address_country_id', $row)) {
612 if ($value = $row['civicrm_address_country_id']) {
613 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
617 if (array_key_exists('civicrm_address_state_province_id', $row)) {
618 if ($value = $row['civicrm_address_state_province_id']) {
619 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
623 if (array_key_exists('civicrm_activity_last_completed_last_completed_activity_subject', $row) &&
624 empty($row['civicrm_activity_last_completed_last_completed_activity_subject'])
626 $rows[$rowNum]['civicrm_activity_last_completed_last_completed_activity_subject'] = ts('(no subject)');
629 if (array_key_exists('civicrm_contact_client_sort_name', $row) &&
630 array_key_exists('civicrm_contact_id', $row)
632 $url = CRM_Utils_System
::url("civicrm/contact/view",
633 'reset=1&cid=' . $row['civicrm_contact_id'],
636 $rows[$rowNum]['civicrm_contact_client_sort_name_link'] = $url;
637 $rows[$rowNum]['civicrm_contact_client_sort_name_hover'] = ts("View Contact Summary for this Contact");
640 if (array_key_exists('civicrm_activity_last_completed_last_completed_activity_type', $row)) {
641 if ($value = $row['civicrm_activity_last_completed_last_completed_activity_type']) {
642 $rows[$rowNum]['civicrm_activity_last_completed_last_completed_activity_type'] = $activityTypes[$value];
647 if (array_key_exists('case_activity_all_dates', $row)) {
648 if ($value = $row['case_activity_all_dates']) {
649 $activityDates = explode(',', $value);
650 foreach ($activityDates as $num => $activityDate) {
651 $activityDates[$num] = CRM_Utils_Date
::customFormat($activityDate);
653 $rows[$rowNum]['case_activity_all_dates'] = implode('; ', $activityDates);
658 if (array_key_exists('civicrm_case_is_deleted', $row)) {
659 $value = $row['civicrm_case_is_deleted'];
660 $rows[$rowNum]['civicrm_case_is_deleted'] = $this->deleted_labels
[$value];