NormalizationController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Controllers;
  4. use PhpMyAdmin\Core;
  5. use PhpMyAdmin\Normalization;
  6. use PhpMyAdmin\Response;
  7. use PhpMyAdmin\Template;
  8. use PhpMyAdmin\Url;
  9. use function intval;
  10. use function json_decode;
  11. use function json_encode;
  12. use function min;
  13. /**
  14. * Normalization process (temporarily specific to 1NF).
  15. */
  16. class NormalizationController extends AbstractController
  17. {
  18. /** @var Normalization */
  19. private $normalization;
  20. /**
  21. * @param Response $response
  22. */
  23. public function __construct($response, Template $template, Normalization $normalization)
  24. {
  25. parent::__construct($response, $template);
  26. $this->normalization = $normalization;
  27. }
  28. public function index(): void
  29. {
  30. global $db, $table;
  31. if (isset($_POST['getColumns'])) {
  32. $html = '<option selected disabled>' . __('Select one…') . '</option>'
  33. . '<option value="no_such_col">' . __('No such column') . '</option>';
  34. //get column whose datatype falls under string category
  35. $html .= $this->normalization->getHtmlForColumnsList(
  36. $db,
  37. $table,
  38. _pgettext('string types', 'String')
  39. );
  40. echo $html;
  41. return;
  42. }
  43. if (isset($_POST['splitColumn'])) {
  44. $num_fields = min(4096, intval($_POST['numFields']));
  45. $html = $this->normalization->getHtmlForCreateNewColumn($num_fields, $db, $table);
  46. $html .= Url::getHiddenInputs($db, $table);
  47. echo $html;
  48. return;
  49. }
  50. if (isset($_POST['addNewPrimary'])) {
  51. $num_fields = 1;
  52. $columnMeta = [
  53. 'Field' => $table . '_id',
  54. 'Extra' => 'auto_increment',
  55. ];
  56. $html = $this->normalization->getHtmlForCreateNewColumn(
  57. $num_fields,
  58. $db,
  59. $table,
  60. $columnMeta
  61. );
  62. $html .= Url::getHiddenInputs($db, $table);
  63. echo $html;
  64. return;
  65. }
  66. if (isset($_POST['findPdl'])) {
  67. $html = $this->normalization->findPartialDependencies($table, $db);
  68. echo $html;
  69. return;
  70. }
  71. if (isset($_POST['getNewTables2NF'])) {
  72. $partialDependencies = json_decode($_POST['pd'], true);
  73. $html = $this->normalization->getHtmlForNewTables2NF($partialDependencies, $table);
  74. echo $html;
  75. return;
  76. }
  77. if (isset($_POST['getNewTables3NF'])) {
  78. $dependencies = json_decode($_POST['pd']);
  79. $tables = json_decode($_POST['tables'], true);
  80. $newTables = $this->normalization->getHtmlForNewTables3NF($dependencies, $tables, $db);
  81. $this->response->disable();
  82. Core::headerJSON();
  83. echo json_encode($newTables);
  84. return;
  85. }
  86. $this->addScriptFiles(['normalization.js', 'vendor/jquery/jquery.uitablefilter.js']);
  87. $normalForm = '1nf';
  88. if (Core::isValid($_POST['normalizeTo'], ['1nf', '2nf', '3nf'])) {
  89. $normalForm = $_POST['normalizeTo'];
  90. }
  91. if (isset($_POST['createNewTables2NF'])) {
  92. $partialDependencies = json_decode($_POST['pd'], true);
  93. $tablesName = json_decode($_POST['newTablesName']);
  94. $res = $this->normalization->createNewTablesFor2NF($partialDependencies, $tablesName, $table, $db);
  95. $this->response->addJSON($res);
  96. return;
  97. }
  98. if (isset($_POST['createNewTables3NF'])) {
  99. $newtables = json_decode($_POST['newTables'], true);
  100. $res = $this->normalization->createNewTablesFor3NF($newtables, $db);
  101. $this->response->addJSON($res);
  102. return;
  103. }
  104. if (isset($_POST['repeatingColumns'])) {
  105. $repeatingColumns = $_POST['repeatingColumns'];
  106. $newTable = $_POST['newTable'];
  107. $newColumn = $_POST['newColumn'];
  108. $primary_columns = $_POST['primary_columns'];
  109. $res = $this->normalization->moveRepeatingGroup(
  110. $repeatingColumns,
  111. $primary_columns,
  112. $newTable,
  113. $newColumn,
  114. $table,
  115. $db
  116. );
  117. $this->response->addJSON($res);
  118. return;
  119. }
  120. if (isset($_POST['step1'])) {
  121. $html = $this->normalization->getHtmlFor1NFStep1($db, $table, $normalForm);
  122. $this->response->addHTML($html);
  123. } elseif (isset($_POST['step2'])) {
  124. $res = $this->normalization->getHtmlContentsFor1NFStep2($db, $table);
  125. $this->response->addJSON($res);
  126. } elseif (isset($_POST['step3'])) {
  127. $res = $this->normalization->getHtmlContentsFor1NFStep3($db, $table);
  128. $this->response->addJSON($res);
  129. } elseif (isset($_POST['step4'])) {
  130. $res = $this->normalization->getHtmlContentsFor1NFStep4($db, $table);
  131. $this->response->addJSON($res);
  132. } elseif (isset($_POST['step']) && $_POST['step'] == '2.1') {
  133. $res = $this->normalization->getHtmlFor2NFstep1($db, $table);
  134. $this->response->addJSON($res);
  135. } elseif (isset($_POST['step']) && $_POST['step'] == '3.1') {
  136. $tables = $_POST['tables'];
  137. $res = $this->normalization->getHtmlFor3NFstep1($db, $tables);
  138. $this->response->addJSON($res);
  139. } else {
  140. $this->response->addHTML($this->normalization->getHtmlForNormalizeTable());
  141. }
  142. }
  143. }