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 $cfgRelation = PMA_getRelationsParam();
25 // This is to avoid "Command out of sync" errors. Before switching this to
26 // a value of 0 (for MYSQLI_USE_RESULT), please check the logic
27 // to free results wherever needed.
28 $query_default_option = PMA_DBI_QUERY_STORE
;
31 * Now in ./libraries/relation.lib.php we check for all tables
32 * that we need, but if we don't find them we are quiet about it
33 * so people can work without.
34 * This page is absolutely useless if you didn't set up your tables
35 * correctly, so it is a good place to see which tables we can and
38 if (!$cfgRelation['relwork']) {
39 echo sprintf(__('<b>%s</b> table not found or not set in %s'), 'relation', 'config.inc.php') . '<br />' . "\n"
40 . PMA_showDocu('relation') . "\n";
41 require './libraries/footer.inc.php';
44 if (!$cfgRelation['displaywork']) {
45 echo sprintf(__('<b>%s</b> table not found or not set in %s'), 'table_info', 'config.inc.php') . '<br />' . "\n"
46 . PMA_showDocu('table_info') . "\n";
47 require './libraries/footer.inc.php';
50 if (!isset($cfgRelation['table_coords'])){
51 echo sprintf(__('<b>%s</b> table not found or not set in %s'), 'table_coords', 'config.inc.php') . '<br />' . "\n"
52 . PMA_showDocu('table_coords') . "\n";
53 require './libraries/footer.inc.php';
55 if (!isset($cfgRelation['pdf_pages'])) {
56 echo sprintf(__('<b>%s</b> table not found or not set in %s'), 'pdf_page', 'config.inc.php') . '<br />' . "\n"
57 . PMA_showDocu('pdf_pages') . "\n";
58 require './libraries/footer.inc.php';
61 if ($cfgRelation['pdfwork']) {
62 // Now is the time to work on all changes
66 if ($action_choose=="1") {
67 $ch_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
68 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
69 . ' AND pdf_page_number = \'' . PMA_sqlAddslashes($chpage) . '\'';
70 PMA_query_as_controluser($ch_query, FALSE, $query_default_option);
72 $ch_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
73 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
74 . ' AND page_nr = \'' . PMA_sqlAddslashes($chpage) . '\'';
75 PMA_query_as_controluser($ch_query, FALSE, $query_default_option);
81 $pdf_page_number = PMA_REL_create_page($newpage, $cfgRelation, $db, $query_default_option);
83 // A u t o m a t i c l a y o u t
84 // ================================
85 if (isset($auto_layout_internal) ||
isset($auto_layout_foreign)) {
86 $all_tables = array();
89 if (isset($auto_layout_foreign)) {
90 // get the tables list
91 $tables = PMA_DBI_get_tables_full($db);
92 // find the ones who support FOREIGN KEY; it's not
93 // important that we group together InnoDB tables
94 // and PBXT tables, as this logic is just to put
95 // the tables on the layout, not to determine relations
96 $foreignkey_tables = array();
97 foreach($tables as $table_name => $table_properties) {
98 if (PMA_foreignkey_supported($table_properties['ENGINE'])) {
99 $foreignkey_tables[] = $table_name;
102 $all_tables = $foreignkey_tables;
103 // could be improved by finding the tables which have the
104 // most references keys and placing them at the beginning
105 // of the array (so that they are all center of schema)
106 unset($tables, $foreignkey_tables);
107 } // endif auto_layout_foreign
109 if (isset($auto_layout_internal)) {
110 // get the tables that have relations, by descending
112 $master_tables = 'SELECT COUNT(master_table), master_table'
113 . ' FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
114 . ' WHERE master_db = \'' . $db . '\''
115 . ' GROUP BY master_table'
116 . ' ORDER BY ' . PMA_backquote('COUNT(master_table)') . ' DESC ';
117 $master_tables_rs = PMA_query_as_controluser($master_tables, FALSE, $query_default_option);
118 if ($master_tables_rs && PMA_DBI_num_rows($master_tables_rs) > 0) {
119 // first put all the master tables at beginning
120 // of the list, so they are near the center of
122 while (list(, $master_table) = PMA_DBI_fetch_row($master_tables_rs)) {
123 $all_tables[] = $master_table;
126 // then for each master, add its foreigns into an array
127 // of foreign tables, if not already there
128 // (a foreign might be foreign for more than
129 // one table, and might be a master itself)
131 $foreign_tables = array();
132 foreach ($all_tables AS $master_table) {
133 $foreigners = PMA_getForeigners($db, $master_table);
134 foreach ($foreigners AS $foreigner) {
135 if (!in_array($foreigner['foreign_table'], $foreign_tables)) {
136 $foreign_tables[] = $foreigner['foreign_table'];
141 // then merge the arrays
142 foreach ($foreign_tables AS $foreign_table) {
143 if (!in_array($foreign_table, $all_tables)) {
144 $all_tables[] = $foreign_table;
147 } // endif there are master tables
148 } // endif auto_layout_internal
150 if (isset($auto_layout_internal) ||
isset($auto_layout_foreign)) {
151 // now generate the coordinates for the schema,
152 // in a clockwise spiral
158 $direction = "right";
159 foreach ($all_tables AS $current_table) {
161 // save current table's coordinates
162 $insert_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' '
163 . '(db_name, table_name, pdf_page_number, x, y) '
164 . 'VALUES (\'' . PMA_sqlAddslashes($db) . '\', \'' . PMA_sqlAddslashes($current_table) . '\',' . $pdf_page_number . ',' . $pos_x . ',' . $pos_y . ')';
165 PMA_query_as_controluser($insert_query, FALSE, $query_default_option);
167 // compute for the next table
168 switch ($direction) {
172 $delta *= $delta_mult;
177 $delta *= $delta_mult;
182 $delta *= $delta_mult;
186 $direction = "right";
187 $delta *= $delta_mult;
191 } // end if some auto-layout to do
193 $chpage = $pdf_page_number;
198 for ($i = 0; $i < $c_table_rows; $i++
) {
199 $arrvalue = 'c_table_' . $i;
200 $arrvalue = $
$arrvalue;
201 if (!isset($arrvalue['x']) ||
$arrvalue['x'] == '') {
204 if (!isset($arrvalue['y']) ||
$arrvalue['y'] == '') {
207 if (isset($arrvalue['name']) && $arrvalue['name'] != '--') {
208 $test_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
209 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
210 . ' AND table_name = \'' . PMA_sqlAddslashes($arrvalue['name']) . '\''
211 . ' AND pdf_page_number = \'' . PMA_sqlAddslashes($chpage) . '\'';
212 $test_rs = PMA_query_as_controluser($test_query, FALSE, $query_default_option);
213 if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) {
214 if (isset($arrvalue['delete']) && $arrvalue['delete'] == 'y') {
215 $ch_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
216 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
217 . ' AND table_name = \'' . PMA_sqlAddslashes($arrvalue['name']) . '\''
218 . ' AND pdf_page_number = \'' . PMA_sqlAddslashes($chpage) . '\'';
220 $ch_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' '
221 . 'SET x = ' . $arrvalue['x'] . ', y= ' . $arrvalue['y']
222 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
223 . ' AND table_name = \'' . PMA_sqlAddslashes($arrvalue['name']) . '\''
224 . ' AND pdf_page_number = \'' . PMA_sqlAddslashes($chpage) . '\'';
227 $ch_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' '
228 . '(db_name, table_name, pdf_page_number, x, y) '
229 . 'VALUES (\'' . PMA_sqlAddslashes($db) . '\', \'' . PMA_sqlAddslashes($arrvalue['name']) . '\', \'' . PMA_sqlAddslashes($chpage) . '\',' . $arrvalue['x'] . ',' . $arrvalue['y'] . ')';
231 PMA_query_as_controluser($ch_query, FALSE, $query_default_option);
236 foreach ($delrow AS $current_row) {
237 $d_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords']) . ' ' . "\n"
238 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'' . "\n"
239 . ' AND table_name = \'' . PMA_sqlAddslashes($current_row) . '\'' . "\n"
240 . ' AND pdf_page_number = \'' . PMA_sqlAddslashes($chpage) . '\'';
241 PMA_query_as_controluser($d_query, FALSE, $query_default_option);
245 } // end if (isset($do))
247 // We will need an array of all tables in this db
248 $selectboxall = array('--');
249 $alltab_rs = PMA_DBI_query('SHOW TABLES FROM ' . PMA_backquote($db) . ';', null, PMA_DBI_QUERY_STORE
);
250 while ($val = @PMA_DBI_fetch_row
($alltab_rs)) {
251 $selectboxall[] = $val[0];
254 // Now first show some possibility to choose a page for the pdf
255 $page_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
256 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'';
257 $page_rs = PMA_query_as_controluser($page_query, FALSE, $query_default_option);
259 if ($page_rs && PMA_DBI_num_rows($page_rs) > 0) {
261 <form method
="get" action
="pdf_pages.php" name
="selpage">
264 <?php
echo __('Please choose a page to edit') . "\n"; ?
>
266 <?php
echo PMA_generate_common_hidden_inputs($db, $table); ?
>
267 <input type
="hidden" name
="do" value
="choosepage" />
268 <select name
="chpage" onchange
="this.form.submit()">
270 while ($curr_page = PMA_DBI_fetch_assoc($page_rs)) {
272 . '<option value="' . $curr_page['page_nr'] . '"';
273 if (isset($chpage) && $chpage == $curr_page['page_nr']) {
274 echo ' selected="selected"';
276 echo '>' . $curr_page['page_nr'] . ': ' . htmlspecialchars($curr_page['page_descr']) . '</option>';
284 '1' => __('Delete'));
285 PMA_display_html_radio('action_choose', $choices, '0', false);
289 <fieldset
class="tblFooters">
290 <input type
="submit" value
="<?php echo __('Go'); ?>" /><br
/>
297 // Possibility to create a new page:
299 <form method
="post" action
="pdf_pages.php" name
="crpage">
302 <?php
echo __('Create a page') . "\n"; ?
>
304 <?php
echo PMA_generate_common_hidden_inputs($db, $table); ?
>
305 <input type
="hidden" name
="do" value
="createpage" />
308 <td
><label
for="id_newpage"><?php
echo __('Page name'); ?
></label
></td
>
309 <td
><input type
="text" name
="newpage" id
="id_newpage" size
="20" maxlength
="50" /></td
>
312 <td
><?php
echo __('Automatic layout based on'); ?
></td
>
314 <input type
="checkbox" name
="auto_layout_internal" id
="id_auto_layout_internal" /><label
for="id_auto_layout_internal"><?php
echo __('Internal relations'); ?
></label
><br
/>
316 if (PMA_StorageEngine
::isValid('InnoDB') || PMA_StorageEngine
::isValid('PBXT')) {
318 <input type
="checkbox" name
="auto_layout_foreign" id
="id_auto_layout_foreign" /><label
for="id_auto_layout_foreign">FOREIGN KEY
</label
><br
/>
325 <fieldset
class="tblFooters">
326 <input type
="submit" value
="<?php echo __('Go'); ?>" />
330 // Now if we already have chosen a page number then we should show the
332 if (isset($chpage) && $chpage > 0) {
337 <h2
><?php
echo __('Select Tables') ;?
></h2
>
340 $page_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
341 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
342 . ' AND pdf_page_number = \'' . PMA_sqlAddslashes($chpage) . '\'';
343 $page_rs = PMA_query_as_controluser($page_query, FALSE, $query_default_option);
344 $array_sh_page = array();
346 $reset_draginit = '';
348 while ($temp_sh_page = @PMA_DBI_fetch_assoc
($page_rs)) {
349 $array_sh_page[] = $temp_sh_page;
352 // Display WYSIWYG-PDF parts?
353 if ($cfg['WYSIWYG-PDF']) {
354 if (!isset($_POST['with_field_names']) && !isset($_POST['showwysiwyg'])) {
355 $with_field_names = TRUE;
358 <script type
="text/javascript" src
="./js/dom-drag.js"></script
>
359 <form method
="post" action
="pdf_pages.php" name
="dragdrop">
360 <input type
="button" name
="dragdrop" value
="<?php echo __('Toggle scratchboard'); ?>" onclick
="ToggleDragDrop('pdflayout');" />
361 <input type
="button" name
="dragdropreset" value
="<?php echo __('Reset'); ?>" onclick
="resetDrag();" />
363 <div id
="pdflayout" class="pdflayout" style
="visibility: hidden;">
365 foreach ($array_sh_page AS $key => $temp_sh_page) {
366 $drag_x = $temp_sh_page['x'];
367 $drag_y = $temp_sh_page['y'];
369 $draginit .= ' Drag.init(getElement("table_' . $i . '"), null, 0, parseInt(myid.style.width)-2, 0, parseInt(myid.style.height)-5);' . "\n";
370 $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";
371 $draginit .= ' getElement("table_' . $i . '").style.left = "' . $drag_x . 'px";' . "\n";
372 $draginit .= ' getElement("table_' . $i . '").style.top = "' . $drag_y . 'px";' . "\n";
373 $reset_draginit .= ' getElement("table_' . $i . '").style.left = "2px";' . "\n";
374 $reset_draginit .= ' getElement("table_' . $i . '").style.top = "' . (15 * $i) . 'px";' . "\n";
375 $reset_draginit .= ' document.edcoord.elements["c_table_' . $i . '[x]"].value = "2"' . "\n";
376 $reset_draginit .= ' document.edcoord.elements["c_table_' . $i . '[y]"].value = "' . (15 * $i) . '"' . "\n";
378 $local_query = 'SHOW FIELDS FROM '
379 . PMA_backquote($temp_sh_page['table_name'])
380 . ' FROM ' . PMA_backquote($db);
381 $fields_rs = PMA_DBI_query($local_query);
383 $fields_cnt = PMA_DBI_num_rows($fields_rs);
385 echo '<div id="table_' . $i . '" class="pdflayout_table"><u>' . $temp_sh_page['table_name'] . '</u>';
386 if (isset($with_field_names)) {
387 while ($row = PMA_DBI_fetch_assoc($fields_rs)) {
388 echo '<br />' . htmlspecialchars($row['Field']) . "\n";
391 echo '</div>' . "\n";
392 PMA_DBI_free_result($fields_rs);
399 <script type
="text/javascript">
403 myid
= getElement('pdflayout');
404 <?php
echo $draginit; ?
>
407 function resetDrag() {
408 <?php
echo $reset_draginit; ?
>
413 } // end if WYSIWYG-PDF
416 <form method
="post" action
="pdf_pages.php" name
="edcoord">
417 <?php
echo PMA_generate_common_hidden_inputs($db, $table); ?
>
418 <input type
="hidden" name
="chpage" value
="<?php echo htmlspecialchars($chpage); ?>" />
419 <input type
="hidden" name
="do" value
="edcoord" />
422 <th
><?php
echo __('Table'); ?
></th
>
423 <th
><?php
echo __('Delete'); ?
></th
>
428 if (isset($ctable)) {
435 foreach ($array_sh_page AS $dummy_sh_page => $sh_page) {
436 $_mtab = $sh_page['table_name'];
437 $tabExist[$_mtab] = FALSE;
438 echo "\n" . ' <tr class="';
445 $odd_row != $odd_row;
447 . "\n" . ' <select name="c_table_' . $i . '[name]">';
448 foreach ($selectboxall AS $key => $value) {
449 echo "\n" . ' <option value="' . htmlspecialchars($value) . '"';
450 if ($value == $sh_page['table_name']) {
451 echo ' selected="selected"';
452 $tabExist[$_mtab] = TRUE;
454 echo '>' . htmlspecialchars($value) . '</option>';
456 echo "\n" . ' </select>'
459 . "\n" . ' <input type="checkbox" name="c_table_' . $i . '[delete]" value="y" />' . __('Delete');
460 echo "\n" . ' </td>';
462 . "\n" . ' <input type="text" ' . ($cfg['WYSIWYG-PDF'] ?
'onchange="dragPlace(' . $i . ', \'x\', this.value)"' : '') . ' name="c_table_' . $i . '[x]" value="' . $sh_page['x'] . '" />';
463 echo "\n" . ' </td>';
465 . "\n" . ' <input type="text" ' . ($cfg['WYSIWYG-PDF'] ?
'onchange="dragPlace(' . $i . ', \'y\', this.value)"' : '') . ' name="c_table_' . $i . '[y]" value="' . $sh_page['y'] . '" />';
466 echo "\n" . ' </td>';
467 echo "\n" . ' </tr>';
470 // Do one more empty row
471 echo "\n" . ' <tr class="';
477 $odd_row != $odd_row;
480 . "\n" . ' <select name="c_table_' . $i . '[name]">';
481 foreach ($selectboxall AS $key => $value) {
482 echo "\n" . ' <option value="' . htmlspecialchars($value) . '">' . htmlspecialchars($value) . '</option>';
484 echo "\n" . ' </select>'
487 . "\n" . ' <input type="checkbox" name="c_table_' . $i . '[delete]" value="y" />' . __('Delete');
488 echo "\n" . ' </td>';
490 . "\n" . ' <input type="text" name="c_table_' . $i . '[x]" value="' . (isset($sh_page['x'])?
$sh_page['x']:'') . '" />';
491 echo "\n" . ' </td>';
493 . "\n" . ' <input type="text" name="c_table_' . $i . '[y]" value="' . (isset($sh_page['y'])?
$sh_page['y']:'') . '" />';
494 echo "\n" . ' </td>';
495 echo "\n" . ' </tr>';
496 echo "\n" . ' </table>' . "\n";
498 echo "\n" . ' <input type="hidden" name="c_table_rows" value="' . ($i +
1) . '" />';
499 echo ($cfg['WYSIWYG-PDF'] ?
"\n" . ' <input type="hidden" id="showwysiwyg" name="showwysiwyg" value="' . ((isset($showwysiwyg) && $showwysiwyg == '1') ?
'1' : '0') . '" />' : '');
500 echo "\n" . ' <input type="checkbox" name="with_field_names" ' . (isset($with_field_names) ?
'checked="checked"' : ''). ' />' . __('Column names') . '<br />';
501 echo "\n" . ' <input type="submit" value="' . __('Save') . '" />';
502 echo "\n" . '</form>' . "\n\n";
505 // Check if there are tables that need to be deleted,
506 // if there are, ask the user for allowance
510 if (!empty($tabExist) && is_array($tabExist)) {
511 foreach ($tabExist AS $key => $value) {
513 $_strtrans .= '<input type="hidden" name="delrow[]" value="' . htmlspecialchars($key) . '" />' . "\n";
514 $_strname .= '<li>' . htmlspecialchars($key) . '</li>' . "\n";
519 echo '<form action="pdf_pages.php" method="post">' . "\n"
520 . PMA_generate_common_hidden_inputs($db, $table)
521 . '<input type="hidden" name="do" value="deleteCrap" />' . "\n"
522 . '<input type="hidden" name="chpage" value="' . htmlspecialchars($chpage) . '" />' . "\n"
523 . __('The current page has references to tables that no longer exist. Would you like to delete those references?')
528 . '<input type="submit" value="' . __('Go') . '" />' . "\n"
532 // ------------------------------------
533 // d i s p l a y p d f s c h e m a
534 // ------------------------------------
538 ||
($do == 'choosepage' && isset($chpage))
539 ||
($do == 'createpage' && isset($chpage)))) {
540 include('./libraries/display_pdf_schema.lib.php');
541 if ((isset($showwysiwyg) && $showwysiwyg == '1')) {
543 <script type
="text/javascript">
545 ToggleDragDrop('pdflayout');
551 } // end if ($cfgRelation['pdfwork'])
555 * Displays the footer
558 require './libraries/footer.inc.php';