File: /data/www/webtemplate/wtv2/codedoc/wtAttribute.php
<?php
/**
* The class representing wtAttribute Nodes
*
* @class WTAttribute
* @extends WTNode
*/
class WTAttribute extends WTNode
{
/**
* Get the node type this attribute belongs to
*
* @method getParentType
* @returns {String} The name of the type this attribute belongs to
*/
function getParentType()
{
$typeTableName = "";
$sql = "SELECT
`Type Name`
FROM
wtNode, wtType
WHERE
wtNode.__guid = wtType.__guid
AND wtNode.__guid = {$this->m_parentGuid}
AND wtType.__active <> 0";
$query = mysql_query($sql);
if(!$query) {
print "ERROR IN $sql\n";
print mysql_error();
}
if($row = mysql_fetch_row($query)) {
$typeTableName = $row[0];
} else {
// parent isn't a wtType
return "";
}
return $typeTableName;
}
/**
* Create an attribute node, the parent must be a wtType Node<br/>
* This function is used internally, WTNode::createChild should be used instead to create nodes
*
* @method create
* @static
* @param {int} parentGuid The guid of the parent node - must be a wtType Node
* @param {String} nodeType The type of node to create - must be wtAttribute
* @param {Array} attributes An associative array of the attributes for the wtAttribute Node
* @returns {Object} Returns the new wtAttribute node object, or NULL if something went wrong
*/
public static function &create($parentGuid, $nodeType, $attributes)
{
$parentGuid = (int)$parentGuid;
if($attributes == NULL) {
return NULL;
}
if(!isset($attributes["Attribute Name"])) {
return NULL;
}
$attributeName = trim($attributes["Attribute Name"]);
if($attributeName == "") {
return NULL;
}
if($nodeType != "wtAttribute") {
return NULL;
}
$attributeType = "";
$attributeTypeID = 0;
if(array_key_exists("Attribute Type", $attributes)) {
$attributeType = $attributes["Attribute Type"];
$attributeTypeID = $GLOBALS["WT"]->getOptionID("wtAttributeTypes", $attributeType);
}
if(array_key_exists("Attribute Type ID", $attributes)) {
$attributeTypeID = $attributes["Attribute Type ID"];
$attributeType = $GLOBALS["WT"]->getOptionValue("wtAttributeTypes", $attributeTypeID);
}
$attributeSettings = Array();
parse_str($attributes["Attribute Settings"], $attributeSettings);
if($attributeTypeID == 0) {
return NULL;
}
$typeTableName = "";
$sql = "SELECT
`Type Name`
FROM
wtNode, wtType
WHERE
wtNode.__guid = wtType.__guid
AND wtNode.__guid = $parentGuid
AND wtType.__active <> 0";
$query = mysql_query($sql);
if(!$query) {
print "ERROR IN $sql\n";
print mysql_error();
}
if($row = mysql_fetch_row($query)) {
$typeTableName = $row[0];
} else {
// parent isn't a wtType
return NULL;
}
// make sure the attribute doesn't already exist
$sql = "SELECT
COUNT(wtAttribute.__guid)
FROM
wtAttribute, wtNode
WHERE
wtAttribute.__guid = wtNode.__guid
AND wtNode.__deleted = 0
AND typeGuid = $parentGuid
AND UPPER(`Attribute Name`) = '" . mysql_real_escape_string(strtoupper($attributeName)) . "'";
$query = mysql_query($sql);
print mysql_error();
$row = mysql_fetch_array($query);
if($row[0] > 0) {
return NULL;
}
$attributeNode = WTNode::create($parentGuid, "wtAttribute", $attributes);
if($attributeNode == NULL) {
return NULL;
}
$sql = "UPDATE
wtAttribute
SET
typeGuid = $parentGuid
WHERE
__guid = {$attributeNode->m_guid}";
if(!mysql_query($sql)) {
print mysql_error();
return NULL;
}
$sql = "ALTER TABLE
`$typeTableName`
ADD `$attributeName` ";
switch($attributeType) {
default:
$sql .= "TEXT";// NOT NULL DEFAULT ''";
break;
case "Single Choice Options":
case "Multiple Choice Options":
$sql .= " VARCHAR(255) NOT NULL DEFAULT '';";
mysql_query($sql);
if(array_key_exists("options", $attributeSettings)) {
$optionsParent = $GLOBALS["WT"]->getNode($attributeSettings["options"]);
if($optionsParent == NULL) {
$optionsParent = $GLOBALS["WT"]->getNode("/Config/Options/" . $attributeSettings["options"]);
}
/*
if($optionsParent != NULL) {
if($attributeType == "Single Choice Options") {
$sql = "INSERT INTO
tblSingleOptionUse (optionParentGuid, attributeGuid)
VALUES
({$optionsParent->m_guid}, {$attributeNode->m_guid})";
if(!mysql_query($sql)) {
print $sql;
print mysql_error();
}
}
} else {
print "OPTIONS DONT EXIST!! " . $attributeSettings["options"] . "\n";
}
*/
}
if($attributeType == "Single Choice Options") {
$sql = " ALTER TABLE `$typeTableName` ADD `$attributeName ID` INT NOT NULL DEFAULT 0;";
} else {
$sql = " ALTER TABLE `$typeTableName` ADD `$attributeName IDs` VARCHAR(255) NOT NULL DEFAULT ''";
}
break;
case "File":
$sql .= "VARCHAR(255) NOT NULL DEFAULT ''";
break;
case "Image":
case "Number":
$sql .= "INT NOT NULL DEFAULT 0";
break;
case "Date":
$sql .= "DATETIME NOT NULL";
break;
case "Money":
$sql .= "REAL(9,2) NOT NULL";
break;
}
if($attributeType != "Static") {
if(!mysql_query($sql)) {
print "$sql <br>";
print mysql_error();
// need to delete the attribute node
return NULL;
}
}
return $attributeNode;
}
/**
* Delete this wtAttribute
*
* @method deleteNode
*/
function deleteNode()
{
parent::deleteNode();
$typeTableName = $this->getParentType();
$attributeType = $this->getAttribute("Attribute Type");
if($attributeType != "Static") {
$sql = "ALTER TABLE
`$typeTableName`
DROP
`{$this->m_name}`";
if(!mysql_query($sql)) {
print $sql;
print mysql_error();
}
}
if($attributeType == "Single Choice Options") {
$sql = "ALTER TABLE
`$typeTableName`
DROP
`{$this->m_name} ID`";
print $sql;
if(!mysql_query($sql)) {
print $sql;
print mysql_error();
}
}
}
/**
* If this attribute has options (eg it's Single Choice Options, or Multiple Choice Options), return the options specified
*
* @method getOptions
* @returns {Array} An associative array of the options for this attribute
*/
function getOptions() {
$attributeSettings = Array();
parse_str($this->getAttribute("Attribute Settings"), $attributeSettings);
if(get_magic_quotes_gpc()) {
$attributeSettings = array_map("stripslashes", $attributeSettings);
}
$optionQuery = Array();
if(array_key_exists("optionCriteria", $attributeSettings)) {
$optionQuery["Criteria"] = $attributeSettings["optionCriteria"];
}
if(array_key_exists("optionType", $attributeSettings)) {
$optionQuery["Node Type"] = $attributeSettings["optionType"];
}
return $GLOBALS["WT"]->getOptions($attributeSettings["options"], $optionQuery);
}
/**
* Set the attributes for this node, alter the database table if necessary
*
* @method setAttributes
* @param {Array} attributes the attributes to set
*/
function setAttributes($attributes) {
$oldAttributeName = $this->getAttribute("Attribute Name");
$newAttributeName = $oldAttributeName;
$oldAttributeTypeID = $this->getAttribute("Attribute Type ID");
$oldAttributeType = $GLOBALS["WT"]->getOptionValue("wtAttributeTypes", $oldAttributeTypeID);
$newAttributeTypeID = $oldAttributeTypeID;
if(array_key_exists("Attribute Name", $attributes)) {
$newAttributeName = $attributes["Attribute Name"];
}
if(array_key_exists("Attribute Type", $attributes)) {
$newAttributeType = $attributes["Attribute Type"];
$newAttributeTypeID = $GLOBALS["WT"]->getOptionID("wtAttributeTypes", $attributes["Attribute Type"]);
}
if(array_key_exists("Attribute Type ID", $attributes)) {
$newAttributeTypeID = $attributes["Attribute Type ID"];
$newAttributeType = $GLOBALS["WT"]->getOptionValue("wtAttributeTypes", $newAttributeTypeID);
}
if(!parent::setAttributes($attributes)) {
return false;
}
// if this is true, the attribute in the mysql table has just been created, but the node's attributes
// haven't been set yet, so don't want to do any table alters..
if($oldAttributeName == '') { // && $oldAttributeType == 0) {
return true;
}
if($oldAttributeName != $newAttributeName || $oldAttributeTypeID != $newAttributeTypeID) {
$tableTypeName = "";
$sql = "SELECT
`Type Name`
FROM
wtType
WHERE
__active <> 0
AND __guid = {$this->m_parentGuid}";
$query = mysql_query($sql);
if($row = mysql_fetch_row($query)) {
$tableTypeName = $row[0];
} else {
return false;
}
// $tableTypeName = $this->getAttribute("Type Name");
//print "Table type name = $tableTypeName<br>";
if($oldAttributeTypeID != $newAttributeTypeID) {
if($oldAttributeType == 'Single Choice Options') {
$sql = "ALTER TABLE
`$tableTypeName`
DROP
`$oldAttributeName ID`";
if(!mysql_query($sql)) {
print $sql . "<br>";
print mysql_error();
}
} else if($oldAttributeType == 'Multiple Choice Options') {
$sql = "ALTER TABLE
`$tableTypeName`
DROP
`$oldAttributeName IDs`";
if(!mysql_query($sql)) {
print $sql . "<br>";
print mysql_error();
}
}
}
$idsql = "";
$sql = "ALTER TABLE
`$tableTypeName`
CHANGE
`$oldAttributeName`
`$newAttributeName` ";
$attributeTypeName = $GLOBALS["WTBUILTINOPTIONS"]["wtAttributeTypes"][$attributeType];
switch($newAttributeType) {
default:
$sql .= "TEXT";// NOT NULL DEFAULT ''";
break;
case "Single Choice Options":
// case "Multiple Choice Options":
$sql .= "VARCHAR(255) NOT NULL";
if($newAttributeName != $oldAttributeName && $oldAttributeType == $newAttributeType) {
$idsql = "ALTER TABLE `$tableTypeName` CHANGE `$oldAttributeName ID` `$newAttributeName ID` INT NOT NULL DEFAULT 0";
} else {
$idsql = "ALTER TABLE `$tableTypeName` ADD `$newAttributeName ID` INT NOT NULL DEFAULT 0";
}
break;
case "Multiple Choice Options":
$sql .= "VARCHAR(255) NOT NULL";
if($newAttributeName != $oldAttributeName && $oldAttributeType == $newAttributeType) {
$idsql = "ALTER TABLE `$tableTypeName` CHANGE `$oldAttributeName IDs` `$newAttributeName ID` VARCHAR(255) NOT NULL DEFAULT ''";
} else {
$idsql = "ALTER TABLE `$tableTypeName` ADD `$newAttributeName IDs` VARCHAR(255) NOT NULL DEFAULT ''";
}
break;
case "File":
$sql .= "VARCHAR(255) NOT NULL DEFAULT ''";
break;
case "Image":
case "Number":
$sql .= "INT NOT NULL DEFAULT 0";
break;
case "Date":
$sql .= "DATETIME NOT NULL";
break;
case "Money":
$sql .= "REAL(9,2) NOT NULL";
break;
}
if($newAttributeType != "Static") {
if(!mysql_query($sql)) {
print mysql_error();
}
}
if($idsql != "") {
if(!mysql_query($idsql)) {
print mysql_error();
}
}
}
return true;
}
}
//$GLOBALS["WT"]->registerNodeTypeClass("wtAttribute", "WTAttribute");
?>