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
This commit is contained in:
2015-03-22 11:34:09 +00:00
parent 263741d316
commit ac483b1ef9

View File

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