Initial commit

This commit is contained in:
whoisfrost
2026-02-17 13:30:09 -06:00
commit f24a2e2235
404 changed files with 37425 additions and 0 deletions

View File

@@ -0,0 +1,148 @@
<?php
class Database
{
var $dbLink;
var $dbServer;
var $dbUser;
var $dbPassword;
var $dbDatabase;
var $result;
var $lastRow;
var $errorState;
var $errorMessage;
/**
* Constructor
*/
function Database($dbServer = "",
$dbUser = "",
$dbPassword = "",
$dbDatabase = "") {
$this->dbServer = $dbServer;
$this->dbUser = $dbUser;
$this->dbPassword = $dbPassword;
$this->dbDatabase = $dbDatabase;
$this->errorState = false;
$this->dbLink = mysql_connect($this->dbServer,
$this->dbUser,
$this->dbPassword);
if (!$this->dbLink) {
$this->errorState = true;
$this->errorMessage = 'Could not connect: '.mysql_error();
return false;
}
if (!mysql_select_db($this->dbDatabase, $this->dbLink)) {
$this->errorState = true;
$this->errorMessage = 'Could not select database: '.mysql_error();
return false;
}
return true;
}
/**
* Opens a connection to the database
*/
function connect() {
$this->dbLink = mysql_connect($this->dbServer,
$this->dbUser,
$this->dbPassword);
if (!$this->dbLink) {
$this->errorState = true;
$this->errorMessage = 'Could not connect: '.mysql_error();
return false;
}
if (!mysql_select_db($this->dbDatabase, $this->dbLink)) {
$this->errorState = true;
$this->errorMessage = 'Could not select database: '.mysql_error();
return false;
}
return true;
}
/**
* Queries the database for a specific SQL query
*/
function query($sql) {
if (!$this->result = mysql_query($sql, $this->dbLink)) {
$this->errorState = true;
$this->errorMessage = 'Query Error: '.mysql_error();
}
return $this->result;
}
/**
* Fetches a row from a result set
*/
function fetch(&$result) {
return mysql_fetch_array($result,MYSQL_ASSOC);
}
/**
* Returns an associative array of all rows
*/
function fetchAll(&$result) {
$resultArr = array();
if ($result) {
$resultArr = array();
while ($row = $this->fetch($result)) {
$resultArr[] = $row;
}
}
return $resultArr;
}
function freeResult(&$result) {
if ($result != true && $result != false) {
mysql_free_result($result);
}
unset($result);
}
function getLastError() {
return mysql_error();
}
function getNumRows(&$result) {
$numRows = mysql_num_rows($result);
return $numRows;
}
// return the error state
function getErrorState() {
return $this->errorState;
}
// set the error state
function setErrorState($errorState) {
$this->errorState = $errorState;
}
/**
* disconnect
*/
function close() {
mysql_close($this->dbLink);
}
}
?>

View File

@@ -0,0 +1,119 @@
<?php
$dbTable = array(
"CREATE TABLE `salesDetail` (
`salesDetail_ID` INT(11) NOT NULL auto_increment,
`region_ID` INT(11) NOT NULL default '0',
`product_ID` VARCHAR(255) NOT NULL,
`saledate` timestamp NOT NULL default '0000-00-00 00:00:00',
`amount` REAL NOT NULL default '0.0',
PRIMARY KEY (`salesDetail_ID`),
KEY `region_ID` (`region_ID`),
KEY `product_ID` (`product_ID`)
) ",
"CREATE TABLE `salesByMonth` (
`salesByMonth_ID` INT(11) NOT NULL auto_increment,
`region_ID` INT(11) NOT NULL default '0',
`product_ID` VARCHAR(255) NOT NULL,
`saleMonth` INT NOT NULL default '0',
`saleYear` INT NOT NULL default '0',
`amount` REAL NOT NULL default '0.0',
PRIMARY KEY (`salesByMonth_ID`),
KEY `region_ID` (`region_ID`),
KEY `product_ID` (`product_ID`)
) ",
"CREATE TABLE `product` (
`product_ID` INT(11) NOT NULL auto_increment,
`description` VARCHAR(255) NOT NULL,
`productCode` VARCHAR(255) NOT NULL,
PRIMARY KEY (`product_ID`)
) ",
"CREATE TABLE `region` (
`region_ID` INT(11) NOT NULL auto_increment,
`description` VARCHAR(255) NOT NULL,
PRIMARY KEY (`region_ID`)
) "
);
// Region Data
$dbRegionData = array(
"Europe",
"North America",
"South America",
"Asia",
"Australasia",
"Africa"
);
// Product Data
$dbProductData = array(
array("description"=>"Blue Robots" ,"productCode"=>"robo1"),
array("description"=>"Green Robots" ,"productCode"=>"robo2"),
array("description"=>"Red Robots" ,"productCode"=>"robo3"),
array("description"=>"Yellow Robots" ,"productCode"=>"robo4")
);
// Generate some sales data
$dbSalesDetailData = array();
$dbSalesByMonthData = array();
for ($productID=1;$productID<5;$productID++) {
for ($year=2007;$year<2009;$year++) {
for ($month=1;$month<13;$month++) {
for ($regionID=1;$regionID<7;$regionID++) {
$dbSalesByMonthData[$regionID][$productID][$month][$year] = 0.0;
}
}
}
}
for ($productID=1;$productID<5;$productID++) {
$productPrice = 10;
switch ($productID) {
case 1: $productPrice = 7; break;
case 2: $productPrice = 5; break;
case 3: $productPrice = 5; break;
case 4: $productPrice = 10; break;
case 5: $productPrice = 2; break;
default: $productPrice = 10; break;
}
for ($year=2007;$year<2009;$year++) {
for ($month=1;$month<13;$month++) {
for ($regionID=1;$regionID<7;$regionID++) {
$nsales = rand(1,4);
for ($i=0;$i<$nsales;$i++) {
$day = rand(1,28);
$datetime = mktime(10,30,15,$month,$day,$year);
$saleDate = date("Y-m-d H:i:s",$datetime);
$amount = $productPrice * rand(10000,90000) / 100;
$dbSalesDetailData[] = array("productID"=>$productID,
"regionID"=>$regionID,
"saleDate"=>$saleDate,
"amount"=>$amount
);
$dbSalesByMonthData[$regionID][$productID][$month][$year] += $amount;
}
}
}
}
}
?>

View File

@@ -0,0 +1,310 @@
<?php
$dbFiles = array(
"./dataQueries/dbConfig.php",
"./dataQueries/dataInterfaceScript.php",
"./dataQueries/homePagedbConfig.php",
"./dataQueries/salesByMonthDB.php",
"./dataQueries/salesByRegionDB.php",
"./graphConfig/salesByRegionConfig.php",
"./graphConfig/salesByRegionStackedConfig.php"
);
function testConnection(&$jpDatabase) {
$error["allOk"] = false;
$error["messages"] = "";
$dbLink = @mysql_connect($jpDatabase["dbServer"],
$jpDatabase["dbUser"],
$jpDatabase["dbPassword"]);
if (!$dbLink) {
$error["messages"] .= "Error: Unable to connect to the database server ! <br>\n";
$error["messages"] .= "Double check the connection details and try again. <br>\n";
$error["messages"] .= mysql_error()." <br>\n";
}
else {
$error["allOk"] = true;
mysql_close($dbLink);
}
return $error;
}
function performSetUp(&$jpDatabase) {
global $dbSalesDetailData;
global $dbSalesByMonthData;
global $dbProductData;
global $dbRegionData;
global $dbTable;
$error["allOk"] = false;
$error["messages"] = "";
// connect and create the database
$dbLink = @mysql_connect($jpDatabase["dbServer"],
$jpDatabase["dbUser"],
$jpDatabase["dbPassword"]);
if (!$dbLink) {
$error["messages"] .= "Error: Unable to connect to the database server ! <br>\n";
$error["messages"] .= "Double check the connection details and try again. <br>\n";
$error["messages"] .= mysql_error()." <br>\n";
return getPage("setUpError.html",$error);
}
$sql = "DROP DATABASE ".$jpDatabase["dbDatabase"]." ";
mysql_query($sql, $dbLink);
$sql = "CREATE DATABASE ".$jpDatabase["dbDatabase"]." ";
if (!$result = mysql_query($sql, $dbLink)) {
$error["messages"] .= "Error: Unable to create database ".$jpDatabase["dbDatabase"]." ! <br>\n";
$error["messages"] .= mysql_error()." <br>\n";
mysql_close($dbLink);
return getPage("setUpError.html",$error);
}
// create and set up the JPSAMPLEsalesDB ready for use by the
// sample application
$dbObj = new Database($jpDatabase["dbServer"],
$jpDatabase["dbUser"],
$jpDatabase["dbPassword"],
$jpDatabase["dbDatabase"]);
if (!$dbObj) {
$error["messages"] .= "Error: Unable to connect to the database server ! <br>\n";
$error["messages"] .= "Double check the connection details and try again. <br>\n";
$error["messages"] .= mysql_error()." <br>\n";
mysql_close($dbLink);
return getPage("setUpError.html",$error);
}
// create the database tables
foreach ($dbTable as $sql) {
$result = $dbObj->query($sql);
if (!$result) {
$error["messages"] .= "Error: Unable to create database table ! <br>\n";
$error["messages"] .= $sql." <br>\n";
$error["messages"] .= $dbObj->getLastError()." <br>\n";
$dbObj->close();
return getPage("setUpError.html",$error);
}
}
// Populate Region Data
foreach ($dbRegionData as $region) {
$sql = "INSERT INTO region (region_ID,description)
VALUES (0,'".$region."') ";
$result = $dbObj->query($sql);
if (!$result) {
$error["messages"] .= "Error: Unable to Insert Region Data ! <br>\n";
$error["messages"] .= $sql." <br>\n";
$error["messages"] .= $dbObj->getLastError()." <br>\n";
$dbObj->close();
return getPage("setUpError.html",$error);
}
}
// Populate Product Data
foreach ($dbProductData as $product) {
$sql = "INSERT INTO product (product_ID,description,productCode)
VALUES (0,'".$product["description"]."','".$product["productCode"]."') ";
$result = $dbObj->query($sql);
if (!$result) {
$error["messages"] .= "Error: Unable to Insert Product Data ! <br>\n";
$error["messages"] .= $sql." <br>\n";
$error["messages"] .= $dbObj->getLastError()." <br>\n";
$dbObj->close();
return getPage("setUpError.html",$error);
}
}
// populate the sales detail table
foreach ($dbSalesDetailData as $salesDetail) {
$sql = "INSERT INTO salesDetail (salesDetail_ID,region_ID,product_ID,saledate,amount)
VALUES (0,".$salesDetail["regionID"].",'".$salesDetail["productID"]."','".$salesDetail["saleDate"]."',".$salesDetail["amount"].") ";
$result = $dbObj->query($sql);
if (!$result) {
$error["messages"] .= "Error: Unable to Insert Sales Detail Data ! <br>\n";
$error["messages"] .= $sql." <br>\n";
$error["messages"] .= $dbObj->getLastError()." <br>\n";
$dbObj->close();
return getPage("setUpError.html",$error);
}
}
// Populate Sales By Month Table
for ($productID=1;$productID<5;$productID++) {
for ($year=2007;$year<2009;$year++) {
for ($month=1;$month<13;$month++) {
for ($regionID=1;$regionID<7;$regionID++) {
$sql = "INSERT INTO salesByMonth (salesByMonth_ID,region_ID,product_ID,saleMonth,saleYear,amount)
VALUES (0,".$regionID.",'".$productID."',".$month.",".$year.",".$dbSalesByMonthData[$regionID][$productID][$month][$year].") ";
$result = $dbObj->query($sql);
if (!$result) {
$error["messages"] .= "Error: Unable to Insert Sales Detail Data ! <br>\n";
$error["messages"] .= $sql." <br>\n";
$error["messages"] .= $dbObj->getLastError()." <br>\n";
$dbObj->close();
return getPage("setUpError.html",$error);
}
}
}
}
}
$error["allOk"] = true;
$error["messages"] = "";
$dbObj->close();
$error = updateDBinfo($jpDatabase);
if (!$error["allOk"]) {
return getPage("setUpError.html",$error);
}
else {
// update the flag in index.php
$output = get_File_As_String("index.php");
$search = array("setupDone = false;", "setupDone = FALSE;");
$replace = array("setupDone = TRUE;", "setupDone = TRUE;");
$output = str_replace($search,$replace,$output);
put_File_Content("index.php",$output);
return getPage("setUpComplete.html",$error);
}
}
function getPage($pageFile,$error) {
$output = get_File_As_String("./pageTemplates/".$pageFile);
$search = array("[ERRORS]");
$replace = array($error["messages"]);
$output = str_replace($search,$replace,$output);
return $output;
}
function updateDBinfo(&$jpDatabase) {
global $dbFiles;
$error["allOk"] = true;
$error["messages"] = "";
foreach ($dbFiles as $dbFile) {
$output = get_File_As_String($dbFile);
$search = array("[DBSERVER]","[DBUSER]","[DBPASSWORD]","[DBDATABASE]");
$replace = array($jpDatabase["dbServer"],$jpDatabase["dbUser"],$jpDatabase["dbPassword"],$jpDatabase["dbDatabase"]);
$output = str_replace($search,$replace,$output);
if (!put_File_Content($dbFile,$output)) {
$error["messages"] .= "Error: Unable to update database access information in the data files ! <br>\n";
$error["messages"] .= "Double check file permission on the ./dataQueries/ directory and all files in this directory. <br>\n";
$error["allOk"] = false;
}
}
return $error;
}
function doSetUp() {
$jpDatabase["dbServer"] = "localhost:3306";
$jpDatabase["dbUser"] = "";
$jpDatabase["dbPassword"] = "";
$jpDatabase["dbDatabase"] = "JPSAMPLEsalesDB";
$valuesEntered = false;
if (isset($_REQUEST["dbServer"])) {$jpDatabase["dbServer"] = $_REQUEST["dbServer"];$valuesEntered = true;}
if (isset($_REQUEST["dbUser"])) {$jpDatabase["dbUser"] = $_REQUEST["dbUser"];$valuesEntered = true;}
if (isset($_REQUEST["dbPassword"])) {$jpDatabase["dbPassword"] = $_REQUEST["dbPassword"];$valuesEntered = true;}
// test the db connection
$valuesOK = false;
$error["allOk"] = false;
$error["messages"] = "";
if ($valuesEntered) {
$error = testConnection($jpDatabase);
if ($error["allOk"]) {
$valuesOK = true;
}
}
if ($valuesOK) {
$output = performSetUp($jpDatabase);
print $output;
exit(0);
}
else {
// return the set up page
$output = get_File_As_String("./pageTemplates/setup.html");
$search = array("[DBSERVER]","[DBUSER]","[DBPASSWORD]","[ERRORS]");
$replace = array($jpDatabase["dbServer"],$jpDatabase["dbUser"],$jpDatabase["dbPassword"],$error["messages"]);
$output = str_replace($search,$replace,$output);
print $output;
exit(0);
}
return true;
}
function get_File_As_String($filename) {
$contents = "";
$lines = file($filename);
$contents = implode("\n",$lines);
return $contents;
}
function put_File_Content($filename,$contents) {
if (is_array($contents)) {
$contents = implode("\n",$contents);
}
if (!$handle = fopen($filename, "w+")) {
return false;
}
fwrite($handle, $contents);
fclose($handle);
return true;
}
?>