метод переноса ветки(смена родителя).
Код:
function updateNode($categoryID, $newCategoryParentID, $nodeInfoData)
{
$db = new MySql(null,"mport4",null,null);
$db->MySql_Connect();
$db->MySql_SelectDb();
$updateQuery = "update categories
set cat_TITLE='" . $nodeInfoData["title"] . "',
cat_MODIFY_DATE=" . time() . ",
cat_DESC='" . $nodeInfoData["desc"] . "',
cat_PHOTO_PATH='" .$nodeInfoData["photo"] . "'
where cat_ID = " . $categoryID;
$db->MySql_QueryDb($updateQuery);
if (mysql_affected_rows() >= 0)
{
//update tree hierarchy
if ($categoryID == $newCategoryParentID)
{
//no need update
return 0;
}
//get nodeInfo
if ($this->getNodeInfo($categoryID) != 0)
{
return -1;
}
//get newParentNodeInfo
$categoryObj = new Category2();
if ($categoryObj->getNodeInfo($newCategoryParentID) != null)
{
return -1;
}
//checks
//get parentcat for node
$parentCategoryObj = new Category2();
if ($parentCategoryObj->getParent($this->nodeInfo["id"]) != 0)
{
return -1;
}
//if parentcat = "newparentcat"
if ($newCategoryParentID == $parentCategoryObj->nodeInfo["id"])
{
//no need update
return 0;
}
// whether it is being moved upwards along the path
if ($categoryObj->nodeInfo["left"] < $this->nodeInfo["left"] && $categoryObj->nodeInfo["right"] > $this->nodeInfo["right"] && $categoryObj->nodeInfo["level"] < $this->nodeInfo["level"] - 1 )
{
$updateTreeQuery = 'UPDATE categories SET
cat_LEVEL=IF(cat_LEFT BETWEEN '. $this->nodeInfo["left"] .' AND '.$this->nodeInfo["right"].', cat_LEVEL' . sprintf('%+d', -($this->nodeInfo["level"]-1)+$categoryObj->nodeInfo["level"]).', cat_LEVEL), '
. 'cat_RIGHT=IF(cat_RIGHT BETWEEN '.($this->nodeInfo["right"]+1).' AND '.($categoryObj->nodeInfo["right"]-1).', cat_RIGHT-'.($this->nodeInfo["right"]-$this->nodeInfo["left"]+1).', '
.'IF(cat_LEFT BETWEEN '.($this->nodeInfo["left"]).' AND '.($this->nodeInfo["right"]).', cat_RIGHT+'.((($categoryObj->nodeInfo["right"]-$this->nodeInfo["right"]-$this->nodeInfo["level"]+$categoryObj->nodeInfo["level"])/2)*2 + $this->nodeInfo["level"] - $categoryObj->nodeInfo["level"] - 1).', cat_RIGHT)), '
. 'cat_LEFT=IF(cat_LEFT BETWEEN '.($this->nodeInfo["right"]+1).' AND '.($categoryObj->nodeInfo["right"]-1).', cat_LEFT-'.($this->nodeInfo["right"]-$this->nodeInfo["left"]+1).', '
.'IF(cat_LEFT BETWEEN '.$this->nodeInfo["left"].' AND '.($this->nodeInfo["right"]).', cat_LEFT+'.((($categoryObj->nodeInfo["right"]-$this->nodeInfo["right"]-$this->nodeInfo["level"]+$categoryObj->nodeInfo["level"])/2)*2 + $this->nodeInfo["level"] - $categoryObj->nodeInfo["level"] - 1).', cat_LEFT)) '
. 'WHERE cat_LEFT BETWEEN '.($categoryObj->nodeInfo["left"]+1).' AND '.($categoryObj->nodeInfo["right"]-1);
}
elseif($categoryObj->nodeInfo["left"] < $this->nodeInfo["left"])
{
$updateTreeQuery = 'UPDATE categories SET '
. 'cat_LEVEL=IF(cat_LEFT BETWEEN '. $this->nodeInfo["left"].' AND '.$this->nodeInfo["right"].', cat_LEVEL'.sprintf('%+d', -((int)$this->nodeInfo["level"]-1)+(int)$categoryObj->nodeInfo["level"]).', cat_LEVEL), '
. 'cat_LEFT=IF(cat_LEFT BETWEEN '.$categoryObj->nodeInfo["right"].' AND '.($this->nodeInfo["left"]-1).', cat_LEFT+'.((int)$this->nodeInfo["right"]-(int)$this->nodeInfo["left"]+1).', '
. 'IF(cat_LEFT BETWEEN '. $this->nodeInfo["left"].' AND '.$this->nodeInfo["right"].', cat_LEFT-'.( $this->nodeInfo["left"]-$categoryObj->nodeInfo["right"]).', cat_LEFT) '
. '), '
. 'cat_RIGHT=IF(cat_RIGHT BETWEEN '.$categoryObj->nodeInfo["right"].' AND '.$this->nodeInfo["left"].', cat_RIGHT+'.($this->nodeInfo["right"]-$this->nodeInfo["left"]+1).', '
. 'IF(cat_RIGHT BETWEEN '.$this->nodeInfo["left"].' AND '.$this->nodeInfo["right"].', cat_RIGHT-'.((int)$this->nodeInfo["left"]-(int)$categoryObj->nodeInfo["right"]).', cat_RIGHT) '
. ') '
. 'WHERE cat_LEFT BETWEEN '.$categoryObj->nodeInfo["left"].' AND '.$this->nodeInfo["right"]
// !!! added this line (Maxim Matyukhin)
.' OR cat_RIGHT BETWEEN '.$categoryObj->nodeInfo["left"].' AND '.$this->nodeInfo["right"];
}
else
{
$updateTreeQuery = 'UPDATE categories SET '
. 'cat_LEVEL=IF(cat_LEFT BETWEEN '.$this->nodeInfo["left"].' AND '.$this->nodeInfo["right"].', cat_LEVEL'.sprintf('%+d', -($this->nodeInfo["level"]-1)+$categoryObj->nodeInfo["level"]).', cat_LEVEL), '
. 'cat_LEFT=IF(cat_LEFT BETWEEN '.$this->nodeInfo["right"].' AND '.$categoryObj->nodeInfo["right"].', cat_LEFT-'.($this->nodeInfo["right"]-$this->nodeInfo["left"]+1).', '
. 'IF(cat_LEFT BETWEEN '.$this->nodeInfo["left"].' AND '.$this->nodeInfo["right"].', cat_LEFT+'.($categoryObj->nodeInfo["right"]-1-$this->nodeInfo["right"]).', cat_LEFT)'
. '), '
. 'cat_RIGHT=IF(cat_RIGHT BETWEEN '.($this->nodeInfo["right"]+1).' AND '.($categoryObj->nodeInfo["right"]-1).', cat_RIGHT-'.($this->nodeInfo["right"]-$this->nodeInfo["left"]+1).', '
. 'IF(cat_RIGHT BETWEEN '.$this->nodeInfo["left"].' AND '.$this->nodeInfo["right"].', cat_RIGHT+'.($categoryObj->nodeInfo["right"]-1-$this->nodeInfo["right"]).', cat_RIGHT) '
. ') '
. 'WHERE cat_LEFT BETWEEN '.$this->nodeInfo["left"].' AND '.$categoryObj->nodeInfo["right"]
// !!! added this line (Maxim Matyukhin)
. ' OR cat_RIGHT BETWEEN '.$this->nodeInfo["left"].' AND '.$categoryObj->nodeInfo["right"]
;
}
$db->MySql_QueryDb($updateTreeQuery);
if (mysql_affected_rows() > 0)
{
return 0;
}
}
return -1;
}