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_Case_Detail
extends CRM_Report_Form
{
38 protected $_relField = FALSE;
40 protected $_addressField = FALSE;
42 protected $_emailField = FALSE;
44 protected $_phoneField = FALSE;
46 protected $_worldRegionField = FALSE;
48 protected $_activityLast = FALSE;
50 protected $_activityLastCompleted = FALSE;
52 protected $_includeCaseDetailExtra = FALSE;
54 protected $_caseDetailExtra = array(
57 function __construct() {
58 $this->case_statuses
= CRM_Case_PseudoConstant
::caseStatus();
59 $this->case_types
= CRM_Case_PseudoConstant
::caseType();
60 $rels = CRM_Core_PseudoConstant
::relationshipType();
61 foreach ($rels as $relid => $v) {
62 $this->rel_types
[$relid] = $v['label_b_a'];
65 $this->deleted_labels
= array('' => ts('- select -'), 0 => ts('No'), 1 => ts('Yes'));
67 $this->caseActivityTypes
= array();
68 foreach (CRM_Case_PseudoConstant
::caseActivityType() as $typeDetail) {
69 $this->caseActivityTypes
[$typeDetail['id']] = $typeDetail['label'];
72 $this->_columns
= array(
75 'dao' => 'CRM_Case_DAO_Case',
78 'id' => array('title' => ts('Case ID'),
82 'subject' => array('title' => ts('Subject'),
85 'start_date' => array('title' => ts('Start Date'),
86 'type' => CRM_Utils_Type
::T_DATE
,
88 'end_date' => array('title' => ts('End Date'),
89 'type' => CRM_Utils_Type
::T_DATE
,
91 'status_id' => array('title' => ts('Case Status')),
92 'case_type_id' => array('title' => ts('Case Type')),
93 'is_deleted' => array('title' => ts('Deleted?'),
95 'type' => CRM_Utils_Type
::T_INT
,
100 'start_date' => array('title' => ts('Start Date'),
101 'operatorType' => CRM_Report_Form
::OP_DATE
,
102 'type' => CRM_Utils_Type
::T_DATE
,
104 'end_date' => array('title' => ts('End Date'),
105 'operatorType' => CRM_Report_Form
::OP_DATE
,
106 'type' => CRM_Utils_Type
::T_DATE
,
108 'status_id' => array('title' => ts('Case Status'),
109 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
110 'options' => $this->case_statuses
,
112 'case_type_id' => array('title' => ts('Case Type'),
113 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
114 'options' => $this->case_types
,
116 'is_deleted' => array('title' => ts('Deleted?'),
117 'type' => CRM_Utils_Type
::T_INT
,
118 'operatorType' => CRM_Report_Form
::OP_SELECT
,
119 'options' => $this->deleted_labels
,
126 'dao' => 'CRM_Contact_DAO_Contact',
129 'client_sort_name' => array(
130 'name' => 'sort_name',
131 'title' => ts('Client Name'),
135 'no_display' => TRUE,
141 'sort_name' => array('title' => ts('Client Name')),
144 'civicrm_relationship' =>
146 'dao' => 'CRM_Contact_DAO_Relationship',
149 'case_role' => array('name' => 'relationship_type_id',
150 'title' => ts('Case Role(s)'),
157 'name' => 'relationship_type_id',
158 'title' => ts('Case Role(s)'),
159 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
160 'options' => $this->rel_types
,
166 'dao' => 'CRM_Core_DAO_Email',
168 array('email' => array('title' => ts('Email'),
172 'grouping' => 'contact-fields',
176 'dao' => 'CRM_Core_DAO_Phone',
180 array('title' => ts('Phone'),
184 'grouping' => 'contact-fields',
188 'dao' => 'CRM_Core_DAO_Address',
191 'street_address' => NULL,
192 'state_province_id' => array('title' => ts('State/Province'),
194 'country_id' => array('title' => ts('Country')),
196 'grouping' => 'contact-fields',
200 array('title' => ts('Country'),
201 'type' => CRM_Utils_Type
::T_INT
,
202 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
203 'options' => CRM_Core_PseudoConstant
::country(),
205 'state_province_id' =>
206 array('title' => ts('State/Province'),
207 'type' => CRM_Utils_Type
::T_INT
,
208 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
209 'options' => CRM_Core_PseudoConstant
::stateProvince(),
213 'civicrm_worldregion' =>
215 'dao' => 'CRM_Core_DAO_Worldregion',
218 'worldregion_id' => array(
220 'title' => ts('WorldRegion'),
221 'type' => CRM_Utils_Type
::T_INT
,
222 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
223 'options' => CRM_Core_PseudoConstant
::worldRegion(),
229 'dao' => 'CRM_Core_DAO_Country',
231 'civicrm_activity_last' =>
233 'dao' => 'CRM_Activity_DAO_Activity',
236 'last_activity_date_time' =>
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' =>
247 'dao' => 'CRM_Activity_DAO_Activity',
250 'last_completed_activity_subject' =>
253 'title' => ts('Subject of the last completed activity in the case'),
255 'last_completed_activity_type' =>
257 'name' => 'activity_type_id',
258 'title' => ts('Activity type of the last completed activity'),
264 $this->_options
= array(
266 array('title' => ts('My Cases'),
267 'type' => 'checkbox',
270 parent
::__construct();
273 function preProcess() {
274 parent
::preProcess();
277 function buildQuickForm() {
278 parent
::buildQuickForm();
279 $this->caseDetailSpecialColumnsAdd();
282 function caseDetailSpecialColumnsAdd() {
284 $elements[] = &$this->createElement('select', 'case_activity_all_dates', NULL,
286 '' => ts('-- select --')) +
$this->caseActivityTypes
288 $this->addGroup($elements, 'case_detail_extra');
290 $this->_caseDetailExtra
= array('case_activity_all_dates' => array('title' => ts('List of all dates of activities of Type'),
291 'name' => 'activity_date_time',
295 $this->assign('caseDetailExtra', $this->_caseDetailExtra
);
300 $this->_columnHeaders
= array();
301 foreach ($this->_columns
as $tableName => $table) {
302 if (array_key_exists('fields', $table)) {
303 foreach ($table['fields'] as $fieldName => $field) {
304 if ($tableName == 'civicrm_address') {
305 $this->_addressField
= TRUE;
307 if (CRM_Utils_Array
::value('required', $field) ||
308 CRM_Utils_Array
::value($fieldName, $this->_params
['fields'])
310 if ($tableName == 'civicrm_email') {
311 $this->_emailField
= TRUE;
313 elseif ($tableName == 'civicrm_phone') {
314 $this->_phoneField
= TRUE;
316 elseif ($tableName == 'civicrm_relationship') {
317 $this->_relField
= TRUE;
319 if ($fieldName == 'sort_name') {
320 $select[] = "GROUP_CONCAT({$field['dbAlias']} ORDER BY {$field['dbAlias']} )
321 as {$tableName}_{$fieldName}";
323 if ($tableName == 'civicrm_activity_last_completed') {
324 $this->_activityLastCompleted
= TRUE;
327 if ($fieldName == 'case_role') {
328 $select[] = "GROUP_CONCAT(DISTINCT({$field['dbAlias']}) ORDER BY {$field['dbAlias']}) as {$tableName}_{$fieldName}";
331 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
334 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
335 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
341 $this->_select
= 'SELECT ' . implode(', ', $select) . ' ';
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 ) )";
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' && CRM_Utils_Array
::value('case_type_id_value', $this->_params
)) {
416 foreach ($this->_params
['case_type_id_value'] as $key => $value) {
417 if (strpos($value, CRM_Core_DAO
::VALUE_SEPARATOR
) === FALSE) {
418 $value = CRM_Core_DAO
::VALUE_SEPARATOR
. $value . CRM_Core_DAO
::VALUE_SEPARATOR
;
420 $this->_params
['case_type_id_value'][$key] = "'{$value}'";
423 $this->_params
['case_type_id_value'][$key] = $value;
429 $clause = $this->whereClause($field,
431 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
432 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
433 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
438 if (!empty($clause)) {
439 $clauses[] = $clause;
445 if (isset($this->_params
['options']['my_cases'])) {
446 $session = CRM_Core_Session
::singleton();
447 $userID = $session->get('userID');
448 $clauses[] = "{$this->_aliases['civicrm_contact']}.id = {$userID}";
451 if (empty($clauses)) {
452 $this->_where
= 'WHERE ( 1 ) ';
455 $this->_where
= 'WHERE ' . implode(' AND ', $clauses);
460 $this->_groupBy
= " GROUP BY {$this->_aliases['civicrm_case']}.id";
463 function statistics(&$rows) {
464 $statistics = parent
::statistics($rows);
466 $select = "select COUNT( DISTINCT( {$this->_aliases['civicrm_address']}.country_id))";
467 $sql = "{$select} {$this->_from} {$this->_where}";
468 $countryCount = CRM_Core_DAO
::singleValueQuery($sql);
470 //CaseType statistics
471 if (array_key_exists('filters', $statistics)) {
472 foreach ($statistics['filters'] as $id => $value) {
473 if ($value['title'] == 'Case Type') {
474 $statistics['filters'][$id]['value'] = 'Is ' . $this->case_types
[substr($statistics['filters'][$id]
480 $statistics['counts']['case'] = array(
481 'title' => ts('Total Number of Cases '),
482 'value' => isset($statistics['counts']['rowsFound']) ?
$statistics['counts']['rowsFound']['value'] : count($rows),
484 $statistics['counts']['country'] = array(
485 'title' => ts('Total Number of Countries '),
486 'value' => $countryCount,
493 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_case']}.start_date DESC ";
496 function caseDetailSpecialColumnProcess() {
497 if (!$this->_includeCaseDetailExtra
) {
501 $from = $select = array();
502 $case = $this->_aliases
['civicrm_case'];
504 if ($activityType = CRM_Utils_Array
::value('case_activity_all_dates', $this->_params
['case_detail_extra'])) {
505 $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";
507 $from[] = " LEFT JOIN civicrm_case_activity civireport_case_activity_all_{$activityType} ON ( civireport_case_activity_all_{$activityType}.case_id = {$case}.id)
508 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})";
510 $this->_columnHeaders
['case_activity_all_dates'] = array(
511 'title' => $this->_caseDetailExtra
['case_activity_all_dates']['title'] . ": {$this->caseActivityTypes[$activityType]}",
512 'type' => CRM_Utils_Array
::value('type', $this->_caseDetailExtra
['case_activity_all_dates']),
516 $this->_select
.= ', ' . implode(', ', $select) . ' ';
517 $this->_from
.= ' ' . implode(' ', $from) . ' ';
520 function postProcess() {
522 $this->beginPostProcess();
524 $this->checkEnabledFields();
526 $this->buildQuery(TRUE);
528 $this->caseDetailSpecialColumnProcess();
530 $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy} {$this->_limit}";
532 $rows = $graphRows = array();
533 $this->buildRows($sql, $rows);
535 $this->formatDisplay($rows);
537 $this->doTemplateAssignment($rows);
538 $this->endPostProcess($rows);
541 function checkEnabledFields() {
542 if (isset($this->_params
['worldregion_id_value']) && !empty($this->_params
['worldregion_id_value'])) {
543 $this->_addressField
= TRUE;
544 $this->_worldRegionField
= TRUE;
547 if (isset($this->_params
['case_role_value'])
548 && !empty($this->_params
['case_role_value'])
550 $this->_relField
= TRUE;
553 if (CRM_Utils_Array
::value('activity_date_time_relative', $this->_params
) ||
554 CRM_Utils_Array
::value('activity_date_time_from', $this->_params
) ||
555 CRM_Utils_Array
::value('activity_date_time_to', $this->_params
)
557 $this->_activityLast
= TRUE;
560 foreach (array_keys($this->_caseDetailExtra
) as $field) {
561 if (CRM_Utils_Array
::value($field, $this->_params
['case_detail_extra'])) {
562 $this->_includeCaseDetailExtra
= TRUE;
568 function alterDisplay(&$rows) {
570 $activityTypes = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE);
572 foreach ($rows as $rowNum => $row) {
573 if (array_key_exists('civicrm_case_status_id', $row)) {
574 if ($value = $row['civicrm_case_status_id']) {
575 $rows[$rowNum]['civicrm_case_status_id'] = $this->case_statuses
[$value];
580 if (array_key_exists('civicrm_case_case_type_id', $row)) {
581 if ($value = str_replace(CRM_Core_DAO
::VALUE_SEPARATOR
, '', $row['civicrm_case_case_type_id'])) {
582 $rows[$rowNum]['civicrm_case_case_type_id'] = $this->case_types
[$value];
587 if (array_key_exists('civicrm_case_subject', $row)) {
588 if ($value = $row['civicrm_case_subject']) {
589 $caseId = $row['civicrm_case_id'];
590 $contactId = $row['civicrm_contact_id'];
591 $rows[$rowNum]['civicrm_case_subject'] = "<a href= 'javascript:viewCase( $caseId,$contactId );'>$value</a>";
592 $rows[$rowNum]['civicrm_case_subject_hover'] = ts('View Details of Case.');
597 if (array_key_exists('civicrm_relationship_case_role', $row)) {
598 if ($value = $row['civicrm_relationship_case_role']) {
599 $caseRoles = explode(',', $value);
600 foreach ($caseRoles as $num => $caseRole) {
601 $caseRoles[$num] = $this->rel_types
[$caseRole];
603 $rows[$rowNum]['civicrm_relationship_case_role'] = implode('; ', $caseRoles);
607 if (array_key_exists('civicrm_address_country_id', $row)) {
608 if ($value = $row['civicrm_address_country_id']) {
609 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
613 if (array_key_exists('civicrm_address_state_province_id', $row)) {
614 if ($value = $row['civicrm_address_state_province_id']) {
615 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
619 if (array_key_exists('civicrm_activity_last_completed_last_completed_activity_subject', $row) &&
620 !CRM_Utils_Array
::value('civicrm_activity_last_completed_last_completed_activity_subject', $row)
622 $rows[$rowNum]['civicrm_activity_last_completed_last_completed_activity_subject'] = ts('(No Subject)');
625 if (array_key_exists('civicrm_contact_client_sort_name', $row) &&
626 array_key_exists('civicrm_contact_id', $row)
628 $url = CRM_Utils_System
::url("civicrm/contact/view",
629 'reset=1&cid=' . $row['civicrm_contact_id'],
632 $rows[$rowNum]['civicrm_contact_client_sort_name_link'] = $url;
633 $rows[$rowNum]['civicrm_contact_client_sort_name_hover'] = ts("View Contact Summary for this Contact");
636 if (array_key_exists('civicrm_activity_last_completed_last_completed_activity_type', $row)) {
637 if ($value = $row['civicrm_activity_last_completed_last_completed_activity_type']) {
638 $rows[$rowNum]['civicrm_activity_last_completed_last_completed_activity_type'] = $activityTypes[$value];
643 if (array_key_exists('case_activity_all_dates', $row)) {
644 if ($value = $row['case_activity_all_dates']) {
645 $activityDates = explode(',', $value);
646 foreach ($activityDates as $num => $activityDate) {
647 $activityDates[$num] = CRM_Utils_Date
::customFormat($activityDate);
649 $rows[$rowNum]['case_activity_all_dates'] = implode('; ', $activityDates);
654 if (array_key_exists('civicrm_case_is_deleted', $row)) {
655 $value = $row['civicrm_case_is_deleted'];
656 $rows[$rowNum]['civicrm_case_is_deleted'] = $this->deleted_labels
[$value];