From: eileen Date: Fri, 22 Feb 2019 04:22:51 +0000 (+1300) Subject: Fix case detail report breaking when sorted by case type. X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=0f880d50ae48dd0ac0602d0804e5c977885ec737;p=civicrm-core.git Fix case detail report breaking when sorted by case type. I removed the hacks to cope with poor metadata declaration & fixed the declaration - this should stop breaking now. Test added --- diff --git a/CRM/Report/Form/Case/Detail.php b/CRM/Report/Form/Case/Detail.php index 2493d21b1d..0a38995fcb 100644 --- a/CRM/Report/Form/Case/Detail.php +++ b/CRM/Report/Form/Case/Detail.php @@ -142,11 +142,16 @@ class CRM_Report_Form_Case_Detail extends CRM_Report_Form { 'status_id' => [ 'title' => ts('Status'), ], - 'case_type_name' => [ - 'title' => 'Case Type', - ], ], ], + 'civicrm_case_type' => [ + 'dao' => 'CRM_Case_DAO_Case', + 'order_bys' => [ + 'case_type_title' => [ + 'title' => 'Case Type', + 'name' => 'title', + ]] + ], 'civicrm_contact' => [ 'dao' => 'CRM_Contact_DAO_Contact', 'fields' => [ @@ -339,6 +344,8 @@ class CRM_Report_Form_Case_Detail extends CRM_Report_Form { } public function select() { + // @todo - get rid of this function & use parent. Use selectWhere to setthe clause for the + // few fields that need custom handling. $select = []; $this->_columnHeaders = []; foreach ($this->_columns as $tableName => $table) { @@ -375,15 +382,6 @@ class CRM_Report_Form_Case_Detail extends CRM_Report_Form { } } - if ($orderBys = $this->_params['order_bys']) { - foreach ($orderBys as $orderBy) { - if ($orderBy['column'] == 'case_type_name') { - $select[] = "civireport_case_types.title as case_type_name"; - $this->_caseTypeNameOrderBy = TRUE; - } - } - } - $this->_selectClauses = $select; $this->_select = 'SELECT ' . implode(', ', $select) . ' '; @@ -427,11 +425,10 @@ class CRM_Report_Form_Case_Detail extends CRM_Report_Form { $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 ) )"; } - //include case type name - if ($this->_caseTypeNameOrderBy) { + if ($this->isTableSelected('civicrm_case_type')) { $this->_from .= " - LEFT JOIN civicrm_case_type civireport_case_types - ON {$case}.case_type_id = civireport_case_types.id + LEFT JOIN civicrm_case_type {$this->_aliases['civicrm_case_type']} + ON {$this->_aliases['civicrm_case']}.case_type_id = {$this->_aliases['civicrm_case_type']}.id "; } } @@ -521,17 +518,6 @@ class CRM_Report_Form_Case_Detail extends CRM_Report_Form { return $statistics; } - public function orderBy() { - parent::orderBy(); - - if ($this->_caseTypeNameOrderBy) { - $this->_orderBy = str_replace('case_civireport.case_type_name', 'civireport_case_types.title', $this->_orderBy); - if (isset($this->_sections['civicrm_case_case_type_name'])) { - $this->_sections['civicrm_case_case_type_name']['dbAlias'] = 'civireport_case_types.title'; - } - } - } - public function caseDetailSpecialColumnProcess() { if (!$this->_includeCaseDetailExtra) { return; diff --git a/tests/phpunit/api/v3/ReportTemplateTest.php b/tests/phpunit/api/v3/ReportTemplateTest.php index 00b257d998..2f02b1409a 100644 --- a/tests/phpunit/api/v3/ReportTemplateTest.php +++ b/tests/phpunit/api/v3/ReportTemplateTest.php @@ -552,6 +552,23 @@ class api_v3_ReportTemplateTest extends CiviUnitTestCase { $this->assertEquals(2, $rows['values'][0]['civicrm_contribution_total_amount_count']); } + /** + * Test no fatal on order by per https://lab.civicrm.org/dev/core/issues/739 + */ + public function testCaseDetailsCaseTypeHeader() { + $this->callAPISuccess('report_template', 'getrows', [ + 'report_id' => 'case/detail', + 'fields' => ['subject' => 1, 'client_sort_name' => 1], + 'order_bys' => [ + 1 => [ + 'column' => 'case_type_title', + 'order' => 'ASC', + 'section' => '1', + ], + ], + ]); + } + /** * Test the group filter works on the contribution summary. */