From f197e6f61f0b9482405bd0a0a3331c126e2d206f Mon Sep 17 00:00:00 2001 From: Thomas Casteleyn Date: Fri, 1 Sep 2023 13:22:28 +0200 Subject: [PATCH] Add support to have alternative import column names --- core/collector.class.inc.php | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/core/collector.class.inc.php b/core/collector.class.inc.php index 5f6ae88..84e00e8 100644 --- a/core/collector.class.inc.php +++ b/core/collector.class.inc.php @@ -108,7 +108,8 @@ public function Init(): void throw new Exception('Cannot create Collector (invalid JSON definition)'); } foreach ($aSourceDefinition['attribute_list'] as $aAttr) { - $this->aFields[$aAttr['attcode']] = ['class' => $aAttr['finalclass'], 'update' => ($aAttr['update'] != 0), 'reconcile' => ($aAttr['reconcile'] != 0)]; + $aColumns = isset($aAttr['import_columns']) ? explode(',', $aAttr['import_columns']) : [$aAttr['attcode']]; + $this->aFields[$aAttr['attcode']] = ['class' => $aAttr['finalclass'], 'update' => ($aAttr['update'] != 0), 'reconcile' => ($aAttr['reconcile'] != 0), 'columns' => $aColumns]; } $this->ReadCollectorConfig(); @@ -576,7 +577,7 @@ protected function AddHeader($aHeaders) { $this->aCSVHeaders = array(); foreach ($aHeaders as $sHeader) { - if (($sHeader != 'primary_key') && !array_key_exists($sHeader, $this->aFields)) { + if (($sHeader != 'primary_key') && !$this->HeaderIsAllowed($sHeader)) { if (!$this->AttributeIsOptional($sHeader)) { Utils::Log(LOG_WARNING, "Invalid column '$sHeader', will be ignored."); } @@ -585,9 +586,25 @@ protected function AddHeader($aHeaders) $this->aCSVHeaders[] = $sHeader; } } - //fwrite($this->aCSVFile[$this->iFileIndex], implode($this->sSeparator, $this->aCSVHeaders)."\n"); fputcsv($this->aCSVFile[$this->iFileIndex], $this->aCSVHeaders, $this->sSeparator); } + + /** + * Added to add multi-column field support or situations + * where column name is different than attribute code. + * + * @param string $sHeader + * @return bool + */ + protected function HeaderIsAllowed($sHeader) + { + foreach ($this->aFields as $aField) { + if (in_array($sHeader, $aField['columns'])) return true; + } + + // fallback old behaviour + return array_key_exists($sHeader, $this->aFields); + } protected function AddRow($aRow) {