2 /* vim: set expandtab sw=4 ts=4 sts=4: */
9 * Gets some core libraries
11 require_once './libraries/common.inc.php';
12 require_once './libraries/db_common.inc.php';
13 require './libraries/StorageEngine.class.php';
15 $active_page = 'db_operations.php';
16 require_once './libraries/db_common.inc.php';
17 $url_query .= '&goto=pdf_pages.php';
18 require_once './libraries/db_info.inc.php';
21 * Settings for relation stuff
23 require_once './libraries/relation.lib.php';
24 $cfgRelation = PMA_getRelationsParam();
26 // This is to avoid "Command out of sync" errors. Before switching this to
27 // a value of 0 (for MYSQLI_USE_RESULT), please check the logic
28 // to free results wherever needed.
29 $query_default_option = PMA_DBI_QUERY_STORE
;
32 * Now in ./libraries/relation.lib.php we check for all tables
33 * that we need, but if we don't find them we are quiet about it
34 * so people can work without.
35 * This page is absolutely useless if you didn't set up your tables
36 * correctly, so it is a good place to see which tables we can and
39 if (!$cfgRelation['relwork']) {
40 echo sprintf(__('<b>%s</b> table not found or not set in %s'), 'relation', 'config.inc.php') . '<br />' . "\n"
41 . PMA_showDocu('relation') . "\n";
42 require_once './libraries/footer.inc.php';
45 if (!$cfgRelation['displaywork']) {
46 echo sprintf(__('<b>%s</b> table not found or not set in %s'), 'table_info', 'config.inc.php') . '<br />' . "\n"
47 . PMA_showDocu('table_info') . "\n";
48 require_once './libraries/footer.inc.php';
51 if (!isset($cfgRelation['table_coords'])){
52 echo sprintf(__('<b>%s</b> table not found or not set in %s'), 'table_coords', 'config.inc.php') . '<br />' . "\n"
53 . PMA_showDocu('table_coords') . "\n";
54 require_once './libraries/footer.inc.php';
56 if (!isset($cfgRelation['pdf_pages'])) {
57 echo sprintf(__('<b>%s</b> table not found or not set in %s'), 'pdf_page', 'config.inc.php') . '<br />' . "\n"
58 . PMA_showDocu('pdf_pages') . "\n";
59 require_once './libraries/footer.inc.php';
62 if ($cfgRelation['pdfwork']) {
63 // Now is the time to work on all changes
67 if ($action_choose=="1") {
68 $ch_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
69 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
70 . ' AND pdf_page_number = \'' . PMA_sqlAddslashes($chpage) . '\'';
71 PMA_query_as_controluser($ch_query, FALSE, $query_default_option);
73 $ch_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
74 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
75 . ' AND page_nr = \'' . PMA_sqlAddslashes($chpage) . '\'';
76 PMA_query_as_controluser($ch_query, FALSE, $query_default_option);
82 $pdf_page_number = PMA_REL_create_page($newpage, $cfgRelation, $db, $query_default_option);
84 // A u t o m a t i c l a y o u t
85 // ================================
86 if (isset($auto_layout_internal) ||
isset($auto_layout_foreign)) {
87 $all_tables = array();
90 if (isset($auto_layout_foreign)) {
91 // get the tables list
92 $tables = PMA_DBI_get_tables_full($db);
93 // find the ones who support FOREIGN KEY; it's not
94 // important that we group together InnoDB tables
95 // and PBXT tables, as this logic is just to put
96 // the tables on the layout, not to determine relations
97 $foreignkey_tables = array();
98 foreach($tables as $table_name => $table_properties) {
99 if (PMA_foreignkey_supported($table_properties['ENGINE'])) {
100 $foreignkey_tables[] = $table_name;
103 $all_tables = $foreignkey_tables;
104 // could be improved by finding the tables which have the
105 // most references keys and placing them at the beginning
106 // of the array (so that they are all center of schema)
107 unset($tables, $foreignkey_tables);
108 } // endif auto_layout_foreign
110 if (isset($auto_layout_internal)) {
111 // get the tables that have relations, by descending
113 $master_tables = 'SELECT COUNT(master_table), master_table'
114 . ' FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
115 . ' WHERE master_db = \'' . $db . '\''
116 . ' GROUP BY master_table'
117 . ' ORDER BY ' . PMA_backquote('COUNT(master_table)') . ' DESC ';
118 $master_tables_rs = PMA_query_as_controluser($master_tables, FALSE, $query_default_option);
119 if ($master_tables_rs && PMA_DBI_num_rows($master_tables_rs) > 0) {
120 // first put all the master tables at beginning
121 // of the list, so they are near the center of
123 while (list(, $master_table) = PMA_DBI_fetch_row($master_tables_rs)) {
124 $all_tables[] = $master_table;
127 // then for each master, add its foreigns into an array
128 // of foreign tables, if not already there
129 // (a foreign might be foreign for more than
130 // one table, and might be a master itself)
132 $foreign_tables = array();
133 foreach ($all_tables AS $master_table) {
134 $foreigners = PMA_getForeigners($db, $master_table);
135 foreach ($foreigners AS $foreigner) {
136 if (!in_array($foreigner['foreign_table'], $foreign_tables)) {
137 $foreign_tables[] = $foreigner['foreign_table'];
142 // then merge the arrays
143 foreach ($foreign_tables AS $foreign_table) {
144 if (!in_array($foreign_table, $all_tables)) {
145 $all_tables[] = $foreign_table;
148 } // endif there are master tables
149 } // endif auto_layout_internal
151 if (isset($auto_layout_internal) ||
isset($auto_layout_foreign)) {
152 // now generate the coordinates for the schema,
153 // in a clockwise spiral
159 $direction = "right";
160 foreach ($all_tables AS $current_table) {
162 // save current table's coordinates
163 $insert_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' '
164 . '(db_name, table_name, pdf_page_number, x, y) '
165 . 'VALUES (\'' . PMA_sqlAddslashes($db) . '\', \'' . PMA_sqlAddslashes($current_table) . '\',' . $pdf_page_number . ',' . $pos_x . ',' . $pos_y . ')';
166 PMA_query_as_controluser($insert_query, FALSE, $query_default_option);
168 // compute for the next table
169 switch ($direction) {
173 $delta *= $delta_mult;
178 $delta *= $delta_mult;
183 $delta *= $delta_mult;
187 $direction = "right";
188 $delta *= $delta_mult;
192 } // end if some auto-layout to do
194 $chpage = $pdf_page_number;
199 for ($i = 0; $i < $c_table_rows; $i++
) {
200 $arrvalue = 'c_table_' . $i;
201 $arrvalue = $
$arrvalue;
202 if (!isset($arrvalue['x']) ||
$arrvalue['x'] == '') {
205 if (!isset($arrvalue['y']) ||
$arrvalue['y'] == '') {
208 if (isset($arrvalue['name']) && $arrvalue['name'] != '--') {
209 $test_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
210 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
211 . ' AND table_name = \'' . PMA_sqlAddslashes($arrvalue['name']) . '\''
212 . ' AND pdf_page_number = \'' . PMA_sqlAddslashes($chpage) . '\'';
213 $test_rs = PMA_query_as_controluser($test_query, FALSE, $query_default_option);
214 if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) {
215 if (isset($arrvalue['delete']) && $arrvalue['delete'] == 'y') {
216 $ch_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
217 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
218 . ' AND table_name = \'' . PMA_sqlAddslashes($arrvalue['name']) . '\''
219 . ' AND pdf_page_number = \'' . PMA_sqlAddslashes($chpage) . '\'';
221 $ch_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' '
222 . 'SET x = ' . $arrvalue['x'] . ', y= ' . $arrvalue['y']
223 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
224 . ' AND table_name = \'' . PMA_sqlAddslashes($arrvalue['name']) . '\''
225 . ' AND pdf_page_number = \'' . PMA_sqlAddslashes($chpage) . '\'';
228 $ch_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' '
229 . '(db_name, table_name, pdf_page_number, x, y) '
230 . 'VALUES (\'' . PMA_sqlAddslashes($db) . '\', \'' . PMA_sqlAddslashes($arrvalue['name']) . '\', \'' . PMA_sqlAddslashes($chpage) . '\',' . $arrvalue['x'] . ',' . $arrvalue['y'] . ')';
232 PMA_query_as_controluser($ch_query, FALSE, $query_default_option);
237 foreach ($delrow AS $current_row) {
238 $d_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' ' . "\n"
239 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'' . "\n"
240 . ' AND table_name = \'' . PMA_sqlAddslashes($current_row) . '\'' . "\n"
241 . ' AND pdf_page_number = \'' . PMA_sqlAddslashes($chpage) . '\'';
242 PMA_query_as_controluser($d_query, FALSE, $query_default_option);
246 } // end if (isset($do))
248 // We will need an array of all tables in this db
249 $selectboxall = array('--');
250 $alltab_rs = PMA_DBI_query('SHOW TABLES FROM ' . PMA_backquote($db) . ';', null, PMA_DBI_QUERY_STORE
);
251 while ($val = @PMA_DBI_fetch_row
($alltab_rs)) {
252 $selectboxall[] = $val[0];
255 // Now first show some possibility to choose a page for the pdf
256 $page_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
257 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'';
258 $page_rs = PMA_query_as_controluser($page_query, FALSE, $query_default_option);
260 if ($page_rs && PMA_DBI_num_rows($page_rs) > 0) {
262 <form method
="get" action
="pdf_pages.php" name
="selpage">
265 <?php
echo __('Please choose a page to edit') . "\n"; ?
>
267 <?php
echo PMA_generate_common_hidden_inputs($db, $table); ?
>
268 <input type
="hidden" name
="do" value
="choosepage" />
269 <select name
="chpage" onchange
="this.form.submit()">
271 while ($curr_page = PMA_DBI_fetch_assoc($page_rs)) {
273 . '<option value="' . $curr_page['page_nr'] . '"';
274 if (isset($chpage) && $chpage == $curr_page['page_nr']) {
275 echo ' selected="selected"';
277 echo '>' . $curr_page['page_nr'] . ': ' . htmlspecialchars($curr_page['page_descr']) . '</option>';
285 '1' => __('Delete'));
286 PMA_display_html_radio('action_choose', $choices, '0', false);
290 <fieldset
class="tblFooters">
291 <input type
="submit" value
="<?php echo __('Go'); ?>" /><br
/>
298 // Possibility to create a new page:
300 <form method
="post" action
="pdf_pages.php" name
="crpage">
303 <?php
echo __('Create a page') . "\n"; ?
>
305 <?php
echo PMA_generate_common_hidden_inputs($db, $table); ?
>
306 <input type
="hidden" name
="do" value
="createpage" />
309 <td
><label
for="id_newpage"><?php
echo __('Page name'); ?
></label
></td
>
310 <td
><input type
="text" name
="newpage" id
="id_newpage" size
="20" maxlength
="50" /></td
>
313 <td
><?php
echo __('Automatic layout based on'); ?
></td
>
315 <input type
="checkbox" name
="auto_layout_internal" id
="id_auto_layout_internal" /><label
for="id_auto_layout_internal"><?php
echo __('Internal relations'); ?
></label
><br
/>
317 if (PMA_StorageEngine
::isValid('InnoDB') || PMA_StorageEngine
::isValid('PBXT')) {
319 <input type
="checkbox" name
="auto_layout_foreign" id
="id_auto_layout_foreign" /><label
for="id_auto_layout_foreign">FOREIGN KEY
</label
><br
/>
326 <fieldset
class="tblFooters">
327 <input type
="submit" value
="<?php echo __('Go'); ?>" />
331 // Now if we already have chosen a page number then we should show the
333 if (isset($chpage) && $chpage > 0) {
338 <h2
><?php
echo __('Select Tables') ;?
></h2
>
341 $page_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
342 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
343 . ' AND pdf_page_number = \'' . PMA_sqlAddslashes($chpage) . '\'';
344 $page_rs = PMA_query_as_controluser($page_query, FALSE, $query_default_option);
345 $array_sh_page = array();
347 $reset_draginit = '';
349 while ($temp_sh_page = @PMA_DBI_fetch_assoc
($page_rs)) {
350 $array_sh_page[] = $temp_sh_page;
353 // Display WYSIWYG-PDF parts?
354 if ($cfg['WYSIWYG-PDF']) {
355 if (!isset($_POST['with_field_names']) && !isset($_POST['showwysiwyg'])) {
356 $with_field_names = TRUE;
359 <script type
="text/javascript" src
="./js/dom-drag.js"></script
>
360 <form method
="post" action
="pdf_pages.php" name
="dragdrop">
361 <input type
="button" name
="dragdrop" value
="<?php echo __('Toggle scratchboard'); ?>" onclick
="ToggleDragDrop('pdflayout');" />
362 <input type
="button" name
="dragdropreset" value
="<?php echo __('Reset'); ?>" onclick
="resetDrag();" />
364 <div id
="pdflayout" class="pdflayout" style
="visibility: hidden;">
366 foreach ($array_sh_page AS $key => $temp_sh_page) {
367 $drag_x = $temp_sh_page['x'];
368 $drag_y = $temp_sh_page['y'];
370 $draginit .= ' Drag.init(getElement("table_' . $i . '"), null, 0, parseInt(myid.style.width)-2, 0, parseInt(myid.style.height)-5);' . "\n";
371 $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";
372 $draginit .= ' getElement("table_' . $i . '").style.left = "' . $drag_x . 'px";' . "\n";
373 $draginit .= ' getElement("table_' . $i . '").style.top = "' . $drag_y . 'px";' . "\n";
374 $reset_draginit .= ' getElement("table_' . $i . '").style.left = "2px";' . "\n";
375 $reset_draginit .= ' getElement("table_' . $i . '").style.top = "' . (15 * $i) . 'px";' . "\n";
376 $reset_draginit .= ' document.edcoord.elements["c_table_' . $i . '[x]"].value = "2"' . "\n";
377 $reset_draginit .= ' document.edcoord.elements["c_table_' . $i . '[y]"].value = "' . (15 * $i) . '"' . "\n";
379 $local_query = 'SHOW FIELDS FROM '
380 . PMA_backquote($temp_sh_page['table_name'])
381 . ' FROM ' . PMA_backquote($db);
382 $fields_rs = PMA_DBI_query($local_query);
384 $fields_cnt = PMA_DBI_num_rows($fields_rs);
386 echo '<div id="table_' . $i . '" class="pdflayout_table"><u>' . $temp_sh_page['table_name'] . '</u>';
387 if (isset($with_field_names)) {
388 while ($row = PMA_DBI_fetch_assoc($fields_rs)) {
389 echo '<br />' . htmlspecialchars($row['Field']) . "\n";
392 echo '</div>' . "\n";
393 PMA_DBI_free_result($fields_rs);
400 <script type
="text/javascript">
404 myid
= getElement('pdflayout');
405 <?php
echo $draginit; ?
>
408 function resetDrag() {
409 <?php
echo $reset_draginit; ?
>
414 } // end if WYSIWYG-PDF
417 <form method
="post" action
="pdf_pages.php" name
="edcoord">
418 <?php
echo PMA_generate_common_hidden_inputs($db, $table); ?
>
419 <input type
="hidden" name
="chpage" value
="<?php echo htmlspecialchars($chpage); ?>" />
420 <input type
="hidden" name
="do" value
="edcoord" />
423 <th
><?php
echo __('Table'); ?
></th
>
424 <th
><?php
echo __('Delete'); ?
></th
>
429 if (isset($ctable)) {
436 foreach ($array_sh_page AS $dummy_sh_page => $sh_page) {
437 $_mtab = $sh_page['table_name'];
438 $tabExist[$_mtab] = FALSE;
439 echo "\n" . ' <tr class="';
446 $odd_row != $odd_row;
448 . "\n" . ' <select name="c_table_' . $i . '[name]">';
449 foreach ($selectboxall AS $key => $value) {
450 echo "\n" . ' <option value="' . htmlspecialchars($value) . '"';
451 if ($value == $sh_page['table_name']) {
452 echo ' selected="selected"';
453 $tabExist[$_mtab] = TRUE;
455 echo '>' . htmlspecialchars($value) . '</option>';
457 echo "\n" . ' </select>'
460 . "\n" . ' <input type="checkbox" name="c_table_' . $i . '[delete]" value="y" />' . __('Delete');
461 echo "\n" . ' </td>';
463 . "\n" . ' <input type="text" ' . ($cfg['WYSIWYG-PDF'] ?
'onchange="dragPlace(' . $i . ', \'x\', this.value)"' : '') . ' name="c_table_' . $i . '[x]" value="' . $sh_page['x'] . '" />';
464 echo "\n" . ' </td>';
466 . "\n" . ' <input type="text" ' . ($cfg['WYSIWYG-PDF'] ?
'onchange="dragPlace(' . $i . ', \'y\', this.value)"' : '') . ' name="c_table_' . $i . '[y]" value="' . $sh_page['y'] . '" />';
467 echo "\n" . ' </td>';
468 echo "\n" . ' </tr>';
471 // Do one more empty row
472 echo "\n" . ' <tr class="';
478 $odd_row != $odd_row;
481 . "\n" . ' <select name="c_table_' . $i . '[name]">';
482 foreach ($selectboxall AS $key => $value) {
483 echo "\n" . ' <option value="' . htmlspecialchars($value) . '">' . htmlspecialchars($value) . '</option>';
485 echo "\n" . ' </select>'
488 . "\n" . ' <input type="checkbox" name="c_table_' . $i . '[delete]" value="y" />' . __('Delete');
489 echo "\n" . ' </td>';
491 . "\n" . ' <input type="text" name="c_table_' . $i . '[x]" value="' . (isset($sh_page['x'])?
$sh_page['x']:'') . '" />';
492 echo "\n" . ' </td>';
494 . "\n" . ' <input type="text" name="c_table_' . $i . '[y]" value="' . (isset($sh_page['y'])?
$sh_page['y']:'') . '" />';
495 echo "\n" . ' </td>';
496 echo "\n" . ' </tr>';
497 echo "\n" . ' </table>' . "\n";
499 echo "\n" . ' <input type="hidden" name="c_table_rows" value="' . ($i +
1) . '" />';
500 echo ($cfg['WYSIWYG-PDF'] ?
"\n" . ' <input type="hidden" id="showwysiwyg" name="showwysiwyg" value="' . ((isset($showwysiwyg) && $showwysiwyg == '1') ?
'1' : '0') . '" />' : '');
501 echo "\n" . ' <input type="checkbox" name="with_field_names" ' . (isset($with_field_names) ?
'checked="checked"' : ''). ' />' . __('Column names') . '<br />';
502 echo "\n" . ' <input type="submit" value="' . __('Save') . '" />';
503 echo "\n" . '</form>' . "\n\n";
506 // Check if there are tables that need to be deleted,
507 // if there are, ask the user for allowance
511 if (!empty($tabExist) && is_array($tabExist)) {
512 foreach ($tabExist AS $key => $value) {
514 $_strtrans .= '<input type="hidden" name="delrow[]" value="' . htmlspecialchars($key) . '" />' . "\n";
515 $_strname .= '<li>' . htmlspecialchars($key) . '</li>' . "\n";
520 echo '<form action="pdf_pages.php" method="post">' . "\n"
521 . PMA_generate_common_hidden_inputs($db, $table)
522 . '<input type="hidden" name="do" value="deleteCrap" />' . "\n"
523 . '<input type="hidden" name="chpage" value="' . htmlspecialchars($chpage) . '" />' . "\n"
524 . __('The current page has references to tables that no longer exist. Would you like to delete those references?')
529 . '<input type="submit" value="' . __('Go') . '" />' . "\n"
533 // ------------------------------------
534 // d i s p l a y p d f s c h e m a
535 // ------------------------------------
539 ||
($do == 'choosepage' && isset($chpage))
540 ||
($do == 'createpage' && isset($chpage)))) {
541 include('./libraries/display_pdf_schema.lib.php');
542 if ((isset($showwysiwyg) && $showwysiwyg == '1')) {
544 <script type
="text/javascript">
546 ToggleDragDrop('pdflayout');
552 } // end if ($cfgRelation['pdfwork'])
556 * Displays the footer
559 require_once './libraries/footer.inc.php';