/** * copy an object to $pid or over $targetId * * better way to copy an object over another one is to delete the target, * but this could be very dangerous. We could delete required/important data * so i suggest to just mark overwriten object with dstatus = 3. * But in this situation appears another problem with child nodes. * Childs should be moved to new parent. * * @param int $pid if not specified then will be set to pid of targetId * @param int $targetId * @return int the id of copied object */ public function copyTo($pid = false, $targetId = false) { // check input params if (!is_numeric($this->id) || !is_numeric($pid) && !is_numeric($targetId)) { return false; } /* security check */ if (!\CB\Security::canRead($this->id)) { return false; } /* end of security check */ if (is_numeric($targetId)) { /* target security check */ if (!\CB\Security::canWrite($targetId)) { return false; } /* end of target security check */ // marking overwriten object with dstatus = 3 DB\dbQuery('UPDATE tree SET updated = 1 ,dstatus = 3 ,did = $2 WHERE id = $1', array($targetId, $_SESSION['user']['id'])) or die(DB\dbQueryError()); //get pid from target if not specified $res = DB\dbQuery('SELECT pid FROM tree WHERE id = $1', $targetId) or die(DB\dbQueryError()); if ($r = $res->fetch_assoc()) { $pid = $r['pid']; } $res->close(); } else { /* pid security check */ if (!\CB\Security::canWrite($pid)) { return false; } /* end of pid security check */ } /* check again if we have pid set It can be unset when not existent $targetId is specified */ if (!is_numeric($pid)) { return false; } // copying the object to $pid DB\dbQuery('INSERT INTO `tree` (`id` ,`pid` ,`user_id` ,`system` ,`type` ,`template_id` ,`tag_id` ,`target_id` ,`name` ,`date` ,`date_end` ,`size` ,`is_main` ,`cfg` ,`inherit_acl` ,`cid` ,`cdate` ,`uid` ,`udate` ,`updated` ,`oid` ,`did` ,`ddate` ,`dstatus`) SELECT NULL ,$2 ,`user_id` ,`system` ,`type` ,`template_id` ,`tag_id` ,`target_id` ,`name` ,`date` ,`date_end` ,`size` ,`is_main` ,`cfg` ,`inherit_acl` ,`cid` ,`cdate` ,$3 ,CURRENT_TIMESTAMP ,1 ,`oid` ,`did` ,`ddate` ,`dstatus` FROM `tree` t WHERE id = $1', array($this->id, $pid, $_SESSION['user']['id'])) or die(DB\dbQueryError()); $objectId = DB\dbLastInsertId(); /* we have now object created, so we start copy all its possible data: - tree_info is filled automaticly by trigger - custom security rules from tree_acl - custom object data */ // copy node custom security rules if set \CB\Security::copyNodeAcl($this->id, $objectId); $this->copyCustomDataTo($objectId); // move childs from overwriten targetId (which has been marked with dstatus = 3) // to newly copied object if (is_numeric($targetId)) { DB\dbQuery('UPDATE tree SET updated = 1 ,pid = $2 WHERE pid = $1 AND dstatus = 0', array($targetId, $this->id)) or die(DB\dbQueryError()); } return $objectId; }
/** * copy an object to $pid or over $targetId * * better way to copy an object over another one is to delete the target, * but this could be very dangerous. We could delete required/important data * so i suggest to just mark overwriten object with dstatus = 3. * But in this situation appears another problem with child nodes. * Childs should be moved to new parent. * * @param int $pid if not specified then will be set to pid of targetId * @param int $targetId * @return int the id of copied object */ public function copyTo($pid = false, $targetId = false) { // check input params if (!is_numeric($this->id) || !is_numeric($pid) && !is_numeric($targetId)) { return false; } /* security check */ if (!\CB\Security::canRead($this->id)) { return false; } /* end of security check */ if (is_numeric($targetId)) { /* target security check */ if (!\CB\Security::canWrite($targetId)) { return false; } /* end of target security check */ // marking overwriten object with dstatus = 3 DM\Tree::update(array('id' => $targetId, 'updated' => 1, 'dstatus' => 3, 'did' => User::getId())); $r = DM\Tree::read($targetId); if (!empty($r)) { $pid = $r['pid']; } } else { /* pid security check */ if (!\CB\Security::canWrite($pid)) { return false; } /* end of pid security check */ } /* check again if we have pid set It can be unset when not existent $targetId is specified */ if (!is_numeric($pid)) { return false; } // copying the object to $pid $objectId = DM\Tree::copy($this->id, $pid); /* we have now object created, so we start copy all its possible data: - tree_info is filled automaticly by trigger - custom security rules from tree_acl - custom object data */ // copy node custom security rules if set \CB\Security::copyNodeAcl($this->id, $objectId); $this->copyCustomDataTo($objectId); // move childs from overwriten targetId (which has been marked with dstatus = 3) // to newly copied object if (is_numeric($targetId)) { // DM\Tree::update( // ) DM\Tree::moveActiveChildren($targetId, $this->id); } return $objectId; }