normalization.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. "use strict";
  2. /**
  3. * @fileoverview events handling from normalization page
  4. * @name normalization
  5. *
  6. * @requires jQuery
  7. */
  8. // eslint-disable-next-line no-unused-vars
  9. /* global centralColumnList:writable */
  10. // js/functions.js
  11. /**
  12. * AJAX scripts for normalization
  13. *
  14. */
  15. var normalizeto = '1nf';
  16. var primaryKey;
  17. var dataParsed = null;
  18. function appendHtmlColumnsList() {
  19. $.post('index.php?route=/normalization', {
  20. 'ajax_request': true,
  21. 'db': CommonParams.get('db'),
  22. 'table': CommonParams.get('table'),
  23. 'server': CommonParams.get('server'),
  24. 'getColumns': true
  25. }, function (data) {
  26. if (data.success === true) {
  27. $('select[name=makeAtomic]').html(data.message);
  28. }
  29. });
  30. }
  31. function goTo3NFStep1(newTables) {
  32. var tables = newTables;
  33. if (Object.keys(tables).length === 1) {
  34. tables = [CommonParams.get('table')];
  35. }
  36. $.post('index.php?route=/normalization', {
  37. 'ajax_request': true,
  38. 'db': CommonParams.get('db'),
  39. 'server': CommonParams.get('server'),
  40. 'tables': tables,
  41. 'step': '3.1'
  42. }, function (data) {
  43. $('#page_content').find('h3').html(Messages.str3NFNormalization);
  44. $('#mainContent').find('legend').html(data.legendText);
  45. $('#mainContent').find('h4').html(data.headText);
  46. $('#mainContent').find('p').html(data.subText);
  47. $('#mainContent').find('#extra').html(data.extra);
  48. $('#extra').find('form').each(function () {
  49. var formId = $(this).attr('id');
  50. var colName = $(this).data('colname');
  51. $('#' + formId + ' input[value=\'' + colName + '\']').next().remove();
  52. $('#' + formId + ' input[value=\'' + colName + '\']').remove();
  53. });
  54. $('#mainContent').find('#newCols').html('');
  55. $('.tblFooters').html('');
  56. if (data.subText !== '') {
  57. $('<input>').attr({
  58. type: 'button',
  59. value: Messages.strDone,
  60. class: 'btn btn-primary'
  61. }).on('click', function () {
  62. processDependencies('', true);
  63. }).appendTo('.tblFooters');
  64. }
  65. });
  66. }
  67. function goTo2NFStep1() {
  68. $.post('index.php?route=/normalization', {
  69. 'ajax_request': true,
  70. 'db': CommonParams.get('db'),
  71. 'table': CommonParams.get('table'),
  72. 'server': CommonParams.get('server'),
  73. 'step': '2.1'
  74. }, function (data) {
  75. $('#page_content h3').html(Messages.str2NFNormalization);
  76. $('#mainContent legend').html(data.legendText);
  77. $('#mainContent h4').html(data.headText);
  78. $('#mainContent p').html(data.subText);
  79. $('#mainContent #extra').html(data.extra);
  80. $('#mainContent #newCols').html('');
  81. if (data.subText !== '') {
  82. $('<input>').attr({
  83. type: 'submit',
  84. value: Messages.strDone,
  85. class: 'btn btn-primary'
  86. }).on('click', function () {
  87. processDependencies(data.primary_key);
  88. }).appendTo('.tblFooters');
  89. } else {
  90. if (normalizeto === '3nf') {
  91. $('#mainContent #newCols').html(Messages.strToNextStep);
  92. setTimeout(function () {
  93. goTo3NFStep1([CommonParams.get('table')]);
  94. }, 3000);
  95. }
  96. }
  97. });
  98. }
  99. function goToFinish1NF() {
  100. if (normalizeto !== '1nf') {
  101. goTo2NFStep1();
  102. return true;
  103. }
  104. $('#mainContent legend').html(Messages.strEndStep);
  105. $('#mainContent h4').html('<h3>' + Functions.sprintf(Messages.strFinishMsg, Functions.escapeHtml(CommonParams.get('table'))) + '</h3>');
  106. $('#mainContent p').html('');
  107. $('#mainContent #extra').html('');
  108. $('#mainContent #newCols').html('');
  109. $('.tblFooters').html('');
  110. } // eslint-disable-next-line no-unused-vars
  111. function goToStep4() {
  112. $.post('index.php?route=/normalization', {
  113. 'ajax_request': true,
  114. 'db': CommonParams.get('db'),
  115. 'table': CommonParams.get('table'),
  116. 'server': CommonParams.get('server'),
  117. 'step4': true
  118. }, function (data) {
  119. $('#mainContent legend').html(data.legendText);
  120. $('#mainContent h4').html(data.headText);
  121. $('#mainContent p').html(data.subText);
  122. $('#mainContent #extra').html(data.extra);
  123. $('#mainContent #newCols').html('');
  124. $('.tblFooters').html('');
  125. for (var pk in primaryKey) {
  126. $('#extra input[value=\'' + Functions.escapeJsString(primaryKey[pk]) + '\']').attr('disabled', 'disabled');
  127. }
  128. });
  129. }
  130. function goToStep3() {
  131. $.post('index.php?route=/normalization', {
  132. 'ajax_request': true,
  133. 'db': CommonParams.get('db'),
  134. 'table': CommonParams.get('table'),
  135. 'server': CommonParams.get('server'),
  136. 'step3': true
  137. }, function (data) {
  138. $('#mainContent legend').html(data.legendText);
  139. $('#mainContent h4').html(data.headText);
  140. $('#mainContent p').html(data.subText);
  141. $('#mainContent #extra').html(data.extra);
  142. $('#mainContent #newCols').html('');
  143. $('.tblFooters').html('');
  144. primaryKey = JSON.parse(data.primary_key);
  145. for (var pk in primaryKey) {
  146. $('#extra input[value=\'' + Functions.escapeJsString(primaryKey[pk]) + '\']').attr('disabled', 'disabled');
  147. }
  148. });
  149. }
  150. function goToStep2(extra) {
  151. $.post('index.php?route=/normalization', {
  152. 'ajax_request': true,
  153. 'db': CommonParams.get('db'),
  154. 'table': CommonParams.get('table'),
  155. 'server': CommonParams.get('server'),
  156. 'step2': true
  157. }, function (data) {
  158. $('#mainContent legend').html(data.legendText);
  159. $('#mainContent h4').html(data.headText);
  160. $('#mainContent p').html(data.subText);
  161. $('#mainContent #extra,#mainContent #newCols').html('');
  162. $('.tblFooters').html('');
  163. if (data.hasPrimaryKey === '1') {
  164. if (extra === 'goToStep3') {
  165. $('#mainContent h4').html(Messages.strPrimaryKeyAdded);
  166. $('#mainContent p').html(Messages.strToNextStep);
  167. }
  168. if (extra === 'goToFinish1NF') {
  169. goToFinish1NF();
  170. } else {
  171. setTimeout(function () {
  172. goToStep3();
  173. }, 3000);
  174. }
  175. } else {
  176. // form to select columns to make primary
  177. $('#mainContent #extra').html(data.extra);
  178. }
  179. });
  180. }
  181. function goTo2NFFinish(pd) {
  182. var tables = {};
  183. for (var dependson in pd) {
  184. tables[dependson] = $('#extra input[name="' + dependson + '"]').val();
  185. }
  186. var datastring = {
  187. 'ajax_request': true,
  188. 'db': CommonParams.get('db'),
  189. 'table': CommonParams.get('table'),
  190. 'server': CommonParams.get('server'),
  191. 'pd': JSON.stringify(pd),
  192. 'newTablesName': JSON.stringify(tables),
  193. 'createNewTables2NF': 1
  194. };
  195. $.ajax({
  196. type: 'POST',
  197. url: 'index.php?route=/normalization',
  198. data: datastring,
  199. async: false,
  200. success: function success(data) {
  201. if (data.success === true) {
  202. if (data.queryError === false) {
  203. if (normalizeto === '3nf') {
  204. $('#pma_navigation_reload').trigger('click');
  205. goTo3NFStep1(tables);
  206. return true;
  207. }
  208. $('#mainContent legend').html(data.legendText);
  209. $('#mainContent h4').html(data.headText);
  210. $('#mainContent p').html('');
  211. $('#mainContent #extra').html('');
  212. $('.tblFooters').html('');
  213. } else {
  214. Functions.ajaxShowMessage(data.extra, false);
  215. }
  216. $('#pma_navigation_reload').trigger('click');
  217. } else {
  218. Functions.ajaxShowMessage(data.error, false);
  219. }
  220. }
  221. });
  222. }
  223. function goTo3NFFinish(newTables) {
  224. for (var table in newTables) {
  225. for (var newtbl in newTables[table]) {
  226. var updatedname = $('#extra input[name="' + newtbl + '"]').val();
  227. newTables[table][updatedname] = newTables[table][newtbl];
  228. if (updatedname !== newtbl) {
  229. delete newTables[table][newtbl];
  230. }
  231. }
  232. }
  233. var datastring = {
  234. 'ajax_request': true,
  235. 'db': CommonParams.get('db'),
  236. 'server': CommonParams.get('server'),
  237. 'newTables': JSON.stringify(newTables),
  238. 'createNewTables3NF': 1
  239. };
  240. $.ajax({
  241. type: 'POST',
  242. url: 'index.php?route=/normalization',
  243. data: datastring,
  244. async: false,
  245. success: function success(data) {
  246. if (data.success === true) {
  247. if (data.queryError === false) {
  248. $('#mainContent legend').html(data.legendText);
  249. $('#mainContent h4').html(data.headText);
  250. $('#mainContent p').html('');
  251. $('#mainContent #extra').html('');
  252. $('.tblFooters').html('');
  253. } else {
  254. Functions.ajaxShowMessage(data.extra, false);
  255. }
  256. $('#pma_navigation_reload').trigger('click');
  257. } else {
  258. Functions.ajaxShowMessage(data.error, false);
  259. }
  260. }
  261. });
  262. }
  263. var backup = '';
  264. function goTo2NFStep2(pd, primaryKey) {
  265. $('#newCols').html('');
  266. $('#mainContent legend').html(Messages.strStep + ' 2.2 ' + Messages.strConfirmPd);
  267. $('#mainContent h4').html(Messages.strSelectedPd);
  268. $('#mainContent p').html(Messages.strPdHintNote);
  269. var extra = '<div class="dependencies_box">';
  270. var pdFound = false;
  271. for (var dependson in pd) {
  272. if (dependson !== primaryKey) {
  273. pdFound = true;
  274. extra += '<p class="displayblock desc">' + Functions.escapeHtml(dependson) + ' -> ' + Functions.escapeHtml(pd[dependson].toString()) + '</p>';
  275. }
  276. }
  277. if (!pdFound) {
  278. extra += '<p class="displayblock desc">' + Messages.strNoPdSelected + '</p>';
  279. extra += '</div>';
  280. } else {
  281. extra += '</div>';
  282. var datastring = {
  283. 'ajax_request': true,
  284. 'db': CommonParams.get('db'),
  285. 'table': CommonParams.get('table'),
  286. 'server': CommonParams.get('server'),
  287. 'pd': JSON.stringify(pd),
  288. 'getNewTables2NF': 1
  289. };
  290. $.ajax({
  291. type: 'POST',
  292. url: 'index.php?route=/normalization',
  293. data: datastring,
  294. async: false,
  295. success: function success(data) {
  296. if (data.success === true) {
  297. extra += data.message;
  298. } else {
  299. Functions.ajaxShowMessage(data.error, false);
  300. }
  301. }
  302. });
  303. }
  304. $('#mainContent #extra').html(extra);
  305. $('.tblFooters').html('<input type="button" class="btn btn-primary" value="' + Messages.strBack + '" id="backEditPd"><input type="button" class="btn btn-primary" id="goTo2NFFinish" value="' + Messages.strGo + '">');
  306. $('#goTo2NFFinish').on('click', function () {
  307. goTo2NFFinish(pd);
  308. });
  309. }
  310. function goTo3NFStep2(pd, tablesTds) {
  311. $('#newCols').html('');
  312. $('#mainContent legend').html(Messages.strStep + ' 3.2 ' + Messages.strConfirmTd);
  313. $('#mainContent h4').html(Messages.strSelectedTd);
  314. $('#mainContent p').html(Messages.strPdHintNote);
  315. var extra = '<div class="dependencies_box">';
  316. var pdFound = false;
  317. for (var table in tablesTds) {
  318. for (var i in tablesTds[table]) {
  319. var dependson = tablesTds[table][i];
  320. if (dependson !== '' && dependson !== table) {
  321. pdFound = true;
  322. extra += '<p class="displayblock desc">' + Functions.escapeHtml(dependson) + ' -> ' + Functions.escapeHtml(pd[dependson].toString()) + '</p>';
  323. }
  324. }
  325. }
  326. if (!pdFound) {
  327. extra += '<p class="displayblock desc">' + Messages.strNoTdSelected + '</p>';
  328. extra += '</div>';
  329. } else {
  330. extra += '</div>';
  331. var datastring = {
  332. 'ajax_request': true,
  333. 'db': CommonParams.get('db'),
  334. 'tables': JSON.stringify(tablesTds),
  335. 'server': CommonParams.get('server'),
  336. 'pd': JSON.stringify(pd),
  337. 'getNewTables3NF': 1
  338. };
  339. $.ajax({
  340. type: 'POST',
  341. url: 'index.php?route=/normalization',
  342. data: datastring,
  343. async: false,
  344. success: function success(data) {
  345. dataParsed = data;
  346. if (data.success === true) {
  347. extra += dataParsed.html;
  348. } else {
  349. Functions.ajaxShowMessage(data.error, false);
  350. }
  351. }
  352. });
  353. }
  354. $('#mainContent #extra').html(extra);
  355. $('.tblFooters').html('<input type="button" class="btn btn-primary" value="' + Messages.strBack + '" id="backEditPd"><input type="button" class="btn btn-primary" id="goTo3NFFinish" value="' + Messages.strGo + '">');
  356. $('#goTo3NFFinish').on('click', function () {
  357. if (!pdFound) {
  358. goTo3NFFinish([]);
  359. } else {
  360. goTo3NFFinish(dataParsed.newTables);
  361. }
  362. });
  363. }
  364. function processDependencies(primaryKey, isTransitive) {
  365. var pk = primaryKey;
  366. var pd = {};
  367. var tablesTds = {};
  368. var dependsOn;
  369. pd[pk] = [];
  370. $('#extra form').each(function () {
  371. var tblname;
  372. if (isTransitive === true) {
  373. tblname = $(this).data('tablename');
  374. pk = tblname;
  375. if (!(tblname in tablesTds)) {
  376. tablesTds[tblname] = [];
  377. }
  378. tablesTds[tblname].push(pk);
  379. }
  380. var formId = $(this).attr('id');
  381. $('#' + formId + ' input[type=checkbox]:not(:checked)').prop('checked', false);
  382. dependsOn = '';
  383. $('#' + formId + ' input[type=checkbox]:checked').each(function () {
  384. dependsOn += $(this).val() + ', ';
  385. $(this).attr('checked', 'checked');
  386. });
  387. if (dependsOn === '') {
  388. dependsOn = pk;
  389. } else {
  390. dependsOn = dependsOn.slice(0, -2);
  391. }
  392. if (!(dependsOn in pd)) {
  393. pd[dependsOn] = [];
  394. }
  395. pd[dependsOn].push($(this).data('colname'));
  396. if (isTransitive === true) {
  397. if (!(tblname in tablesTds)) {
  398. tablesTds[tblname] = [];
  399. }
  400. if ($.inArray(dependsOn, tablesTds[tblname]) === -1) {
  401. tablesTds[tblname].push(dependsOn);
  402. }
  403. }
  404. });
  405. backup = $('#mainContent').html();
  406. if (isTransitive === true) {
  407. goTo3NFStep2(pd, tablesTds);
  408. } else {
  409. goTo2NFStep2(pd, pk);
  410. }
  411. return false;
  412. }
  413. function moveRepeatingGroup(repeatingCols) {
  414. var newTable = $('input[name=repeatGroupTable]').val();
  415. var newColumn = $('input[name=repeatGroupColumn]').val();
  416. if (!newTable) {
  417. $('input[name=repeatGroupTable]').trigger('focus');
  418. return false;
  419. }
  420. if (!newColumn) {
  421. $('input[name=repeatGroupColumn]').trigger('focus');
  422. return false;
  423. }
  424. var datastring = {
  425. 'ajax_request': true,
  426. 'db': CommonParams.get('db'),
  427. 'table': CommonParams.get('table'),
  428. 'server': CommonParams.get('server'),
  429. 'repeatingColumns': repeatingCols,
  430. 'newTable': newTable,
  431. 'newColumn': newColumn,
  432. 'primary_columns': primaryKey.toString()
  433. };
  434. $.ajax({
  435. type: 'POST',
  436. url: 'index.php?route=/normalization',
  437. data: datastring,
  438. async: false,
  439. success: function success(data) {
  440. if (data.success === true) {
  441. if (data.queryError === false) {
  442. goToStep3();
  443. }
  444. Functions.ajaxShowMessage(data.message, false);
  445. $('#pma_navigation_reload').trigger('click');
  446. } else {
  447. Functions.ajaxShowMessage(data.error, false);
  448. }
  449. }
  450. });
  451. }
  452. AJAX.registerTeardown('normalization.js', function () {
  453. $('#extra').off('click', '#selectNonAtomicCol');
  454. $('#splitGo').off('click');
  455. $('.tblFooters').off('click', '#saveSplit');
  456. $('#extra').off('click', '#addNewPrimary');
  457. $('.tblFooters').off('click', '#saveNewPrimary');
  458. $('#extra').off('click', '#removeRedundant');
  459. $('#mainContent p').off('click', '#createPrimaryKey');
  460. $('#mainContent').off('click', '#backEditPd');
  461. $('#mainContent').off('click', '#showPossiblePd');
  462. $('#mainContent').off('click', '.pickPd');
  463. });
  464. AJAX.registerOnload('normalization.js', function () {
  465. var selectedCol;
  466. normalizeto = $('#mainContent').data('normalizeto');
  467. $('#extra').on('click', '#selectNonAtomicCol', function () {
  468. if ($(this).val() === 'no_such_col') {
  469. goToStep2();
  470. } else {
  471. selectedCol = $(this).val();
  472. }
  473. });
  474. $('#splitGo').on('click', function () {
  475. if (!selectedCol || selectedCol === '') {
  476. return false;
  477. }
  478. var numField = $('#numField').val();
  479. $.post('index.php?route=/normalization', {
  480. 'ajax_request': true,
  481. 'db': CommonParams.get('db'),
  482. 'table': CommonParams.get('table'),
  483. 'server': CommonParams.get('server'),
  484. 'splitColumn': true,
  485. 'numFields': numField
  486. }, function (data) {
  487. if (data.success === true) {
  488. $('#newCols').html(data.message);
  489. $('.default_value').hide();
  490. $('.enum_notice').hide();
  491. $('<input>').attr({
  492. type: 'submit',
  493. id: 'saveSplit',
  494. value: Messages.strSave,
  495. class: 'btn btn-primary'
  496. }).appendTo('.tblFooters');
  497. $('<input>').attr({
  498. type: 'submit',
  499. id: 'cancelSplit',
  500. value: Messages.strCancel,
  501. class: 'btn btn-secondary'
  502. }).on('click', function () {
  503. $('#newCols').html('');
  504. $(this).parent().html('');
  505. }).appendTo('.tblFooters');
  506. }
  507. });
  508. return false;
  509. });
  510. $('.tblFooters').on('click', '#saveSplit', function () {
  511. centralColumnList = [];
  512. if ($('#newCols #field_0_1').val() === '') {
  513. $('#newCols #field_0_1').trigger('focus');
  514. return false;
  515. }
  516. var argsep = CommonParams.get('arg_separator');
  517. var datastring = $('#newCols :input').serialize();
  518. datastring += argsep + 'ajax_request=1' + argsep + 'do_save_data=1' + argsep + 'field_where=last';
  519. $.post('index.php?route=/table/add-field', datastring, function (data) {
  520. if (data.success) {
  521. $.post('index.php?route=/sql', {
  522. 'ajax_request': true,
  523. 'db': CommonParams.get('db'),
  524. 'table': CommonParams.get('table'),
  525. 'server': CommonParams.get('server'),
  526. 'dropped_column': selectedCol,
  527. 'purge': 1,
  528. 'sql_query': 'ALTER TABLE `' + CommonParams.get('table') + '` DROP `' + selectedCol + '`;',
  529. 'is_js_confirmed': 1
  530. }, function (data) {
  531. if (data.success === true) {
  532. appendHtmlColumnsList();
  533. $('#newCols').html('');
  534. $('.tblFooters').html('');
  535. } else {
  536. Functions.ajaxShowMessage(data.error, false);
  537. }
  538. selectedCol = '';
  539. });
  540. } else {
  541. Functions.ajaxShowMessage(data.error, false);
  542. }
  543. });
  544. });
  545. $('#extra').on('click', '#addNewPrimary', function () {
  546. $.post('index.php?route=/normalization', {
  547. 'ajax_request': true,
  548. 'db': CommonParams.get('db'),
  549. 'table': CommonParams.get('table'),
  550. 'server': CommonParams.get('server'),
  551. 'addNewPrimary': true
  552. }, function (data) {
  553. if (data.success === true) {
  554. $('#newCols').html(data.message);
  555. $('.default_value').hide();
  556. $('.enum_notice').hide();
  557. $('<input>').attr({
  558. type: 'submit',
  559. id: 'saveNewPrimary',
  560. value: Messages.strSave,
  561. class: 'btn btn-primary'
  562. }).appendTo('.tblFooters');
  563. $('<input>').attr({
  564. type: 'submit',
  565. id: 'cancelSplit',
  566. value: Messages.strCancel,
  567. class: 'btn btn-secondary'
  568. }).on('click', function () {
  569. $('#newCols').html('');
  570. $(this).parent().html('');
  571. }).appendTo('.tblFooters');
  572. } else {
  573. Functions.ajaxShowMessage(data.error, false);
  574. }
  575. });
  576. return false;
  577. });
  578. $('.tblFooters').on('click', '#saveNewPrimary', function () {
  579. var datastring = $('#newCols :input').serialize();
  580. var argsep = CommonParams.get('arg_separator');
  581. datastring += argsep + 'field_key[0]=primary_0' + argsep + 'ajax_request=1' + argsep + 'do_save_data=1' + argsep + 'field_where=last';
  582. $.post('index.php?route=/table/add-field', datastring, function (data) {
  583. if (data.success === true) {
  584. $('#mainContent h4').html(Messages.strPrimaryKeyAdded);
  585. $('#mainContent p').html(Messages.strToNextStep);
  586. $('#mainContent #extra').html('');
  587. $('#mainContent #newCols').html('');
  588. $('.tblFooters').html('');
  589. setTimeout(function () {
  590. goToStep3();
  591. }, 2000);
  592. } else {
  593. Functions.ajaxShowMessage(data.error, false);
  594. }
  595. });
  596. });
  597. $('#extra').on('click', '#removeRedundant', function () {
  598. var dropQuery = 'ALTER TABLE `' + CommonParams.get('table') + '` ';
  599. $('#extra input[type=checkbox]:checked').each(function () {
  600. dropQuery += 'DROP `' + $(this).val() + '`, ';
  601. });
  602. dropQuery = dropQuery.slice(0, -2);
  603. $.post('index.php?route=/sql', {
  604. 'ajax_request': true,
  605. 'db': CommonParams.get('db'),
  606. 'table': CommonParams.get('table'),
  607. 'server': CommonParams.get('server'),
  608. 'sql_query': dropQuery,
  609. 'is_js_confirmed': 1
  610. }, function (data) {
  611. if (data.success === true) {
  612. goToStep2('goToFinish1NF');
  613. } else {
  614. Functions.ajaxShowMessage(data.error, false);
  615. }
  616. });
  617. });
  618. $('#extra').on('click', '#moveRepeatingGroup', function () {
  619. var repeatingCols = '';
  620. $('#extra input[type=checkbox]:checked').each(function () {
  621. repeatingCols += $(this).val() + ', ';
  622. });
  623. if (repeatingCols !== '') {
  624. var newColName = $('#extra input[type=checkbox]:checked').first().val();
  625. repeatingCols = repeatingCols.slice(0, -2);
  626. var confirmStr = Functions.sprintf(Messages.strMoveRepeatingGroup, Functions.escapeHtml(repeatingCols), Functions.escapeHtml(CommonParams.get('table')));
  627. confirmStr += '<input type="text" name="repeatGroupTable" placeholder="' + Messages.strNewTablePlaceholder + '">' + '( ' + Functions.escapeHtml(primaryKey.toString()) + ', <input type="text" name="repeatGroupColumn" placeholder="' + Messages.strNewColumnPlaceholder + '" value="' + Functions.escapeHtml(newColName) + '">)' + '</ol>';
  628. $('#newCols').html(confirmStr);
  629. $('<input>').attr({
  630. type: 'submit',
  631. value: Messages.strCancel,
  632. class: 'btn btn-secondary'
  633. }).on('click', function () {
  634. $('#newCols').html('');
  635. $('#extra input[type=checkbox]').prop('checked', false);
  636. }).appendTo('.tblFooters');
  637. $('<input>').attr({
  638. type: 'submit',
  639. value: Messages.strGo,
  640. class: 'btn btn-primary'
  641. }).on('click', function () {
  642. moveRepeatingGroup(repeatingCols);
  643. }).appendTo('.tblFooters');
  644. }
  645. });
  646. $('#mainContent p').on('click', '#createPrimaryKey', function (event) {
  647. event.preventDefault();
  648. var url = {
  649. 'create_index': 1,
  650. 'server': CommonParams.get('server'),
  651. 'db': CommonParams.get('db'),
  652. 'table': CommonParams.get('table'),
  653. 'added_fields': 1,
  654. 'add_fields': 1,
  655. 'index': {
  656. 'Key_name': 'PRIMARY'
  657. },
  658. 'ajax_request': true
  659. };
  660. var title = Messages.strAddPrimaryKey;
  661. Functions.indexEditorDialog(url, title, function () {
  662. // on success
  663. $('.sqlqueryresults').remove();
  664. $('.result_query').remove();
  665. $('.tblFooters').html('');
  666. goToStep2('goToStep3');
  667. });
  668. return false;
  669. });
  670. $('#mainContent').on('click', '#backEditPd', function () {
  671. $('#mainContent').html(backup);
  672. });
  673. $('#mainContent').on('click', '#showPossiblePd', function () {
  674. if ($(this).hasClass('hideList')) {
  675. $(this).html('+ ' + Messages.strShowPossiblePd);
  676. $(this).removeClass('hideList');
  677. $('#newCols').slideToggle('slow');
  678. return false;
  679. }
  680. if ($('#newCols').html() !== '') {
  681. $('#showPossiblePd').html('- ' + Messages.strHidePd);
  682. $('#showPossiblePd').addClass('hideList');
  683. $('#newCols').slideToggle('slow');
  684. return false;
  685. }
  686. $('#newCols').insertAfter('#mainContent h4');
  687. $('#newCols').html('<div class="text-center">' + Messages.strLoading + '<br>' + Messages.strWaitForPd + '</div>');
  688. $.post('index.php?route=/normalization', {
  689. 'ajax_request': true,
  690. 'db': CommonParams.get('db'),
  691. 'table': CommonParams.get('table'),
  692. 'server': CommonParams.get('server'),
  693. 'findPdl': true
  694. }, function (data) {
  695. $('#showPossiblePd').html('- ' + Messages.strHidePd);
  696. $('#showPossiblePd').addClass('hideList');
  697. $('#newCols').html(data.message);
  698. });
  699. });
  700. $('#mainContent').on('click', '.pickPd', function () {
  701. var strColsLeft = $(this).next('.determinants').html();
  702. var colsLeft = strColsLeft.split(',');
  703. var strColsRight = $(this).next().next().html();
  704. var colsRight = strColsRight.split(',');
  705. for (var i in colsRight) {
  706. $('form[data-colname="' + colsRight[i].trim() + '"] input[type="checkbox"]').prop('checked', false);
  707. for (var j in colsLeft) {
  708. $('form[data-colname="' + colsRight[i].trim() + '"] input[value="' + colsLeft[j].trim() + '"]').prop('checked', true);
  709. }
  710. }
  711. });
  712. });