Fix indentation
[phpmyadmin.git] / pmd_common.php
blob6593762a917a82e26615c02c564f26dc13b9a42b
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * @package phpMyAdmin-Designer
5 */
7 /**
9 */
10 require_once './libraries/common.inc.php';
11 // not understand
12 require_once './libraries/header_http.inc.php';
14 $GLOBALS['PMD']['STYLE'] = 'default';
16 $cfgRelation = PMA_getRelationsParam();
18 $GLOBALS['script_display_field'] =
19 '<script type="text/javascript">' . "\n" .
20 '// <![CDATA[' . "\n" .
21 'var display_field = new Array();' . "\n";
23 /**
24 * retrieves table info and stores it in $GLOBALS['PMD']
27 function get_tabs()
29 $GLOBALS['PMD']['TABLE_NAME'] = array();// that foreach no error
30 $GLOBALS['PMD']['OWNER'] = array();
31 $GLOBALS['PMD']['TABLE_NAME_SMALL'] = array();
33 $tables = PMA_DBI_get_tables_full($GLOBALS['db']);
34 // seems to be needed later
35 PMA_DBI_select_db($GLOBALS['db']);
36 $i = 0;
37 foreach ($tables as $one_table) {
38 $GLOBALS['PMD']['TABLE_NAME'][$i] = $GLOBALS['db'] . "." . $one_table['TABLE_NAME'];
39 $GLOBALS['PMD']['OWNER'][$i] = $GLOBALS['db'];
40 $GLOBALS['PMD']['TABLE_NAME_SMALL'][$i] = $one_table['TABLE_NAME'];
42 $GLOBALS['PMD_URL']['TABLE_NAME'][$i] = urlencode($GLOBALS['db'] . "." . $one_table['TABLE_NAME']);
43 $GLOBALS['PMD_URL']['OWNER'][$i] = urlencode($GLOBALS['db']);
44 $GLOBALS['PMD_URL']['TABLE_NAME_SMALL'][$i] = urlencode($one_table['TABLE_NAME']);
46 $GLOBALS['PMD_OUT']['TABLE_NAME'][$i] = htmlspecialchars($GLOBALS['db'] . "." . $one_table['TABLE_NAME'], ENT_QUOTES);
47 $GLOBALS['PMD_OUT']['OWNER'][$i] = htmlspecialchars($GLOBALS['db'], ENT_QUOTES);
48 $GLOBALS['PMD_OUT']['TABLE_NAME_SMALL'][$i] = htmlspecialchars($one_table['TABLE_NAME'], ENT_QUOTES);
50 $GLOBALS['PMD']['TABLE_TYPE'][$i] = strtoupper($one_table['ENGINE']);
52 $DF = PMA_getDisplayField($GLOBALS['db'], $one_table['TABLE_NAME']);
53 if ($DF != '') {
54 $GLOBALS['script_display_field'] .= " display_field['"
55 . $GLOBALS['PMD_URL']["TABLE_NAME_SMALL"][$i] . "'] = '"
56 . urlencode($DF) . "';\n";
59 $i++;
62 $GLOBALS['script_display_field'] .=
63 '// ]]>' . "\n" .
64 '</script>' . "\n";
65 // return $GLOBALS['PMD']; // many bases // not use ??????
68 /**
69 * retrieves table column info
71 * @return array table column nfo
73 function get_tab_info()
75 PMA_DBI_select_db($GLOBALS['db']);
76 $tab_column = array();
77 for ($i = 0, $cnt = count($GLOBALS['PMD']["TABLE_NAME"]); $i < $cnt; $i++) {
78 $fields_rs = PMA_DBI_query('SHOW FULL FIELDS FROM '.PMA_backquote($GLOBALS['PMD']["TABLE_NAME_SMALL"][$i]), NULL, PMA_DBI_QUERY_STORE);
79 $j = 0;
80 while ($row = PMA_DBI_fetch_assoc($fields_rs)) {
81 $tab_column[$GLOBALS['PMD']['TABLE_NAME'][$i]]['COLUMN_ID'][$j] = $j;
82 $tab_column[$GLOBALS['PMD']['TABLE_NAME'][$i]]['COLUMN_NAME'][$j] = $row['Field'];
83 $tab_column[$GLOBALS['PMD']['TABLE_NAME'][$i]]['TYPE'][$j] = $row['Type'];
84 $tab_column[$GLOBALS['PMD']['TABLE_NAME'][$i]]['NULLABLE'][$j] = $row['Null'];
85 $j++;
88 return $tab_column;
91 /**
92 * returns JavaScript code for intializing vars
94 * @return string JavaScript code
96 function get_script_contr()
98 PMA_DBI_select_db($GLOBALS['db']);
99 $con["C_NAME"] = array();
100 $i = 0;
101 $alltab_rs = PMA_DBI_query('SHOW TABLES FROM ' . PMA_backquote($GLOBALS['db']), NULL, PMA_DBI_QUERY_STORE);
102 while ($val = @PMA_DBI_fetch_row($alltab_rs)) {
103 $row = PMA_getForeigners($GLOBALS['db'], $val[0], '', 'internal');
104 //echo "<br> internal ".$GLOBALS['db']." - ".$val[0]." - ";
105 //print_r($row);
106 if ($row !== false) {
107 foreach ($row as $field => $value) {
108 $con['C_NAME'][$i] = '';
109 $con['DTN'][$i] = urlencode($GLOBALS['db'] . "." . $val[0]);
110 $con['DCN'][$i] = urlencode($field);
111 $con['STN'][$i] = urlencode($value['foreign_db'] . "." . $value['foreign_table']);
112 $con['SCN'][$i] = urlencode($value['foreign_field']);
113 $i++;
116 $row = PMA_getForeigners($GLOBALS['db'], $val[0], '', 'foreign');
117 //echo "<br> INNO ";
118 //print_r($row);
119 if ($row !== false) {
120 foreach ($row as $field => $value) {
121 $con['C_NAME'][$i] = '';
122 $con['DTN'][$i] = urlencode($GLOBALS['db'].".".$val[0]);
123 $con['DCN'][$i] = urlencode($field);
124 $con['STN'][$i] = urlencode($value['foreign_db'].".".$value['foreign_table']);
125 $con['SCN'][$i] = urlencode($value['foreign_field']);
126 $i++;
131 $ti = 0;
132 $script_contr =
133 '<script type="text/javascript">' . "\n" .
134 '// <![CDATA[' . "\n" .
135 'var contr = new Array();' . "\n";
136 for ($i = 0, $cnt = count($con["C_NAME"]); $i < $cnt; $i++) {
137 $js_var = ' contr[' . $ti . ']';
138 $script_contr .= $js_var . " = new Array();\n";
139 $js_var .= "['" . $con['C_NAME'][$i] . "']";
140 $script_contr .= $js_var . " = new Array();\n";
141 if (in_array($con['DTN'][$i], $GLOBALS['PMD_URL']["TABLE_NAME"])
142 && in_array($con['STN'][$i], $GLOBALS['PMD_URL']["TABLE_NAME"])) {
143 $js_var .= "['" . $con['DTN'][$i] . "']";
144 $script_contr .= $js_var . " = new Array();\n";
145 $m_col = array();//}
146 $js_var .= "['" . $con['DCN'][$i] . "']";
147 $script_contr .= $js_var . " = new Array();\n";//}
148 $script_contr .= $js_var . "[0] = '" . $con['STN'][$i] . "';\n"; //
149 $script_contr .= $js_var . "[1] = '" . $con['SCN'][$i] . "';\n"; //
151 $ti++;
153 $script_contr .=
154 '// ]]>' . "\n" .
155 '</script>' . "\n";
156 return $script_contr;
160 * @return array unique or primary indizes
162 function get_pk_or_unique_keys()
164 return get_all_keys(true);
168 * returns all indizes
170 * @param boolean whether to include ony unique ones
171 * @return array indizes
173 function get_all_keys($unique_only = false)
175 require_once './libraries/Index.class.php';
177 $keys = array();
179 foreach ($GLOBALS['PMD']['TABLE_NAME_SMALL'] as $I => $table) {
180 $schema = $GLOBALS['PMD']['OWNER'][$I];
181 // for now, take into account only the first index segment
182 foreach (PMA_Index::getFromTable($table, $schema) as $index) {
183 if ($unique_only && ! $index->isUnique()) {
184 continue;
186 $columns = $index->getColumns();
187 foreach ($columns as $column_name => $dummy) {
188 $keys[$schema . '.' .$table . '.' . $column_name] = 1;
192 return $keys;
198 * @return array ???
200 function get_script_tabs()
202 $script_tabs =
203 '<script type="text/javascript">' . "\n" .
204 '// <![CDATA[' . "\n" .
205 'var j_tabs = new Array();' . "\n" .
206 'var h_tabs = new Array();' . "\n" ;
207 for ($i = 0, $cnt = count($GLOBALS['PMD']['TABLE_NAME']); $i < $cnt; $i++) {
208 $script_tabs .= "j_tabs['" . $GLOBALS['PMD_URL']['TABLE_NAME'][$i] . "'] = '"
209 . (PMA_foreignkey_supported($GLOBALS['PMD']['TABLE_TYPE'][$i]) ? '1' : '0') . "';\n";
210 $script_tabs .="h_tabs['" . $GLOBALS['PMD_URL']['TABLE_NAME'][$i] . "'] = 1;"."\n" ;
212 $script_tabs .=
213 '// ]]>' . "\n" .
214 '</script>' . "\n";
215 return $script_tabs;
219 * @return array table positions and sizes
221 function get_tab_pos()
223 $cfgRelation = PMA_getRelationsParam();
225 if (! $cfgRelation['designerwork']) {
226 return null;
229 $query = "
230 SELECT CONCAT_WS('.', `db_name`, `table_name`) AS `name`,
231 `x` AS `X`,
232 `y` AS `Y`,
233 `v` AS `V`,
234 `h` AS `H`
235 FROM " . PMA_backquote($cfgRelation['db']) . "." . PMA_backquote($cfgRelation['designer_coords']);
236 $tab_pos = PMA_DBI_fetch_result($query, 'name', null, $GLOBALS['controllink'], PMA_DBI_QUERY_STORE);
237 return count($tab_pos) ? $tab_pos : null;
241 * returns distinct values from $GLOBALS['PMD']['OWNER']
243 * @return array owner
245 function get_owners()
247 return array_values(array_unique($GLOBALS['PMD']['OWNER']));
250 get_tabs();