export.js 34 KB

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