This repository has been archived on 2018-10-12. You can view files and clone it, but cannot push or open issues or pull requests.
OpenSQLManager/sys/widgets/connection_sidebar.php

226 lines
4.8 KiB
PHP

<?php
/**
* OpenSQLManager
*
* Free Database manager for Open Source Databases
*
* @package OpenSQLManager
* @author Timothy J. Warren
* @copyright Copyright (c) 2012
* @link https://github.com/aviat4ion/OpenSQLManager
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
namespace OpenSQLManager;
/**
* Widget managing saved database connections
*
* @package OpenSQLManager
* @subpackage Widgets
*/
class Connection_Sidebar extends \wxPanel {
const MENU_CONNECT = 1;
const MENU_DISCONNECT = 2;
const MENU_EDIT_CONNECT = 3;
const MENU_DELETE_CONNECT = 4;
const BTN_ADD = 5;
/**
* Reference to Settings instance
*
* @var Settings
*/
protected $settings;
/**
* Reference to popup menu
*
* @var wxMenu
*/
protected $menu;
/**
* Singleton instance
*
* @var Connection_Sidebar
*/
private static $instance;
/**
* Name of current db connection
*
* @var string
*/
private $conn_name;
/**
* Reference to the list control that holds the connections
*
* @var wxListCtrl
*/
private $list;
/**
* Return the current instance of the class
*
* @param wxWindow
* @return Connection_Sidebar
*/
public static function &get_instance($parent)
{
if( ! isset(self::$instance))
{
$name = __CLASS__;
self::$instance = new $name($parent);
}
return self::$instance;
}
// --------------------------------------------------------------------------
/**
* Constructor method
*
* @param wxWindow
*/
public function __construct($parent)
{
// Create the frame
parent::__construct($parent, 1);
$this->list = new \wxListCtrl($parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_LIST|wxLC_SINGLE_SEL);
$this->settings =& Settings::get_instance();
$this->list->Connect(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, array($this, 'menu'));
// Create a button for adding new connections
$new_conn = new \wxButton($this, self::BTN_ADD, 'New Connection');
$new_conn->Connect(wxEVT_COMMAND_BUTTON_CLICKED, array($this, 'add_conn'));
// Layout the connection list
$this->_layout();
// Add a sizer
$sizer = new \wxBoxSizer(wxVERTICAL);
$sizer->add($this->list, 1, wxALL|wxEXPAND);
$sizer->add($new_conn, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_BOTTOM|wxEXPAND, 5);
$this->SetSizer($sizer);
$this->Layout();
$this->Fit();
}
// --------------------------------------------------------------------------
/**
* Right-click event to create context menu
*
* @param wxEvent
* @return void
*/
public function menu($event)
{
if ($this->list->GetSelectedItemCount() === 1)
{
// Create the menu items
$menu = new \wxMenu();
$menu->Append(self::MENU_EDIT_CONNECT, "Edit Connection", "Edit Connection Settings for the selected Database");
$menu->Append(self::MENU_DELETE_CONNECT, "Delete Connection", "Remove the selected connection");
// Wire up the event handler
$menu->Connect(wxEVT_COMMAND_MENU_SELECTED, array($this, 'menu_event'));
// Tell the object to show the menu
$this->list->PopupMenu($menu);
}
}
// --------------------------------------------------------------------------
/**
* Handler for context menu options
*
* @param wxEvent
* @return void
*/
public function menu_event($event)
{
//alert(print_r($event, TRUE));
}
// --------------------------------------------------------------------------
/**
* Handles an event for adding a connection
*
* @param wxEvent
* @return void
*/
public function add_conn($event)
{
$win = new Connection_Manager($this);
$win->show();
}
// --------------------------------------------------------------------------
/**
* Add the existing items to the connection sidebar
*
* @return void
*/
private function _layout()
{
// Create an ImageList
/*\wxBitmap::InitStandardHandlers();
$img_list = new \wxImageList();
$img_list->Add(new \wxBitmap(BASE_DIR . '/images/firebirdlogo32.xpm', wxBITMAP_TYPE_XPM));
$img_list->Add(new \wxBitmap(BASE_DIR . '/images/mysqllogo32.xpm', wxBITMAP_TYPE_XPM));
$img_list->Add(new \wxBitmap(BASE_DIR . '/images/posgresqllogo32.xpm', wxBITMAP_TYPE_XPM));
$img_list->Add(new \wxBitmap(file_get_contents(BASE_DIR . '/images/sqlitelogo32.xpm'), wxBITMAP_TYPE_XPM_DATA));*/
// Add the actual connections
$conns = $this->settings->get_dbs();
foreach($conns as $c)
{
/*switch($c->type)
{
case "firebird":
$img_num = 0;
break;
case "mysql":
$img_num = 1;
break;
case "pgsql":
$img_num = 2;
break;
case "sqlite":
$img_num = 3;
break;
default:
$img_num = NULL;
break;
}*/
/*if ( ! is_null($img_num))
{
$this->list->InsertItem(0, $c->name, 0);
}
else
{*/
$this->list->InsertItem(0, $c->name);
//}
}
}
}
// End of connection_sidebar.php