/**
  * 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));
 }
 /**
  * @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));
 }
 /**
  * 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));
 }