Designer.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Holds the PhpMyAdmin\Database\Designer class
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. namespace PhpMyAdmin\Database;
  9. use PhpMyAdmin\DatabaseInterface;
  10. use PhpMyAdmin\Message;
  11. use PhpMyAdmin\Plugins;
  12. use PhpMyAdmin\Plugins\SchemaPlugin;
  13. use PhpMyAdmin\Relation;
  14. use PhpMyAdmin\Template;
  15. use PhpMyAdmin\Util;
  16. /**
  17. * Set of functions related to database designer
  18. *
  19. * @package PhpMyAdmin
  20. */
  21. class Designer
  22. {
  23. /**
  24. * @var Relation $relation
  25. */
  26. private $relation;
  27. /**
  28. * Constructor
  29. */
  30. public function __construct()
  31. {
  32. $this->relation = new Relation();
  33. }
  34. /**
  35. * Function to get html for displaying the page edit/delete form
  36. *
  37. * @param string $db database name
  38. * @param string $operation 'edit' or 'delete' depending on the operation
  39. *
  40. * @return string html content
  41. */
  42. public function getHtmlForEditOrDeletePages($db, $operation)
  43. {
  44. $cfgRelation = $this->relation->getRelationsParam();
  45. return Template::get('database/designer/edit_delete_pages')->render([
  46. 'db' => $db,
  47. 'operation' => $operation,
  48. 'pdfwork' => $cfgRelation['pdfwork'],
  49. 'pages' => $this->getPageIdsAndNames($db),
  50. ]);
  51. }
  52. /**
  53. * Function to get html for displaying the page save as form
  54. *
  55. * @param string $db database name
  56. *
  57. * @return string html content
  58. */
  59. public function getHtmlForPageSaveAs($db)
  60. {
  61. $cfgRelation = $this->relation->getRelationsParam();
  62. return Template::get('database/designer/page_save_as')->render([
  63. 'db' => $db,
  64. 'pdfwork' => $cfgRelation['pdfwork'],
  65. 'pages' => $this->getPageIdsAndNames($db),
  66. ]);
  67. }
  68. /**
  69. * Retrieve IDs and names of schema pages
  70. *
  71. * @param string $db database name
  72. *
  73. * @return array array of schema page id and names
  74. */
  75. private function getPageIdsAndNames($db)
  76. {
  77. $cfgRelation = $this->relation->getRelationsParam();
  78. $page_query = "SELECT `page_nr`, `page_descr` FROM "
  79. . Util::backquote($cfgRelation['db']) . "."
  80. . Util::backquote($cfgRelation['pdf_pages'])
  81. . " WHERE db_name = '" . $GLOBALS['dbi']->escapeString($db) . "'"
  82. . " ORDER BY `page_descr`";
  83. $page_rs = $this->relation->queryAsControlUser(
  84. $page_query,
  85. false,
  86. DatabaseInterface::QUERY_STORE
  87. );
  88. $result = [];
  89. while ($curr_page = $GLOBALS['dbi']->fetchAssoc($page_rs)) {
  90. $result[intval($curr_page['page_nr'])] = $curr_page['page_descr'];
  91. }
  92. return $result;
  93. }
  94. /**
  95. * Function to get html for displaying the schema export
  96. *
  97. * @param string $db database name
  98. * @param int $page the page to be exported
  99. *
  100. * @return string
  101. */
  102. public function getHtmlForSchemaExport($db, $page)
  103. {
  104. /* Scan for schema plugins */
  105. /* @var $export_list SchemaPlugin[] */
  106. $export_list = Plugins::getPlugins(
  107. "schema",
  108. 'libraries/classes/Plugins/Schema/',
  109. null
  110. );
  111. /* Fail if we didn't find any schema plugin */
  112. if (empty($export_list)) {
  113. return Message::error(
  114. __('Could not load schema plugins, please check your installation!')
  115. )->getDisplay();
  116. }
  117. return Template::get('database/designer/schema_export')
  118. ->render(
  119. [
  120. 'db' => $db,
  121. 'page' => $page,
  122. 'export_list' => $export_list
  123. ]
  124. );
  125. }
  126. /**
  127. * Returns HTML for including some variable to be accessed by JavaScript
  128. *
  129. * @param array $script_tables array on foreign key support for each table
  130. * @param array $script_contr initialization data array
  131. * @param array $script_display_field display fields of each table
  132. * @param int $display_page page number of the selected page
  133. *
  134. * @return string html
  135. */
  136. public function getHtmlForJsFields(
  137. array $script_tables,
  138. array $script_contr,
  139. array $script_display_field,
  140. $display_page
  141. ) {
  142. $cfgRelation = $this->relation->getRelationsParam();
  143. $designerConfig = new \stdClass();
  144. $designerConfig->db = $_GET['db'];
  145. $designerConfig->scriptTables = $script_tables;
  146. $designerConfig->scriptContr = $script_contr;
  147. $designerConfig->server = $GLOBALS['server'];
  148. $designerConfig->scriptDisplayField = $script_display_field;
  149. $designerConfig->displayPage = $display_page;
  150. $designerConfig->tablesEnabled = $cfgRelation['pdfwork'];
  151. return Template::get('database/designer/js_fields')->render([
  152. 'designer_config' => json_encode($designerConfig)
  153. ]);
  154. }
  155. /**
  156. * Returns HTML for the menu bar of the designer page
  157. *
  158. * @param boolean $visualBuilder whether this is visual query builder
  159. * @param string $selectedPage name of the selected page
  160. * @param array $paramsArray array with class name for various buttons
  161. * on side menu
  162. *
  163. * @return string html
  164. */
  165. public function getPageMenu($visualBuilder, $selectedPage, array $paramsArray)
  166. {
  167. return Template::get('database/designer/side_menu')->render([
  168. 'visual_builder' => $visualBuilder,
  169. 'selected_page' => $selectedPage,
  170. 'params_array' => $paramsArray,
  171. 'theme' => $GLOBALS['PMA_Theme'],
  172. ]);
  173. }
  174. /**
  175. * Returns array of stored values of Designer Settings
  176. *
  177. * @return array stored values
  178. */
  179. private function getSideMenuParamsArray()
  180. {
  181. $params = [];
  182. $cfgRelation = $this->relation->getRelationsParam();
  183. if ($GLOBALS['cfgRelation']['designersettingswork']) {
  184. $query = 'SELECT `settings_data` FROM '
  185. . Util::backquote($cfgRelation['db']) . '.'
  186. . Util::backquote($cfgRelation['designer_settings'])
  187. . ' WHERE ' . Util::backquote('username') . ' = "'
  188. . $GLOBALS['dbi']->escapeString($GLOBALS['cfg']['Server']['user'])
  189. . '";';
  190. $result = $GLOBALS['dbi']->fetchSingleRow($query);
  191. $params = json_decode($result['settings_data'], true);
  192. }
  193. return $params;
  194. }
  195. /**
  196. * Returns class names for various buttons on Designer Side Menu
  197. *
  198. * @return array class names of various buttons
  199. */
  200. public function returnClassNamesFromMenuButtons()
  201. {
  202. $classes_array = [];
  203. $params_array = $this->getSideMenuParamsArray();
  204. if (isset($params_array['angular_direct'])
  205. && $params_array['angular_direct'] == 'angular'
  206. ) {
  207. $classes_array['angular_direct'] = 'M_butt_Selected_down';
  208. } else {
  209. $classes_array['angular_direct'] = 'M_butt';
  210. }
  211. if (isset($params_array['snap_to_grid'])
  212. && $params_array['snap_to_grid'] == 'on'
  213. ) {
  214. $classes_array['snap_to_grid'] = 'M_butt_Selected_down';
  215. } else {
  216. $classes_array['snap_to_grid'] = 'M_butt';
  217. }
  218. if (isset($params_array['pin_text'])
  219. && $params_array['pin_text'] == 'true'
  220. ) {
  221. $classes_array['pin_text'] = 'M_butt_Selected_down';
  222. } else {
  223. $classes_array['pin_text'] = 'M_butt';
  224. }
  225. if (isset($params_array['relation_lines'])
  226. && $params_array['relation_lines'] == 'false'
  227. ) {
  228. $classes_array['relation_lines'] = 'M_butt_Selected_down';
  229. } else {
  230. $classes_array['relation_lines'] = 'M_butt';
  231. }
  232. if (isset($params_array['small_big_all'])
  233. && $params_array['small_big_all'] == 'v'
  234. ) {
  235. $classes_array['small_big_all'] = 'M_butt_Selected_down';
  236. } else {
  237. $classes_array['small_big_all'] = 'M_butt';
  238. }
  239. if (isset($params_array['side_menu'])
  240. && $params_array['side_menu'] == 'true'
  241. ) {
  242. $classes_array['side_menu'] = 'M_butt_Selected_down';
  243. } else {
  244. $classes_array['side_menu'] = 'M_butt';
  245. }
  246. return $classes_array;
  247. }
  248. /**
  249. * Returns HTML for the canvas element
  250. *
  251. * @return string html
  252. */
  253. public function getHtmlCanvas()
  254. {
  255. return Template::get('database/designer/canvas')->render();
  256. }
  257. /**
  258. * Return HTML for the table list
  259. *
  260. * @param array $tab_pos table positions
  261. * @param int $display_page page number of the selected page
  262. *
  263. * @return string html
  264. */
  265. public function getHtmlTableList(array $tab_pos, $display_page)
  266. {
  267. return Template::get('database/designer/table_list')->render([
  268. 'tab_pos' => $tab_pos,
  269. 'display_page' => $display_page,
  270. 'theme' => $GLOBALS['PMA_Theme'],
  271. 'table_names' => $GLOBALS['designer']['TABLE_NAME'],
  272. 'table_names_url' => $GLOBALS['designer_url']['TABLE_NAME'],
  273. 'table_names_small_url' => $GLOBALS['designer_url']['TABLE_NAME_SMALL'],
  274. 'table_names_out' => $GLOBALS['designer_out']['TABLE_NAME'],
  275. ]);
  276. }
  277. /**
  278. * Get HTML to display tables on designer page
  279. *
  280. * @param array $tab_pos tables positions
  281. * @param int $display_page page number of the selected page
  282. * @param array $tab_column table column info
  283. * @param array $tables_all_keys all indices
  284. * @param array $tables_pk_or_unique_keys unique or primary indices
  285. *
  286. * @return string html
  287. */
  288. public function getDatabaseTables(
  289. array $tab_pos,
  290. $display_page,
  291. array $tab_column,
  292. array $tables_all_keys,
  293. array $tables_pk_or_unique_keys
  294. ) {
  295. return Template::get('database/designer/database_tables')->render([
  296. 'db' => $GLOBALS['db'],
  297. 'get_db' => $_GET['db'],
  298. 'has_query' => isset($_REQUEST['query']),
  299. 'tab_pos' => $tab_pos,
  300. 'display_page' => $display_page,
  301. 'tab_column' => $tab_column,
  302. 'tables_all_keys' => $tables_all_keys,
  303. 'tables_pk_or_unique_keys' => $tables_pk_or_unique_keys,
  304. 'table_names' => $GLOBALS['designer']['TABLE_NAME'],
  305. 'table_names_url' => $GLOBALS['designer_url']['TABLE_NAME'],
  306. 'table_names_small' => $GLOBALS['designer']['TABLE_NAME_SMALL'],
  307. 'table_names_small_url' => $GLOBALS['designer_url']['TABLE_NAME_SMALL'],
  308. 'table_names_small_out' => $GLOBALS['designer_out']['TABLE_NAME_SMALL'],
  309. 'table_types' => $GLOBALS['designer']['TABLE_TYPE'],
  310. 'owner_out' => $GLOBALS['designer_out']['OWNER'],
  311. 'theme' => $GLOBALS['PMA_Theme'],
  312. ]);
  313. }
  314. /**
  315. * Returns HTML for the new relations panel.
  316. *
  317. * @return string html
  318. */
  319. public function getNewRelationPanel()
  320. {
  321. return Template::get('database/designer/new_relation_panel')
  322. ->render();
  323. }
  324. /**
  325. * Returns HTML for the relations delete panel
  326. *
  327. * @return string html
  328. */
  329. public function getDeleteRelationPanel()
  330. {
  331. return Template::get('database/designer/delete_relation_panel')
  332. ->render();
  333. }
  334. /**
  335. * Returns HTML for the options panel
  336. *
  337. * @return string html
  338. */
  339. public function getOptionsPanel()
  340. {
  341. return Template::get('database/designer/options_panel')->render();
  342. }
  343. /**
  344. * Get HTML for the 'rename to' panel
  345. *
  346. * @return string html
  347. */
  348. public function getRenameToPanel()
  349. {
  350. return Template::get('database/designer/rename_to_panel')
  351. ->render();
  352. }
  353. /**
  354. * Returns HTML for the 'having' panel
  355. *
  356. * @return string html
  357. */
  358. public function getHavingQueryPanel()
  359. {
  360. return Template::get('database/designer/having_query_panel')
  361. ->render();
  362. }
  363. /**
  364. * Returns HTML for the 'aggregate' panel
  365. *
  366. * @return string html
  367. */
  368. public function getAggregateQueryPanel()
  369. {
  370. return Template::get('database/designer/aggregate_query_panel')
  371. ->render();
  372. }
  373. /**
  374. * Returns HTML for the 'where' panel
  375. *
  376. * @return string html
  377. */
  378. public function getWhereQueryPanel()
  379. {
  380. return Template::get('database/designer/where_query_panel')
  381. ->render();
  382. }
  383. /**
  384. * Returns HTML for the query details panel
  385. *
  386. * @param string $db Database name
  387. *
  388. * @return string html
  389. */
  390. public function getQueryDetails($db)
  391. {
  392. return Template::get('database/designer/query_details')->render([
  393. 'db' => $db,
  394. ]);
  395. }
  396. }