Advertisement
Guest User

CakePHP copy subtree from one parent to another

a guest
Oct 24th, 2012
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.30 KB | None | 0 0
  1. <?php
  2. // copy a subtree from one parent to another.  only works 5 levels deep
  3. function admin_clone_subtree($source_parent_id, $target_parent_id) {
  4.     // find all pages from the source id
  5.     $source_parent_node = $this->Page->find('first', array('conditions' => array('Page.id' => $source_parent_id))); // find the source parent page
  6.     $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)));
  7.     $id_map = array();
  8.     $target_parent_node = $this->Page->find('first', array('conditions' => array('Page.id' => $target_parent_id))); // find the target parent page
  9.     foreach ($pages_to_copy as $page) {
  10.         // insert the page as a new page
  11.         $old_id = $page['Page']['id'];
  12.         unset($page['Page']['id']);
  13.         unset($page['Page']['lft']);
  14.         unset($page['Page']['rght']);
  15.         unset($page['Page']['created']);
  16.         unset($page['Page']['modified']);
  17.         $page['Page']['parent_id'] = $target_parent_id; // make it a child of the new root parent
  18.         $this->Page->create();
  19.         $this->Page->save($page);
  20.         $id_map[$old_id] = $this->Page->id;
  21.         foreach ($page['children'] as $page1) {
  22.             // insert the page as a new page
  23.             $old_id = $page1['Page']['id'];
  24.             unset($page1['Page']['id']);
  25.             unset($page1['Page']['lft']);
  26.             unset($page1['Page']['rght']);
  27.             unset($page1['Page']['created']);
  28.             unset($page1['Page']['modified']);
  29.             $page1['Page']['parent_id'] = $id_map[$page1['Page']['parent_id']]; // find the new parent_id
  30.             $this->Page->create();
  31.             $this->Page->save($page1);
  32.             $id_map[$old_id] = $this->Page->id;
  33.             foreach ($page1['children'] as $page2) {
  34.                 // insert the page as a new page
  35.                 $old_id = $page2['Page']['id'];
  36.                 unset($page2['Page']['id']);
  37.                 unset($page2['Page']['lft']);
  38.                 unset($page2['Page']['rght']);
  39.                 unset($page2['Page']['created']);
  40.                 unset($page2['Page']['modified']);
  41.                 $page2['Page']['parent_id'] = $id_map[$page2['Page']['parent_id']]; // find the new parent_id
  42.                 $this->Page->create();
  43.                 $this->Page->save($page2);
  44.                 $id_map[$old_id] = $this->Page->id;
  45.                 foreach ($page2['children'] as $page3) {
  46.                     // insert the page as a new page
  47.                     $old_id = $page3['Page']['id'];
  48.                     unset($page3['Page']['id']);
  49.                     unset($page3['Page']['lft']);
  50.                     unset($page3['Page']['rght']);
  51.                     unset($page3['Page']['created']);
  52.                     unset($page3['Page']['modified']);
  53.                     $page3['Page']['parent_id'] = $id_map[$page3['Page']['parent_id']]; // find the new parent_id
  54.                     $this->Page->create();
  55.                     $this->Page->save($page3);
  56.                     $id_map[$old_id] = $this->Page->id;
  57.                     foreach ($page3['children'] as $page4) {
  58.                         // insert the page as a new page
  59.                         $old_id = $page4['Page']['id'];
  60.                         unset($page4['Page']['id']);
  61.                         unset($page4['Page']['lft']);
  62.                         unset($page4['Page']['rght']);
  63.                         unset($page4['Page']['created']);
  64.                         unset($page4['Page']['modified']);
  65.                         $page4['Page']['parent_id'] = $id_map[$page4['Page']['parent_id']]; // find the new parent_id
  66.                         $this->Page->create();
  67.                         $this->Page->save($page4);
  68.                         $id_map[$old_id] = $this->Page->id;
  69.                     }
  70.                 }
  71.             }
  72.         }
  73.     }
  74.     $this->Session->setFlash('Pages cloned successfully', 'success');
  75.     $this->redirect(array('action'=>'index'));
  76. }
  77. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement