Skip to content

Commit

Permalink
Merge pull request #6888 from ChurchCRM/6887-bug-csvimport-fails-when…
Browse files Browse the repository at this point in the history
…-importing-birthdate-if-not-all-records-have-birthdate

Have GetAge() return -1 if Year is null, and fix return type
  • Loading branch information
DAcodedBEAT authored Feb 29, 2024
2 parents 0addaa3 + 1c016ad commit 0d90f08
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
3 changes: 3 additions & 0 deletions cypress/data/test_import.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ImportTest,hasBday,"123 Main St", Downtown, TX, 77777,myBday@example.com,2001-07-04,888-555-1212
ImportTest,noYrBday,"123 Main St", Downtown, TX, 77777,noYrBday@example.com,0000-07-04,888-555-1212
ImportTest,noBday,"123 Main St", Downtown, TX, 77777,noBday@example.com,,
22 changes: 22 additions & 0 deletions cypress/e2e/ui/admin/admin.csvimport.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
context('CSVImport', () => {
it('Verify CSV Import', () => {
cy.loginAdmin('CSVImport.php');
cy.get('#CSVFileChooser').selectFile('cypress/data/test_import.csv')
cy.get('#UploadCSVBtn').click();
cy.contains('Total number of rows in the CSV file:3');
// It is not clear why, but it seems that force:true was needed to get the selections to work
cy.get('#SelField0').select("Last Name",{force:true});
cy.get('#SelField1').select("First Name",{force:true});
cy.get('#SelField2').select("Address 1",{force:true});
cy.get('#SelField3').select("City",{force:true});
cy.get('#SelField4').select("State",{force:true});
cy.get('#SelField5').select("Zip",{force:true});
cy.get('#SelField6').select("Email",{force:true});
cy.get('#SelField7').select("Birth Date",{force:true});
cy.get('#SelField8').select("Home Phone",{force:true});
// Now that we have mapped the right fields, do the import
cy.get('#DoImportBtn').click();
cy.contains('Data import successful.');
});
});

30 changes: 14 additions & 16 deletions src/CSVImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,9 @@ public function assignRoles()
$csvError = gettext('No file selected for upload.');
} else {
// Valid file, so save it and display the import mapping form.
$csvTempFile = 'import.csv';
$system_temp = ini_get('session.save_path');
if (strlen($system_temp) > 0) {
$csvTempFile = $system_temp . '/' . $csvTempFile;
}
// Use a temp filename in the system temp dir, and save in SESSION
$csvTempFile = tempnam(sys_get_temp_dir(), 'csvimport');
$_SESSION['csvTempFile'] = $csvTempFile;
move_uploaded_file($_FILES['CSVfile']['tmp_name'], $csvTempFile);

// create the file pointer
Expand Down Expand Up @@ -196,10 +194,11 @@ public function assignRoles()
}

// add select boxes for import destination mapping
// and provide with unique id to assist with testing
for ($col = 0; $col < $numCol; $col++) {
?>
<td>
<select name="<?= 'col' . $col ?>" class="columns">
<select id="<?= 'SelField' . $col ?>" name="<?= 'col' . $col ?>" class="columns">
<option value="0"><?= gettext('Ignore this Field') ?></option>
<option value="1"><?= gettext('Title') ?></option>
<option value="2"><?= gettext('First Name') ?></option>
Expand Down Expand Up @@ -273,7 +272,7 @@ public function assignRoles()
</select>
<?= gettext('Classification') ?>
<BR><BR>
<input type="submit" class="btn btn-primary" value="<?= gettext('Perform Import') ?>" name="DoImport">
<input id="DoImportBtn" type="submit" class="btn btn-primary" value="<?= gettext('Perform Import') ?>" name="DoImport">
</form>

<?php
Expand All @@ -288,14 +287,10 @@ public function assignRoles()
$bHasCustom = false;
$bHasFamCustom = false;

$csvTempFile = 'import.csv';
$system_temp = ini_get('session.save_path');
if (strlen($system_temp) > 0) {
$csvTempFile = $system_temp . '/' . $csvTempFile;
}
//Get the temp filename stored in the session
$csvTempFile = $_SESSION['csvTempFile'];

$Families = [];

// make sure the file still exists
if (file_exists($csvTempFile)) {
// create the file pointer
Expand Down Expand Up @@ -826,9 +821,9 @@ public function assignRoles()
// Display the select file form?>
<p style="color: red"> <?= $csvError ?></p>
<form method="post" action="CSVImport.php" enctype="multipart/form-data">
<input class="icTinyButton" type="file" name="CSVfile">
<input id="CSVFileChooser" class="icTinyButton" type="file" name="CSVfile">
<p></p>
<input type="submit" class="btn btn-success" value=" <?= gettext('Upload CSV File') ?> " name="UploadCSV">
<input id="UploadCSVBtn" type="submit" class="btn btn-success" value=" <?= gettext('Upload CSV File') ?> " name="UploadCSV">
</form>
</p>
<?php
Expand Down Expand Up @@ -916,8 +911,11 @@ function ParseDate(string $sDate, int $iDateMode): array
return $aDate;
}

function GetAge($Month, $Day, $Year): bool
function GetAge(int $Month, int $Day, ?int $Year): int
{
if ($Year === null) {
return -1;
}
if ($Year > 0) {
if ($Year == date('Y')) {
return 0;
Expand Down

0 comments on commit 0d90f08

Please sign in to comment.