User_Schema.class.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Schema support library
  5. *
  6. * @package PhpMyAdmin-schema
  7. */
  8. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. require_once 'libraries/schema/Export_Relation_Schema.class.php';
  12. /**
  13. * This Class interacts with the user to gather the information
  14. * about their tables for which they want to export the relational schema
  15. * export options are shown to user from they can choose
  16. *
  17. * @package PhpMyAdmin-schema
  18. */
  19. class PMA_User_Schema
  20. {
  21. public $chosenPage;
  22. public $autoLayoutForeign;
  23. public $autoLayoutInternal;
  24. public $pageNumber;
  25. public $c_table_rows;
  26. public $action;
  27. /**
  28. * Sets action to be performed with schema.
  29. *
  30. * @param string $value action name
  31. *
  32. * @return void
  33. */
  34. public function setAction($value)
  35. {
  36. $this->action = $value;
  37. }
  38. /**
  39. * This function will process the user defined pages
  40. * and tables which will be exported as Relational schema
  41. * you can set the table positions on the paper via scratchboard
  42. * for table positions, put the x,y co-ordinates
  43. *
  44. * $this->action tells what the Schema is supposed to do
  45. * create and select a page, generate schema etc
  46. *
  47. * @access public
  48. * @return void
  49. */
  50. public function processUserChoice()
  51. {
  52. global $db, $cfgRelation;
  53. if (isset($this->action)) {
  54. switch ($this->action) {
  55. case 'selectpage':
  56. $this->chosenPage = $_REQUEST['chpage'];
  57. if ('1' == $_REQUEST['action_choose']) {
  58. $this->deleteCoordinates(
  59. $db,
  60. $cfgRelation,
  61. $this->chosenPage
  62. );
  63. $this->deletePages(
  64. $db,
  65. $cfgRelation,
  66. $this->chosenPage
  67. );
  68. $this->chosenPage = 0;
  69. }
  70. break;
  71. case 'createpage':
  72. $this->pageNumber = PMA_REL_createPage(
  73. $_POST['newpage'],
  74. $cfgRelation,
  75. $db
  76. );
  77. $this->autoLayoutForeign = isset($_POST['auto_layout_foreign'])
  78. ? "1"
  79. : null;
  80. $this->autoLayoutInternal = isset($_POST['auto_layout_internal'])
  81. ? "1"
  82. : null;
  83. $this->processRelations(
  84. $db,
  85. $this->pageNumber,
  86. $cfgRelation
  87. );
  88. break;
  89. case 'edcoord':
  90. $this->chosenPage = $_POST['chpage'];
  91. $this->c_table_rows = $_POST['c_table_rows'];
  92. $this->_editCoordinates($db, $cfgRelation);
  93. break;
  94. case 'delete_old_references':
  95. $this->_deleteTableRows(
  96. $_POST['delrow'],
  97. $cfgRelation,
  98. $db,
  99. $_POST['chpage']
  100. );
  101. break;
  102. case 'process_export':
  103. $this->_processExportSchema();
  104. break;
  105. } // end switch
  106. } // end if (isset($do))
  107. }
  108. /**
  109. * shows/displays the HTML FORM to create the page
  110. *
  111. * @param string $db name of the selected database
  112. *
  113. * @return void
  114. * @access public
  115. */
  116. public function showCreatePageDialog($db)
  117. {
  118. $htmlString = '<form method="post" action="schema_edit.php"'
  119. . ' name="frm_create_page">'
  120. . '<fieldset>'
  121. . '<legend>'
  122. . __('Create a page')
  123. . '</legend>'
  124. . PMA_URL_getHiddenInputs($db)
  125. . '<input type="hidden" name="do" value="createpage" />'
  126. . '<table>'
  127. . '<tr>'
  128. . '<td><label for="id_newpage">' . __('Page name') . '</label></td>'
  129. . '<td>'
  130. . '<input type="text" name="newpage" id="id_newpage"'
  131. . ' size="20" maxlength="50" />'
  132. . '</td>'
  133. . '</tr>'
  134. . '<tr>'
  135. . __('Automatic layout based on') . '</td>'
  136. . '<td>'
  137. . '<input type="checkbox" name="auto_layout_internal"'
  138. . ' id="id_auto_layout_internal" /><label for="id_auto_layout_internal">'
  139. . __('Internal relations') . '</label><br />';
  140. /*
  141. * Check to see whether INNODB and PBXT storage engines
  142. * are Available in MYSQL PACKAGE
  143. * If available, then provide AutoLayout for Foreign Keys in Schema View
  144. */
  145. if (PMA_StorageEngine::isValid('InnoDB')
  146. || PMA_StorageEngine::isValid('PBXT')
  147. ) {
  148. $htmlString .= '<input type="checkbox" name="auto_layout_foreign"'
  149. . ' id="id_auto_layout_foreign" />'
  150. . '<label for="id_auto_layout_foreign">'
  151. . __('FOREIGN KEY') . '</label><br />';
  152. }
  153. $htmlString .= '</td></tr>'
  154. . '</table>'
  155. . '</fieldset>'
  156. . '<fieldset class="tblFooters">'
  157. . '<input type="submit" value="' . __('Go') . '" />'
  158. . '</fieldset>'
  159. . '</form>';
  160. echo $htmlString;
  161. }
  162. /**
  163. * shows/displays the created page names in a drop down list
  164. * User can select any page number and edit it using dashboard etc
  165. *
  166. * @return void
  167. * @access public
  168. */
  169. public function selectPage()
  170. {
  171. global $db,$table,$cfgRelation;
  172. $page_query = 'SELECT * FROM '
  173. . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.'
  174. . PMA_Util::backquote($cfgRelation['pdf_pages'])
  175. . ' WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\'';
  176. $page_rs = PMA_queryAsControlUser(
  177. $page_query, false, PMA_DatabaseInterface::QUERY_STORE
  178. );
  179. if ($page_rs && $GLOBALS['dbi']->numRows($page_rs) > 0) {
  180. ?>
  181. <form method="get" action="schema_edit.php" name="frm_select_page">
  182. <fieldset>
  183. <legend>
  184. <?php echo __('Please choose a page to edit') . "\n"; ?>
  185. </legend>
  186. <?php echo PMA_URL_getHiddenInputs($db, $table); ?>
  187. <input type="hidden" name="do" value="selectpage" />
  188. <select name="chpage" id="chpage" class="autosubmit">
  189. <option value="0"><?php echo __('Select page'); ?></option>
  190. <?php
  191. while ($curr_page = $GLOBALS['dbi']->fetchAssoc($page_rs)) {
  192. echo "\n" . ' '
  193. . '<option value="' . $curr_page['page_nr'] . '"';
  194. if (isset($this->chosenPage)
  195. && $this->chosenPage == $curr_page['page_nr']
  196. ) {
  197. echo ' selected="selected"';
  198. }
  199. echo '>' . $curr_page['page_nr'] . ': '
  200. . htmlspecialchars($curr_page['page_descr']) . '</option>';
  201. } // end while
  202. echo "\n";
  203. ?>
  204. </select>
  205. <?php
  206. $choices = array(
  207. '0' => __('Edit'),
  208. '1' => __('Delete')
  209. );
  210. echo PMA_Util::getRadioFields(
  211. 'action_choose', $choices, '0', false
  212. );
  213. unset($choices);
  214. ?>
  215. </fieldset>
  216. <fieldset class="tblFooters">
  217. <input type="submit" value="<?php echo __('Go'); ?>" /><br />
  218. </fieldset>
  219. </form>
  220. <?php
  221. } // end IF
  222. echo "\n";
  223. } // end function
  224. /**
  225. * A dashboard is displayed to AutoLayout the position of tables
  226. * users can drag n drop the tables and change their positions
  227. *
  228. * @return void
  229. * @access public
  230. */
  231. public function showTableDashBoard()
  232. {
  233. global $db, $cfgRelation, $table, $with_field_names;
  234. /*
  235. * We will need an array of all tables in this db
  236. */
  237. $selectboxall = array('--');
  238. $alltab_rs = $GLOBALS['dbi']->query(
  239. 'SHOW TABLES FROM ' . PMA_Util::backquote($db) . ';',
  240. null,
  241. PMA_DatabaseInterface::QUERY_STORE
  242. );
  243. while ($val = @$GLOBALS['dbi']->fetchRow($alltab_rs)) {
  244. $selectboxall[] = $val[0];
  245. }
  246. $tabExist = array();
  247. /*
  248. * Now if we already have chosen a page number then we should
  249. * show the tables involved
  250. */
  251. if (isset($this->chosenPage) && $this->chosenPage > 0) {
  252. echo "\n";
  253. echo "<h2>" . __('Select Tables') . "</h2>";
  254. $page_query = 'SELECT * FROM '
  255. . PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
  256. . '.' . PMA_Util::backquote($cfgRelation['table_coords'])
  257. . ' WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\''
  258. . ' AND pdf_page_number = \''
  259. . PMA_Util::sqlAddSlashes($this->chosenPage) . '\'';
  260. $page_rs = PMA_queryAsControlUser($page_query, false);
  261. $array_sh_page = array();
  262. while ($temp_sh_page = @$GLOBALS['dbi']->fetchAssoc($page_rs)) {
  263. $array_sh_page[] = $temp_sh_page;
  264. }
  265. /*
  266. * Display WYSIWYG parts
  267. */
  268. if (! isset($_POST['with_field_names'])
  269. && ! isset($_POST['showwysiwyg'])
  270. ) {
  271. $with_field_names = true;
  272. } elseif (isset($_POST['with_field_names'])) {
  273. $with_field_names = true;
  274. }
  275. $this->_displayScratchboardTables($array_sh_page);
  276. echo '<form method="post" action="schema_edit.php" name="edcoord">';
  277. echo PMA_URL_getHiddenInputs($db, $table);
  278. echo '<input type="hidden" name="chpage" '
  279. . 'value="' . htmlspecialchars($this->chosenPage) . '" />';
  280. echo '<input type="hidden" name="do" value="edcoord" />';
  281. echo '<table>';
  282. echo '<tr>';
  283. echo '<th>' . __('Table') . '</th>';
  284. echo '<th>' . __('Delete') . '</th>';
  285. echo '<th>X</th>';
  286. echo '<th>Y</th>';
  287. echo '</tr>';
  288. if (isset($ctable)) {
  289. unset($ctable);
  290. }
  291. /*
  292. * Add one more empty row
  293. */
  294. $array_sh_page[] = array(
  295. 'table_name' => '',
  296. 'x' => '0',
  297. 'y' => '0',
  298. );
  299. $i = 0;
  300. $odd_row = true;
  301. foreach ($array_sh_page as $sh_page) {
  302. $_mtab = $sh_page['table_name'];
  303. if (! empty($_mtab)) {
  304. $tabExist[$_mtab] = false;
  305. }
  306. echo '<tr class="noclick ';
  307. if ($odd_row) {
  308. echo 'odd';
  309. } else {
  310. echo 'even';
  311. }
  312. echo '">';
  313. $odd_row = ! $odd_row;
  314. echo '<td>';
  315. echo '<select name="c_table_' . $i . '[name]">';
  316. foreach ($selectboxall as $value) {
  317. echo '<option value="' . htmlspecialchars($value) . '"';
  318. if (! empty($_mtab) && $value == $_mtab) {
  319. echo ' selected="selected"';
  320. $tabExist[$_mtab] = true;
  321. }
  322. echo '>' . htmlspecialchars($value) . '</option>';
  323. }
  324. echo '</select>';
  325. echo '</td>';
  326. echo '<td>';
  327. echo '<input type="checkbox" id="id_c_table_' . $i . '" '
  328. . 'name="c_table_' . $i . '[delete]" value="y" />';
  329. echo '<label for="id_c_table_' . $i . '">'
  330. . __('Delete') . '</label>';
  331. echo '</td>';
  332. echo '<td>';
  333. echo '<input type="text" class="position-change" data-axis="left" '
  334. . 'data-number="' . $i . '" id="c_table_' . $i . '_x" '
  335. . 'name="c_table_' . $i . '[x]" value="'
  336. . $sh_page['x'] . '" />';
  337. echo '</td>';
  338. echo '<td>';
  339. echo '<input type="text" class="position-change" data-axis="top" '
  340. . 'data-number="' . $i . '" id="c_table_' . $i . '_y" '
  341. . 'name="c_table_' . $i . '[y]" value="'
  342. . $sh_page['y'] . '" />';
  343. echo '</td>';
  344. echo '</tr>';
  345. $i++;
  346. }
  347. echo '</table>';
  348. echo '<input type="hidden" name="c_table_rows" value="' . $i . '" />';
  349. echo '<input type="hidden" id="showwysiwyg" name="showwysiwyg" value="'
  350. . ((isset($showwysiwyg) && $showwysiwyg == '1') ? '1' : '0')
  351. . '" />';
  352. echo '<input type="checkbox" id="id_with_field_names" '
  353. . 'name="with_field_names" '
  354. . (isset($with_field_names) ? 'checked="checked"' : '') . ' />';
  355. echo '<label for="id_with_field_names">'
  356. . __('Column names') . '</label><br />';
  357. echo '<input type="submit" value="' . __('Save') . '" />';
  358. echo '</form>' . "\n\n";
  359. } // end if
  360. if (isset($tabExist)) {
  361. $this->_deleteTables($db, $this->chosenPage, $tabExist);
  362. }
  363. }
  364. /**
  365. * show Export relational schema generation options
  366. * user can select export type of his own choice
  367. * and the attributes related to it
  368. *
  369. * @return void
  370. * @access public
  371. */
  372. public function displaySchemaGenerationOptions()
  373. {
  374. global $cfg, $db, $test_rs, $chpage;
  375. $htmlString = '<form method="post" action="schema_export.php"'
  376. . ' class="disableAjax">'
  377. . '<fieldset>'
  378. . '<legend>'
  379. . PMA_URL_getHiddenInputs($db);
  380. if (PMA_Util::showIcons('ActionLinksMode')) {
  381. $htmlString .= PMA_Util::getImage('b_views.png');
  382. }
  383. /*
  384. * TODO: This list should be generated dynamically based on list of
  385. * available plugins.
  386. */
  387. $htmlString .= __('Display relational schema')
  388. . ':'
  389. . '</legend>'
  390. . '<select name="export_type" id="export_type">';
  391. if (file_exists(TCPDF_INC)) {
  392. $htmlString .= '<option value="pdf" selected="selected">PDF</option>';
  393. }
  394. $htmlString .=
  395. '<option value="svg">SVG</option>'
  396. . '<option value="dia">DIA</option>'
  397. . '<option value="eps">EPS</option>'
  398. . '</select>'
  399. . '<label>' . __('Select Export Relational Type') . '</label><br />';
  400. if (isset($test_rs)) {
  401. $htmlString .= '<label for="pdf_page_number_opt">'
  402. . __('Page number:')
  403. . '</label>'
  404. . '<select name="pdf_page_number" id="pdf_page_number_opt">';
  405. while ($pages = @$GLOBALS['dbi']->fetchAssoc($test_rs)) {
  406. $htmlString .= '<option value="' . $pages['page_nr'] . '">'
  407. . $pages['page_nr'] . ': '
  408. . htmlspecialchars($pages['page_descr'])
  409. . '</option>' . "\n";
  410. } // end while
  411. $GLOBALS['dbi']->freeResult($test_rs);
  412. unset($test_rs);
  413. $htmlString .= '</select><br />';
  414. } else {
  415. $htmlString .= '<input type="hidden" name="pdf_page_number"'
  416. . ' value="' . htmlspecialchars($this->chosenPage) . '" />';
  417. }
  418. $htmlString .= '<input type="hidden" name="do" value="process_export" />'
  419. . '<input type="hidden" name="chpage" value="' . $chpage . '" />'
  420. . '<input type="checkbox" name="show_grid" id="show_grid_opt" />'
  421. . '<label for="show_grid_opt">' . __('Show grid') . '</label><br />'
  422. . '<input type="checkbox" name="show_color"'
  423. . ' id="show_color_opt" checked="checked" />'
  424. . '<label for="show_color_opt">' . __('Show color') . '</label>'
  425. . '<br />'
  426. . '<input type="checkbox" name="show_table_dimension"'
  427. . ' id="show_table_dim_opt" />'
  428. . '<label for="show_table_dim_opt">'
  429. . __('Show dimension of tables')
  430. . '</label><br />'
  431. . '<input type="checkbox" name="all_tables_same_width"'
  432. . ' id="all_tables_same_width" />'
  433. . '<label for="all_tables_same_width">'
  434. . __('Same width for all tables')
  435. . '</label><br />'
  436. . '<input type="checkbox" name="with_doc"'
  437. . ' id="with_doc" checked="checked" />'
  438. . '<label for="with_doc">' . __('Data Dictionary') . '</label><br />'
  439. . '<input type="checkbox" name="show_keys" id="show_keys" />'
  440. . '<label for="show_keys">' . __('Only show keys') . '</label><br />'
  441. . '<select name="orientation" id="orientation_opt" class="paper-change">'
  442. . '<option value="L">' . __('Landscape') . '</option>'
  443. . '<option value="P">' . __('Portrait') . '</option>'
  444. . '</select>'
  445. . '<label for="orientation_opt">' . __('Orientation') . '</label>'
  446. . '<br />'
  447. . '<select name="paper" id="paper_opt" class="paper-change">';
  448. foreach ($cfg['PDFPageSizes'] as $val) {
  449. $htmlString .= '<option value="' . $val . '"';
  450. if ($val == $cfg['PDFDefaultPageSize']) {
  451. $htmlString .= ' selected="selected"';
  452. }
  453. $htmlString .= ' >' . $val . '</option>' . "\n";
  454. }
  455. $htmlString .= '</select>'
  456. . '<label for="paper_opt">' . __('Paper size') . '</label>'
  457. . '</fieldset>'
  458. . '<fieldset class="tblFooters">'
  459. . '<input type="submit" value="' . __('Go') . '" />'
  460. . '</fieldset>'
  461. . '</form>';
  462. echo $htmlString;
  463. }
  464. /**
  465. * Check if there are tables that need to be deleted in dashboard,
  466. * if there are, ask the user for allowance
  467. *
  468. * @param string $db name of database selected
  469. * @param integer $chpage selected page
  470. * @param array $tabExist array of booleans
  471. *
  472. * @return void
  473. * @access private
  474. */
  475. private function _deleteTables($db, $chpage, $tabExist)
  476. {
  477. $_strtrans = '';
  478. $_strname = '';
  479. $shoot = false;
  480. if (empty($tabExist) || ! is_array($tabExist)) {
  481. return;
  482. }
  483. foreach ($tabExist as $key => $value) {
  484. if (! $value) {
  485. $_strtrans .= '<input type="hidden" name="delrow[]" value="'
  486. . htmlspecialchars($key) . '" />' . "\n";
  487. $_strname .= '<li>' . htmlspecialchars($key) . '</li>' . "\n";
  488. $shoot = true;
  489. }
  490. }
  491. if (! $shoot) {
  492. return;
  493. }
  494. echo '<br /><form action="schema_edit.php" method="post">' . "\n"
  495. . PMA_URL_getHiddenInputs($db)
  496. . '<input type="hidden" name="do" value="delete_old_references" />'
  497. . "\n"
  498. . '<input type="hidden" name="chpage" value="'
  499. . htmlspecialchars($chpage) . '" />' . "\n"
  500. . __(
  501. 'The current page has references to tables that no longer exist.'
  502. . ' Would you like to delete those references?'
  503. )
  504. . '<ul>' . "\n"
  505. . $_strname
  506. . '</ul>' . "\n"
  507. . $_strtrans
  508. . '<input type="submit" value="' . __('Go') . '" />' . "\n"
  509. . '</form>';
  510. }
  511. /**
  512. * Check if there are tables that need to be deleted in dashboard,
  513. * if there are, ask the user for allowance
  514. *
  515. * @param array $array_sh_page array of tables on page
  516. *
  517. * @return void
  518. * @access private
  519. */
  520. private function _displayScratchboardTables($array_sh_page)
  521. {
  522. global $with_field_names, $db;
  523. echo '<form method="post" action="schema_edit.php" name="dragdrop">';
  524. echo '<input type="button" name="dragdrop" id="toggle-dragdrop" '
  525. . 'value="' . __('Toggle scratchboard') . '" />';
  526. echo '<input type="button" name="dragdropreset" id="reset-dragdrop" '
  527. . 'value="' . __('Reset') . '" />';
  528. echo '</form>';
  529. echo '<div id="pdflayout" class="pdflayout" style="visibility: hidden;">';
  530. $i = 0;
  531. foreach ($array_sh_page as $temp_sh_page) {
  532. $drag_x = $temp_sh_page['x'];
  533. $drag_y = $temp_sh_page['y'];
  534. echo '<div id="table_' . $i . '" '
  535. . 'data-number="' . $i . '" '
  536. . 'data-x="' . $drag_x . '" '
  537. . 'data-y="' . $drag_y . '" '
  538. . 'class="pdflayout_table"'
  539. . '>'
  540. . '<u>'
  541. . htmlspecialchars($temp_sh_page['table_name'])
  542. . '</u>';
  543. if (isset($with_field_names)) {
  544. $fields = $GLOBALS['dbi']->getColumns(
  545. $db, $temp_sh_page['table_name']
  546. );
  547. // if the table has been dropped from outside phpMyAdmin,
  548. // we can no longer obtain its columns list
  549. if ($fields) {
  550. foreach ($fields as $row) {
  551. echo '<br />' . htmlspecialchars($row['Field']) . "\n";
  552. }
  553. }
  554. }
  555. echo '</div>' . "\n";
  556. $i++;
  557. }
  558. echo '</div>';
  559. }
  560. /**
  561. * delete the table rows with table co-ordinates
  562. *
  563. * @param int $delrow delete selected table from list of tables
  564. * @param array $cfgRelation relation settings
  565. * @param string $db database name
  566. * @param integer $chpage selected page for adding relations etc
  567. *
  568. * @return void
  569. * @access private
  570. */
  571. private function _deleteTableRows($delrow,$cfgRelation,$db,$chpage)
  572. {
  573. foreach ($delrow as $current_row) {
  574. $del_query = 'DELETE FROM '
  575. . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.'
  576. . PMA_Util::backquote($cfgRelation['table_coords']) . ' ' . "\n"
  577. . ' WHERE db_name = \''
  578. . PMA_Util::sqlAddSlashes($db) . '\'' . "\n"
  579. . ' AND table_name = \''
  580. . PMA_Util::sqlAddSlashes($current_row) . '\'' . "\n"
  581. . ' AND pdf_page_number = \''
  582. . PMA_Util::sqlAddSlashes($chpage) . '\'';
  583. PMA_queryAsControlUser($del_query, false);
  584. }
  585. }
  586. /**
  587. * get all the export options and verify
  588. * call and include the appropriate Schema Class depending on $export_type
  589. *
  590. * @return void
  591. * @access private
  592. */
  593. private function _processExportSchema()
  594. {
  595. /**
  596. * Settings for relation stuff
  597. */
  598. include_once './libraries/transformations.lib.php';
  599. include_once './libraries/Index.class.php';
  600. /**
  601. * default is PDF, otherwise validate it's only letters a-z
  602. */
  603. global $db,$export_type;
  604. if (! isset($export_type) || ! preg_match('/^[a-zA-Z]+$/', $export_type)) {
  605. $export_type = 'pdf';
  606. }
  607. $GLOBALS['dbi']->selectDb($db);
  608. $path = PMA_securePath(ucfirst($export_type));
  609. $filename = 'libraries/schema/' . $path . '_Relation_Schema.class.php';
  610. if (!file_exists($filename)) {
  611. PMA_Export_Relation_Schema::dieSchema(
  612. $_POST['chpage'],
  613. $export_type,
  614. __('File doesn\'t exist')
  615. );
  616. }
  617. $GLOBALS['skip_import'] = false;
  618. include $filename;
  619. if ( $GLOBALS['skip_import']) {
  620. PMA_Export_Relation_Schema::dieSchema(
  621. $_POST['chpage'],
  622. $export_type,
  623. __('Plugin is disabled')
  624. );
  625. }
  626. $class_name = 'PMA_' . $path . '_Relation_Schema';
  627. $obj_schema = new $class_name();
  628. $obj_schema->showOutput();
  629. }
  630. /**
  631. * delete X and Y coordinates
  632. *
  633. * @param string $db The database name
  634. * @param array $cfgRelation relation settings
  635. * @param integer $choosePage selected page for adding relations etc
  636. *
  637. * @return void
  638. * @access private
  639. */
  640. public function deleteCoordinates($db, $cfgRelation, $choosePage)
  641. {
  642. $query = 'DELETE FROM '
  643. . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.'
  644. . PMA_Util::backquote($cfgRelation['table_coords'])
  645. . ' WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\''
  646. . ' AND pdf_page_number = \''
  647. . PMA_Util::sqlAddSlashes($choosePage) . '\'';
  648. PMA_queryAsControlUser($query, false);
  649. }
  650. /**
  651. * delete pages
  652. *
  653. * @param string $db The database name
  654. * @param array $cfgRelation relation settings
  655. * @param integer $choosePage selected page for adding relations etc
  656. *
  657. * @return void
  658. * @access private
  659. */
  660. public function deletePages($db, $cfgRelation, $choosePage)
  661. {
  662. $query = 'DELETE FROM '
  663. . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.'
  664. . PMA_Util::backquote($cfgRelation['pdf_pages'])
  665. . ' WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\''
  666. . ' AND page_nr = \'' . PMA_Util::sqlAddSlashes($choosePage) . '\'';
  667. PMA_queryAsControlUser($query, false);
  668. }
  669. /**
  670. * process internal and foreign key relations
  671. *
  672. * @param string $db The database name
  673. * @param integer $pageNumber document number/Id
  674. * @param array $cfgRelation relation settings
  675. *
  676. * @return void
  677. * @access private
  678. */
  679. public function processRelations($db, $pageNumber, $cfgRelation)
  680. {
  681. /*
  682. * A u t o m a t i c l a y o u t
  683. *
  684. * There are 2 kinds of relations in PMA
  685. * 1) Internal Relations 2) Foreign Key Relations
  686. */
  687. if (isset($this->autoLayoutInternal) || isset($this->autoLayoutForeign)) {
  688. $all_tables = array();
  689. }
  690. if (isset($this->autoLayoutForeign)) {
  691. /*
  692. * get the tables list
  693. * who support FOREIGN KEY, it's not
  694. * important that we group together InnoDB tables
  695. * and PBXT tables, as this logic is just to put
  696. * the tables on the layout, not to determine relations
  697. */
  698. $tables = $GLOBALS['dbi']->getTablesFull($db);
  699. $foreignkey_tables = array();
  700. foreach ($tables as $table_name => $table_properties) {
  701. if (PMA_Util::isForeignKeySupported($table_properties['ENGINE'])) {
  702. $foreignkey_tables[] = $table_name;
  703. }
  704. }
  705. $all_tables = $foreignkey_tables;
  706. /*
  707. * could be improved by finding the tables which have the
  708. * most references keys and placing them at the beginning
  709. * of the array (so that they are all center of schema)
  710. */
  711. unset($tables, $foreignkey_tables);
  712. }
  713. if (isset($this->autoLayoutInternal)) {
  714. /*
  715. * get the tables list who support Internal Relations;
  716. * This type of relations will be created when
  717. * you setup the PMA tables correctly
  718. */
  719. $master_tables = 'SELECT COUNT(master_table), master_table'
  720. . ' FROM ' . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.'
  721. . PMA_Util::backquote($cfgRelation['relation'])
  722. . ' WHERE master_db = \'' . PMA_Util::sqlAddSlashes($db) . '\''
  723. . ' GROUP BY master_table'
  724. . ' ORDER BY COUNT(master_table) DESC';
  725. $master_tables_rs = PMA_queryAsControlUser(
  726. $master_tables, false, PMA_DatabaseInterface::QUERY_STORE
  727. );
  728. if ($master_tables_rs
  729. && $GLOBALS['dbi']->numRows($master_tables_rs) > 0
  730. ) {
  731. /* first put all the master tables at beginning
  732. * of the list, so they are near the center of
  733. * the schema
  734. */
  735. while (list(, $master_table)
  736. = $GLOBALS['dbi']->fetchRow($master_tables_rs)
  737. ) {
  738. $all_tables[] = $master_table;
  739. }
  740. /* Now for each master, add its foreigns into an array
  741. * of foreign tables, if not already there
  742. * (a foreign might be foreign for more than
  743. * one table, and might be a master itself)
  744. */
  745. $foreign_tables = array();
  746. foreach ($all_tables as $master_table) {
  747. $foreigners = PMA_getForeigners($db, $master_table);
  748. foreach ($foreigners as $foreigner) {
  749. if (! in_array(
  750. $foreigner['foreign_table'], $foreign_tables
  751. )) {
  752. $foreign_tables[] = $foreigner['foreign_table'];
  753. }
  754. }
  755. }
  756. /*
  757. * Now merge the master and foreign arrays/tables
  758. */
  759. foreach ($foreign_tables as $foreign_table) {
  760. if (! in_array($foreign_table, $all_tables)) {
  761. $all_tables[] = $foreign_table;
  762. }
  763. }
  764. }
  765. }
  766. if (isset($this->autoLayoutInternal) || isset($this->autoLayoutForeign)) {
  767. $this->addRelationCoordinates(
  768. $all_tables, $pageNumber, $db, $cfgRelation
  769. );
  770. }
  771. $this->chosenPage = $pageNumber;
  772. }
  773. /**
  774. * Add X and Y coordinates for a table
  775. *
  776. * @param array $all_tables A list of all tables involved
  777. * @param integer $pageNumber document number/Id
  778. * @param string $db The database name
  779. * @param array $cfgRelation relation settings
  780. *
  781. * @return void
  782. * @access private
  783. */
  784. public function addRelationCoordinates(
  785. $all_tables, $pageNumber, $db, $cfgRelation
  786. ) {
  787. /*
  788. * Now generate the coordinates for the schema
  789. * in a clockwise spiral and add to co-ordinates table
  790. */
  791. $pos_x = 300;
  792. $pos_y = 300;
  793. $delta = 110;
  794. $delta_mult = 1.10;
  795. $direction = "right";
  796. foreach ($all_tables as $current_table) {
  797. /*
  798. * save current table's coordinates
  799. */
  800. $insert_query = 'INSERT INTO '
  801. . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.'
  802. . PMA_Util::backquote($cfgRelation['table_coords']) . ' '
  803. . '(db_name, table_name, pdf_page_number, x, y) '
  804. . 'VALUES (\'' . PMA_Util::sqlAddSlashes($db) . '\', \''
  805. . PMA_Util::sqlAddSlashes($current_table) . '\',' . $pageNumber
  806. . ',' . $pos_x . ',' . $pos_y . ')';
  807. PMA_queryAsControlUser($insert_query, false);
  808. /*
  809. * compute for the next table
  810. */
  811. switch ($direction) {
  812. case 'right':
  813. $pos_x += $delta;
  814. $direction = "down";
  815. $delta *= $delta_mult;
  816. break;
  817. case 'down':
  818. $pos_y += $delta;
  819. $direction = "left";
  820. $delta *= $delta_mult;
  821. break;
  822. case 'left':
  823. $pos_x -= $delta;
  824. $direction = "up";
  825. $delta *= $delta_mult;
  826. break;
  827. case 'up':
  828. $pos_y -= $delta;
  829. $direction = "right";
  830. $delta *= $delta_mult;
  831. break;
  832. }
  833. }
  834. }
  835. /**
  836. * update X and Y coordinates for a table
  837. *
  838. * @param string $db The database name
  839. * @param array $cfgRelation relation settings
  840. *
  841. * @return void
  842. * @access private
  843. */
  844. private function _editCoordinates($db, $cfgRelation)
  845. {
  846. for ($i = 0; $i < $this->c_table_rows; $i++) {
  847. $arrvalue = $_POST['c_table_' . $i];
  848. if (! isset($arrvalue['x']) || $arrvalue['x'] == '') {
  849. $arrvalue['x'] = 0;
  850. }
  851. if (! isset($arrvalue['y']) || $arrvalue['y'] == '') {
  852. $arrvalue['y'] = 0;
  853. }
  854. if (isset($arrvalue['name']) && $arrvalue['name'] != '--') {
  855. $test_query = 'SELECT * FROM '
  856. . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.'
  857. . PMA_Util::backquote($cfgRelation['table_coords'])
  858. . ' WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\''
  859. . ' AND table_name = \''
  860. . PMA_Util::sqlAddSlashes($arrvalue['name']) . '\''
  861. . ' AND pdf_page_number = \''
  862. . PMA_Util::sqlAddSlashes($this->chosenPage) . '\'';
  863. $test_rs = PMA_queryAsControlUser(
  864. $test_query, false, PMA_DatabaseInterface::QUERY_STORE
  865. );
  866. //echo $test_query;
  867. if ($test_rs && $GLOBALS['dbi']->numRows($test_rs) > 0) {
  868. if (isset($arrvalue['delete']) && $arrvalue['delete'] == 'y') {
  869. $ch_query = 'DELETE FROM '
  870. . PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
  871. . '.'
  872. . PMA_Util::backquote($cfgRelation['table_coords'])
  873. . ' WHERE db_name = \''
  874. . PMA_Util::sqlAddSlashes($db) . '\''
  875. . ' AND table_name = \''
  876. . PMA_Util::sqlAddSlashes($arrvalue['name']) . '\''
  877. . ' AND pdf_page_number = \''
  878. . PMA_Util::sqlAddSlashes($this->chosenPage) . '\'';
  879. } else {
  880. $ch_query = 'UPDATE '
  881. . PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
  882. . '.' . PMA_Util::backquote($cfgRelation['table_coords'])
  883. . ' '
  884. . 'SET x = ' . $arrvalue['x'] . ', y= ' . $arrvalue['y']
  885. . ' WHERE db_name = \''
  886. . PMA_Util::sqlAddSlashes($db) . '\''
  887. . ' AND table_name = \''
  888. . PMA_Util::sqlAddSlashes($arrvalue['name']) . '\''
  889. . ' AND pdf_page_number = \''
  890. . PMA_Util::sqlAddSlashes($this->chosenPage) . '\'';
  891. }
  892. } else {
  893. $ch_query = 'INSERT INTO '
  894. . PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
  895. . '.' . PMA_Util::backquote($cfgRelation['table_coords'])
  896. . ' '
  897. . '(db_name, table_name, pdf_page_number, x, y) '
  898. . 'VALUES (\'' . PMA_Util::sqlAddSlashes($db) . '\', \''
  899. . PMA_Util::sqlAddSlashes($arrvalue['name']) . '\', \''
  900. . PMA_Util::sqlAddSlashes($this->chosenPage) . '\','
  901. . $arrvalue['x'] . ',' . $arrvalue['y'] . ')';
  902. }
  903. //echo $ch_query;
  904. PMA_queryAsControlUser($ch_query, false);
  905. } // end if
  906. } // end for
  907. }
  908. }
  909. ?>