Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Collector): Add support to have alternative import column names #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) {
Comment on lines 579 to 581
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Especially this part is key to allow for more customisation of the data collectors..

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