/** * Handles Page creation request. */ public function createAction() { $this->isPostRequest(); $page = new GroupPage(); $localeId = $this->getCurrentLocale()->getId(); $localization = Localization::factory($page, $localeId); /* @var $localization \Supra\Package\Cms\Entity\GroupLocalization */ $title = trim($this->getRequestParameter('title', '')); if (empty($title)) { throw new CmsException(null, 'Group title cannot be empty.'); } $localization->setTitle($title); $parentLocalizationId = $this->getRequestParameter('parent_id'); if (empty($parentLocalizationId)) { throw new \UnexpectedValueException('Parent ID is empty while it is not allowed Group to be root.'); } $parentLocalization = $this->getEntityManager()->find(Localization::CN(), $parentLocalizationId); if ($parentLocalization === null) { throw new CmsException(null, sprintf('Specified parent page [%s] not found.', $parentLocalizationId)); } $entityManager = $this->getEntityManager(); $entityManager->transactional(function (EntityManager $entityManager) use($page, $localization, $parentLocalization) { $this->lockNestedSet($page); $entityManager->persist($page); $entityManager->persist($localization); if ($parentLocalization) { $page->moveAsLastChildOf($parentLocalization->getMaster()); } $this->unlockNestedSet($page); }); return new SupraJsonResponse($this->loadNodeMainData($localization)); }
/** * Checks if associations scopes are matching * * @param Entity $object * @throws \Exception rethrows caught exception */ private function checkScope(Entity &$object) { if (!empty($this->localization) && !empty($this->block)) { try { // do not-strict match (allows page data with template block) $this->localization->matchDiscriminator($this->block); } catch (\Exception $e) { $object = null; throw $e; } } }
/** * @return SupraJsonResponse */ public function createAction() { $this->isPostRequest(); $localeId = $this->getCurrentLocale()->getId(); $template = new Template(); $localization = Localization::factory($template, $localeId); $title = trim($this->getRequestParameter('title', '')); if (empty($title)) { throw new CmsException(null, 'Template title cannot be empty.'); } $localization->setTitle($title); $entityManager = $this->getEntityManager(); $parentLocalization = null; $parentLocalizationId = $this->getRequestParameter('parent_id'); $layoutName = $this->getRequestParameter('layout'); if (empty($parentLocalizationId) && empty($layoutName)) { throw new CmsException(null, 'Root template must have layout specified.'); } if (!empty($parentLocalizationId)) { $parentLocalization = $this->getEntityManager()->find(TemplateLocalization::CN(), $parentLocalizationId); if ($parentLocalization === null) { throw new CmsException(null, sprintf('Specified parent template [%s] not found.', $parentLocalizationId)); } } if (!empty($layoutName)) { $themeTemplateLayout = $this->getActiveTheme()->getLayout($layoutName); if ($themeTemplateLayout === null) { throw new CmsException(null, sprintf('Layout [%s] not found.', $themeTemplateLayout)); } $template->addLayout($this->getMedia(), $themeTemplateLayout); } $entityManager->transactional(function (EntityManager $entityManager) use($template, $localization, $parentLocalization) { $this->lockNestedSet($template); $entityManager->persist($template); $entityManager->persist($localization); $entityManager->flush(); if ($parentLocalization) { $template->moveAsLastChildOf($parentLocalization->getMaster()); } $this->unlockNestedSet($template); }); // @FIXME // // Decision in #2695 to publish the template right after creating it // $this->pageData = $templateData; // $this->publish(); return new SupraJsonResponse($this->loadNodeMainData($localization)); }
protected function prepareData(ClassMetadata $metadata, array $data) { foreach ($data as $name => &$entityData) { switch ($metadata->name) { case Localization::CN(): case PageLocalization::CN(): case TemplateLocalization::CN(): case ApplicationLocalization::CN(): $entityData['publishedRevision'] = null; $args = array($entityData['locale']); break; case PageLocalizationPath::CN(): $args = array(str_ireplace('pageLocalizationPath', '', $name), $entityData['locale']); break; case TemplateLayout::CN(): $args = array($entityData['media']); break; case BlockPropertyMetadata::CN(): $args = array($entityData['name'], $entityData['blockProperty']); break; case BlockProperty::CN(): $args = array($entityData['name']); break; case PagePlaceHolder::CN(): case TemplatePlaceHolder::CN(): $args = array($entityData['name']); break; default: $constructor = new \ReflectionMethod($metadata->name, '__construct'); if ($constructor->getNumberOfRequiredParameters() === 0) { return parent::prepareData($metadata, $data); } throw new \RuntimeException(sprintf('Don\'t know how to build constructor required for [%s].', $metadata->name)); } $entityData = array_merge(array('__construct' => $args), $entityData); } return parent::prepareData($metadata, $data); }
/** * Handles Page creation request. */ public function createAction() { $this->isPostRequest(); $type = $this->getRequestParameter('type'); $page = null; switch ($type) { case Entity::APPLICATION_DISCR: $page = new ApplicationPage($this->getRequestParameter('application_id')); break; case Entity::PAGE_DISCR: $page = new Page(); break; default: throw new \InvalidArgumentException(sprintf('Unknown page type [%s]', $type)); } $localeId = $this->getCurrentLocale()->getId(); $localization = Localization::factory($page, $localeId); /* @var $localization PageLocalization */ if (!$localization instanceof PageLocalization) { throw new \UnexpectedValueException(sprintf('Expecting created localization to be instance of PageLocalization, [%s] received.', get_class($localization))); } $templateId = $this->getRequestParameter('template'); $template = $this->getEntityManager()->find(Template::CN(), $templateId); /* @var $template Template */ if ($template === null) { throw new CmsException(null, 'Template not specified or found.'); } $templateLocalization = $template->getLocalization($localeId); if ($templateLocalization === null) { throw new \InvalidArgumentException("Specified template has no localization for [{$localeId}] locale."); } $localization->setTemplate($template); // copy values from template $localization->setIncludedInSearch($templateLocalization->isIncludedInSearch()); $localization->setVisibleInMenu($templateLocalization->isVisibleInMenu()); $localization->setVisibleInSitemap($templateLocalization->isVisibleInSitemap()); $title = trim($this->getRequestParameter('title', '')); if (empty($title)) { throw new CmsException(null, 'Page title cannot be empty.'); } $localization->setTitle($title); $parentLocalization = $pathPart = null; $parentLocalizationId = $this->getRequestParameter('parent_id'); if (!empty($parentLocalizationId)) { $parentLocalization = $this->getEntityManager()->find(Localization::CN(), $parentLocalizationId); if ($parentLocalization === null) { throw new CmsException(null, sprintf('Specified parent page [%s] not found.', $parentLocalizationId)); } $pathPart = trim($this->getRequestParameter('path')); // path part cannot be empty for non-root pages. if (empty($pathPart)) { throw new CmsException(null, 'Page path can not be empty.'); } } if ($parentLocalization && $pathPart) { $localization->setPathPart($pathPart); } else { $rootPath = $localization->getPathEntity(); $rootPath->setPath(''); $localization->setPathPart(''); } $entityManager = $this->getEntityManager(); $entityManager->transactional(function (EntityManager $entityManager) use($page, $localization, $parentLocalization) { $this->lockNestedSet($page); $entityManager->persist($page); $entityManager->persist($localization); if ($parentLocalization) { $page->moveAsLastChildOf($parentLocalization->getMaster()); } $this->unlockNestedSet($page); }); return new SupraJsonResponse($this->loadNodeMainData($localization)); }
/** * @param Localization $localization * @return Localization */ private function findLocalizationPublishedVersion(Localization $localization) { if (!$localization->isPublished()) { return null; } return $this->getAuditReader()->find($localization::CN(), $localization->getId(), $localization->getPublishedRevision()); }
/** * @param Localization $localization * @return array */ private function getLocalizationData(Localization $localization) { $page = $localization->getMaster(); $allLocalizationData = array(); foreach ($page->getLocalizations() as $locale => $pageLocalization) { $allLocalizationData[$locale] = $pageLocalization->getId(); } $ancestorIds = Entity::collectIds($localization->getAncestors()); // abstract localization data $localizationData = array('root' => $page->isRoot(), 'tree_path' => $ancestorIds, 'locale' => $localization->getLocaleId(), 'localizations' => $allLocalizationData, 'lock' => $this->getLocalizationEditLockData($localization), 'is_visible_in_menu' => $localization->isVisibleInMenu(), 'is_visible_in_sitemap' => $localization->isVisibleInSitemap(), 'include_in_search' => $localization->isIncludedInSearch(), 'page_change_frequency' => $localization->getChangeFrequency(), 'page_priority' => $localization->getPagePriority(), 'internal_html' => null, 'contents' => array()); if ($localization instanceof PageLocalization) { $creationTime = $localization->getCreationTime(); $publicationSchedule = $localization->getScheduleTime(); $localizationData = array_replace($localizationData, array('keywords' => $localization->getMetaKeywords(), 'description' => $localization->getMetaDescription(), 'created_date' => $creationTime->format('Y-m-d'), 'created_time' => $creationTime->format('H:i:s'), 'scheduled_date' => $publicationSchedule ? $publicationSchedule->format('Y-m-d') : null, 'scheduled_time' => $publicationSchedule ? $publicationSchedule->format('H:i:s') : null, 'active' => $localization->isActive(), 'template' => array('id' => $localization->getTemplate()->getId(), 'title' => $localization->getTemplateLocalization()->getTitle()))); } elseif ($localization instanceof TemplateLocalization) { $layoutData = null; if ($page->hasLayout($this->getMedia())) { $layoutName = $page->getLayoutName($this->getMedia()); $layout = $this->getActiveTheme()->getLayout($layoutName); if ($layout !== null) { $layoutData = array('id' => $layout->getName(), 'title' => $layout->getTitle()); } } $localizationData = array_replace($localizationData, array('layouts' => $this->getActiveThemeLayoutsData(), 'layout' => $layoutData)); } return array_replace($this->loadNodeMainData($localization), $localizationData); }
protected function execute(InputInterface $input, OutputInterface $output) { $dataFolder = $this->container->getParameter('directories.project_root') . DIRECTORY_SEPARATOR . $input->getArgument('folder'); if (!is_dir($dataFolder)) { throw new \RuntimeException(sprintf('The directory [%s] does not exists.', $dataFolder)); } $dataFile = $dataFolder . DIRECTORY_SEPARATOR . 'fixtures.yml'; if (!is_file($dataFile)) { throw new \RuntimeException(sprintf('The fixtures file [%s] does not exists.', $dataFile)); } $entityManager = $this->container->getDoctrine()->getManager(); /* @var $entityManager EntityManager */ $entityManager->beginTransaction(); $entities = array('CmsAuthentication:User', 'CmsAuthentication:Group', 'CmsAuthentication:AbstractUser', 'CmsAuthentication:UserPreference', 'CmsAuthentication:UserPreferencesCollection', 'Cms:ApplicationLocalizationParameter', 'Cms:BlockPropertyMetadata', 'Cms:ReferencedElement\\ReferencedElementAbstract', 'Cms:ReferencedElement\\ImageReferencedElement', 'Cms:ReferencedElement\\LinkReferencedElement', 'Cms:ReferencedElement\\MediaReferencedElement', 'Cms:BlockProperty', 'Cms:Abstraction\\Block', 'Cms:Abstraction\\PlaceHolder', 'Cms:LocalizationTag', 'Cms:Abstraction\\Localization', 'Cms:Abstraction\\RedirectTarget', 'Cms:PageLocalizationPath', 'Cms:Page', 'Cms:GroupPage', 'Cms:EditLock', 'Cms:TemplateLayout', 'Cms:Template', 'Cms:Abstraction\\AbstractPage', 'Cms:FileProperty', 'Cms:ImageSize', 'Cms:Image', 'Cms:File', 'Cms:Folder', 'Cms:FilePath', 'Cms:Abstraction\\File'); if ($input->getOption('clear')) { $auditManager = $this->container['entity_audit.manager']; /* @var $auditManager AuditManager */ $metadata = array(); foreach ($entities as $name) { $classMetadata = $entityManager->getClassMetadata($name); $metadata[] = $classMetadata; if ($auditManager->getMetadataFactory()->isAudited($classMetadata->name)) { $tableName = $auditManager->getConfiguration()->getTablePrefix() . $classMetadata->getTableName() . $auditManager->getConfiguration()->getTableSuffix(); $entityManager->getConnection()->executeQuery(sprintf('DELETE FROM %s ', $tableName))->execute(); } } $entityManager->getConnection()->executeQuery(sprintf('DELETE FROM %s ', $auditManager->getConfiguration()->getRevisionTableName()))->execute(); $schemaTool = new SchemaTool($entityManager); $schemaTool->dropSchema($metadata); $schemaTool->createSchema($metadata); } $evtManager = $entityManager->getEventManager(); foreach ($evtManager->getListeners() as $event => $listeners) { foreach ($listeners as $listener) { if ($listener instanceof PagePathGeneratorListener || $listener instanceof FilePathChangeListener || $listener instanceof NestedSetListener || $listener instanceof ImageSizeCreatorListener) { $evtManager->removeEventListener($event, $listener); } } } Fixtures::load($dataFile, $entityManager, array('persist_once' => true)); $entityManager->flush(); // 'publish' pages $entityManager->createQuery(sprintf('UPDATE %s l SET l.publishedRevision = 1', Localization::CN()))->execute(); $fileStorage = $this->getFileStorage(); foreach ($entityManager->getRepository(File::CN())->findAll() as $file) { /* @var $file File */ $tmpName = $this->getFileTemporaryName($file, $dataFolder); if (is_file($tmpName)) { $this->ensureFileDirectoryExists($file); copy($tmpName, $fileStorage->getFilesystemPath($file)); } if ($file instanceof Image) { foreach ($file->getImageSizeCollection() as $imageSize) { /* @var $imageSize ImageSize */ $tmpName = $this->getImageSizeTemporaryName($imageSize, $dataFolder); if (is_file($tmpName)) { $this->ensureImageSizeDirectoryExists($file, $imageSize->getName()); copy($tmpName, $fileStorage->getImagePath($file, $imageSize->getName())); } } } } $entityManager->commit(); }
public function getCacheKey(Localization $localization, Block $block, ResponseContext $context = null) { return sprintf('supra_block_cache_%s_%s_%s', $localization->getId(), $block->getId(), $this->getContextKey($context)); }
public function addFilterByChild(Localization $localization, $minDepth = 0, $maxDepth = null) { $this->setLocale($localization->getLocaleId()); $this->pageFinder->addFilterByChild($localization->getMaster(), $minDepth, $maxDepth); }
/** * @param string $locale */ public function setLocaleId($localeId) { parent::setLocaleId($localeId); if (!empty($this->path)) { $this->path->setLocale($localeId); } }
/** * @param DeepCopy $deepCopy * @param EntityManager $entityManager * @return DeepCopy */ private function addDeepCopyCommonFilters(DeepCopy $deepCopy, EntityManager $entityManager) { $keepFilter = new KeepFilter(); $nullifyFilter = new SetNullFilter(); // Matches RedirectTargetPage::$page property. // Keeps the $page property redirect target is referencing to. $deepCopy->addFilter($keepFilter, new PropertyMatcher(RedirectTargetPage::CN(), 'page')); // Matches PageLocalization::$template. // Prevents the template to be cloned. $deepCopy->addFilter($keepFilter, new PropertyMatcher(PageLocalization::CN(), 'template')); // Matches Localization::$path // Keeps the value since it is cloned manually (see PageLocalization::__clone()); $deepCopy->addFilter($keepFilter, new PropertyMatcher(Localization::CN(), 'path')); // Matches Block::$blockProperties collection. // Replaces with empty collection, since block properties can be obtained via Localization::$blockProperties. $deepCopy->addFilter(new DoctrineEmptyCollectionFilter(), new PropertyMatcher(Block::CN(), 'blockProperties')); // Matches Localization::$lock. // Nullifies editing lock entity. $deepCopy->addFilter($nullifyFilter, new PropertyMatcher(Localization::CN(), 'lock')); // Matches Localization::$publishedRevision. $deepCopy->addFilter($nullifyFilter, new PropertyMatcher(Localization::CN(), 'publishedRevision')); // Matches Localization::$publishTime. $deepCopy->addFilter($nullifyFilter, new PropertyMatcher(Localization::CN(), 'publishTime')); // Matches Entity Collection. // Creates Copy and persists the elements in it. $deepCopy->addFilter(new DoctrineCollectionFilter($entityManager), new PropertyTypeMatcher('Doctrine\\Common\\Collections\\Collection')); // Matches any Entity. // Creates copy and persists it. $deepCopy->addFilter(new DoctrineEntityFilter($entityManager), new PropertyTypeMatcher(Entity::CN())); }