OpenSQLManager/sys/windows/widgets/db_tabs.php

340 lines
7.0 KiB
PHP
Raw Normal View History

<?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
*/
// --------------------------------------------------------------------------
/**
* Tabbed Container for database properties
*/
class DB_tabs extends GTKNotebook {
2012-04-02 16:56:55 -04:00
/**
* Current Tab Widget object
* @var DB_Tabs
*/
2012-03-29 16:26:50 -04:00
private static $instance;
2012-04-10 15:42:12 -04:00
private $data;
2012-03-29 16:26:50 -04:00
/**
* Return the db tabs object if it exists, or create and return
*
* @return DB_tabs
*/
public static function &get_instance()
{
if (empty(self::$instance))
{
self::$instance = new DB_tabs();
}
return self::$instance;
}
// --------------------------------------------------------------------------
/**
* Create the object
*/
public function __construct()
{
parent::__construct();
2012-04-10 15:42:12 -04:00
$this->data = new StdClass();
}
// --------------------------------------------------------------------------
/**
* Add a new tab with the provided label
*
* @param string $label
* @param GObject $widget
* @return void
*/
public function add_tab($label, $widget = NULL)
{
if (is_null($widget))
{
$widget = new Data_Grid();
}
$this->append_page($widget, new GtkLabel($label));
}
2012-03-29 16:26:50 -04:00
// --------------------------------------------------------------------------
2012-04-02 09:47:13 -04:00
/**
* Create tabs for database aspects
*
* @param Query_Builder $conn
* @return void
*/
2012-04-03 10:16:25 -04:00
public static function get_db_tabs(&$conn)
2012-04-02 09:47:13 -04:00
{
2012-04-03 11:23:53 -04:00
// Empty the tabs
self::reset();
2012-04-10 15:42:12 -04:00
self::$instance->hide_all();
2012-04-03 11:23:53 -04:00
// 'Databases' Tab
{
2012-04-10 15:42:12 -04:00
self::_add_tab($conn, 'Databases', 'Db Name', 'get_dbs', array(
2012-04-09 15:24:10 -04:00
'row-activated' => array(self::$instance, '_switch_db'),
));
2012-04-03 11:23:53 -04:00
}
// 'Schemas' Tab
{
self::_add_tab($conn, 'Schemas', 'Schema Name', 'get_schemas');
}
2012-04-04 07:47:39 -04:00
// 'Tables' Tab
{
2012-04-04 16:28:18 -04:00
self::_add_tab($conn, 'Tables', 'Table Name', 'get_tables');
2012-04-09 10:52:51 -04:00
}
// 'System Tables' Tab
{
self::_add_tab($conn, 'System Tables', 'Table Name', 'get_system_tables');
2012-04-04 07:47:39 -04:00
}
// 'Views' Tab
{
2012-04-04 16:28:18 -04:00
self::_add_tab($conn, 'Views', 'View Name', 'get_views');
2012-04-04 07:47:39 -04:00
}
// 'Sequences' Tab
{
self::_add_tab($conn, 'Sequences', 'Sequence Name', 'get_sequences');
}
2012-04-06 21:07:49 -04:00
// 'Triggers' Tab
{
2012-04-09 13:27:12 -04:00
self::_add_row_tab($conn, 'Triggers','get_triggers');
2012-04-06 21:07:49 -04:00
}
// 'Procedures' Tab
{
2012-04-09 13:27:12 -04:00
self::_add_tab($conn, 'Procedures', 'Procedure name', 'get_procedures');
}
// 'Functions' Tab
{
self::_add_row_tab($conn, 'Functions', 'get_functions');
2012-04-06 21:07:49 -04:00
}
2012-04-03 10:16:25 -04:00
self::$instance->show_all();
}
// --------------------------------------------------------------------------
/**
* Adds a column of data to the model
*
* @param GtkTreeViewColumn $col
* @param GtkCellRenderer $cell
* @param GtkTreeModel $model
* @param GtkTreeIter $iter
* @param int $i
* @return void
*/
public function add_data_col($col, $cell, $model, $iter, $i=0)
{
$data = $model->get_value($iter, $i);
if (empty($data))
{
return;
}
$col->set_visible(TRUE);
$cell->set_property('text', $data);
2012-04-02 09:47:13 -04:00
}
2012-04-03 11:23:53 -04:00
// --------------------------------------------------------------------------
/**
* Remove current tabs
*/
public static function reset()
{
2012-04-10 15:42:12 -04:00
self::$instance->hide_all();
2012-04-09 08:57:01 -04:00
for($i=self::$instance->get_n_pages(); $i >= 0; $i--)
2012-04-03 11:23:53 -04:00
{
self::$instance->remove_page($i);
}
2012-04-10 15:42:12 -04:00
self::$instance->show_all();
2012-04-03 11:23:53 -04:00
}
2012-04-04 16:28:18 -04:00
// --------------------------------------------------------------------------
/**
* Simplify adding tabs to the Notebook object
*
* @param object $conn
* @param string $tab_name
* @param string $col_name
* @param string $method
2012-04-10 15:42:12 -04:00
* @param array $events
2012-04-04 16:28:18 -04:00
* @return void
*/
2012-04-10 15:42:12 -04:00
private static function _add_tab(&$conn, $tab_name, $col_name, $method, $events=array())
2012-04-04 16:28:18 -04:00
{
$tab = new Data_Grid();
$tab_model = $tab->get_model();
2012-04-10 15:42:12 -04:00
$conn_name = $conn->conn_name;
if ( ! isset(self::$instance->data->{$conn_name}))
{
self::$instance->data->{$conn_name}= array();
}
$instance_data =& self::$instance->data->{$conn_name};
$tab_data = (empty($instance_data[$tab_name]))
? call_user_func_array(array($conn, $method), array())
: $instance_data[$tab_name];
$instance_data[$tab_name] = $tab_data;
2012-04-04 16:28:18 -04:00
2012-04-06 21:07:49 -04:00
if ($tab_data !== FALSE)
2012-04-04 16:28:18 -04:00
{
foreach($tab_data as $d)
{
$tab_model->append(null, array($d));
}
$cell_renderer = new GtkCellRendererText();
$cell_renderer->set_property('editable', FALSE);
2012-04-04 16:28:18 -04:00
$tab->insert_column_with_data_func(0, $col_name, $cell_renderer, array(self::$instance, 'add_data_col'));
2012-04-09 15:24:10 -04:00
if ( ! empty($events))
{
foreach($events as $name => $method)
{
$tab->connect($name, $method);
}
}
2012-04-04 16:28:18 -04:00
self::$instance->add_tab($tab_name, $tab);
}
2012-04-09 13:27:12 -04:00
return;
}
// --------------------------------------------------------------------------
/**
* Add a multidimensional array to a tab
*
* @param object $conn
* @param string $tab_name
* @param string $method
* @return void
*/
private static function _add_row_tab(&$conn, $tab_name, $method)
{
2012-04-10 15:42:12 -04:00
$conn_name = $conn->conn_name;
if ( ! isset(self::$instance->data->{$conn_name}))
{
self::$instance->data->{$conn_name}= array();
}
$instance_data =& self::$instance->data->{$conn_name};
$tab_data = (empty($instance_data[$tab_name]))
? call_user_func_array(array($conn, $method), array())
: $instance_data[$tab_name];
$instance_data[$tab_name] = $tab_data;
2012-04-09 13:27:12 -04:00
if ( ! empty($tab_data))
{
$tab_model = new StdClass();
$cols = array_keys($tab_data[0]);
// Add columns to model
$model_args = array_fill(0, count($cols), Gobject::TYPE_PHP_VALUE);
$eval_string = '$tab_model = new GTKTreeStore('.implode(',', $model_args).');';
// Shame, shame, but how else?
eval($eval_string);
$tab= new Data_Grid($tab_model);
// Set the data in the model
for($i=0, $c = count($tab_data); $i < $c; $i++)
{
// Add a row
$row = $tab_model->insert($i);
$j = -1;
$vals = array($row);
foreach($tab_data[$i] as $v)
{
$vals[] = ++$j;
$vals[] = $v;
}
call_user_func_array(array($tab_model, 'set'), $vals);
}
// Add columns to view
foreach($cols as $i => $c)
{
$renderer = new GtkCellRendererText();
$renderer->set_property('editable', TRUE);
2012-04-09 13:27:12 -04:00
$tab->insert_column_with_data_func($i, $c, $renderer, array(self::$instance, 'add_data_col'), $i);
}
self::$instance->add_tab($tab_name, $tab);
}
return;
2012-04-04 16:28:18 -04:00
}
2012-04-09 15:24:10 -04:00
/**
* Connects to a different database than the one currently in use
*
* @param type $view
* @param type $path
* @param type $column
* @param type $data
*/
public function _switch_db($view, $path, $column, $data=array())
{
// Get the selected database
$new_db = $view->get(0);
2012-04-09 16:11:14 -04:00
// Get existing connections
$conns = DB_REG::get_connections();
// Get connection info for existing connections
$conn_info = array();
foreach($conns as $c)
{
$conn_info[$c] = Settings::get_instance()->get_db($c);
}
// @todo figure out how to single out the current db connection
2012-04-09 15:24:10 -04:00
}
}
// End of db_tabs.php