normalization.js 27 KB

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