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_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');
61 public function __construct() {
62 $this->case_statuses
= CRM_Core_OptionGroup
::values('case_status');
63 $this->case_types
= CRM_Case_PseudoConstant
::caseType();
64 $rels = CRM_Core_PseudoConstant
::relationshipType();
65 foreach ($rels as $relid => $v) {
66 $this->rel_types
[$relid] = $v['label_b_a'];
69 $this->deleted_labels
= array(
70 '' => ts('- select -'),
75 $this->caseActivityTypes
= array();
76 foreach (CRM_Case_PseudoConstant
::caseActivityType() as $typeDetail) {
77 $this->caseActivityTypes
[$typeDetail['id']] = $typeDetail['label'];
80 $this->_columns
= array(
81 'civicrm_case' => array(
82 'dao' => 'CRM_Case_DAO_Case',
85 'title' => ts('Case ID'),
90 'title' => ts('Subject'),
93 'start_date' => array(
94 'title' => ts('Start Date'),
95 'type' => CRM_Utils_Type
::T_DATE
,
98 'title' => ts('End Date'),
99 'type' => CRM_Utils_Type
::T_DATE
,
101 'status_id' => array('title' => ts('Case Status')),
102 'case_type_id' => array('title' => ts('Case Type')),
103 'is_deleted' => array(
104 'title' => ts('Deleted?'),
106 'type' => CRM_Utils_Type
::T_INT
,
110 'start_date' => array(
111 'title' => ts('Start Date'),
112 'operatorType' => CRM_Report_Form
::OP_DATE
,
113 'type' => CRM_Utils_Type
::T_DATE
,
116 'title' => ts('End Date'),
117 'operatorType' => CRM_Report_Form
::OP_DATE
,
118 'type' => CRM_Utils_Type
::T_DATE
,
120 'status_id' => array(
121 'title' => ts('Case Status'),
122 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
123 'options' => $this->case_statuses
,
125 'case_type_id' => array(
126 'title' => ts('Case Type'),
127 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
128 'options' => $this->case_types
,
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 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
169 'options' => $this->rel_types
,
173 'civicrm_email' => array(
174 'dao' => 'CRM_Core_DAO_Email',
177 'title' => ts('Email'),
181 'grouping' => 'contact-fields',
183 'civicrm_phone' => array(
184 'dao' => 'CRM_Core_DAO_Phone',
187 'title' => ts('Phone'),
191 'grouping' => 'contact-fields',
193 'civicrm_address' => array(
194 'dao' => 'CRM_Core_DAO_Address',
196 'street_address' => NULL,
197 'state_province_id' => array(
198 'title' => ts('State/Province'),
200 'country_id' => array('title' => ts('Country')),
202 'grouping' => 'contact-fields',
204 'country_id' => array(
205 'title' => ts('Country'),
206 'type' => CRM_Utils_Type
::T_INT
,
207 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
208 'options' => CRM_Core_PseudoConstant
::country(),
210 'state_province_id' => array(
211 'title' => ts('State/Province'),
212 'type' => CRM_Utils_Type
::T_INT
,
213 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
214 'options' => CRM_Core_PseudoConstant
::stateProvince(),
218 'civicrm_worldregion' => array(
219 'dao' => 'CRM_Core_DAO_Worldregion',
221 'worldregion_id' => array(
223 'title' => ts('World Region'),
224 'type' => CRM_Utils_Type
::T_INT
,
225 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
226 'options' => CRM_Core_PseudoConstant
::worldRegion(),
230 'civicrm_country' => array(
231 'dao' => 'CRM_Core_DAO_Country',
233 'civicrm_activity_last' => array(
234 'dao' => 'CRM_Activity_DAO_Activity',
236 'last_activity_date_time' => array(
237 'name' => 'activity_date_time',
238 'title' => ts('Last Action Date'),
239 'operatorType' => CRM_Report_Form
::OP_DATE
,
242 'alias' => 'civireport_activity_last',
244 'civicrm_activity_last_completed' => array(
245 'dao' => 'CRM_Activity_DAO_Activity',
247 'last_completed_activity_subject' => array(
249 'title' => ts('Subject of the last completed activity in the case'),
251 'last_completed_activity_type' => array(
252 'name' => 'activity_type_id',
253 'title' => ts('Activity type of the last completed activity'),
259 $this->_options
= array(
261 'title' => ts('My Cases'),
262 'type' => 'checkbox',
265 parent
::__construct();
268 public function preProcess() {
269 parent
::preProcess();
272 public function buildQuickForm() {
273 parent
::buildQuickForm();
274 $this->caseDetailSpecialColumnsAdd();
277 public function caseDetailSpecialColumnsAdd() {
279 $elements[] = &$this->createElement('select', 'case_activity_all_dates', NULL,
281 '' => ts('- select -'),
282 ) +
$this->caseActivityTypes
284 $this->addGroup($elements, 'case_detail_extra');
286 $this->_caseDetailExtra
= array(
287 'case_activity_all_dates' => array(
288 'title' => ts('List of all dates of activities of Type'),
289 'name' => 'activity_date_time',
293 $this->assign('caseDetailExtra', $this->_caseDetailExtra
);
296 public function select() {
298 $this->_columnHeaders
= array();
299 foreach ($this->_columns
as $tableName => $table) {
300 if (array_key_exists('fields', $table)) {
301 foreach ($table['fields'] as $fieldName => $field) {
302 if ($tableName == 'civicrm_address') {
303 $this->_addressField
= TRUE;
305 if (!empty($field['required']) ||
306 !empty($this->_params
['fields'][$fieldName])
308 if ($tableName == 'civicrm_email') {
309 $this->_emailField
= TRUE;
311 elseif ($tableName == 'civicrm_phone') {
312 $this->_phoneField
= TRUE;
314 elseif ($tableName == 'civicrm_relationship') {
315 $this->_relField
= TRUE;
317 if ($fieldName == 'sort_name') {
318 $select[] = "GROUP_CONCAT({$field['dbAlias']} ORDER BY {$field['dbAlias']} )
319 as {$tableName}_{$fieldName}";
321 if ($tableName == 'civicrm_activity_last_completed') {
322 $this->_activityLastCompleted
= TRUE;
325 if ($fieldName == 'case_role') {
326 $select[] = "GROUP_CONCAT(DISTINCT({$field['dbAlias']}) ORDER BY {$field['dbAlias']}) as {$tableName}_{$fieldName}";
329 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
332 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
333 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
339 $this->_select
= 'SELECT ' . implode(', ', $select) . ' ';
342 public function from() {
344 $case = $this->_aliases
['civicrm_case'];
345 $conact = $this->_aliases
['civicrm_contact'];
348 FROM civicrm_case $case
349 LEFT JOIN civicrm_case_contact civireport_case_contact on civireport_case_contact.case_id = {$case}.id
350 LEFT JOIN civicrm_contact $conact ON {$conact}.id = civireport_case_contact.contact_id
352 if ($this->_relField
) {
354 LEFT JOIN civicrm_relationship {$this->_aliases['civicrm_relationship']} ON {$this->_aliases['civicrm_relationship']}.case_id = {$case}.id
358 if ($this->_addressField
) {
360 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
361 ON {$conact}.id = {$this->_aliases['civicrm_address']}.contact_id AND
362 {$this->_aliases['civicrm_address']}.is_primary = 1 ";
364 if ($this->_emailField
) {
366 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
367 ON {$conact}.id = {$this->_aliases['civicrm_email']}.contact_id AND
368 {$this->_aliases['civicrm_email']}.is_primary = 1 ";
370 if ($this->_phoneField
) {
372 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
373 ON ( {$conact}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
374 {$this->_aliases['civicrm_phone']}.is_primary = 1) ";
376 if ($this->_worldRegionField
) {
378 LEFT JOIN civicrm_country {$this->_aliases['civicrm_country']}
379 ON {$this->_aliases['civicrm_country']}.id ={$this->_aliases['civicrm_address']}.country_id
380 LEFT JOIN civicrm_worldregion {$this->_aliases['civicrm_worldregion']}
381 ON {$this->_aliases['civicrm_country']}.region_id = {$this->_aliases['civicrm_worldregion']}.id ";
384 // Include clause for last activity of the case
385 if ($this->_activityLast
) {
386 $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) )";
389 // Include clause for last completed activity of the case
390 if ($this->_activityLastCompleted
) {
391 $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 ) )";
395 public function where() {
398 foreach ($this->_columns
as $tableName => $table) {
399 if (array_key_exists('filters', $table)) {
400 foreach ($table['filters'] as $fieldName => $field) {
403 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
404 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
405 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
406 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
408 $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to, $field['type']);
412 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
413 if ($fieldName == 'case_type_id' &&
414 !empty($this->_params
['case_type_id_value'])
416 foreach ($this->_params
['case_type_id_value'] as $key => $value) {
417 $this->_params
['case_type_id_value'][$key] = $value;
422 $clause = $this->whereClause($field,
424 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
425 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
426 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
431 if (!empty($clause)) {
432 $clauses[] = $clause;
438 if (isset($this->_params
['options']['my_cases'])) {
439 $session = CRM_Core_Session
::singleton();
440 $userID = $session->get('userID');
441 $clauses[] = "{$this->_aliases['civicrm_contact']}.id = {$userID}";
444 if (empty($clauses)) {
445 $this->_where
= 'WHERE ( 1 ) ';
448 $this->_where
= 'WHERE ' . implode(' AND ', $clauses);
452 public function groupBy() {
453 $this->_groupBy
= " GROUP BY {$this->_aliases['civicrm_case']}.id";
461 public function statistics(&$rows) {
462 $statistics = parent
::statistics($rows);
464 $select = "select COUNT( DISTINCT( {$this->_aliases['civicrm_address']}.country_id))";
465 $sql = "{$select} {$this->_from} {$this->_where}";
466 $countryCount = CRM_Core_DAO
::singleValueQuery($sql);
468 $statistics['counts']['case'] = array(
469 'title' => ts('Total Number of Cases '),
470 'value' => isset($statistics['counts']['rowsFound']) ?
$statistics['counts']['rowsFound']['value'] : count($rows),
472 $statistics['counts']['country'] = array(
473 'title' => ts('Total Number of Countries '),
474 'value' => $countryCount,
480 public function orderBy() {
481 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_case']}.start_date DESC ";
484 public function caseDetailSpecialColumnProcess() {
485 if (!$this->_includeCaseDetailExtra
) {
489 $from = $select = array();
490 $case = $this->_aliases
['civicrm_case'];
492 if ($activityType = CRM_Utils_Array
::value('case_activity_all_dates', $this->_params
['case_detail_extra'])) {
493 $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";
495 $from[] = " LEFT JOIN civicrm_case_activity civireport_case_activity_all_{$activityType} ON ( civireport_case_activity_all_{$activityType}.case_id = {$case}.id)
496 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})";
498 $this->_columnHeaders
['case_activity_all_dates'] = array(
499 'title' => $this->_caseDetailExtra
['case_activity_all_dates']['title'] . ": {$this->caseActivityTypes[$activityType]}",
500 'type' => CRM_Utils_Array
::value('type', $this->_caseDetailExtra
['case_activity_all_dates']),
504 $this->_select
.= ', ' . implode(', ', $select) . ' ';
505 $this->_from
.= ' ' . implode(' ', $from) . ' ';
508 public function postProcess() {
510 $this->beginPostProcess();
512 $this->checkEnabledFields();
514 $this->buildQuery(TRUE);
516 $this->caseDetailSpecialColumnProcess();
518 $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy} {$this->_limit}";
520 $rows = $graphRows = array();
521 $this->buildRows($sql, $rows);
523 $this->formatDisplay($rows);
525 $this->doTemplateAssignment($rows);
526 $this->endPostProcess($rows);
529 public function checkEnabledFields() {
530 if (isset($this->_params
['worldregion_id_value']) &&
531 !empty($this->_params
['worldregion_id_value'])
533 $this->_addressField
= TRUE;
534 $this->_worldRegionField
= TRUE;
537 if (isset($this->_params
['case_role_value'])
538 && !empty($this->_params
['case_role_value'])
540 $this->_relField
= TRUE;
543 if (!empty($this->_params
['activity_date_time_relative']) ||
544 !empty($this->_params
['activity_date_time_from']) ||
545 CRM_Utils_Array
::value('activity_date_time_to', $this->_params
)
547 $this->_activityLast
= TRUE;
550 foreach (array_keys($this->_caseDetailExtra
) as $field) {
551 if (!empty($this->_params
['case_detail_extra'][$field])) {
552 $this->_includeCaseDetailExtra
= TRUE;
559 * Alter display of rows.
561 * Iterate through the rows retrieved via SQL and make changes for display purposes,
562 * such as rendering contacts as links.
565 * Rows generated by SQL, with an array for each row.
567 public function alterDisplay(&$rows) {
569 $activityTypes = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE);
571 foreach ($rows as $rowNum => $row) {
572 if (array_key_exists('civicrm_case_status_id', $row)) {
573 if ($value = $row['civicrm_case_status_id']) {
574 $rows[$rowNum]['civicrm_case_status_id'] = $this->case_statuses
[$value];
579 if (array_key_exists('civicrm_case_case_type_id', $row)) {
580 if ($value = str_replace(CRM_Core_DAO
::VALUE_SEPARATOR
, '', $row['civicrm_case_case_type_id'])) {
581 $rows[$rowNum]['civicrm_case_case_type_id'] = $this->case_types
[$value];
586 if (array_key_exists('civicrm_case_subject', $row)) {
587 if ($value = $row['civicrm_case_subject']) {
588 $url = CRM_Utils_System
::url("civicrm/case/ajax/details",
589 "caseId={$row['civicrm_case_id']}&contactId={$row['civicrm_contact_id']}",
592 $rows[$rowNum]['civicrm_case_subject'] = "<a class=\"crm-popup\" href=\"$url\">$value</a>";
593 $rows[$rowNum]['civicrm_case_subject_hover'] = ts('View Details of Case.');
598 if (array_key_exists('civicrm_relationship_case_role', $row)) {
599 if ($value = $row['civicrm_relationship_case_role']) {
600 $caseRoles = explode(',', $value);
601 foreach ($caseRoles as $num => $caseRole) {
602 $caseRoles[$num] = $this->rel_types
[$caseRole];
604 $rows[$rowNum]['civicrm_relationship_case_role'] = implode('; ', $caseRoles);
608 if (array_key_exists('civicrm_address_country_id', $row)) {
609 if ($value = $row['civicrm_address_country_id']) {
610 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
614 if (array_key_exists('civicrm_address_state_province_id', $row)) {
615 if ($value = $row['civicrm_address_state_province_id']) {
616 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
620 if (array_key_exists('civicrm_activity_last_completed_last_completed_activity_subject', $row) &&
621 empty($row['civicrm_activity_last_completed_last_completed_activity_subject'])
623 $rows[$rowNum]['civicrm_activity_last_completed_last_completed_activity_subject'] = ts('(no subject)');
626 if (array_key_exists('civicrm_contact_client_sort_name', $row) &&
627 array_key_exists('civicrm_contact_id', $row)
629 $url = CRM_Utils_System
::url("civicrm/contact/view",
630 'reset=1&cid=' . $row['civicrm_contact_id'],
633 $rows[$rowNum]['civicrm_contact_client_sort_name_link'] = $url;
634 $rows[$rowNum]['civicrm_contact_client_sort_name_hover'] = ts("View Contact Summary for this Contact");
637 if (array_key_exists('civicrm_activity_last_completed_last_completed_activity_type', $row)) {
638 if ($value = $row['civicrm_activity_last_completed_last_completed_activity_type']) {
639 $rows[$rowNum]['civicrm_activity_last_completed_last_completed_activity_type'] = $activityTypes[$value];
644 if (array_key_exists('case_activity_all_dates', $row)) {
645 if ($value = $row['case_activity_all_dates']) {
646 $activityDates = explode(',', $value);
647 foreach ($activityDates as $num => $activityDate) {
648 $activityDates[$num] = CRM_Utils_Date
::customFormat($activityDate);
650 $rows[$rowNum]['case_activity_all_dates'] = implode('; ', $activityDates);
655 if (array_key_exists('civicrm_case_is_deleted', $row)) {
656 $value = $row['civicrm_case_is_deleted'];
657 $rows[$rowNum]['civicrm_case_is_deleted'] = $this->deleted_labels
[$value];