improved error reporting

This commit is contained in:
Nathan Eikermann 2021-06-21 23:33:02 +02:00
parent 95468c609d
commit 562ff50bb0
3 changed files with 92 additions and 44 deletions

View File

@ -24,4 +24,26 @@ final class CSVXMLValidator
$inv_errors = []; $inv_errors = [];
$depcon_errors = []; $depcon_errors = [];
} }
public function addError(String $error)
{
$this->error_msgs[] = $error;
}
public function addInvError(String $error)
{
$this->inv_errors[] = $error;
}
public function addDepconError(String $error)
{
$this->depcon_errors[] = $error;
}
public function overallErrorCount(): int
{
return count($this->error_msgs)
+ count($this->inv_errors)
+ count($this->depcon_errors);
}
} }

View File

@ -21,12 +21,12 @@ final class FieldEntry
/** /**
* Function for constructing a new FieldEntry Object. * Function for constructing a new FieldEntry Object.
* *
* @param bool $required True if the field is required. * @param bool $required True if the field is required.
* @param array $allowedValues Array of allowed values. * @param array $allowedValues Array of allowed values.
* @param array $dependsOn Array of fields the entry depends on. * @param array $dependsOn Array of fields the entry depends on.
* @param string $remark String variable. * @param string $remark String variable.
* @param string $name_human_readable Human readable translation. * @param string $name_human_readable Human readable translation.
* @param string $explica String variable * @param string $explica String variable
*/ */
public function __construct( public function __construct(
bool $required, array $allowedValues, bool $required, array $allowedValues,

View File

@ -27,6 +27,7 @@ $csv_datei = MD_STD::realpath(__DIR__ . '/../csv/' . $filename);
$fieldNoMultiplicator = 10; $fieldNoMultiplicator = 10;
$fieldsGetter = new CsvxmlAvailableFields($lang); $fieldsGetter = new CsvxmlAvailableFields($lang);
$availableFields = $fieldsGetter->getFields(); $availableFields = $fieldsGetter->getFields();
$validator = new CSVXMLValidator();
$allowed = $eventpart = $eventpartsure = $fieldsWithDependency = $fieldsWithAllowedValueSet = []; $allowed = $eventpart = $eventpartsure = $fieldsWithDependency = $fieldsWithAllowedValueSet = [];
@ -68,7 +69,6 @@ echo '</form>';
echo '1: Only allowed tags (column names) used?'; echo '1: Only allowed tags (column names) used?';
$fp = fopen($csv_datei, 'r'); $fp = fopen($csv_datei, 'r');
$y = 1; $y = 1;
$error = 0;
$zeile = fgetcsv($fp, 100000, ';'); $zeile = fgetcsv($fp, 100000, ';');
$maxLoopLen = count($zeile); $maxLoopLen = count($zeile);
for ($x = 0; $x < $maxLoopLen; $x++) { for ($x = 0; $x < $maxLoopLen; $x++) {
@ -76,22 +76,29 @@ for ($x = 0; $x < $maxLoopLen; $x++) {
$zeile[$x] = str_replace("\xEF\xBB\xBF", "", $zeile[$x]); $zeile[$x] = str_replace("\xEF\xBB\xBF", "", $zeile[$x]);
$inhalt[$y][$x] = $zeile[$x]; $inhalt[$y][$x] = $zeile[$x];
if (!in_array($inhalt[1][$x], $allowed)) { if (!in_array($inhalt[1][$x], $allowed)) {
echo '<br><i style="font-style:normal;color:#990000;">ERROR in column ' . $x . ' created by value: ' . $inhalt[1][$x] . '</i>'; $validator->addError(
$error = $error + 1; '<br><i style="font-style:normal;color:#990000;">ERROR in column ' . $x
. ' created by value: ' . $inhalt[1][$x] . '</i>'
);
} }
//echo '<br/>';var_dump($inhalt[1][$x]); //echo '<br/>';var_dump($inhalt[1][$x]);
} }
fclose($fp); fclose($fp);
$erstezeile = $zeile; $erstezeile = $zeile;
if ($error != 0) echo '<br><b style="color:#990000;">Not allowed tags found !</b>'; else echo '<br><i style="font-style:normal;color:#009900;">Only allowed tags used !</i>'; if (count($validator->error_msgs) != 0) {
echo '<br><b style="color:#990000;">Not allowed tags found !</b>';
} else {
echo '<br><i style="font-style:normal;color:#009900;">Only allowed tags used !</i>';
}
//// Check #2 //// Check #2
echo '<br><br>2: Not allowed multiple use of tags (column names)?'; echo '<br><br>2: Not allowed multiple use of tags (column names)?';
$compare = array_unique($zeile); $compare = array_unique($zeile);
if (count($zeile) != count($compare)) { if (count($zeile) != count($compare)) {
echo '<br><b style="color:#990000;">There are duplicate column names !</b>'; $validator->addError(
$error = $error + 1; '<br><b style="color:#990000;">There are duplicate column names !</b>'
);
} else { } else {
echo '<br><i style="font-style:normal;color:#009900;">No duplicate column names !</i>'; echo '<br><i style="font-style:normal;color:#009900;">No duplicate column names !</i>';
} }
@ -114,7 +121,6 @@ fclose($fp);
///// Check #3 ///// Check #3
echo '<br><br>3: Mandatory tags available and always filled in?'; echo '<br><br>3: Mandatory tags available and always filled in?';
unset($inv_array); unset($inv_array);
$inv_error = 0;
/** /**
* Function for finding duplicates?. * Function for finding duplicates?.
@ -129,20 +135,21 @@ function Get_duplicates(array $array):array
} }
//TODO: function for returning the error strings & counting errors
$mandatory = ['inventory_number','object_type','object_title','object_description']; $mandatory = ['inventory_number','object_type','object_title','object_description'];
foreach ($mandatory as $tMandatoryField) { foreach ($mandatory as $tMandatoryField) {
if (!in_array($tMandatoryField, $erstezeile)) { if (!in_array($tMandatoryField, $erstezeile)) {
echo '<br><i style="font-style:normal;color:#990000;">Mandatory: Column <b>' . $tMandatoryField . '</b> missing</i>'; $validator->addInvError(
$error = $error + 1; '<br><i style="font-style:normal;color:#990000;">Mandatory: Column <b>'
$inv_error = $inv_error + 1; . $tMandatoryField . '</b> missing</i>'
);
} else { } else {
$spaltenr = array_search($tMandatoryField, $erstezeile); $spaltenr = array_search($tMandatoryField, $erstezeile);
for ($j = 0; $j < $y; $j++) { for ($j = 0; $j < $y; $j++) {
if ($inhalt[$j + 1][$spaltenr] == '') { if ($inhalt[$j + 1][$spaltenr] == '') {
echo '<br><i style="font-style:normal;color:#990000;">Missing value for <b>' . $tMandatoryField . '</b> in row ' . ($j + 1) . '</i>'; $validator->addInvError(
$error = $error + 1; '<br><i style="font-style:normal;color:#990000;">Missing value for <b>'
$inv_error = $inv_error + 1; . $tMandatoryField . '</b> in row ' . ($j + 1) . '</i>'
);
} }
if ($tMandatoryField == 'inventory_number') { if ($tMandatoryField == 'inventory_number') {
$inv_array[] = $inhalt[$j + 1][$spaltenr]; $inv_array[] = $inhalt[$j + 1][$spaltenr];
@ -151,7 +158,7 @@ foreach ($mandatory as $tMandatoryField) {
} }
} }
if ($inv_error == 0) { if (count($validator->inv_errors) == 0) {
echo '<br><i style="font-style:normal;color:#009900;">All mandatory tags available and with values !</i>'; echo '<br><i style="font-style:normal;color:#009900;">All mandatory tags available and with values !</i>';
} }
@ -163,15 +170,18 @@ if (in_array('inventory_number', $erstezeile)) {
$doppelte_inv = array_values($doppelte_inv); $doppelte_inv = array_values($doppelte_inv);
if (!empty($doppelte_inv)) { if (!empty($doppelte_inv)) {
foreach ($doppelte_inv as $tDublicateInvNo) { foreach ($doppelte_inv as $tDublicateInvNo) {
echo '<br><i style="font-style:normal;color:#990000;">Multiple use of inventory_number <b>' . $tDublicateInvNo . '</b></i>'; $validator->addError(
$error = $error + 1; '<br><i style="font-style:normal;color:#990000;">Multiple use of inventory_number <b>'
. $tDublicateInvNo . '</b></i>'
);
} }
} else { } else {
echo '<br><i style="font-style:normal;color:#009900;">All inventory_numbers are unique !</i>'; echo '<br><i style="font-style:normal;color:#009900;">All inventory_numbers are unique !</i>';
} }
} else { } else {
echo '<br><b style="font-style:normal;color:#990000;">Aborted, column inventory_number is missing</b>'; $validator->addError(
$error = $error + 1; '<br><b style="font-style:normal;color:#990000;">Aborted, column inventory_number is missing</b>'
);
} }
///// Check #5 ///// Check #5
@ -192,8 +202,7 @@ foreach ($fieldsWithDependency as $tField => $tDependentFields) {
if (!empty($depencymessage)) { if (!empty($depencymessage)) {
echo '<b style="color:#990000;">Dependent columns were not observed !</b>'; echo '<b style="color:#990000;">Dependent columns were not observed !</b>';
foreach ($depencymessage as $tDepMsg) { foreach ($depencymessage as $tDepMsg) {
echo '<br>' . $tDepMsg; $validator->addError('<br>' . $tDepMsg)
$error = $error + 1;
} }
} else { } else {
echo '<i style="font-style:normal;color:#009900;">Dependent columns were observed !</i>'; echo '<i style="font-style:normal;color:#009900;">Dependent columns were observed !</i>';
@ -207,15 +216,16 @@ echo '<br><br>6: Dependency of content observed?';
$crosscheck1 = ['object_other_title','detailed_description','detailed_description','inscription','inscription','dimensions_separate_length_value', 'dimensions_separate_width_value', 'dimensions_separate_height_value', 'dimensions_separate_diameter_value', 'dimensions_separate_wall_thickness_value', 'dimensions_separate_weight_value','closer_location','bought_for','worth_value','worth_insurance_value']; $crosscheck1 = ['object_other_title','detailed_description','detailed_description','inscription','inscription','dimensions_separate_length_value', 'dimensions_separate_width_value', 'dimensions_separate_height_value', 'dimensions_separate_diameter_value', 'dimensions_separate_wall_thickness_value', 'dimensions_separate_weight_value','closer_location','bought_for','worth_value','worth_insurance_value'];
$crosscheck2 = ['object_other_title_kind_of','detailed_description_md','detailed_description_extern','inscription_md','inscription_extern','dimensions_separate_length_unit', 'dimensions_separate_width_unit', 'dimensions_separate_height_unit', 'dimensions_separate_diameter_unit', 'dimensions_separate_wall_thickness_unit', 'dimensions_separate_weight_unit','closer_location_as','bought_for_currency','worth_unit','worth_insurance_unit']; $crosscheck2 = ['object_other_title_kind_of','detailed_description_md','detailed_description_extern','inscription_md','inscription_extern','dimensions_separate_length_unit', 'dimensions_separate_width_unit', 'dimensions_separate_height_unit', 'dimensions_separate_diameter_unit', 'dimensions_separate_wall_thickness_unit', 'dimensions_separate_weight_unit','closer_location_as','bought_for_currency','worth_unit','worth_insurance_unit'];
$depcon_error = 0;
foreach ($crosscheck1 as $l => $tCrossCheck) { foreach ($crosscheck1 as $l => $tCrossCheck) {
if (in_array($tCrossCheck, $erstezeile)) { if (in_array($tCrossCheck, $erstezeile)) {
for ($j = 1; $j < ($y + 1); $j++) { for ($j = 1; $j < ($y + 1); $j++) {
if ($inhalt[$j][array_search($crosscheck2[$l], $erstezeile)] !== '' and $inhalt[$j][array_search($tCrossCheck, $erstezeile)] == '') { if ($inhalt[$j][array_search($crosscheck2[$l], $erstezeile)] !== '' and $inhalt[$j][array_search($tCrossCheck, $erstezeile)] == '') {
echo '<br>Tag <b>' . $crosscheck2[$l] . '</b> given but no entry for <b>' . $tCrossCheck . '</b> (row ' . $j . ')'; $validator->addDepconError(
$depcon_error++; '<br>Tag <b>' . $crosscheck2[$l] . '</b> given but no entry for <b>'
. $tCrossCheck . '</b> (row ' . $j . ')'
);
} }
} }
@ -227,8 +237,10 @@ foreach ($eventpart as $l => $tEventPart) {
if (in_array($tEventPart, $erstezeile)) { if (in_array($tEventPart, $erstezeile)) {
for ($j = 1; $j < ($y + 1); $j++) { for ($j = 1; $j < ($y + 1); $j++) {
if ($inhalt[$j][array_search($eventpartsure[$l], $erstezeile)] !== '' and $inhalt[$j][array_search($tEventPart, $erstezeile)] == '') { if ($inhalt[$j][array_search($eventpartsure[$l], $erstezeile)] !== '' and $inhalt[$j][array_search($tEventPart, $erstezeile)] == '') {
echo '<br>Tag <b>' . $eventpartsure[$l] . '</b> given but no entry for <b>' . $tEventPart . '</b> (row ' . $j . ')'; $validator->addDepconError(
$depcon_error++; '<br>Tag <b>' . $eventpartsure[$l] . '</b> given but no entry for <b>'
. $tEventPart . '</b> (row ' . $j . ')'
);
} }
} }
} }
@ -237,8 +249,10 @@ foreach ($eventpart as $l => $tEventPart) {
if (in_array('dimensions_separate_show_md', $erstezeile)) { if (in_array('dimensions_separate_show_md', $erstezeile)) {
for ($j = 1; $j < ($y + 1); $j++) { for ($j = 1; $j < ($y + 1); $j++) {
if ($inhalt[$j][array_search('dimensions_separate_show_md', $erstezeile)] !== '' and ($inhalt[$j][array_search('dimensions_separate_length_value', $erstezeile)] == '') and $inhalt[$j][array_search('dimensions_separate_width_value', $erstezeile)] == '' and $inhalt[$j][array_search('dimensions_separate_heigt_value', $erstezeile)] == '' and $inhalt[$j][array_search('dimensions_separate_weight_value', $erstezeile)] == '' and $inhalt[$j][array_search('dimensions_separate_diameter_value', $erstezeile)] == '' and $inhalt[$j][array_search('dimensions_separate_wall_thickness_value', $erstezeile)] == '') { if ($inhalt[$j][array_search('dimensions_separate_show_md', $erstezeile)] !== '' and ($inhalt[$j][array_search('dimensions_separate_length_value', $erstezeile)] == '') and $inhalt[$j][array_search('dimensions_separate_width_value', $erstezeile)] == '' and $inhalt[$j][array_search('dimensions_separate_heigt_value', $erstezeile)] == '' and $inhalt[$j][array_search('dimensions_separate_weight_value', $erstezeile)] == '' and $inhalt[$j][array_search('dimensions_separate_diameter_value', $erstezeile)] == '' and $inhalt[$j][array_search('dimensions_separate_wall_thickness_value', $erstezeile)] == '') {
echo '<br>Tag <b>dimensions_separate_show_md</b> given but no separate values available (row ' . $j . ')'; $validator->addDepconError(
$depcon_error = $depcon_error + 1; '<br>Tag <b>dimensions_separate_show_md</b> given but no separate values available (row '
. $j . ')'
);
} }
} }
} }
@ -246,8 +260,10 @@ if (in_array('dimensions_separate_show_md', $erstezeile)) {
if (in_array('dimensions_separate_show_extern', $erstezeile)) { if (in_array('dimensions_separate_show_extern', $erstezeile)) {
for ($j = 1; $j < ($y + 1); $j++) { for ($j = 1; $j < ($y + 1); $j++) {
if ($inhalt[$j][array_search('dimensions_separate_show_extern', $erstezeile)] !== '' and ($inhalt[$j][array_search('dimensions_separate_length_value', $erstezeile)] == '') and $inhalt[$j][array_search('dimensions_separate_width_value', $erstezeile)] == '' and $inhalt[$j][array_search('dimensions_separate_heigt_value', $erstezeile)] == '' and $inhalt[$j][array_search('dimensions_separate_weight_value', $erstezeile)] == '' and $inhalt[$j][array_search('dimensions_separate_diameter_value', $erstezeile)] == '' and $inhalt[$j][array_search('dimensions_separate_wall_thickness_value', $erstezeile)] == '') { if ($inhalt[$j][array_search('dimensions_separate_show_extern', $erstezeile)] !== '' and ($inhalt[$j][array_search('dimensions_separate_length_value', $erstezeile)] == '') and $inhalt[$j][array_search('dimensions_separate_width_value', $erstezeile)] == '' and $inhalt[$j][array_search('dimensions_separate_heigt_value', $erstezeile)] == '' and $inhalt[$j][array_search('dimensions_separate_weight_value', $erstezeile)] == '' and $inhalt[$j][array_search('dimensions_separate_diameter_value', $erstezeile)] == '' and $inhalt[$j][array_search('dimensions_separate_wall_thickness_value', $erstezeile)] == '') {
echo '<br>Tag <b>dimensions_separate_show_extern</b> given but no separate values available (row ' . $j . ')'; $validator->addDepconError(
$depcon_error = $depcon_error + 1; '<br>Tag <b>dimensions_separate_show_extern</b> given but no separate values available (row '
. $j . ')'
);
} }
} }
} }
@ -272,7 +288,9 @@ for ($im=1;$im<11;$im++)
} }
} }
*/ */
if ($depcon_error == 0) echo '<br><i style="font-style:normal;color:#009900;">Dependency of content was observed !</i>'; if (count($validator->depcon_errors) == 0) {
echo '<br><i style="font-style:normal;color:#009900;">Dependency of content was observed !</i>';
}
///// Check #7 ///// Check #7
echo '<br><br>7: Not allowed values in controlled lists?<br>'; echo '<br><br>7: Not allowed values in controlled lists?<br>';
@ -316,11 +334,10 @@ for ($i = 2; $i <= $y; $i++) {
} }
} }
if (isset($errormessage) and $errormessage != '') { if (!empty($errormessage)) {
echo '<b style="color:#990000;">Columns with controlled values contain invalid values !</b>'; echo '<b style="color:#990000;">Columns with controlled values contain invalid values !</b>';
foreach ($errormessage as $tMsg) { foreach ($errormessage as $tMsg) {
echo '<br>' . $tMsg; $validator->addError('<br>' . $tMsg);
$error++;
} }
} else { } else {
echo '<i style="font-style:normal;color:#009900;">Values in controlled fields are all valid !</i>'; echo '<i style="font-style:normal;color:#009900;">Values in controlled fields are all valid !</i>';
@ -378,8 +395,7 @@ if ($hasanyimage > 0) {
if (!empty($errormessage)) { if (!empty($errormessage)) {
echo '<b style="color:#990000;">There is not one main image for each object !</b>'; echo '<b style="color:#990000;">There is not one main image for each object !</b>';
foreach ($errormessage as $tMsg) { foreach ($errormessage as $tMsg) {
echo '<br>' . $tMsg; $validator->addError('<br>' . $tMsg);
$error++;
} }
} else { } else {
echo '<i style="font-style:normal;color:#009900;">For each object that has images attached exactly one main image is given !</i>'; echo '<i style="font-style:normal;color:#009900;">For each object that has images attached exactly one main image is given !</i>';
@ -389,9 +405,19 @@ if ($hasanyimage > 0) {
} }
echo '<hr>'; echo '<hr>';
if ($error + $depcon_error > 0) {
if ($validator->overallErrorCount() > 0) {
echo ' echo '
<p>Error(s) found: ' . ($error + $depcon_error) . '</p>'; <p>Error(s) found: ' . $validator->overallErrorCount() . '</p>';
foreach ($validator->error_msgs as $msg) {
echo $msg;
}
foreach ($validator->inv_errors as $msg) {
echo $msg;
}
foreach ($validator->depcon_errors as $msg) {
echo $msg;
}
echo '<a href="index6.php?fnam=' . htmlspecialchars($_GET['fnam']) . '" class="buttonLike">Create XML for md:import (utf8)</a><br>'; echo '<a href="index6.php?fnam=' . htmlspecialchars($_GET['fnam']) . '" class="buttonLike">Create XML for md:import (utf8)</a><br>';
} else { } else {
echo '<a href="index6.php?fnam=' . htmlspecialchars($_GET['fnam']) . '" class="buttonLike">Create XML for md:import (utf8)</a><br>'; echo '<a href="index6.php?fnam=' . htmlspecialchars($_GET['fnam']) . '" class="buttonLike">Create XML for md:import (utf8)</a><br>';