1
0
mirror of https://github.com/vrana/adminer.git synced 2025-08-30 09:39:51 +02:00

Refactor and fix the plugin AdminerEditForeign

Thanks to Amunak (https://github.com/adminerevo/adminerevo/pull/86).
This commit is contained in:
Peter Knut
2024-08-25 23:43:21 +02:00
parent 2ce88d9bdc
commit 9b8d14c3ee

View File

@@ -6,38 +6,70 @@
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0 * @license https://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @license https://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 (one or other) * @license https://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 (one or other)
*/ */
class AdminerEditForeign { class AdminerEditForeign
var $_limit; {
private $limit;
private $foreignTables = [];
private $foreignOptions = [];
function __construct($limit = 0) { /**
$this->_limit = $limit; * @param int $limit
*/
function __construct($limit = 0)
{
$this->limit = $limit;
} }
function editInput($table, $field, $attrs, $value) { /**
static $foreignTables = array(); * @param string $table
static $values = array(); * @param array $field
$foreignKeys = &$foreignTables[$table]; * @param string $attrs
if ($foreignKeys === null) { * @param string|null $value
$foreignKeys = column_foreign_keys($table); *
* @return string
*/
function editInput($table, array $field, $attrs, $value)
{
if (!isset($this->foreignTables[$table])) {
$this->foreignTables[$table] = column_foreign_keys($table);
} }
foreach ((array) $foreignKeys[$field["field"]] as $foreignKey) { $foreignKeys = $this->foreignTables[$table];
if (count($foreignKey["source"]) == 1) {
if (empty($foreignKeys[$field["field"]])) {
return "";
}
foreach ($foreignKeys[$field["field"]] as $foreignKey) {
if (count($foreignKey["source"]) != 1) {
continue;
}
$target = $foreignKey["table"]; $target = $foreignKey["table"];
$id = $foreignKey["target"][0]; $id = $foreignKey["target"][0];
$options = &$values[$target][$id];
if (!$options) { if (!isset($this->foreignOptions[$target][$id])) {
$column = idf_escape($id); $column = idf_escape($id);
if (preg_match('~binary~', $field["type"])) { if (preg_match('~binary~', $field["type"])) {
$column = "HEX($column)"; $column = "HEX($column)";
} }
$options = array("" => "") + get_vals("SELECT $column FROM " . table($target) . " ORDER BY 1" . ($this->_limit ? " LIMIT " . ($this->_limit + 1) : ""));
if ($this->_limit && count($options) - 1 > $this->_limit) { $values = get_vals("SELECT $column FROM " . table($target) . " ORDER BY 1" .
return; ($this->limit ? " LIMIT " . ($this->limit + 1) : ""));
}
} if ($this->limit && count($values) > $this->limit) {
return "<select$attrs>" . optionlist($options, $value) . "</select>"; $this->foreignOptions[$target][$id] = false;
} } else {
$this->foreignOptions[$target][$id] = ["" => ""] + $values;
} }
} }
if ($options = $this->foreignOptions[$target][$id]) {
return "<select$attrs>" . optionlist($options, $value) . "</select>";
} else {
return "";
}
}
return "";
}
} }