/**
  * 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));
 }
 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));
 }
 /**
  * Get first page localization ID to show in the CMS.
  *
  * @return Localization
  */
 protected function getInitialPageLocalization()
 {
     $localization = null;
     $cookieBag = $this->container->getRequest()->cookies;
     if ($cookieBag->has(self::INITIAL_PAGE_ID_COOKIE)) {
         $localization = $this->getEntityManager()->find(Localization::CN(), $cookieBag->get(self::INITIAL_PAGE_ID_COOKIE));
     }
     // Root page otherwise
     if ($localization === null) {
         $pageRepository = $this->getEntityManager()->getRepository(Page::CN());
         /* @var $pageRepository PageRepository */
         $pages = $pageRepository->getRootNodes();
         if (!empty($pages)) {
             /* @var $pages AbstractPage[] */
             return $pages[0]->getLocalization($this->getCurrentLocale()->getId());
         }
     }
     return null;
 }
 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();
 }
Beispiel #6
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()));
 }