OpenSQLManager/sys/windows/db_table_data.php
Timothy Warren 91edf09ff7 Fix #12
Show Database table data in a popup
2012-04-11 11:16:44 -04:00

101 lines
2.1 KiB
PHP

<?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
*/
// --------------------------------------------------------------------------
/**
* Popup window to display database table data
*/
class DB_Table_Data extends GTKWindow {
protected $win;
public function __construct($data)
{
parent::__construct();
$this->set_title("Table Data");
$this->set_position(Gtk::WIN_POS_CENTER_ALWAYS);
$this->set_destroy_with_parent(TRUE);
$this->win = new GTKScrolledWindow();
$this->win->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
$this->add($this->win);
$this->_render($data);
// Resize to a sane size
$this->set_size_request(640, 480);
}
/**
* Layout the window
*
* @param array $data
* @return void
*/
protected function _render($data)
{
if ( ! is_array($data))
{
return;
}
$model = new StdClass();
$cols = ( ! empty($data)) ? array_keys($data[0]) : array();
// Add columns to model
$col_num = (count($cols) > 0) ? count($cols) : 1;
$model_args = array_fill(0, $col_num, Gobject::TYPE_PHP_VALUE);
$eval_string = '$model = new GTKTreeStore('.implode(',', $model_args).');';
// Shame, shame, but how else?
eval($eval_string);
$view = new Data_Grid($model);
$view->set_headers_clickable(TRUE);
// Set the data in the model
for($i=0, $c = count($data); $i < $c; $i++)
{
// Add a row
$row = $model->insert($i);
$j = -1;
$vals = array($row);
foreach($data[$i] as $v)
{
$vals[] = ++$j;
$vals[] = trim($v);
}
call_user_func_array(array($model, 'set'), $vals);
}
// Add columns to view
foreach($cols as $i => $c)
{
$renderer = new GtkCellRendererText();
$renderer->set_property('editable', TRUE);
$view->insert_column_with_data_func($i, $c, $renderer, array($view, 'add_data_col'), $i);
}
// Add the grid to the window
$this->win->add_with_viewport($view);
$this->show_all();
}
}