2012-01-30 07:57:17 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* OpenSQLManager
|
|
|
|
*
|
|
|
|
* Free Database manager for Open Source Databases
|
|
|
|
*
|
|
|
|
* @author Timothy J. Warren
|
|
|
|
* @copyright Copyright (c) 2012
|
|
|
|
* @link https://github.com/aviat4ion/OpenSQLManager
|
|
|
|
* @license http://philsturgeon.co.uk/code/dbad-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Window controlling addtion of database connections
|
|
|
|
*/
|
|
|
|
class Add_DB extends GtkWindow {
|
2012-01-31 16:19:34 -05:00
|
|
|
|
2012-02-20 16:22:03 -05:00
|
|
|
var $conn, $dbtype, $host, $user, $pass, $database, $settings, $db_file;
|
2012-01-30 07:57:17 -05:00
|
|
|
|
2012-02-21 20:15:49 -05:00
|
|
|
public function __construct($conn='', $dbtype='', $host='localhost', $user='', $pass='', $database='', $db_file=NULL)
|
2012-01-30 07:57:17 -05:00
|
|
|
{
|
|
|
|
parent::__construct();
|
2012-01-31 12:42:38 -05:00
|
|
|
|
2012-02-21 20:15:49 -05:00
|
|
|
$this->settings =& Settings::get_instance();
|
2012-02-01 16:36:55 -05:00
|
|
|
|
2012-02-03 16:17:19 -05:00
|
|
|
$this->set_position(Gtk::WIN_POS_CENTER);
|
2012-02-01 16:36:55 -05:00
|
|
|
$this->set_title("Add Database Connection");
|
2012-01-31 12:42:38 -05:00
|
|
|
|
2012-02-21 20:15:49 -05:00
|
|
|
// Set up the form elements, with default values
|
|
|
|
$this->conn = new GtkEntry();
|
|
|
|
$this->host = new GtkEntry();
|
|
|
|
$this->user = new GtkEntry();
|
|
|
|
$this->pass = new GtkEntry();
|
|
|
|
$this->dbtype = GtkComboBox::new_text();
|
|
|
|
$this->db_file = new GtkFileChooserButton("Select a database file",
|
|
|
|
Gtk::FILE_CHOOSER_ACTION_OPEN);
|
|
|
|
|
|
|
|
// Populate the available database types
|
|
|
|
$db_types = $this->get_available_dbs();
|
|
|
|
foreach($db_types as $t)
|
|
|
|
{
|
|
|
|
$this->dbtype->append_text($t);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset defaults when changing db types
|
|
|
|
$this->dbtype->connect_simple("changed", array($this, "change_db"));
|
|
|
|
|
|
|
|
// Populate the text fields with default values
|
|
|
|
$this->conn->set_text($conn);
|
|
|
|
$this->host->set_text($host);
|
|
|
|
$this->user->set_text($user);
|
|
|
|
$this->pass->set_text($pass);
|
|
|
|
$this->db_file->set_uri($db_file);
|
|
|
|
|
|
|
|
// Create the layout table
|
|
|
|
$this->table = new GtkTable();
|
|
|
|
|
2012-01-31 12:42:38 -05:00
|
|
|
// Add the Vbox, and show the window
|
2012-02-21 20:15:49 -05:00
|
|
|
$this->_layout();
|
|
|
|
$this->add($this->table);
|
2012-01-31 12:42:38 -05:00
|
|
|
$this->show_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Window layout
|
|
|
|
*
|
|
|
|
* @return GtkVBox
|
|
|
|
*/
|
|
|
|
private function _layout()
|
|
|
|
{
|
2012-01-31 16:19:34 -05:00
|
|
|
//Table attach
|
|
|
|
//$tbl->attach(left_start, right_stop, top_start, bottom_stop)
|
|
|
|
|
2012-02-01 10:51:06 -05:00
|
|
|
// Placeholder vars for y values, so that rows can be
|
|
|
|
// easily moved
|
|
|
|
$y1 = -1;
|
|
|
|
$y2 = 0;
|
2012-02-01 13:02:39 -05:00
|
|
|
|
|
|
|
// Connection name
|
|
|
|
{
|
2012-02-21 20:15:49 -05:00
|
|
|
$this->_add_row("Connection name", $this->conn, $y1, $y2);
|
2012-02-01 13:02:39 -05:00
|
|
|
}
|
|
|
|
|
2012-02-01 10:51:06 -05:00
|
|
|
// Database type
|
2012-01-31 16:19:34 -05:00
|
|
|
{
|
|
|
|
$dbtypelbl = new GtkLabel("Database Type");
|
|
|
|
$typealign = new GtkAlignment(0, 0.5, 0, 0);
|
|
|
|
$typealign->add($dbtypelbl);
|
2012-02-21 20:15:49 -05:00
|
|
|
$this->table->attach($typealign, 0, 1, ++$y1, ++$y2);
|
|
|
|
$this->table->attach($this->dbtype, 1, 2, $y1, $y2);
|
2012-01-31 16:19:34 -05:00
|
|
|
}
|
2012-01-31 12:42:38 -05:00
|
|
|
|
2012-02-20 16:22:03 -05:00
|
|
|
// DB File
|
|
|
|
{
|
2012-02-21 20:15:49 -05:00
|
|
|
$this->_add_row("Database File", $this->db_file, $y1, $y2);
|
2012-02-20 16:22:03 -05:00
|
|
|
}
|
|
|
|
|
2012-02-01 10:51:06 -05:00
|
|
|
// Host
|
2012-01-31 12:42:38 -05:00
|
|
|
{
|
2012-02-21 20:15:49 -05:00
|
|
|
$this->_add_row("Host", $this->host, $y1, $y2);
|
2012-01-31 16:19:34 -05:00
|
|
|
}
|
|
|
|
|
2012-02-01 10:51:06 -05:00
|
|
|
// Username
|
2012-01-31 16:19:34 -05:00
|
|
|
{
|
2012-02-21 20:15:49 -05:00
|
|
|
$this->_add_row("User", $this->user, $y1, $y2);
|
2012-01-31 12:42:38 -05:00
|
|
|
}
|
|
|
|
|
2012-02-01 10:51:06 -05:00
|
|
|
// Password
|
2012-01-31 16:19:34 -05:00
|
|
|
{
|
2012-02-21 20:15:49 -05:00
|
|
|
$this->_add_row("Password", $this->pass, $y1, $y2);
|
2012-01-31 16:19:34 -05:00
|
|
|
}
|
2012-01-31 12:42:38 -05:00
|
|
|
|
2012-02-06 12:14:39 -05:00
|
|
|
// Add connection button
|
|
|
|
{
|
|
|
|
$add_button = new GtkButton();
|
|
|
|
$add_button->set_label("Add Connnection");
|
2012-02-10 13:13:19 -05:00
|
|
|
$add_button->set_image(GTKImage::new_from_stock(GTK::STOCK_ADD,
|
|
|
|
Gtk::ICON_SIZE_SMALL_TOOLBAR));
|
2012-02-21 20:15:49 -05:00
|
|
|
$this->table->attach($add_button, 0, 3, ++$y1, ++$y2);
|
2012-02-06 12:14:39 -05:00
|
|
|
$add_button->connect_simple("clicked", array($this, 'db_add'));
|
|
|
|
}
|
2012-01-30 07:57:17 -05:00
|
|
|
}
|
2012-01-31 16:29:48 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks what database drivers are available
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-02-21 11:45:42 -05:00
|
|
|
public function get_available_dbs()
|
2012-01-31 16:29:48 -05:00
|
|
|
{
|
|
|
|
$drivers = array();
|
|
|
|
|
|
|
|
// Check if there is pdo support
|
|
|
|
if( ! function_exists('pdo_drivers'))
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-01-31 17:52:46 -05:00
|
|
|
// Add PDO drivers
|
|
|
|
foreach(pdo_drivers() as $d)
|
|
|
|
{
|
2012-02-01 13:02:39 -05:00
|
|
|
// Skip sqlite2 as opposed to sqlite3
|
2012-01-31 17:52:46 -05:00
|
|
|
if($d === 'sqlite2')
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-02-01 16:36:55 -05:00
|
|
|
// Replace default capitalization with something that looks better.
|
|
|
|
$d = str_replace("sql", "SQL", $d);
|
|
|
|
$d = str_ireplace("pg", "Postgre", $d);
|
2012-02-01 21:02:11 -05:00
|
|
|
$d = str_ireplace("odbc", "ODBC", $d);
|
2012-02-01 16:36:55 -05:00
|
|
|
$d = ucfirst($d);
|
|
|
|
|
2012-01-31 17:52:46 -05:00
|
|
|
$drivers[] = $d;
|
|
|
|
}
|
2012-01-31 16:29:48 -05:00
|
|
|
|
2012-01-31 17:52:46 -05:00
|
|
|
// Add firebird support, if exists
|
2012-01-31 16:29:48 -05:00
|
|
|
if(function_exists('ibase_connect'))
|
|
|
|
{
|
|
|
|
$drivers[] = "Firebird";
|
|
|
|
}
|
|
|
|
|
|
|
|
sort($drivers);
|
|
|
|
|
|
|
|
return $drivers;
|
|
|
|
}
|
2012-02-01 13:02:39 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple helper function for adding a row to the GtkTable
|
|
|
|
*
|
|
|
|
* @param GtkTable &$table
|
|
|
|
* @param string $label
|
|
|
|
* @param mixed &$vname
|
|
|
|
* @param int &$y1
|
|
|
|
* @param int &$y2
|
|
|
|
*/
|
2012-02-21 20:15:49 -05:00
|
|
|
private function _add_row($label, &$vname, &$y1, &$y2)
|
2012-02-01 13:02:39 -05:00
|
|
|
{
|
|
|
|
$lbl = new GtkLabel($label);
|
2012-02-21 20:15:49 -05:00
|
|
|
//$vname =& $this->{$vname};
|
2012-02-01 13:02:39 -05:00
|
|
|
$lblalign = new GtkAlignment(0, 0.5, 0, 0);
|
|
|
|
$lblalign->add($lbl);
|
|
|
|
|
2012-02-21 20:15:49 -05:00
|
|
|
$this->table->attach($lblalign, 0, 1, ++$y1, ++$y2);
|
|
|
|
$this->table->attach($vname, 1, 2, $y1, $y2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set defaults for new database type
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2012-02-21 20:19:21 -05:00
|
|
|
public function change_db()
|
2012-02-21 20:15:49 -05:00
|
|
|
{
|
|
|
|
$new_db = $this->dbtype->get_active_text();
|
|
|
|
|
|
|
|
switch($new_db)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
$this->host->set_text('localhost');
|
|
|
|
$this->db_file->set_uri(NULL);
|
|
|
|
|
|
|
|
case "MySQL":
|
|
|
|
$this->user->set_text('root');
|
|
|
|
$this->pass->set_text('');
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "PostgreSQL":
|
|
|
|
$this->user->set_text('');
|
|
|
|
$this->pass->set_text('');
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "Firebird":
|
|
|
|
$this->user->set_text('sysdba');
|
|
|
|
$this->pass->set_text('masterkey');
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "ODBC":
|
|
|
|
case "SQLite":
|
|
|
|
$this->user->set_text('');
|
|
|
|
$this->pass->set_text('');
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
2012-02-01 13:02:39 -05:00
|
|
|
}
|
2012-02-06 12:14:39 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds the database to the settings file
|
|
|
|
*/
|
2012-02-21 11:45:42 -05:00
|
|
|
public function db_add()
|
2012-02-06 12:14:39 -05:00
|
|
|
{
|
|
|
|
$data = array(
|
2012-02-21 17:00:22 -05:00
|
|
|
'type' => strtolower($this->dbtype->get_active_text()),
|
2012-02-06 12:14:39 -05:00
|
|
|
'host' => $this->host->get_text(),
|
|
|
|
'user' => $this->user->get_text(),
|
|
|
|
'pass' => $this->pass->get_text(),
|
2012-02-21 17:00:22 -05:00
|
|
|
'file' => $this->db_file->get_uri(),
|
2012-02-06 12:14:39 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->settings->add_db($this->conn->get_text(), $data);
|
|
|
|
|
|
|
|
// Destroy this window
|
|
|
|
$this->destroy();
|
|
|
|
}
|
2012-01-30 07:57:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// End of add_db.php
|