Login   Register  
PHP Classes
elePHPant
Icontem

File: myXTree/setup.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Tereshchenko Andrey  >  myXTree  >  myXTree/setup.php  >  Download  
File: myXTree/setup.php
Role: Application script
Content type: text/plain
Description: Setup script
Class: myXTree
myXTree is an interface to a SQL-tree.
Author: By
Last change:
Date: 10 years ago
Size: 15,342 bytes
 

Contents

Class file image Download
<?php
session_start();
require_once('setup.inc');
function ApplicationOnStart(&$event) {
    if ($GLOBALS['Config']->loaded == true) {
        return checkConfig();
    } else {
        $event->sender->pushEvent(new Event('onNewSetup', &$event->sender));
    }
}
function ApplicationOnContinue(&$event) {
    if (!$event->sender->defaultPage) {
        $event->sender->setDefault($GLOBALS['mainMenuPage']);
    }
}
function lbNewSetupOnClick(&$event) {
    global $Application;
    $Application->pushEvent(new Event('onNewSetup', &$event->sender));
}
function lbPkgOptionsOnClick(&$event) {
    global $Application;
    $Application->setDefault($GLOBALS['getPathsPage']);
}
function lbDBOptionsOnClick(&$event) {
    global $Application;
    $Application->setDefault($GLOBALS['getDBOptionsPage']);
}
function lbCreateTblOnClick(&$event) {
    global $Config, $Application;
    if (!$Config->loaded) {
        return raiseUserError('Set database options before create tables.', &$event->sender);
    }
    includeDBClass();
    extract($Config->getSection('DB'));
    $dsn = "mysql://$user:$pass@$host/$name";
    $db = DB::connect($dsn);
    if (PEAR::isError($db)) return raiseUserError($db->getMessage(), &$event->sender);
    if (!$fp = @fopen('base.sql', "r")) {
        return raiseError('The file "base.sql" not found.');
    }
    $query = fread($fp, filesize('base.sql'));
    fclose($fp);
    $query = prepareQuery($query, $prefix);
    preg_match_all('/.*?(?:;)/s', $query, $matches);
    foreach ($matches[0] as $query) {
       $result = $db->query(rtrim($query, ';'));
       if (PEAR::isError($result)) return raiseUserError($result->getMessage(), &$event->sender);
    }
    doMessage("The database tables are succesfully created.");
    $Application->setDefault($GLOBALS['mainMenuPage']);
}
function lbFillTblOnClick(&$event) {
    global $Config, $Application;
    if (!$Config->loaded) {
        return raiseUserError('Set database options before fill tables.', &$event->sender);
    }
    includeDBClass();
    extract($Config->getSection('DB'));
    $dsn = "mysql://$user:$pass@$host/$name";
    $db = DB::connect($dsn);
    if (PEAR::isError($db)) return raiseUserError($db->getMessage(), &$event->sender);
    if (!$fp = @fopen('tree.sql', "r")) {
        return raiseError('The file "tree.sql" not found.');
    }
    $query = fread($fp, filesize('tree.sql'));
    fclose($fp);
    $query = prepareQuery($query, $prefix);
    preg_match_all('/.*?(?:;)/s', $query, $matches);
    foreach ($matches[0] as $query) {
       $result = $db->query(rtrim($query, ';'));
       if (PEAR::isError($result)) return raiseError($result->getMessage());
    }
    doMessage("The database tables succesfully filled.");
    $Application->setDefault($GLOBALS['mainMenuPage']);
}
function lbClearTblOnClick(&$event) {
    global $Config, $Application;
    if (!$Config->loaded) {
        return raiseUserError('Set database options before clear tables.', &$event->sender);
    }
    includeDBClass();
    extract($Config->getSection('DB'));
    $dsn = "mysql://$user:$pass@$host/$name";
    $db = DB::connect($dsn);
    if (PEAR::isError($db)) return raiseUserError($db->getMessage(), &$event->sender);
    $query = 'DELETE FROM Attributes; DELETE FROM Objects;
        INSERT INTO Objects (ObjectID, ObjectName, leftLimit, rightLimit, depth)
        VALUES (200303301, "_ROOT_", 1, 2, 0);
        INSERT INTO Attributes (ObjectID, ObjectName, ObjectValue, OwnerObject)
        VALUES (200303302, "id", "200303301", 200303301);';
    $query = prepareQuery($query, $prefix);
    preg_match_all('/.*?(?:;)/s', $query, $matches);
    foreach ($matches[0] as $query) {
       $result = $db->query(rtrim($query, ';'));
       if (PEAR::isError($result)) return raiseError($result->getMessage());
    }
    doMessage("The database tables succesfully cleared.");
    $Application->setDefault($GLOBALS['mainMenuPage']);
}
function lbDeleteTblOnClick(&$event) {
    global $Config, $Application;
    if (!$Config->loaded) {
        return raiseUserError('Set database options before delete tables.', &$event->sender);
    }
    includeDBClass();
    extract($Config->getSection('DB'));
    $dsn = "mysql://$user:$pass@$host/$name";
    $db = DB::connect($dsn);
    if (PEAR::isError($db)) return raiseUserError($db->getMessage(), &$event->sender);
    $query = 'DROP TABLE Objects, Attributes, myXTree';
    $query = prepareQuery($query, $prefix);
    $result = $db->query($query);
    if (PEAR::isError($result)) return raiseUserError($result->getMessage(), &$event->sender);
    doMessage("The database tables succesfully deleted.");
    $Application->setDefault($GLOBALS['mainMenuPage']);
}
function btSetPathOnClick(&$event) {
    global $Config, $pearPath, $myXMLPath, $myXTreePath, $Application;
    $Config->newSection('PATH'); // ???????
    $Config->setValue('pear', $_SERVER['DOCUMENT_ROOT'].$pearPath->value, 'PATH');
    $Config->setValue('myxml', $_SERVER['DOCUMENT_ROOT'].$myXMLPath->value, 'PATH');
    $Config->setValue('myxtree', $_SERVER['DOCUMENT_ROOT'].$myXTreePath->value, 'PATH');
    $Config->saveConfig();
    $Application->setDefault($GLOBALS['mainMenuPage']);
    $Application->pushEvent(new Event('onSetPathComplete', &$event->sender));
}
function btCreateDBOnClick(&$event) {
    global $dbHost, $dbUser, $dbPass, $dbName, $Application;
    btSetDBOptionsOnClick(&$event);
    $link = @mysql_pconnect($dbHost->value, $dbUser->value, $dbPass->value);
    if ($link === false) {
        return raiseUserError("Could not connect: ".mysql_error(), &$event->sender);
    } else if (mysql_create_db($dbName->value, $link)) {
        doMessage("Database created successfully.");
    } else {
        return raiseUserError("Error creating database: ".mysql_error(), &$event->sender);
    }
    $Application->pushEvent(new Event('onCreateDBComplete', &$event->sender));
}
function btSetDBOptionsOnClick(&$event) {
    global $Config, $dbHost, $dbUser, $dbPass, $dbName, $tablePrefix, $Application;
    $Config->newSection('DB');
    $Config->setValue('host', $dbHost->value, 'DB');
    $Config->setValue('user', $dbUser->value, 'DB');
    $Config->setValue('pass', $dbPass->value, 'DB');
    $Config->setValue('name', $dbName->value, 'DB');
    $Config->setValue('prefix', $tablePrefix->value, 'DB');
    $Config->saveConfig();
    $Application->setDefault($GLOBALS['mainMenuPage']);
    $Application->pushEvent(new Event('onSetDBOptionsComplete', &$event->sender));
}
function btCancelOnClick(&$event) {
    global $Application;
    $Application->setDefault($GLOBALS['mainMenuPage']);
}
function ipPearPathOnInit(&$event) {
    global $Config;
    if ($Config->loaded) {
        $result = $Config->getValue('pear', 'PATH');
        if (isError($result)) return;
        $event->sender->value = '/'.basename($result);
    }
}
function ipMyXMLPathOnInit(&$event) {
    global $Config;
    if ($Config->loaded) {
        $result = $Config->getValue('myxml', 'PATH');
        if (isError($result)) return;
        $event->sender->value = '/'.basename($result);
    }
}
function ipMyXTreePathOnInit(&$event) {
    global $Config;
    if ($Config->loaded) {
        $result = $Config->getValue('myxtree', 'PATH');
        if (isError($result)) return;
        $event->sender->value = '/'.basename($result);
    }
}
function ipPearPathOnInput(&$event) {
    if (!checkFile('DB.php', $_SERVER['DOCUMENT_ROOT'].$event->sender->value)) {
        return raiseUserError('The file "DB.php" not found. Check the path to PEAR package.', &$event->sender);
    }  
}
function ipMyXMLPathOnInput(&$event) {
    if (!checkFile('myXML.php', $_SERVER['DOCUMENT_ROOT'].$event->sender->value)) {
        return raiseUserError('The file "myXML.php" not found. Check the path to myXML package.', &$event->sender);
    }
}
function ipMyXTreePathOnInput(&$event) {
    if (!checkFile('myXTree.php', $_SERVER['DOCUMENT_ROOT'].$event->sender->value)) {
        return raiseUserError('The file "myXTree.php" not found. Check the path to myXTree package.', &$event->sender);
    }
}
function ipDbHostOnInit(&$event) {
    global $Config;
    if ($Config->loaded) {
        $result = $Config->getValue('host', 'DB');
        if (isError($result)) return;
        $event->sender->value = $result;
    }
}
function ipDbUserOnInit(&$event) {
    global $Config;
    if ($Config->loaded) {
        $result = $Config->getValue('user', 'DB');
        if (isError($result)) return;
        $event->sender->value = $result;
    }
}
function ipDbPassOnInit(&$event) {
    global $Config;
    if ($Config->loaded) {
        $result = $Config->getValue('pass', 'DB');
        if (isError($result)) return;
        $event->sender->value = $result;
    }
}
function ipDbNameOnInit(&$event) {
    global $Config;
    if ($Config->loaded) {
        $result = $Config->getValue('name', 'DB');
        if (isError($result)) return;
        $event->sender->value = $result;
    }
}
function ipTablePrefixOnInit(&$event) {
    global $Config;
    if ($Config->loaded) {
        $result = $Config->getValue('prefix', 'DB');
        if (isError($result)) return;
        $event->sender->value = $result;
    }
}
function checkConfig() {
    global $Config, $Application, $mainMenuPage;
    extract($Config->getSection('PATH'));
    if (!checkFile('DB.php', $pear)) {
        return raiseUserError('The file "DB.php" not found. Check the path to PEAR package.', &$mainMenuPage);
    }
    if (!checkFile('myXML.php', $myxml)) {
        return raiseUserError('The file "myXML.php" not found. Check the path to myXML package.', &$mainMenuPage);
    }
    if (!checkFile('myXTree.php', $myxtree)) {
        return raiseUserError('The file "myXTree.php" not found. Check the path to myXTree package.', &$mainMenuPage);
    }
    extract($Config->getSection('DB'));
    $link = @mysql_pconnect($host, $user, $pass);
    if ($link === false) {
        return raiseUserError('Could not connect: '.mysql_error(), &$mainMenuPage);
    }
    if (!mysql_select_db($name, $link)) {
        return raiseUserError('Could not select database: '.mysql_error(), &$mainMenuPage);
    }
    $query = 'SELECT * FROM Objects, Attributes, myXTree WHERE Objects.ObjectID = 1 OR Attributes.ObjectID = 1 OR Version = 1';
    $query = prepareQuery($query, $prefix);
    if (!mysql_query($query, $link)) {
        return raiseUserError('Could not execute query: '.mysql_error(), &$mainMenuPage);
    }
    $Config->checked = true;
    $Application->setDefault($GLOBALS['mainMenuPage']);
}
function prepareQuery($query, $prefix) {
    return $query = str_replace(array('Objects','Attributes','myXTree'),array($prefix.'Objects',$prefix.'Attributes',$prefix.'myXTree'),$query);
}
function includeDBClass() {
    global $Config;
    $pear = $Config->getValue('pear', 'PATH');
    ini_set('include_path', $pear);
    require_once('DB.php');
}
new Application('Application');
new Config($Application, 'Config');
new Wizard($Application);
new Page($Application, 'mainMenuPage');
new FieldSet($mainMenuPage, 'Main setup menu', 'mainSetup');
new Label($mainSetup, 'New setup', 'lbNewSetup');
new Label($mainSetup, 'The easy wizard for new setup.');
new Label($mainSetup, 'Packages options', 'lbPkgOptions');
new Label($mainSetup, 'Options of packages such as paths to files.');
new Label($mainSetup, 'Database options', 'lbDBOptions');
new Label($mainSetup, 'Options for database connections.');
new Label($mainSetup, 'Create tables', 'lbCreateTbl');
new Label($mainSetup, 'Create tables in empty database with table prefix specified in database options');
new Label($mainSetup, 'Fill tables', 'lbFillTbl');
new Label($mainSetup, 'Fills in tables the data necessary for demonstration of an example.<br>NOTE: For work of automatic tests, tables should remain empty.');
new Label($mainSetup, 'Clear tables', 'lbClearTbl');
new Label($mainSetup, 'Clears tables. It is necessary for work of automatic tests.');
new Label($mainSetup, 'Delete tables', 'lbDeleteTbl');
new Label($mainSetup, 'There are no comments.');
new Page($Application, 'getPathsPage');
new Form($getPathsPage, 'setup.php', 'GET', 'getPathsForm');
new FieldSet($getPathsForm, 'Paths to packages (relative to server root)', 'pathsInfo');
$getPathsForm->newField('pearPath', '/PEAR', 'path to PEAR package', 'pathsInfo');
$getPathsForm->newField('myXMLPath', '/myXML', 'path to myXML package', 'pathsInfo');
$getPathsForm->newField('myXTreePath', '/myXTree', 'path to myXTree package', 'pathsInfo');
new Button($getPathsForm, 'Set paths', 'btSetPath');
new BitBtn($getPathsForm, 'Cancel', 'btCancelSetPath');
new Page($Application, 'getDBOptionsPage');
new Form($getDBOptionsPage, 'setup.php', 'GET', 'getDBOptionsForm');
new FieldSet($getDBOptionsForm, 'Database connection info', 'dbInfo');
$getDBOptionsForm->newField('dbHost', 'localhost', 'the mySQL server', 'dbInfo');
$getDBOptionsForm->newField('dbUser', 'root', 'the name of the user', 'dbInfo');
$getDBOptionsForm->newField('dbPass', '', 'the password of the user', 'dbInfo');
$getDBOptionsForm->newField('dbName', 'myXTree', 'the database name', 'dbInfo');
$getDBOptionsForm->newField('tablePrefix', 'xt_', 'the name prefix for tables', 'dbInfo');
new Button($getDBOptionsForm, 'Create new', 'btCreateDB');
new Button($getDBOptionsForm, 'Set options', 'btSetDBOptions');
new BitBtn($getDBOptionsForm, 'Cancel', 'btCancelSetDBOptions');
$Application->addEventHandler('ApplicationOnStart', 'onStart');
$Application->addEventHandler('ApplicationOnContinue', 'onContinue');
$lbNewSetup->addEventHandler('lbNewSetupOnClick', 'onClick');
$lbPkgOptions->addEventHandler('lbPkgOptionsOnClick', 'onClick');
$lbDBOptions->addEventHandler('lbDBOptionsOnClick', 'onClick');
$lbCreateTbl->addEventHandler('lbCreateTblOnClick', 'onClick');
$lbFillTbl->addEventHandler('lbFillTblOnClick', 'onClick');
$lbClearTbl->addEventHandler('lbClearTblOnClick', 'onClick');
$lbDeleteTbl->addEventHandler('lbDeleteTblOnClick', 'onClick');
$btSetPath->addEventHandler('btSetPathOnClick', 'onClick');
$btCreateDB->addEventHandler('btCreateDBOnClick', 'onClick');
$btSetDBOptions->addEventHandler('btSetDBOptionsOnClick', 'onClick');
$btCancelSetPath->addEventHandler('btCancelOnClick', 'onClick');
$btCancelSetDBOptions->addEventHandler('btCancelOnClick', 'onClick');
$pearPath->addEventHandler('ipPearPathOnInit', 'onInit');
$myXMLPath->addEventHandler('ipMyXMLPathOnInit', 'onInit');
$myXTreePath->addEventHandler('ipMyXTreePathOnInit', 'onInit');
$pearPath->addEventHandler('ipPearPathOnInput', 'onInput');
$myXMLPath->addEventHandler('ipMyXMLPathOnInput', 'onInput');
$myXTreePath->addEventHandler('ipMyXTreePathOnInput', 'onInput');
$dbHost->addEventHandler('ipDbHostOnInit', 'onInit');
$dbUser->addEventHandler('ipDbUserOnInit', 'onInit');
$dbPass->addEventHandler('ipDbPassOnInit', 'onInit');
$dbName->addEventHandler('ipDbNameOnInit', 'onInit');
$tablePrefix->addEventHandler('ipTablePrefixOnInit', 'onInit');
$Application->init();
$Application->run();
$Application->close();
?>