<?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 for adding / Editing Connections
 *
 * @package OpenSQLManager
 * @subpackage Widgets
 */
class Connection_Manager extends \wxFrame {
	
	const TXT_CONN_NAME = 1;
	const COMBO_DB_TYPE = 2;
	const FILE_DB_FILE = 3;
	const TXT_DB_NAME = 4;
	const TXT_DB_HOST = 5;
	const TXT_DB_PORT = 6;
	const TXT_DB_USER = 7;
	const TXT_DB_PASS = 8;
	
	/**
	 * Array of fields for Connection Information manipulation
	 *
	 * @var array
	 */
	protected $fields = array();
	
	/**
	 * Create the window
	 *
	 * @param wxWindow
	 * @param mixed
	 */
	public function __construct($parent, $params = array())
	{
		parent::__construct($parent, 32, "Connection Manager", wxDefaultPosition, new \wxSize(640, 480));//, wxCAPTION|wxCLOSE_BOX);
		
		// Layout the window
		$this->_layout($params);
	}
	
	// --------------------------------------------------------------------------
	
	/**
	 * Layout fields on the form
	 *
	 * @param array
	 */
	protected function _layout($params)
	{
		// Use a table-like sizer
		$sizer = new \wxFlexGridSizer(2, 5, 5);
		$sizer->SetFlexibleDirection(wxBOTH);
		$sizer->AddGrowableCol(0, 1);
		$sizer->AddGrowableCol(1, 1);
		
		$db_types = $this->get_available_dbs();
		
		if ($db_types === FALSE)
		{
			error("No valid databases set up in PHP");
			return;
		}
		
		// Create the controls
		// label => control
		$this->fields = array(
			'Connection name' => new \wxTextCtrl($this, self::TXT_CONN_NAME),
			'Database Type' => $choice = new \wxChoice(),
			'Database File' => new \wxFilePickerCtrl($this, self::FILE_DB_FILE, wxEmptyString, "Select the database file", wxFileSelectorDefaultWildcardStr),
			'Database Name' => new \wxTextCtrl($this, self::TXT_DB_NAME),
			'Host' => new \wxTextCtrl($this, self::TXT_DB_HOST),
			'Port' => new \wxTextCtrl($this, self::TXT_DB_PORT),
			'User' => new \wxTextCtrl($this, self::TXT_DB_USER),
			'Password' => new \wxTextCtrl($this, self::TXT_DB_PASS)
		);
		
		$choice->Create($this, self::COMBO_DB_TYPE, wxDefaultPosition, wxDefaultSize, $db_types);
		
		// Add the controls to the sizer
		$i = 1;
		foreach ($this->fields as $lbl => $ctrl)
		{
			$label = new \wxStaticText($this, $i, $lbl);
			
			$sizer->Add($label, 0, wxALIGN_LEFT);
			$sizer->Add($ctrl, 1, wxALIGN_RIGHT|wxEXPAND);
			
			$i++;
		}
		
		$this->SetSizer($sizer);
		$this->Layout();
		
		$this->Center(wxBOTH);
	}
	
	// --------------------------------------------------------------------------
	
	/**
	 * Get the list of available database types
	 *
	 * return array
	 */
	protected function get_available_dbs()
	{
		$drivers = array();
		
		$pdo_drivers = \pdo_drivers();
		
		// Add PDO drivers
		foreach ($pdo_drivers as &$d)
		{
			// Skip sqlite2 as opposed to sqlite3
			if($d === 'sqlite2' && (in_array('sqlite', $pdo_drivers) || in_array('sqlite3', $pdo_drivers)))
			{
				continue;
			}
		
			// Use the ibase_functions over PDO::Firebird, at least for now
			if($d === 'firebird')
			{
				continue;
			}
		
			// Replace default capitalization with something that looks better.
			$d = str_replace("sql", "SQL", $d);
			$d = str_ireplace("pg", "Postgre", $d);
			$d = str_ireplace("odbc", "ODBC", $d);
			$d = ucfirst($d);
		
			$drivers[] = $d;
		}
		
		// Add firebird support, if exists
		if(function_exists('fbird_connect') && ! in_array('firebird', $pdo_drivers))
		{
			$drivers[] = "Firebird";
		}
		
		sort($drivers);
		
		return $drivers;
	}
}

// End of connection_manager.php