diff --git a/config.sample.inc.php b/config.sample.inc.php index 8e87694629..0004f05cf7 100644 --- a/config.sample.inc.php +++ b/config.sample.inc.php @@ -63,6 +63,7 @@ $cfg['Servers'][$i]['AllowNoPassword'] = false; // $cfg['Servers'][$i]['savedsearches'] = 'pma__savedsearches'; // $cfg['Servers'][$i]['central_columns'] = 'pma__central_columns'; // $cfg['Servers'][$i]['designer_settings'] = 'pma__designer_settings'; +// $cfg['Servers'][$i]['exporttemplates'] = 'pma__exporttemplates'; /* Contrib / Swekey authentication */ // $cfg['Servers'][$i]['auth_swekey_config'] = '/etc/swekey-pma.conf'; diff --git a/doc/config.rst b/doc/config.rst index 0bc8c41fb9..3617bd436a 100644 --- a/doc/config.rst +++ b/doc/config.rst @@ -812,6 +812,20 @@ Server connection settings * put the table name in :config:option:`$cfg['Servers'][$i]['savedsearches']` (e.g. ``pma__savedsearches``) +.. _exporttemplates: +.. config:option:: $cfg['Servers'][$i]['exporttemplates'] + + :type: string + :default: ``''`` + + Since release 4.5.0 you can save and load export templates. + + To allow the usage of this functionality: + + * set up :config:option:`$cfg['Servers'][$i]['pmadb']` and the phpMyAdmin configuration storage + * put the table name in :config:option:`$cfg['Servers'][$i]['exporttemplates']` (e.g. + ``pma__exporttemplates``) + .. _tracking: .. config:option:: $cfg['Servers'][$i]['tracking'] diff --git a/examples/config.manyhosts.inc.php b/examples/config.manyhosts.inc.php index 9c8c79b4b1..6f7ecb66ad 100644 --- a/examples/config.manyhosts.inc.php +++ b/examples/config.manyhosts.inc.php @@ -49,4 +49,5 @@ foreach ($hosts as $host) { $cfg['Servers'][$i]['savedsearches'] = 'pma__savedsearches'; $cfg['Servers'][$i]['central_columns'] = 'pma__central_columns'; $cfg['Servers'][$i]['designer_settings'] = 'pma__designer_settings'; + $cfg['Servers'][$i]['exporttemplates'] = 'pma__exporttemplates'; } diff --git a/js/export.js b/js/export.js index 470526b4db..4a3c7d94f3 100644 --- a/js/export.js +++ b/js/export.js @@ -26,6 +26,167 @@ function enable_dump_some_rows_sub_options() $("input[type='text'][name='limit_from']").prop('disabled', ''); } +/** + * Return template data as a json object + * + * @returns template data + */ +function getTemplateData() +{ + var $form = $('form[name="dump"]'); + var blacklist = ['token', 'server', 'db', 'table', 'single_table', + 'export_type', 'export_method', 'sql_query']; + var obj = {}; + var arr = $form.serializeArray(); + $.each(arr, function () { + if ($.inArray(this.name, blacklist) < 0) { + if (obj[this.name] !== undefined) { + if (! obj[this.name].push) { + obj[this.name] = [obj[this.name]]; + } + obj[this.name].push(this.value || ''); + } else { + obj[this.name] = this.value || ''; + } + } + }); + + return obj; +} + +/** + * Create a template with selected options + * + * @param name name of the template + */ +function createTemplate(name) +{ + var templateData = getTemplateData(); + + var params = { + ajax_request : true, + token : PMA_commonParams.get('token'), + server : PMA_commonParams.get('server'), + db : PMA_commonParams.get('db'), + table : PMA_commonParams.get('table'), + templateAction : 'create', + templateName : name, + templateData : JSON.stringify(templateData) + }; + + var $msgbox = PMA_ajaxShowMessage(); + $.post('tbl_export.php', params, function (response) { + if (response.success === true) { + $('#templateName').val(''); + $('#template').html(response.data); + PMA_ajaxRemoveMessage($msgbox); + } else { + PMA_ajaxShowMessage(response.error, false); + } + }); +} + +/** + * Loads a template + * + * @param id ID of the template to load + */ +function loadTemplate(id) +{ + var params = { + ajax_request : true, + token : PMA_commonParams.get('token'), + server : PMA_commonParams.get('server'), + db : PMA_commonParams.get('db'), + table : PMA_commonParams.get('table'), + templateAction : 'load', + templateId : id, + }; + + var $msgbox = PMA_ajaxShowMessage(); + $.post('tbl_export.php', params, function (response) { + if (response.success === true) { + var $form = $('form[name="dump"]'); + var options = $.parseJSON(response.data); + $.each(options, function (key, value) { + var $element = $form.find('[name="' + key + '"]'); + if ($element.length) { + if (($element.is('input') && $element.attr('type') == 'checkbox') || + ($element.is('input') && $element.attr('type') == 'radio') || + ($element.is('select') && $element.attr('multiple') == 'multiple')) { + if (! value.push) { + value = [value]; + } + } + $element.val(value); + $element.trigger('change'); + } + }); + PMA_ajaxRemoveMessage($msgbox); + } else { + PMA_ajaxShowMessage(response.error, false); + } + }); +} + +/** + * Updates an existing template with current options + * + * @param id ID of the template to update + */ +function updateTemplate(id) +{ + var templateData = getTemplateData(); + + var params = { + ajax_request : true, + token : PMA_commonParams.get('token'), + server : PMA_commonParams.get('server'), + db : PMA_commonParams.get('db'), + table : PMA_commonParams.get('table'), + templateAction : 'update', + templateId : id, + templateData : JSON.stringify(templateData) + }; + + var $msgbox = PMA_ajaxShowMessage(); + $.post('tbl_export.php', params, function (response) { + if (response.success === true) { + PMA_ajaxRemoveMessage($msgbox); + } else { + PMA_ajaxShowMessage(response.error, false); + } + }); +} + +/** + * Delete a template + * + * @param id ID of the template to delete + */ +function deleteTemplate(id) +{ + var params = { + ajax_request : true, + token : PMA_commonParams.get('token'), + server : PMA_commonParams.get('server'), + db : PMA_commonParams.get('db'), + table : PMA_commonParams.get('table'), + templateAction : 'delete', + templateId : id, + }; + + var $msgbox = PMA_ajaxShowMessage(); + $.post('tbl_export.php', params, function (response) { + if (response.success === true) { + $('#template option[value="' + id + '"]').remove(); + PMA_ajaxRemoveMessage($msgbox); + } else { + PMA_ajaxShowMessage(response.error, false); + } + }); +} + /** * Unbind all event handlers before tearing down a page */ @@ -45,9 +206,53 @@ AJAX.registerTeardown('export.js', function () { $('input[name="table_data[]"]').off('change'); $('#table_structure_all').off('change'); $('#table_data_all').off('change'); + $('input[name="createTemplate"]').off('click'); + $('input[name="loadTemplate"]').off('click'); + $('input[name="updateTemplate"]').off('click'); + $('input[name="deleteTemplate"]').off('click'); }); AJAX.registerOnload('export.js', function () { + + /** + * Export template handling code + */ + // create a new template + $('input[name="createTemplate"]').on('click', function (e) { + e.preventDefault(); + var name = $('input[name="templateName"]').val(); + if (name.length) { + createTemplate(name); + } + }); + + // load an existing template + $('input[name="loadTemplate"]').on('click', function (e) { + e.preventDefault(); + var id = $('select[name="template"]').val(); + if (id.length) { + loadTemplate(id); + } + }); + + // udpate an existing template with new criteria + $('input[name="updateTemplate"]').on('click', function (e) { + e.preventDefault(); + var id = $('select[name="template"]').val(); + if (id.length) { + updateTemplate(id); + } + }); + + // delete an existing template + $('input[name="deleteTemplate"]').on('click', function (e) { + e.preventDefault(); + var id = $('select[name="template"]').val(); + if (id.length) { + deleteTemplate(id); + } + }); + /** * Toggles the hiding and showing of each plugin's options * according to the currently selected plugin from the dropdown list diff --git a/libraries/DisplayResults.class.php b/libraries/DisplayResults.class.php index aff372d7d9..7304502935 100644 --- a/libraries/DisplayResults.class.php +++ b/libraries/DisplayResults.class.php @@ -349,6 +349,11 @@ class PMA_DisplayResults 'config_data' => $json_highlighting_data ); } + if (! empty($cfgRelation['exporttemplates'])) { + $relDb[$cfgRelation['exporttemplates']] = array( + 'template_data' => $json_highlighting_data + ); + } } } diff --git a/libraries/config.default.php b/libraries/config.default.php index 485a6039c4..da11c2f1ac 100644 --- a/libraries/config.default.php +++ b/libraries/config.default.php @@ -490,6 +490,15 @@ $cfg['Servers'][$i]['central_columns'] = ''; */ $cfg['Servers'][$i]['designer_settings'] = ''; +/** + * table to store export templates + * - leave blank to disable saved searches feature + * SUGGESTED: 'pma__exporttemplates' + * + * @global string $cfg['Servers'][$i]['exporttemplates'] + */ +$cfg['Servers'][$i]['exporttemplates'] = ''; + /** * Maximum number of records saved in $cfg['Servers'][$i]['table_uiprefs'] table. * diff --git a/libraries/config/messages.inc.php b/libraries/config/messages.inc.php index 72b6a3c83a..9d75d2909d 100644 --- a/libraries/config/messages.inc.php +++ b/libraries/config/messages.inc.php @@ -652,6 +652,11 @@ $strConfigServers_savedsearches_desc = __( 'Leave blank for no QBE saved searches support, suggested: ' . '[kbd]pma__savedsearches[/kbd].' ); +$strConfigServers_savedsearches_name = __('Export templates table'); +$strConfigServers_savedsearches_desc = __( + 'Leave blank for no export template support, suggested: ' + . '[kbd]pma__exporttemplates[/kbd].' +); $strConfigServers_central_columns_name = __('Central columns table'); $strConfigServers_central_columns_desc = __( 'Leave blank for no central columns support, suggested: ' diff --git a/libraries/config/setup.forms.php b/libraries/config/setup.forms.php index c5995d3090..477f48f165 100644 --- a/libraries/config/setup.forms.php +++ b/libraries/config/setup.forms.php @@ -84,6 +84,7 @@ $forms['Servers']['Server_pmadb'] = array('Servers' => array(1 => array( 'savedsearches' => 'pma__savedsearches', 'central_columns' => 'pma__central_columns', 'designer_settings' => 'pma__designer_settings', + 'exporttemplates' => 'pma__exporttemplates', 'MaxTableUiprefs' => 100))); $forms['Servers']['Server_tracking'] = array('Servers' => array(1 => array( 'tracking_version_auto_create', diff --git a/libraries/display_export.inc.php b/libraries/display_export.inc.php index f36e951ce4..a2dac43f2f 100644 --- a/libraries/display_export.inc.php +++ b/libraries/display_export.inc.php @@ -39,7 +39,10 @@ if (empty($export_list)) { exit; } -$html = '