adminerevo/adminer/edit.inc.php

200 lines
6 KiB
PHP
Raw Normal View History

<?php
$TABLE = $_GET["edit"];
$fields = fields($TABLE);
$where = (isset($_GET["select"]) ? (count($_POST["check"]) == 1 ? where_check($_POST["check"][0], $fields) : "") : where($_GET, $fields));
$update = (isset($_GET["select"]) ? $_POST["edit"] : $where);
foreach ($fields as $name => $field) {
if (!isset($field["privileges"][$update ? "update" : "insert"]) || $adminer->fieldName($field) == "") {
unset($fields[$name]);
}
}
2013-05-02 01:28:04 +00:00
if ($_POST && !$error && !isset($_GET["select"])) {
$location = $_POST["referer"];
if ($_POST["insert"]) { // continue edit or insert
$location = ($update ? null : $_SERVER["REQUEST_URI"]);
} elseif (!preg_match('~^.+&select=.+$~', $location)) {
$location = ME . "select=" . urlencode($TABLE);
}
$indexes = indexes($TABLE);
$unique_array = unique_array($_GET["where"], $indexes);
$query_where = "\nWHERE $where";
if (isset($_POST["delete"])) {
queries_redirect(
$location,
lang('Item has been deleted.'),
$driver->delete($TABLE, $query_where, !$unique_array)
);
} else {
$set = array();
foreach ($fields as $name => $field) {
$val = process_input($field);
if ($val !== false && $val !== null) {
2013-07-06 17:31:21 +00:00
$set[idf_escape($name)] = $val;
}
}
if ($update) {
if (!$set) {
redirect($location);
}
2013-07-05 22:32:15 +00:00
queries_redirect(
$location,
2013-07-05 22:32:15 +00:00
lang('Item has been updated.'),
$driver->update($TABLE, $set, $query_where, !$unique_array)
);
2013-06-29 19:41:35 +00:00
if (is_ajax()) {
page_headers();
page_messages($error);
exit;
}
} else {
$result = $driver->insert($TABLE, $set);
$last_id = ($result ? last_id() : 0);
queries_redirect($location, lang('Item%s has been inserted.', ($last_id ? " $last_id" : "")), $result); //! link
}
}
}
$table_name = $adminer->tableName(table_status1($TABLE, true));
page_header(
($update ? lang('Edit') : lang('Insert')),
$error,
array("select" => array($TABLE, $table_name)),
$table_name
);
$row = null;
if ($_POST["save"]) {
$row = (array) $_POST["fields"];
} elseif ($where) {
$select = array();
foreach ($fields as $name => $field) {
if (isset($field["privileges"]["select"])) {
2012-09-09 05:56:34 +00:00
$as = convert_field($field);
if ($_POST["clone"] && $field["auto_increment"]) {
$as = "''";
}
if ($jush == "sql" && preg_match("~enum|set~", $field["type"])) {
2012-09-09 05:56:34 +00:00
$as = "1*" . idf_escape($name);
}
$select[] = ($as ? "$as AS " : "") . idf_escape($name);
}
}
$row = array();
2013-07-06 17:31:21 +00:00
if (!support("table")) {
$select = array("*");
}
if ($select) {
2014-01-11 04:51:04 +00:00
$result = $driver->select($TABLE, $select, array($where), $select, array(), (isset($_GET["select"]) ? 2 : 1), 0);
$row = $result->fetch_assoc();
if (isset($_GET["select"]) && (!$row || $result->fetch_assoc())) { // $result->num_rows != 1 isn't available in all drivers
$row = null;
}
}
}
2011-08-01 19:50:57 +00:00
2013-07-06 17:31:21 +00:00
if (!support("table") && !$fields) {
2014-01-09 08:38:48 +00:00
$id = ($jush == "mongo" ? "_id" : "itemName()"); // simpledb
2013-07-06 17:31:21 +00:00
if (!$where) { // insert
$result = $driver->select($TABLE, array("*"), $where, array("*"), array(), 1, 0);
if ($result) {
$row = $result->fetch_assoc();
}
if (!$row) {
$row = array($id => "");
}
2013-07-06 17:31:21 +00:00
}
if ($row) {
foreach ($row as $key => $val) {
2013-07-07 05:22:13 +00:00
if (!$where) {
2013-07-06 17:31:21 +00:00
$row[$key] = null;
}
2014-01-09 08:38:48 +00:00
$fields[$key] = array("field" => $key, "null" => ($key != $id), "auto_increment" => ($key == $id));
2013-07-06 17:31:21 +00:00
}
}
}
2011-08-01 19:50:57 +00:00
if ($row === false) {
echo "<p class='error'>" . lang('No rows.') . "\n";
}
?>
2013-06-29 19:41:35 +00:00
<div id="message"></div>
<form action="" method="post" enctype="multipart/form-data" id="form">
<?php
if (!$fields) {
echo "<p class='error'>" . lang('You have no privileges to update this table.') . "\n";
} else {
echo "<table cellspacing='0' onkeydown='return editingKeydown(event);'>\n";
foreach ($fields as $name => $field) {
echo "<tr><th>" . $adminer->fieldName($field);
$default = $_GET["set"][bracket_escape($name)];
if ($default === null) {
$default = $field["default"];
if ($field["type"] == "bit" && preg_match("~^b'([01]*)'\$~", $default, $regs)) {
$default = $regs[1];
}
}
2012-05-14 06:54:07 +00:00
$value = ($row !== null
2013-08-08 23:01:49 +00:00
? ($row[$name] != "" && $jush == "sql" && preg_match("~enum|set~", $field["type"])
? (is_array($row[$name]) ? array_sum($row[$name]) : +$row[$name])
: $row[$name]
)
: (!$update && $field["auto_increment"]
? ""
: (isset($_GET["select"]) ? false : $default)
)
);
if (!$_POST["save"] && is_string($value)) {
$value = $adminer->editVal($value, $field);
}
2012-05-14 06:54:07 +00:00
$function = ($_POST["save"] ? (string) $_POST["function"][$name] : ($update && $field["on_update"] == "CURRENT_TIMESTAMP" ? "now" : ($value === false ? null : ($value !== null ? '' : 'NULL'))));
if (preg_match("~time~", $field["type"]) && $value == "CURRENT_TIMESTAMP") {
$value = "";
$function = "now";
}
input($field, $value, $function);
echo "\n";
}
2013-07-06 17:31:21 +00:00
if (!support("table")) {
2013-08-09 23:26:51 +00:00
echo "<tr>"
. "<th><input name='field_keys[]' onkeyup='keyupChange.call(this);' onchange='fieldChange(this);' value=''>" // needs empty value for keyupChange()
2014-01-11 23:44:03 +00:00
. "<td class='function'>" . html_select("field_funs[]", $adminer->editFunctions(array()))
. "<td><input name='field_vals[]'>"
2013-08-09 23:26:51 +00:00
. "\n"
;
2013-07-06 17:31:21 +00:00
}
echo "</table>\n";
}
?>
<p>
<?php
if ($fields) {
2010-11-23 10:50:53 +00:00
echo "<input type='submit' value='" . lang('Save') . "'>\n";
if (!isset($_GET["select"])) {
2013-06-29 19:41:35 +00:00
echo "<input type='submit' name='insert' value='" . ($update
2013-12-19 20:44:15 +00:00
? lang('Save and continue edit') . "' onclick='return !ajaxForm(this.form, \"" . lang('Saving') . '...", this)'
2013-06-29 19:41:35 +00:00
: lang('Save and insert next')
) . "' title='Ctrl+Shift+Enter'>\n";
}
}
echo ($update ? "<input type='submit' name='delete' value='" . lang('Delete') . "'" . confirm() . ">\n"
2013-05-05 00:02:59 +00:00
: ($_POST || !$fields ? "" : "<script type='text/javascript'>focus(document.getElementById('form').getElementsByTagName('td')[1].firstChild);</script>\n")
);
2011-03-08 12:43:05 +00:00
if (isset($_GET["select"])) {
hidden_fields(array("check" => (array) $_POST["check"], "clone" => $_POST["clone"], "all" => $_POST["all"]));
}
?>
2011-03-08 12:43:05 +00:00
<input type="hidden" name="referer" value="<?php echo h(isset($_POST["referer"]) ? $_POST["referer"] : $_SERVER["HTTP_REFERER"]); ?>">
<input type="hidden" name="save" value="1">
<input type="hidden" name="token" value="<?php echo $token; ?>">
</form>