/** * Ajax action to copy a page or page tree. * */ function copy() { $original_id = $_POST['originalid']; $page = Record::findByIdFrom('Page', $original_id); $new_root_id = Page::cloneTree($page, $page->parent_id); $page = Record::findByIdFrom('Page', $new_root_id); $page->position += 1; $page->created_on_time = time(); $page->published_on_time = $page->created_on_time; $page->save(); $newUrl = URL_PUBLIC; $newUrl .= USE_MOD_REWRITE == false ? '?' : ''; $newUrl .= $page->getUri(); $newUrl .= $page->getUri() != '' ? URL_SUFFIX : ''; $newData = array($new_root_id, get_url('page/edit/' . $new_root_id), $page->title(), $page->slug(), $newUrl, get_url('page/add', $new_root_id), get_url('page/delete/' . $new_root_id)); echo implode('||', $newData); }
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; }
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; }
/** * Ajax action to copy a page or page tree * */ function copy($parent_id) { parse_str($_POST['data']); $page = Record::findByIdFrom('Page', $dragged_id); $new_root_id = Page::cloneTree($page, $parent_id); foreach ($pages as $position => $page_id) { if ($page_id == $dragged_id) { /* Move the cloned tree, not original. */ $page = Record::findByIdFrom('Page', $new_root_id); } else { $page = Record::findByIdFrom('Page', $page_id); } $page->position = (int) $position; $page->parent_id = (int) $parent_id; $page->save(); } }
/** * Ajax action to copy a page or page tree. * */ function copy() { $original_id = $_POST['originalid']; $page = Record::findByIdFrom('Page', $original_id); $new_root_id = Page::cloneTree($page, $page->parent_id); $page = Record::findByIdFrom('Page', $new_root_id); $page->position += 1; $page->save(); $newUrl = URL_PUBLIC; $newUrl .= USE_MOD_REWRITE == false ? '?' : ''; $newUrl .= $page->path(); $newUrl .= $page->path() != '' ? URL_SUFFIX : ''; $newData = array($new_root_id, get_url('page/edit/' . $new_root_id), $page->title(), $page->slug(), $newUrl, get_url('page/add', $new_root_id), get_url('page/delete/' . $new_root_id . '?csrf_token=' . SecureToken::generateToken(BASE_URL . 'page/delete/' . $new_root_id))); echo implode('||', $newData); }