Skip to content

Commit 71c594f

Browse files
authored
N°8421 - CSV Collector: Align headerless CSV file collector behavior … (#57)
1 parent 68ce284 commit 71c594f

File tree

6 files changed

+75
-26
lines changed

6 files changed

+75
-26
lines changed

core/csvcollector.class.inc.php

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -134,20 +134,21 @@ public function Prepare()
134134

135135
return false;
136136
}
137-
138-
array_multisort($aCurrentConfiguredHeaderColumns);
139-
$this->aConfiguredHeaderColumns = array_keys($aCurrentConfiguredHeaderColumns);
140-
141-
if ($this->bHasHeader) {
142-
foreach ($aCurrentConfiguredHeaderColumns as $sSynchroField => $sCsvColumn) {
143-
$this->aMappingCsvColumnNameToFields[$sCsvColumn][] = $sSynchroField;
144-
$this->aMappedFields[$sSynchroField] = '';
145-
}
146-
}
137+
$this->aConfiguredHeaderColumns = [];
138+
if ($this->bHasHeader) {
139+
array_multisort($aCurrentConfiguredHeaderColumns);
140+
$this->aConfiguredHeaderColumns = array_keys($aCurrentConfiguredHeaderColumns);
141+
142+
foreach ($aCurrentConfiguredHeaderColumns as $sSynchroField => $sCsvColumn) {
143+
$this->aMappingCsvColumnNameToFields[$sCsvColumn][] = $sSynchroField;
144+
$this->aMappedFields[$sSynchroField] = '';
145+
}
146+
} else {
147+
$this->aConfiguredHeaderColumns = $aCurrentConfiguredHeaderColumns;
147148
}
148-
149-
}
150-
}
149+
}
150+
}
151+
}
151152

152153
if ($sCsvFilePath === '') {
153154
// No query at all !!
@@ -158,15 +159,15 @@ public function Prepare()
158159
}
159160

160161
Utils::Log(LOG_INFO, "[".get_class($this)."] CSV file is [".$sCsvFilePath."]");
161-
Utils::Log(LOG_DEBUG, "[".get_class($this)."] Has cs header [".$this->bHasHeader."]");
162+
Utils::Log(LOG_DEBUG, "[".get_class($this)."] Has csv header [".($this->bHasHeader?"yes":"no")."]");
162163
Utils::Log(LOG_DEBUG, "[".get_class($this)."] Separator used is [".$this->sCsvSeparator."]");
163164
Utils::Log(LOG_DEBUG, "[".get_class($this)."] Encoding used is [".$this->sCsvEncoding."]");
164165
Utils::Log(LOG_DEBUG, "[".get_class($this)."] Fields [".var_export($this->aConfiguredHeaderColumns, true)."]");
165166
Utils::Log(LOG_DEBUG, "[".get_class($this)."] Ignored csv fields [".var_export($this->aIgnoredCsvColumns, true)."]");
166167
Utils::Log(LOG_DEBUG, "[".get_class($this)."] Default values [".var_export($this->aSynchroFieldsToDefaultValues, true)."]");
167168

168169
if (!empty($this->sCsvCliCommand)) {
169-
Utils::Exec($this->sCsvCliCommand);
170+
Utils::Exec($this->sCsvCliCommand);
170171
}
171172

172173
try {
@@ -314,13 +315,14 @@ protected function Configure($aCsvHeaderColumns)
314315
}
315316
}
316317
} else {
317-
318-
foreach ($this->aConfiguredHeaderColumns as $sSynchroColumn) {
319-
$this->aMappingCsvColumnIndexToFields[] = [$sSynchroColumn];
320-
$this->aMappedFields[$sSynchroColumn] = '';
318+
foreach ($this->aConfiguredHeaderColumns as $sSynchroField => $sCsvColumn) {
319+
$this->aMappingCsvColumnIndexToFields[$sCsvColumn-1][] = $sSynchroField;
320+
$this->aMappedFields[$sSynchroField] = '';
321321
}
322-
}
323-
322+
foreach ( $this->aIgnoredCsvColumns as $sCsvColumn) {
323+
$this->aMappingCsvColumnIndexToFields[$sCsvColumn-1] = ['ignored_attribute_'.$sCsvColumn];
324+
}
325+
}
324326
foreach ($this->aIgnoredCsvColumns as $sIgnoredCsvColumn) {
325327
$this->aIgnoredSynchroFields = array_merge( $this->aIgnoredSynchroFields, ($this->bHasHeader) ? $this->aMappingCsvColumnNameToFields[$sIgnoredCsvColumn] : $this->aMappingCsvColumnIndexToFields[$sIgnoredCsvColumn - 1]);
326328
}

test/CsvCollectorTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public function OrgCollectorProvider()
8181
"separator_incolumns" => ["separator_incolumns"],
8282
"mapping 1 column twice" => ["map_1_column_twice"],
8383
"mapping 1 column twice adding primary key" => ["map_1_column_twice_primary_key"],
84+
"mapping 1 column twice without header" => ["map_1_column_twice_without_header"],
8485
"mapping 1 column 3 times" => ["map_1_column_3_times"],
8586
"mapping 2 columns twice" => ["map_2_columns_twice"],
8687
"mapping 2 columns 3 times" => ["map_2_columns_3_times"],
@@ -177,11 +178,11 @@ public function ErrorFileProvider()
177178
"[iTopPersonCsvCollector] The mandatory column \"primary_key\" is missing in the csv file.",
178179
'iTopPersonCsvCollector::Collect() got an exception: Missing columns in the csv file.',
179180
),
180-
"no email" => array(
181-
"no_email.csv",
182-
"[iTopPersonCsvCollector] The field \"email\", used for reconciliation, has missing column(s) in the csv file.",
183-
"iTopPersonCsvCollector::Collect() got an exception: Missing columns in the csv file.",
184-
),
181+
"no email" => array(
182+
"no_email.csv",
183+
"[iTopPersonCsvCollector] The field \"email\", used for reconciliation, has missing column(s) in the csv file.",
184+
"iTopPersonCsvCollector::Collect() got an exception: Missing columns in the csv file.",
185+
),
185186
"empty csv" => array(
186187
"empty_file.csv",
187188
"[iTopPersonCsvCollector] CSV file is empty. Data collection stops here.",
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
primary_key;first_name;name;org_id;employee_number;phone;email;function;status;mobile_phone
2+
Isaac;Asimov;Demo;+33123456789;EMP001;EMP001;[email protected];writer;Active;+33601234567
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
class iTopPersonCsvCollector extends CSVCollector
4+
{
5+
protected function MustProcessBeforeSynchro()
6+
{
7+
return true;
8+
}
9+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Isaac;Asimov;Demo;+33123456789;EMP001;[email protected];writer;Active
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- Default values for parameters. Do NOT alter this file, copy its content to conf/params.local.xml and edit it instead -->
3+
<parameters>
4+
<console_log_level>6</console_log_level>
5+
6+
<iTopPersonCsvCollector>
7+
<has_header>no</has_header>
8+
<csv_file>collectors/iTopPersonCsvCollector.csv</csv_file>
9+
<fields>
10+
<primary_key>1</primary_key>
11+
<first_name>2</first_name>
12+
<name>3</name>
13+
<employee_number>5</employee_number>
14+
<function>7</function>
15+
<email>6</email>
16+
<status>8</status>
17+
<phone>5</phone>
18+
<org_id>4</org_id>
19+
</fields>
20+
<defaults>
21+
<mobile_phone>+33601234567</mobile_phone>
22+
</defaults>
23+
</iTopPersonCsvCollector>
24+
25+
<synchro_user>admin</synchro_user>
26+
<contact_to_notify></contact_to_notify>
27+
<full_load_interval></full_load_interval>
28+
29+
<json_placeholders>
30+
<prefix></prefix>
31+
<persons_data_table>synchro_data_person_1</persons_data_table>
32+
<synchro_status>production</synchro_status>
33+
</json_placeholders>
34+
</parameters>

0 commit comments

Comments
 (0)