Exemplo n.º 1
0
 /**
  * Adds the page's seo attributes when a new page is added, for each language of
  * the site
  *
  * @param  BeforeAddPageCommitEvent $event
  * @return boolean
  * @throws InvalidArgumentException
  * @throws \Exception
  *
  * @api
  */
 public function onBeforeAddPageCommit(BeforeAddPageCommitEvent $event)
 {
     if ($event->isAborted()) {
         return;
     }
     $pageManager = $event->getContentManager();
     $pageRepository = $pageManager->getPageRepository();
     $values = $event->getValues();
     if (!is_array($values)) {
         throw new InvalidArgumentException('exception_invalid_value_array_required');
     }
     try {
         $languages = $this->languageRepository->activeLanguages();
         if (count($languages)) {
             $result = true;
             $idPage = $pageManager->get()->getId();
             $this->seoManager->getSeoRepository()->setConnection($pageRepository->getConnection());
             $pageRepository->startTransaction();
             foreach ($languages as $alLanguage) {
                 $seoManagerValues = array_merge($values, array('PageId' => $idPage, 'LanguageId' => $alLanguage->getId(), 'CreatedAt' => date("Y-m-d H:i:s")));
                 if (!$alLanguage->getMainLanguage() && array_key_exists('Permalink', $seoManagerValues)) {
                     $seoManagerValues['Permalink'] = $alLanguage->getLanguageName() . '-' . $seoManagerValues['Permalink'];
                 }
                 $this->seoManager->set(null);
                 $result = $this->seoManager->save($seoManagerValues);
                 if (false === $result) {
                     break;
                 }
             }
             if (false !== $result) {
                 $pageRepository->commit();
                 return;
             }
             $pageRepository->rollBack();
             $event->abort();
         }
     } catch (\Exception $e) {
         $event->abort();
         if (isset($pageRepository) && $pageRepository !== null) {
             $pageRepository->rollBack();
         }
         throw $e;
     }
 }
 /**
  * Adds the contents for the page when a new page is added, for each language of the site
  *
  * @param  \RedKiteLabs\RedKiteCms\RedKiteCmsBundle\Core\Event\Content\Page\BeforeAddPageCommitEvent $event
  * @return boolean
  * @throws \Exception
  *
  * @api
  */
 public function onBeforeAddPageCommit(BeforeAddPageCommitEvent $event)
 {
     if ($event->isAborted()) {
         return;
     }
     $pageManager = $event->getContentManager();
     $templateManager = $pageManager->getTemplateManager();
     $pageRepository = $pageManager->getPageRepository();
     try {
         $languages = $this->languageRepository->activeLanguages();
         if (count($languages) > 0) {
             $result = true;
             $templateManager->getBlockRepository()->setConnection($pageRepository->getConnection());
             $pageRepository->startTransaction();
             // The min number of pages is setted to 1 because we are adding a page which has been saved but not
             // committed so it counts as one
             $ignoreRepeatedSlots = $pageManager->getValidator()->hasPages(1);
             $idPage = $pageManager->get()->getId();
             foreach ($languages as $alLanguage) {
                 $result = $templateManager->populate($alLanguage->getId(), $idPage, $ignoreRepeatedSlots);
                 if ($result === false) {
                     break;
                 }
             }
             if ($result === false) {
                 $pageRepository->rollBack();
                 $event->abort();
             } else {
                 $pageRepository->commit();
             }
         }
     } catch (\Exception $e) {
         $event->abort();
         if (isset($pageRepository) && $pageRepository !== null) {
             $pageRepository->rollBack();
         }
         throw $e;
     }
 }