From 1ea18963788475de21eacbb4705b950f28449471 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 30 Mar 2012 15:22:52 -0400 Subject: [PATCH] Db connection editing, misc cleanup --- sys/common/settings.php | 21 +++++++-- sys/windows/edit_db.php | 3 ++ sys/windows/widgets/db_info_widget.php | 63 ++++++++++++++++++++++++-- 3 files changed, 79 insertions(+), 8 deletions(-) diff --git a/sys/common/settings.php b/sys/common/settings.php index c31effa..2636c43 100644 --- a/sys/common/settings.php +++ b/sys/common/settings.php @@ -146,11 +146,24 @@ class Settings { * * @param array $params */ - public function edit_db($params) + public function edit_db($name, $params) { - if(isset($this->current->dbs->{$params['name']})) + if (isset($this->current->dbs->{$name}) && ($name === $params['name'])) { - $this->current->dbs->{$params['name']} = $params; + $this->current->dbs->{$name} = $params; + } + elseif ($name !== $params['name']) + { + unset($this->current->dbs->{$name}); + + if ( ! isset($this->current->dbs->{$params['name']})) + { + $this->current->dbs->{$params['name']} = $params; + } + else + { + return FALSE; + } } else { @@ -159,6 +172,8 @@ class Settings { // Save the json $this->__destruct(); + + return TRUE; } // -------------------------------------------------------------------------- diff --git a/sys/windows/edit_db.php b/sys/windows/edit_db.php index 34f83fd..589cee7 100644 --- a/sys/windows/edit_db.php +++ b/sys/windows/edit_db.php @@ -35,6 +35,9 @@ class Edit_DB extends GtkWindow { // Add the Vbox, and show the window $this->add($connection_form); $this->show_all(); + + // Hide fields + $connection_form->set_db(); } } // End of edit_db.php \ No newline at end of file diff --git a/sys/windows/widgets/db_info_widget.php b/sys/windows/widgets/db_info_widget.php index eba43e9..2482452 100644 --- a/sys/windows/widgets/db_info_widget.php +++ b/sys/windows/widgets/db_info_widget.php @@ -17,7 +17,38 @@ */ class DB_Info_Widget extends GtkTable { - protected $conn, $dbtype, $host, $user, $pass, $database, $settings, $db_file, $port; + /** + * Alias to Settings::get_instance + * @var Settings + */ + private $settings; + + /** + * Fields + */ + protected $conn, + $conn_db, + $dbtype, + $host, + $user, + $pass, + $database, + $db_file, + $port, + $old_conn; + + /** + * Labels + */ + protected $lblconn, + $lblconn_db, + $lbldbtype, + $lblhost, + $lbluser, + $lblpass, + $lbldatabase, + $lbldb_file, + $lblport; /** * No params = add, params = edit @@ -76,7 +107,12 @@ class DB_Info_Widget extends GtkTable { // Select the proper db type if editing if ( ! empty($db->type)) { + // Set the old conn variable for editing + $this->old_conn = $db->name; + $dbtype = strtolower($db->type); + + // Set the db type based on the current connection $this->dbtype->set_active(array_search($dbtype, $lower_db_types)); // Set default path @@ -84,11 +120,11 @@ class DB_Info_Widget extends GtkTable { { $this->db_file->set_filename($db->file); } - - $this->set_db($dbtype); } } + // -------------------------------------------------------------------------- + /** * Table layout */ @@ -185,6 +221,8 @@ class DB_Info_Widget extends GtkTable { } } + // -------------------------------------------------------------------------- + /** * Set defaults for new database type * @@ -252,11 +290,16 @@ class DB_Info_Widget extends GtkTable { } } + // -------------------------------------------------------------------------- + /** * Like change_db function, but save current values + * + * @return void */ - public function set_db($dbtype) + public function set_db() { + $dbtype = strtolower($this->dbtype->get_active_text()); // Reset $this->db_file->hide(); @@ -292,6 +335,8 @@ class DB_Info_Widget extends GtkTable { } } + // -------------------------------------------------------------------------- + /** * Adds the database to the settings file */ @@ -319,6 +364,8 @@ class DB_Info_Widget extends GtkTable { $parent_window->destroy(); } + // -------------------------------------------------------------------------- + /** * Edit an existing database connection */ @@ -335,7 +382,7 @@ class DB_Info_Widget extends GtkTable { 'name' => $this->conn->get_text(), ); - if ($this->settings->edit_db($data)) + if ($this->settings->edit_db($this->old_conn, $data)) { // Let the user know the connection has been updated alert("Changes to database connection have been saved"); @@ -354,6 +401,8 @@ class DB_Info_Widget extends GtkTable { $parent_window->destroy(); } + // -------------------------------------------------------------------------- + /** * Test a db connection, and display a popup with the result of the test */ @@ -396,6 +445,8 @@ class DB_Info_Widget extends GtkTable { alert("Successfully Connected."); } + // -------------------------------------------------------------------------- + /** * Checks what database drivers are available * @@ -448,6 +499,8 @@ class DB_Info_Widget extends GtkTable { return $drivers; } + // -------------------------------------------------------------------------- + /** * Simple helper function for adding a row to the GtkTable *