2 /* vim: set expandtab sw=4 ts=4 sts=4: */
10 * Gets some core libraries
12 require_once './libraries/common.inc.php';
13 require_once './libraries/db_common.inc.php';
14 require './libraries/StorageEngine.class.php';
16 $active_page = 'db_operations.php';
17 require_once './libraries/db_common.inc.php';
18 $url_query .= '&goto=pdf_pages.php';
19 require_once './libraries/db_info.inc.php';
22 * Settings for relation stuff
24 require_once './libraries/relation.lib.php';
25 $cfgRelation = PMA_getRelationsParam();
27 // This is to avoid "Command out of sync" errors. Before switching this to
28 // a value of 0 (for MYSQLI_USE_RESULT), please check the logic
29 // to free results wherever needed.
30 $query_default_option = PMA_DBI_QUERY_STORE
;
33 * Now in ./libraries/relation.lib.php we check for all tables
34 * that we need, but if we don't find them we are quiet about it
35 * so people can work without.
36 * This page is absolutely useless if you didn't set up your tables
37 * correctly, so it is a good place to see which tables we can and
40 if (!$cfgRelation['relwork']) {
41 echo sprintf(__('<b>%s</b> table not found or not set in %s'), 'relation', 'config.inc.php') . '<br />' . "\n"
42 . PMA_showDocu('relation') . "\n";
43 require_once './libraries/footer.inc.php';
46 if (!$cfgRelation['displaywork']) {
47 echo sprintf(__('<b>%s</b> table not found or not set in %s'), 'table_info', 'config.inc.php') . '<br />' . "\n"
48 . PMA_showDocu('table_info') . "\n";
49 require_once './libraries/footer.inc.php';
52 if (!isset($cfgRelation['table_coords'])){
53 echo sprintf(__('<b>%s</b> table not found or not set in %s'), 'table_coords', 'config.inc.php') . '<br />' . "\n"
54 . PMA_showDocu('table_coords') . "\n";
55 require_once './libraries/footer.inc.php';
57 if (!isset($cfgRelation['pdf_pages'])) {
58 echo sprintf(__('<b>%s</b> table not found or not set in %s'), 'pdf_page', 'config.inc.php') . '<br />' . "\n"
59 . PMA_showDocu('pdf_pages') . "\n";
60 require_once './libraries/footer.inc.php';
63 if ($cfgRelation['pdfwork']) {
64 // Now is the time to work on all changes
68 if ($action_choose=="1") {
69 $ch_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
70 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
71 . ' AND pdf_page_number = \'' . PMA_sqlAddslashes($chpage) . '\'';
72 PMA_query_as_controluser($ch_query, FALSE, $query_default_option);
74 $ch_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
75 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
76 . ' AND page_nr = \'' . PMA_sqlAddslashes($chpage) . '\'';
77 PMA_query_as_controluser($ch_query, FALSE, $query_default_option);
83 $pdf_page_number = PMA_REL_create_page($newpage, $cfgRelation, $db, $query_default_option);
85 // A u t o m a t i c l a y o u t
86 // ================================
87 if (isset($auto_layout_internal) ||
isset($auto_layout_foreign)) {
88 $all_tables = array();
91 if (isset($auto_layout_foreign)) {
92 // get the tables list
93 $tables = PMA_DBI_get_tables_full($db);
94 // find the ones who support FOREIGN KEY; it's not
95 // important that we group together InnoDB tables
96 // and PBXT tables, as this logic is just to put
97 // the tables on the layout, not to determine relations
98 $foreignkey_tables = array();
99 foreach($tables as $table_name => $table_properties) {
100 if (PMA_foreignkey_supported($table_properties['ENGINE'])) {
101 $foreignkey_tables[] = $table_name;
104 $all_tables = $foreignkey_tables;
105 // could be improved by finding the tables which have the
106 // most references keys and placing them at the beginning
107 // of the array (so that they are all center of schema)
108 unset($tables, $foreignkey_tables);
109 } // endif auto_layout_foreign
111 if (isset($auto_layout_internal)) {
112 // get the tables that have relations, by descending
114 $master_tables = 'SELECT COUNT(master_table), master_table'
115 . ' FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
116 . ' WHERE master_db = \'' . $db . '\''
117 . ' GROUP BY master_table'
118 . ' ORDER BY ' . PMA_backquote('COUNT(master_table)') . ' DESC ';
119 $master_tables_rs = PMA_query_as_controluser($master_tables, FALSE, $query_default_option);
120 if ($master_tables_rs && PMA_DBI_num_rows($master_tables_rs) > 0) {
121 // first put all the master tables at beginning
122 // of the list, so they are near the center of
124 while (list(, $master_table) = PMA_DBI_fetch_row($master_tables_rs)) {
125 $all_tables[] = $master_table;
128 // then for each master, add its foreigns into an array
129 // of foreign tables, if not already there
130 // (a foreign might be foreign for more than
131 // one table, and might be a master itself)
133 $foreign_tables = array();
134 foreach ($all_tables AS $master_table) {
135 $foreigners = PMA_getForeigners($db, $master_table);
136 foreach ($foreigners AS $foreigner) {
137 if (!in_array($foreigner['foreign_table'], $foreign_tables)) {
138 $foreign_tables[] = $foreigner['foreign_table'];
143 // then merge the arrays
144 foreach ($foreign_tables AS $foreign_table) {
145 if (!in_array($foreign_table, $all_tables)) {
146 $all_tables[] = $foreign_table;
149 } // endif there are master tables
150 } // endif auto_layout_internal
152 if (isset($auto_layout_internal) ||
isset($auto_layout_foreign)) {
153 // now generate the coordinates for the schema,
154 // in a clockwise spiral
160 $direction = "right";
161 foreach ($all_tables AS $current_table) {
163 // save current table's coordinates
164 $insert_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' '
165 . '(db_name, table_name, pdf_page_number, x, y) '
166 . 'VALUES (\'' . PMA_sqlAddslashes($db) . '\', \'' . PMA_sqlAddslashes($current_table) . '\',' . $pdf_page_number . ',' . $pos_x . ',' . $pos_y . ')';
167 PMA_query_as_controluser($insert_query, FALSE, $query_default_option);
169 // compute for the next table
170 switch ($direction) {
174 $delta *= $delta_mult;
179 $delta *= $delta_mult;
184 $delta *= $delta_mult;
188 $direction = "right";
189 $delta *= $delta_mult;
193 } // end if some auto-layout to do
195 $chpage = $pdf_page_number;
200 for ($i = 0; $i < $c_table_rows; $i++
) {
201 $arrvalue = 'c_table_' . $i;
202 $arrvalue = $
$arrvalue;
203 if (!isset($arrvalue['x']) ||
$arrvalue['x'] == '') {
206 if (!isset($arrvalue['y']) ||
$arrvalue['y'] == '') {
209 if (isset($arrvalue['name']) && $arrvalue['name'] != '--') {
210 $test_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
211 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
212 . ' AND table_name = \'' . PMA_sqlAddslashes($arrvalue['name']) . '\''
213 . ' AND pdf_page_number = \'' . PMA_sqlAddslashes($chpage) . '\'';
214 $test_rs = PMA_query_as_controluser($test_query, FALSE, $query_default_option);
215 if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) {
216 if (isset($arrvalue['delete']) && $arrvalue['delete'] == 'y') {
217 $ch_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
218 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
219 . ' AND table_name = \'' . PMA_sqlAddslashes($arrvalue['name']) . '\''
220 . ' AND pdf_page_number = \'' . PMA_sqlAddslashes($chpage) . '\'';
222 $ch_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' '
223 . 'SET x = ' . $arrvalue['x'] . ', y= ' . $arrvalue['y']
224 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
225 . ' AND table_name = \'' . PMA_sqlAddslashes($arrvalue['name']) . '\''
226 . ' AND pdf_page_number = \'' . PMA_sqlAddslashes($chpage) . '\'';
229 $ch_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' '
230 . '(db_name, table_name, pdf_page_number, x, y) '
231 . 'VALUES (\'' . PMA_sqlAddslashes($db) . '\', \'' . PMA_sqlAddslashes($arrvalue['name']) . '\', \'' . PMA_sqlAddslashes($chpage) . '\',' . $arrvalue['x'] . ',' . $arrvalue['y'] . ')';
233 PMA_query_as_controluser($ch_query, FALSE, $query_default_option);
238 foreach ($delrow AS $current_row) {
239 $d_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' ' . "\n"
240 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'' . "\n"
241 . ' AND table_name = \'' . PMA_sqlAddslashes($current_row) . '\'' . "\n"
242 . ' AND pdf_page_number = \'' . PMA_sqlAddslashes($chpage) . '\'';
243 PMA_query_as_controluser($d_query, FALSE, $query_default_option);
247 } // end if (isset($do))
249 // We will need an array of all tables in this db
250 $selectboxall = array('--');
251 $alltab_rs = PMA_DBI_query('SHOW TABLES FROM ' . PMA_backquote($db) . ';', null, PMA_DBI_QUERY_STORE
);
252 while ($val = @PMA_DBI_fetch_row
($alltab_rs)) {
253 $selectboxall[] = $val[0];
256 // Now first show some possibility to choose a page for the pdf
257 $page_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
258 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'';
259 $page_rs = PMA_query_as_controluser($page_query, FALSE, $query_default_option);
261 if ($page_rs && PMA_DBI_num_rows($page_rs) > 0) {
263 <form method
="get" action
="pdf_pages.php" name
="selpage">
266 <?php
echo __('Please choose a page to edit') . "\n"; ?
>
268 <?php
echo PMA_generate_common_hidden_inputs($db, $table); ?
>
269 <input type
="hidden" name
="do" value
="choosepage" />
270 <select name
="chpage" onchange
="this.form.submit()">
272 while ($curr_page = PMA_DBI_fetch_assoc($page_rs)) {
274 . '<option value="' . $curr_page['page_nr'] . '"';
275 if (isset($chpage) && $chpage == $curr_page['page_nr']) {
276 echo ' selected="selected"';
278 echo '>' . $curr_page['page_nr'] . ': ' . htmlspecialchars($curr_page['page_descr']) . '</option>';
286 '1' => __('Delete'));
287 PMA_display_html_radio('action_choose', $choices, '0', false);
291 <fieldset
class="tblFooters">
292 <input type
="submit" value
="<?php echo __('Go'); ?>" /><br
/>
299 // Possibility to create a new page:
301 <form method
="post" action
="pdf_pages.php" name
="crpage">
304 <?php
echo __('Create a page') . "\n"; ?
>
306 <?php
echo PMA_generate_common_hidden_inputs($db, $table); ?
>
307 <input type
="hidden" name
="do" value
="createpage" />
310 <td
><label
for="id_newpage"><?php
echo __('Page name'); ?
></label
></td
>
311 <td
><input type
="text" name
="newpage" id
="id_newpage" size
="20" maxlength
="50" /></td
>
314 <td
><?php
echo __('Automatic layout based on'); ?
></td
>
316 <input type
="checkbox" name
="auto_layout_internal" id
="id_auto_layout_internal" /><label
for="id_auto_layout_internal"><?php
echo __('Internal relations'); ?
></label
><br
/>
318 if (PMA_StorageEngine
::isValid('InnoDB') || PMA_StorageEngine
::isValid('PBXT')) {
320 <input type
="checkbox" name
="auto_layout_foreign" id
="id_auto_layout_foreign" /><label
for="id_auto_layout_foreign">FOREIGN KEY
</label
><br
/>
327 <fieldset
class="tblFooters">
328 <input type
="submit" value
="<?php echo __('Go'); ?>" />
332 // Now if we already have chosen a page number then we should show the
334 if (isset($chpage) && $chpage > 0) {
339 <h2
><?php
echo __('Select Tables') ;?
></h2
>
342 $page_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
343 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
344 . ' AND pdf_page_number = \'' . PMA_sqlAddslashes($chpage) . '\'';
345 $page_rs = PMA_query_as_controluser($page_query, FALSE, $query_default_option);
346 $array_sh_page = array();
348 $reset_draginit = '';
350 while ($temp_sh_page = @PMA_DBI_fetch_assoc
($page_rs)) {
351 $array_sh_page[] = $temp_sh_page;
354 // Display WYSIWYG-PDF parts?
355 if ($cfg['WYSIWYG-PDF']) {
356 if (!isset($_POST['with_field_names']) && !isset($_POST['showwysiwyg'])) {
357 $with_field_names = TRUE;
360 <script type
="text/javascript" src
="./js/dom-drag.js"></script
>
361 <form method
="post" action
="pdf_pages.php" name
="dragdrop">
362 <input type
="button" name
="dragdrop" value
="<?php echo __('Toggle scratchboard'); ?>" onclick
="ToggleDragDrop('pdflayout');" />
363 <input type
="button" name
="dragdropreset" value
="<?php echo __('Reset'); ?>" onclick
="resetDrag();" />
365 <div id
="pdflayout" class="pdflayout" style
="visibility: hidden;">
367 foreach ($array_sh_page AS $key => $temp_sh_page) {
368 $drag_x = $temp_sh_page['x'];
369 $drag_y = $temp_sh_page['y'];
371 $draginit .= ' Drag.init(getElement("table_' . $i . '"), null, 0, parseInt(myid.style.width)-2, 0, parseInt(myid.style.height)-5);' . "\n";
372 $draginit .= ' getElement("table_' . $i . '").onDrag = function (x, y) { document.edcoord.elements["c_table_' . $i . '[x]"].value = parseInt(x); document.edcoord.elements["c_table_' . $i . '[y]"].value = parseInt(y) }' . "\n";
373 $draginit .= ' getElement("table_' . $i . '").style.left = "' . $drag_x . 'px";' . "\n";
374 $draginit .= ' getElement("table_' . $i . '").style.top = "' . $drag_y . 'px";' . "\n";
375 $reset_draginit .= ' getElement("table_' . $i . '").style.left = "2px";' . "\n";
376 $reset_draginit .= ' getElement("table_' . $i . '").style.top = "' . (15 * $i) . 'px";' . "\n";
377 $reset_draginit .= ' document.edcoord.elements["c_table_' . $i . '[x]"].value = "2"' . "\n";
378 $reset_draginit .= ' document.edcoord.elements["c_table_' . $i . '[y]"].value = "' . (15 * $i) . '"' . "\n";
380 $local_query = 'SHOW FIELDS FROM '
381 . PMA_backquote($temp_sh_page['table_name'])
382 . ' FROM ' . PMA_backquote($db);
383 $fields_rs = PMA_DBI_query($local_query);
385 $fields_cnt = PMA_DBI_num_rows($fields_rs);
387 echo '<div id="table_' . $i . '" class="pdflayout_table"><u>' . $temp_sh_page['table_name'] . '</u>';
388 if (isset($with_field_names)) {
389 while ($row = PMA_DBI_fetch_assoc($fields_rs)) {
390 echo '<br />' . htmlspecialchars($row['Field']) . "\n";
393 echo '</div>' . "\n";
394 PMA_DBI_free_result($fields_rs);
401 <script type
="text/javascript">
405 myid
= getElement('pdflayout');
406 <?php
echo $draginit; ?
>
409 function resetDrag() {
410 <?php
echo $reset_draginit; ?
>
415 } // end if WYSIWYG-PDF
418 <form method
="post" action
="pdf_pages.php" name
="edcoord">
419 <?php
echo PMA_generate_common_hidden_inputs($db, $table); ?
>
420 <input type
="hidden" name
="chpage" value
="<?php echo htmlspecialchars($chpage); ?>" />
421 <input type
="hidden" name
="do" value
="edcoord" />
424 <th
><?php
echo __('Table'); ?
></th
>
425 <th
><?php
echo __('Delete'); ?
></th
>
430 if (isset($ctable)) {
437 foreach ($array_sh_page AS $dummy_sh_page => $sh_page) {
438 $_mtab = $sh_page['table_name'];
439 $tabExist[$_mtab] = FALSE;
440 echo "\n" . ' <tr class="';
447 $odd_row != $odd_row;
449 . "\n" . ' <select name="c_table_' . $i . '[name]">';
450 foreach ($selectboxall AS $key => $value) {
451 echo "\n" . ' <option value="' . htmlspecialchars($value) . '"';
452 if ($value == $sh_page['table_name']) {
453 echo ' selected="selected"';
454 $tabExist[$_mtab] = TRUE;
456 echo '>' . htmlspecialchars($value) . '</option>';
458 echo "\n" . ' </select>'
461 . "\n" . ' <input type="checkbox" name="c_table_' . $i . '[delete]" value="y" />' . __('Delete');
462 echo "\n" . ' </td>';
464 . "\n" . ' <input type="text" ' . ($cfg['WYSIWYG-PDF'] ?
'onchange="dragPlace(' . $i . ', \'x\', this.value)"' : '') . ' name="c_table_' . $i . '[x]" value="' . $sh_page['x'] . '" />';
465 echo "\n" . ' </td>';
467 . "\n" . ' <input type="text" ' . ($cfg['WYSIWYG-PDF'] ?
'onchange="dragPlace(' . $i . ', \'y\', this.value)"' : '') . ' name="c_table_' . $i . '[y]" value="' . $sh_page['y'] . '" />';
468 echo "\n" . ' </td>';
469 echo "\n" . ' </tr>';
472 // Do one more empty row
473 echo "\n" . ' <tr class="';
479 $odd_row != $odd_row;
482 . "\n" . ' <select name="c_table_' . $i . '[name]">';
483 foreach ($selectboxall AS $key => $value) {
484 echo "\n" . ' <option value="' . htmlspecialchars($value) . '">' . htmlspecialchars($value) . '</option>';
486 echo "\n" . ' </select>'
489 . "\n" . ' <input type="checkbox" name="c_table_' . $i . '[delete]" value="y" />' . __('Delete');
490 echo "\n" . ' </td>';
492 . "\n" . ' <input type="text" name="c_table_' . $i . '[x]" value="' . (isset($sh_page['x'])?
$sh_page['x']:'') . '" />';
493 echo "\n" . ' </td>';
495 . "\n" . ' <input type="text" name="c_table_' . $i . '[y]" value="' . (isset($sh_page['y'])?
$sh_page['y']:'') . '" />';
496 echo "\n" . ' </td>';
497 echo "\n" . ' </tr>';
498 echo "\n" . ' </table>' . "\n";
500 echo "\n" . ' <input type="hidden" name="c_table_rows" value="' . ($i +
1) . '" />';
501 echo ($cfg['WYSIWYG-PDF'] ?
"\n" . ' <input type="hidden" id="showwysiwyg" name="showwysiwyg" value="' . ((isset($showwysiwyg) && $showwysiwyg == '1') ?
'1' : '0') . '" />' : '');
502 echo "\n" . ' <input type="checkbox" name="with_field_names" ' . (isset($with_field_names) ?
'checked="checked"' : ''). ' />' . __('Column names') . '<br />';
503 echo "\n" . ' <input type="submit" value="' . __('Save') . '" />';
504 echo "\n" . '</form>' . "\n\n";
507 // Check if there are tables that need to be deleted,
508 // if there are, ask the user for allowance
512 if (!empty($tabExist) && is_array($tabExist)) {
513 foreach ($tabExist AS $key => $value) {
515 $_strtrans .= '<input type="hidden" name="delrow[]" value="' . htmlspecialchars($key) . '" />' . "\n";
516 $_strname .= '<li>' . htmlspecialchars($key) . '</li>' . "\n";
521 echo '<form action="pdf_pages.php" method="post">' . "\n"
522 . PMA_generate_common_hidden_inputs($db, $table)
523 . '<input type="hidden" name="do" value="deleteCrap" />' . "\n"
524 . '<input type="hidden" name="chpage" value="' . htmlspecialchars($chpage) . '" />' . "\n"
525 . __('The current page has references to tables that no longer exist. Would you like to delete those references?')
530 . '<input type="submit" value="' . __('Go') . '" />' . "\n"
534 // ------------------------------------
535 // d i s p l a y p d f s c h e m a
536 // ------------------------------------
540 ||
($do == 'choosepage' && isset($chpage))
541 ||
($do == 'createpage' && isset($chpage)))) {
542 include('./libraries/display_pdf_schema.lib.php');
543 if ((isset($showwysiwyg) && $showwysiwyg == '1')) {
545 <script type
="text/javascript">
547 ToggleDragDrop('pdflayout');
553 } // end if ($cfgRelation['pdfwork'])
557 * Displays the footer
560 require_once './libraries/footer.inc.php';