]> git.joonet.de Git - adminer.git/commitdiff
Edit foreign plugin
authorJakub Vrana <jakub@vrana.cz>
Thu, 23 Jun 2011 20:08:08 +0000 (22:08 +0200)
committerJakub Vrana <jakub@vrana.cz>
Thu, 23 Jun 2011 20:11:08 +0000 (22:11 +0200)
adminer/plugin.php
editor/include/adminer.inc.php
plugins/edit-foreign.php [new file with mode: 0644]

index 5d60a8545dfd10d615595c6db199f2396ebc9456..9df8f32a2476da487af597a4da6ec6c7f49274b8 100644 (file)
@@ -20,6 +20,7 @@ function adminer_object() {
                new AdminerForeignSystem,
                new AdminerEnumOption,
                new AdminerTablesFilter,
+               new AdminerEditForeign,
        );
        
        /* It is possible to combine customization and plugins:
index 58821b0a2b6984545c403b19e4212a8e2c99f964..4760896ed6d88a7222c10a4585e733da6cbdc5d3 100644 (file)
@@ -575,14 +575,14 @@ ORDER BY ORDINAL_POSITION", null, "") as $row) { //! requires MySQL 5
        
        function _foreignKeyOptions($table, $column, $value = null) {
                global $connection;
-               if (list($table, $id, $name) = $this->_foreignColumn(column_foreign_keys($table), $column)) {
-                       $return = &$this->_values[$table];
+               if (list($target, $id, $name) = $this->_foreignColumn(column_foreign_keys($table), $column)) {
+                       $return = &$this->_values[$target];
                        if (!isset($return)) {
-                               $table_status = table_status($table);
-                               $return = ($table_status["Rows"] > 1000 ? "" : array("" => "") + get_key_vals("SELECT $id, $name FROM " . table($table) . " ORDER BY 2"));
+                               $table_status = table_status($target);
+                               $return = ($table_status["Rows"] > 1000 ? "" : array("" => "") + get_key_vals("SELECT $id, $name FROM " . table($target) . " ORDER BY 2"));
                        }
                        if (!$return && isset($value)) {
-                               return $connection->result("SELECT $name FROM " . table($table) . " WHERE $id = " . q($value));
+                               return $connection->result("SELECT $name FROM " . table($target) . " WHERE $id = " . q($value));
                        }
                        return $return;
                }
diff --git a/plugins/edit-foreign.php b/plugins/edit-foreign.php
new file mode 100644 (file)
index 0000000..fa488eb
--- /dev/null
@@ -0,0 +1,30 @@
+<?php
+
+/** Select foreign key in edit form
+* @author Jakub Vrana, http://www.vrana.cz/
+* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
+* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 (one or other)
+*/
+class AdminerEditForeign {
+       
+       function editInput($table, $field, $attrs, $value) {
+               static $foreignTables = array();
+               static $values = array();
+               $foreignKeys = &$foreignTables[$table];
+               if (!isset($foreignKeys)) {
+                       $foreignKeys = 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) {
+                                       $options = array("" => "") + get_vals("SELECT " . idf_escape($id) . " FROM " . table($target) . " ORDER BY 1");
+                               }
+                               return "<select$attrs>" . optionlist($options, $value) . "</select>";
+                       }
+               }
+       }
+       
+}