git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2015 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
656 lines
24 KiB
PHP
656 lines
24 KiB
PHP
<?php
|
|
/** DomFramework
|
|
@package domframework
|
|
@author Dominique Fournier <dominique@fournier38.fr> */
|
|
|
|
require_once ("domframework/route.php");
|
|
require_once ("domframework/form.php");
|
|
require_once ("domframework/renderer.php");
|
|
|
|
error_reporting (E_ALL);
|
|
|
|
/** Automatic Routing for SQL database
|
|
Allow to do CRUD on datas with only one line in index.php */
|
|
class routeSQL
|
|
{
|
|
/** Display the Actions column in list of entries */
|
|
public $displayActions = true;
|
|
/** Do a confirmation in javascript before deleting entry */
|
|
public $deleteConfirm = true;
|
|
/** Push the actions buttons at end of line */
|
|
public $actionsAtEnd = false;
|
|
/** The Text to Delete */
|
|
public $textDelete = "";
|
|
/** The Text to Edit */
|
|
public $textEdit = "";
|
|
/** enable internal CSS */
|
|
public $enableInternalCSS = true;
|
|
/** Definition of the position in top bar at left
|
|
Allowed : addNew numberEntryByDisplay search informations paginator */
|
|
public $topBarLeft = array ("addNew", "numberEntryByDisplay");
|
|
/** Definition of the position in top bar at right
|
|
Allowed : addNew numberEntryByDisplay search informations paginator */
|
|
public $topBarRight = array ("search");
|
|
/** Definition of the position in bottom bar at left
|
|
Allowed : addNew numberEntryByDisplay search informations paginator */
|
|
public $bottomBarLeft = array ("informations");
|
|
/** Definition of the position in bottom bar at right
|
|
Allowed : addNew numberEntryByDisplay search informations paginator */
|
|
public $bottomBarRight = array ("paginator");
|
|
/** The cookie path used to determine the old parameters
|
|
It is automatically generated with the URL */
|
|
public $path = "";
|
|
/** The model file containing the database description */
|
|
private $model_file = "";
|
|
/** The model class included in the model file */
|
|
private $model_class = "";
|
|
/** The prefix to be used in the URL. Should be the end of $model_file
|
|
Ex : if $model_file = models/model_zone.php, the url_prefix should be
|
|
zone */
|
|
private $url_prefix = "";
|
|
/** The SQL object created */
|
|
private $objectDB = null;
|
|
/** The DSN to connect to the database */
|
|
private $dsn = null;
|
|
/** The Username to connect to the database */
|
|
private $username = null;
|
|
/** The Password to connect to the database */
|
|
private $password = null;
|
|
/** The Options to the PDO driver if needed */
|
|
private $driver_options = null;
|
|
|
|
/** Connect to the database */
|
|
public function __construct ($model_file, $model_class, $url_prefix, $dsn,
|
|
$username = null, $password = null, $driver_options = null)
|
|
{
|
|
$this->model_file = $model_file;
|
|
$this->model_class = $model_class;
|
|
$this->url_prefix = $url_prefix;
|
|
$this->dsn = $dsn;
|
|
$this->username = $username;
|
|
$this->password = $password;
|
|
$this->driver_options = $driver_options;
|
|
$this->textDelete = "❌";
|
|
$this->textEdit = "✎";
|
|
$pos = strpos ($_SERVER["REQUEST_URI"], "?");
|
|
if ($pos === false)
|
|
$this->path = $_SERVER["REQUEST_URI"];
|
|
else
|
|
$this->path = substr ($_SERVER["REQUEST_URI"], 0, $pos);
|
|
}
|
|
|
|
/** Connect to the database */
|
|
private function connect ()
|
|
{
|
|
include "models/$this->model_file";
|
|
$this->objectDB = new $this->model_class ($this->dsn, $this->username,
|
|
$this->password, $this->driver_options);
|
|
}
|
|
|
|
/** Display the flash informations if no flash view is available */
|
|
private function showflash ()
|
|
{
|
|
$dataflash = "";
|
|
if (file_exists ("views/flash.php"))
|
|
require ("views/flash.php");
|
|
else
|
|
{
|
|
if (isset ($_SESSION["renderer"]["flash"]))
|
|
{
|
|
foreach ($_SESSION["renderer"]["flash"] as $flash)
|
|
{
|
|
$dataflash .= "<div class='alert ";
|
|
switch ($flash[0])
|
|
{
|
|
case 4: $dataflash .= "alert-danger";$alert = _("Error!");break;
|
|
case 3: $dataflash .= "alert-warning";$alert = _("Warning!");break;
|
|
case 2: $dataflash .= "alert-info";$alert = _("Info :");break;
|
|
case 1: $dataflash .= "alert-success";$alert = _("Success : ");break;
|
|
}
|
|
$dataflash .= " alert-dismissable'>\n";
|
|
$dataflash .= "<strong>$alert</strong> ".$flash[1]."\n";
|
|
$dataflash .= "</div>\n";
|
|
}
|
|
|
|
unset ($_SESSION["renderer"]["flash"]);
|
|
}
|
|
}
|
|
return $dataflash;
|
|
}
|
|
|
|
/** Display a paginator
|
|
$nbentries is the total number of elements
|
|
num is the number of elements displayed by page
|
|
page is the page to display */
|
|
private function paginatorArea ($nbentries, $page, $num, $search)
|
|
{
|
|
// The maximum of links available in the paginator
|
|
$maxClickPaginator = 10;
|
|
$prePage = false;
|
|
$postPage = false;
|
|
$content = " <div class='paginatorArea'>\n";
|
|
$displayedNumbers = 0;
|
|
for ($i = 1 ; $i < (1+$nbentries/($num)) ; $i++)
|
|
{
|
|
if ($i < ($page - $maxClickPaginator/2))
|
|
{
|
|
if ($prePage === false)
|
|
$content .= " ...\n";
|
|
$prePage = true;
|
|
continue;
|
|
}
|
|
if ($displayedNumbers >= $maxClickPaginator)
|
|
{
|
|
if ($postPage === false)
|
|
$content .= " ...\n";
|
|
$postPage = true;
|
|
continue;
|
|
}
|
|
$displayedNumbers++;
|
|
$content .= " <a href='".$this->url_prefix.
|
|
"?page=$i&num=$num&search=".urlencode ($search)."'";
|
|
if ($page == $i)
|
|
$content .= " class='selected'";
|
|
$content .= ">$i</a>\n";
|
|
}
|
|
if ($displayedNumbers === 0)
|
|
{
|
|
$i = 1;
|
|
$content .= " <a href='".$this->url_prefix.
|
|
"?page=$i&num=$num&search=".urlencode($search)."'";
|
|
if ($page == $i)
|
|
$content .= " class='selected'";
|
|
$content .= ">$i</a>\n";
|
|
|
|
}
|
|
$content .= " </div>\n";
|
|
return $content;
|
|
}
|
|
|
|
/** Display the actions buttons outside of the table (actually, juste the
|
|
'Add new entry' button */
|
|
private function addNewArea ($nbentries, $page, $num, $search)
|
|
{
|
|
$content = "";
|
|
if ($this->displayActions)
|
|
{
|
|
$route = new route ();
|
|
$content .= " <div class='actionExtern'>\n";
|
|
$content .= " <a href='".$route->baseURL().$this->url_prefix."/add'>"
|
|
.dgettext("domframework","Add new entry")."</a>\n";
|
|
$content .= " </div>\n";
|
|
}
|
|
return $content;
|
|
}
|
|
|
|
/** Display the select list to choose the number of displayed entries */
|
|
private function numberEntryByDisplayArea ($nbentries, $page, $num, $search)
|
|
{
|
|
$route = new route ();
|
|
$content = "";
|
|
$content .= " <div class='numberEntryByDisplayArea'>\n";
|
|
$content .= " <form method='get' action='".$route->baseURL().
|
|
$this->url_prefix."'>\n";
|
|
$content .= " <select name='num' onchange='this.form.submit()' >\n";
|
|
$list = array (10,20,50,100,200,500,1000);
|
|
foreach ($list as $element)
|
|
{
|
|
$content .= " <option ";
|
|
if ($element == $num)
|
|
$content .= "selected='selected'";
|
|
$content .= ">$element</option>\n";
|
|
}
|
|
$content .= " </select>\n";
|
|
$content .= " ".dgettext("domframework"," elements")."\n";
|
|
$content .= " </form>\n";
|
|
$content .= " </div>\n";
|
|
return $content;
|
|
}
|
|
|
|
/** Display the search area */
|
|
public function searchArea ($nbentries, $page, $num, $search)
|
|
{
|
|
$route = new route ();
|
|
$content = "";
|
|
$content .= " <div class='searchArea'>\n";
|
|
$content .= " <form method='get' action='".$route->baseURL().
|
|
$this->url_prefix."'>\n";
|
|
$content .= " ".dgettext("domframework"," Search: ");
|
|
$content .= " <input type='text' name='search' value='".
|
|
htmlentities ($search, ENT_QUOTES)."'/>\n";
|
|
$content .= " </form>\n";
|
|
$content .= " </div>\n";
|
|
return $content;
|
|
}
|
|
|
|
/** Display the informations */
|
|
private function informationsArea ($nbentries, $page, $num, $search)
|
|
{
|
|
$content = "";
|
|
$content .= " <div class='informationsArea'>\n";
|
|
$message = dgettext("domframework",
|
|
"Display the element {FIRST} to {LAST} on {COUNT} elements");
|
|
if ($nbentries === 0)
|
|
$message = str_replace ("{FIRST}", 0, $message);
|
|
else
|
|
$message = str_replace ("{FIRST}", 1+($num*($page-1)), $message);
|
|
if ($nbentries < ($num*$page))
|
|
$message = str_replace ("{LAST}", $nbentries, $message);
|
|
else
|
|
$message = str_replace ("{LAST}", ($num*$page), $message);
|
|
$message = str_replace ("{COUNT}", $nbentries, $message);
|
|
$content .= $message;
|
|
$content .= " </div>\n";
|
|
return $content;
|
|
}
|
|
|
|
/** Create the routes and the associated actions */
|
|
public function routes ()
|
|
{
|
|
/** Add HTML routes */
|
|
$route = new route ();
|
|
$route->get ($this->url_prefix."/", function () use ($route)
|
|
{
|
|
$route->redirect ("/".$this->url_prefix, "");
|
|
});
|
|
|
|
$route->get ($this->url_prefix."(\?({p1}=({v1})?)(&{p2}=({v2})?(&{p3}=({v3})?)?)?)?",
|
|
function ($p1, $v1, $p2, $v2, $p3, $v3) use ($route)
|
|
{
|
|
// LIST ALL THE OBJECTS OF THE TABLE
|
|
// num is the number of elements displayed by page
|
|
// page is the page to display
|
|
// Allow the parameters to be sent in any order
|
|
if (isset ($_COOKIE["num"])) $num = $_COOKIE["num"];
|
|
if (isset ($_COOKIE["page"])) $page = $_COOKIE["page"];
|
|
if (isset ($_COOKIE["search"])) $search = $_COOKIE["search"];
|
|
if ($p1 === "num") $num = $v1;
|
|
if ($p2 === "num") $num = $v2;
|
|
if ($p3 === "num") $num = $v3;
|
|
if ($p1 === "page") $page = $v1;
|
|
if ($p2 === "page") $page = $v2;
|
|
if ($p3 === "page") $page = $v3;
|
|
if ($p1 === "search") $search = $v1;
|
|
if ($p2 === "search") $search = $v2;
|
|
if ($p3 === "search") $search = $v3;
|
|
if (!isset ($num) || $num === null || $num === "") $num = 10;
|
|
if (!isset ($page) || $page === null || $page === "") $page = 1;
|
|
if (!isset ($search) || $search === null || $search === "") $search = "";
|
|
$page = intval ($page);
|
|
$num = intval ($num);
|
|
$search = rawurldecode ($search);
|
|
setcookie ("page", $page, time()+3600*24*30, $this->path);
|
|
setcookie ("num", $num, time()+3600*24*30, $this->path);
|
|
setcookie ("search", $search, time()+3600*24*30, $this->path);
|
|
//echo "PAGE=$page\n";
|
|
//echo "NUM=$num\n";
|
|
//echo "SEARCH=$search\n";
|
|
//$route->debug=1;
|
|
$this->connect();
|
|
$csrf = new csrf ();
|
|
$token = $csrf->createToken ();
|
|
$titles = $this->objectDB->titles ();
|
|
if ($search === "")
|
|
$datas = $this->objectDB->read ();
|
|
else
|
|
{
|
|
$criteria = array ();
|
|
foreach (array_keys($titles) as $column)
|
|
$criteria[] = array ($column, "%$search%", "LIKE");
|
|
$datas = $this->objectDB->read ($criteria, null, null, true);
|
|
}
|
|
$nbentries = count ($datas);
|
|
if ($num > 1000)
|
|
$route->redirect ("/".$this->url_prefix.
|
|
"?page=$page&num=1000&search=$search", "");
|
|
if ($page < 1)
|
|
$route->redirect ("/".$this->url_prefix.
|
|
"?page=1&num=$num&search=$search", "");
|
|
// Push on the last page if the values are too high
|
|
if ($nbentries > 0 && ($page-1)*$num > $nbentries)
|
|
{
|
|
$maxPage = intval ($nbentries/$num)+1;
|
|
$route->redirect ("/".$this->url_prefix.
|
|
"?page=$maxPage&num=$num&search=$search", "");
|
|
}
|
|
|
|
|
|
$content = "";
|
|
// Internal CSS
|
|
if ($this->enableInternalCSS === true)
|
|
{
|
|
$content .= "<style type='text/css' scoped='scoped'>\n";
|
|
$content .= ".routeSQL { width:95%; margin-left:auto; margin-right:auto; }\n";
|
|
$content .= ".routeSQL a { text-decoration:none; }\n";
|
|
$content .= ".routeSQL .topBar { display: block; overflow: auto; }\n";
|
|
$content .= ".routeSQL .topBar .topleft { display: inline; float:left; }\n";
|
|
$content .= ".routeSQL .topBar .topright { display: inline; float:right; }\n";
|
|
$content .= ".routeSQL .bottomBar { display: block; overflow: auto; }\n";
|
|
$content .= ".routeSQL .bottomBar .bottomleft { display: inline; float:left; }\n";
|
|
$content .= ".routeSQL .bottomBar .bottomright { display: inline; float:right; }\n";
|
|
$content .= ".routeSQL .actionExtern { border:1px solid #ddd; border-radius:5px; padding:10px; margin:3px; float:left; }\n";
|
|
$content .= ".routeSQL .numberEntryByDisplayArea { border:1px solid #ddd; border-radius:5px; padding:10px; margin:3px; float:left; }\n";
|
|
$content .= ".routeSQL .searchArea { border:1px solid #ddd; border-radius:5px; padding:10px; margin:3px; float:left; }\n";
|
|
$content .= ".routeSQL .searchArea form { margin:-1px; }\n";
|
|
$content .= ".routeSQL .numberEntryByDisplayArea form { margin:-3px; }\n";
|
|
$content .= ".routeSQL .informationsArea { border:1px solid #ddd; border-radius:5px; padding:10px; margin:3px; float:left; }\n";
|
|
$content .= ".routeSQL .paginatorArea { border:1px solid #ddd; border-radius:5px; padding:10px; margin:3px; float:left; }\n";
|
|
$content .= ".routeSQL .paginatorArea a { border:1px solid grey; border-radius:5px; padding:3px; }\n";
|
|
$content .= ".routeSQL .paginatorArea .selected { background-color:#04d; color:white;font-weight:bold; }\n";
|
|
$content .= ".routeSQL table { width:100%;overflow:auto; border-collapse:collapse; }\n";
|
|
$content .= ".routeSQL table tr { border-top:1px solid #ccc;}\n";
|
|
$content .= ".routeSQL table th { border-bottom:3px solid #ccc; border-top:1px solid #fff; padding:9px 5px 9px 1px; }\n";
|
|
$content .= ".routeSQL table td { empty-cells:true; padding:9px 5px 9px 1px; }\n";
|
|
$content .= ".routeSQL table .noentry { text-align:center; color:#c00; font-weight:bolder; }\n";
|
|
$content .= ".routeSQL table .action { text-align:center; }\n";
|
|
$content .= ".routeSQL table .action .edit { color:#222; font-weight:bolder;}\n";
|
|
$content .= ".routeSQL table .action .delete { color:#c00; font-weight:bolder; }\n";
|
|
$content .= ".routeSQL table .odd { background-color:#f9f9f9; }\n";
|
|
$content .= "</style>\n";
|
|
}
|
|
$content .= "<div class='routeSQL'>\n";
|
|
$content .= $this->showflash ();
|
|
$content .= " <div class='topBar'>\n";
|
|
$content .= " <div class='topleft'>\n";
|
|
foreach ($this->topBarLeft as $area)
|
|
{
|
|
$areaName = $area."Area";
|
|
$content .= $this->$areaName ($nbentries, $page, $num, $search);
|
|
}
|
|
$content .= " </div>\n";
|
|
$content .= " <div class='topright'>\n";
|
|
foreach ($this->topBarRight as $area)
|
|
{
|
|
$areaName = $area."Area";
|
|
$content .= $this->$areaName ($nbentries, $page, $num, $search);
|
|
}
|
|
$content .= " </div>\n";
|
|
$content .= " </div>\n"; // End of topBar
|
|
$content .= " <table>\n";
|
|
$content .= " <thead>\n";
|
|
$content .= " <tr>\n";
|
|
if ($this->displayActions && $this->actionsAtEnd === false)
|
|
$content .= " <th>".dgettext("domframework","Actions")."</th>\n";
|
|
foreach ($titles as $title)
|
|
$content .= " <th>".htmlentities ($title)."</th>\n";
|
|
if ($this->displayActions && $this->actionsAtEnd !== false)
|
|
$content .= " <th>".dgettext("domframework","Actions")."</th>\n";
|
|
$content .= " </tr>\n";
|
|
$content .= " </thead>\n";
|
|
$content .= " <tbody>\n";
|
|
if ($nbentries === 0)
|
|
{
|
|
// Add one column more for actions
|
|
$countTitles = count($titles);
|
|
if ($this->displayActions)
|
|
$countTitles++;
|
|
$content .= " <tr><td colspan='$countTitles' class='noentry'>";
|
|
$content .= dgettext("domframework","No entry available");
|
|
$content .= "</td></tr>\n";
|
|
}
|
|
else
|
|
{
|
|
$i = 1;
|
|
$odd = "odd";
|
|
foreach ($datas as $line)
|
|
{
|
|
if ($i <= (($page-1)*$num) || $i > (($page-1)*$num + $num))
|
|
{
|
|
$i++;
|
|
continue;
|
|
}
|
|
$content .= " <tr class='$odd'>";
|
|
if ($odd === "odd") $odd = "even";
|
|
else $odd = "odd";
|
|
if ($this->actionsAtEnd !== false)
|
|
{
|
|
foreach ($line as $col)
|
|
$content .= "<td>".htmlentities ($col)."</td>";
|
|
}
|
|
if ($this->displayActions)
|
|
{
|
|
$content .= "<td class='action'>";
|
|
$content .= " <a href='".$route->baseURL().$this->url_prefix."/".
|
|
$line[$this->objectDB->primary]."' class='edit'>".
|
|
$this->textEdit."</a>";
|
|
$content .= " <a href='".$route->baseURL().$this->url_prefix."/".
|
|
$line[$this->objectDB->primary]."/delete/$token'";
|
|
if ($this->deleteConfirm)
|
|
$content .= " onclick=\"return confirm('".
|
|
dgettext("domframework",
|
|
"Are you sure to delete this entry?")."')\"";
|
|
$content .= " class='delete'>".$this->textDelete."</a>";
|
|
$content .= "</td>";
|
|
}
|
|
if ($this->actionsAtEnd === false)
|
|
{
|
|
foreach ($line as $col)
|
|
$content .= "<td>".htmlentities ($col)."</td>";
|
|
}
|
|
$content .= "</tr>\n";
|
|
$i++;
|
|
}
|
|
}
|
|
$content .= " </tbody>\n";
|
|
$content .= " </table>\n";
|
|
$content .= " <div class='bottomBar'>\n";
|
|
$content .= " <div class='bottomleft'>\n";
|
|
foreach ($this->bottomBarLeft as $area)
|
|
{
|
|
$areaName = $area."Area";
|
|
$content .= $this->$areaName ($nbentries, $page, $num, $search);
|
|
}
|
|
$content .= " </div>\n";
|
|
$content .= " <div class='bottomright'>\n";
|
|
foreach ($this->bottomBarRight as $area)
|
|
{
|
|
$areaName = $area."Area";
|
|
$content .= $this->$areaName ($nbentries, $page, $num, $search);
|
|
}
|
|
$content .= " </div>\n";
|
|
$content .= " </div>\n"; // End of bottomBar
|
|
$content .= "</div>\n";
|
|
echo $content;
|
|
});
|
|
|
|
$route->get ($this->url_prefix."/{id}/delete/{token}",
|
|
function ($id, $token)
|
|
{
|
|
echo "DELETE AN EXISTING OBJECT IF THE TOKEN IS VALID !";
|
|
$this->connect();
|
|
$csrf = new csrf ();
|
|
$renderer = new renderer ();
|
|
$route = new route ();
|
|
try
|
|
{
|
|
$csrf->checkToken ($token);
|
|
$this->objectDB->delete ($id);
|
|
$route->redirect ("/".$this->url_prefix, "");
|
|
}
|
|
catch (Exception $e)
|
|
{
|
|
$renderer->flash ("ERROR", $e->getMessage());
|
|
$route->redirect ("/".$this->url_prefix, "");
|
|
}
|
|
|
|
|
|
});
|
|
|
|
$route->get ($this->url_prefix."/add", function ()
|
|
{
|
|
// Add a new entry : form to be filled by the user
|
|
$this->connect();
|
|
$content = $this->showflash ();
|
|
$values = array ();
|
|
$errors = array();
|
|
$titles = $this->objectDB->titles ();
|
|
if (isset ($_SESSION["domframework"]["routeSQL"]["errors"]))
|
|
{
|
|
$errors = $_SESSION["domframework"]["routeSQL"]["errors"];
|
|
unset ($_SESSION["domframework"]["routeSQL"]["errors"]);
|
|
}
|
|
if (isset ($_SESSION["domframework"]["routeSQL"]["values"]))
|
|
{
|
|
$values = $_SESSION["domframework"]["routeSQL"]["values"];
|
|
unset ($_SESSION["domframework"]["routeSQL"]["values"]);
|
|
}
|
|
|
|
$f = new form ();
|
|
$fields = array ();
|
|
foreach ($titles as $key=>$val)
|
|
{
|
|
$field = new formfield ($key, $val);
|
|
if (! isset ($this->objectDB->fields[$key]))
|
|
throw new Exception (sprintf (dgettext("domframework",
|
|
"Field '%s' (defined in titles) not found in fields"),
|
|
$key), 500);
|
|
if (in_array ("not null", $this->objectDB->fields[$key]))
|
|
$field->mandatory = true;
|
|
if (in_array ("autoincrement", $this->objectDB->fields[$key]))
|
|
$field->type = "hidden";
|
|
$fields[] = $field;
|
|
unset ($field);
|
|
}
|
|
|
|
$field = new formfield ("submit", _("Save the zone"));
|
|
$field->defaults = _("Save the zone");
|
|
$field->type = "submit";
|
|
$fields[] = $field;
|
|
unset ($field);
|
|
$f->fields ($fields);
|
|
$content .= $f->printHTML ("post", $values, $errors);
|
|
echo $content;
|
|
});
|
|
|
|
$route->post ($this->url_prefix."/add", function () use ($route)
|
|
{
|
|
// Add a new entry : effective save of the datas
|
|
$this->connect();
|
|
$f = new form ();
|
|
$values = $f->values ();
|
|
$errors = $this->objectDB->verify ($values);
|
|
if (count ($errors) == 0)
|
|
{
|
|
try
|
|
{
|
|
$this->objectDB->insert ($values);
|
|
$renderer = new renderer ();
|
|
$renderer->flash ("SUCCESS", _("Creation done"));
|
|
$route->redirect ("/".$this->url_prefix, "");
|
|
}
|
|
catch (Exception $e)
|
|
{
|
|
$renderer = new renderer ();
|
|
$renderer->flash ("ERROR", $e->getMessage ());
|
|
}
|
|
}
|
|
else
|
|
{
|
|
$renderer = new renderer ();
|
|
foreach ($errors as $error)
|
|
$renderer->flash (strtoupper ($error[0]), $error[1]);
|
|
}
|
|
// If errors : save them and redirect to the page of editing to be
|
|
// corrected
|
|
$_SESSION["domframework"]["routeSQL"]["errors"] = $errors;
|
|
$_SESSION["domframework"]["routeSQL"]["values"] = $values;
|
|
$route->redirect ("/".$this->url_prefix."/add", "");
|
|
|
|
});
|
|
|
|
$route->get ($this->url_prefix."/{id}", function ($id)
|
|
{
|
|
// LIST THE DETAILS OF ONE EXISTING OBJECT !
|
|
$this->connect();
|
|
$content = $this->showflash ();
|
|
$values = array ();
|
|
$errors = array();
|
|
$titles = $this->objectDB->titles ();
|
|
$values = $this->objectDB->read (array (array ($this->objectDB->primary,
|
|
$id)));
|
|
if (count ($values) === 0)
|
|
throw new Exception (dgettext("domframework", "Object not found"), 404);
|
|
$values = $values[0];
|
|
if (isset ($_SESSION["domframework"]["routeSQL"]["errors"]))
|
|
{
|
|
$errors = $_SESSION["domframework"]["routeSQL"]["errors"];
|
|
unset ($_SESSION["domframework"]["routeSQL"]["errors"]);
|
|
}
|
|
if (isset ($_SESSION["domframework"]["routeSQL"]["values"]))
|
|
{
|
|
$values = $_SESSION["domframework"]["routeSQL"]["values"];
|
|
unset ($_SESSION["domframework"]["routeSQL"]["values"]);
|
|
}
|
|
|
|
$f = new form ();
|
|
$fields = array ();
|
|
foreach ($titles as $key=>$val)
|
|
{
|
|
$field = new formfield ($key, $val);
|
|
if (! isset ($this->objectDB->fields[$key]))
|
|
throw new Exception (sprintf (dgettext("domframework",
|
|
"Field '%s' (defined in titles) not found in fields"),
|
|
$key), 500);
|
|
if (in_array ("not null", $this->objectDB->fields[$key]))
|
|
$field->mandatory = true;
|
|
if (in_array ("autoincrement", $this->objectDB->fields[$key]))
|
|
$field->type = "hidden";
|
|
$fields[] = $field;
|
|
unset ($field);
|
|
}
|
|
|
|
$field = new formfield ("submit", _("Save the zone"));
|
|
$field->defaults = _("Save the zone");
|
|
$field->type = "submit";
|
|
$fields[] = $field;
|
|
unset ($field);
|
|
$f->fields ($fields);
|
|
$content .= $f->printHTML ("post", $values, $errors);
|
|
echo $content;
|
|
});
|
|
|
|
$route->post ($this->url_prefix."/{id}", function ($id) use ($route)
|
|
{
|
|
// SAVE THE DETAILS OF ONE EXISTING OBJECT !
|
|
$this->connect();
|
|
$oldvalues = $this->objectDB->read (array (array
|
|
($this->objectDB->primary, $id)));
|
|
if (count ($oldvalues) === 0)
|
|
throw new Exception (dgettext("domframework", "Object not found"), 404);
|
|
$oldvalues = $oldvalues[0];
|
|
$f = new form ();
|
|
$values = $f->values ();
|
|
if ($values[$this->objectDB->primary] !== $id)
|
|
throw new Exception (dgettext("domframework",
|
|
"Can not change the primary key"), 403);
|
|
$errors = $this->objectDB->verify ($values, $id);
|
|
if (count ($errors) == 0)
|
|
{
|
|
try
|
|
{
|
|
$this->objectDB->update ($id, $values);
|
|
$renderer = new renderer ();
|
|
$renderer->flash ("SUCCESS", _("Update done"));
|
|
$route->redirect ("/".$this->url_prefix, "");
|
|
}
|
|
catch (Exception $e)
|
|
{
|
|
$renderer = new renderer ();
|
|
$renderer->flash ("ERROR", $e->getMessage ());
|
|
}
|
|
}
|
|
else
|
|
{
|
|
$renderer = new renderer ();
|
|
foreach ($errors as $error)
|
|
$renderer->flash (strtoupper ($error[0]), $error[1]);
|
|
}
|
|
// If errors : save them and redirect to the page of editing to be
|
|
// corrected
|
|
$_SESSION["domframework"]["routeSQL"]["errors"] = $errors;
|
|
$_SESSION["domframework"]["routeSQL"]["values"] = $values;
|
|
$route->redirect ("/".$this->url_prefix."/$id", "");
|
|
});
|
|
}
|
|
}
|