static function formatSchool($name, $city, $country) { $saniValid = 1; $msgs = ""; foreach (array("name", "city", "country") as $field) { // Let's try to sanitize it try { ${$field} = DataSanitizer::formatNameComplex(${$field}); } catch (Exception $e) { // Oups : do not modify it, it will be checked in the future // Statistically : 0.5% of names only $saniValid = 0; $msgs .= $e->getMessage() . ";"; } } // The name has been fixed, let's try to format the school category try { $name = DataSanitizer::postFormatSchoolName($name); } catch (Exception $e) { $saniValid = 0; $msgs .= $e->getMessage() . ";"; } return array($name, $city, $country, $saniValid, $msgs); }
function checkAndCorrectSchoolField($field, $execRequests = false) { global $db; if (!in_array($field, array("city", "country", "name"))) { echo "INVALID FIELD {$field}"; return; } /* if ($field == "city") { $query = "UPDATE school SET saniMsg = ''"; $db->prepare($query)->execute(); } */ $query = "\n SELECT \n `school`.ID, \n `school`.{$field}\n FROM `school`\n WHERE\n `school`.`orig_" . $field . "` IS NULL \n "; $stmt = $db->prepare($query); $stmt->execute(); $all = array(); while ($row = $stmt->fetchObject()) { $all[] = $row; } //$all[] = (object)array('ID'=>-1, 'city' => 'Aude', "$field"=>"Bidule de Truc Machin de la marne d'arras de l'eau"); $updateQuery = "UPDATE school SET `orig_" . $field . "` = `" . $field . "`, `" . $field . "` = :value, `saniValid` = :saniValid, saniMsg = CONCAT(saniMsg, :saniMsg) WHERE ID = :ID"; $updateStmt = $db->prepare($updateQuery); foreach ($all as $row) { $newVal = $row->{$field}; $saniValid = 1; $msg = ""; try { $newVal = DataSanitizer::formatNameComplex($row->{$field}); if ($field == 'name') { $newVal = DataSanitizer::postFormatSchoolName($newVal); } if ($newVal != $row->{$field}) { echo "DIFF : {$row->{$field}} => {$newVal}<br/>\n"; } else { echo "GOOD : {$row->{$field}}<br/>\n"; } } catch (Exception $e) { $saniValid = 0; $msg = $e->getMessage() . ";"; echo "ERROR : (id={$row->ID}) {$row->{$field}} => " . $e->getMessage() . "<br/>\n"; } if ($execRequests) { $updateStmt->execute(array(':ID' => $row->ID, ':value' => $newVal, ':saniValid' => $saniValid, ":saniMsg" => $msg)); } } echo "Updated schools (" . $field . ") : " . count($all) . "<br/>"; }