Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // copy a subtree from one parent to another. only works 5 levels deep
- function admin_clone_subtree($source_parent_id, $target_parent_id) {
- // find all pages from the source id
- $source_parent_node = $this->Page->find('first', array('conditions' => array('Page.id' => $source_parent_id))); // find the source parent page
- $pages_to_copy = $this->Page->find('threaded', array('conditions' => array('Page.lft >=' => $source_parent_node['Page']['lft'], 'Page.rght <=' => $source_parent_node['Page']['rght'], 'Page.id <>' => $source_parent_id)));
- $id_map = array();
- $target_parent_node = $this->Page->find('first', array('conditions' => array('Page.id' => $target_parent_id))); // find the target parent page
- foreach ($pages_to_copy as $page) {
- // insert the page as a new page
- $old_id = $page['Page']['id'];
- unset($page['Page']['id']);
- unset($page['Page']['lft']);
- unset($page['Page']['rght']);
- unset($page['Page']['created']);
- unset($page['Page']['modified']);
- $page['Page']['parent_id'] = $target_parent_id; // make it a child of the new root parent
- $this->Page->create();
- $this->Page->save($page);
- $id_map[$old_id] = $this->Page->id;
- foreach ($page['children'] as $page1) {
- // insert the page as a new page
- $old_id = $page1['Page']['id'];
- unset($page1['Page']['id']);
- unset($page1['Page']['lft']);
- unset($page1['Page']['rght']);
- unset($page1['Page']['created']);
- unset($page1['Page']['modified']);
- $page1['Page']['parent_id'] = $id_map[$page1['Page']['parent_id']]; // find the new parent_id
- $this->Page->create();
- $this->Page->save($page1);
- $id_map[$old_id] = $this->Page->id;
- foreach ($page1['children'] as $page2) {
- // insert the page as a new page
- $old_id = $page2['Page']['id'];
- unset($page2['Page']['id']);
- unset($page2['Page']['lft']);
- unset($page2['Page']['rght']);
- unset($page2['Page']['created']);
- unset($page2['Page']['modified']);
- $page2['Page']['parent_id'] = $id_map[$page2['Page']['parent_id']]; // find the new parent_id
- $this->Page->create();
- $this->Page->save($page2);
- $id_map[$old_id] = $this->Page->id;
- foreach ($page2['children'] as $page3) {
- // insert the page as a new page
- $old_id = $page3['Page']['id'];
- unset($page3['Page']['id']);
- unset($page3['Page']['lft']);
- unset($page3['Page']['rght']);
- unset($page3['Page']['created']);
- unset($page3['Page']['modified']);
- $page3['Page']['parent_id'] = $id_map[$page3['Page']['parent_id']]; // find the new parent_id
- $this->Page->create();
- $this->Page->save($page3);
- $id_map[$old_id] = $this->Page->id;
- foreach ($page3['children'] as $page4) {
- // insert the page as a new page
- $old_id = $page4['Page']['id'];
- unset($page4['Page']['id']);
- unset($page4['Page']['lft']);
- unset($page4['Page']['rght']);
- unset($page4['Page']['created']);
- unset($page4['Page']['modified']);
- $page4['Page']['parent_id'] = $id_map[$page4['Page']['parent_id']]; // find the new parent_id
- $this->Page->create();
- $this->Page->save($page4);
- $id_map[$old_id] = $this->Page->id;
- }
- }
- }
- }
- }
- $this->Session->setFlash('Pages cloned successfully', 'success');
- $this->redirect(array('action'=>'index'));
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement