From ac483b1ef93c1c03bda74fb39da3bf02d64f6249 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Sun, 22 Mar 2015 11:34:09 +0000 Subject: [PATCH] routeSQL : Add a select list (or a text search) for foreign keys when insert or update git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2049 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- routeSQL.php | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/routeSQL.php b/routeSQL.php index eb15454..e4525b1 100644 --- a/routeSQL.php +++ b/routeSQL.php @@ -995,6 +995,40 @@ class routeSQL throw new Exception (dgettext("domframework","Access forbidden"), 403); $this->connect(); + + // Get the non mandatory foreign keys and display them instead of the + // unusable id + $foreignData = array (); + if (isset($this->objectDB->foreign)) + { + foreach ($this->objectDB->foreign as $foreign=>$params) + { + if (! isset ($params[0])) + throw new Exception ("Undefined foreign key", 500); + if (! isset ($params[1])) + throw new Exception ("Undefined foreign key column", 500); + $class = $params[0]; + $column = $params[1]; + require_once ("models/model_$class.php"); + $foreignObject = new $class ($this->dsn, $this->username, + $this->password, $this->driver_options); + if (isset ($foreignObject->unique)) + { + $tmpData = $foreignObject->read (null, array_merge (array ($column), + $foreignObject->unique)); + $associated = array (); + $unique = reset ($foreignObject->unique); + foreach ($tmpData as $vals) + { + // TODO : If $foreignObject->unique is not an array ? + // or multidimensionnal array + $associated[$vals[$column]] = $vals[$unique]; + } + $foreignData[$foreign] = $associated; + } + } + } + $content = $this->showflash (); $values = array (); $errors = array(); @@ -1054,6 +1088,13 @@ class routeSQL $field->defaults = $chain; $field->readonly = true; } + if (isset ($foreignData[$key])) + { + // Add a select list to the foreign keys : the user can't add a value + // that doesn't exists + $field->type = "select"; + $field->defaults = $foreignData[$key]; + } $fields[] = $field; unset ($field); } @@ -1181,6 +1222,38 @@ class routeSQL $readonly = $this->readonly ($this->authHTML["email"], $id); $this->connect(); + // Get the non mandatory foreign keys and display them instead of the + // unusable id + $foreignData = array (); + if (isset($this->objectDB->foreign)) + { + foreach ($this->objectDB->foreign as $foreign=>$params) + { + if (! isset ($params[0])) + throw new Exception ("Undefined foreign key", 500); + if (! isset ($params[1])) + throw new Exception ("Undefined foreign key column", 500); + $class = $params[0]; + $column = $params[1]; + require_once ("models/model_$class.php"); + $foreignObject = new $class ($this->dsn, $this->username, + $this->password, $this->driver_options); + if (isset ($foreignObject->unique)) + { + $tmpData = $foreignObject->read (null, array_merge (array ($column), + $foreignObject->unique)); + $associated = array (); + $unique = reset ($foreignObject->unique); + foreach ($tmpData as $vals) + { + // TODO : If $foreignObject->unique is not an array ? + // or multidimensionnal array + $associated[$vals[$column]] = $vals[$unique]; + } + $foreignData[$foreign] = $associated; + } + } + } $content = $this->showflash (); $values = array (); $errors = array(); @@ -1247,6 +1320,13 @@ class routeSQL $field->defaults = $chain; $field->readonly = true; } + if (isset ($foreignData[$key])) + { + // Add a select list to the foreign keys : the user can't add a value + // that doesn't exists + $field->type = "select"; + $field->defaults = $foreignData[$key]; + } $fields[] = $field; unset ($field); }