Skip to content

Commit

Permalink
Add support to have alternative import column names
Browse files Browse the repository at this point in the history
  • Loading branch information
Hipska committed Sep 1, 2023
1 parent 2d3a901 commit 9f204ea
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions core/collector.class.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -576,18 +577,33 @@ 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.");
}
} else {

$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)
{
Expand Down

0 comments on commit 9f204ea

Please sign in to comment.