From c71452932f15de6325b08492862920eae74d4dbb Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Fri, 19 Sep 2014 08:38:07 +0000 Subject: [PATCH] dblayer : return the verify result with priority git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1834 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- dblayer.php | 60 +++++++++++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/dblayer.php b/dblayer.php index ace2070..85c70ae 100644 --- a/dblayer.php +++ b/dblayer.php @@ -198,7 +198,8 @@ class dblayer extends PDO /** Verify if the provided datas can be inserted/updated in the database. @param $datas An array containing the datas to verify with keys @param $updatekey the key to update - @return an array with the errors */ + @return an array with the errors in array($key=>array($priority,$message)) + */ public function verify ($datas, $updatekey=false) { // TODO : See if the edited key is needed in UPDATE mode @@ -210,16 +211,16 @@ class dblayer extends PDO // Don't check if there is an update : the database is already filled if (in_array ("not null", $params) && !array_key_exists ($key, $datas)) { - $errors[$key] = sprintf (dgettext("domframework", + $errors[$key] = array ("error", sprintf (dgettext("domframework", "Mandatory field '%s' not provided"), - $key); + $key)); continue; } if (in_array ("not null", $params) && $datas[$key] === "") { - $errors[$key] = sprintf (dgettext("domframework", + $errors[$key] = array ("error", sprintf (dgettext("domframework", "Mandatory field '%s' is empty"), - $key); + $key)); continue; } } @@ -232,7 +233,8 @@ class dblayer extends PDO $verify = $this->verifyOne ($key, $datas[$key]); if (is_array ($verify) && count ($verify)) { - $errors[$key] = $verify[0]." ".$verify[1]." in ".$key; + $errors[$key] = array ($verify[0], $verify[1]." ". + dgettext("domframework","in")." ".$key); continue; } // Check for type inconsistencies if the value is provided @@ -240,10 +242,10 @@ class dblayer extends PDO { if (strspn ($datas[$key], "0123456789") !== strlen ($datas[$key])) { - $errors[$key] = sprintf ( + $errors[$key] = array ("error", sprintf ( dgettext("domframework", "Errors in consistency : '%s' is not an integer"), - $key); + $key)); continue; } } @@ -251,18 +253,18 @@ class dblayer extends PDO { if (! isset ($params[1])) { - $errors[$key] = sprintf ( + $errors[$key] = array ("error", sprintf ( dgettext("domframework", "The length of varchar field '%s' is not provided"), - $key); + $key)); continue; } if (strlen ($datas[$key]) > $params[1]) { - $errors[$key] = sprintf ( + $errors[$key] = array ("error", sprintf ( dgettext("domframework", "Errors in consistency : '%s' data is too long"), - $key); + $key)); continue; } } @@ -272,10 +274,10 @@ class dblayer extends PDO $d = DateTime::createFromFormat("Y-m-d H:i:s", $datas[$key]); if (!$d || $d->format("Y-m-d H:i:s") !== $datas[$key]) { - $errors[$key] = sprintf ( + $errors[$key] = array ("error", sprintf ( dgettext("domframework", "Incorrect datetime provided for field '%s'"), - $key); + $key)); continue; } } @@ -285,17 +287,17 @@ class dblayer extends PDO $d = DateTime::createFromFormat("Y-m-d", $datas[$key]); if (!$d || $d->format("Y-m-d") !== $datas[$key]) { - $errors[$key] = sprintf ( + $errors[$key] = array ("error", sprintf ( dgettext("domframework", "Incorrect date provided for field '%s'"), - $key); + $key)); continue; } } elseif ($datas[$key] !== "") { - $errors[$key] = sprintf (dgettext("domframework", - "Unknown field type for '%s'"), $key); + $errors[$key] = array ("error", sprintf (dgettext("domframework", + "Unknown field type for '%s'"), $key)); continue; } else @@ -304,6 +306,9 @@ class dblayer extends PDO } } + if (count ($errors) !== 0) + return $errors; + // Check for inconsistency $verify = $this->verifyAll ($datas); if (count ($verify)) @@ -322,7 +327,8 @@ class dblayer extends PDO // 1. Read the actual state $before = $this->read (array (array ($this->primary, $updatekey))); if (count ($before) === 0) - return array (dgettext("domframework", "Entry to modify unavailable")); + return array ("error", dgettext("domframework", + "Entry to modify unavailable")); $before = reset ($before); // 2. Map the proposal entries into the before state $after = $before; @@ -354,9 +360,9 @@ class dblayer extends PDO $rc = $this->read ($select, array ($this->primary)); if (count ($rc) > 0) { - $errors[] = sprintf (dgettext("domframework", + $errors[] = array ("error", sprintf (dgettext("domframework", "The provided values for columns '%s' already exists"), - implode (",", $columns)); + implode (",", $columns))); continue; } } @@ -376,9 +382,9 @@ class dblayer extends PDO array ($this->primary)); if (count ($rc) > 0) { - $errors[] = sprintf (dgettext("domframework", + $errors[] = array ("error", sprintf (dgettext("domframework", "The column '%s' with this value already exists"), - $columns); + $columns)); continue; } } @@ -389,9 +395,9 @@ class dblayer extends PDO { if (! isset ($datas[$foreign])) { - $errors[] = sprintf (dgettext("domframework", + $errors[] = array ("error", sprintf (dgettext("domframework", "The forign column '%s' is not provided"), - $foreign); + $foreign)); continue; } $table = $data[0]; @@ -423,9 +429,9 @@ class dblayer extends PDO $res[] = $d; if (count ($res) === 0) { - $errors[] = sprintf (dgettext("domframework", + $errors[] = array ("error", sprintf (dgettext("domframework", "The foreign key '%s' doesn't exists"), - $column); + $column)); continue; } }