@@ -141,25 +141,38 @@ private function validate_csv($csv_data) {
141141
142142 //Validate CSV
143143 $ validate_num_fields = VALIDATE_NUM_FIELDS ;
144- $ validation_flag = true ;
144+ $ validation_flag = true ; //Set to false to invalidate the entire CSV file.
145+ $ rpi_found_non_empty_row = false ; //RPI edge case flag.
145146 foreach ($ csv_data as $ index => $ csv_row ) {
146- //Split each row by delim character so that individual fields are indexed .
147- //Trim any extraneous whitespaces from all rows and fields .
148- $ row = array ();
149- foreach ( explode ( CSV_DELIM_CHAR , trim ( $ csv_row )) as $ i => $ field ) {
150- $ row [ $ i ] = trim ( $ field );
151- }
147+ // 1) Trim CSV row. Do not trim CSV_DELIM_CHAR .
148+ // 2) Convert CSV row to array .
149+ // 3) Trim array fields.
150+ $ trim_str = " \t\n\r\0\x0B" ; //$trim_str = space, tab, newline, carriage return, null byte, vertical tab
151+ $ trim_str = str_replace ( CSV_DELIM_CHAR , "" , $ trim_str ); //remove CSV_DELIM_CHAR from $trim_str
152+ $ row = array_map ( ' trim ' , explode ( CSV_DELIM_CHAR , trim ( $ csv_row , $ trim_str )));
152153
153154 //BEGIN VALIDATION
154155 //Invalidate any row that doesn't have requisite number of fields. Do this, first.
155156 //Invalidation will disqualify the data file to protect DB data integrity.
156157 $ num_fields = count ($ row );
157158 if ($ num_fields !== $ validate_num_fields ) {
158- $ this ->log_it ("Row {$ index } has {$ num_fields } columns. {$ validate_num_fields } expected. " );
159+ $ this ->log_it ("Row {$ index } has {$ num_fields } columns. {$ validate_num_fields } expected. CSV disqualified. " );
159160 $ validation_flag = false ;
160161 continue ;
162+ } else if (empty (array_filter ($ row , function ($ field ) { return !empty ($ field ); }))) {
163+ if (!$ rpi_found_non_empty_row ) {
164+ // RPI edge case to skip a correctly sized row of all empty fields — at the top of a data file, before proper data is read — without invalidating the whole data file.
165+ $ this ->log_it ("Row {$ index } is correct size ( {$ validate_num_fields }), but all columns are empty — at top of CSV. Ignoring row. " );
166+ continue ;
167+ } else {
168+ // Correctly sized empty row below data row(s) — invalidate data file.
169+ $ this ->log_it ("Row {$ index } is correct size ( {$ validate_num_fields }), but all columns are empty — below a non-empty data row. CSV disqualified. " );
170+ $ validation_flag = false ;
171+ continue ;
172+ }
161173 }
162174
175+ $ rpi_found_non_empty_row = true ;
163176 $ course = strtolower ($ row [COLUMN_COURSE_PREFIX ]) . $ row [COLUMN_COURSE_NUMBER ];
164177 // Remove any leading zeroes from "integer" registration sections.
165178 $ section = (ctype_digit ($ row [COLUMN_SECTION ])) ? ltrim ($ row [COLUMN_SECTION ], "0 " ) : $ row [COLUMN_SECTION ];
@@ -222,6 +235,7 @@ private function validate_csv($csv_data) {
222235
223236 //Validation passed. Include row in data set.
224237 self ::$ data ['users ' ][] = array ('user_id ' => $ row [COLUMN_USER_ID ],
238+ 'user_numeric_id ' => $ row [COLUMN_NUMERIC_ID ],
225239 'user_firstname ' => $ row [COLUMN_FIRSTNAME ],
226240 'user_preferredname ' => $ row [COLUMN_PREFERREDNAME ],
227241 'user_lastname ' => $ row [COLUMN_LASTNAME ],
@@ -451,6 +465,7 @@ private function upsert_psql() {
451465 $ sql ['users ' ]['temp_table ' ] = <<<SQL
452466CREATE TEMPORARY TABLE upsert_users (
453467 user_id VARCHAR,
468+ user_numeric_id VARCHAR,
454469 user_firstname VARCHAR,
455470 user_preferred_firstname VARCHAR,
456471 user_lastname VARCHAR,
@@ -484,7 +499,7 @@ private function upsert_psql() {
484499SQL ;
485500
486501 $ sql ['users ' ]['data ' ] = <<<SQL
487- INSERT INTO upsert_users VALUES ($1,$2,$3,$4,$5);
502+ INSERT INTO upsert_users VALUES ($1,$2,$3,$4,$5,$6 );
488503SQL ;
489504
490505 $ sql ['courses_users ' ]['data ' ] = <<<SQL
@@ -509,6 +524,7 @@ private function upsert_psql() {
509524 $ sql ['users ' ]['update ' ] = <<<SQL
510525UPDATE users
511526SET
527+ user_numeric_id=upsert_users.user_numeric_id,
512528 user_firstname=upsert_users.user_firstname,
513529 user_lastname=upsert_users.user_lastname,
514530 user_preferred_firstname=
@@ -543,12 +559,14 @@ private function upsert_psql() {
543559 $ sql ['users ' ]['insert ' ] = <<<SQL
544560INSERT INTO users (
545561 user_id,
562+ user_numeric_id,
546563 user_firstname,
547564 user_lastname,
548565 user_preferred_firstname,
549566 user_email
550567) SELECT
551568 upsert_users.user_id,
569+ upsert_users.user_numeric_id,
552570 upsert_users.user_firstname,
553571 upsert_users.user_lastname,
554572 upsert_users.user_preferred_firstname,
0 commit comments