OpenSQLManager/sys/widgets/db_tabs.php

325 lines
6.8 KiB
PHP
Raw Permalink Normal View History

<?php
/**
* OpenSQLManager
*
* Free Database manager for Open Source Databases
*
2012-04-20 13:30:27 -04:00
* @package OpenSQLManager
* @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
2012-04-20 13:30:27 -04:00
*
* @package OpenSQLManager
* @subpackage Widgets
*/
2012-04-20 16:37:26 -04:00
class DB_tabs extends GtkNotebook {
2012-04-02 16:56:55 -04:00
/**
* Current Tab Widget object
2012-04-19 21:55:44 -04:00
*
2012-04-02 16:56:55 -04:00
* @var DB_Tabs
*/
2012-03-29 16:26:50 -04:00
private static $instance;
2012-04-19 12:29:47 -04:00
/**
* Db Data cache
2012-04-19 21:55:44 -04:00
*
* @var array
2012-04-19 12:29:47 -04:00
*/
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-11 14:57:38 -04:00
/**
* Create popup window with table data
*
* @param GTKTreeView $view
* @param array $path
* @param GtkTreeviewColumn $col
* @param Query_Builder $conn
* @return void
*/
public function show_table_data($view, $path, $col, &$conn)
{
$table = $view->get(0);
$query = $conn->get($table);
$data = $query->fetchAll(PDO::FETCH_ASSOC);
return new DB_Table_data($data);
}
// --------------------------------------------------------------------------
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
self::_add_tab($conn, 'Databases', 'Db Name', 'get_dbs');
// 'Schemas' Tab
self::_add_tab($conn, 'Schemas', 'Schema Name', 'get_schemas');
2012-04-04 07:47:39 -04:00
// 'Tables' Tab
self::_add_tab($conn, 'Tables', 'Table Name', 'get_tables', array(
array(
'row-activated',
array(self::$instance, 'show_table_data'),
$conn
)
));
2012-04-09 10:52:51 -04:00
// 'System Tables' Tab
self::_add_tab($conn, 'System Tables', 'Table Name', 'get_system_tables', array(
array(
'row-activated',
array(self::$instance, 'show_table_data'),
$conn
)
));
2012-04-04 07:47:39 -04:00
// 'Views' Tab
self::_add_tab($conn, 'Views', 'View Name', 'get_views', array(
array(
'row-activated',
array(self::$instance, 'show_table_data'),
$conn
)
));
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
self::_add_row_tab($conn, 'Triggers','get_triggers');
2012-04-06 21:07:49 -04:00
// 'Procedures' Tab
self::_add_row_tab($conn, 'Procedures', 'get_procedures');
2012-04-09 13:27:12 -04:00
// 'Functions' Tab
self::_add_row_tab($conn, 'Functions', 'get_functions');
2012-04-03 10:16:25 -04:00
// Show the tabs
2012-04-03 10:16:25 -04:00
self::$instance->show_all();
}
// --------------------------------------------------------------------------
2012-04-03 11:23:53 -04:00
/**
* Remove current tabs
2012-04-11 14:57:38 -04:00
*
* @param string $conn_name
* @return void
2012-04-03 11:23:53 -04:00
*/
2012-04-11 14:57:38 -04:00
public static function reset($conn_name = '')
2012-04-03 11:23:53 -04:00
{
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
2012-04-11 14:57:38 -04:00
if ( ! empty($conn_name))
{
unset(self::$instance->data->{$conn_name});
}
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
{
2012-04-12 11:32:15 -04:00
$tab = new Data_Grid(new GTKTreeStore(Gobject::TYPE_PHP_VALUE));
2012-04-04 16:28:18 -04:00
$tab_model = $tab->get_model();
2012-04-10 15:42:12 -04:00
$conn_name = $conn->conn_name;
2012-04-11 14:57:38 -04:00
$instance_data = self::$instance->_get_db_info($conn_name, $tab_name);
2012-04-10 15:42:12 -04:00
2012-04-11 14:57:38 -04:00
$tab_data = ($instance_data === FALSE)
2012-04-10 15:42:12 -04:00
? call_user_func_array(array($conn, $method), array())
2012-04-11 14:57:38 -04:00
: $instance_data;
2012-04-10 15:42:12 -04:00
2012-04-11 14:57:38 -04:00
self::$instance->_set_db_info($conn_name, $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
{
2012-05-14 13:25:32 -04:00
foreach($tab_data as &$d)
2012-04-04 16:28:18 -04:00
{
$tab_model->append(null, array($d));
}
$cell_renderer = new GtkCellRendererText();
$cell_renderer->set_property('editable', FALSE);
$tab->insert_column_with_data_func(0, $col_name, $cell_renderer, array($tab, 'add_data_col'));
2012-04-04 16:28:18 -04:00
2012-04-09 15:24:10 -04:00
if ( ! empty($events))
{
2012-05-14 13:25:32 -04:00
foreach($events as &$method)
2012-04-09 15:24:10 -04:00
{
call_user_func_array(array($tab, 'connect'), $method);
2012-04-09 15:24:10 -04:00
}
}
2012-04-04 16:28:18 -04:00
self::$instance->add_tab($tab_name, $tab);
}
2012-04-09 13:27:12 -04:00
self::$instance->show_all();
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;
2012-04-11 14:57:38 -04:00
$instance_data = self::$instance->_get_db_info($conn_name, $tab_name);
2012-04-10 15:42:12 -04:00
2012-04-11 14:57:38 -04:00
$tab_data = ($instance_data === FALSE)
2012-04-10 15:42:12 -04:00
? call_user_func_array(array($conn, $method), array())
2012-04-11 14:57:38 -04:00
: $instance_data;
2012-04-10 15:42:12 -04:00
2012-04-11 14:57:38 -04:00
self::$instance->_set_db_info($conn_name, $tab_name, $tab_data);
2012-04-09 13:27:12 -04:00
if ( ! empty($tab_data))
{
$tab = new Data_Grid();
$tab->render_data($tab_data);
2012-04-09 13:27:12 -04:00
self::$instance->add_tab($tab_name, $tab);
}
self::$instance->show_all();
2012-04-09 13:27:12 -04:00
return;
2012-04-04 16:28:18 -04:00
}
// --------------------------------------------------------------------------
/**
2012-04-11 14:57:38 -04:00
* Returns cached database data for the tab and connection specified
*
2012-04-19 12:29:47 -04:00
* @param string $conn_name
* @param string $tab_name
2012-04-11 14:57:38 -04:00
* @return mixed
*/
2012-04-11 14:57:38 -04:00
private function _get_db_info($conn_name, $tab_name)
{
2012-04-11 14:57:38 -04:00
$data =& self::$instance->data;
2012-04-11 14:57:38 -04:00
if ( ! isset($data->{$conn_name}))
{
$data->{$conn_name}= array();
return FALSE;
}
2012-04-11 14:57:38 -04:00
if ( ! isset($data->{$conn_name}[$tab_name]))
{
return FALSE;
}
return $data->{$conn_name}[$tab_name];
}
// --------------------------------------------------------------------------
/**
* Sets cached database data for the tab and connection specified
*
2012-04-19 12:29:47 -04:00
* @param string $conn_name
* @param string $tab_name
* @param mixed $data
2012-04-11 14:57:38 -04:00
*/
private function _set_db_info($conn_name, $tab_name, $data)
{
self::$instance->data->{$conn_name}[$tab_name] = $data;
}
}
2012-05-14 13:25:32 -04:00
// End of db_tabs.php