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); }