3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
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_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();
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 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
121 'options' => $this->case_statuses
,
123 'case_type_id' => array(
124 'title' => ts('Case Type'),
125 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
126 'options' => $this->case_types
,
128 'is_deleted' => array(
129 'title' => ts('Deleted?'),
130 'type' => CRM_Utils_Type
::T_INT
,
131 'operatorType' => CRM_Report_Form
::OP_SELECT
,
132 'options' => $this->deleted_labels
,
137 'civicrm_contact' => array(
138 'dao' => 'CRM_Contact_DAO_Contact',
140 'client_sort_name' => array(
141 'name' => 'sort_name',
142 'title' => ts('Client Name'),
146 'no_display' => TRUE,
151 'sort_name' => array('title' => ts('Client Name')),
154 'civicrm_relationship' => array(
155 'dao' => 'CRM_Contact_DAO_Relationship',
157 'case_role' => array(
158 'name' => 'relationship_type_id',
159 'title' => ts('Case Role(s)'),
163 'case_role' => array(
164 'name' => 'relationship_type_id',
165 'title' => ts('Case Role(s)'),
166 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
167 'options' => $this->rel_types
,
171 'civicrm_email' => array(
172 'dao' => 'CRM_Core_DAO_Email',
175 'title' => ts('Email'),
179 'grouping' => 'contact-fields',
181 'civicrm_phone' => array(
182 'dao' => 'CRM_Core_DAO_Phone',
185 'title' => ts('Phone'),
189 'grouping' => 'contact-fields',
191 'civicrm_address' => array(
192 'dao' => 'CRM_Core_DAO_Address',
194 'street_address' => NULL,
195 'state_province_id' => array(
196 'title' => ts('State/Province'),
198 'country_id' => array('title' => ts('Country')),
200 'grouping' => 'contact-fields',
202 'country_id' => array(
203 'title' => ts('Country'),
204 'type' => CRM_Utils_Type
::T_INT
,
205 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
206 'options' => CRM_Core_PseudoConstant
::country(),
208 'state_province_id' => array(
209 'title' => ts('State/Province'),
210 'type' => CRM_Utils_Type
::T_INT
,
211 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
212 'options' => CRM_Core_PseudoConstant
::stateProvince(),
216 'civicrm_worldregion' => array(
217 'dao' => 'CRM_Core_DAO_Worldregion',
219 'worldregion_id' => array(
221 'title' => ts('World Region'),
222 'type' => CRM_Utils_Type
::T_INT
,
223 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
224 'options' => CRM_Core_PseudoConstant
::worldRegion(),
228 'civicrm_country' => array(
229 'dao' => 'CRM_Core_DAO_Country',
231 'civicrm_activity_last' => array(
232 'dao' => 'CRM_Activity_DAO_Activity',
234 'last_activity_date_time' => array(
235 'name' => 'activity_date_time',
236 'title' => ts('Last Action Date'),
237 'operatorType' => CRM_Report_Form
::OP_DATE
,
240 'alias' => 'civireport_activity_last',
242 'civicrm_activity_last_completed' => array(
243 'dao' => 'CRM_Activity_DAO_Activity',
245 'last_completed_activity_subject' => array(
247 'title' => ts('Subject of the last completed activity in the case'),
249 'last_completed_activity_type' => array(
250 'name' => 'activity_type_id',
251 'title' => ts('Activity type of the last completed activity'),
257 $this->_options
= array(
259 'title' => ts('My Cases'),
260 'type' => 'checkbox',
263 parent
::__construct();
266 public function preProcess() {
267 parent
::preProcess();
270 public function buildQuickForm() {
271 parent
::buildQuickForm();
272 $this->caseDetailSpecialColumnsAdd();
275 public function caseDetailSpecialColumnsAdd() {
277 $elements[] = &$this->createElement('select', 'case_activity_all_dates', NULL,
279 '' => ts('- select -'),
280 ) +
$this->caseActivityTypes
282 $this->addGroup($elements, 'case_detail_extra');
284 $this->_caseDetailExtra
= array(
285 'case_activity_all_dates' => array(
286 'title' => ts('List of all dates of activities of Type'),
287 'name' => 'activity_date_time',
291 $this->assign('caseDetailExtra', $this->_caseDetailExtra
);
294 public function select() {
296 $this->_columnHeaders
= array();
297 foreach ($this->_columns
as $tableName => $table) {
298 if (array_key_exists('fields', $table)) {
299 foreach ($table['fields'] as $fieldName => $field) {
300 if ($tableName == 'civicrm_address') {
301 $this->_addressField
= TRUE;
303 if (!empty($field['required']) ||
304 !empty($this->_params
['fields'][$fieldName])
306 if ($tableName == 'civicrm_email') {
307 $this->_emailField
= TRUE;
309 elseif ($tableName == 'civicrm_phone') {
310 $this->_phoneField
= TRUE;
312 elseif ($tableName == 'civicrm_relationship') {
313 $this->_relField
= TRUE;
315 if ($fieldName == 'sort_name') {
316 $select[] = "GROUP_CONCAT({$field['dbAlias']} ORDER BY {$field['dbAlias']} )
317 as {$tableName}_{$fieldName}";
319 if ($tableName == 'civicrm_activity_last_completed') {
320 $this->_activityLastCompleted
= TRUE;
323 if ($fieldName == 'case_role') {
324 $select[] = "GROUP_CONCAT(DISTINCT({$field['dbAlias']}) ORDER BY {$field['dbAlias']}) as {$tableName}_{$fieldName}";
327 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
330 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
331 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
337 $this->_select
= 'SELECT ' . implode(', ', $select) . ' ';
340 public function from() {
342 $case = $this->_aliases
['civicrm_case'];
343 $conact = $this->_aliases
['civicrm_contact'];
346 FROM civicrm_case $case
347 LEFT JOIN civicrm_case_contact civireport_case_contact on civireport_case_contact.case_id = {$case}.id
348 LEFT JOIN civicrm_contact $conact ON {$conact}.id = civireport_case_contact.contact_id
350 if ($this->_relField
) {
352 LEFT JOIN civicrm_relationship {$this->_aliases['civicrm_relationship']} ON {$this->_aliases['civicrm_relationship']}.case_id = {$case}.id
356 if ($this->_addressField
) {
358 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
359 ON {$conact}.id = {$this->_aliases['civicrm_address']}.contact_id AND
360 {$this->_aliases['civicrm_address']}.is_primary = 1 ";
362 if ($this->_emailField
) {
364 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
365 ON {$conact}.id = {$this->_aliases['civicrm_email']}.contact_id AND
366 {$this->_aliases['civicrm_email']}.is_primary = 1 ";
368 if ($this->_phoneField
) {
370 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
371 ON ( {$conact}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
372 {$this->_aliases['civicrm_phone']}.is_primary = 1) ";
374 if ($this->_worldRegionField
) {
376 LEFT JOIN civicrm_country {$this->_aliases['civicrm_country']}
377 ON {$this->_aliases['civicrm_country']}.id ={$this->_aliases['civicrm_address']}.country_id
378 LEFT JOIN civicrm_worldregion {$this->_aliases['civicrm_worldregion']}
379 ON {$this->_aliases['civicrm_country']}.region_id = {$this->_aliases['civicrm_worldregion']}.id ";
382 // Include clause for last activity of the case
383 if ($this->_activityLast
) {
384 $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) )";
387 // Include clause for last completed activity of the case
388 if ($this->_activityLastCompleted
) {
389 $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 ) )";
393 public function where() {
396 foreach ($this->_columns
as $tableName => $table) {
397 if (array_key_exists('filters', $table)) {
398 foreach ($table['filters'] as $fieldName => $field) {
401 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
402 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
403 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
404 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
406 $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to, $field['type']);
410 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
411 if ($fieldName == 'case_type_id' &&
412 !empty($this->_params
['case_type_id_value'])
414 foreach ($this->_params
['case_type_id_value'] as $key => $value) {
415 $this->_params
['case_type_id_value'][$key] = $value;
420 $clause = $this->whereClause($field,
422 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
423 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
424 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
429 if (!empty($clause)) {
430 $clauses[] = $clause;
436 if (isset($this->_params
['options']['my_cases'])) {
437 $session = CRM_Core_Session
::singleton();
438 $userID = $session->get('userID');
439 $clauses[] = "{$this->_aliases['civicrm_contact']}.id = {$userID}";
442 if (empty($clauses)) {
443 $this->_where
= 'WHERE ( 1 ) ';
446 $this->_where
= 'WHERE ' . implode(' AND ', $clauses);
450 public function groupBy() {
451 $this->_groupBy
= " GROUP BY {$this->_aliases['civicrm_case']}.id";
459 public function statistics(&$rows) {
460 $statistics = parent
::statistics($rows);
462 $select = "select COUNT( DISTINCT( {$this->_aliases['civicrm_address']}.country_id))";
463 $sql = "{$select} {$this->_from} {$this->_where}";
464 $countryCount = CRM_Core_DAO
::singleValueQuery($sql);
466 $statistics['counts']['case'] = array(
467 'title' => ts('Total Number of Cases '),
468 'value' => isset($statistics['counts']['rowsFound']) ?
$statistics['counts']['rowsFound']['value'] : count($rows),
470 $statistics['counts']['country'] = array(
471 'title' => ts('Total Number of Countries '),
472 'value' => $countryCount,
478 public function orderBy() {
479 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_case']}.start_date DESC ";
482 public function caseDetailSpecialColumnProcess() {
483 if (!$this->_includeCaseDetailExtra
) {
487 $from = $select = array();
488 $case = $this->_aliases
['civicrm_case'];
490 if ($activityType = CRM_Utils_Array
::value('case_activity_all_dates', $this->_params
['case_detail_extra'])) {
491 $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";
493 $from[] = " LEFT JOIN civicrm_case_activity civireport_case_activity_all_{$activityType} ON ( civireport_case_activity_all_{$activityType}.case_id = {$case}.id)
494 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})";
496 $this->_columnHeaders
['case_activity_all_dates'] = array(
497 'title' => $this->_caseDetailExtra
['case_activity_all_dates']['title'] . ": {$this->caseActivityTypes[$activityType]}",
498 'type' => CRM_Utils_Array
::value('type', $this->_caseDetailExtra
['case_activity_all_dates']),
502 $this->_select
.= ', ' . implode(', ', $select) . ' ';
503 $this->_from
.= ' ' . implode(' ', $from) . ' ';
506 public function postProcess() {
508 $this->beginPostProcess();
510 $this->checkEnabledFields();
512 $this->buildQuery(TRUE);
514 $this->caseDetailSpecialColumnProcess();
516 $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy} {$this->_limit}";
518 $rows = $graphRows = array();
519 $this->buildRows($sql, $rows);
521 $this->formatDisplay($rows);
523 $this->doTemplateAssignment($rows);
524 $this->endPostProcess($rows);
527 public function checkEnabledFields() {
528 if (isset($this->_params
['worldregion_id_value']) &&
529 !empty($this->_params
['worldregion_id_value'])
531 $this->_addressField
= TRUE;
532 $this->_worldRegionField
= TRUE;
535 if (isset($this->_params
['case_role_value'])
536 && !empty($this->_params
['case_role_value'])
538 $this->_relField
= TRUE;
541 if (!empty($this->_params
['activity_date_time_relative']) ||
542 !empty($this->_params
['activity_date_time_from']) ||
543 CRM_Utils_Array
::value('activity_date_time_to', $this->_params
)
545 $this->_activityLast
= TRUE;
548 foreach (array_keys($this->_caseDetailExtra
) as $field) {
549 if (!empty($this->_params
['case_detail_extra'][$field])) {
550 $this->_includeCaseDetailExtra
= TRUE;
557 * Alter display of rows.
559 * Iterate through the rows retrieved via SQL and make changes for display purposes,
560 * such as rendering contacts as links.
563 * Rows generated by SQL, with an array for each row.
565 public function alterDisplay(&$rows) {
567 $activityTypes = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE);
569 foreach ($rows as $rowNum => $row) {
570 if (array_key_exists('civicrm_case_status_id', $row)) {
571 if ($value = $row['civicrm_case_status_id']) {
572 $rows[$rowNum]['civicrm_case_status_id'] = $this->case_statuses
[$value];
577 if (array_key_exists('civicrm_case_case_type_id', $row)) {
578 if ($value = str_replace(CRM_Core_DAO
::VALUE_SEPARATOR
, '', $row['civicrm_case_case_type_id'])) {
579 $rows[$rowNum]['civicrm_case_case_type_id'] = $this->case_types
[$value];
584 if (array_key_exists('civicrm_case_subject', $row)) {
585 if ($value = $row['civicrm_case_subject']) {
586 $url = CRM_Utils_System
::url("civicrm/case/ajax/details",
587 "caseId={$row['civicrm_case_id']}&contactId={$row['civicrm_contact_id']}",
590 $rows[$rowNum]['civicrm_case_subject'] = "<a class=\"crm-popup\" href=\"$url\">$value</a>";
591 $rows[$rowNum]['civicrm_case_subject_hover'] = ts('View Details of Case.');
596 if (array_key_exists('civicrm_relationship_case_role', $row)) {
597 if ($value = $row['civicrm_relationship_case_role']) {
598 $caseRoles = explode(',', $value);
599 foreach ($caseRoles as $num => $caseRole) {
600 $caseRoles[$num] = $this->rel_types
[$caseRole];
602 $rows[$rowNum]['civicrm_relationship_case_role'] = implode('; ', $caseRoles);
606 if (array_key_exists('civicrm_address_country_id', $row)) {
607 if ($value = $row['civicrm_address_country_id']) {
608 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
612 if (array_key_exists('civicrm_address_state_province_id', $row)) {
613 if ($value = $row['civicrm_address_state_province_id']) {
614 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
618 if (array_key_exists('civicrm_activity_last_completed_last_completed_activity_subject', $row) &&
619 empty($row['civicrm_activity_last_completed_last_completed_activity_subject'])
621 $rows[$rowNum]['civicrm_activity_last_completed_last_completed_activity_subject'] = ts('(no subject)');
624 if (array_key_exists('civicrm_contact_client_sort_name', $row) &&
625 array_key_exists('civicrm_contact_id', $row)
627 $url = CRM_Utils_System
::url("civicrm/contact/view",
628 'reset=1&cid=' . $row['civicrm_contact_id'],
631 $rows[$rowNum]['civicrm_contact_client_sort_name_link'] = $url;
632 $rows[$rowNum]['civicrm_contact_client_sort_name_hover'] = ts("View Contact Summary for this Contact");
635 if (array_key_exists('civicrm_activity_last_completed_last_completed_activity_type', $row)) {
636 if ($value = $row['civicrm_activity_last_completed_last_completed_activity_type']) {
637 $rows[$rowNum]['civicrm_activity_last_completed_last_completed_activity_type'] = $activityTypes[$value];
642 if (array_key_exists('case_activity_all_dates', $row)) {
643 if ($value = $row['case_activity_all_dates']) {
644 $activityDates = explode(',', $value);
645 foreach ($activityDates as $num => $activityDate) {
646 $activityDates[$num] = CRM_Utils_Date
::customFormat($activityDate);
648 $rows[$rowNum]['case_activity_all_dates'] = implode('; ', $activityDates);
653 if (array_key_exists('civicrm_case_is_deleted', $row)) {
654 $value = $row['civicrm_case_is_deleted'];
655 $rows[$rowNum]['civicrm_case_is_deleted'] = $this->deleted_labels
[$value];