/** * Restore page from serialized object. * * @param Page $page * @param array $pageData * @param array $pageMap */ public static function restorePage(Page $page, array $pageData, array $pageMap) { unset($pageData['id']); unset($pageData['uid']); unset($pageData['name']); unset($pageData['url']); if ($pageData['redirect_page']) { $page->setRedirectPage(self::findPageByMap($pageData['redirect_page'], $pageMap)); } $page->fromArray($pageData, BasePeer::TYPE_FIELDNAME); $page->save(); if (!$pageData['revision']) { return; } // Create new revision $pr = new PageRevision(); $pr->setPage($page); $pr->setBasePage(self::findPageByMap($pageData['revision']['base_page'], $pageMap)); $pr->fromArray($pageData['revision'], BasePeer::TYPE_FIELDNAME); $pr->setDescription('Copied'); $page->setWorkingPageRevision($pr); $page->save(); // Add module data... $order = array(); $parentPages = ArrayHelper::objectsToArray($page->getWorkingPageRevision()->getInheritanceChain(true), null, 'getPageId'); $inheritedModules = $pr->getModules(); foreach ($pageData['revision']['modules'] as $module) { $pm = null; if (!$module['is_inherited']) { $pm = PageModuleQuery::create()->findOneByUid($module['uid']); if ($pm && !in_array($pm->getPageId(), $parentPages)) { // Page module exists, but is not in our "inheritance chain" // Give the module a new unique-id, and create the module here $pm->setUid(Helper::getUniqueId()); $pm->save(); $pm = null; } } else { // find inherited module foreach ($inheritedModules as $inheritedModule) { if ($inheritedModule->getUid() == $module['uid']) { $pm = $inheritedModule; break; } } } if (!$pm) { $pm = new PageModule(); $pm->setPage($page); $pm->fromArray($module, BasePeer::TYPE_FIELDNAME); } if (!$module['is_inherited']) { $rm = new RevisionModule(); $rm->setPageModule($pm); $rm->setPageRevision($pr); } foreach ($module['datas'] as $moduleData) { $md = new ModuleData(); $md->setPageModule($pm); $md->setPageRevision($pr); $md->fromArray($moduleData, BasePeer::TYPE_FIELDNAME); } $order[] = $pm->getUid(); } $pr->save(); $modules = ArrayHelper::objectsToArray($pr->getModules(), 'getUid'); if (array_keys($modules) !== $order) { foreach ($order as $uid) { $module = $modules[$uid]; $sortorder = new ModuleSortorder(); $sortorder->setPageModule($module); $sortorder->setPageRevision($pr); $sortorder->insertAtBottom(); $sortorder->save(); } } $page->setActivePageRevision($pr); $page->save(); }
/** * Get ModuleSortorder object for this module/pagerevision. * * @param bool $create * @return ModuleSortorder|null */ public function getSortorder($create = false) { $sortorder = ModuleSortorderQuery::create()->findPk(array($this->pageModuleId, $this->pageRevisionId)); if ($sortorder === null && $create) { $sortorder = new ModuleSortorder(); $sortorder->setPageModuleId($this->pageModuleId); $sortorder->setPageRevisionId($this->pageRevisionId); } return $sortorder; }