2012-03-30 14:48:50 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* OpenSQLManager
|
|
|
|
*
|
|
|
|
* Free Database manager for Open Source Databases
|
|
|
|
*
|
2012-04-20 13:30:27 -04:00
|
|
|
* @package OpenSQLManager
|
2012-03-30 14:48:50 -04:00
|
|
|
* @author Timothy J. Warren
|
|
|
|
* @copyright Copyright (c) 2012
|
|
|
|
* @link https://github.com/aviat4ion/OpenSQLManager
|
|
|
|
* @license http://philsturgeon.co.uk/code/dbad-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2012-04-20 13:30:27 -04:00
|
|
|
* Class to simplify dealing with GtkTreeView
|
|
|
|
*
|
|
|
|
* @package OpenSQLManager
|
|
|
|
* @subpackage Widgets
|
2012-03-30 14:48:50 -04:00
|
|
|
*/
|
|
|
|
class Data_Grid extends GtkTreeView {
|
|
|
|
|
2012-04-19 12:29:47 -04:00
|
|
|
/**
|
|
|
|
* GtkTreeStore object
|
2012-04-19 21:55:44 -04:00
|
|
|
*
|
|
|
|
* @var GtkTreeStore
|
2012-04-19 12:29:47 -04:00
|
|
|
*/
|
2012-03-30 14:48:50 -04:00
|
|
|
protected $model;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the object
|
|
|
|
*
|
|
|
|
* @param object $model
|
|
|
|
*/
|
|
|
|
public function __construct($model = null)
|
|
|
|
{
|
2012-04-12 11:32:15 -04:00
|
|
|
if ( ! is_null($model))
|
|
|
|
{
|
|
|
|
$this->model = $model;
|
|
|
|
parent::__construct($this->model);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
2012-03-30 14:48:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the value of the model for the current selection
|
|
|
|
*
|
|
|
|
* @param int pos
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function get($pos = 0)
|
|
|
|
{
|
2012-04-11 11:16:44 -04:00
|
|
|
if ( ! is_numeric($pos))
|
|
|
|
{
|
|
|
|
return parent::get($pos);
|
|
|
|
}
|
|
|
|
|
2012-03-30 14:48:50 -04:00
|
|
|
// Get the selection object of the row
|
|
|
|
$sel = $this->get_selection();
|
|
|
|
|
|
|
|
// Get the model and iterator for the selected row
|
|
|
|
list($model, $iter) = $sel->get_selected();
|
|
|
|
|
2012-04-10 10:47:37 -04:00
|
|
|
// Return on lack of $iter
|
|
|
|
if (is_null($iter))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-30 14:48:50 -04:00
|
|
|
// Get the data from the model
|
|
|
|
return $model->get_value($iter, $pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2012-04-11 14:57:38 -04:00
|
|
|
* Clear datagrid
|
|
|
|
*
|
|
|
|
* @return void
|
2012-03-30 14:48:50 -04:00
|
|
|
*/
|
|
|
|
public function reset()
|
|
|
|
{
|
|
|
|
$this->model->clear();
|
|
|
|
|
|
|
|
$cols = $this->get_columns();
|
|
|
|
|
2012-05-14 13:25:32 -04:00
|
|
|
foreach($cols as &$c)
|
2012-03-30 14:48:50 -04:00
|
|
|
{
|
|
|
|
$this->remove_column($c);
|
|
|
|
}
|
|
|
|
}
|
2012-04-11 11:16:44 -04:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-11 12:50:06 -04:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a datagrid from the provided data array
|
|
|
|
*
|
|
|
|
* @param array $data
|
2012-04-11 14:57:38 -04:00
|
|
|
* @param array $events
|
2012-04-11 12:50:06 -04:00
|
|
|
* @return void
|
|
|
|
*/
|
2012-04-11 14:57:38 -04:00
|
|
|
public function render_data($data, $events=array())
|
2012-04-11 12:50:06 -04:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
$this->set_model($model);
|
|
|
|
$this->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);
|
2012-05-14 13:25:32 -04:00
|
|
|
foreach($data[$i] as &$v)
|
2012-04-11 12:50:06 -04:00
|
|
|
{
|
|
|
|
$vals[] = ++$j;
|
|
|
|
$vals[] = trim($v);
|
|
|
|
}
|
|
|
|
|
|
|
|
call_user_func_array(array($model, 'set'), $vals);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add columns to view
|
2012-05-14 13:25:32 -04:00
|
|
|
foreach($cols as $i => &$c)
|
2012-04-11 12:50:06 -04:00
|
|
|
{
|
|
|
|
$renderer = new GtkCellRendererText();
|
|
|
|
$renderer->set_property('editable', TRUE);
|
|
|
|
$this->insert_column_with_data_func($i, $c, $renderer, array($this, 'add_data_col'), $i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-30 14:48:50 -04:00
|
|
|
}
|
|
|
|
// End of data_grid.php
|