Example #1
0
 public static function cloneTree($page, $parent_id)
 {
     /* This will hold new id of root of cloned tree. */
     static $new_root_id = false;
     /* Clone passed in page. */
     $clone = Record::findByIdFrom('Page', $page->id);
     $clone->parent_id = (int) $parent_id;
     $clone->id = null;
     $clone->title .= " (copy)";
     $clone->slug .= "-copy";
     $clone->save();
     /* Also clone the page parts. */
     $page_part = PagePart::findByPageId($page->id);
     if (count($page_part)) {
         foreach ($page_part as $part) {
             $part->page_id = $clone->id;
             $part->id = null;
             $part->save();
         }
     }
     /* This gets set only once even when called recursively. */
     if (!$new_root_id) {
         $new_root_id = $clone->id;
     }
     /* Clone and update childrens parent_id to clones new id. */
     if (Page::hasChildren($page->id)) {
         foreach (Page::childrenOf($page->id) as $child) {
             Page::cloneTree($child, $clone->id);
         }
     }
     return $new_root_id;
 }
Example #2
0
 /**
  * Action to return a list View of all first level children of a page.
  *
  * @todo improve phpdoc desc
  *
  * @param <type> $parent_id
  * @param <type> $level
  * @param <type> $return
  * @return View
  */
 function children($parent_id, $level, $return = false)
 {
     $expanded_rows = isset($_COOKIE['expanded_rows']) ? explode(',', $_COOKIE['expanded_rows']) : array();
     // get all children of the page (parent_id)
     $childrens = Page::childrenOf($parent_id);
     foreach ($childrens as $index => $child) {
         $childrens[$index]->has_children = Page::hasChildren($child->id);
         $childrens[$index]->is_expanded = in_array($child->id, $expanded_rows);
         if ($childrens[$index]->is_expanded) {
             $childrens[$index]->children_rows = $this->children($child->id, $level + 1, true);
         }
     }
     $content = new View('page/children', array('childrens' => $childrens, 'level' => $level + 1));
     if ($return) {
         return $content;
     }
     echo $content;
 }
 public static function cloneTree($page, $parent_id)
 {
     /* This will hold new id of root of cloned tree. */
     static $new_root_id = false;
     /* Clone passed in page. */
     $clone = Page::findById($page->id);
     $clone->parent_id = (int) $parent_id;
     $clone->id = null;
     if (!$new_root_id) {
         $clone->title .= " (copy)";
         $clone->slug .= "-copy";
     }
     $clone->save();
     /* Also clone the page parts. */
     $page_part = PagePart::findByPageId($page->id);
     if (count($page_part)) {
         foreach ($page_part as $part) {
             $part->page_id = $clone->id;
             $part->id = null;
             $part->save();
         }
     }
     /* Also clone the page tags. */
     $page_tags = $page->getTags();
     if (count($page_tags)) {
         foreach ($page_tags as $tag_id => $tag_name) {
             // create the relation between the page and the tag
             $tag = new PageTag(array('page_id' => $clone->id, 'tag_id' => $tag_id));
             $tag->save();
         }
     }
     /* This gets set only once even when called recursively. */
     if (!$new_root_id) {
         $new_root_id = $clone->id;
     }
     /* Clone and update childrens parent_id to clones new id. */
     if (Page::hasChildren($page->id)) {
         foreach (Page::childrenOf($page->id) as $child) {
             Page::cloneTree($child, $clone->id);
         }
     }
     return $new_root_id;
 }