From 104ac11c1d2184a15a5cd7ae2a521728fe73b5b2 Mon Sep 17 00:00:00 2001 From: Crack Date: Thu, 8 Jul 2010 19:01:09 +0200 Subject: [PATCH] import and export for user preferences (currently JSON file only) add preferences reset --- .gsoc/todo.txt | 2 + js/config.js | 47 +++++++- libraries/config/ConfigFile.class.php | 14 +++ libraries/config/FormDisplay.class.php | 11 +- libraries/server_links.inc.php | 4 +- libraries/user_preferences.inc.php | 82 +++++++++++++ libraries/user_preferences.lib.php | 34 ++++++ main.php | 2 +- prefs_forms.php | 84 +------------ prefs_manage.php | 203 ++++++++++++++++++++++++++++++++ themes/original/css/theme_right.css.php | 6 + 11 files changed, 402 insertions(+), 87 deletions(-) create mode 100644 libraries/user_preferences.inc.php create mode 100644 prefs_manage.php diff --git a/.gsoc/todo.txt b/.gsoc/todo.txt index e36b793975..961215a7b9 100644 --- a/.gsoc/todo.txt +++ b/.gsoc/todo.txt @@ -2,3 +2,5 @@ restore cache'ing in phpmyadmin.css.php make CSS for darkblue_orange check input escaping, $cfg is no longer safe to use +preference: +- synchronize language, theme ThemeDefault \ No newline at end of file diff --git a/js/config.js b/js/config.js index aa2e41bd06..2b07f0a7a8 100644 --- a/js/config.js +++ b/js/config.js @@ -1,5 +1,5 @@ /** - * Functions used in configuration forms + * Functions used in configuration forms and on user preferences pages */ // default values for fields @@ -625,3 +625,48 @@ $(function() { // // END: "Restore default" and "set value" buttons // ------------------------------------------------------------------ + +// ------------------------------------------------------------------ +// User preferences import/export +// + +$(function() { + var radios = $('#import_local_storage, #export_local_storage'); + if (!radios.length) { + return; + } + + // enable JavaScript dependent fields + radios + .attr('disabled', false) + .add('#export_text_file, #import_text_file') + .click(function(){ + var show_id = $(this).attr('id'); + var hide_id = show_id.match(/local_storage$/) + ? show_id.replace(/local_storage$/, 'text_file') + : show_id.replace(/text_file$/, 'local_storage'); + $('#opts_'+hide_id).hide('fast'); + $('#opts_'+show_id).show('fast'); + }); + + // detect localStorage state + var ls_supported = window.localStorage || false; + var ls_exists = ls_supported ? (window.localStorage['config'] || false) : false; + $('.localStorage-'+(ls_supported ? 'un' : '')+'supported').hide(); + $('.localStorage-'+(ls_exists ? 'empty' : 'exists')).hide(); + $('form.prefs-form').change(function(){ + var form = $(this); + var disabled = false; + if (!ls_supported) { + disabled = form.find('input[type=radio][value$=local_storage]').attr('checked'); + } else if (!ls_exists && form.attr('name') == 'prefs_import' + && $('#import_local_storage')[0].checked) { + disabled = true; + } + form.find('input[type=submit]').attr('disabled', disabled); + }); +}); + +// +// END: User preferences import/export +// ------------------------------------------------------------------ \ No newline at end of file diff --git a/libraries/config/ConfigFile.class.php b/libraries/config/ConfigFile.class.php index 62b23e0c49..1f501140a6 100644 --- a/libraries/config/ConfigFile.class.php +++ b/libraries/config/ConfigFile.class.php @@ -186,6 +186,20 @@ class ConfigFile } /** + * Returns default config in a flattened array + * + * @return array + */ + public function getFlatDefaultConfig() + { + $this->_flattenArrayResult = array(); + array_walk($this->cfg, array($this, '_flattenArray'), ''); + $flat_cfg = $this->_flattenArrayResult; + $this->_flattenArrayResult = null; + return $flat_cfg; + } + + /** * Updates config with values read from PMA_Config class * (config will contain differences to defaults from config.defaults.php). * diff --git a/libraries/config/FormDisplay.class.php b/libraries/config/FormDisplay.class.php index cd2d6a4070..ef8592a9c5 100644 --- a/libraries/config/FormDisplay.class.php +++ b/libraries/config/FormDisplay.class.php @@ -111,12 +111,12 @@ class FormDisplay * Processes forms, returns true on successful save * * @param bool $allow_partial_save allows for partial form saving on failed validation + * @param bool $check_form_submit whether check for $_POST['submit_save'] * @return boolean */ - public function process($allow_partial_save = true) + public function process($allow_partial_save = true, $check_form_submit = true) { - // gather list of forms to save - if (!isset($_POST['submit_save'])) { + if ($check_form_submit && !isset($_POST['submit_save'])) { return false; } @@ -475,6 +475,11 @@ class FormDisplay $work_path = array_search($system_path, $this->system_paths); $key = $this->translated_paths[$work_path]; + // skip groups + if ($form->getOptionType($field) == 'group') { + continue; + } + // ensure the value is set if (!isset($_POST[$key])) { // checkboxes aren't set by browsers if they're off diff --git a/libraries/server_links.inc.php b/libraries/server_links.inc.php index b0a78247e0..34e327f88e 100644 --- a/libraries/server_links.inc.php +++ b/libraries/server_links.inc.php @@ -92,8 +92,10 @@ $tabs['synchronize']['link'] = 'server_synchronize.php'; $tabs['synchronize']['text'] = __('Synchronize'); $tabs['settings']['icon'] = 'b_tblops.png'; -$tabs['settings']['link'] = 'prefs_forms.php'; +$tabs['settings']['link'] = 'prefs_manage.php'; $tabs['settings']['text'] = __('Settings'); +$tabs['settings']['active'] = in_array(basename($GLOBALS['PMA_PHP_SELF']), + array('prefs_forms.php', 'prefs_manage.php')); echo PMA_generate_html_tabs($tabs, array()); unset($tabs); diff --git a/libraries/user_preferences.inc.php b/libraries/user_preferences.inc.php new file mode 100644 index 0000000000..2cdb01368a --- /dev/null +++ b/libraries/user_preferences.inc.php @@ -0,0 +1,82 @@ + 'b_tblops.png', + 'Sql_queries' => 'b_sql.png', + 'Left_frame' => 'b_select.png', + 'Main_frame' => 'b_props.png', + 'Import' => 'b_import.png', + 'Export' => 'b_export.png'); +echo ''; + +// show "configuration saved" message and reload navigation frame if needed +if (!empty($_GET['saved'])) { + $message = PMA_Message::rawSuccess(__('Configuration has been saved')); + $message->display(); + if (isset($_GET['refresh_left_frame']) && $_GET['refresh_left_frame'] == '1') { +?> + +setAllowedKeys($forms_all_keys); +$cf->updateWithGlobalConfig($GLOBALS['PMA_Config']); + +// todo: debug - remove +$arr = $cf->getConfigArray(); +$arr2 = array(); +foreach ($arr as $k => $v) { + $arr2[] = "$k " . var_export($v, true); +} +$arr2 = implode(', ', $arr2); +$arr2 .= '
Blacklist: ' . (empty($cfg['UserprefsDisallow']) + ? 'empty' + : implode(', ', $cfg['UserprefsDisallow'])); +$msg = PMA_Message::notice('Debug: ' . $arr2); +$msg->display(); + +if (isset($error) && $error) { + if (!$error instanceof PMA_Message) { + $error = PMA_Message::error($error); + } + $error->display(); +} diff --git a/libraries/user_preferences.lib.php b/libraries/user_preferences.lib.php index 8477e96f02..3b52e9bbfc 100644 --- a/libraries/user_preferences.lib.php +++ b/libraries/user_preferences.lib.php @@ -174,4 +174,38 @@ function PMA_persist_option($path, $value, $default_value) } PMA_save_userprefs($prefs['config_data']); } + +/** + * Redirects after saving new user preferences + * + * @param array $forms + * @param array $old_settings + * @param string $file_name + * @param array $params + */ +function PMA_userprefs_redirect(array $forms, array $old_settings, $file_name, $params = null) +{ + // compute differences and check whether left frame should be refreshed + $old_settings = isset($old_settings['config_data']) + ? $old_settings['config_data'] + : array(); + $new_settings = ConfigFile::getInstance()->getConfigArray(); + $diff_keys = array_keys(array_diff_assoc($old_settings, $new_settings) + + array_diff_assoc($new_settings, $old_settings)); + $check_keys = array('NaturalOrder', 'MainPageIconic', 'DefaultTabDatabase'); + $check_keys = array_merge($check_keys, $forms['Left_frame']['Left_frame'], + $forms['Left_frame']['Left_servers'], $forms['Left_frame']['Left_databases']); + $diff = array_intersect($check_keys, $diff_keys); + $refresh_left_frame = !empty($diff); + + // redirect + $url_params = array( + 'saved' => 1, + 'refresh_left_frame' => $refresh_left_frame); + if (is_array($params)) { + $url_params = array_merge($params, $url_params); + } + PMA_sendHeaderLocation($GLOBALS['cfg']['PmaAbsoluteUri'] . $file_name + . PMA_generate_common_url($url_params, '&')); +} ?> \ No newline at end of file diff --git a/main.php b/main.php index 7bd80ad6dc..3ff65ad91f 100644 --- a/main.php +++ b/main.php @@ -176,7 +176,7 @@ echo ''; echo '