fix for export to windows, take 2.
[openemr.git] / phpmyadmin / pmd_relation_new.php
blob134450b4455b343deb502f7bec4072124189cc3b
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * PMD handler for creating new relation
6 * @package PhpMyAdmin-Designer
7 */
9 /**
12 require_once './libraries/common.inc.php';
14 PMA_Response::getInstance()->disable();
16 require_once 'libraries/pmd_common.php';
17 $die_save_pos = 0;
18 require_once 'pmd_save_pos.php';
19 extract($_POST, EXTR_SKIP);
21 $tables = $GLOBALS['dbi']->getTablesFull($db, $T1);
22 $type_T1 = strtoupper($tables[$T1]['ENGINE']);
23 $tables = $GLOBALS['dbi']->getTablesFull($db, $T2);
24 $type_T2 = strtoupper($tables[$T2]['ENGINE']);
26 // native foreign key
27 if (PMA_Util::isForeignKeySupported($type_T1)
28 && PMA_Util::isForeignKeySupported($type_T2)
29 && $type_T1 == $type_T2
30 ) {
31 // relation exists?
32 $existrel_foreign = PMA_getForeigners($db, $T2, '', 'foreign');
33 if (isset($existrel_foreign[$F2])
34 && isset($existrel_foreign[$F2]['constraint'])
35 ) {
36 PMD_Return_new(0, __('Error: relation already exists.'));
38 // note: in InnoDB, the index does not requires to be on a PRIMARY
39 // or UNIQUE key
40 // improve: check all other requirements for InnoDB relations
41 $result = $GLOBALS['dbi']->query(
42 'SHOW INDEX FROM ' . PMA_Util::backquote($db)
43 . '.' . PMA_Util::backquote($T1) . ';'
45 $index_array1 = array(); // will be use to emphasis prim. keys in the table view
46 while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
47 $index_array1[$row['Column_name']] = 1;
49 $GLOBALS['dbi']->freeResult($result);
51 $result = $GLOBALS['dbi']->query(
52 'SHOW INDEX FROM ' . PMA_Util::backquote($db)
53 . '.' . PMA_Util::backquote($T2) . ';'
55 // will be used to emphasis prim. keys in the table view
56 $index_array2 = array();
57 while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
58 $index_array2[$row['Column_name']] = 1;
60 $GLOBALS['dbi']->freeResult($result);
62 if (! empty($index_array1[$F1]) && ! empty($index_array2[$F2])) {
63 $upd_query = 'ALTER TABLE ' . PMA_Util::backquote($db)
64 . '.' . PMA_Util::backquote($T2)
65 . ' ADD FOREIGN KEY ('
66 . PMA_Util::backquote($F2) . ')'
67 . ' REFERENCES '
68 . PMA_Util::backquote($db) . '.'
69 . PMA_Util::backquote($T1) . '('
70 . PMA_Util::backquote($F1) . ')';
72 if ($on_delete != 'nix') {
73 $upd_query .= ' ON DELETE ' . $on_delete;
75 if ($on_update != 'nix') {
76 $upd_query .= ' ON UPDATE ' . $on_update;
78 $upd_query .= ';';
79 $GLOBALS['dbi']->tryQuery($upd_query)
80 || PMD_Return_new(0, __('Error: Relation could not be added!'));
81 PMD_Return_new(1, __('FOREIGN KEY relation has been added.'));
83 } else { // internal (pmadb) relation
84 if ($GLOBALS['cfgRelation']['relwork'] == false) {
85 PMD_Return_new(0, __('Error: Relational features are disabled!'));
86 } else {
87 // no need to recheck if the keys are primary or unique at this point,
88 // this was checked on the interface part
90 $q = 'INSERT INTO '
91 . PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
92 . '.' . PMA_Util::backquote($cfgRelation['relation'])
93 . '(master_db, master_table, master_field,'
94 . 'foreign_db, foreign_table, foreign_field)'
95 . ' values('
96 . '\'' . PMA_Util::sqlAddSlashes($db) . '\', '
97 . '\'' . PMA_Util::sqlAddSlashes($T2) . '\', '
98 . '\'' . PMA_Util::sqlAddSlashes($F2) . '\', '
99 . '\'' . PMA_Util::sqlAddSlashes($db) . '\', '
100 . '\'' . PMA_Util::sqlAddSlashes($T1) . '\','
101 . '\'' . PMA_Util::sqlAddSlashes($F1) . '\')';
103 if (PMA_queryAsControlUser($q, false, PMA_DatabaseInterface::QUERY_STORE)) {
104 PMD_Return_new(1, __('Internal relation has been added.'));
105 } else {
106 PMD_Return_new(0, __('Error: Relation could not be added!'));
112 * Send xml
114 * @param string $b Value of attribute "b"
115 * @param string $ret Value of attribute "return"
117 * @return void
119 function PMD_Return_new($b,$ret)
121 global $db,$T1,$F1,$T2,$F2;
122 header("Content-Type: text/xml; charset=utf-8");
123 header("Cache-Control: no-cache");
124 die('<root act="relation_new" return="' . $ret . '" b="' . $b .
125 '" DB1="' . urlencode($db) .
126 '" T1="' . urlencode($T1) .
127 '" F1="' . urlencode($F1) .
128 '" DB2="' . urlencode($db) .
129 '" T2="' . urlencode($T2) .
130 '" F2="' . urlencode($F2) .
131 '"></root>');