Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 88 lines (85 sloc) 3.324 kb
88e1daf Prepare for index edit
jakubvrana authored
1 <?php
2 $index_types = array("PRIMARY", "UNIQUE", "INDEX", "FULLTEXT");
0b6ab61 Alter indexes
jakubvrana authored
3 $indexes = indexes($_GET["indexes"]);
430034c Cottage homework
jakubvrana authored
4 if ($_POST && !$error && !$_POST["add"]) {
0b6ab61 Alter indexes
jakubvrana authored
5 $alter = array();
6 foreach ($_POST["indexes"] as $index) {
7 if (in_array($index["type"], $index_types)) {
8 $columns = array();
e78ec76 Index length
jakubvrana authored
9 $lengths = array();
10 $set = array();
0b6ab61 Alter indexes
jakubvrana authored
11 ksort($index["columns"]);
e78ec76 Index length
jakubvrana authored
12 foreach ($index["columns"] as $key => $column) {
13 if (strlen($column)) {
14 $length = $index["lengths"][$key];
15 $set[] = idf_escape($column) . ($length ? "(" . intval($length) . ")" : "");
0b6ab61 Alter indexes
jakubvrana authored
16 $columns[count($columns) + 1] = $column;
e78ec76 Index length
jakubvrana authored
17 $lengths[count($lengths) + 1] = ($length ? $length : null);
0b6ab61 Alter indexes
jakubvrana authored
18 }
19 }
20 if ($columns) {
21 foreach ($indexes as $name => $existing) {
8983582 Sort existing for ===
jakubvrana authored
22 ksort($existing["columns"]);
23 ksort($existing["lengths"]);
e78ec76 Index length
jakubvrana authored
24 if ($index["type"] == $existing["type"] && $existing["columns"] === $columns && $existing["lengths"] === $lengths) {
0b6ab61 Alter indexes
jakubvrana authored
25 unset($indexes[$name]);
26 continue 2;
27 }
28 }
20679d8 Use \n in SQL commands
jakubvrana authored
29 $alter[] = "\nADD $index[type]" . ($index["type"] == "PRIMARY" ? " KEY" : "") . " (" . implode(", ", $set) . ")";
0b6ab61 Alter indexes
jakubvrana authored
30 }
31 }
32 }
33 foreach ($indexes as $name => $existing) {
20679d8 Use \n in SQL commands
jakubvrana authored
34 $alter[] = "\nDROP INDEX " . idf_escape($name);
0b6ab61 Alter indexes
jakubvrana authored
35 }
f91b2c4 Print SQL query by message
jakubvrana authored
36 if (!$alter) {
37 redirect($SELF . "table=" . urlencode($_GET["indexes"]));
0b6ab61 Alter indexes
jakubvrana authored
38 }
20679d8 Use \n in SQL commands
jakubvrana authored
39 query_redirect("ALTER TABLE " . idf_escape($_GET["indexes"]) . implode(",", $alter), $SELF . "table=" . urlencode($_GET["indexes"]), lang('Indexes has been altered.'));
88e1daf Prepare for index edit
jakubvrana authored
40 }
eff527b Move error output to page_header()
jakubvrana authored
41 page_header(lang('Indexes'), $error, array("table" => $_GET["indexes"]), $_GET["indexes"]);
88e1daf Prepare for index edit
jakubvrana authored
42
e78ec76 Index length
jakubvrana authored
43 $fields = array_keys(fields($_GET["indexes"]));
d8df520 Initialize variables outside blocks
jakubvrana authored
44 $row = array("indexes" => $indexes);
88e1daf Prepare for index edit
jakubvrana authored
45 if ($_POST) {
e78ec76 Index length
jakubvrana authored
46 $row = $_POST;
4d38c7d Protection against big POST data
jakubvrana authored
47 if ($_POST["add"]) {
e78ec76 Index length
jakubvrana authored
48 foreach ($row["indexes"] as $key => $index) {
49 if (strlen($index["columns"][count($index["columns"])])) {
50 $row["indexes"][$key]["columns"][] = "";
51 }
52 }
69673ae Use end() for last item
jakubvrana authored
53 $index = end($row["indexes"]);
8983582 Sort existing for ===
jakubvrana authored
54 if ($index["type"] || array_filter($index["columns"], 'strlen') || array_filter($index["lengths"], 'strlen')) {
e78ec76 Index length
jakubvrana authored
55 $row["indexes"][] = array("columns" => array(1 => ""));
56 }
aaa22e0 Add next
jakubvrana authored
57 }
88e1daf Prepare for index edit
jakubvrana authored
58 } else {
e78ec76 Index length
jakubvrana authored
59 foreach ($row["indexes"] as $key => $index) {
60 $row["indexes"][$key]["columns"][] = "";
61 }
62 $row["indexes"][] = array("columns" => array(1 => ""));
88e1daf Prepare for index edit
jakubvrana authored
63 }
64 ?>
f508fca Empty line
jakubvrana authored
65
88e1daf Prepare for index edit
jakubvrana authored
66 <form action="" method="post">
d30face Remove useless table attributes (thanks to Juraj Krivda)
jakubvrana authored
67 <table cellspacing="0">
ace55ed HTML instead of XHTML
jakubvrana authored
68 <thead><tr><th><?php echo lang('Index Type'); ?><th><?php echo lang('Column (length)'); ?></thead>
88e1daf Prepare for index edit
jakubvrana authored
69 <?php
70 $j = 0;
720f5fc Restructure indexes
jakubvrana authored
71 foreach ($row["indexes"] as $index) {
ace55ed HTML instead of XHTML
jakubvrana authored
72 echo "<tr><td><select name='indexes[$j][type]'" . ($j == count($row["indexes"]) - 1 ? " onchange='indexes_add_row(this);'" : "") . "><option>" . optionlist($index_types, $index["type"]) . "</select><td>\n";
e78ec76 Index length
jakubvrana authored
73 ksort($index["columns"]);
74 foreach ($index["columns"] as $i => $column) {
ace55ed HTML instead of XHTML
jakubvrana authored
75 echo "<span><select name='indexes[$j][columns][$i]'" . ($i == count($index["columns"]) ? " onchange='indexes_add_column(this);'" : "") . "><option>" . optionlist($fields, $column) . "</select>";
76 echo "<input name='indexes[$j][lengths][$i]' size='2' value=\"" . htmlspecialchars($index["lengths"][$i]) . "\"> </span>\n";
88e1daf Prepare for index edit
jakubvrana authored
77 }
ace55ed HTML instead of XHTML
jakubvrana authored
78 echo "\n";
e78ec76 Index length
jakubvrana authored
79 $j++;
88e1daf Prepare for index edit
jakubvrana authored
80 }
81 ?>
82 </table>
58178ab Move buttons
jakubvrana authored
83 <p>
ace55ed HTML instead of XHTML
jakubvrana authored
84 <input type="hidden" name="token" value="<?php echo $token; ?>">
85 <input type="submit" value="<?php echo lang('Save'); ?>">
86 <noscript><p><input type="submit" name="add" value="<?php echo lang('Add next'); ?>"></noscript>
88e1daf Prepare for index edit
jakubvrana authored
87 </form>
Something went wrong with that request. Please try again.