From 9b8d14c3eef59de5152f511a0c778df5fec16a2b Mon Sep 17 00:00:00 2001 From: Peter Knut Date: Sun, 25 Aug 2024 23:43:21 +0200 Subject: [PATCH] Refactor and fix the plugin AdminerEditForeign Thanks to Amunak (https://github.com/adminerevo/adminerevo/pull/86). --- plugins/edit-foreign.php | 96 ++++++++++++++++++++++++++-------------- 1 file changed, 64 insertions(+), 32 deletions(-) diff --git a/plugins/edit-foreign.php b/plugins/edit-foreign.php index ea205306..eebcb497 100644 --- a/plugins/edit-foreign.php +++ b/plugins/edit-foreign.php @@ -1,43 +1,75 @@ _limit = $limit; + * @link https://www.adminer.org/plugins/#use + * @author Jakub Vrana, https://www.vrana.cz/ + * @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) + */ +class AdminerEditForeign +{ + private $limit; + private $foreignTables = []; + private $foreignOptions = []; + + /** + * @param int $limit + */ + function __construct($limit = 0) + { + $this->limit = $limit; } - - function editInput($table, $field, $attrs, $value) { - static $foreignTables = array(); - static $values = array(); - $foreignKeys = &$foreignTables[$table]; - if ($foreignKeys === null) { - $foreignKeys = column_foreign_keys($table); + + /** + * @param string $table + * @param array $field + * @param string $attrs + * @param string|null $value + * + * @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) { - if (count($foreignKey["source"]) == 1) { - $target = $foreignKey["table"]; - $id = $foreignKey["target"][0]; - $options = &$values[$target][$id]; - if (!$options) { - $column = idf_escape($id); - if (preg_match('~binary~', $field["type"])) { - $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) { - return; - } + $foreignKeys = $this->foreignTables[$table]; + + if (empty($foreignKeys[$field["field"]])) { + return ""; + } + + foreach ($foreignKeys[$field["field"]] as $foreignKey) { + if (count($foreignKey["source"]) != 1) { + continue; + } + + $target = $foreignKey["table"]; + $id = $foreignKey["target"][0]; + + if (!isset($this->foreignOptions[$target][$id])) { + $column = idf_escape($id); + if (preg_match('~binary~', $field["type"])) { + $column = "HEX($column)"; } + + $values = get_vals("SELECT $column FROM " . table($target) . " ORDER BY 1" . + ($this->limit ? " LIMIT " . ($this->limit + 1) : "")); + + if ($this->limit && count($values) > $this->limit) { + $this->foreignOptions[$target][$id] = false; + } else { + $this->foreignOptions[$target][$id] = ["" => ""] + $values; + } + } + + if ($options = $this->foreignOptions[$target][$id]) { return "" . optionlist($options, $value) . ""; + } else { + return ""; } } + + return ""; } - }