routeSQL : Redirection are now compatible with chained mode

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2025 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2015-03-13 13:10:23 +00:00
parent 156cf358ac
commit 37f0159ae8

View File

@@ -276,9 +276,10 @@ class routeSQL
/** Add HTML routes */ /** Add HTML routes */
$route = new route (); $route = new route ();
$route->allowSlashes=false; $route->allowSlashes=false;
$route->get ($this->url_prefix."/", function () use ($route) $route->get ($this->url_prefix."/", function ($chain) use ($route)
{ {
$route->redirect ("/".$this->url_prefix, ""); $route->redirect ("/".str_replace ("{chain}", $chain, $this->url_prefix),
"");
}); });
$route->get ($this->url_prefix. $route->get ($this->url_prefix.
@@ -365,16 +366,16 @@ class routeSQL
} }
$nbentries = count ($datas); $nbentries = count ($datas);
if ($num > 1000) if ($num > 1000)
$route->redirect ("/".$this->url_prefix. $route->redirect ("/".str_replace ("{chain}", $chain, $this->url_prefix)
"?page=$page&num=1000&search=$search", ""); ."?page=$page&num=1000&search=$search", "");
if ($page < 1) if ($page < 1)
$route->redirect ("/".$this->url_prefix. $route->redirect ("/".str_replace ("{chain}", $chain, $this->url_prefix)
"?page=1&num=$num&search=$search", ""); . "?page=1&num=$num&search=$search", "");
// Push on the last page if the values are too high // Push on the last page if the values are too high
if ($nbentries > 0 && ($page-1)*$num > $nbentries) if ($nbentries > 0 && ($page-1)*$num > $nbentries)
{ {
$maxPage = intval ($nbentries/$num)+1; $maxPage = intval ($nbentries/$num)+1;
$route->redirect ("/".$this->url_prefix. $route->redirect ("/".str_replace("{chain}", $chain, $this->url_prefix).
"?page=$maxPage&num=$num&search=$search", ""); "?page=$maxPage&num=$num&search=$search", "");
} }
@@ -553,12 +554,16 @@ echo $content;
{ {
$csrf->checkToken ($token); $csrf->checkToken ($token);
$this->objectDB->delete ($id); $this->objectDB->delete ($id);
$route->redirect ("/".$this->url_prefix, ""); $route->redirect ("/".
str_replace ("{chain}", $chain, $this->url_prefix),
"");
} }
catch (Exception $e) catch (Exception $e)
{ {
$renderer->flash ("ERROR", $e->getMessage()); $renderer->flash ("ERROR", $e->getMessage());
$route->redirect ("/".$this->url_prefix, ""); $route->redirect ("/".
str_replace ("{chain}", $chain, $this->url_prefix),
"");
} }
@@ -644,6 +649,11 @@ echo $content;
$field->mandatory = true; $field->mandatory = true;
if (in_array ("autoincrement", $this->objectDB->fields[$key])) if (in_array ("autoincrement", $this->objectDB->fields[$key]))
$field->type = "hidden"; $field->type = "hidden";
if ($key === $this->chainedForeign)
{
$field->defaults = $chain;
$field->readonly = true;
}
$fields[] = $field; $fields[] = $field;
unset ($field); unset ($field);
} }
@@ -694,7 +704,9 @@ echo $content;
$this->objectDB->insert ($values); $this->objectDB->insert ($values);
$renderer = new renderer (); $renderer = new renderer ();
$renderer->flash ("SUCCESS", _("Creation done")); $renderer->flash ("SUCCESS", _("Creation done"));
$route->redirect ("/".$this->url_prefix, ""); $route->redirect ("/".
str_replace ("{chain}", $chain, $this->url_prefix),
"");
} }
catch (Exception $e) catch (Exception $e)
{ {
@@ -712,7 +724,8 @@ echo $content;
// corrected // corrected
$_SESSION["domframework"]["routeSQL"]["errors"] = $errors; $_SESSION["domframework"]["routeSQL"]["errors"] = $errors;
$_SESSION["domframework"]["routeSQL"]["values"] = $values; $_SESSION["domframework"]["routeSQL"]["values"] = $values;
$route->redirect ("/".$this->url_prefix."/add", ""); $route->redirect ("/".str_replace("{chain}", $chain, $this->url_prefix).
"/add", "");
}); });
@@ -801,6 +814,11 @@ echo $content;
$field->type = "hidden"; $field->type = "hidden";
if ($readonly === true || $this->readwriteAllowed === false) if ($readonly === true || $this->readwriteAllowed === false)
$field->readonly = true; $field->readonly = true;
if ($key === $this->chainedForeign)
{
$field->defaults = $chain;
$field->readonly = true;
}
$fields[] = $field; $fields[] = $field;
unset ($field); unset ($field);
} }
@@ -854,15 +872,24 @@ echo $content;
if ($values[$this->objectDB->primary] !== $id) if ($values[$this->objectDB->primary] !== $id)
throw new Exception (dgettext("domframework", throw new Exception (dgettext("domframework",
"Can not change the primary key"), 403); "Can not change the primary key"), 403);
$errorsChain = array ();
if ($this->chainedForeign !== null &&
$values[$this->chainedForeign] !== $chain)
$errorsChain[$this->chainedForeign] =
array ("error", dgettext("domframework",
"Can not change the external key"));
$errors = $this->objectDB->verify ($values, $id); $errors = $this->objectDB->verify ($values, $id);
if (count ($errors) == 0) if (count ($errors) == 0 && count ($errorsChain) == 0)
{ {
try try
{ {
$this->objectDB->update ($id, $values); $this->objectDB->update ($id, $values);
$renderer = new renderer (); $renderer = new renderer ();
$renderer->flash ("SUCCESS", _("Update done")); $renderer->flash ("SUCCESS", _("Update done"));
$route->redirect ("/".$this->url_prefix, ""); $route->redirect ("/".
str_replace ("{chain}", $chain, $this->url_prefix),
"");
} }
catch (Exception $e) catch (Exception $e)
{ {
@@ -875,12 +902,15 @@ echo $content;
$renderer = new renderer (); $renderer = new renderer ();
foreach ($errors as $error) foreach ($errors as $error)
$renderer->flash (strtoupper ($error[0]), $error[1]); $renderer->flash (strtoupper ($error[0]), $error[1]);
foreach ($errorsChain as $error)
$renderer->flash (strtoupper ($error[0]), $error[1]);
} }
// If errors : save them and redirect to the page of editing to be // If errors : save them and redirect to the page of editing to be
// corrected // corrected
$_SESSION["domframework"]["routeSQL"]["errors"] = $errors; $_SESSION["domframework"]["routeSQL"]["errors"] = $errors;
$_SESSION["domframework"]["routeSQL"]["values"] = $values; $_SESSION["domframework"]["routeSQL"]["values"] = $values;
$route->redirect ("/".$this->url_prefix."/$id", ""); $route->redirect ("/".str_replace ("{chain}", $chain, $this->url_prefix).
"/$id", "");
}); });
} }