From 6317c7737db9b823dbd0192ed056a29d9115efcc Mon Sep 17 00:00:00 2001 From: Jakub Vrana Date: Fri, 9 Aug 2013 15:49:34 -0700 Subject: [PATCH] NoSQL: Allow editing complex values --- adminer/drivers/elastic.inc.php | 2 +- adminer/drivers/mongo.inc.php | 2 +- adminer/drivers/simpledb.inc.php | 16 ++++++++++------ adminer/include/functions.inc.php | 20 +++++++++++++++++++- 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/adminer/drivers/elastic.inc.php b/adminer/drivers/elastic.inc.php index d448a6b2..6698f73b 100644 --- a/adminer/drivers/elastic.inc.php +++ b/adminer/drivers/elastic.inc.php @@ -283,5 +283,5 @@ if (isset($_GET["elastic"])) { $operators = array("=", "query"); $functions = array(); $grouping = array(); - $edit_functions = array(); + $edit_functions = array(array("json")); } diff --git a/adminer/drivers/mongo.inc.php b/adminer/drivers/mongo.inc.php index 1a6eb308..e3999fc8 100644 --- a/adminer/drivers/mongo.inc.php +++ b/adminer/drivers/mongo.inc.php @@ -290,5 +290,5 @@ if (isset($_GET["mongo"])) { $operators = array("="); $functions = array(); $grouping = array(); - $edit_functions = array(); + $edit_functions = array(array("json")); } diff --git a/adminer/drivers/simpledb.inc.php b/adminer/drivers/simpledb.inc.php index 6810a5ae..652314dc 100644 --- a/adminer/drivers/simpledb.inc.php +++ b/adminer/drivers/simpledb.inc.php @@ -173,11 +173,15 @@ if (isset($_GET["simpledb"])) { $key = idf_unescape($key); if ($val == "NULL") { $delete["Attribute." . count($delete) . ".Name"] = $key; - } elseif ($key != "itemName()") { - $insert["Attribute.$i.Name"] = $key; - $insert["Attribute.$i.Value"] = idf_unescape($val); - $insert["Attribute.$i.Replace"] = "true"; - $i++; + } elseif ($key != "itemName()") { //! allow changing itemName() + foreach ((array) $val as $k => $v) { + $insert["Attribute.$i.Name"] = $key; + $insert["Attribute.$i.Value"] = (is_array($val) ? $v : idf_unescape($v)); + if (!$k) { + $insert["Attribute.$i.Replace"] = "true"; + } + $i++; + } } } $ids = $this->_extractIds($table, $queryWhere, $limit); @@ -473,5 +477,5 @@ if (isset($_GET["simpledb"])) { $operators = array("=", "<", ">", "<=", ">=", "!=", "LIKE", "LIKE %%", "IN", "IS NULL", "NOT LIKE", "IS NOT NULL"); $functions = array(); $grouping = array("count"); - $edit_functions = array(); + $edit_functions = array(array("json")); } diff --git a/adminer/include/functions.inc.php b/adminer/include/functions.inc.php index c5f3af97..85ecb28d 100644 --- a/adminer/include/functions.inc.php +++ b/adminer/include/functions.inc.php @@ -781,6 +781,14 @@ function input($field, $value, $function) { global $connection, $types, $adminer, $jush; $name = h(bracket_escape($field["field"])); echo ""; + if (is_array($value) && !$function) { + $args = array($value); + if (version_compare(PHP_VERSION, 5.4) >= 0) { + $args[] = JSON_PRETTY_PRINT; + } + $value = call_user_func_array('json_encode', $args); //! requires PHP 5.2 + $function = "json"; + } $reset = ($jush == "mssql" && $field["auto_increment"]); if ($reset && !$_POST["save"]) { $function = null; @@ -823,6 +831,8 @@ function input($field, $value, $function) { $attrs .= " cols='30' rows='$rows'" . ($rows == 1 ? " style='height: 1.2em;'" : ""); // 1.2em - line-height } echo "" . h($value) . ''; + } elseif ($function == "json") { + echo "" . h($value) . ''; } else { // int(3) is only a display hint $maxlength = (!preg_match('~int~', $field["type"]) && preg_match('~^(\\d+)(,(\\d+))?$~', $field["length"], $match) ? ((preg_match("~binary~", $field["type"]) ? 2 : 1) * $match[1] + ($match[3] ? 1 : 0) + ($match[2] && !$field["unsigned"] ? 1 : 0)) : ($types[$field["type"]] ? $types[$field["type"]] + ($field["unsigned"] ? 0 : 1) : 0)); @@ -837,7 +847,7 @@ function input($field, $value, $function) { /** Process edit input field * @param one field from fields() -* @return string +* @return string or false to leave the original value */ function process_input($field) { global $adminer; @@ -865,6 +875,14 @@ function process_input($field) { if ($field["type"] == "set") { return array_sum((array) $value); } + if ($function == "json") { + $function = ""; + $value = json_decode($value, true); + if (!is_array($value)) { + return false; //! report errors + } + return $value; + } if (preg_match('~blob|bytea|raw|file~', $field["type"]) && ini_bool("file_uploads")) { $file = get_file("fields-$idf"); if (!is_string($file)) {