Permalink
Browse files

Edit foreign plugin

  • Loading branch information...
1 parent f8ae1d6 commit 08e70e630991733370e535621b1201ee9ee8350c @vrana committed Jun 23, 2011
Showing with 36 additions and 5 deletions.
  1. +1 −0 adminer/plugin.php
  2. +5 −5 editor/include/adminer.inc.php
  3. +30 −0 plugins/edit-foreign.php
View
@@ -20,6 +20,7 @@ function adminer_object() {
new AdminerForeignSystem,
new AdminerEnumOption,
new AdminerTablesFilter,
+ new AdminerEditForeign,
);
/* It is possible to combine customization and plugins:
@@ -575,14 +575,14 @@ function _foreignColumn($foreignKeys, $column) {
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;
}
@@ -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>";
+ }
+ }
+ }
+
+}

0 comments on commit 08e70e6

Please sign in to comment.