export.js 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  1. "use strict";
  2. /**
  3. * Functions used in the export tab
  4. *
  5. */
  6. var Export = {};
  7. /**
  8. * Disables the "Dump some row(s)" sub-options
  9. */
  10. Export.disableDumpSomeRowsSubOptions = function () {
  11. $('label[for=\'limit_to\']').fadeTo('fast', 0.4);
  12. $('label[for=\'limit_from\']').fadeTo('fast', 0.4);
  13. $('input[type=\'text\'][name=\'limit_to\']').prop('disabled', 'disabled');
  14. $('input[type=\'text\'][name=\'limit_from\']').prop('disabled', 'disabled');
  15. };
  16. /**
  17. * Enables the "Dump some row(s)" sub-options
  18. */
  19. Export.enableDumpSomeRowsSubOptions = function () {
  20. $('label[for=\'limit_to\']').fadeTo('fast', 1);
  21. $('label[for=\'limit_from\']').fadeTo('fast', 1);
  22. $('input[type=\'text\'][name=\'limit_to\']').prop('disabled', '');
  23. $('input[type=\'text\'][name=\'limit_from\']').prop('disabled', '');
  24. };
  25. /**
  26. * Return template data as a json object
  27. *
  28. * @returns template data
  29. */
  30. Export.getTemplateData = function () {
  31. var $form = $('form[name="dump"]');
  32. var excludeList = ['token', 'server', 'db', 'table', 'single_table', 'export_type', 'export_method', 'sql_query', 'template_id'];
  33. var obj = {};
  34. var arr = $form.serializeArray();
  35. $.each(arr, function () {
  36. if ($.inArray(this.name, excludeList) < 0) {
  37. if (obj[this.name] !== undefined) {
  38. if (!obj[this.name].push) {
  39. obj[this.name] = [obj[this.name]];
  40. }
  41. obj[this.name].push(this.value || '');
  42. } else {
  43. obj[this.name] = this.value || '';
  44. }
  45. }
  46. }); // include unchecked checkboxes (which are ignored by serializeArray()) with null
  47. // to uncheck them when loading the template
  48. $form.find('input[type="checkbox"]:not(:checked)').each(function () {
  49. if (obj[this.name] === undefined) {
  50. obj[this.name] = null;
  51. }
  52. }); // include empty multiselects
  53. $form.find('select').each(function () {
  54. if ($(this).find('option:selected').length === 0) {
  55. obj[this.name] = [];
  56. }
  57. });
  58. return obj;
  59. };
  60. /**
  61. * Create a template with selected options
  62. *
  63. * @param name name of the template
  64. */
  65. Export.createTemplate = function (name) {
  66. var templateData = Export.getTemplateData();
  67. var params = {
  68. 'ajax_request': true,
  69. 'server': CommonParams.get('server'),
  70. 'db': CommonParams.get('db'),
  71. 'table': CommonParams.get('table'),
  72. 'exportType': $('input[name="export_type"]').val(),
  73. 'templateName': name,
  74. 'templateData': JSON.stringify(templateData)
  75. };
  76. Functions.ajaxShowMessage();
  77. $.post('index.php?route=/export/template/create', params, function (response) {
  78. if (response.success === true) {
  79. $('#templateName').val('');
  80. $('#template').html(response.data);
  81. $('#template').find('option').each(function () {
  82. if ($(this).text() === name) {
  83. $(this).prop('selected', true);
  84. }
  85. });
  86. Functions.ajaxShowMessage(Messages.strTemplateCreated);
  87. } else {
  88. Functions.ajaxShowMessage(response.error, false);
  89. }
  90. });
  91. };
  92. /**
  93. * Loads a template
  94. *
  95. * @param id ID of the template to load
  96. */
  97. Export.loadTemplate = function (id) {
  98. var params = {
  99. 'ajax_request': true,
  100. 'server': CommonParams.get('server'),
  101. 'db': CommonParams.get('db'),
  102. 'table': CommonParams.get('table'),
  103. 'exportType': $('input[name="export_type"]').val(),
  104. 'templateId': id
  105. };
  106. Functions.ajaxShowMessage();
  107. $.post('index.php?route=/export/template/load', params, function (response) {
  108. if (response.success === true) {
  109. var $form = $('form[name="dump"]');
  110. var options = JSON.parse(response.data);
  111. $.each(options, function (key, value) {
  112. var localValue = value;
  113. var $element = $form.find('[name="' + key + '"]');
  114. if ($element.length) {
  115. if ($element.is('input') && $element.attr('type') === 'checkbox' && localValue === null) {
  116. $element.prop('checked', false);
  117. } else {
  118. if ($element.is('input') && $element.attr('type') === 'checkbox' || $element.is('input') && $element.attr('type') === 'radio' || $element.is('select') && $element.attr('multiple') === 'multiple') {
  119. if (!localValue.push) {
  120. localValue = [localValue];
  121. }
  122. }
  123. $element.val(localValue);
  124. }
  125. $element.trigger('change');
  126. }
  127. });
  128. $('input[name="template_id"]').val(id);
  129. Functions.ajaxShowMessage(Messages.strTemplateLoaded);
  130. } else {
  131. Functions.ajaxShowMessage(response.error, false);
  132. }
  133. });
  134. };
  135. /**
  136. * Updates an existing template with current options
  137. *
  138. * @param id ID of the template to update
  139. */
  140. Export.updateTemplate = function (id) {
  141. var templateData = Export.getTemplateData();
  142. var params = {
  143. 'ajax_request': true,
  144. 'server': CommonParams.get('server'),
  145. 'db': CommonParams.get('db'),
  146. 'table': CommonParams.get('table'),
  147. 'exportType': $('input[name="export_type"]').val(),
  148. 'templateId': id,
  149. 'templateData': JSON.stringify(templateData)
  150. };
  151. Functions.ajaxShowMessage();
  152. $.post('index.php?route=/export/template/update', params, function (response) {
  153. if (response.success === true) {
  154. Functions.ajaxShowMessage(Messages.strTemplateUpdated);
  155. } else {
  156. Functions.ajaxShowMessage(response.error, false);
  157. }
  158. });
  159. };
  160. /**
  161. * Delete a template
  162. *
  163. * @param id ID of the template to delete
  164. */
  165. Export.deleteTemplate = function (id) {
  166. var params = {
  167. 'ajax_request': true,
  168. 'server': CommonParams.get('server'),
  169. 'db': CommonParams.get('db'),
  170. 'table': CommonParams.get('table'),
  171. 'exportType': $('input[name="export_type"]').val(),
  172. 'templateId': id
  173. };
  174. Functions.ajaxShowMessage();
  175. $.post('index.php?route=/export/template/delete', params, function (response) {
  176. if (response.success === true) {
  177. $('#template').find('option[value="' + id + '"]').remove();
  178. Functions.ajaxShowMessage(Messages.strTemplateDeleted);
  179. } else {
  180. Functions.ajaxShowMessage(response.error, false);
  181. }
  182. });
  183. };
  184. /**
  185. * Unbind all event handlers before tearing down a page
  186. */
  187. AJAX.registerTeardown('export.js', function () {
  188. $('#plugins').off('change');
  189. $('input[type=\'radio\'][name=\'sql_structure_or_data\']').off('change');
  190. $('input[type=\'radio\'][name$=\'_structure_or_data\']').off('change');
  191. $('input[type=\'radio\'][name=\'output_format\']').off('change');
  192. $('#checkbox_sql_include_comments').off('change');
  193. $('input[type=\'radio\'][name=\'quick_or_custom\']').off('change');
  194. $('input[type=\'radio\'][name=\'allrows\']').off('change');
  195. $('#btn_alias_config').off('click');
  196. $('.alias_remove').off('click');
  197. $('#db_alias_button').off('click');
  198. $('#table_alias_button').off('click');
  199. $('#column_alias_button').off('click');
  200. $('input[name="table_select[]"]').off('change');
  201. $('input[name="table_structure[]"]').off('change');
  202. $('input[name="table_data[]"]').off('change');
  203. $('#table_structure_all').off('change');
  204. $('#table_data_all').off('change');
  205. $('input[name="createTemplate"]').off('click');
  206. $('select[name="template"]').off('change');
  207. $('input[name="updateTemplate"]').off('click');
  208. $('input[name="deleteTemplate"]').off('click');
  209. });
  210. AJAX.registerOnload('export.js', function () {
  211. $('#showsqlquery').on('click', function () {
  212. // Creating a dialog box similar to preview sql container to show sql query
  213. var modalOptions = {};
  214. modalOptions[Messages.strClose] = function () {
  215. $(this).dialog('close');
  216. };
  217. $('#export_sql_modal_content').clone().dialog({
  218. minWidth: 550,
  219. maxHeight: 400,
  220. modal: true,
  221. buttons: modalOptions,
  222. title: Messages.strQuery,
  223. close: function close() {
  224. $(this).remove();
  225. },
  226. open: function open() {
  227. // Pretty SQL printing.
  228. Functions.highlightSql($(this));
  229. }
  230. });
  231. });
  232. /**
  233. * Export template handling code
  234. */
  235. // create a new template
  236. $('input[name="createTemplate"]').on('click', function (e) {
  237. e.preventDefault();
  238. var name = $('input[name="templateName"]').val();
  239. if (name.length) {
  240. Export.createTemplate(name);
  241. }
  242. }); // load an existing template
  243. $('select[name="template"]').on('change', function (e) {
  244. e.preventDefault();
  245. var id = $(this).val();
  246. if (id.length) {
  247. Export.loadTemplate(id);
  248. }
  249. }); // update an existing template with new criteria
  250. $('input[name="updateTemplate"]').on('click', function (e) {
  251. e.preventDefault();
  252. var id = $('select[name="template"]').val();
  253. if (id.length) {
  254. Export.updateTemplate(id);
  255. }
  256. }); // delete an existing template
  257. $('input[name="deleteTemplate"]').on('click', function (e) {
  258. e.preventDefault();
  259. var id = $('select[name="template"]').val();
  260. if (id.length) {
  261. Export.deleteTemplate(id);
  262. }
  263. });
  264. /**
  265. * Toggles the hiding and showing of each plugin's options
  266. * according to the currently selected plugin from the dropdown list
  267. */
  268. $('#plugins').on('change', function () {
  269. $('#format_specific_opts').find('div.format_specific_options').hide();
  270. var selectedPluginName = $('#plugins').find('option:selected').val();
  271. $('#' + selectedPluginName + '_options').show();
  272. });
  273. /**
  274. * Toggles the enabling and disabling of the SQL plugin's comment options that apply only when exporting structure
  275. */
  276. $('input[type=\'radio\'][name=\'sql_structure_or_data\']').on('change', function () {
  277. var commentsArePresent = $('#checkbox_sql_include_comments').prop('checked');
  278. var show = $('input[type=\'radio\'][name=\'sql_structure_or_data\']:checked').val();
  279. if (show === 'data') {
  280. // disable the SQL comment options
  281. if (commentsArePresent) {
  282. $('#checkbox_sql_dates').prop('disabled', true).parent().fadeTo('fast', 0.4);
  283. }
  284. $('#checkbox_sql_relation').prop('disabled', true).parent().fadeTo('fast', 0.4);
  285. $('#checkbox_sql_mime').prop('disabled', true).parent().fadeTo('fast', 0.4);
  286. } else {
  287. // enable the SQL comment options
  288. if (commentsArePresent) {
  289. $('#checkbox_sql_dates').prop('disabled', false).parent().fadeTo('fast', 1);
  290. }
  291. $('#checkbox_sql_relation').prop('disabled', false).parent().fadeTo('fast', 1);
  292. $('#checkbox_sql_mime').prop('disabled', false).parent().fadeTo('fast', 1);
  293. }
  294. if (show === 'structure') {
  295. $('#checkbox_sql_auto_increment').prop('disabled', true).parent().fadeTo('fast', 0.4);
  296. } else {
  297. $('#checkbox_sql_auto_increment').prop('disabled', false).parent().fadeTo('fast', 1);
  298. }
  299. }); // When MS Excel is selected as the Format automatically Switch to Character Set as windows-1252
  300. $('#plugins').on('change', function () {
  301. var selectedPluginName = $('#plugins').find('option:selected').val();
  302. if (selectedPluginName === 'excel') {
  303. $('#select_charset').val('windows-1252');
  304. } else {
  305. $('#select_charset').val('utf-8');
  306. }
  307. }); // For separate-file exports only ZIP compression is allowed
  308. $('input[type="checkbox"][name="as_separate_files"]').on('change', function () {
  309. if ($(this).is(':checked')) {
  310. $('#compression').val('zip');
  311. }
  312. });
  313. $('#compression').on('change', function () {
  314. if ($('option:selected').val() !== 'zip') {
  315. $('input[type="checkbox"][name="as_separate_files"]').prop('checked', false);
  316. }
  317. });
  318. });
  319. Export.setupTableStructureOrData = function () {
  320. if ($('input[name=\'export_type\']').val() !== 'database') {
  321. return;
  322. }
  323. var pluginName = $('#plugins').find('option:selected').val();
  324. var formElemName = pluginName + '_structure_or_data';
  325. var forceStructureOrData = !$('input[name=\'' + formElemName + '_default\']').length;
  326. if (forceStructureOrData === true) {
  327. $('input[name="structure_or_data_forced"]').val(1);
  328. $('.export_structure input[type="checkbox"], .export_data input[type="checkbox"]').prop('disabled', true);
  329. $('.export_structure, .export_data').fadeTo('fast', 0.4);
  330. } else {
  331. $('input[name="structure_or_data_forced"]').val(0);
  332. $('.export_structure input[type="checkbox"], .export_data input[type="checkbox"]').prop('disabled', false);
  333. $('.export_structure, .export_data').fadeTo('fast', 1);
  334. var structureOrData = $('input[name="' + formElemName + '_default"]').val();
  335. if (structureOrData === 'structure') {
  336. $('.export_data input[type="checkbox"]').prop('checked', false);
  337. } else if (structureOrData === 'data') {
  338. $('.export_structure input[type="checkbox"]').prop('checked', false);
  339. }
  340. if (structureOrData === 'structure' || structureOrData === 'structure_and_data') {
  341. if (!$('.export_structure input[type="checkbox"]:checked').length) {
  342. $('input[name="table_select[]"]:checked').closest('tr').find('.export_structure input[type="checkbox"]').prop('checked', true);
  343. }
  344. }
  345. if (structureOrData === 'data' || structureOrData === 'structure_and_data') {
  346. if (!$('.export_data input[type="checkbox"]:checked').length) {
  347. $('input[name="table_select[]"]:checked').closest('tr').find('.export_data input[type="checkbox"]').prop('checked', true);
  348. }
  349. }
  350. Export.checkSelectedTables();
  351. Export.checkTableSelectAll();
  352. Export.checkTableSelectStructureOrData();
  353. }
  354. };
  355. /**
  356. * Toggles the hiding and showing of plugin structure-specific and data-specific
  357. * options
  358. */
  359. Export.toggleStructureDataOpts = function () {
  360. var pluginName = $('select#plugins').val();
  361. var radioFormName = pluginName + '_structure_or_data';
  362. var dataDiv = '#' + pluginName + '_data';
  363. var structureDiv = '#' + pluginName + '_structure';
  364. var show = $('input[type=\'radio\'][name=\'' + radioFormName + '\']:checked').val(); // Show the #rows if 'show' is not structure
  365. $('#rows').toggle(show !== 'structure');
  366. if (show === 'data') {
  367. $(dataDiv).slideDown('slow');
  368. $(structureDiv).slideUp('slow');
  369. } else {
  370. $(structureDiv).slideDown('slow');
  371. if (show === 'structure') {
  372. $(dataDiv).slideUp('slow');
  373. } else {
  374. $(dataDiv).slideDown('slow');
  375. }
  376. }
  377. };
  378. /**
  379. * Toggles the disabling of the "save to file" options
  380. */
  381. Export.toggleSaveToFile = function () {
  382. var $ulSaveAsfile = $('#ul_save_asfile');
  383. if (!$('#radio_dump_asfile').prop('checked')) {
  384. $ulSaveAsfile.find('> li').fadeTo('fast', 0.4);
  385. $ulSaveAsfile.find('> li > input').prop('disabled', true);
  386. $ulSaveAsfile.find('> li > select').prop('disabled', true);
  387. } else {
  388. $ulSaveAsfile.find('> li').fadeTo('fast', 1);
  389. $ulSaveAsfile.find('> li > input').prop('disabled', false);
  390. $ulSaveAsfile.find('> li > select').prop('disabled', false);
  391. }
  392. };
  393. AJAX.registerOnload('export.js', function () {
  394. Export.toggleSaveToFile();
  395. $('input[type=\'radio\'][name=\'output_format\']').on('change', Export.toggleSaveToFile);
  396. });
  397. /**
  398. * For SQL plugin, toggles the disabling of the "display comments" options
  399. */
  400. Export.toggleSqlIncludeComments = function () {
  401. $('#checkbox_sql_include_comments').on('change', function () {
  402. var $ulIncludeComments = $('#ul_include_comments');
  403. if (!$('#checkbox_sql_include_comments').prop('checked')) {
  404. $ulIncludeComments.find('> li').fadeTo('fast', 0.4);
  405. $ulIncludeComments.find('> li > input').prop('disabled', true);
  406. } else {
  407. // If structure is not being exported, the comment options for structure should not be enabled
  408. if ($('#radio_sql_structure_or_data_data').prop('checked')) {
  409. $('#text_sql_header_comment').prop('disabled', false).parent('li').fadeTo('fast', 1);
  410. } else {
  411. $ulIncludeComments.find('> li').fadeTo('fast', 1);
  412. $ulIncludeComments.find('> li > input').prop('disabled', false);
  413. }
  414. }
  415. });
  416. };
  417. Export.checkTableSelectAll = function () {
  418. var total = $('input[name="table_select[]"]').length;
  419. var strChecked = $('input[name="table_structure[]"]:checked').length;
  420. var dataChecked = $('input[name="table_data[]"]:checked').length;
  421. var strAll = $('#table_structure_all');
  422. var dataAll = $('#table_data_all');
  423. if (strChecked === total) {
  424. strAll.prop('indeterminate', false).prop('checked', true);
  425. } else if (strChecked === 0) {
  426. strAll.prop('indeterminate', false).prop('checked', false);
  427. } else {
  428. strAll.prop('indeterminate', true).prop('checked', false);
  429. }
  430. if (dataChecked === total) {
  431. dataAll.prop('indeterminate', false).prop('checked', true);
  432. } else if (dataChecked === 0) {
  433. dataAll.prop('indeterminate', false).prop('checked', false);
  434. } else {
  435. dataAll.prop('indeterminate', true).prop('checked', false);
  436. }
  437. };
  438. Export.checkTableSelectStructureOrData = function () {
  439. var strChecked = $('input[name="table_structure[]"]:checked').length;
  440. var dataChecked = $('input[name="table_data[]"]:checked').length;
  441. var autoIncrement = $('#checkbox_sql_auto_increment');
  442. var pluginName = $('select#plugins').val();
  443. var dataDiv = '#' + pluginName + '_data';
  444. var structureDiv = '#' + pluginName + '_structure';
  445. if (strChecked === 0) {
  446. $(structureDiv).slideUp('slow');
  447. } else {
  448. $(structureDiv).slideDown('slow');
  449. }
  450. if (dataChecked === 0) {
  451. $(dataDiv).slideUp('slow');
  452. autoIncrement.prop('disabled', true).parent().fadeTo('fast', 0.4);
  453. } else {
  454. $(dataDiv).slideDown('slow');
  455. autoIncrement.prop('disabled', false).parent().fadeTo('fast', 1);
  456. }
  457. };
  458. Export.toggleTableSelectAllStr = function () {
  459. var strAll = $('#table_structure_all').is(':checked');
  460. if (strAll) {
  461. $('input[name="table_structure[]"]').prop('checked', true);
  462. } else {
  463. $('input[name="table_structure[]"]').prop('checked', false);
  464. }
  465. };
  466. Export.toggleTableSelectAllData = function () {
  467. var dataAll = $('#table_data_all').is(':checked');
  468. if (dataAll) {
  469. $('input[name="table_data[]"]').prop('checked', true);
  470. } else {
  471. $('input[name="table_data[]"]').prop('checked', false);
  472. }
  473. };
  474. Export.checkSelectedTables = function () {
  475. $('.export_table_select tbody tr').each(function () {
  476. Export.checkTableSelected(this);
  477. });
  478. };
  479. Export.checkTableSelected = function (row) {
  480. var $row = $(row);
  481. var tableSelect = $row.find('input[name="table_select[]"]');
  482. var strCheck = $row.find('input[name="table_structure[]"]');
  483. var dataCheck = $row.find('input[name="table_data[]"]');
  484. var data = dataCheck.is(':checked:not(:disabled)');
  485. var structure = strCheck.is(':checked:not(:disabled)');
  486. if (data && structure) {
  487. tableSelect.prop({
  488. checked: true,
  489. indeterminate: false
  490. });
  491. $row.addClass('marked');
  492. } else if (data || structure) {
  493. tableSelect.prop({
  494. checked: true,
  495. indeterminate: true
  496. });
  497. $row.removeClass('marked');
  498. } else {
  499. tableSelect.prop({
  500. checked: false,
  501. indeterminate: false
  502. });
  503. $row.removeClass('marked');
  504. }
  505. };
  506. Export.toggleTableSelect = function (row) {
  507. var $row = $(row);
  508. var tableSelected = $row.find('input[name="table_select[]"]').is(':checked');
  509. if (tableSelected) {
  510. $row.find('input[type="checkbox"]:not(:disabled)').prop('checked', true);
  511. $row.addClass('marked');
  512. } else {
  513. $row.find('input[type="checkbox"]:not(:disabled)').prop('checked', false);
  514. $row.removeClass('marked');
  515. }
  516. };
  517. Export.handleAddProcCheckbox = function () {
  518. if ($('#table_structure_all').is(':checked') === true && $('#table_data_all').is(':checked') === true) {
  519. $('#checkbox_sql_procedure_function').prop('checked', true);
  520. } else {
  521. $('#checkbox_sql_procedure_function').prop('checked', false);
  522. }
  523. };
  524. AJAX.registerOnload('export.js', function () {
  525. /**
  526. * For SQL plugin, if "CREATE TABLE options" is checked/unchecked, check/uncheck each of its sub-options
  527. */
  528. var $create = $('#checkbox_sql_create_table_statements');
  529. var $createOptions = $('#ul_create_table_statements').find('input');
  530. $create.on('change', function () {
  531. $createOptions.prop('checked', $(this).prop('checked'));
  532. });
  533. $createOptions.on('change', function () {
  534. if ($createOptions.is(':checked')) {
  535. $create.prop('checked', true);
  536. }
  537. });
  538. /**
  539. * Disables the view output as text option if the output must be saved as a file
  540. */
  541. $('#plugins').on('change', function () {
  542. var activePlugin = $('#plugins').find('option:selected').val();
  543. var forceFile = $('#force_file_' + activePlugin).val();
  544. if (forceFile === 'true') {
  545. if ($('#radio_dump_asfile').prop('checked') !== true) {
  546. $('#radio_dump_asfile').prop('checked', true);
  547. Export.toggleSaveToFile();
  548. }
  549. $('#radio_view_as_text').prop('disabled', true).parent().fadeTo('fast', 0.4);
  550. } else {
  551. $('#radio_view_as_text').prop('disabled', false).parent().fadeTo('fast', 1);
  552. }
  553. });
  554. $('input[type=\'radio\'][name$=\'_structure_or_data\']').on('change', function () {
  555. Export.toggleStructureDataOpts();
  556. });
  557. $('input[name="table_select[]"]').on('change', function () {
  558. Export.toggleTableSelect($(this).closest('tr'));
  559. Export.checkTableSelectAll();
  560. Export.handleAddProcCheckbox();
  561. Export.checkTableSelectStructureOrData();
  562. });
  563. $('input[name="table_structure[]"]').on('change', function () {
  564. Export.checkTableSelected($(this).closest('tr'));
  565. Export.checkTableSelectAll();
  566. Export.handleAddProcCheckbox();
  567. Export.checkTableSelectStructureOrData();
  568. });
  569. $('input[name="table_data[]"]').on('change', function () {
  570. Export.checkTableSelected($(this).closest('tr'));
  571. Export.checkTableSelectAll();
  572. Export.handleAddProcCheckbox();
  573. Export.checkTableSelectStructureOrData();
  574. });
  575. $('#table_structure_all').on('change', function () {
  576. Export.toggleTableSelectAllStr();
  577. Export.checkSelectedTables();
  578. Export.handleAddProcCheckbox();
  579. Export.checkTableSelectStructureOrData();
  580. });
  581. $('#table_data_all').on('change', function () {
  582. Export.toggleTableSelectAllData();
  583. Export.checkSelectedTables();
  584. Export.handleAddProcCheckbox();
  585. Export.checkTableSelectStructureOrData();
  586. });
  587. if ($('input[name=\'export_type\']').val() === 'database') {
  588. // Hide structure or data radio buttons
  589. $('input[type=\'radio\'][name$=\'_structure_or_data\']').each(function () {
  590. var $this = $(this);
  591. var name = $this.prop('name');
  592. var val = $('input[name="' + name + '"]:checked').val();
  593. var nameDefault = name + '_default';
  594. if (!$('input[name="' + nameDefault + '"]').length) {
  595. $this.after($('<input type="hidden" name="' + nameDefault + '" value="' + val + '" disabled>')).after($('<input type="hidden" name="' + name + '" value="structure_and_data">'));
  596. $this.parent().find('label').remove();
  597. } else {
  598. $this.parent().remove();
  599. }
  600. });
  601. $('input[type=\'radio\'][name$=\'_structure_or_data\']').remove(); // Disable CREATE table checkbox for sql
  602. var createTableCheckbox = $('#checkbox_sql_create_table');
  603. createTableCheckbox.prop('checked', true);
  604. var dummyCreateTable = $('#checkbox_sql_create_table').clone().removeAttr('id').attr('type', 'hidden');
  605. createTableCheckbox.prop('disabled', true).after(dummyCreateTable).parent().fadeTo('fast', 0.4);
  606. Export.setupTableStructureOrData();
  607. }
  608. /**
  609. * Handle force structure_or_data
  610. */
  611. $('#plugins').on('change', Export.setupTableStructureOrData);
  612. });
  613. /**
  614. * Toggles display of options when quick and custom export are selected
  615. */
  616. Export.toggleQuickOrCustom = function () {
  617. if ($('input[name=\'quick_or_custom\']').length === 0 // custom_no_form option
  618. || $('#radio_custom_export').prop('checked') // custom
  619. ) {
  620. $('#databases_and_tables').show();
  621. $('#rows').show();
  622. $('#output').show();
  623. $('#format_specific_opts').show();
  624. $('#output_quick_export').hide();
  625. var selectedPluginName = $('#plugins').find('option:selected').val();
  626. $('#' + selectedPluginName + '_options').show();
  627. } else {
  628. // quick
  629. $('#databases_and_tables').hide();
  630. $('#rows').hide();
  631. $('#output').hide();
  632. $('#format_specific_opts').hide();
  633. $('#output_quick_export').show();
  634. }
  635. };
  636. var timeOut;
  637. Export.checkTimeOut = function (timeLimit) {
  638. var limit = timeLimit;
  639. if (typeof limit === 'undefined' || limit === 0) {
  640. return true;
  641. } // margin of one second to avoid race condition to set/access session variable
  642. limit = limit + 1;
  643. clearTimeout(timeOut);
  644. timeOut = setTimeout(function () {
  645. $.get('index.php?route=/export/check-time-out', {
  646. 'ajax_request': true
  647. }, function (data) {
  648. if (data.message === 'timeout') {
  649. Functions.ajaxShowMessage('<div class="alert alert-danger" role="alert">' + Messages.strTimeOutError + '</div>', false);
  650. }
  651. });
  652. }, limit * 1000);
  653. };
  654. /**
  655. * Handler for Database/table alias select
  656. *
  657. * @param event object the event object
  658. *
  659. * @return void
  660. */
  661. Export.aliasSelectHandler = function (event) {
  662. var sel = event.data.sel;
  663. var type = event.data.type;
  664. var inputId = $(this).val();
  665. var $label = $(this).next('label');
  666. $('input#' + $label.attr('for')).addClass('hide');
  667. $('input#' + inputId).removeClass('hide');
  668. $label.attr('for', inputId);
  669. $('#alias_modal ' + sel + '[id$=' + type + ']:visible').addClass('hide');
  670. var $inputWrapper = $('#alias_modal ' + sel + '#' + inputId + type);
  671. $inputWrapper.removeClass('hide');
  672. if (type === '_cols' && $inputWrapper.length > 0) {
  673. var outer = $inputWrapper[0].outerHTML; // Replace opening tags
  674. var regex = /<dummy_inp/gi;
  675. if (outer.match(regex)) {
  676. var newTag = outer.replace(regex, '<input'); // Replace closing tags
  677. regex = /<\/dummy_inp/gi;
  678. newTag = newTag.replace(regex, '</input'); // Assign replacement
  679. $inputWrapper.replaceWith(newTag);
  680. }
  681. } else if (type === '_tables') {
  682. $('.table_alias_select:visible').trigger('change');
  683. }
  684. $('#alias_modal').dialog('option', 'position', 'center');
  685. };
  686. /**
  687. * Handler for Alias dialog box
  688. *
  689. * @param event object the event object
  690. *
  691. * @return void
  692. */
  693. Export.createAliasModal = function (event) {
  694. event.preventDefault();
  695. var dlgButtons = {};
  696. dlgButtons[Messages.strSaveAndClose] = function () {
  697. $(this).dialog('close');
  698. $('#alias_modal').parent().appendTo($('form[name="dump"]'));
  699. };
  700. $('#alias_modal').dialog({
  701. width: Math.min($(window).width() - 100, 700),
  702. maxHeight: $(window).height(),
  703. modal: true,
  704. dialogClass: 'alias-dialog',
  705. buttons: dlgButtons,
  706. create: function create() {
  707. $(this).closest('.ui-dialog').find('.ui-button').addClass('btn btn-secondary');
  708. $(this).css('maxHeight', $(window).height() - 150);
  709. var db = CommonParams.get('db');
  710. if (db) {
  711. var option = $('<option></option>');
  712. option.text(db);
  713. option.attr('value', db);
  714. $('#db_alias_select').append(option).val(db).trigger('change');
  715. } else {
  716. var params = {
  717. 'ajax_request': true,
  718. 'server': CommonParams.get('server')
  719. };
  720. $.post('index.php?route=/databases', params, function (response) {
  721. if (response.success === true) {
  722. $.each(response.databases, function (idx, value) {
  723. var option = $('<option></option>');
  724. option.text(value);
  725. option.attr('value', value);
  726. $('#db_alias_select').append(option);
  727. });
  728. } else {
  729. Functions.ajaxShowMessage(response.error, false);
  730. }
  731. });
  732. }
  733. },
  734. close: function close() {
  735. var isEmpty = true;
  736. $(this).find('input[type="text"]').each(function () {
  737. // trim empty input fields on close
  738. if ($(this).val()) {
  739. isEmpty = false;
  740. } else {
  741. $(this).parents('tr').remove();
  742. }
  743. }); // Toggle checkbox based on aliases
  744. $('input#btn_alias_config').prop('checked', !isEmpty);
  745. },
  746. position: {
  747. my: 'center top',
  748. at: 'center top',
  749. of: window
  750. }
  751. });
  752. };
  753. Export.aliasToggleRow = function (elm) {
  754. var inputs = elm.parents('tr').find('input,button');
  755. if (elm.val()) {
  756. inputs.attr('disabled', false);
  757. } else {
  758. inputs.attr('disabled', true);
  759. }
  760. };
  761. Export.aliasRow = null;
  762. Export.addAlias = function (type, name, field, value) {
  763. if (value === '') {
  764. return;
  765. }
  766. if (Export.aliasRow === null) {
  767. Export.aliasRow = $('#alias_data tfoot tr');
  768. }
  769. var row = Export.aliasRow.clone();
  770. row.find('th').text(type);
  771. row.find('td').first().text(name);
  772. row.find('input').attr('name', field);
  773. row.find('input').val(value);
  774. row.find('.alias_remove').on('click', function () {
  775. $(this).parents('tr').remove();
  776. });
  777. var matching = $('#alias_data [name="' + $.escapeSelector(field) + '"]');
  778. if (matching.length > 0) {
  779. matching.parents('tr').remove();
  780. }
  781. $('#alias_data tbody').append(row);
  782. };
  783. AJAX.registerOnload('export.js', function () {
  784. $('input[type=\'radio\'][name=\'quick_or_custom\']').on('change', Export.toggleQuickOrCustom);
  785. $('#scroll_to_options_msg').hide();
  786. $('#format_specific_opts').find('div.format_specific_options').hide().css({
  787. 'border': 0,
  788. 'margin': 0,
  789. 'padding': 0
  790. }).find('h3').remove();
  791. Export.toggleQuickOrCustom();
  792. Export.toggleStructureDataOpts();
  793. Export.toggleSqlIncludeComments();
  794. Export.checkTableSelectAll();
  795. Export.handleAddProcCheckbox();
  796. /**
  797. * Initially disables the "Dump some row(s)" sub-options
  798. */
  799. Export.disableDumpSomeRowsSubOptions();
  800. /**
  801. * Disables the "Dump some row(s)" sub-options when it is not selected
  802. */
  803. $('input[type=\'radio\'][name=\'allrows\']').on('change', function () {
  804. if ($('input[type=\'radio\'][name=\'allrows\']').prop('checked')) {
  805. Export.enableDumpSomeRowsSubOptions();
  806. } else {
  807. Export.disableDumpSomeRowsSubOptions();
  808. }
  809. }); // Open Alias Modal Dialog on click
  810. $('#btn_alias_config').on('click', Export.createAliasModal);
  811. $('.alias_remove').on('click', function () {
  812. $(this).parents('tr').remove();
  813. });
  814. $('#db_alias_select').on('change', function () {
  815. Export.aliasToggleRow($(this));
  816. var table = CommonParams.get('table');
  817. if (table) {
  818. var option = $('<option></option>');
  819. option.text(table);
  820. option.attr('value', table);
  821. $('#table_alias_select').append(option).val(table).trigger('change');
  822. } else {
  823. var database = $(this).val();
  824. var params = {
  825. 'ajax_request': true,
  826. 'server': CommonParams.get('server'),
  827. 'db': database
  828. };
  829. var url = 'index.php?route=/tables';
  830. $.post(url, params, function (response) {
  831. if (response.success === true) {
  832. $.each(response.tables, function (idx, value) {
  833. var option = $('<option></option>');
  834. option.text(value);
  835. option.attr('value', value);
  836. $('#table_alias_select').append(option);
  837. });
  838. } else {
  839. Functions.ajaxShowMessage(response.error, false);
  840. }
  841. });
  842. }
  843. });
  844. $('#table_alias_select').on('change', function () {
  845. Export.aliasToggleRow($(this));
  846. var database = $('#db_alias_select').val();
  847. var table = $(this).val();
  848. var params = {
  849. 'ajax_request': true,
  850. 'server': CommonParams.get('server'),
  851. 'db': database,
  852. 'table': table
  853. };
  854. var url = 'index.php?route=/columns';
  855. $.post(url, params, function (response) {
  856. if (response.success === true) {
  857. $.each(response.columns, function (idx, value) {
  858. var option = $('<option></option>');
  859. option.text(value);
  860. option.attr('value', value);
  861. $('#column_alias_select').append(option);
  862. });
  863. } else {
  864. Functions.ajaxShowMessage(response.error, false);
  865. }
  866. });
  867. });
  868. $('#column_alias_select').on('change', function () {
  869. Export.aliasToggleRow($(this));
  870. });
  871. $('#db_alias_button').on('click', function (e) {
  872. e.preventDefault();
  873. var db = $('#db_alias_select').val();
  874. Export.addAlias(Messages.strAliasDatabase, db, 'aliases[' + db + '][alias]', $('#db_alias_name').val());
  875. $('#db_alias_name').val('');
  876. });
  877. $('#table_alias_button').on('click', function (e) {
  878. e.preventDefault();
  879. var db = $('#db_alias_select').val();
  880. var table = $('#table_alias_select').val();
  881. Export.addAlias(Messages.strAliasTable, db + '.' + table, 'aliases[' + db + '][tables][' + table + '][alias]', $('#table_alias_name').val());
  882. $('#table_alias_name').val('');
  883. });
  884. $('#column_alias_button').on('click', function (e) {
  885. e.preventDefault();
  886. var db = $('#db_alias_select').val();
  887. var table = $('#table_alias_select').val();
  888. var column = $('#column_alias_select').val();
  889. Export.addAlias(Messages.strAliasColumn, db + '.' + table + '.' + column, 'aliases[' + db + '][tables][' + table + '][colums][' + column + ']', $('#column_alias_name').val());
  890. $('#column_alias_name').val('');
  891. });
  892. var setSelectOptions = function setSelectOptions(doCheck) {
  893. Functions.setSelectOptions('dump', 'db_select[]', doCheck);
  894. };
  895. $('#db_select_all').on('click', function (e) {
  896. e.preventDefault();
  897. setSelectOptions(true);
  898. });
  899. $('#db_unselect_all').on('click', function (e) {
  900. e.preventDefault();
  901. setSelectOptions(false);
  902. });
  903. $('#buttonGo').on('click', function () {
  904. var timeLimit = parseInt($(this).attr('data-exec-time-limit')); // If the time limit set is zero,
  905. // then time out won't occur so no need to check for time out.
  906. if (timeLimit > 0) {
  907. Export.checkTimeOut(timeLimit);
  908. }
  909. });
  910. });