Skip to content

Commit

Permalink
More code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Nov 1, 2019
1 parent 76d9be7 commit 6a9770b
Showing 1 changed file with 41 additions and 31 deletions.
72 changes: 41 additions & 31 deletions CRM/Extendedreport/Form/Report/ExtendedReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ public function __construct() {
*
* @return array
*/
public function getMetadata() {
public function getMetadata():array {
if (empty($this->metaData)) {
$definitionTypes = [
'fields',
Expand Down Expand Up @@ -403,6 +403,8 @@ public function getInstanceID() {

/**
* Adds group filters to _columns (called from _Construct).
*
* @throws \CiviCRM_API3_Exception
*/
public function buildGroupFilter() {
$this->_columns += $this->buildColumns(
Expand Down Expand Up @@ -826,6 +828,8 @@ function select() {
* Generally a rowHeader and a columnHeader will be defined.
*
* Column Header is optional - in which case a single total column will show.
*
* @throws \Exception
*/
function aggregateSelect() {
if (empty($this->_customGroupAggregates)) {
Expand All @@ -846,7 +850,7 @@ function aggregateSelect() {
if (array_key_exists($this->_params['aggregate_column_headers'], $this->getMetadataByType('aggregate_columns'))) {
$spec = $this->getMetadataByType('aggregate_columns')[$this->_params['aggregate_column_headers']];
}
if ($this->_params['aggregate_column_headers'] == 'contribution_total_amount_year' || $this->_params['aggregate_column_headers'] == 'contribution_total_amount_month') {
if ($this->_params['aggregate_column_headers'] === 'contribution_total_amount_year' || $this->_params['aggregate_column_headers'] === 'contribution_total_amount_month') {
$columnType = explode('_', $this->_params['aggregate_column_headers']);
$columnType = end($columnType);
$spec = [
Expand Down Expand Up @@ -897,7 +901,7 @@ function addColumnAggregateSelect($fieldName, $dbAlias, $spec) {
}
$options = $this->getCustomFieldOptions($spec);

if (!empty($this->_params[$fieldName . '_value']) && CRM_Utils_Array::value($fieldName . '_op', $this->_params) == 'in') {
if (!empty($this->_params[$fieldName . '_value']) && CRM_Utils_Array::value($fieldName . '_op', $this->_params) === 'in') {
$options['values'] = array_intersect_key($options, array_flip($this->_params[$fieldName . '_value']));
}

Expand All @@ -908,7 +912,7 @@ function addColumnAggregateSelect($fieldName, $dbAlias, $spec) {

if ($this->getFilterFieldValue($spec)) {
// for now we will literally just handle IN
if ($filterSpec['field']['op'] == 'in') {
if ($filterSpec['field']['op'] === 'in') {
$options = array_intersect_key($options, array_flip($filterSpec['field']['value']));
$this->_aggregatesIncludeNULL = FALSE;
}
Expand All @@ -925,7 +929,7 @@ function addColumnAggregateSelect($fieldName, $dbAlias, $spec) {
], '_', "{$fieldName}_" . strtolower(str_replace(' ', '', $optionValue)));

// htmlType is set for custom data and tells us the field will be stored using hex(01) separators.
if (!empty($spec['htmlType']) && ($spec['htmlType'] == 'CheckBox' || strpos($spec['htmlType'], 'Multi') !== FALSE)) {
if (!empty($spec['htmlType']) && ($spec['htmlType'] === 'CheckBox' || strpos($spec['htmlType'], 'Multi') !== FALSE)) {
$this->_select .= " , SUM( CASE WHEN {$dbAlias} LIKE '%" . CRM_Core_DAO::VALUE_SEPARATOR . $optionValue . CRM_Core_DAO::VALUE_SEPARATOR . "%' THEN 1 ELSE 0 END ) AS $fieldAlias ";
}
else {
Expand Down Expand Up @@ -986,7 +990,7 @@ function getFilterFieldValue(&$spec) {
/**
* @param $fieldName
*/
function addAggregateTotal($fieldName) {
protected function addAggregateTotal($fieldName) {
$fieldAlias = "{$fieldName}_total";
$this->_columnHeaders[$fieldAlias] = [
'title' => ts('Total'),
Expand All @@ -999,7 +1003,7 @@ function addAggregateTotal($fieldName) {
/**
* From clause build where baseTable & fromClauses are defined.
*/
function from() {
public function from() {
if (!empty($this->_baseTable)) {
$tableAlias = (empty($this->_aliases[$this->_baseTable]) ? '' : $this->_aliases[$this->_baseTable]);
$this->setFromBase($this->_baseTable, 'id', $tableAlias);
Expand Down Expand Up @@ -1028,7 +1032,7 @@ function from() {
}

}
if (strstr($this->_from, 'civicrm_contact')) {
if (strpos($this->_from, 'civicrm_contact') !== FALSE) {
$this->_from .= $this->_aclFrom;
}
$this->_from .= $this->_extraFrom;
Expand Down Expand Up @@ -2541,13 +2545,13 @@ function extractCustomFields(&$customFields, $context = 'select') {
$fieldAlias = $selectedField['alias'];
$tableAlias = $tableName;
// these should be in separate functions
if ($context == 'select' && (!$this->_preConstrain || $this->_preConstrained)) {
if ($context === 'select' && (!$this->_preConstrain || $this->_preConstrained)) {
$this->_select .= ", {$tableAlias}.{$metadata[$fieldName]['name']} as $fieldAlias ";
}
if ($context == 'row_header') {
if ($context === 'row_header') {
$this->addRowHeader($tableAlias, $selectedField, $fieldAlias);
}
if ($context == 'column_header') {
if ($context === 'column_header') {
$this->addColumnAggregateSelect($metadata[$fieldName]['name'], $selectedField['dbAlias'], $metadata[$fieldName]);
}
if (!isset($selectedField['type'])) {
Expand Down Expand Up @@ -2640,8 +2644,8 @@ function alterDisplay(&$rows) {
$alterFunctions = $alterMap = $alterSpecs = [];

foreach ($this->getSelectedAggregateRows() as $pivotRowField => $pivotRowFieldSpec) {
$pivotRowIsCustomField = substr($pivotRowField, 0, 7) == 'custom_';
if ($pivotRowIsCustomField && $pivotRowFieldSpec['html_type'] != 'Text') {
$pivotRowIsCustomField = strpos($pivotRowField, 'custom_') === 0;
if ($pivotRowIsCustomField && $pivotRowFieldSpec['html_type'] !== 'Text') {
$alias = $pivotRowFieldSpec['alias'];
$alterFunctions[$alias] = 'alterFromOptions';
$alterMap[$alias] = $pivotRowField;
Expand Down Expand Up @@ -3764,6 +3768,7 @@ protected function getMembershipColumns($options) {
* @param array $options
*
* @return array
* @throws \CiviCRM_API3_Exception
*/
function getMembershipLogColumns($options = []) {
$columns = [
Expand Down Expand Up @@ -5186,6 +5191,7 @@ function getAddressColumns($options = []) {
* @param array $options
*
* @return array billing address columns definition
* @throws \CiviCRM_API3_Exception
*/
function getBillingAddressColumns($options = []) {
$options['prefix'] = 'billing';
Expand Down Expand Up @@ -5224,8 +5230,9 @@ function alterBillingName($value, &$row, $selectedField) {
* @param array $options
*
* @return array
* @throws \CiviCRM_API3_Exception
*/
function getTagColumns($options = []) {
protected function getTagColumns($options = []) {
$defaultOptions = [
'prefix' => '',
'prefix_label' => '',
Expand Down Expand Up @@ -5259,7 +5266,7 @@ function getTagColumns($options = []) {
*
* @return array
*/
function getAvailableJoins() {
protected function getAvailableJoins() {
return [
'batch_from_financialTrxn' => [
'leftTable' => 'civicrm_financial_trxn',
Expand Down Expand Up @@ -6516,7 +6523,7 @@ function alterCountryID($value, &$row, $selectedField, $criteriaFieldName) {
*
* @return array
*/
function alterCountyID($value, &$row, $selectedfield, $criteriaFieldName) {
protected function alterCountyID($value, &$row, $selectedfield, $criteriaFieldName) {
$url = CRM_Utils_System::url(CRM_Utils_System::currentPath(), "reset=1&force=1&{$criteriaFieldName}_op=in&{$criteriaFieldName}_value={$value}", $this->_absoluteUrl);
$row[$selectedfield . '_link'] = $url;
$row[$selectedfield . '_hover'] = ts("%1 for this county.", [
Expand Down Expand Up @@ -7098,16 +7105,17 @@ protected function generateFilterClause($field, $fieldName, $prefix = '') {
* array('custom_2' => array('civicrm_contact');
* ie fieldname => table alias
*/
protected function getAggregateField($type) {
protected function getAggregateField($type): array {
if (empty($this->_params['aggregate_' . $type . '_headers'])) {
return [];
}
$columnHeader = $this->_params['aggregate_' . $type . '_headers'];

$fieldSpec = [];
if (array_key_exists($columnHeader, $this->getMetadataByType('aggregate_columns'))) {
$fieldSpec = $this->getMetadataByType('aggregate_columns')[$columnHeader];
}
if ($columnHeader == 'contribution_total_amount_year' || $columnHeader == 'contribution_total_amount_month') {
if ($columnHeader === 'contribution_total_amount_year' || $columnHeader === 'contribution_total_amount_month') {
$fieldSpec = [
'table_name' => 'civicrm_contribution',
'name' => 'total_amount',
Expand All @@ -7129,7 +7137,7 @@ protected function getAggregateField($type) {
* array('custom_2' => array('civicrm_contact');
* ie fieldname => table alias
*/
protected function getAggregateFieldSpec($type) {
protected function getAggregateFieldSpec($type): array {
if (empty($this->_params['aggregate_' . $type . '_headers'])) {
return [];
}
Expand All @@ -7148,17 +7156,19 @@ protected function getAggregateFieldSpec($type) {
* array('custom_2' => array('civicrm_contact');
* ie fieldname => table alias
*/
protected function getFieldBreakdownForAggregates($type) {
protected function getFieldBreakdownForAggregates($type):array {
if (empty($this->_params['aggregate_' . $type . '_headers'])) {
return [];
}
$columnHeader = $this->_params['aggregate_' . $type . '_headers'];
$fieldArr = explode(":", $columnHeader);
$fieldArr = explode(':', $columnHeader);
return [$fieldArr[1] => [$fieldArr[0]]];
}

/**
* Add the fields to select the aggregate fields to the report.
*
* @throws \CiviCRM_API3_Exception
*/
protected function addAggregateSelectorsToForm() {
if (!$this->isPivot) {
Expand Down Expand Up @@ -7218,11 +7228,12 @@ protected function addAggregateSelectorsToForm() {
*
* @return string
*/
protected function getPivotRowFieldName() {
protected function getPivotRowFieldName(): string {
if (!empty($this->_params['aggregate_row_headers'])) {
$aggregateField = explode(':', $this->_params['aggregate_row_headers']);
return $aggregateField[1];
}
return '';
}

/**
Expand Down Expand Up @@ -7266,11 +7277,11 @@ protected function getGroupByCriteria($tableCol, $row) {
}

/**
* @param $options
* @param array $options
*
* @return array
*/
protected function getDefaultsFromOptions($options) {
protected function getDefaultsFromOptions($options):array {
$defaults = [
'fields_defaults' => $options['fields_defaults'],
'filters_defaults' => $options['filters_defaults'],
Expand Down Expand Up @@ -8212,7 +8223,7 @@ protected function getQillForField($field, $fieldName, $prefix = '') {
}
$value = "{$pair[$op]} " . implode(', ', $values);
}
elseif ($op == 'nll' || $op == 'nnll') {
elseif ($op === 'nll' || $op === 'nnll') {
$value = $pair[$op];
}
elseif (is_array($val) && (!empty($val))) {
Expand All @@ -8222,7 +8233,7 @@ protected function getQillForField($field, $fieldName, $prefix = '') {
$val[$key] = $options[$valIds];
}
}
$pair[$op] = (count($val) == 1) ? (($op == 'notin' || $op ==
$pair[$op] = (count($val) == 1) ? (($op === 'notin' || $op ===
'mnot') ? ts('Is Not') : ts('Is')) : CRM_Utils_Array::value($op, $pair);
$val = implode(', ', $val);
$value = "{$pair[$op]} " . $val;
Expand Down Expand Up @@ -8259,7 +8270,7 @@ protected function getQillForField($field, $fieldName, $prefix = '') {
*
* @return bool
*/
protected function isValidTitle($string) {
protected function isValidTitle($string):bool {
return CRM_Utils_Rule::alphanumeric(str_replace(' ', '', $string));
}

Expand All @@ -8270,7 +8281,7 @@ protected function isValidTitle($string) {
*
* @return array
*/
protected function getConfiguredFieldsFlatArray() {
protected function getConfiguredFieldsFlatArray():array {
$sortingArray = [];
foreach ($this->getExtendedFieldsSelection() as $configuredExtendedField) {
$sortingArray[$configuredExtendedField['name']] = 1;
Expand Down Expand Up @@ -8388,9 +8399,8 @@ protected function getConfiguredOrderBys($params) {
$orderBys = [];
$quickFormOrderBys = isset($params['order_bys']) ? (array) $params['order_bys'] : [];
foreach ($quickFormOrderBys as $index => $quickFormOrderBy) {
if ($quickFormOrderBy == ['column' => "-"]) {
unset($params['order_bys'][$index]);
unset($quickFormOrderBys['index']);
if ($quickFormOrderBy === ['column' => '-']) {
unset($params['order_bys'][$index], $quickFormOrderBys['index']);
continue;
}
$orderBys[$quickFormOrderBy['column']] = $quickFormOrderBy;
Expand Down

0 comments on commit 6a9770b

Please sign in to comment.