From f2947aea898f332c115073cdc276536eaa0aca2f Mon Sep 17 00:00:00 2001 From: Deepak Srivastava Date: Mon, 2 Sep 2013 15:09:31 +0530 Subject: [PATCH] CRM-12490 - order-by fatal error fix --- CRM/Report/Form.php | 7 ++- CRM/Report/Form/Contribute/Detail.php | 62 +++++++-------------------- 2 files changed, 21 insertions(+), 48 deletions(-) diff --git a/CRM/Report/Form.php b/CRM/Report/Form.php index b26f468b24..3bf7ac7844 100644 --- a/CRM/Report/Form.php +++ b/CRM/Report/Form.php @@ -241,6 +241,8 @@ class CRM_Report_Form extends CRM_Core_Form { public $_select = NULL; public $_columnHeaders = array(); public $_orderBy = NULL; + public $_orderByFields = array(); + public $_orderByArray = array(); public $_groupBy = NULL; public $_whereClauses = array(); public $_havingClauses = array(); @@ -2104,6 +2106,7 @@ WHERE cg.extends IN ('" . implode("','", $this->_customGroupExtends) . "') AND } $this->assign('sections', $this->_sections); } + /* * In some cases other functions want to know which fields are selected for ordering by * Separating this into a separate function allows it to be called separately from constructing @@ -2132,7 +2135,7 @@ WHERE cg.extends IN ('" . implode("','", $this->_customGroupExtends) . "') AND if (!empty($fields) && is_array($fields)) { foreach ($fields as $fieldName => $field) { if ($fieldName == $orderBy['column']) { - $orderByField = $field; + $orderByField = array_merge($field, $orderBy); $orderByField['tplField'] = "{$tableName}_{$fieldName}"; break 2; } @@ -2141,6 +2144,7 @@ WHERE cg.extends IN ('" . implode("','", $this->_customGroupExtends) . "') AND } if (!empty($orderByField)) { + $this->_orderByFields[] = $orderByField; $orderBys[] = "{$orderByField['dbAlias']} {$orderBy['order']}"; // Record any section headers for assignment to the template @@ -2170,6 +2174,7 @@ WHERE cg.extends IN ('" . implode("','", $this->_customGroupExtends) . "') AND } } } + if (is_array($this->_sections)) { return array_diff_key($this->_sections, $selectColumns); } diff --git a/CRM/Report/Form/Contribute/Detail.php b/CRM/Report/Form/Contribute/Detail.php index f8683dbf72..ec1693bf07 100644 --- a/CRM/Report/Form/Contribute/Detail.php +++ b/CRM/Report/Form/Contribute/Detail.php @@ -377,8 +377,6 @@ class CRM_Report_Form_Contribute_Detail extends CRM_Report_Form { } function select() { - $select = array(); - $this->_columnHeaders = array(); foreach ($this->_columns as $tableName => $table) { if (array_key_exists('fields', $table)) { @@ -386,61 +384,31 @@ class CRM_Report_Form_Contribute_Detail extends CRM_Report_Form { if (CRM_Utils_Array::value('required', $field) || CRM_Utils_Array::value($fieldName, $this->_params['fields']) ) { - if ($tableName == 'civicrm_address') { - $this->_addressField = TRUE; - } - if ($tableName == 'civicrm_email') { - $this->_emailField = TRUE; - } - elseif ($tableName == 'civicrm_email_honor') { + if ($tableName == 'civicrm_email_honor') { $this->_emailFieldHonor = TRUE; } - if ($tableName == 'civicrm_contact_honor') { $this->_nameFieldHonor = TRUE; } - - // only include statistics columns if set - if (CRM_Utils_Array::value('statistics', $field)) { - foreach ($field['statistics'] as $stat => $label) { - switch (strtolower($stat)) { - case 'sum': - $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}"; - $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label; - $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type']; - $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}"; - $this->_selectAliases[] = "{$tableName}_{$fieldName}_{$stat}"; - break; - - case 'count': - $select[] = "COUNT({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}"; - $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label; - $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}"; - $this->_selectAliases[] = "{$tableName}_{$fieldName}_{$stat}"; - break; - - case 'avg': - $select[] = "ROUND(AVG({$field['dbAlias']}),2) as {$tableName}_{$fieldName}_{$stat}"; - $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type']; - $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label; - $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}"; - $this->_selectAliases[] = "{$tableName}_{$fieldName}_{$stat}"; - break; - } - } - } - else { - $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}"; - $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field); - $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field); - $this->_selectAliases[] = "{$tableName}_{$fieldName}"; - } } } } } - $this->_select = "SELECT " . implode(', ', $select) . " "; + parent::select(); + } + + function orderBy() { + parent::orderBy(); + + // please note this will just add the order-by columns to select query, and not display in column-headers. + // This is a solution to not throw fatal errors when there is a column in order-by, not present in select/display columns. + foreach ($this->_orderByFields as $orderBy) { + if (!array_key_exists($orderBy['name'], $this->_params['fields']) + && !CRM_Utils_Array::value('section', $orderBy)) { + $this->_select .= ", {$orderBy['dbAlias']} as {$orderBy['tplField']}"; + } + } } function from($softcredit = false) { -- 2.25.1