Db File saving, fix settings class with explicit writes

This commit is contained in:
Timothy Warren 2012-02-21 17:00:22 -05:00
parent d4bd456cbc
commit 80091147a1
4 changed files with 49 additions and 20 deletions

View File

@ -19,7 +19,7 @@
*/ */
class Settings { class Settings {
protected $current; private $current;
/** /**
* Load the settings file * Load the settings file
@ -49,16 +49,6 @@ class Settings {
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/**
* Save the settings file on close, just to be safe
*/
public function __destruct()
{
file_put_contents(BASE_DIR.'/settings.json', json_encode($this->current));
}
// --------------------------------------------------------------------------
/** /**
* Magic method to simplify isset checking for config options * Magic method to simplify isset checking for config options
* *
@ -67,7 +57,9 @@ class Settings {
*/ */
public function __get($key) public function __get($key)
{ {
return (isset($this->current->{$key})) ? $this->current->{$key} : NULL; return (isset($this->current->{$key}) && $key != "dbs")
? $this->current->{$key}
: NULL;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -87,6 +79,7 @@ class Settings {
} }
$this->current->{$key} = $val; $this->current->{$key} = $val;
$this->write();
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -103,6 +96,29 @@ class Settings {
{ {
$this->current->dbs->{$name} = array(); $this->current->dbs->{$name} = array();
$this->current->dbs->{$name} = $params; $this->current->dbs->{$name} = $params;
$this->write();
}
else
{
return FALSE;
}
}
// --------------------------------------------------------------------------
/**
* Edit a database connection
*
* @param string $name
* @param array $params
*/
public function edit_db($name, $params)
{
if(isset($this->current->dbs->{$name}))
{
$this->current->dbs->{$name} = $params;
$this->write();
} }
else else
{ {
@ -126,6 +142,7 @@ class Settings {
// Remove the db name from the object // Remove the db name from the object
unset($this->current->dbs->{$name}); unset($this->current->dbs->{$name});
$this->write();
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -139,5 +156,15 @@ class Settings {
{ {
return $this->current->dbs; return $this->current->dbs;
} }
// --------------------------------------------------------------------------
/**
* Write the settings to the file
*/
public function write()
{
file_put_contents(BASE_DIR . '/settings.json', json_encode($this->current));
}
} }
// End of settings.php // End of settings.php

View File

@ -254,6 +254,7 @@ SQL;
/** /**
* Run a prepared statement query * Run a prepared statement query
*
* @param array $args * @param array $args
* @return bool * @return bool
*/ */

View File

@ -77,13 +77,13 @@ class Add_DB extends GtkWindow {
// DB File // DB File
{ {
$filelbl = new GtkLabel("Database file"); $filelbl = new GtkLabel("Database file");
$this->dbfile = new GtkFileChooserButton("Select a database file", $this->db_file = new GtkFileChooserButton("Select a database file",
Gtk::FILE_CHOOSER_ACTION_OPEN); Gtk::FILE_CHOOSER_ACTION_OPEN);
$filealign = new GtkAlignment(0, 0.5, 0, 0); $filealign = new GtkAlignment(0, 0.5, 0, 0);
$filealign->add($filelbl); $filealign->add($filelbl);
$table->attach($filealign, 0, 1, ++$y1, ++$y2); $table->attach($filealign, 0, 1, ++$y1, ++$y2);
$table->attach($this->dbfile, 1, 2, $y1, $y2); $table->attach($this->db_file, 1, 2, $y1, $y2);
} }
// Host // Host
@ -185,10 +185,11 @@ class Add_DB extends GtkWindow {
public function db_add() public function db_add()
{ {
$data = array( $data = array(
'type' => $this->dbtype->get_active_text(), 'type' => strtolower($this->dbtype->get_active_text()),
'host' => $this->host->get_text(), 'host' => $this->host->get_text(),
'user' => $this->user->get_text(), 'user' => $this->user->get_text(),
'pass' => $this->pass->get_text(), 'pass' => $this->pass->get_text(),
'file' => $this->db_file->get_uri(),
); );
$this->settings->add_db($this->conn->get_text(), $data); $this->settings->add_db($this->conn->get_text(), $data);