1 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 * Functions used in the export tab
8 * Disables the "Dump some row(s)" sub-options
10 function disable_dump_some_rows_sub_options()
12 $("label[for='limit_to']").fadeTo('fast', 0.4);
13 $("label[for='limit_from']").fadeTo('fast', 0.4);
14 $("input[type='text'][name='limit_to']").prop('disabled', 'disabled');
15 $("input[type='text'][name='limit_from']").prop('disabled', 'disabled');
19 * Enables the "Dump some row(s)" sub-options
21 function enable_dump_some_rows_sub_options()
23 $("label[for='limit_to']").fadeTo('fast', 1);
24 $("label[for='limit_from']").fadeTo('fast', 1);
25 $("input[type='text'][name='limit_to']").prop('disabled', '');
26 $("input[type='text'][name='limit_from']").prop('disabled', '');
30 * Return template data as a json object
32 * @returns template data
34 function getTemplateData()
36 var $form = $('form[name="dump"]');
37 var blacklist = ['token', 'server', 'db', 'table', 'single_table',
38 'export_type', 'export_method', 'sql_query', 'template_id'];
40 var arr = $form.serializeArray();
41 $.each(arr, function () {
42 if ($.inArray(this.name, blacklist) < 0) {
43 if (obj[this.name] !== undefined) {
44 if (! obj[this.name].push) {
45 obj[this.name] = [obj[this.name]];
47 obj[this.name].push(this.value || '');
49 obj[this.name] = this.value || '';
53 // include unchecked checboxes (which are ignored by serializeArray()) with null
54 // to uncheck them when loading the template
55 $form.find('input[type="checkbox"]:not(:checked)').each(function () {
56 if (obj[this.name] === undefined) {
57 obj[this.name] = null;
60 // include empty multiselects
61 $form.find('select').each(function () {
62 if ($(this).find('option:selected').length == 0) {
70 * Create a template with selected options
72 * @param name name of the template
74 function createTemplate(name)
76 var templateData = getTemplateData();
80 token : PMA_commonParams.get('token'),
81 server : PMA_commonParams.get('server'),
82 db : PMA_commonParams.get('db'),
83 table : PMA_commonParams.get('table'),
84 exportType : $('input[name="export_type"]').val(),
85 templateAction : 'create',
87 templateData : JSON.stringify(templateData)
90 PMA_ajaxShowMessage();
91 $.post('tbl_export.php', params, function (response) {
92 if (response.success === true) {
93 $('#templateName').val('');
94 $('#template').html(response.data);
95 $("#template").find("option").each(function() {
96 if ($(this).text() == name) {
97 $(this).prop('selected', true);
100 PMA_ajaxShowMessage(PMA_messages.strTemplateCreated);
102 PMA_ajaxShowMessage(response.error, false);
110 * @param id ID of the template to load
112 function loadTemplate(id)
116 token : PMA_commonParams.get('token'),
117 server : PMA_commonParams.get('server'),
118 db : PMA_commonParams.get('db'),
119 table : PMA_commonParams.get('table'),
120 exportType : $('input[name="export_type"]').val(),
121 templateAction : 'load',
125 PMA_ajaxShowMessage();
126 $.post('tbl_export.php', params, function (response) {
127 if (response.success === true) {
128 var $form = $('form[name="dump"]');
129 var options = JSON.parse(response.data);
130 $.each(options, function (key, value) {
131 var $element = $form.find('[name="' + key + '"]');
132 if ($element.length) {
133 if (($element.is('input') && $element.attr('type') == 'checkbox') && value === null) {
134 $element.prop('checked', false);
136 if (($element.is('input') && $element.attr('type') == 'checkbox') ||
137 ($element.is('input') && $element.attr('type') == 'radio') ||
138 ($element.is('select') && $element.attr('multiple') == 'multiple')) {
145 $element.trigger('change');
148 $('input[name="template_id"]').val(id);
149 PMA_ajaxShowMessage(PMA_messages.strTemplateLoaded);
151 PMA_ajaxShowMessage(response.error, false);
157 * Updates an existing template with current options
159 * @param id ID of the template to update
161 function updateTemplate(id)
163 var templateData = getTemplateData();
167 token : PMA_commonParams.get('token'),
168 server : PMA_commonParams.get('server'),
169 db : PMA_commonParams.get('db'),
170 table : PMA_commonParams.get('table'),
171 exportType : $('input[name="export_type"]').val(),
172 templateAction : 'update',
174 templateData : JSON.stringify(templateData)
177 PMA_ajaxShowMessage();
178 $.post('tbl_export.php', params, function (response) {
179 if (response.success === true) {
180 PMA_ajaxShowMessage(PMA_messages.strTemplateUpdated);
182 PMA_ajaxShowMessage(response.error, false);
190 * @param id ID of the template to delete
192 function deleteTemplate(id)
196 token : PMA_commonParams.get('token'),
197 server : PMA_commonParams.get('server'),
198 db : PMA_commonParams.get('db'),
199 table : PMA_commonParams.get('table'),
200 exportType : $('input[name="export_type"]').val(),
201 templateAction : 'delete',
205 PMA_ajaxShowMessage();
206 $.post('tbl_export.php', params, function (response) {
207 if (response.success === true) {
208 $('#template').find('option[value="' + id + '"]').remove();
209 PMA_ajaxShowMessage(PMA_messages.strTemplateDeleted);
211 PMA_ajaxShowMessage(response.error, false);
217 * Unbind all event handlers before tearing down a page
219 AJAX.registerTeardown('export.js', function () {
220 $("#plugins").off('change');
221 $("input[type='radio'][name='sql_structure_or_data']").off('change');
222 $("input[type='radio'][name$='_structure_or_data']").off('change');
223 $("input[type='radio'][name='output_format']").off('change');
224 $("#checkbox_sql_include_comments").off('change');
225 $("input[type='radio'][name='quick_or_custom']").off('change');
226 $("input[type='radio'][name='allrows']").off('change');
227 $('#btn_alias_config').off('click');
228 $('#db_alias_select').off('change');
229 $('.table_alias_select').off('change');
230 $('input[name="table_select[]"]').off('change');
231 $('input[name="table_structure[]"]').off('change');
232 $('input[name="table_data[]"]').off('change');
233 $('#table_structure_all').off('change');
234 $('#table_data_all').off('change');
235 $('input[name="createTemplate"]').off('click');
236 $('select[name="template"]').off('change');
237 $('input[name="updateTemplate"]').off('click');
238 $('input[name="deleteTemplate"]').off('click');
241 AJAX.registerOnload('export.js', function () {
244 * Export template handling code
246 // create a new template
247 $('input[name="createTemplate"]').on('click', function (e) {
249 var name = $('input[name="templateName"]').val();
251 createTemplate(name);
255 // load an existing template
256 $('select[name="template"]').on('change', function (e) {
258 var id = $(this).val();
264 // udpate an existing template with new criteria
265 $('input[name="updateTemplate"]').on('click', function (e) {
267 var id = $('select[name="template"]').val();
273 // delete an existing template
274 $('input[name="deleteTemplate"]').on('click', function (e) {
276 var id = $('select[name="template"]').val();
283 * Toggles the hiding and showing of each plugin's options
284 * according to the currently selected plugin from the dropdown list
286 $("#plugins").change(function () {
287 $("#format_specific_opts").find("div.format_specific_options").hide();
288 var selected_plugin_name = $("#plugins").find("option:selected").val();
289 $("#" + selected_plugin_name + "_options").show();
293 * Toggles the enabling and disabling of the SQL plugin's comment options that apply only when exporting structure
295 $("input[type='radio'][name='sql_structure_or_data']").change(function () {
296 var comments_are_present = $("#checkbox_sql_include_comments").prop("checked");
297 var show = $("input[type='radio'][name='sql_structure_or_data']:checked").val();
298 if (show == 'data') {
299 // disable the SQL comment options
300 if (comments_are_present) {
301 $("#checkbox_sql_dates").prop('disabled', true).parent().fadeTo('fast', 0.4);
303 $("#checkbox_sql_relation").prop('disabled', true).parent().fadeTo('fast', 0.4);
304 $("#checkbox_sql_mime").prop('disabled', true).parent().fadeTo('fast', 0.4);
306 // enable the SQL comment options
307 if (comments_are_present) {
308 $("#checkbox_sql_dates").prop('disabled', false).parent().fadeTo('fast', 1);
310 $("#checkbox_sql_relation").prop('disabled', false).parent().fadeTo('fast', 1);
311 $("#checkbox_sql_mime").prop('disabled', false).parent().fadeTo('fast', 1);
314 if (show == 'structure') {
315 $('#checkbox_sql_auto_increment').prop('disabled', true).parent().fadeTo('fast', 0.4);
317 $("#checkbox_sql_auto_increment").prop('disabled', false).parent().fadeTo('fast', 1);
321 // For separate-file exports only ZIP compression is allowed
322 $('input[type="checkbox"][name="as_separate_files"]').change(function(){
323 if ($(this).is(':checked')) {
324 $('#compression').val('zip');
328 $('#compression').change(function(){
329 if ($('option:selected').val() !== 'zip') {
330 $('input[type="checkbox"][name="as_separate_files"]').prop('checked', false);
336 function setup_table_structure_or_data() {
337 if ($("input[name='export_type']").val() != 'database') {
340 var pluginName = $("#plugins").find("option:selected").val();
341 var formElemName = pluginName + "_structure_or_data";
342 var force_structure_or_data = !($("input[name='" + formElemName + "_default']").length);
344 if (force_structure_or_data === true) {
345 $('input[name="structure_or_data_forced"]').val(1);
346 $('.export_structure input[type="checkbox"], .export_data input[type="checkbox"]')
347 .prop('disabled', true);
348 $('.export_structure, .export_data').fadeTo('fast', 0.4);
350 $('input[name="structure_or_data_forced"]').val(0);
351 $('.export_structure input[type="checkbox"], .export_data input[type="checkbox"]')
352 .prop('disabled', false);
353 $('.export_structure, .export_data').fadeTo('fast', 1);
355 var structure_or_data = $('input[name="' + formElemName + '_default"]').val();
357 if (structure_or_data == 'structure') {
358 $('.export_data input[type="checkbox"]')
359 .prop('checked', false);
360 } else if (structure_or_data == 'data') {
361 $('.export_structure input[type="checkbox"]')
362 .prop('checked', false);
364 if (structure_or_data == 'structure' || structure_or_data == 'structure_and_data') {
365 if (!$('.export_structure input[type="checkbox"]:checked').length) {
366 $('input[name="table_select[]"]:checked')
368 .find('.export_structure input[type="checkbox"]')
369 .prop('checked', true);
372 if (structure_or_data == 'data' || structure_or_data == 'structure_and_data') {
373 if (!$('.export_data input[type="checkbox"]:checked').length) {
374 $('input[name="table_select[]"]:checked')
376 .find('.export_data input[type="checkbox"]')
377 .prop('checked', true);
381 check_selected_tables();
382 check_table_select_all();
387 * Toggles the hiding and showing of plugin structure-specific and data-specific
390 function toggle_structure_data_opts()
392 var pluginName = $("select#plugins").val();
393 var radioFormName = pluginName + "_structure_or_data";
394 var dataDiv = "#" + pluginName + "_data";
395 var structureDiv = "#" + pluginName + "_structure";
396 var show = $("input[type='radio'][name='" + radioFormName + "']:checked").val();
397 if (show == 'data') {
398 $(dataDiv).slideDown('slow');
399 $(structureDiv).slideUp('slow');
401 $(structureDiv).slideDown('slow');
402 if (show == 'structure') {
403 $(dataDiv).slideUp('slow');
405 $(dataDiv).slideDown('slow');
411 * Toggles the disabling of the "save to file" options
413 function toggle_save_to_file()
415 var $ulSaveAsfile = $("#ul_save_asfile");
416 if (!$("#radio_dump_asfile").prop("checked")) {
417 $ulSaveAsfile.find("> li").fadeTo('fast', 0.4);
418 $ulSaveAsfile.find("> li > input").prop('disabled', true);
419 $ulSaveAsfile.find("> li > select").prop('disabled', true);
421 $ulSaveAsfile.find("> li").fadeTo('fast', 1);
422 $ulSaveAsfile.find("> li > input").prop('disabled', false);
423 $ulSaveAsfile.find("> li > select").prop('disabled', false);
427 AJAX.registerOnload('export.js', function () {
428 toggle_save_to_file();
429 $("input[type='radio'][name='output_format']").change(toggle_save_to_file);
433 * For SQL plugin, toggles the disabling of the "display comments" options
435 function toggle_sql_include_comments()
437 $("#checkbox_sql_include_comments").change(function () {
438 var $ulIncludeComments = $("#ul_include_comments");
439 if (!$("#checkbox_sql_include_comments").prop("checked")) {
440 $ulIncludeComments.find("> li").fadeTo('fast', 0.4);
441 $ulIncludeComments.find("> li > input").prop('disabled', true);
443 // If structure is not being exported, the comment options for structure should not be enabled
444 if ($("#radio_sql_structure_or_data_data").prop("checked")) {
445 $("#text_sql_header_comment").prop('disabled', false).parent("li").fadeTo('fast', 1);
447 $ulIncludeComments.find("> li").fadeTo('fast', 1);
448 $ulIncludeComments.find("> li > input").prop('disabled', false);
454 function check_table_select_all() {
455 var total = $('input[name="table_select[]"]').length;
456 var str_checked = $('input[name="table_structure[]"]:checked').length;
457 var data_checked = $('input[name="table_data[]"]:checked').length;
458 var str_all = $('#table_structure_all');
459 var data_all = $('#table_data_all');
461 if (str_checked == total) {
463 .prop("indeterminate", false)
464 .prop('checked', true);
465 } else if (str_checked === 0) {
467 .prop("indeterminate", false)
468 .prop('checked', false);
471 .prop("indeterminate", true)
472 .prop('checked', false);
475 if (data_checked == total) {
477 .prop("indeterminate", false)
478 .prop('checked', true);
479 } else if (data_checked === 0) {
481 .prop("indeterminate", false)
482 .prop('checked', false);
485 .prop("indeterminate", true)
486 .prop('checked', false);
490 function toggle_table_select_all_str() {
491 var str_all = $('#table_structure_all').is(':checked');
493 $('input[name="table_structure[]"]').prop('checked', true);
495 $('input[name="table_structure[]"]').prop('checked', false);
499 function toggle_table_select_all_data() {
500 var data_all = $('#table_data_all').is(':checked');
502 $('input[name="table_data[]"]').prop('checked', true);
504 $('input[name="table_data[]"]').prop('checked', false);
508 function check_selected_tables(argument) {
509 $('.export_table_select tbody tr').each(function() {
510 check_table_selected(this);
514 function check_table_selected(row) {
516 var table_select = $row.find('input[name="table_select[]"]');
517 var str_check = $row.find('input[name="table_structure[]"]');
518 var data_check = $row.find('input[name="table_data[]"]');
520 var data = data_check.is(':checked:not(:disabled)');
521 var structure = str_check.is(':checked:not(:disabled)');
523 if (data && structure) {
524 table_select.prop({checked: true, indeterminate: false});
525 $row.addClass('marked');
526 } else if (data || structure) {
527 table_select.prop({checked: true, indeterminate: true});
528 $row.removeClass('marked');
530 table_select.prop({checked: false, indeterminate: false});
531 $row.removeClass('marked');
535 function toggle_table_select(row) {
537 var table_selected = $row.find('input[name="table_select[]"]').is(':checked');
539 if (table_selected) {
540 $row.find('input[type="checkbox"]:not(:disabled)').prop('checked', true);
541 $row.addClass('marked');
543 $row.find('input[type="checkbox"]:not(:disabled)').prop('checked', false);
544 $row.removeClass('marked');
548 function handleAddProcCheckbox() {
549 if ($('#table_structure_all').is(':checked') === true
550 && $('#table_data_all').is(':checked') === true
552 $('#checkbox_sql_procedure_function').prop('checked', true);
554 $('#checkbox_sql_procedure_function').prop('checked', false);
558 AJAX.registerOnload('export.js', function () {
560 * For SQL plugin, if "CREATE TABLE options" is checked/unchecked, check/uncheck each of its sub-options
562 var $create = $("#checkbox_sql_create_table_statements");
563 var $create_options = $("#ul_create_table_statements").find("input");
564 $create.change(function () {
565 $create_options.prop('checked', $(this).prop("checked"));
567 $create_options.change(function () {
568 if ($create_options.is(":checked")) {
569 $create.prop('checked', true);
574 * Disables the view output as text option if the output must be saved as a file
576 $("#plugins").change(function () {
577 var active_plugin = $("#plugins").find("option:selected").val();
578 var force_file = $("#force_file_" + active_plugin).val();
579 if (force_file == "true") {
580 if ($("#radio_dump_asfile").prop('checked') !== true) {
581 $("#radio_dump_asfile").prop('checked', true);
582 toggle_save_to_file();
584 $("#radio_view_as_text").prop('disabled', true).parent().fadeTo('fast', 0.4);
586 $("#radio_view_as_text").prop('disabled', false).parent().fadeTo('fast', 1);
590 $("input[type='radio'][name$='_structure_or_data']").on('change', function () {
591 toggle_structure_data_opts();
594 $('input[name="table_select[]"]').on('change', function() {
595 toggle_table_select($(this).closest('tr'));
596 check_table_select_all();
597 handleAddProcCheckbox();
600 $('input[name="table_structure[]"]').on('change', function() {
601 check_table_selected($(this).closest('tr'));
602 check_table_select_all();
603 handleAddProcCheckbox();
606 $('input[name="table_data[]"]').on('change', function() {
607 check_table_selected($(this).closest('tr'));
608 check_table_select_all();
609 handleAddProcCheckbox();
612 $('#table_structure_all').on('change', function() {
613 toggle_table_select_all_str();
614 check_selected_tables();
615 handleAddProcCheckbox();
618 $('#table_data_all').on('change', function() {
619 toggle_table_select_all_data();
620 check_selected_tables();
621 handleAddProcCheckbox();
624 if ($("input[name='export_type']").val() == 'database') {
625 // Hide structure or data radio buttons
626 $("input[type='radio'][name$='_structure_or_data']").each(function() {
628 var name = $this.prop('name');
629 var val = $('input[name="' + name + '"]:checked').val();
630 var name_default = name + '_default';
631 if (!$('input[name="' + name_default + '"]').length) {
634 $('<input type="hidden" name="' + name_default + '" value="' + val + '" disabled>')
637 $('<input type="hidden" name="' + name + '" value="structure_and_data">')
639 $this.parent().find('label').remove();
641 $this.parent().remove();
644 $("input[type='radio'][name$='_structure_or_data']").remove();
646 // Disable CREATE table checkbox for sql
647 var createTableCheckbox = $('#checkbox_sql_create_table');
648 createTableCheckbox.prop('checked', true);
649 var dummyCreateTable = $('#checkbox_sql_create_table')
652 .attr('type', 'hidden');
654 .prop('disabled', true)
655 .after(dummyCreateTable)
657 .fadeTo('fast', 0.4);
659 setup_table_structure_or_data();
663 * Handle force structure_or_data
665 $("#plugins").change(setup_table_structure_or_data);
669 * Toggles display of options when quick and custom export are selected
671 function toggle_quick_or_custom()
673 if ($("input[name='quick_or_custom']").length === 0 // custom_no_form option
674 || $("#radio_custom_export").prop("checked") // custom
676 $("#databases_and_tables").show();
679 $("#format_specific_opts").show();
680 $("#output_quick_export").hide();
681 var selected_plugin_name = $("#plugins").find("option:selected").val();
682 $("#" + selected_plugin_name + "_options").show();
684 $("#databases_and_tables").hide();
687 $("#format_specific_opts").hide();
688 $("#output_quick_export").show();
692 function check_time_out(time_limit)
694 if (typeof time_limit === 'undefined' || time_limit === 0) {
697 //margin of one second to avoid race condition to set/access session variable
698 time_limit = time_limit + 1;
699 var href = "export.php";
701 'ajax_request' : true,
702 'token' : PMA_commonParams.get('token'),
703 'check_time_out' : true
705 clearTimeout(time_out);
706 time_out = setTimeout(function(){
707 $.get(href, params, function (data) {
708 if (data.message === 'timeout') {
710 '<div class="error">' +
711 PMA_messages.strTimeOutError +
717 }, time_limit * 1000);
722 * Handler for Database/table alias select
724 * @param event object the event object
728 function aliasSelectHandler(event) {
729 var sel = event.data.sel;
730 var type = event.data.type;
731 var inputId = $(this).val();
732 var $label = $(this).next('label');
733 $('input#' + $label.attr('for')).addClass('hide');
734 $('input#' + inputId).removeClass('hide');
735 $label.attr('for', inputId);
736 $('#alias_modal ' + sel + '[id$=' + type + ']:visible').addClass('hide');
737 var $inputWrapper = $('#alias_modal ' + sel + '#' + inputId + type);
738 $inputWrapper.removeClass('hide');
739 if (type === '_cols' && $inputWrapper.length > 0) {
740 var outer = $inputWrapper[0].outerHTML;
741 // Replace opening tags
742 var regex = /<dummy_inp/gi;
743 if (outer.match(regex)) {
744 var newTag = outer.replace(regex, '<input');
745 // Replace closing tags
746 regex = /<\/dummy_inp/gi;
747 newTag = newTag.replace(regex, '</input');
748 // Assign replacement
749 $inputWrapper.replaceWith(newTag);
751 } else if (type === '_tables') {
752 $('.table_alias_select:visible').change();
754 $("#alias_modal").dialog("option", "position", "center");
758 * Handler for Alias dialog box
760 * @param event object the event object
764 function createAliasModal(event) {
765 event.preventDefault();
767 dlgButtons[PMA_messages.strResetAll] = function() {
768 $(this).find('input[type="text"]').val('');
770 dlgButtons[PMA_messages.strReset] = function() {
771 $(this).find('input[type="text"]:visible').val('');
773 dlgButtons[PMA_messages.strSaveAndClose] = function() {
774 $(this).dialog("close");
775 // do not fillup form submission with empty values
776 $.each($(this).find('input[type="text"]'), function (i, e) {
777 if ($(e).val().trim().length == 0) {
778 $(e).prop('disabled', true);
781 $('#alias_modal').parent().appendTo($('form[name="dump"]'));
783 $('#alias_modal').find('input[type="text"]').prop('disabled', false);
784 $('#alias_modal').dialog({
785 width: Math.min($(window).width() - 100, 700),
786 maxHeight: $(window).height(),
788 dialogClass: "alias-dialog",
791 $(this).css('maxHeight', $(window).height() - 150);
792 $('.alias-dialog .ui-dialog-titlebar-close').remove();
796 $(this).find('input[type="text"]').each(function() {
797 // trim input fields on close
798 $(this).val($(this).val().trim());
799 // check if non empty field present
804 $('input#btn_alias_config').prop('checked', !isEmpty);
806 position: { my: "center top", at: "center top", of: window }
808 // Call change event of .table_alias_select
809 $('.table_alias_select:visible').trigger('change');
812 AJAX.registerOnload('export.js', function () {
813 $("input[type='radio'][name='quick_or_custom']").change(toggle_quick_or_custom);
815 $("#scroll_to_options_msg").hide();
816 $("#format_specific_opts").find("div.format_specific_options")
825 toggle_quick_or_custom();
826 toggle_structure_data_opts();
827 toggle_sql_include_comments();
828 check_table_select_all();
829 handleAddProcCheckbox();
832 * Initially disables the "Dump some row(s)" sub-options
834 disable_dump_some_rows_sub_options();
837 * Disables the "Dump some row(s)" sub-options when it is not selected
839 $("input[type='radio'][name='allrows']").change(function () {
840 if ($("input[type='radio'][name='allrows']").prop("checked")) {
841 enable_dump_some_rows_sub_options();
843 disable_dump_some_rows_sub_options();
847 // Open Alias Modal Dialog on click
848 $('#btn_alias_config').on('click', createAliasModal);
850 // Database alias select on change event
851 $('#db_alias_select').on(
853 {sel: 'span', type: '_tables'},
857 // Table alias select on change event
858 $('.table_alias_select').on(
860 {sel: 'table', type: '_cols'},