export.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  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. if (show === 'data') {
  358. $(dataDiv).slideDown('slow');
  359. $(structureDiv).slideUp('slow');
  360. } else {
  361. $(structureDiv).slideDown('slow');
  362. if (show === 'structure') {
  363. $(dataDiv).slideUp('slow');
  364. } else {
  365. $(dataDiv).slideDown('slow');
  366. }
  367. }
  368. }
  369. /**
  370. * Toggles the disabling of the "save to file" options
  371. */
  372. function toggle_save_to_file () {
  373. var $ulSaveAsfile = $('#ul_save_asfile');
  374. if (!$('#radio_dump_asfile').prop('checked')) {
  375. $ulSaveAsfile.find('> li').fadeTo('fast', 0.4);
  376. $ulSaveAsfile.find('> li > input').prop('disabled', true);
  377. $ulSaveAsfile.find('> li > select').prop('disabled', true);
  378. } else {
  379. $ulSaveAsfile.find('> li').fadeTo('fast', 1);
  380. $ulSaveAsfile.find('> li > input').prop('disabled', false);
  381. $ulSaveAsfile.find('> li > select').prop('disabled', false);
  382. }
  383. }
  384. AJAX.registerOnload('export.js', function () {
  385. toggle_save_to_file();
  386. $('input[type=\'radio\'][name=\'output_format\']').change(toggle_save_to_file);
  387. });
  388. /**
  389. * For SQL plugin, toggles the disabling of the "display comments" options
  390. */
  391. function toggle_sql_include_comments () {
  392. $('#checkbox_sql_include_comments').change(function () {
  393. var $ulIncludeComments = $('#ul_include_comments');
  394. if (!$('#checkbox_sql_include_comments').prop('checked')) {
  395. $ulIncludeComments.find('> li').fadeTo('fast', 0.4);
  396. $ulIncludeComments.find('> li > input').prop('disabled', true);
  397. } else {
  398. // If structure is not being exported, the comment options for structure should not be enabled
  399. if ($('#radio_sql_structure_or_data_data').prop('checked')) {
  400. $('#text_sql_header_comment').prop('disabled', false).parent('li').fadeTo('fast', 1);
  401. } else {
  402. $ulIncludeComments.find('> li').fadeTo('fast', 1);
  403. $ulIncludeComments.find('> li > input').prop('disabled', false);
  404. }
  405. }
  406. });
  407. }
  408. function check_table_select_all () {
  409. var total = $('input[name="table_select[]"]').length;
  410. var str_checked = $('input[name="table_structure[]"]:checked').length;
  411. var data_checked = $('input[name="table_data[]"]:checked').length;
  412. var str_all = $('#table_structure_all');
  413. var data_all = $('#table_data_all');
  414. if (str_checked === total) {
  415. str_all
  416. .prop('indeterminate', false)
  417. .prop('checked', true);
  418. } else if (str_checked === 0) {
  419. str_all
  420. .prop('indeterminate', false)
  421. .prop('checked', false);
  422. } else {
  423. str_all
  424. .prop('indeterminate', true)
  425. .prop('checked', false);
  426. }
  427. if (data_checked === total) {
  428. data_all
  429. .prop('indeterminate', false)
  430. .prop('checked', true);
  431. } else if (data_checked === 0) {
  432. data_all
  433. .prop('indeterminate', false)
  434. .prop('checked', false);
  435. } else {
  436. data_all
  437. .prop('indeterminate', true)
  438. .prop('checked', false);
  439. }
  440. }
  441. function check_table_select_struture_or_data () {
  442. var str_checked = $('input[name="table_structure[]"]:checked').length;
  443. var data_checked = $('input[name="table_data[]"]:checked').length;
  444. var auto_increment = $('#checkbox_sql_auto_increment');
  445. var pluginName = $('select#plugins').val();
  446. var dataDiv = '#' + pluginName + '_data';
  447. var structureDiv = '#' + pluginName + '_structure';
  448. if (str_checked === 0) {
  449. $(structureDiv).slideUp('slow');
  450. } else {
  451. $(structureDiv).slideDown('slow');
  452. }
  453. if (data_checked === 0) {
  454. $(dataDiv).slideUp('slow');
  455. auto_increment.prop('disabled', true).parent().fadeTo('fast', 0.4);
  456. } else {
  457. $(dataDiv).slideDown('slow');
  458. auto_increment.prop('disabled', false).parent().fadeTo('fast', 1);
  459. }
  460. }
  461. function toggle_table_select_all_str () {
  462. var str_all = $('#table_structure_all').is(':checked');
  463. if (str_all) {
  464. $('input[name="table_structure[]"]').prop('checked', true);
  465. } else {
  466. $('input[name="table_structure[]"]').prop('checked', false);
  467. }
  468. }
  469. function toggle_table_select_all_data () {
  470. var data_all = $('#table_data_all').is(':checked');
  471. if (data_all) {
  472. $('input[name="table_data[]"]').prop('checked', true);
  473. } else {
  474. $('input[name="table_data[]"]').prop('checked', false);
  475. }
  476. }
  477. function check_selected_tables (argument) {
  478. $('.export_table_select tbody tr').each(function () {
  479. check_table_selected(this);
  480. });
  481. }
  482. function check_table_selected (row) {
  483. var $row = $(row);
  484. var table_select = $row.find('input[name="table_select[]"]');
  485. var str_check = $row.find('input[name="table_structure[]"]');
  486. var data_check = $row.find('input[name="table_data[]"]');
  487. var data = data_check.is(':checked:not(:disabled)');
  488. var structure = str_check.is(':checked:not(:disabled)');
  489. if (data && structure) {
  490. table_select.prop({ checked: true, indeterminate: false });
  491. $row.addClass('marked');
  492. } else if (data || structure) {
  493. table_select.prop({ checked: true, indeterminate: true });
  494. $row.removeClass('marked');
  495. } else {
  496. table_select.prop({ checked: false, indeterminate: false });
  497. $row.removeClass('marked');
  498. }
  499. }
  500. function toggle_table_select (row) {
  501. var $row = $(row);
  502. var table_selected = $row.find('input[name="table_select[]"]').is(':checked');
  503. if (table_selected) {
  504. $row.find('input[type="checkbox"]:not(:disabled)').prop('checked', true);
  505. $row.addClass('marked');
  506. } else {
  507. $row.find('input[type="checkbox"]:not(:disabled)').prop('checked', false);
  508. $row.removeClass('marked');
  509. }
  510. }
  511. function handleAddProcCheckbox () {
  512. if ($('#table_structure_all').is(':checked') === true
  513. && $('#table_data_all').is(':checked') === true
  514. ) {
  515. $('#checkbox_sql_procedure_function').prop('checked', true);
  516. } else {
  517. $('#checkbox_sql_procedure_function').prop('checked', false);
  518. }
  519. }
  520. AJAX.registerOnload('export.js', function () {
  521. /**
  522. * For SQL plugin, if "CREATE TABLE options" is checked/unchecked, check/uncheck each of its sub-options
  523. */
  524. var $create = $('#checkbox_sql_create_table_statements');
  525. var $create_options = $('#ul_create_table_statements').find('input');
  526. $create.change(function () {
  527. $create_options.prop('checked', $(this).prop('checked'));
  528. });
  529. $create_options.change(function () {
  530. if ($create_options.is(':checked')) {
  531. $create.prop('checked', true);
  532. }
  533. });
  534. /**
  535. * Disables the view output as text option if the output must be saved as a file
  536. */
  537. $('#plugins').change(function () {
  538. var active_plugin = $('#plugins').find('option:selected').val();
  539. var force_file = $('#force_file_' + active_plugin).val();
  540. if (force_file === 'true') {
  541. if ($('#radio_dump_asfile').prop('checked') !== true) {
  542. $('#radio_dump_asfile').prop('checked', true);
  543. toggle_save_to_file();
  544. }
  545. $('#radio_view_as_text').prop('disabled', true).parent().fadeTo('fast', 0.4);
  546. } else {
  547. $('#radio_view_as_text').prop('disabled', false).parent().fadeTo('fast', 1);
  548. }
  549. });
  550. $('input[type=\'radio\'][name$=\'_structure_or_data\']').on('change', function () {
  551. toggle_structure_data_opts();
  552. });
  553. $('input[name="table_select[]"]').on('change', function () {
  554. toggle_table_select($(this).closest('tr'));
  555. check_table_select_all();
  556. handleAddProcCheckbox();
  557. check_table_select_struture_or_data();
  558. });
  559. $('input[name="table_structure[]"]').on('change', function () {
  560. check_table_selected($(this).closest('tr'));
  561. check_table_select_all();
  562. handleAddProcCheckbox();
  563. check_table_select_struture_or_data();
  564. });
  565. $('input[name="table_data[]"]').on('change', function () {
  566. check_table_selected($(this).closest('tr'));
  567. check_table_select_all();
  568. handleAddProcCheckbox();
  569. check_table_select_struture_or_data();
  570. });
  571. $('#table_structure_all').on('change', function () {
  572. toggle_table_select_all_str();
  573. check_selected_tables();
  574. handleAddProcCheckbox();
  575. check_table_select_struture_or_data();
  576. });
  577. $('#table_data_all').on('change', function () {
  578. toggle_table_select_all_data();
  579. check_selected_tables();
  580. handleAddProcCheckbox();
  581. check_table_select_struture_or_data();
  582. });
  583. if ($('input[name=\'export_type\']').val() === 'database') {
  584. // Hide structure or data radio buttons
  585. $('input[type=\'radio\'][name$=\'_structure_or_data\']').each(function () {
  586. var $this = $(this);
  587. var name = $this.prop('name');
  588. var val = $('input[name="' + name + '"]:checked').val();
  589. var name_default = name + '_default';
  590. if (!$('input[name="' + name_default + '"]').length) {
  591. $this
  592. .after(
  593. $('<input type="hidden" name="' + name_default + '" value="' + val + '" disabled>')
  594. )
  595. .after(
  596. $('<input type="hidden" name="' + name + '" value="structure_and_data">')
  597. );
  598. $this.parent().find('label').remove();
  599. } else {
  600. $this.parent().remove();
  601. }
  602. });
  603. $('input[type=\'radio\'][name$=\'_structure_or_data\']').remove();
  604. // Disable CREATE table checkbox for sql
  605. var createTableCheckbox = $('#checkbox_sql_create_table');
  606. createTableCheckbox.prop('checked', true);
  607. var dummyCreateTable = $('#checkbox_sql_create_table')
  608. .clone()
  609. .removeAttr('id')
  610. .attr('type', 'hidden');
  611. createTableCheckbox
  612. .prop('disabled', true)
  613. .after(dummyCreateTable)
  614. .parent()
  615. .fadeTo('fast', 0.4);
  616. setup_table_structure_or_data();
  617. }
  618. /**
  619. * Handle force structure_or_data
  620. */
  621. $('#plugins').change(setup_table_structure_or_data);
  622. });
  623. /**
  624. * Toggles display of options when quick and custom export are selected
  625. */
  626. function toggle_quick_or_custom () {
  627. if ($('input[name=\'quick_or_custom\']').length === 0 // custom_no_form option
  628. || $('#radio_custom_export').prop('checked') // custom
  629. ) {
  630. $('#databases_and_tables').show();
  631. $('#rows').show();
  632. $('#output').show();
  633. $('#format_specific_opts').show();
  634. $('#output_quick_export').hide();
  635. var selected_plugin_name = $('#plugins').find('option:selected').val();
  636. $('#' + selected_plugin_name + '_options').show();
  637. } else { // quick
  638. $('#databases_and_tables').hide();
  639. $('#rows').hide();
  640. $('#output').hide();
  641. $('#format_specific_opts').hide();
  642. $('#output_quick_export').show();
  643. }
  644. }
  645. var time_out;
  646. function check_time_out (time_limit) {
  647. if (typeof time_limit === 'undefined' || time_limit === 0) {
  648. return true;
  649. }
  650. // margin of one second to avoid race condition to set/access session variable
  651. time_limit = time_limit + 1;
  652. var href = 'export.php';
  653. var params = {
  654. 'ajax_request' : true,
  655. 'check_time_out' : true
  656. };
  657. clearTimeout(time_out);
  658. time_out = setTimeout(function () {
  659. $.get(href, params, function (data) {
  660. if (data.message === 'timeout') {
  661. PMA_ajaxShowMessage(
  662. '<div class="error">' +
  663. PMA_messages.strTimeOutError +
  664. '</div>',
  665. false
  666. );
  667. }
  668. });
  669. }, time_limit * 1000);
  670. }
  671. /**
  672. * Handler for Database/table alias select
  673. *
  674. * @param event object the event object
  675. *
  676. * @return void
  677. */
  678. function aliasSelectHandler (event) {
  679. var sel = event.data.sel;
  680. var type = event.data.type;
  681. var inputId = $(this).val();
  682. var $label = $(this).next('label');
  683. $('input#' + $label.attr('for')).addClass('hide');
  684. $('input#' + inputId).removeClass('hide');
  685. $label.attr('for', inputId);
  686. $('#alias_modal ' + sel + '[id$=' + type + ']:visible').addClass('hide');
  687. var $inputWrapper = $('#alias_modal ' + sel + '#' + inputId + type);
  688. $inputWrapper.removeClass('hide');
  689. if (type === '_cols' && $inputWrapper.length > 0) {
  690. var outer = $inputWrapper[0].outerHTML;
  691. // Replace opening tags
  692. var regex = /<dummy_inp/gi;
  693. if (outer.match(regex)) {
  694. var newTag = outer.replace(regex, '<input');
  695. // Replace closing tags
  696. regex = /<\/dummy_inp/gi;
  697. newTag = newTag.replace(regex, '</input');
  698. // Assign replacement
  699. $inputWrapper.replaceWith(newTag);
  700. }
  701. } else if (type === '_tables') {
  702. $('.table_alias_select:visible').change();
  703. }
  704. $('#alias_modal').dialog('option', 'position', 'center');
  705. }
  706. /**
  707. * Handler for Alias dialog box
  708. *
  709. * @param event object the event object
  710. *
  711. * @return void
  712. */
  713. function createAliasModal (event) {
  714. event.preventDefault();
  715. var dlgButtons = {};
  716. dlgButtons[PMA_messages.strSaveAndClose] = function () {
  717. $(this).dialog('close');
  718. $('#alias_modal').parent().appendTo($('form[name="dump"]'));
  719. };
  720. $('#alias_modal').dialog({
  721. width: Math.min($(window).width() - 100, 700),
  722. maxHeight: $(window).height(),
  723. modal: true,
  724. dialogClass: 'alias-dialog',
  725. buttons: dlgButtons,
  726. create: function () {
  727. $(this).css('maxHeight', $(window).height() - 150);
  728. var db = PMA_commonParams.get('db');
  729. if (db) {
  730. var option = $('<option></option>');
  731. option.text(db);
  732. option.attr('value', db);
  733. $('#db_alias_select').append(option).val(db).change();
  734. } else {
  735. var params = {
  736. ajax_request : true,
  737. server : PMA_commonParams.get('server'),
  738. type: 'list-databases'
  739. };
  740. $.post('ajax.php', params, function (response) {
  741. if (response.success === true) {
  742. $.each(response.databases, function (idx, value) {
  743. var option = $('<option></option>');
  744. option.text(value);
  745. option.attr('value', value);
  746. $('#db_alias_select').append(option);
  747. });
  748. } else {
  749. PMA_ajaxShowMessage(response.error, false);
  750. }
  751. });
  752. }
  753. },
  754. close: function () {
  755. var isEmpty = true;
  756. $(this).find('input[type="text"]').each(function () {
  757. // trim empty input fields on close
  758. if ($(this).val()) {
  759. isEmpty = false;
  760. } else {
  761. $(this).parents('tr').remove();
  762. }
  763. });
  764. // Toggle checkbox based on aliases
  765. $('input#btn_alias_config').prop('checked', !isEmpty);
  766. },
  767. position: { my: 'center top', at: 'center top', of: window }
  768. });
  769. }
  770. function aliasToggleRow (elm) {
  771. var inputs = elm.parents('tr').find('input,button');
  772. if (elm.val()) {
  773. inputs.attr('disabled', false);
  774. } else {
  775. inputs.attr('disabled', true);
  776. }
  777. }
  778. function addAlias (type, name, field, value) {
  779. if (value === '') {
  780. return;
  781. }
  782. var row = $('#alias_data tfoot tr').clone();
  783. row.find('th').text(type);
  784. row.find('td:first').text(name);
  785. row.find('input').attr('name', field);
  786. row.find('input').val(value);
  787. row.find('.alias_remove').on('click', function () {
  788. $(this).parents('tr').remove();
  789. });
  790. var matching = $('#alias_data [name="' + $.escapeSelector(field) + '"]');
  791. if (matching.length > 0) {
  792. matching.parents('tr').remove();
  793. }
  794. $('#alias_data tbody').append(row);
  795. }
  796. AJAX.registerOnload('export.js', function () {
  797. $('input[type=\'radio\'][name=\'quick_or_custom\']').change(toggle_quick_or_custom);
  798. $('#scroll_to_options_msg').hide();
  799. $('#format_specific_opts').find('div.format_specific_options')
  800. .hide()
  801. .css({
  802. 'border': 0,
  803. 'margin': 0,
  804. 'padding': 0
  805. })
  806. .find('h3')
  807. .remove();
  808. toggle_quick_or_custom();
  809. toggle_structure_data_opts();
  810. toggle_sql_include_comments();
  811. check_table_select_all();
  812. handleAddProcCheckbox();
  813. /**
  814. * Initially disables the "Dump some row(s)" sub-options
  815. */
  816. disable_dump_some_rows_sub_options();
  817. /**
  818. * Disables the "Dump some row(s)" sub-options when it is not selected
  819. */
  820. $('input[type=\'radio\'][name=\'allrows\']').change(function () {
  821. if ($('input[type=\'radio\'][name=\'allrows\']').prop('checked')) {
  822. enable_dump_some_rows_sub_options();
  823. } else {
  824. disable_dump_some_rows_sub_options();
  825. }
  826. });
  827. // Open Alias Modal Dialog on click
  828. $('#btn_alias_config').on('click', createAliasModal);
  829. $('.alias_remove').on('click', function () {
  830. $(this).parents('tr').remove();
  831. });
  832. $('#db_alias_select').on('change', function () {
  833. aliasToggleRow($(this));
  834. var db = $(this).val();
  835. var table = PMA_commonParams.get('table');
  836. if (table) {
  837. var option = $('<option></option>');
  838. option.text(table);
  839. option.attr('value', table);
  840. $('#table_alias_select').append(option).val(table).change();
  841. } else {
  842. var params = {
  843. ajax_request : true,
  844. server : PMA_commonParams.get('server'),
  845. db : $(this).val(),
  846. type: 'list-tables'
  847. };
  848. $.post('ajax.php', params, function (response) {
  849. if (response.success === true) {
  850. $.each(response.tables, function (idx, value) {
  851. var option = $('<option></option>');
  852. option.text(value);
  853. option.attr('value', value);
  854. $('#table_alias_select').append(option);
  855. });
  856. } else {
  857. PMA_ajaxShowMessage(response.error, false);
  858. }
  859. });
  860. }
  861. });
  862. $('#table_alias_select').on('change', function () {
  863. aliasToggleRow($(this));
  864. var params = {
  865. ajax_request : true,
  866. server : PMA_commonParams.get('server'),
  867. db : $('#db_alias_select').val(),
  868. table: $(this).val(),
  869. type: 'list-columns'
  870. };
  871. $.post('ajax.php', params, function (response) {
  872. if (response.success === true) {
  873. $.each(response.columns, function (idx, value) {
  874. var option = $('<option></option>');
  875. option.text(value);
  876. option.attr('value', value);
  877. $('#column_alias_select').append(option);
  878. });
  879. } else {
  880. PMA_ajaxShowMessage(response.error, false);
  881. }
  882. });
  883. });
  884. $('#column_alias_select').on('change', function () {
  885. aliasToggleRow($(this));
  886. });
  887. $('#db_alias_button').on('click', function (e) {
  888. e.preventDefault();
  889. var db = $('#db_alias_select').val();
  890. addAlias(
  891. PMA_messages.strAliasDatabase,
  892. db,
  893. 'aliases[' + db + '][alias]',
  894. $('#db_alias_name').val()
  895. );
  896. $('#db_alias_name').val('');
  897. });
  898. $('#table_alias_button').on('click', function (e) {
  899. e.preventDefault();
  900. var db = $('#db_alias_select').val();
  901. var table = $('#table_alias_select').val();
  902. addAlias(
  903. PMA_messages.strAliasTable,
  904. db + '.' + table,
  905. 'aliases[' + db + '][tables][' + table + '][alias]',
  906. $('#table_alias_name').val()
  907. );
  908. $('#table_alias_name').val('');
  909. });
  910. $('#column_alias_button').on('click', function (e) {
  911. e.preventDefault();
  912. var db = $('#db_alias_select').val();
  913. var table = $('#table_alias_select').val();
  914. var column = $('#column_alias_select').val();
  915. addAlias(
  916. PMA_messages.strAliasColumn,
  917. db + '.' + table + '.' + column,
  918. 'aliases[' + db + '][tables][' + table + '][colums][' + column + ']',
  919. $('#column_alias_name').val()
  920. );
  921. $('#column_alias_name').val('');
  922. });
  923. });