Example #1
0
    /**
     * @return PageLocalization
     * @throws ResourceNotFoundException if page not found or is inactive
     */
    protected function detectRequestPageLocalization()
    {
        $pathString = trim($this->attributes->get('path'), '/');
        $entityManager = $this->getEntityManager();
        $queryString = sprintf('SELECT l FROM %s l JOIN l.path p WHERE p.path = :path
			AND p.active = true AND p.locale = :locale AND l.publishedRevision IS NOT NULL', PageLocalization::CN());
        $query = $entityManager->createQuery($queryString)->setParameters(array('locale' => $this->getLocale(), 'path' => $pathString));
        $pageLocalization = $query->getOneOrNullResult();
        /* @var $pageData PageLocalization */
        if ($pageLocalization === null) {
            throw new ResourceNotFoundException(sprintf('
					No page found by path [%s] in pages controller.', $pathString));
        }
        if (!$pageLocalization->isActive()) {
            throw new ResourceNotFoundException(sprintf('Page found by path [%s] in pages controller is inactive.'));
        }
        $localeManager = $this->container->getLocaleManager();
        if (!$localeManager->isActive($pageLocalization->getLocaleId())) {
            throw new ResourceNotFoundException(sprintf('Page found by path [%s] in pages controller belongs to inactive locale [%s].', $pathString, $this->getLocale()));
        }
        return $pageLocalization;
    }
 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);
 }
 public static function getLinkReferencedElementUrl(LinkReferencedElement $element, EntityManager $entityManager, LocaleInterface $currenctLocale)
 {
     switch ($element->getResource()) {
         case LinkReferencedElement::RESOURCE_LINK:
             return $element->getHref();
         case LinkReferencedElement::RESOURCE_PAGE:
             $localization = $entityManager->getRepository(PageLocalization::CN())->findOneBy(array('master' => $element->getPageId(), 'locale' => $currenctLocale->getId()));
             /* @var $localization PageLocalization */
             if ($localization !== null) {
                 // @TODO: allow to affect the path somehow?
                 return $localization->getFullPath(Path::FORMAT_LEFT_DELIMITER);
             }
             break;
         case LinkReferencedElement::RESOURCE_FILE:
             // @FIXME: get the filestorage somehow, or use File entity?
             throw new \Exception('Implement me, I have no file storage here.');
             break;
         default:
             throw new \UnexpectedValueException("Unrecognized resource type [{$element->getResource()}].");
     }
     return null;
     // @TODO: 'email' resource type handling?
 }
 /**
  * @return \Doctrine\ORM\QueryBuilder
  */
 protected function doGetQueryBuilder()
 {
     // Clones the query builder for local usage
     $qb = clone $this->pageFinder->getQueryBuilder();
     if ($this->pageFinder instanceof TemplateFinder) {
         $qb->from(TemplateLocalization::CN(), 'l');
         $qb->select('l');
     } else {
         $qb->from(PageLocalization::CN(), 'l');
         $qb->select('l, e2, p');
         $qb->join('l.path', 'p');
         $qb->join('l.master', 'e2');
         //			if ($this->active) {
         //				$qb->andWhere('l.active = true AND p.path IS NOT NULL');
         //			}
         if ($this->public) {
             $qb->andWhere('l.publishedRevision IS NOT NULL');
         }
         if ($this->visibleInSitemap) {
             $qb->andWhere('l.visibleInSitemap = true');
         }
         if ($this->visibleInMenu) {
             $qb->andWhere('l.visibleInMenu = true');
         }
         if (!is_null($this->redirect)) {
             $qb->andWhere('l.redirect IS ' . ($this->redirect ? 'NOT ' : '') . 'NULL');
         }
     }
     $qb->andWhere('l.master = e');
     // Join only to fetch the master
     // It's important to include all or else extra queries will be executed
     //$qb->select('l, e2, p');
     if (!empty($this->locale)) {
         $qb->andWhere('l.locale = :locale')->setParameter('locale', $this->locale);
     }
     return $qb;
 }
 protected function restorePageVersion()
 {
     //$this->isPostRequest();
     $revisionId = $this->getRequestParameter('revision_id');
     $localizationId = $this->getRequestParameter('page_id');
     $em = $this->container->getDoctrine()->getManager();
     $locale = $this->container->getLocaleManager()->getCurrentLocale();
     $auditManager = $this->container['entity_audit.manager'];
     /* @var $auditManager AuditManager */
     $auditReader = $auditManager->createAuditReader($em);
     //TODO: this is hacky (revisionId - 1) because EA does not take into account DEL revisions
     $localization = $auditReader->find(PageLocalization::CN(), $localizationId, $revisionId - 1);
     /* @var PageLocalization $localization */
     $page = $localization->getMaster();
     /* @var Page $page */
     foreach ($page->getLocalizations() as $localization) {
         //reload templates
         $template = $localization->getTemplate();
         $realTemplate = $em->getRepository('Cms:Template')->find($template->getId());
         if ($realTemplate) {
             $localization->setTemplate($realTemplate);
         } else {
             $em->persist($template);
         }
         //reload localization path
         $localizationPath = $localization->getPathEntity();
         $realLocalizationPath = $em->getRepository('Cms:PageLocalizationPath')->find($localizationPath->getId());
         if ($realLocalizationPath) {
             $localization->setPathEntity($realLocalizationPath);
         } else {
             $em->persist($localizationPath);
         }
         $em->persist($localization);
     }
     //reset nested set node
     $page->setLevel(null);
     $page->setLeftValue(null);
     $page->setRightValue(null);
     $node = new DoctrineNode($em->getRepository($page->getNestedSetRepositoryClassName()));
     $page->setNestedSetNode($node);
     $node->belongsTo($page);
     $em->persist($page);
     $em->flush();
     $parent = $this->getPageByRequestKey('parent_id');
     $reference = $this->getPageByRequestKey('reference');
     try {
         if (!is_null($reference)) {
             $page->moveAsPrevSiblingOf($reference);
         } elseif (!is_null($parent)) {
             $parent->addChild($page);
         }
     } catch (DuplicatePagePathException $uniqueException) {
         $confirmation = $this->getConfirmation('{#sitemap.confirmation.duplicate_path#}');
         if ($confirmation instanceof Response) {
             return $confirmation;
         }
         $localizations = $page->getLocalizations();
         foreach ($localizations as $localization) {
             $pathPart = $localization->getPathPart();
             // some bad solution
             $localization->setPathPart($pathPart . '-' . time());
         }
         if (!is_null($reference)) {
             $page->moveAsPrevSiblingOf($reference);
         } elseif (!is_null($parent)) {
             $parent->addChild($page);
         }
     }
     return new SupraJsonResponse($this->convertPageToArray($page, $this->getCurrentLocale()->getId()));
     //		// We need it so later we can mark it as restored
     //		$pageRevisionData = $draftEm->getRepository(PageRevisionData::CN())
     //			->findOneBy(array('type' => PageRevisionData::TYPE_TRASH, 'id' => $revisionId));
     //
     //		if ( ! ($pageRevisionData instanceof PageRevisionData)) {
     //			throw new CmsException(null, 'Page revision data not found');
     //		}
     //
     //		$masterId = $auditEm->createQuery("SELECT l.master FROM page:Abstraction\Localization l
     //				WHERE l.id = :id AND l.revision = :revision")
     //			->execute(
     //				array('id' => $localizationId, 'revision' => $revisionId), ColumnHydrator::HYDRATOR_ID);
     //
     //		$page = null;
     //
     //		try {
     //			$page = $auditEm->getRepository(AbstractPage::CN())
     //				->findOneBy(array('id' => $masterId, 'revision' => $revisionId));
     //		} catch (MissingResourceOnRestore $missingResource) {
     //			$missingResourceName = $missingResource->getMissingResourceName();
     //			throw new CmsException(null, "Wasn't able to load the page from the history because linked resource {$missingResourceName} is not available anymore.");
     //		}
     //
     //		if (empty($page)) {
     //			throw new CmsException(null, "Cannot find the page");
     //		}
     //
     //		$localeId = $this->getLocale()->getId();
     //		$media = $this->getMedia();
     //
     //		$pageLocalization = $page->getLocalization($localeId);
     //
     //		if (is_null($pageLocalization)) {
     //			throw new CmsException(null, 'This page has no localization for current locale');
     //		}
     //
     //		$request = new HistoryPageRequestEdit($localeId, $media);
     //		$request->setDoctrineEntityManager($draftEm);
     //		$request->setPageLocalization($pageLocalization);
     //
     //		$draftEventManager = $draftEm->getEventManager();
     //		$draftEventManager->dispatchEvent(AuditEvents::pagePreRestoreEvent);
     //
     //		$parent = $this->getPageByRequestKey('parent_id');
     //		$reference = $this->getPageByRequestKey('reference');
     //
     //		// Did not allowed to restore root page. Ask Aigars for details
     ////		if (is_null($parent) && $page instanceof Page) {
     ////			throw new CmsException('sitemap.error.parent_page_not_found');
     ////		}
     //
     //		$draftEm->beginTransaction();
     //		try {
     //			$request->restorePage();
     //
     //			// Read from the draft now
     //			$page = $draftEm->find(AbstractPage::CN(), $page->getId());
     //
     //			/* @var $page AbstractPage */
     //
     //			try {
     //				if ( ! is_null($reference)) {
     //					$page->moveAsPrevSiblingOf($reference);
     //				} elseif ( ! is_null($parent)) {
     //					$parent->addChild($page);
     //				}
     //			} catch (DuplicatePagePathException $uniqueException) {
     //
     //				$this->getConfirmation('{#sitemap.confirmation.duplicate_path#}');
     //
     //				$localizations = $page->getLocalizations();
     //				foreach ($localizations as $localization) {
     //					$pathPart = $localization->getPathPart();
     //
     //					// some bad solution
     //					$localization->setPathPart($pathPart . '-' . time());
     //				}
     //
     //				if ( ! is_null($reference)) {
     //					$page->moveAsPrevSiblingOf($reference);
     //				} elseif ( ! is_null($parent)) {
     //					$parent->addChild($page);
     //				}
     //			}
     //
     //			$pageRevisionData->setType(PageRevisionData::TYPE_RESTORED);
     //			$draftEm->flush();
     //
     //			$localization = $page->getLocalization($localeId);
     //			$this->pageData = $localization;
     //		} catch (\Exception $e) {
     //			$draftEm->rollback();
     //			throw $e;
     //		}
 }
 /**
  * @param PageLocalization $localization
  * @return array
  */
 private function getPageLocalizationRedirectData(PageLocalization $localization)
 {
     if (!$localization->hasRedirectTarget()) {
         return array();
     }
     $redirectTarget = $localization->getRedirectTarget();
     $targetPage = null;
     if ($redirectTarget instanceof Entity\RedirectTargetPage) {
         $targetPage = $redirectTarget->getTargetPage();
     }
     return array('url' => $redirectTarget->getRedirectUrl(), 'target_page_id' => $targetPage ? $targetPage->getId() : null);
 }
 /**
  * Method to override the used page localization
  * @param PageLocalization $pageLocalization
  */
 public function setPageLocalization(PageLocalization $pageLocalization)
 {
     $this->pageLocalization = $pageLocalization;
     $this->pageId = $pageLocalization->getMaster()->getId();
 }
 /**
  * @param array $data
  * @return PageLocalization
  */
 protected function createEntityPageLocalization($data)
 {
     $entity = new PageLocalization($data['locale']);
     $entity->setPathPart($data['pathPart']);
     $entity->setMaster($this->resolveEntity('page', $data['page']));
     $entity->setTitle($data['title']);
     $entity->setTemplate($this->resolveEntity('template', $data['template']));
     return $entity;
 }
 /**
  * Get list of pages converted to array
  * @param string $entity
  * @return array
  */
 private function loadSitemapTree($entity)
 {
     $em = $this->getEntityManager();
     $input = $this->getRequestInput();
     $localeId = $this->getCurrentLocale()->getId();
     // Parent ID and level
     $levels = null;
     $parentId = null;
     if ($input->has('parent_id')) {
         $parentId = $input->get('parent_id');
         // Special case for root page, need to fetch 2 levels
         if (empty($parentId)) {
             $levels = 2;
         } else {
             $levels = 1;
         }
     } else {
         $levels = 2;
     }
     $parentLocalization = null;
     $filter = null;
     if (!empty($parentId)) {
         if (strpos($parentId, '_') !== false) {
             list($parentId, $filter) = explode('_', $parentId);
         }
         // Find localization, special case for group pages
         if ($entity == Page::CN()) {
             $parentLocalization = $em->find(PageLocalization::CN(), $parentId);
             if (is_null($parentLocalization)) {
                 $rootNode = $em->find($entity, $parentId);
                 if ($rootNode instanceof GroupPage) {
                     $parentLocalization = $rootNode->getLocalization($localeId);
                 }
             }
         } else {
             $parentLocalization = $em->find(TemplateLocalization::CN(), $parentId);
         }
         if (is_null($parentLocalization)) {
             throw new CmsException(null, 'The page the children has been requested for was not found');
         }
     }
     $response = $this->gatherChildrenData($entity, $parentLocalization, $filter, $levels);
     return $response;
 }
Example #10
0
 /**
  * @param AbstractPage $page
  * @param string $locale
  * @return Localization
  */
 public static function factory(AbstractPage $page, $locale)
 {
     $localization = null;
     if ($page instanceof ApplicationPage) {
         $localization = new ApplicationLocalization($locale);
     } elseif ($page instanceof GroupPage) {
         $localization = new GroupLocalization($locale, $page);
     } elseif ($page instanceof Template) {
         $localization = new TemplateLocalization($locale);
     } elseif ($page instanceof Page) {
         $localization = new PageLocalization($locale);
     } else {
         throw new \UnexpectedValueException(sprintf('Don\'t know what to do with [%s]', get_class($page)));
     }
     $localization->setMaster($page);
     return $localization;
 }
Example #11
0
 /**
  * @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()));
 }
 /**
  * Loads page path
  * @param PageLocalization $pageData
  * @return Path
  */
 protected function findPagePath(PageLocalization $pageData)
 {
     $active = true;
     $limited = false;
     $inSitemap = true;
     $path = new Path();
     // Inactive page children have no path
     if (!$pageData->isActive()) {
         $active = false;
     }
     if (!$pageData->isVisibleInSitemap()) {
         $inSitemap = false;
     }
     $pathPart = $pageData->getPathPart();
     if (is_null($pathPart)) {
         return array(null, false, false, false);
     }
     $path->prependString($pathPart);
     $page = $pageData->getMaster();
     $locale = $pageData->getLocale();
     $parentPage = $page->getParent();
     if (is_null($parentPage)) {
         return array($path, $active, $limited, $inSitemap);
     }
     $parentLocalization = $parentPage->getLocalization($locale);
     // No parent page localization
     if (is_null($parentLocalization)) {
         return array(null, false, false, false);
     }
     // Page application feature to generate base path for pages
     if ($parentPage instanceof ApplicationPage) {
         $applicationId = $parentPage->getApplicationId();
         $pageApplicationManager = $this->container['cms.pages.page_application_manager'];
         /* @var $pageApplicationManager PageApplicationManager */
         if (!$pageApplicationManager->hasApplication($applicationId)) {
             throw new Exception\PagePathException(sprintf("Failed to generate path because, page application [%s] not found.", $applicationId), $pageData);
         }
         $application = $pageApplicationManager->createApplicationFor($pageData, $this->em);
         $pathBasePart = $application->generatePath($pageData);
         $path->prepend($pathBasePart);
     }
     // Search nearest page parent
     while (!$parentLocalization instanceof PageLocalization) {
         $parentPage = $parentPage->getParent();
         if (is_null($parentPage)) {
             return array($pageData->getPathPart(), $active, $limited, $inSitemap);
         }
         $parentLocalization = $parentPage->getLocalization($locale);
         // No parent page localization
         if (is_null($parentLocalization)) {
             return array(null, false, false, false);
         }
     }
     // Is checked further
     //		if ( ! $parentLocalization->isActive()) {
     //			$active = false;
     //		}
     // Assume that path is already regenerated for the parent
     $parentPath = $parentLocalization->getPathEntity()->getPath();
     $parentActive = $parentLocalization->getPathEntity()->isActive();
     $parentInSitemap = $parentLocalization->getPathEntity()->isVisibleInSitemap();
     if (!$parentActive) {
         $active = false;
     }
     if (!$parentInSitemap) {
         $inSitemap = false;
     }
     if (is_null($parentPath)) {
         return array(null, false, false, false);
     }
     $path->prepend($parentPath);
     return array($path, $active, $limited, $inSitemap);
 }
 /**
  * Template delete action.
  *
  * @return SupraJsonResponse
  */
 public function deleteAction()
 {
     $page = $this->getPageLocalization()->getMaster();
     $entityManager = $this->getEntityManager();
     $count = (int) $entityManager->createQuery(sprintf('SELECT COUNT(p.id) FROM %s p WHERE p.template = ?0', PageLocalization::CN()))->setParameters(array($page->getId()))->getSingleScalarResult();
     if ($count > 0) {
         throw new CmsException(null, "Cannot remove template, {$count} page(s) still uses it.");
     }
     $entityManager->remove($page);
     $entityManager->flush();
     return new SupraJsonResponse();
 }
Example #14
0
 /**
  * @param PageLocalization $localization
  * @return Set\PageSet
  */
 protected function getPageTemplateHierarchy(PageLocalization $localization)
 {
     $template = $localization->getTemplate();
     $page = $localization->getPage();
     if (empty($template)) {
         throw new RuntimeException("No template assigned to the page {$localization->getId()}");
     }
     $pageSet = $this->getTemplateTemplateHierarchy($template);
     $pageSet[] = $page;
     return $pageSet;
 }