4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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();
62 function __construct() {
63 $this->case_statuses
= CRM_Core_OptionGroup
::values('case_status');
64 $this->case_types
= CRM_Case_PseudoConstant
::caseType();
65 $rels = CRM_Core_PseudoConstant
::relationshipType();
66 foreach ($rels as $relid => $v) {
67 $this->rel_types
[$relid] = $v['label_b_a'];
70 $this->deleted_labels
= array(
71 '' => ts('- select -'),
76 $this->caseActivityTypes
= array();
77 foreach (CRM_Case_PseudoConstant
::caseActivityType() as $typeDetail) {
78 $this->caseActivityTypes
[$typeDetail['id']] = $typeDetail['label'];
81 $this->_columns
= array(
82 'civicrm_case' => array(
83 'dao' => 'CRM_Case_DAO_Case',
86 'title' => ts('Case ID'),
91 'title' => ts('Subject'),
94 'start_date' => array(
95 'title' => ts('Start Date'),
96 'type' => CRM_Utils_Type
::T_DATE
,
99 'title' => ts('End Date'),
100 'type' => CRM_Utils_Type
::T_DATE
,
102 'status_id' => array('title' => ts('Case Status')),
103 'case_type_id' => array('title' => ts('Case Type')),
104 'is_deleted' => array(
105 'title' => ts('Deleted?'),
107 'type' => CRM_Utils_Type
::T_INT
,
111 'start_date' => array(
112 'title' => ts('Start Date'),
113 'operatorType' => CRM_Report_Form
::OP_DATE
,
114 'type' => CRM_Utils_Type
::T_DATE
,
117 'title' => ts('End Date'),
118 'operatorType' => CRM_Report_Form
::OP_DATE
,
119 'type' => CRM_Utils_Type
::T_DATE
,
121 'status_id' => array(
122 'title' => ts('Case Status'),
123 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
124 'options' => $this->case_statuses
,
126 'case_type_id' => array(
127 'title' => ts('Case Type'),
128 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
129 'options' => $this->case_types
,
131 'is_deleted' => array(
132 'title' => ts('Deleted?'),
133 'type' => CRM_Utils_Type
::T_INT
,
134 'operatorType' => CRM_Report_Form
::OP_SELECT
,
135 'options' => $this->deleted_labels
,
140 'civicrm_contact' => array(
141 'dao' => 'CRM_Contact_DAO_Contact',
143 'client_sort_name' => array(
144 'name' => 'sort_name',
145 'title' => ts('Client Name'),
149 'no_display' => TRUE,
154 'sort_name' => array('title' => ts('Client Name')),
157 'civicrm_relationship' => array(
158 'dao' => 'CRM_Contact_DAO_Relationship',
160 'case_role' => array(
161 'name' => 'relationship_type_id',
162 'title' => ts('Case Role(s)'),
166 'case_role' => array(
167 'name' => 'relationship_type_id',
168 'title' => ts('Case Role(s)'),
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 function preProcess() {
270 parent
::preProcess();
273 function buildQuickForm() {
274 parent
::buildQuickForm();
275 $this->caseDetailSpecialColumnsAdd();
278 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
);
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'];
340 $this->_select
= 'SELECT ' . implode(', ', $select) . ' ';
345 $case = $this->_aliases
['civicrm_case'];
346 $conact = $this->_aliases
['civicrm_contact'];
349 FROM civicrm_case $case
350 LEFT JOIN civicrm_case_contact civireport_case_contact on civireport_case_contact.case_id = {$case}.id
351 LEFT JOIN civicrm_contact $conact ON {$conact}.id = civireport_case_contact.contact_id
353 if ($this->_relField
) {
355 LEFT JOIN civicrm_relationship {$this->_aliases['civicrm_relationship']} ON {$this->_aliases['civicrm_relationship']}.case_id = {$case}.id
359 if ($this->_addressField
) {
361 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
362 ON {$conact}.id = {$this->_aliases['civicrm_address']}.contact_id AND
363 {$this->_aliases['civicrm_address']}.is_primary = 1 ";
365 if ($this->_emailField
) {
367 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
368 ON {$conact}.id = {$this->_aliases['civicrm_email']}.contact_id AND
369 {$this->_aliases['civicrm_email']}.is_primary = 1 ";
371 if ($this->_phoneField
) {
373 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
374 ON ( {$conact}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
375 {$this->_aliases['civicrm_phone']}.is_primary = 1) ";
377 if ($this->_worldRegionField
) {
379 LEFT JOIN civicrm_country {$this->_aliases['civicrm_country']}
380 ON {$this->_aliases['civicrm_country']}.id ={$this->_aliases['civicrm_address']}.country_id
381 LEFT JOIN civicrm_worldregion {$this->_aliases['civicrm_worldregion']}
382 ON {$this->_aliases['civicrm_country']}.region_id = {$this->_aliases['civicrm_worldregion']}.id ";
385 // Include clause for last activity of the case
386 if ($this->_activityLast
) {
387 $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) )";
390 // Include clause for last completed activity of the case
391 if ($this->_activityLastCompleted
) {
392 $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 ) )";
399 foreach ($this->_columns
as $tableName => $table) {
400 if (array_key_exists('filters', $table)) {
401 foreach ($table['filters'] as $fieldName => $field) {
404 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
405 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
406 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
407 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
409 $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to, $field['type']);
413 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
414 if ($fieldName == 'case_type_id' &&
415 !empty($this->_params
['case_type_id_value'])
417 foreach ($this->_params
['case_type_id_value'] as $key => $value) {
418 if (strpos($value, CRM_Core_DAO
::VALUE_SEPARATOR
) === FALSE) {
419 $value = CRM_Core_DAO
::VALUE_SEPARATOR
. $value .
420 CRM_Core_DAO
::VALUE_SEPARATOR
;
422 $this->_params
['case_type_id_value'][$key] = "'{$value}'";
425 $this->_params
['case_type_id_value'][$key] = $value;
431 $clause = $this->whereClause($field,
433 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
434 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
435 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
440 if (!empty($clause)) {
441 $clauses[] = $clause;
447 if (isset($this->_params
['options']['my_cases'])) {
448 $session = CRM_Core_Session
::singleton();
449 $userID = $session->get('userID');
450 $clauses[] = "{$this->_aliases['civicrm_contact']}.id = {$userID}";
453 if (empty($clauses)) {
454 $this->_where
= 'WHERE ( 1 ) ';
457 $this->_where
= 'WHERE ' . implode(' AND ', $clauses);
462 $this->_groupBy
= " GROUP BY {$this->_aliases['civicrm_case']}.id";
470 function statistics(&$rows) {
471 $statistics = parent
::statistics($rows);
473 $select = "select COUNT( DISTINCT( {$this->_aliases['civicrm_address']}.country_id))";
474 $sql = "{$select} {$this->_from} {$this->_where}";
475 $countryCount = CRM_Core_DAO
::singleValueQuery($sql);
477 //CaseType statistics
478 if (array_key_exists('filters', $statistics)) {
479 foreach ($statistics['filters'] as $id => $value) {
480 if ($value['title'] == 'Case Type') {
481 $statistics['filters'][$id]['value'] = 'Is ' .
482 $this->case_types
[substr($statistics['filters'][$id]
488 $statistics['counts']['case'] = array(
489 'title' => ts('Total Number of Cases '),
490 'value' => isset($statistics['counts']['rowsFound']) ?
$statistics['counts']['rowsFound']['value'] : count($rows),
492 $statistics['counts']['country'] = array(
493 'title' => ts('Total Number of Countries '),
494 'value' => $countryCount,
501 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_case']}.start_date DESC ";
504 function caseDetailSpecialColumnProcess() {
505 if (!$this->_includeCaseDetailExtra
) {
509 $from = $select = array();
510 $case = $this->_aliases
['civicrm_case'];
512 if ($activityType = CRM_Utils_Array
::value('case_activity_all_dates', $this->_params
['case_detail_extra'])) {
513 $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";
515 $from[] = " LEFT JOIN civicrm_case_activity civireport_case_activity_all_{$activityType} ON ( civireport_case_activity_all_{$activityType}.case_id = {$case}.id)
516 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})";
518 $this->_columnHeaders
['case_activity_all_dates'] = array(
519 'title' => $this->_caseDetailExtra
['case_activity_all_dates']['title'] .
520 ": {$this->caseActivityTypes[$activityType]}",
521 'type' => CRM_Utils_Array
::value('type', $this->_caseDetailExtra
['case_activity_all_dates']),
525 $this->_select
.= ', ' . implode(', ', $select) . ' ';
526 $this->_from
.= ' ' . implode(' ', $from) . ' ';
529 function postProcess() {
531 $this->beginPostProcess();
533 $this->checkEnabledFields();
535 $this->buildQuery(TRUE);
537 $this->caseDetailSpecialColumnProcess();
539 $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy} {$this->_limit}";
541 $rows = $graphRows = array();
542 $this->buildRows($sql, $rows);
544 $this->formatDisplay($rows);
546 $this->doTemplateAssignment($rows);
547 $this->endPostProcess($rows);
550 function checkEnabledFields() {
551 if (isset($this->_params
['worldregion_id_value']) &&
552 !empty($this->_params
['worldregion_id_value'])
554 $this->_addressField
= TRUE;
555 $this->_worldRegionField
= TRUE;
558 if (isset($this->_params
['case_role_value'])
559 && !empty($this->_params
['case_role_value'])
561 $this->_relField
= TRUE;
564 if (!empty($this->_params
['activity_date_time_relative']) ||
565 !empty($this->_params
['activity_date_time_from']) ||
566 CRM_Utils_Array
::value('activity_date_time_to', $this->_params
)
568 $this->_activityLast
= TRUE;
571 foreach (array_keys($this->_caseDetailExtra
) as $field) {
572 if (!empty($this->_params
['case_detail_extra'][$field])) {
573 $this->_includeCaseDetailExtra
= TRUE;
582 function alterDisplay(&$rows) {
584 $activityTypes = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE);
586 foreach ($rows as $rowNum => $row) {
587 if (array_key_exists('civicrm_case_status_id', $row)) {
588 if ($value = $row['civicrm_case_status_id']) {
589 $rows[$rowNum]['civicrm_case_status_id'] = $this->case_statuses
[$value];
594 if (array_key_exists('civicrm_case_case_type_id', $row)) {
595 if ($value = str_replace(CRM_Core_DAO
::VALUE_SEPARATOR
, '', $row['civicrm_case_case_type_id'])) {
596 $rows[$rowNum]['civicrm_case_case_type_id'] = $this->case_types
[$value];
601 if (array_key_exists('civicrm_case_subject', $row)) {
602 if ($value = $row['civicrm_case_subject']) {
603 $caseId = $row['civicrm_case_id'];
604 $contactId = $row['civicrm_contact_id'];
605 $rows[$rowNum]['civicrm_case_subject'] = "<a href= 'javascript:viewCase( $caseId,$contactId );'>$value</a>";
606 $rows[$rowNum]['civicrm_case_subject_hover'] = ts('View Details of Case.');
611 if (array_key_exists('civicrm_relationship_case_role', $row)) {
612 if ($value = $row['civicrm_relationship_case_role']) {
613 $caseRoles = explode(',', $value);
614 foreach ($caseRoles as $num => $caseRole) {
615 $caseRoles[$num] = $this->rel_types
[$caseRole];
617 $rows[$rowNum]['civicrm_relationship_case_role'] = implode('; ', $caseRoles);
621 if (array_key_exists('civicrm_address_country_id', $row)) {
622 if ($value = $row['civicrm_address_country_id']) {
623 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
627 if (array_key_exists('civicrm_address_state_province_id', $row)) {
628 if ($value = $row['civicrm_address_state_province_id']) {
629 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
633 if (array_key_exists('civicrm_activity_last_completed_last_completed_activity_subject', $row) &&
634 empty($row['civicrm_activity_last_completed_last_completed_activity_subject'])
636 $rows[$rowNum]['civicrm_activity_last_completed_last_completed_activity_subject'] = ts('(no subject)');
639 if (array_key_exists('civicrm_contact_client_sort_name', $row) &&
640 array_key_exists('civicrm_contact_id', $row)
642 $url = CRM_Utils_System
::url("civicrm/contact/view",
643 'reset=1&cid=' . $row['civicrm_contact_id'],
646 $rows[$rowNum]['civicrm_contact_client_sort_name_link'] = $url;
647 $rows[$rowNum]['civicrm_contact_client_sort_name_hover'] = ts("View Contact Summary for this Contact");
650 if (array_key_exists('civicrm_activity_last_completed_last_completed_activity_type', $row)) {
651 if ($value = $row['civicrm_activity_last_completed_last_completed_activity_type']) {
652 $rows[$rowNum]['civicrm_activity_last_completed_last_completed_activity_type'] = $activityTypes[$value];
657 if (array_key_exists('case_activity_all_dates', $row)) {
658 if ($value = $row['case_activity_all_dates']) {
659 $activityDates = explode(',', $value);
660 foreach ($activityDates as $num => $activityDate) {
661 $activityDates[$num] = CRM_Utils_Date
::customFormat($activityDate);
663 $rows[$rowNum]['case_activity_all_dates'] = implode('; ', $activityDates);
668 if (array_key_exists('civicrm_case_is_deleted', $row)) {
669 $value = $row['civicrm_case_is_deleted'];
670 $rows[$rowNum]['civicrm_case_is_deleted'] = $this->deleted_labels
[$value];