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 = $.parseJSON(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").unbind('change');
221 $("input[type='radio'][name='sql_structure_or_data']").unbind('change');
222 $("input[type='radio'][name$='_structure_or_data']").off('change');
223 $("input[type='radio'][name='output_format']").unbind('change');
224 $("#checkbox_sql_include_comments").unbind('change');
225 $("input[type='radio'][name='quick_or_custom']").unbind('change');
226 $("input[type='radio'][name='allrows']").unbind('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").removeProp('disabled').parent().fadeTo('fast', 1);
310 $("#checkbox_sql_relation").removeProp('disabled').parent().fadeTo('fast', 1);
311 $("#checkbox_sql_mime").removeProp('disabled').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").removeProp('disabled').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 .removeProp('disabled');
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").removeProp('disabled').parent("li").fadeTo('fast', 1);
447 $ulIncludeComments.find("> li").fadeTo('fast', 1);
448 $ulIncludeComments.find("> li > input").removeProp('disabled');
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 } else if (data || structure) {
526 table_select.prop({checked: true, indeterminate: true});
528 table_select.prop({checked: false, indeterminate: false});
532 function toggle_table_select(row) {
534 var table_selected = $row.find('input[name="table_select[]"]').is(':checked');
536 if (table_selected) {
537 $row.find('input[type="checkbox"]:not(:disabled)').prop('checked', true);
539 $row.find('input[type="checkbox"]:not(:disabled)').prop('checked', false);
543 AJAX.registerOnload('export.js', function () {
545 * For SQL plugin, if "CREATE TABLE options" is checked/unchecked, check/uncheck each of its sub-options
547 var $create = $("#checkbox_sql_create_table_statements");
548 var $create_options = $("#ul_create_table_statements").find("input");
549 $create.change(function () {
550 $create_options.prop('checked', $(this).prop("checked"));
552 $create_options.change(function () {
553 if ($create_options.is(":checked")) {
554 $create.prop('checked', true);
559 * Disables the view output as text option if the output must be saved as a file
561 $("#plugins").change(function () {
562 var active_plugin = $("#plugins").find("option:selected").val();
563 var force_file = $("#force_file_" + active_plugin).val();
564 if (force_file == "true") {
565 if ($("#radio_dump_asfile").prop('checked') !== true) {
566 $("#radio_dump_asfile").prop('checked', true);
567 toggle_save_to_file();
569 $("#radio_view_as_text").prop('disabled', true).parent().fadeTo('fast', 0.4);
571 $("#radio_view_as_text").prop('disabled', false).parent().fadeTo('fast', 1);
575 $("input[type='radio'][name$='_structure_or_data']").on('change', function () {
576 toggle_structure_data_opts();
579 $('input[name="table_select[]"]').on('change', function() {
580 toggle_table_select($(this).closest('tr'));
581 check_table_select_all();
584 $('input[name="table_structure[]"]').on('change', function() {
585 check_table_selected($(this).closest('tr'));
586 check_table_select_all();
589 $('input[name="table_data[]"]').on('change', function() {
590 check_table_selected($(this).closest('tr'));
591 check_table_select_all();
594 $('#table_structure_all').on('change', function() {
595 toggle_table_select_all_str();
596 check_selected_tables();
599 $('#table_data_all').on('change', function() {
600 toggle_table_select_all_data();
601 check_selected_tables();
604 if ($("input[name='export_type']").val() == 'database') {
605 // Hide structure or data radio buttons
606 $("input[type='radio'][name$='_structure_or_data']").each(function() {
608 var name = $this.prop('name');
609 var val = $('input[name="' + name + '"]:checked').val();
610 var name_default = name + '_default';
611 if (!$('input[name="' + name_default + '"]').length) {
614 $('<input type="hidden" name="' + name_default + '" value="' + val + '" disabled>')
617 $('<input type="hidden" name="' + name + '" value="structure_and_data">')
619 $this.parent().find('label').remove();
621 $this.parent().remove();
624 $("input[type='radio'][name$='_structure_or_data']").remove();
626 // Disable CREATE table checkbox for sql
627 var createTableCheckbox = $('#checkbox_sql_create_table');
628 createTableCheckbox.prop('checked', true);
629 var dummyCreateTable = $('#checkbox_sql_create_table')
632 .attr('type', 'hidden');
634 .prop('disabled', true)
635 .after(dummyCreateTable)
637 .fadeTo('fast', 0.4);
639 setup_table_structure_or_data();
643 * Handle force structure_or_data
645 $("#plugins").change(setup_table_structure_or_data);
649 * Toggles display of options when quick and custom export are selected
651 function toggle_quick_or_custom()
653 if ($("input[name='quick_or_custom']").length === 0 // custom_no_form option
654 || $("#radio_custom_export").prop("checked") // custom
656 $("#databases_and_tables").show();
659 $("#format_specific_opts").show();
660 $("#output_quick_export").hide();
661 var selected_plugin_name = $("#plugins").find("option:selected").val();
662 $("#" + selected_plugin_name + "_options").show();
664 $("#databases_and_tables").hide();
667 $("#format_specific_opts").hide();
668 $("#output_quick_export").show();
672 function check_time_out(time_limit)
674 if (typeof time_limit === 'undefined' || time_limit === 0) {
677 //margin of one second to avoid race condition to set/access session variable
678 time_limit = time_limit + 1;
679 var href = "export.php";
681 'ajax_request' : true,
682 'token' : PMA_commonParams.get('token'),
683 'check_time_out' : true
685 clearTimeout(time_out);
686 time_out = setTimeout(function(){
687 $.get(href, params, function (data) {
688 if (data.message === 'timeout') {
690 '<div class="error">' +
691 PMA_messages.strTimeOutError +
697 }, time_limit * 1000);
702 * Handler for Database/table alias select
704 * @param event object the event object
708 function aliasSelectHandler(event) {
709 var sel = event.data.sel;
710 var type = event.data.type;
711 var inputId = $(this).val();
712 var $label = $(this).next('label');
713 $('input#' + $label.attr('for')).addClass('hide');
714 $('input#' + inputId).removeClass('hide');
715 $label.attr('for', inputId);
716 $('#alias_modal ' + sel + '[id$=' + type + ']:visible').addClass('hide');
717 var $inputWrapper = $('#alias_modal ' + sel + '#' + inputId + type);
718 $inputWrapper.removeClass('hide');
719 if (type === '_cols' && $inputWrapper.length > 0) {
720 var outer = $inputWrapper[0].outerHTML;
721 // Replace opening tags
722 var regex = /<dummy_inp/gi;
723 if (outer.match(regex)) {
724 var newTag = outer.replace(regex, '<input');
725 // Replace closing tags
726 regex = /<\/dummy_inp/gi;
727 newTag = newTag.replace(regex, '</input');
728 // Assign replacement
729 $inputWrapper.replaceWith(newTag);
731 } else if (type === '_tables') {
732 $('.table_alias_select:visible').change();
734 $("#alias_modal").dialog("option", "position", "center");
738 * Handler for Alias dialog box
740 * @param event object the event object
744 function createAliasModal(event) {
745 event.preventDefault();
747 dlgButtons[PMA_messages.strResetAll] = function() {
748 $(this).find('input[type="text"]').val('');
750 dlgButtons[PMA_messages.strReset] = function() {
751 $(this).find('input[type="text"]:visible').val('');
753 dlgButtons[PMA_messages.strSaveAndClose] = function() {
754 $(this).dialog("close");
755 // do not fillup form submission with empty values
756 $.each($(this).find('input[type="text"]'), function (i, e) {
757 if ($(e).val().trim().length == 0) {
758 $(e).prop('disabled', true);
761 $('#alias_modal').parent().appendTo($('form[name="dump"]'));
763 $('#alias_modal').find('input[type="text"]').prop('disabled', false);
764 $('#alias_modal').dialog({
765 width: Math.min($(window).width() - 100, 700),
766 maxHeight: $(window).height(),
768 dialogClass: "alias-dialog",
771 $(this).css('maxHeight', $(window).height() - 150);
772 $('.alias-dialog .ui-dialog-titlebar-close').remove();
776 $(this).find('input[type="text"]').each(function() {
777 // trim input fields on close
778 $(this).val($(this).val().trim());
779 // check if non empty field present
784 $('input#btn_alias_config').prop('checked', !isEmpty);
786 position: { my: "center top", at: "center top", of: window }
788 // Call change event of .table_alias_select
789 $('.table_alias_select:visible').trigger('change');
792 AJAX.registerOnload('export.js', function () {
793 $("input[type='radio'][name='quick_or_custom']").change(toggle_quick_or_custom);
795 $("#scroll_to_options_msg").hide();
796 $("#format_specific_opts").find("div.format_specific_options")
805 toggle_quick_or_custom();
806 toggle_structure_data_opts();
807 toggle_sql_include_comments();
810 * Initially disables the "Dump some row(s)" sub-options
812 disable_dump_some_rows_sub_options();
815 * Disables the "Dump some row(s)" sub-options when it is not selected
817 $("input[type='radio'][name='allrows']").change(function () {
818 if ($("input[type='radio'][name='allrows']").prop("checked")) {
819 enable_dump_some_rows_sub_options();
821 disable_dump_some_rows_sub_options();
825 // Open Alias Modal Dialog on click
826 $('#btn_alias_config').on('click', createAliasModal);
828 // Database alias select on change event
829 $('#db_alias_select').on(
831 {sel: 'span', type: '_tables'},
835 // Table alias select on change event
836 $('.table_alias_select').on(
838 {sel: 'table', type: '_cols'},