/**
  * Paste item (tree) from clipboard to current lm
  */
 static function pasteTree($a_target_lm, $a_item_id, $a_parent_id, $a_target, $a_insert_time, &$a_copied_nodes, $a_as_copy = false, $a_source_lm = null)
 {
     global $ilUser, $ilias, $ilLog;
     include_once "./Modules/LearningModule/classes/class.ilStructureObject.php";
     include_once "./Modules/LearningModule/classes/class.ilLMPageObject.php";
     $item_lm_id = ilLMObject::_lookupContObjID($a_item_id);
     $item_type = ilLMObject::_lookupType($a_item_id);
     $lm_obj = $ilias->obj_factory->getInstanceByObjId($item_lm_id);
     if ($item_type == "st") {
         $item = new ilStructureObject($lm_obj, $a_item_id);
     } else {
         if ($item_type == "pg") {
             $item = new ilLMPageObject($lm_obj, $a_item_id);
         }
     }
     $ilLog->write("Getting from clipboard type " . $item_type . ", " . "Item ID: " . $a_item_id . ", of original LM: " . $item_lm_id);
     if ($item_lm_id != $a_target_lm->getId() && !$a_as_copy) {
         // @todo: check whether st is NOT in tree
         // "move" metadata to new lm
         include_once "Services/MetaData/classes/class.ilMD.php";
         $md = new ilMD($item_lm_id, $item->getId(), $item->getType());
         $new_md = $md->cloneMD($a_target_lm->getId(), $item->getId(), $item->getType());
         // update lm object
         $item->setLMId($a_target_lm->getId());
         $item->setContentObject($a_target_lm);
         $item->update();
         // delete old meta data set
         $md->deleteAll();
         if ($item_type == "pg") {
             $page = $item->getPageObject();
             $page->buildDom();
             $page->setParentId($a_target_lm->getId());
             $page->update();
         }
     }
     if ($a_as_copy) {
         $target_item = $item->copy($a_target_lm);
         $a_copied_nodes[$item->getId()] = $target_item->getId();
     } else {
         $target_item = $item;
     }
     $ilLog->write("Putting into tree type " . $target_item->getType() . "Item ID: " . $target_item->getId() . ", Parent: " . $a_parent_id . ", " . "Target: " . $a_target . ", Item LM:" . $target_item->getContentObject()->getId());
     ilLMObject::putInTree($target_item, $a_parent_id, $a_target);
     if ($a_source_lm == null) {
         $childs = $ilUser->getClipboardChilds($item->getId(), $a_insert_time);
     } else {
         $childs = $a_source_lm->lm_tree->getChilds($item->getId());
         foreach ($childs as $k => $child) {
             $childs[$k]["id"] = $childs[$k]["child"];
         }
     }
     foreach ($childs as $child) {
         ilLMObject::pasteTree($a_target_lm, $child["id"], $target_item->getId(), IL_LAST_NODE, $a_insert_time, $a_copied_nodes, $a_as_copy, $a_source_lm);
     }
     return $target_item->getId();
     // @todo: write history (see pastePage)
 }
 /**
  * split page at hierarchical id
  *
  * the main reason for this method being static is that a lm page
  * object is not available within ilPageContentGUI where this method
  * is called
  */
 function _splitPage($a_page_id, $a_pg_parent_type, $a_hier_id)
 {
     // get content object (learning module / digilib book)
     $lm_id = ilLMObject::_lookupContObjID($a_page_id);
     $type = ilObject::_lookupType($lm_id, false);
     switch ($type) {
         case "lm":
             include_once "./Modules/LearningModule/classes/class.ilObjLearningModule.php";
             $cont_obj = new ilObjLearningModule($lm_id, false);
             break;
         case "dbk":
             include_once "./Modules/LearningModule/classes/class.ilObjDlBook.php";
             $cont_obj = new ilObjDlBook($lm_id, false);
             break;
     }
     $source_lm_page = new ilLMPageObject($cont_obj, $a_page_id);
     // create new page
     $lm_page = new ilLMPageObject($cont_obj);
     $lm_page->setTitle($source_lm_page->getTitle());
     $lm_page->setLMId($source_lm_page->getLMId());
     $lm_page->setType($source_lm_page->getType());
     $lm_page->setDescription($source_lm_page->getDescription());
     $lm_page->create(true);
     // copy complete content of source page to new page
     $source_page = $source_lm_page->getPageObject();
     $page = $lm_page->getPageObject();
     $page->setXMLContent($source_page->copyXMLContent());
     //echo htmlentities($source_page->copyXMLContent());
     $page->buildDom(true);
     $page->update();
     //		echo "-".$page->getId()."-".$page->getParentType()."-";
     // copy meta data
     include_once "Services/MetaData/classes/class.ilMD.php";
     $md = new ilMD($source_lm_page->getLMId(), $a_page_id, $source_lm_page->getType());
     $md->cloneMD($source_lm_page->getLMId(), $lm_page->getId(), $source_lm_page->getType());
     // insert new page in tree (after original page)
     $tree = new ilTree($cont_obj->getId());
     $tree->setTableNames('lm_tree', 'lm_data');
     $tree->setTreeTablePK("lm_id");
     if ($tree->isInTree($source_lm_page->getId())) {
         $parent_node = $tree->getParentNodeData($source_lm_page->getId());
         $tree->insertNode($lm_page->getId(), $parent_node["child"], $source_lm_page->getId());
     }
     // remove all nodes < hierarchical id from new page (incl. update)
     $page->addHierIds();
     $page->deleteContentBeforeHierId($a_hier_id);
     //		$page->update();
     // remove all nodes >= hierarchical id from source page
     $source_page->buildDom();
     $source_page->addHierIds();
     $source_page->deleteContentFromHierId($a_hier_id);
     return $lm_page;
 }