From 8773bb90580bb2bcf7c1eae957cfe22cc0acd40e Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Tue, 10 Mar 2015 14:34:29 +0000 Subject: [PATCH] Add the routeSQL support : permit to create easily the routing and the interfaces about one SQL table, defined in a model file git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1997 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- routeSQL.php | 358 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 358 insertions(+) create mode 100644 routeSQL.php diff --git a/routeSQL.php b/routeSQL.php new file mode 100644 index 0000000..f7115f8 --- /dev/null +++ b/routeSQL.php @@ -0,0 +1,358 @@ + */ + +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; + /** 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; + } + + /** 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 .= "
\n"; + $dataflash .= "$alert ".$flash[1]."\n"; + $dataflash .= "
\n"; + } + + unset ($_SESSION["renderer"]["flash"]); + } + } + return $dataflash; + } + + 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, function () use ($route) + { + // LIST ALL THE OBJECTS OF THE TABLE + $this->connect(); + $csrf = new csrf (); + $token = $csrf->createToken (); + $datas = $this->objectDB->read (); + $titles = $this->objectDB->titles (); + $content = $this->showflash (); + if ($this->displayActions) + $content .= "". + dgettext("domframework","Add new entry")."\n"; + $content .= "\n"; + $content .= " \n"; + if ($this->displayActions) + $content .= " \n"; + foreach ($titles as $title) + $content .= " \n"; + $content .= " \n"; + $content .= " \n"; + if (count ($datas) === 0) + { + $content .= " \n"; + } + else + { + foreach ($datas as $line) + { + $content .= " "; + if ($this->displayActions) + { + $content .= ""; + } + foreach ($line as $col) + $content .= ""; + $content .= "\n"; + } + } + $content .= " \n"; + $content .= "
".dgettext("domframework","Actions")."".htmlentities ($title)."
"; + $content .= dgettext("domframework","No entry available"); + $content .= "
"; + $content .= " ". + dgettext("domframework","Edit").""; + $content .= " deleteConfirm) + $content .= " onclick=\"return confirm('". + dgettext("domframework", + "Are you sure to delete this entry?")."')\""; + $content .= ">". + dgettext("domframework","Delete").""; + $content .= "".htmlentities ($col)."
\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", ""); + }); + +echo "Route not found"; + } +}