File: /data/www/webtemplate/wtv2/codedoc/wtCategory.php
<?php
/**
* WebTemplate Core
*
* @version 2.0
* @module WebTemplate Core
*/
/**
* The class representing Category Nodes. Used for product categories
*
* @class WTCategory
* @extends WTNode
*/
class WTCategory extends WTNode
{
public function getLinkedPage() {
$q = Array();
$q["Node Type"] = "Page";
// prob safe to have this for all sites
$q["Criteria"] = "`Linked Content ID` = {$this->m_guid} AND `Template` = 'Product Category'";
$q["Select"] = "wtNode.__guid";
$q["Limit"] = 1;
$pageGuid = $GLOBALS["WT"]->query($q, "singleValueCallback");
if($pageGuid) {
return $GLOBALS["WT"]->getNode($pageGuid);
}
return null;
}
public static function &create($parentGuid, $nodeType, $attributes)
{
$categoryNode = WTNode::create($parentGuid, $nodeType, $attributes);
// do we need to create a page for the category
$productsPages = $GLOBALS["WT"]->getNode("/Pages/Products");
if($productsPages) {
$products = $GLOBALS["WT"]->getNode("/Products");
$pageParent = null;
if($parentGuid == $products->m_guid) {
$pageParent = $productsPages;
} else {
$parentCategory = $GLOBALS["WT"]->getNode($parentGuid);
$pageParent = $parentCategory->getLinkedPage();
}
if($pageParent) {
$page = $pageParent->createChild("Page", Array("Menu Text" => $categoryName, "Template" => "Product Category", "Linked Content ID" => $categoryNode->m_guid ) );
$page->setURI("products");
$categoryHolder = $page->createChild("wtNode", Array("Node Name" => "Category"));
$GLOBALS["WT"]->linkNodes($categoryHolder, $categoryNode);
$categoryNode->setAttributes(Array("URI" => $page->getAttribute("URI")));
}
/* $path = $categoryNode->getPath("/", "", "", false);
$categoryName = $categoryNode->getName();
$pageParent = $GLOBALS["WT"]->getNode("/Pages$path");
$page = $pageParent->createChild("Page", Array("Menu Text" => $categoryName, "Template" => "Product Category"));
$page->setURI("products");
$categoryHolder = $page->createChild("wtNode", Array("Node Name" => "Category"));
$GLOBALS["WT"]->linkNodes($categoryHolder, $categoryNode);
$categoryNode->setAttributes(Array("URI" => $page->getAttribute("URI")));
*/
}
return $categoryNode;
}
function createLinkedPage() {
$productsPages = $GLOBALS["WT"]->getNode("/Pages/Products");
if($productsPages) {
$products = $GLOBALS["WT"]->getNode("/Products");
$pageParent = null;
if($this->m_parentGuid == $products->m_guid) {
$pageParent = $productsPages;
} else {
$parentCategory = $GLOBALS["WT"]->getNode($this->m_parentGuid);
if($parentCategory->m_typeName == "Category" || $parentCategory->m_typeName == "Product") {
$pageParent = $parentCategory->getLinkedPage();
} else {
return;
}
}
if($pageParent) {
$page = $pageParent->createChild("Page", Array("Menu Text" => $categoryName, "Template" => "Product Category", "Linked Content ID" => $this->m_guid ) );
$page->setURI("products");
$categoryHolder = $page->createChild("wtNode", Array("Node Name" => "Category"));
$GLOBALS["WT"]->linkNodes($categoryHolder, $this);
//$this->setAttributes(Array("URI" => $page->getAttribute("URI")));
$uri = $page->getAttribute("URI");
$sql = "UPDATE `Category` SET URI = '" . mysql_escape_string($uri) . "' WHERE __guid = {$this->m_guid}";
mysql_query($sql);
return $page;
}
}
return null;
}
function setAttributes($attributes) {
$sql = "SELECT __translationOfGuid FROM wtNode WHERE __guid = {$this->m_guid
}";
$query = mysql_query($sql);
if($row = mysql_fetch_array($query)) {
if($row[0] != 0) {
return parent::setAttributes($attributes);
}
}
$oldPath = $this->getPath();
$value = parent::setAttributes($attributes);
$products = $GLOBALS["WT"]->getNode("/Pages/Products");
if($products) {
$linkedPage = $this->getLinkedPage();
if(!$linkedPage) {
$linkedPage = $this->createLinkedPage();
}
$uri = "";
if($linkedPage) {
$linkedPage->setAttributes(Array("Menu Text" => $this->getName()));
$linkedPage->setURI("products");
$uri = $linkedPage->getAttribute("URI");
parent::setAttributes(Array("URI" => $linkedPage->getAttribute("URI")));
}
/*
$newPath = $this->getPath();
$page = $GLOBALS["WT"]->getNode("/Pages$oldPath");
if($page) {
$page->setAttributes(Array("Menu Text" => $this->getName()));
$page->setKeywords();
parent::setAttributes(Array("URI" => $page->getAttribute("URI")));
}
*/
// }
}
return $value;
}
function deleteNode() {
// $path = $this->getPath();
$linkedPage = $this->getLinkedPage();
parent::deleteNode();
if($linkedPage) {
$linkedPage->deleteNode();
}
// remove all the products from the category
$sql = "SELECT
__guid,
Categories,
`Categories IDs`
FROM
Product
WHERE
`Categories IDs` LIKE '%" . $this->m_guid . "%'";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)) {
$categoryIDs = explode(",", $row["Categories IDs"]);
$categories = explode(",", $row["Categories"]);
$categoryIDString = "";
$categoryString = "";
for($i = 0; $i < count($categoryIDs); $i++) {
if($categoryIDs[$i] != $this->m_guid) {
if($categoryIDString != "") {
$categoryIDString .= ",";
$categoryString .= ",";
}
$categoryIDString .= $categoryIDs[$i];
if($i < count($categories)) {
$categoryString .= $categories[$i];
}
}
}
$sql = "UPDATE Product SET
Categories = '" . $categoryString . "',
`Categories IDs` = '" . $categoryIDString . "'
WHERE
__guid = " . $row["__guid"];
mysql_query($sql);
if($categoryIDString == "") {
$product = $GLOBALS["WT"]->getNode($row["__guid"]);
if($product) {
$product->deleteNode();
}
}
}
/*
// delete the page associated with the category
$products = $GLOBALS["WT"]->getNode("/Pages/Products");
if($products) {
$page = $GLOBALS["WT"]->getNode("/Pages$path");
if($page) {
$page->deleteNode();
}
}
*/
}
}
?>