i've created the import hook:
function __import__csv(&$data, $defaultValues=array())
{
$rv = cghoa_import_csv($data, $defaultValues,
'owners',
array("ownerID","firstNAME","LASTNAME","role","email","eDeliveryConsent","phone"));
if ( is_string($rv) ) // errors
{
print_r($rv);
exit;
}
return $rv;
}
with the utility function:
function cghoa_import_csv(&$data, $defaultValues,
$tableName,
$reqdFields = array(),
$RS = "\n", // not the same as '\n' wtf?
$FS = ',' )
{
$skipit = "SKIP";
$rows = explode($RS, $data);
$row = array_map('trim', explode($FS, array_shift($rows)));
$errs = array();
$inputFields = array();
foreach ( $row as $fieldName )
{
if (($fieldName == $skipit )
or (array_key_exists($fieldName, $defaultValues)))
{
$inputFields[] = $fieldName;
}
else
{
$inputFields[] = $skipit; // keep count aligned
$errs[] = print_r("******ERROR: invalid field name: ".$fieldName."*****", TRUE);
}
}
$nf = count($inputFields);
foreach ($defaultValues as $fieldName => $defVal)
{
if ( in_array($fieldName, $reqdFields)
and !in_array($fieldName, $inputFields)
and empty($defVal) )
$errs[] = print_r("******ERROR: required field missing: ".$fieldName."*****", TRUE);
}
$records = array();
$nr = 0;
foreach ( $rows as $row )
{
$values = explode($FS, $row);
$nr++;
$cv = count($values);
if ($cv != $nf)
{
$errs[] = print_r("******ERROR: expecting ".$nf." fields but found ".$cv." in record# ".$nr.": ".$row, TRUE);
continue;
}
$record = new Dataface_Record($tableName, array());
$record->setValues($defaultValues);
for($i = 0;$i<$nf;$i++)
if ($inputFields[$i] != $skipit )
$record->setValue( $inputFields[$i], trim($values[$i]) );
$records[] = $record;
}
if (! empty($errs))
return implode("<br>", $errs);
return $records;
}
which works fine elsewhere
entered csv:
"ownerID","firstNAME","LASTNAME","role","email","eDeliveryConsent","phone"
1
Mark & Krista
Donlon
BOARDMEMBER
...@
aba.com 02/18/21
703...
2
Badiollah
Shahidian
HOMEOWNER
2...@cg.org 703...
3
James & Margaret
Kirkwood
HOMEOWNER
3...@cg.org 703...
4
Thomas & Carmen
Torbert
HOMEOWNER
4...@cg.org 5
Andrew
Wyczalkowski Estate, C/O: CATHY KINNIBURGH...
HOMEOWNER
...@
inova.org 703...
preview shows expected values (i'm trying to replace dummy emails with valid ones):
OwnerID
FirstNAME
LASTNAME
Role
Password
Email
Consent to Electronic Communications
Phone
1
Mark & Krista
Donlon
BOARDMEMBER
kdonlon@...
02/18/21
703...
2
Badiollah
Shahidian
HOMEOWNER
Badiollah.Shahidian@...
703...
3
James & Margaret
Kirkwood
HOMEOWNER
3...@cg.org 703...
4
Thomas & Carmen
Torbert
HOMEOWNER
Thomas.Carmen.Torbert@...
5
Andrew
Wyczalkowski Estate C/O: CATHY KINNIBURGH EXECUTOR
HOMEOWNER
Cathy.kinniburgh@...
703...
looks good proceed with import, returns
- Records imported successfully.
but no change...is the missing password field the problem?