From 2727203af207b7d9ab631534a4673634e2717959 Mon Sep 17 00:00:00 2001 From: Jaap Jansma <jaap.jansma@civicoop.org> Date: Fri, 22 Nov 2019 17:01:33 +0100 Subject: [PATCH] Fixed annoying bug with adding and editing joins again and again. --- .../DataFlow/MultipleDataFlows/SimpleJoin.php | 40 ++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/Civi/DataProcessor/DataFlow/MultipleDataFlows/SimpleJoin.php b/Civi/DataProcessor/DataFlow/MultipleDataFlows/SimpleJoin.php index 2a247793..58f1a54b 100644 --- a/Civi/DataProcessor/DataFlow/MultipleDataFlows/SimpleJoin.php +++ b/Civi/DataProcessor/DataFlow/MultipleDataFlows/SimpleJoin.php @@ -188,7 +188,7 @@ class SimpleJoin implements JoinInterface, SqlJoinInterface { */ public function processConfiguration($submittedValues, SourceInterface $joinFromSource) { $left_prefix = $joinFromSource->getSourceName(); - $left_field = $submittedValues['left_field']; + $left_field = $this->ocrrectFieldName($submittedValues['left_field'], $joinFromSource); list($right_prefix, $right_field) = explode("::",$submittedValues['right_field'], 2); $configuration = array( @@ -200,6 +200,44 @@ class SimpleJoin implements JoinInterface, SqlJoinInterface { return $configuration; } + /** + * This function corrects the field name. + * Basically when we add a join the name of the source is empty but as soon as we save it + * the name is set. And the fields have the name of the source in their alias. + * + * For example if we join on contribution with the left table campaign + * our join field is called id (the id of the campaign). The alias of this field + * is upon adding _id + * but after the source name is set to for example my_campaign. Then the alias becomes my_campaign_id + * so we should correct this name. + * + * @param String $fieldAlias + * @param \Civi\DataProcessor\Source\SourceInterface $joinFromSource + * @return String + */ + private function ocrrectFieldName($fieldAlias, SourceInterface $joinFromSource) { + try { + $fields = \CRM_Dataprocessor_Utils_DataSourceFields::getAvailableFieldsInDataSource($joinFromSource, '', ''); + if (isset($fields[$fieldAlias])) { + // No need for correction as the field exists. + return $fieldAlias; + } + $sourceWithEmptyName = clone $joinFromSource; + $sourceWithEmptyName->setSourceName(''); + $fieldsWithoutPrefix = \CRM_Dataprocessor_Utils_DataSourceFields::getAvailableFieldsInDataSource($sourceWithEmptyName, '', ''); + $fieldKeys = array_keys($fields); + $fieldsWithoutPrefixKeys = array_keys($fieldsWithoutPrefix); + $key = array_search($fieldAlias, $fieldsWithoutPrefixKeys); + if (isset($fieldKeys[$key])) { + return $fieldKeys[$key]; + } + } catch (\Exception $e) { + // Do nothing. + } + // We could not convert the field alias so return it as it is. + return $fieldAlias; + } + /** * @param array $configuration * -- GitLab