Skip to content

Commit

Permalink
Continued work on 3.0.0 - installation + Data Type plugin design
Browse files Browse the repository at this point in the history
  • Loading branch information
benkeen committed Apr 29, 2012
1 parent cd88cff commit 966941c
Show file tree
Hide file tree
Showing 17 changed files with 500 additions and 2,533 deletions.
24 changes: 20 additions & 4 deletions classes/AjaxRequest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,40 @@ public function __construct($action, $post = array()) {
gd_delete_form($form_id);
break;

// a fresh install assumes it's a blank slate: no database tables, no settings file
case "install":
// check all preconditions
try {
$assertions = array("no_settings_file" => true);
$assertions = array("noSettingsFile" => true);
Utils::assert($assertions);
} catch (GDException $e) {
$this->response = $e->getFormattedError();
return;
}

list($success, $error) = Database::testDbSettings($post["dbHostname"], $post["dbName"], $post["dbUsername"], $post["dbPassword"]);
// check the database settings provided are valid
list($success, $message) = Database::testDbSettings($post["dbHostname"], $post["dbName"], $post["dbUsername"], $post["dbPassword"]);
if (!$success) {
$this->response["success"] = 0;
$this->response["error"] = $error;
$this->response["message"] = $message;
return;
}

// okay! Time to create the settings file and database
list($success, $message) = Installation::createSettingsFile($post["dbHostname"], $post["dbName"],
$post["dbUsername"], $post["dbPassword"], $post["tablePrefix"]);

if (!$success) {
$this->response["success"] = 0;
$this->response["message"] = $message;
return;
}

// now create the database tables
break;

case "create_database":
break;

case "login":
break;

Expand Down
130 changes: 77 additions & 53 deletions classes/Core.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,119 +15,143 @@ class Core {

// overridable settings that the user may define in settings.php
private static $dbHostname;
private static $dbName;
private static $dbName;
private static $dbUsername;
private static $dbPassword;
private static $dbTablePrefix;
private static $dbPassword;
private static $dbTablePrefix = "gd_";
private static $errorReporting = 2047;
private static $maxGeneratedRows = 100000;
private static $defaultNumRows = 100;
private static $defaultLanguageFile = "en";
private static $dataTypeGroups = array("human_data", "text", "other");
private static $defaultLanguageFile = "en";
private static $dataTypeGroups = array("human_data", "text", "other"); // ...

// non-overidable settings
private static $version = "3.0.0";
private static $minimumPHPVersion = '5.2.0';
private static $settingsFileExists = false;
private static $version = "3.0.0";
private static $minimumPHPVersion = "5.2.0";
private static $settingsFileExists = false;
private static $language;
private static $db;

// Hmm.... I like these being public for the simplicity of the calling syntax [Core::$smarty->doStuff() instead
// of Core::getSmarty()->customDoStuff()] but it seems poorly designed. Maybe run this by Julius if no solution
// presents itself.
// presents itself
public static $smarty;
public static $translations;
public static $user; // the current logged in user
public static $db;


public static function init() {
self::$smarty = new Smarty();

// find out if the settings file is defined and override the default properties
$settingsFilePath = realpath(dirname(__FILE__) . "/../settings.php");
$defaultLanguage = "";
if (file_exists($settingsFilePath)) {
self::$settingsFileExists = true;
require_once($settingsFilePath); // boy I don't like this!

if (isset($errorReporting)) {
self::$errorReporting = $errorReporting;
}
if (isset($dbHostname)) {
self::$dbHostname = $dbHostname;
}
if (isset($dbName)) {
self::$dbName = $dbName;
}
if (isset($dbUsername)) {
self::$dbUsername = $dbUsername;
}
if (isset($dbPassword)) {
self::$dbPassword = $dbPassword;
}
if (isset($dbTablePrefix)) {
self::$dbTablePrefix = $dbTablePrefix;
}
if (isset($defaultLanguageFile)) {
self::$defaultLanguageFile;
}
}
self::loadSettingsFile();

error_reporting(self::$errorReporting);

self::$smarty = new Smarty();
self::$translations = new Translations();
self::$language = new Language(self::$defaultLanguageFile);

self::initDatabase();
}


/**
* Attempts to load the settings file. If successful, it updates the various private member vars
* with whatevers been defined.
*/
public function loadSettingsFile() {
$settingsFilePath = realpath(dirname(__FILE__) . "/../settings.php");
if (file_exists($settingsFilePath)) {
self::$settingsFileExists = true;
require_once($settingsFilePath); // boy I don't like this...

if (isset($dbHostname)) {
self::$dbHostname = $dbHostname;
}
if (isset($dbName)) {
self::$dbName = $dbName;
}
if (isset($dbUsername)) {
self::$dbUsername = $dbUsername;
}
if (isset($dbPassword)) {
self::$dbPassword = $dbPassword;
}
if (isset($dbTablePrefix)) {
self::$dbTablePrefix = $dbTablePrefix;
}
if (isset($errorReporting)) {
self::$errorReporting = $errorReporting;
}
if (isset($maxGeneratedRows)) {
self::$maxGeneratedRows = $maxGeneratedRows;
}
if (isset($defaultNumRows)) {
self::$defaultNumRows = $defaultNumRows;
}
if (isset($defaultLanguageFile)) {
self::$defaultLanguageFile;
}
}
}

public function initDatabase() {
if (Core::$settingsFileExists) {
$this->db = new Database();
}
}

public function getHostname() {
return self::$dbHostname;
return self::$dbHostname;
}

public function getDbName() {
return self::$dbName;
return self::$dbName;
}

public function getDbUsername() {
return self::$dbUsername;
return self::$dbUsername;
}

public function getDbPassword() {
return self::$dbPassword;
return self::$dbPassword;
}

public function getDbTablePrefix() {
return self::$dbTablePrefix;
return self::$dbTablePrefix;
}

public function getMaxGeneratedRows() {
return self::$maxGeneratedRows;
return self::$maxGeneratedRows;
}

public function getDefaultNumRows() {
return self::$defaultNumRows;
return self::$defaultNumRows;
}

public function getVersion() {
return self::$version;
return self::$version;
}

public function checkSettingsFileExists() {
return self::$settingsFileExists;
return self::$settingsFileExists;
}

public function getDefaultLanguageFile() {
return self::$defaultLanguageFile;
return self::$defaultLanguageFile;
}

public function getLanguage() {
return self::$language;
}

public function getDataTypeGroups() {
return self::$dataTypeGroups;
return self::$dataTypeGroups;
}

public function getMinimumPHPVersion() {
return self::$minimumPHPVersion;
return self::$minimumPHPVersion;
}

public function getDatabase() {
return self::$db;
}
}
88 changes: 85 additions & 3 deletions classes/DataType.abstract.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,92 @@

/**
* Our base class for all Data Type plugins. All Data Type plugins must define a class that
* extends this class, to ensure all required functions and properties are in place.
* extends this class, to ensure all required functions and properties are in place. If the Data Type doesn't
* extend the class, it won't be accepted.
*/
abstract class DataType {
static $processOrder;

/**
* This is required, even if you don't have any installation code to perform. It's called once, during the
* initial installation of the script. It is called AFTER the Core tables are installed, and you can rely
* on Core::$db having been initialized, and the database connection having been set up. For robustness,
* all Data Type modules should throw a GDException in the event of a problem during creation of database
* tables. If a problem occurs, all tables created are rolled back and an appropriate error message is displayed
* to the user. If no problems occur, this method should return and do nothing.
*/
abstract static function install();

/**
* This returns the field group that this Data Type should be listed in. See the Core::$dataTypeGroups for the
* available options.
*
* @return string
*/
abstract function getDataTypeFieldGroup();

/**
* Returns the order within the field group that this Data Type should appear.
*
* @return integer
*/
abstract function getDataTypeFieldGroupOrder();

/**
* Returns the order in which this data type should be parsed. The generator does N number of passes for each
* row of data generated, each pass processes whatever data types are . This allows
*/
abstract function getProcessOrder();
}

/**
* Called by process.php during data generation. This determines what options the user selected in the user
* interface; it's used to figure out what settings to pass to each Data Type to provide that function the
* information needed to generate that particular data item.
*
* Note: if this function determines that the values entered by the user in the options column are invalid
* (most likely just incomplete) the function can explicitly return false to tell the core script to ignore
* this row.
*
* @param array $post the entire contents of $_POST
* @param integer the column number (well, *row* in the UI!) of the item
* @param integer the number of columns in the data set
* @return array a hash of ... [TODO]
*/
abstract function getTemplateOptions($postdata, $column, $numCols);

/**
* For this data type, row # and metadata aren't needed.
*
* @param integer $row the row number in the generated content
* @param mixed $options whatever options were passed for this Data Type. This could be any data type.
* @param array $metadata
* @return string
*/
abstract function generateItem($row, $options, $existingRowData);

/**
* For this data type, row # and metadata aren't needed.
*
* @param string $export_type e.g. "sql"
* @param mixed $options e.g. "mysql" or "oracle"
* @return string
*/
abstract function getExportTypeInfo($exportType, $options);

/**
* Returns true if this Data Type has a help dialog.
*
* @return boolean
*/
abstract function hasHelpDialog();

/**
* Returns information about the help dialog for this Data Type. It returns a hash with two keys:
* [dialogWidth]
* [content]
*
* [shouldn't be required... just like install(), but I'd like to mention it in this file for documentation purposes]
*
*/
abstract function getHelpDialogInfo();

}
30 changes: 15 additions & 15 deletions classes/Database.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@


class Database {
private $link;

public function __construct() {
$dbHostname = Core::getHostname();
$dbUsername = Core::getDbUsername();
$dbPassword = Core::getDbPassword();
$dbName = Core::getDbPassword();

}
try {
$this->link = mysql_connect($dbHostname, $dbUsername, $dbPassword);
} catch (Exception $e) {
// or die("Couldn't connect to database: " . mysql_error());
}


/**
* Connects to a database. After connecting, you should always call disconnect_db() to close it
* when done.
*/
public function connect() {
global $g_db_hostname, $g_db_username, $g_db_password, $g_db_name;

$link = mysql_connect($g_db_hostname, $g_db_username, $g_db_password)
or die("Couldn't connect to database: " . mysql_error());
@mysql_select_db($g_db_name)
or die ("couldn't find database '$g_db_name': " . mysql_error());

return $link;
try {
@mysql_select_db($dbName);
} catch (Exception $e) {
// die ("couldn't find database '$g_db_name': " . mysql_error());
}
}


Expand Down
10 changes: 6 additions & 4 deletions classes/GDException.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ public function __construct($code, $data = array()) {
}

public function getFormattedError() {
$language = Core::getLanguage();
$languageStrings = $language->getCurrentLanguageStrings();
$errorCode = $this->getCode();
$errorMessage = "";
$language = Core::getLanguage()->getCurrentLanguageStrings();
$errorCode = $this->getCode();
$errorMessage = "";

switch ($errorCode) {
case Exceptions::SETTINGSFILEEXISTS:
$errorMessage = $language["settings_file_exists"];
break;
case Exceptions::NOTLOGGEDIN:
$errorMessage = "";
break;
Expand Down
Loading

0 comments on commit 966941c

Please sign in to comment.