/** * 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(); }
/** * Save new module. * * @todo Set module position. * * @param PageRevision $pageRevision * @param array $values * @return PageModule */ public static function saveNewModule(PageRevision $pageRevision, array $values) { $pageModule = new PageModule(); $pageModule->setUid(Helper::getUniqueId()); $pageModule->setPageId($pageRevision->getPageId()); if (ctype_digit($values['module_class'])) { $module = ModuleQuery::create()->findPk($values['module_class']); $pageModule->setModuleClass($module->getModuleClass()); $pageModule->setName($values['name']); $pageModule->setTarget($values['target']); $pageModule->setContentVisibility($module->getContentVisibility()); $pageModule->setSearchVisibility($module->getSearchVisibility()); $template = $module->getTemplate(); } else { $template = null; if ($values['template'] == 'new' && $values['template_name']) { $className = $values['module_class']; $predefinedTemplates = call_user_func(array($className, 'getPredefinedTemplates')); $root = \Curry\App::getInstance()['template.root']; $template = $values['template_name']; $templateFile = $root . DIRECTORY_SEPARATOR . $template; if (!file_exists($templateFile)) { $dir = dirname($templateFile); if (!is_dir($dir)) { mkdir($dir, 0777, true); } $code = $predefinedTemplates[$values['predefined_template']]; file_put_contents($templateFile, (string) $code); } } else { if ($values['template']) { $template = $values['template']; } } $target = ''; if (!empty($values['target'])) { $target = $values['target']; } else { if (!empty($values['target_name'])) { $target = $values['target_name']; } } if (empty($target)) { throw new Exception('Module target not set'); } $pageModule = new PageModule(); $pageModule->setUid(Helper::getUniqueId()); $pageModule->setPageId($pageRevision->getPageId()); $pageModule->setModuleClass($values['module_class']); $pageModule->setName($values['name']); $pageModule->setTarget($target); $pageModule->setContentVisibility($values['content_visibility']); $pageModule->setSearchVisibility($values['search_visibility']); } $pageModule->save(); $revisionModule = new RevisionModule(); $revisionModule->setPageModule($pageModule); $revisionModule->setPageRevision($pageRevision); $revisionModule->save(); $moduleData = new ModuleData(); $moduleData->setPageRevision($pageRevision); $moduleData->setPageModule($pageModule); $moduleData->setEnabled(true); $moduleData->setTemplate($template); $moduleData->save(); // create default data $wrapper = new PageModuleWrapper($pageModule, $pageRevision, null); $wrapper->createData(); return $pageModule; }