diff --git a/admin/schools.php b/admin/schools.php index ffa43a47..18a6a905 100644 --- a/admin/schools.php +++ b/admin/schools.php @@ -264,8 +264,8 @@ if (get_value_from_array($_GET, 'action') == 'edit' || get_value_from_array($_GE emit_province_selector('province_code', get_value_property_or_default($r, 'province_code', '')); echo "\n"; echo '' . i18n($config['postalzip']) . '\n"; - echo '' . i18n('Phone') . '\n"; - echo '' . i18n('Fax') . '\n"; + echo '' . i18n('Phone') . '\n"; + echo '' . i18n('Fax') . '\n"; if (get_value_property_or_default($r, 'principal_uid') > 0) $pl = user_load_by_uid(get_value_property_or_default($r, 'principal_uid')); @@ -275,9 +275,9 @@ if (get_value_from_array($_GET, 'action') == 'edit' || get_value_from_array($_GE $e = get_value_from_array($pl, 'email', 0) == '*' ? '' : get_value_from_array($pl, 'email'); echo '' . i18n('Principal') . '\n"; - echo '' . i18n('Principal Email') . '\n"; + echo '' . i18n('Principal Email') . '\n"; - echo '' . i18n('School Email') . '\n"; + echo '' . i18n('School Email') . '\n"; echo '' . i18n('Access Code') . '\n"; echo '
' . i18n('Science head/teacher or science fair contact at school') . ''; if (get_value_property_or_default($r, 'sciencehead_uid', '') > 0) @@ -285,10 +285,10 @@ if (get_value_from_array($_GET, 'action') == 'edit' || get_value_from_array($_GE else $sh = array(); /* Don't show autogenerated emails */ - $e = get_value_from_2d_array($sh, 'email', 0, '') == '*' ? '' : get_value_from_2d_array($sh, 'email', ''); - echo '' . i18n('Email') . '\n"; + $e = get_value_from_array($sh, 'email', '') == '*' ? '' : get_value_from_array($sh, 'email', ''); + echo '' . i18n('Email') . '\n"; echo '' . i18n('Name') . '\n"; - echo '' . i18n('Phone') . '\n"; + echo '' . i18n('Phone') . '\n"; if ($config['participant_registration_type'] == 'schoolpassword') { echo '
' . i18n('Participant Registration Password') . ''; @@ -378,13 +378,18 @@ if (get_value_from_array($_GET, 'action') == 'edit' || get_value_from_array($_GE while ($r = $q->fetch(PDO::FETCH_OBJ)) { echo "\n"; echo " $r->school\n"; - echo " $r->address, $r->city, $r->postalcode\n"; - echo " $r->phone\n"; + if ($r->address != '' && $r->city != '' && $r->postalcode != '') { + echo " $r->address, $r->city, $r->postalcode\n"; + } else { + echo " \n"; + } + + echo " $r->phone\n"; $sciencehead = ''; - if ($r->sciencehead_uid > 0) { + if ($r->sciencehead_uid != NULL && $r->sciencehead_uid > 0) { $sh = user_load_by_uid($r->sciencehead_uid); - $sciencehead = $sh['name']; + $sciencehead = $sh['name'] ?? ''; } echo " $sciencehead\n"; if ($config['participant_registration_type'] == 'schoolpassword') diff --git a/admin/schoolsimport.php b/admin/schoolsimport.php index e30974d0..400bcb00 100644 --- a/admin/schoolsimport.php +++ b/admin/schoolsimport.php @@ -48,18 +48,18 @@ if (get_value_from_array($_POST, 'action') == 'import') { if (count($CSVP->data) > 0) { // okay it looks like we have something.. lets dump the current stuff if ($_POST['emptycurrent'] == 1) { - echo happy(i18n('Old school data erased')); - $stmt = $pdo->prepare('DELETE FROM schools WHERE year=?'); - $stmt->execute([$config['FAIRYEAR']]); + try { + $stmt = $pdo->prepare('DELETE FROM schools WHERE year=?'); + $stmt->execute([$config['FAIRYEAR']]); + echo happy(i18n('Old school data erased')); + } catch (PDOException $exception) { + echo error(i18n('Failed to erase old school data')); + } } $loaded = 0; foreach ($CSVP->data AS $raw_row) { - $row = explode(',', $raw_row[0]); - - for ($n = 0; $n < count($row); $n++) { - $row[$n] = trim(trim($row[$n]), '"'); - } + $row = str_getcsv($raw_row[0], ',', '"', '\\'); // $email = $row[16]; // if ($email != '') { @@ -89,37 +89,42 @@ if (get_value_from_array($_POST, 'action') == 'import') { // user_save($principal); // } - $stmt = $pdo->prepare('INSERT INTO schools - (school, schoollang, schoollevel, board, district, phone, fax, address, city, province_code, postalcode, schoolemail, accesscode, registration_password, projectlimit, projectlimitper, year, principal_uid, sciencehead_uid) - VALUES - (:school, :schoollang, :schoollevel, :board, :district, :phone, :fax, :address, :city, :province_code, :postalcode, :schoolemail, :accesscode, :registration_password, :projectlimit, :projectlimitper, :year, :principal_uid, :sciencehead_uid)'); + $total = $row[22] != '' ? $row[22] : 'total'; + $limit = $row[21] != '' ? $row[21] : 0; + try { + $stmt = $pdo->prepare('INSERT INTO schools + (school, schoollang, schoollevel, designate, board, district, phone, fax, address, city, province_code, postalcode, schoolemail, accesscode, registration_password, projectlimit, projectlimitper, year, principal_uid, sciencehead_uid) + VALUES + (:school, :schoollang, :schoollevel, :designate, :board, :district, :phone, :fax, :address, :city, :province_code, :postalcode, :schoolemail, :accesscode, :registration_password, :projectlimit, :projectlimitper, :year, :principal_uid, :sciencehead_uid)'); - $stmt->bindParam(':school', $row[0]); - $stmt->bindParam(':schoollang', $row[1]); - $stmt->bindParam(':schoollevel', $row[2]); - $stmt->bindParam(':board', $row[3]); - $stmt->bindParam(':district', $row[4]); - $stmt->bindParam(':phone', $row[5]); - $stmt->bindParam(':fax', $row[6]); - $stmt->bindParam(':address', $row[7]); - $stmt->bindParam(':city', $row[8]); - $stmt->bindParam(':province_code', $row[9]); - $stmt->bindParam(':postalcode', $row[10]); - $stmt->bindParam(':schoolemail', $row[11]); - $stmt->bindParam(':accesscode', $row[12]); - $stmt->bindParam(':registration_password', $row[13]); - $stmt->bindParam(':projectlimit', $row[14]); - $stmt->bindParam(':projectlimitper', $row[15]); - $stmt->bindParam(':year', $config['FAIRYEAR']); - $stmt->bindParam(':principal_uid', $principal['uid']); - $stmt->bindParam(':sciencehead_uid', $scienceHead['uid']); + $stmt->bindValue(':school', htmlspecialchars($row[0])); + $stmt->bindParam(':schoollang', $row[1]); + $stmt->bindParam(':schoollevel', $row[2]); + $stmt->bindParam(':designate', $row[3]); + $stmt->bindParam(':board', $row[4]); + $stmt->bindParam(':district', $row[5]); + $stmt->bindParam(':phone', $row[6]); + $stmt->bindParam(':fax', $row[7]); + $stmt->bindParam(':address', $row[8]); + $stmt->bindParam(':city', $row[9]); + $stmt->bindParam(':province_code', $row[10]); + $stmt->bindParam(':postalcode', $row[11]); + $stmt->bindParam(':schoolemail', $row[15]); + $stmt->bindParam(':accesscode', $row[19]); + $stmt->bindParam(':registration_password', $row[20]); + $stmt->bindParam(':projectlimit', $limit); + $stmt->bindValue(':projectlimitper', $total); + $stmt->bindParam(':year', $config['FAIRYEAR']); + $stmt->bindParam(':principal_uid', $principal['uid']); + $stmt->bindParam(':sciencehead_uid', $scienceHead['uid']); - $stmt->execute(); + $stmt->execute(); - if ($pdo->errorInfo() != '00000') { $loaded++; - } else { - show_pdo_errors_if_any($pdo); + } catch (PDOException $exception) { + error_log($exception); + echo error(i18n('Failed to load schools %2 at row %1', array($loaded++, $row[0]))); + var_dump($row); } } echo happy(i18n('Successfully loaded %1 schools', array($loaded))); diff --git a/csvimport.inc.php b/csvimport.inc.php index d6fd7eef..9b176080 100644 --- a/csvimport.inc.php +++ b/csvimport.inc.php @@ -56,12 +56,12 @@ class CSVParser // End of line if ($ch == "\n" && !$quoted) { // Remove enclosure delimiters - for ($k = 0; $k < count($row); $k++) { + /*for ($k = 0; $k < count($row); $k++) { if ($row[$k] != '' && $row[$k][0] == $this->enclosure) { $row[$k] = substr($row[$k], 1, strlen($row[$k]) - 2); } $row[$k] = str_replace(str_repeat($this->enclosure, 2), $this->enclosure, $row[$k]); - } + }*/ // Append row into table $this->data[] = $row;