public function changeParent(Mage_Cms_Model_Page $page, Mage_Cms_Model_Page $newParent, $afterPageId = null) { $table = $this->getTable('cms/page'); $adapter = $this->_getWriteAdapter(); // Decrease children count for all old page parent pages $this->decreaseChildrenCount($page, $page->getParentIds()); // Increase children count for new page parents $this->increaseChildrenCount($page, $newParent->getPathIds()); $position = $this->_processPositions($page, $newParent, $afterPageId); $newPath = $newParent->getPath() . '/' . $page->getId(); $identifiers = explode('/', $page->getIdentifier()); $newIdentifier = trim($newParent->getIdentifier() . '/' . array_pop($identifiers), '/'); $newLevel = $newParent->getLevel() + 1; $levelDisposition = $newLevel - $page->getLevel(); if (Mage::helper('cms/page')->isCreatePermanentRedirects($page->getStoreId())) { $newPage = clone $page; $newPage->setIdentifier($newIdentifier); $this->updatePermanentRedirects($page, $newPage); } // Update children nodes identifiers $this->updateChildrenIdentifiers($page, $newIdentifier); // Update children nodes path $sql = "UPDATE {$table} SET\n `path` = REPLACE(`path`, '{$page->getPath()}/', '{$newPath}/'),\n `level` = `level` + {$levelDisposition}\n WHERE " . $adapter->quoteInto('path LIKE ?', $page->getPath() . '/%'); $adapter->query($sql); // Update moved page data $data = array('path' => $newPath, 'level' => $newLevel, 'identifier' => $newIdentifier, 'position' => $position, 'parent_id' => $newParent->getId()); $adapter->update($table, $data, $adapter->quoteInto('page_id = ?', $page->getId())); // Update page object to new data $page->addData($data); return $this; }