Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 78 lines (74 sloc) 3.755 kb
736c7335 » jakubvrana
2007-07-11 Prepare for foreign keys edit
1 <?php
79d19028 » jakubvrana
2009-08-29 Substitute table name to $TABLE
2 $TABLE = $_GET["foreign"];
cbe443c5 » jakubvrana
2007-07-11 Foreign keys
3 if ($_POST && !$error && !$_POST["add"] && !$_POST["change"] && !$_POST["change-js"]) {
2a03e63d » jakubvrana
2007-08-04 Drop and re-create at once
4 if ($_POST["drop"]) {
60c7ed9a »
2010-05-11 MS SQL schema support
5 query_redirect("ALTER TABLE " . table($TABLE) . "\nDROP " . ($jush == "sql" ? "FOREIGN KEY " : "CONSTRAINT ") . idf_escape($_GET["name"]), ME . "table=" . urlencode($TABLE), lang('Foreign key has been dropped.'));
2a03e63d » jakubvrana
2007-08-04 Drop and re-create at once
6 } else {
c9c2a9eb » jakubvrana
2007-07-16 Don't create in case of unsuccessful drop
7 $source = array_filter($_POST["source"], 'strlen');
64ba9242 » jakubvrana
2009-06-21 Comments
8 ksort($source); // enforce input order
c9c2a9eb » jakubvrana
2007-07-16 Don't create in case of unsuccessful drop
9 $target = array();
10 foreach ($source as $key => $val) {
11 $target[$key] = $_POST["target"][$key];
12 }
60c7ed9a »
2010-05-11 MS SQL schema support
13 query_redirect("ALTER TABLE " . table($TABLE)
db3ae281 »
2011-08-29 PostgreSQL: fix alter foreign key
14 . ($_GET["name"] != "" ? "\nDROP " . ($jush == "sql" ? "FOREIGN KEY " : "CONSTRAINT ") . idf_escape($_GET["name"]) . "," : "")
87888f10 »
2011-03-28 Comment
15 . "\nADD FOREIGN KEY (" . implode(", ", array_map('idf_escape', $source)) . ") REFERENCES " . table($_POST["table"]) . " (" . implode(", ", array_map('idf_escape', $target)) . ")" //! reuse $_GET["name"] - check in older MySQL versions
8e75d54e »
2011-08-02 Save bytes
16 . (ereg("^($on_actions)\$", $_POST["on_delete"]) ? " ON DELETE $_POST[on_delete]" : "")
17 . (ereg("^($on_actions)\$", $_POST["on_update"]) ? " ON UPDATE $_POST[on_update]" : "")
7352c281 » jakubvrana
2010-01-09 Replace strlen() by != ""
18 , ME . "table=" . urlencode($TABLE), ($_GET["name"] != "" ? lang('Foreign key has been altered.') : lang('Foreign key has been created.')));
73504b9e » jakubvrana
2009-08-04 English corrections (thanks to crazy1729)
19 $error = lang('Source and target columns must have the same data type, there must be an index on the target columns and referenced data must exist.') . "<br>$error"; //! no partitioning
cbe443c5 » jakubvrana
2007-07-11 Foreign keys
20 }
21 }
36d6864a » jakubvrana
2009-07-20 Whitespace
22
79d19028 » jakubvrana
2009-08-29 Substitute table name to $TABLE
23 page_header(lang('Foreign key'), $error, array("table" => $TABLE), $TABLE);
736c7335 » jakubvrana
2007-07-11 Prepare for foreign keys edit
24
79d19028 » jakubvrana
2009-08-29 Substitute table name to $TABLE
25 $row = array("table" => $TABLE, "source" => array(""));
cbe443c5 » jakubvrana
2007-07-11 Foreign keys
26 if ($_POST) {
27 $row = $_POST;
28 ksort($row["source"]);
29 if ($_POST["add"]) {
30 $row["source"][] = "";
31 } elseif ($_POST["change"] || $_POST["change-js"]) {
32 $row["target"] = array();
33 }
7352c281 » jakubvrana
2010-01-09 Replace strlen() by != ""
34 } elseif ($_GET["name"] != "") {
79d19028 » jakubvrana
2009-08-29 Substitute table name to $TABLE
35 $foreign_keys = foreign_keys($TABLE);
cbe443c5 » jakubvrana
2007-07-11 Foreign keys
36 $row = $foreign_keys[$_GET["name"]];
2a03e63d » jakubvrana
2007-08-04 Drop and re-create at once
37 $row["source"][] = "";
cbe443c5 » jakubvrana
2007-07-11 Foreign keys
38 }
aa14be06 » jakubvrana
2007-07-14 $target uses $row
39
5e01a627 » jakubvrana
2009-10-16 Prepare for SQLite
40 $source = array_keys(fields($TABLE)); //! no text and blob
41 $target = ($TABLE === $row["table"] ? $source : array_keys(fields($row["table"])));
3f5b6834 » jakubvrana
2010-04-21 Reintegrate sqlite branch
42 $referencable = array();
43 foreach (table_status() as $name => $table_status) {
44 if (fk_support($table_status)) {
45 $referencable[] = $name;
46 }
47 }
cbe443c5 » jakubvrana
2007-07-11 Foreign keys
48 ?>
2a03e63d » jakubvrana
2007-08-04 Drop and re-create at once
49
cbe443c5 » jakubvrana
2007-07-11 Foreign keys
50 <form action="" method="post">
51 <p>
9e0e1612 »
2011-07-12 Display foreign keys from other schemas in PostgreSQL (thanks to Ondr…
52 <?php if ($row["db"] == "" && $row["ns"] == "") { ?>
cbe443c5 » jakubvrana
2007-07-11 Foreign keys
53 <?php echo lang('Target table'); ?>:
4815c7d0 »
2012-04-15 Don't use AJAX links and forms
54 <?php echo html_select("table", $referencable, $row["table"], "this.form['change-js'].value = '1'; this.form.submit();"); ?>
ace55ed1 » jakubvrana
2009-07-11 HTML instead of XHTML
55 <input type="hidden" name="change-js" value="">
56 <noscript><p><input type="submit" name="change" value="<?php echo lang('Change'); ?>"></noscript>
d30facef » jakubvrana
2009-05-01 Remove useless table attributes (thanks to Juraj Krivda)
57 <table cellspacing="0">
ace55ed1 » jakubvrana
2009-07-11 HTML instead of XHTML
58 <thead><tr><th><?php echo lang('Source'); ?><th><?php echo lang('Target'); ?></thead>
cbe443c5 » jakubvrana
2007-07-11 Foreign keys
59 <?php
2a03e63d » jakubvrana
2007-08-04 Drop and re-create at once
60 $j = 0;
cbe443c5 » jakubvrana
2007-07-11 Foreign keys
61 foreach ($row["source"] as $key => $val) {
62 echo "<tr>";
04eccbad »
2010-10-22 Allow bigger numbers under 32 bits
63 echo "<td>" . html_select("source[" . (+$key) . "]", array(-1 => "") + $source, $val, ($j == count($row["source"]) - 1 ? "foreignAddRow(this);" : 1));
64 echo "<td>" . html_select("target[" . (+$key) . "]", $target, $row["target"][$key]);
2a03e63d » jakubvrana
2007-08-04 Drop and re-create at once
65 $j++;
736c7335 » jakubvrana
2007-07-11 Prepare for foreign keys edit
66 }
cbe443c5 » jakubvrana
2007-07-11 Foreign keys
67 ?>
68 </table>
69 <p>
8e75d54e »
2011-08-02 Save bytes
70 <?php echo lang('ON DELETE'); ?>: <?php echo html_select("on_delete", array(-1 => "") + explode("|", $on_actions), $row["on_delete"]); ?>
71 <?php echo lang('ON UPDATE'); ?>: <?php echo html_select("on_update", array(-1 => "") + explode("|", $on_actions), $row["on_update"]); ?>
2188934c » jakubvrana
2007-07-25 Separate ON UPDATE and ON DELETE
72 <p>
ace55ed1 » jakubvrana
2009-07-11 HTML instead of XHTML
73 <input type="submit" value="<?php echo lang('Save'); ?>">
74 <noscript><p><input type="submit" name="add" value="<?php echo lang('Add column'); ?>"></noscript>
36c2ae7b » jakubvrana
2009-11-19 Allow dropping remote foreign key
75 <?php } ?>
a93bc191 »
2010-10-18 Create confirm function
76 <?php if ($_GET["name"] != "") { ?><input type="submit" name="drop" value="<?php echo lang('Drop'); ?>"<?php echo confirm(); ?>><?php } ?>
36c2ae7b » jakubvrana
2009-11-19 Allow dropping remote foreign key
77 <input type="hidden" name="token" value="<?php echo $token; ?>">
cbe443c5 » jakubvrana
2007-07-11 Foreign keys
78 </form>
Something went wrong with that request. Please try again.