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 {
protected $current;
private $current;
/**
* 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
*
@ -67,7 +57,9 @@ class Settings {
*/
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->write();
}
// --------------------------------------------------------------------------
@ -103,6 +96,29 @@ class Settings {
{
$this->current->dbs->{$name} = array();
$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
{
@ -126,6 +142,7 @@ class Settings {
// Remove the db name from the object
unset($this->current->dbs->{$name});
$this->write();
}
// --------------------------------------------------------------------------
@ -135,9 +152,19 @@ class Settings {
*
* @return array
*/
public function get_dbs()
{
return $this->current->dbs;
}
public function get_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

View File

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

View File

@ -17,7 +17,7 @@
*
* PDO-firebird isn't stable, so this is a wrapper of the ibase_ public functions.
*/
class firebird_manip extends db_manip{
class firebird_manip extends db_manip {
/**
* Convienience public function to generate sql for creating a db table

View File

@ -77,13 +77,13 @@ class Add_DB extends GtkWindow {
// DB 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);
$filealign = new GtkAlignment(0, 0.5, 0, 0);
$filealign->add($filelbl);
$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
@ -185,10 +185,11 @@ class Add_DB extends GtkWindow {
public function db_add()
{
$data = array(
'type' => $this->dbtype->get_active_text(),
'type' => strtolower($this->dbtype->get_active_text()),
'host' => $this->host->get_text(),
'user' => $this->user->get_text(),
'pass' => $this->pass->get_text(),
'file' => $this->db_file->get_uri(),
);
$this->settings->add_db($this->conn->get_text(), $data);