From 59b19369b75bf68a310ae16130da602a7896cb42 Mon Sep 17 00:00:00 2001 From: Jaap Jansma Date: Wed, 18 Apr 2018 11:00:36 +0200 Subject: [PATCH] Work on issue 66 (https://lab.civicrm.org/dev/core/issues/66) Work on issue 66 (https://lab.civicrm.org/dev/core/issues/66) Work on issue 66 (https://lab.civicrm.org/dev/core/issues/66) Work on issue 66 (https://lab.civicrm.org/dev/core/issues/66) Work on issue 66 (https://lab.civicrm.org/dev/core/issues/66) Work on issue 66 (https://lab.civicrm.org/dev/core/issues/66) Work on issue 66 (https://lab.civicrm.org/dev/core/issues/66) Work on issue 66 (https://lab.civicrm.org/dev/core/issues/66) --- CRM/Report/Form.php | 356 +++++++++++++++++++++++++--- CRM/Report/Form/Contact/Summary.php | 45 +--- 2 files changed, 332 insertions(+), 69 deletions(-) diff --git a/CRM/Report/Form.php b/CRM/Report/Form.php index 9df5605cf4..faf2622e0c 100644 --- a/CRM/Report/Form.php +++ b/CRM/Report/Form.php @@ -837,6 +837,9 @@ class CRM_Report_Form extends CRM_Core_Form { if (!isset($this->_columns[$tableName]['metadata'][$fieldName])) { $this->_columns[$tableName]['metadata'][$fieldName] = $this->_columns[$tableName][$fieldGrp][$fieldName]; } + else { + $this->_columns[$tableName]['metadata'][$fieldName] = array_merge($this->_columns[$tableName][$fieldGrp][$fieldName], $this->_columns[$tableName]['metadata'][$fieldName]); + } } } } @@ -2273,6 +2276,42 @@ WHERE cg.extends IN ('" . implode("','", $this->_customGroupExtends) . "') AND // process grand-total row $this->grandTotal($rows); + // Find alter display functions. + $firstRow = reset($rows); + if ($firstRow) { + $selectedFields = array_keys($firstRow); + $alterFunctions = $alterMap = $alterSpecs = array(); + foreach ($this->_columns as $tableName => $table) { + if (array_key_exists('metadata', $table)) { + foreach ($table['metadata'] as $field => $specs) { + if (in_array($tableName . '_' . $field, $selectedFields)) { + if (array_key_exists('alter_display', $specs)) { + $alterFunctions[$tableName . '_' . $field] = $specs['alter_display']; + $alterMap[$tableName . '_' . $field] = $field; + $alterSpecs[$tableName . '_' . $field] = NULL; + } + // Add any alters that can be intuited from the field specs. + // So far only boolean but a lot more could be. + if (empty($alterSpecs[$tableName . '_' . $field]) && isset($specs['type']) && $specs['type'] == CRM_Utils_Type::T_BOOLEAN) { + $alterFunctions[$tableName . '_' . $field] = 'alterBoolean'; + $alterMap[$tableName . '_' . $field] = $field; + $alterSpecs[$tableName . '_' . $field] = NULL; + } + } + } + } + } + + // Run the alter display functions + foreach ($rows as $index => & $row) { + foreach ($row as $selectedField => $value) { + if (array_key_exists($selectedField, $alterFunctions)) { + $rows[$index][$selectedField] = $this->{$alterFunctions[$selectedField]}($value, $row, $selectedField, $alterMap[$selectedField], $alterSpecs[$selectedField]); + } + } + } + } + // use this method for formatting rows for display purpose. $this->alterDisplay($rows); CRM_Utils_Hook::alterReportVar('rows', $rows, $this); @@ -2281,6 +2320,120 @@ WHERE cg.extends IN ('" . implode("','", $this->_customGroupExtends) . "') AND $this->alterCustomDataDisplay($rows); } + /** + * @param $value + * @param $row + * @param $selectedfield + * @param $criteriaFieldName + * + * @return array + */ + protected function alterStateProvinceID($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 state.", array( + 1 => $value, + )); + + $states = CRM_Core_PseudoConstant::stateProvince($value, FALSE); + if (!is_array($states)) { + return $states; + } + } + + /** + * @param $value + * @param $row + * @param $selectedField + * @param $criteriaFieldName + * + * @return array + */ + protected function alterCountryID($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 country.", array( + 1 => $value, + )); + $countries = CRM_Core_PseudoConstant::country($value, FALSE); + if (!is_array($countries)) { + return $countries; + } + } + + /** + * @param $value + * @param $row + * @param $selectedfield + * @param $criteriaFieldName + * + * @return array + */ + 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.", array( + 1 => $value, + )); + $counties = CRM_Core_PseudoConstant::county($value, FALSE); + if (!is_array($counties)) { + return $counties; + } + } + + /** + * @param $value + * @param $row + * @param $selectedfield + * @param $criteriaFieldName + * + * @return mixed + */ + protected function alterLocationTypeID($value, &$row, $selectedfield, $criteriaFieldName) { + $values = $this->getLocationTypeOptions(); + return CRM_Utils_Array::value($value, $values); + } + + /** + * @param $value + * @param $row + * @param $fieldname + * + * @return mixed + */ + protected function alterContactID($value, &$row, $fieldname) { + $nameField = substr($fieldname, 0, -2) . 'name'; + static $first = TRUE; + static $viewContactList = FALSE; + if ($first) { + $viewContactList = CRM_Core_Permission::check('access CiviCRM'); + $first = FALSE; + } + if (!$viewContactList) { + return $value; + } + if (array_key_exists($nameField, $row)) { + $row[$nameField . '_link'] = CRM_Utils_System::url("civicrm/contact/view", 'reset=1&cid=' . $value, $this->_absoluteUrl); + } + else { + $row[$fieldname . '_link'] = CRM_Utils_System::url("civicrm/contact/view", 'reset=1&cid=' . $value, $this->_absoluteUrl); + } + return $value; + } + + /** + * @param $value + * + * @return mixed + */ + protected function alterBoolean($value) { + $options = array(0 => ts('No'), 1 => ts('Yes')); + if (isset($options[$value])) { + return $options[$value]; + } + return $value; + } + /** * Build chart. * @@ -4530,33 +4683,6 @@ LEFT JOIN civicrm_contact {$field['alias']} ON {$field['alias']}.id = {$this->_a return $fields; } - /** - * Get address columns to add to array. - * - * @param array $options - * - prefix Prefix to add to table (in case of more than one instance of the table) - * - prefix_label Label to give columns from this address table instance - * - * @return array - * address columns definition - */ - public function getAddressColumns($options = array()) { - $options += array( - 'prefix' => '', - 'prefix_label' => '', - 'group_by' => TRUE, - 'order_by' => TRUE, - 'filters' => TRUE, - 'defaults' => array(), - ); - return $this->addAddressFields( - $options['group_by'], - $options['order_by'], - $options['filters'], - $options['defaults'] - ); - } - /** * Get a standard set of contact fields. * @@ -5266,6 +5392,182 @@ LEFT JOIN civicrm_contact {$field['alias']} ON {$field['alias']}.id = {$this->_a return $this->buildColumns($spec, $options['prefix'] . 'civicrm_contact', 'CRM_Contact_DAO_Contact', $tableAlias, $this->getDefaultsFromOptions($options), $options); } + /** + * Get address columns to add to array. + * + * @param array $options + * - prefix Prefix to add to table (in case of more than one instance of the table) + * - prefix_label Label to give columns from this address table instance + * - group_bys enable these fields for group by - default false + * - order_bys enable these fields for order by + * - filters enable these fields for filtering + * + * @return array address columns definition + */ + protected function getAddressColumns($options = array()) { + $defaultOptions = array( + 'prefix' => '', + 'prefix_label' => '', + 'fields' => TRUE, + 'group_bys' => FALSE, + 'order_bys' => TRUE, + 'filters' => TRUE, + 'join_filters' => FALSE, + 'fields_defaults' => array(), + 'filters_defaults' => array(), + 'group_bys_defaults' => array(), + 'order_bys_defaults' => array(), + ); + + $options = array_merge($defaultOptions, $options); + $defaults = $this->getDefaultsFromOptions($options); + $tableAlias = $options['prefix'] . 'address'; + + $spec = array( + $options['prefix'] . 'name' => array( + 'title' => ts($options['prefix_label'] . 'Address Name'), + 'name' => 'name', + 'is_fields' => TRUE, + ), + $options['prefix'] . 'street_number' => array( + 'name' => 'street_number', + 'title' => ts($options['prefix_label'] . 'Street Number'), + 'type' => 1, + 'is_fields' => TRUE, + ), + $options['prefix'] . 'street_name' => array( + 'name' => 'street_name', + 'title' => ts($options['prefix_label'] . 'Street Name'), + 'type' => 1, + 'is_fields' => TRUE, + 'is_filters' => TRUE, + 'operator' => 'like', + 'is_order_bys' => TRUE, + ), + $options['prefix'] . 'street_address' => array( + 'title' => ts($options['prefix_label'] . 'Street Address'), + 'name' => 'street_address', + 'is_fields' => TRUE, + 'is_filters' => TRUE, + 'is_group_bys' => TRUE, + ), + $options['prefix'] . 'supplemental_address_1' => array( + 'title' => ts($options['prefix_label'] . 'Supplementary Address Field 1'), + 'name' => 'supplemental_address_1', + 'is_fields' => TRUE, + ), + $options['prefix'] . 'supplemental_address_2' => array( + 'title' => ts($options['prefix_label'] . 'Supplementary Address Field 2'), + 'name' => 'supplemental_address_2', + 'is_fields' => TRUE, + ), + $options['prefix'] . 'supplemental_address_3' => array( + 'title' => ts($options['prefix_label'] . 'Supplementary Address Field 3'), + 'name' => 'supplemental_address_3', + 'is_fields' => TRUE, + ), + $options['prefix'] . 'street_number' => array( + 'name' => 'street_number', + 'title' => ts($options['prefix_label'] . 'Street Number'), + 'type' => 1, + 'is_order_bys' => TRUE, + 'is_filters' => TRUE, + 'is_fields' => TRUE, + ), + $options['prefix'] . 'street_name' => array( + 'name' => 'street_name', + 'title' => ts($options['prefix_label'] . 'Street Name'), + 'type' => 1, + 'is_fields' => TRUE, + ), + $options['prefix'] . 'street_unit' => array( + 'name' => 'street_unit', + 'title' => ts($options['prefix_label'] . 'Street Unit'), + 'type' => 1, + 'is_fields' => TRUE, + ), + $options['prefix'] . 'city' => array( + 'title' => ts($options['prefix_label'] . 'City'), + 'name' => 'city', + 'operator' => 'like', + 'is_fields' => TRUE, + 'is_filters' => TRUE, + 'is_group_bys' => TRUE, + 'is_order_bys' => TRUE, + ), + $options['prefix'] . 'postal_code' => array( + 'title' => ts($options['prefix_label'] . 'Postal Code'), + 'name' => 'postal_code', + 'type' => 1, + 'is_fields' => TRUE, + 'is_filters' => TRUE, + 'is_group_bys' => TRUE, + 'is_order_bys' => TRUE, + ), + $options['prefix'] . 'postal_code_suffix' => array( + 'title' => ts($options['prefix_label'] . 'Postal Code Suffix'), + 'name' => 'postal_code', + 'type' => 1, + 'is_fields' => TRUE, + 'is_filters' => TRUE, + 'is_group_bys' => TRUE, + 'is_order_bys' => TRUE, + ), + $options['prefix'] . 'county_id' => array( + 'title' => ts($options['prefix_label'] . 'County'), + 'alter_display' => 'alterCountyID', + 'name' => 'county_id', + 'type' => CRM_Utils_Type::T_INT, + 'operatorType' => CRM_Report_Form::OP_MULTISELECT, + 'options' => CRM_Core_PseudoConstant::county(), + 'is_fields' => TRUE, + 'is_filters' => TRUE, + 'is_group_bys' => TRUE, + ), + $options['prefix'] . 'state_province_id' => array( + 'title' => ts($options['prefix_label'] . 'State/Province'), + 'alter_display' => 'alterStateProvinceID', + 'name' => 'state_province_id', + 'type' => CRM_Utils_Type::T_INT, + 'operatorType' => CRM_Report_Form::OP_MULTISELECT, + 'options' => CRM_Core_PseudoConstant::stateProvince(), + 'is_fields' => TRUE, + 'is_filters' => TRUE, + 'is_group_bys' => TRUE, + ), + $options['prefix'] . 'country_id' => array( + 'title' => ts($options['prefix_label'] . 'Country'), + 'alter_display' => 'alterCountryID', + 'name' => 'country_id', + 'is_fields' => TRUE, + 'is_filters' => TRUE, + 'is_group_bys' => TRUE, + 'type' => CRM_Utils_Type::T_INT, + 'operatorType' => CRM_Report_Form::OP_MULTISELECT, + 'options' => CRM_Core_PseudoConstant::country(), + ), + $options['prefix'] . 'location_type_id' => array( + 'name' => 'is_primary', + 'title' => ts($options['prefix_label'] . 'Location Type'), + 'type' => CRM_Utils_Type::T_INT, + 'is_fields' => TRUE, + 'alter_display' => 'alterLocationTypeID', + ), + $options['prefix'] . 'id' => array( + 'title' => ts($options['prefix_label'] . 'ID'), + 'name' => 'id', + 'is_fields' => TRUE, + ), + $options['prefix'] . 'is_primary' => array( + 'name' => 'is_primary', + 'title' => ts($options['prefix_label'] . 'Primary Address?'), + 'type' => CRM_Utils_Type::T_BOOLEAN, + 'is_fields' => TRUE, + ), + ); + return $this->buildColumns($spec, $options['prefix'] . 'civicrm_address', 'CRM_Core_DAO_Address', $tableAlias, $defaults, $options); + } + /** * Build the columns. * diff --git a/CRM/Report/Form/Contact/Summary.php b/CRM/Report/Form/Contact/Summary.php index 706d992981..28a13c8db4 100644 --- a/CRM/Report/Form/Contact/Summary.php +++ b/CRM/Report/Form/Contact/Summary.php @@ -131,7 +131,7 @@ class CRM_Report_Form_Contact_Summary extends CRM_Report_Form { ), 'grouping' => 'contact-fields', ), - ) + $this->getAddressColumns(array('group_by' => FALSE)); + ) + $this->getAddressColumns(array('group_bys' => FALSE)); $this->_groupFilter = TRUE; $this->_tagFilter = TRUE; @@ -142,29 +142,6 @@ class CRM_Report_Form_Contact_Summary extends CRM_Report_Form { parent::preProcess(); } - public function select() { - $select = array(); - $this->_columnHeaders = array(); - foreach ($this->_columns as $tableName => $table) { - if (array_key_exists('fields', $table)) { - foreach ($table['fields'] as $fieldName => $field) { - if (!empty($field['required']) || - !empty($this->_params['fields'][$fieldName]) - ) { - - $alias = "{$tableName}_{$fieldName}"; - $select[] = "{$field['dbAlias']} as {$alias}"; - $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field); - $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title']; - $this->_selectAliases[] = $alias; - } - } - } - } - - $this->_select = "SELECT " . implode(', ', $select) . " "; - } - /** * @param $fields * @param $files @@ -179,10 +156,8 @@ class CRM_Report_Form_Contact_Summary extends CRM_Report_Form { public function from() { $this->_from = " - FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom} - LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} - ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND - {$this->_aliases['civicrm_address']}.is_primary = 1 ) "; + FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom} "; + $this->joinAddressFromContact(); $this->joinPhoneFromContact(); $this->joinEmailFromContact(); $this->joinCountryFromAddress(); @@ -232,20 +207,6 @@ class CRM_Report_Form_Contact_Summary extends CRM_Report_Form { $entryFound = TRUE; } - if (array_key_exists('civicrm_address_state_province_id', $row)) { - if ($value = $row['civicrm_address_state_province_id']) { - $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvince($value, FALSE); - } - $entryFound = TRUE; - } - - if (array_key_exists('civicrm_address_country_id', $row)) { - if ($value = $row['civicrm_address_country_id']) { - $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant::country($value, FALSE); - } - $entryFound = TRUE; - } - // Handle ID to label conversion for contact fields $entryFound = $this->alterDisplayContactFields($row, $rows, $rowNum, 'contact/summary', 'View Contact Summary') ? TRUE : $entryFound; -- 2.25.1