<?php
/*
Copyright 2001 Rikard Bartholf <rikard@bartholf.nu>
This file is part of ecSQL.
ecSQL is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
ecSQL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ecSQL; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
?>
<?php
/**
* This class contains all logic and intelligence needed for global types
* management. Nothing needs to be altered here. All new types are added as
* separate classes.
*
* @author Rikard Bartholf <rikard@bartholf.nu>
* @version 1.0
*/
class typesMain {
/**
* Inits the metadata of the class we are working with. Creates an empty
* type and does set that to the class default.
*/
function init() {
$this->cObj = $this->getProps();
}
/**
* Insert the data of a type to a mySQL table. Please note that the class
* first must be filled with essential data with the ->add() method.
*
* @return The ID of the newly created type.
* Example:
* You have a class that is called "User". Upon successful
* creation of a "User" the class will return the value of
* [USER_ID]. Checking this value directly after a ->create()
* can tell if the operation was successfull or not.
*/
function create() {
$aColl = $this->getProps();
$aQuery = "INSERT INTO " . $this->cClassName . "S (";
while(list($key,$val) = each($aColl)) {
if ($key != $this->cClassName . "_ID") {
$aQuery .= $key . ",";
}
}
/**
* Strip every occurence of "," at the end of string.
*/
$aQuery = preg_replace("/,*$/", "", $aQuery);
$aQuery .= ")";
$aColl = $this->getProps();
$aQuery .= " VALUES (";
while(list($key,$val) = each($aColl)) {
if ($key != $this->cClassName . "_ID") {
$aQuery .= "'" . $this->cObj[$key] . "',";
}
}
$aQuery = preg_replace("/',$/","')", $aQuery);
mysql_query($aQuery);
$aID = mysql_insert_id();
if ($aID) {
$this->add($this->cClassName . "_ID", $aID);
}
return $this->getValue($this->cClassName . "_ID");
}
/**
* Removes a type from a mySQL table.
*
* @return Status of the SQL operation. Really doesn't say much since
* it doesn't count affected rows yet.
*/
function remove() {
$aQuery = "DELETE FROM " . $this->cClassName . "S WHERE " . $this->cClassName . "_ID = " . $this->cObj[$this->cClassName . "_ID"];
return mysql_query($aQuery);
/*
* Some debug code. No need to do anything here at the moment.
*/ //if ( $DEBUG ) alert($aQuery);
}
/**
* Stores (i.e. updates) an already existing class in a mySQL table after
* appropriate values has been added with the ->add() method of this class.
*
* @return The status of the SQL operation.
*/
function store() {
$aColl = $this->getProps();
$aQuery = "UPDATE " . $this->cClassName . "S SET ";
while(list($key,$val) = each($aColl)) {
if ($key != $this->cClassName . "_ID") {
$aQuery .= $key . "='" . $this->cObj[$key] . "',";
}
}
/**
* Strip every occurence of "," at the end of string.
*/
$aQuery = preg_replace("/,*$/", "", $aQuery);
$aQuery .= " WHERE " . $this->cClassName . "_ID='" . $this->cObj[$this->cClassName . "_ID"] . "'";
return mysql_query($aQuery);
}
/**
* @param $inKey The "key" that is to be retrieved.
* @return The value of given key of the Class.
*/
function getValue($inKey) {
return $this->cObj[$inKey];
}
/**
* Adds a value for the given key of a class.
*
* @param $inKey The key that is to be set. Please that this can be used
* for temporary storing any variable in the type even
* though it isn't a type member. This is often useful in
* "creating on the fly" situations such as HTML
* presentations. Ofcourse, a key that isn't a memeber of
* the type will not be stored or, for that sake, make any
* trouble when storing or creating the class since those
* methods depends of the metadata of the table in
* question.
*
*/
function add($inKey, $inVal) {
$this->cObj[$inKey] = $inVal;
}
/**
* Returns an associative array of all the meta data in the affected
* table for a type. Consider this function as private.
*
* @return An array.
*
*/
function getProps() {
$aQuery = " SELECT *
FROM " . $this->cClassName . "S " .
"WHERE " . $this->cClassName . "_ID = -1";
$aRS = mysql_query($aQuery)
or die ("Syntax error");
$i = 0;
while($i < mysql_num_fields($aRS)) {
$aCols = mysql_fetch_field($aRS);
$aArray[$aCols->name] = '';
$i++;
}
mysql_free_result($aRS);
return $aArray;
}
}
/**
* Here is where all new types are created. Please see the text above the two classes
* below for further information.
*/
/**
* Copy, paste and rename this function as many times as you like to
* create new types.
*
* Example:
* A class called "Recipe" is to be created. Thus, a table named "RECIPES" must be created
* first. The table "RECIPES" must have one field that is called "RECIPE_ID".
* "RECIPE_ID" must be an identity column with optional autoincrement i.e for the class
* to behave as it should it must at least have unique values in every field of this
* column.
* That is the only criteries. What the rest of the fields is called is up to the user
* of the class.
*
* Please also note that all field names must be UPPERCASE. To distinguish words in
* a case like this use the underscore, "_", sign such as in "RECIPE_ID".
*
* @author Rikard Bartholf <rikard@bartholf.nu>
*/
class Recipe extends typesMain {
var $cObj;
var $cClassName;
function Recipe() {
$this->cClassName = strToUpper(get_class($this));
}
}
/**
* Copy, paste and rename this function as many times as you like to
* create new types.
*
* Example:
* A class called "Ingredient" is to be created. Thus, a table named "INGREDIENTS"
* must be created first. The table "INGREDIENTS" must have one field that is
* called "INGREDIENT_ID".
* "INGREDIENT_ID" must be an identity column with optional autoincrement i.e for
* the class to behave as it should it must at least have unique values in every
* field of this column.
* That is the only criteries. What the rest of the fields is called is up to the user
* of the class.
*
* Please also note that all field names must be UPPERCASE. To distinguish words in
* a case like this use the underscore, "_", sign such as in "INGREDIENT_ID".
*
* @author Rikard Bartholf <rikard@bartholf.nu>
*/
class Ingredient extends typesMain {
var $cObj;
var $cClassName;
function Ingredient() {
$this->cClassName = strToUpper(get_class($this));
}
}
?> |