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