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