Ejemplo n.º 1
0
 function _handleContentPage($formId = 0)
 {
     if (!$formId) {
         return;
     }
     $objDatabase = \Env::get('db');
     $objFWUser = \FWUser::getFWUserObject();
     $pageRepo = $this->em->getRepository('Cx\\Core\\ContentManager\\Model\\Entity\\Page');
     $pages = array();
     foreach ($pageRepo->findBy(array('module' => 'Contact', 'cmd' => $formId)) as $page) {
         if ($page) {
             $pages[$page->getLang()] = $page;
         }
     }
     $frontendLangIds = array_keys(\FWLanguage::getActiveFrontendLanguages());
     $selectedLangIds = array_keys($this->arrForms[$formId]['lang']);
     // filter out only those languages that are active in the frontent.
     // we do want to create/update only the content pages of those languages.
     $selectedLangIds = array_intersect($selectedLangIds, $frontendLangIds);
     // Maybe there will already be a fallback page if we activate a new language.
     // So we use the fallback page to prevent the creation of a new page.
     foreach ($selectedLangIds as $selectedLangId) {
         if (!isset($pages[$selectedLangId])) {
             $fallbackPage = $pageRepo->findOneByModuleCmdLang('Contact', $formId, $selectedLangId);
             if (!empty($fallbackPage)) {
                 $pages[$selectedLangId] = $fallbackPage;
             }
         }
     }
     $presentLangIds = array_keys($pages);
     // define which languages of the content pages have to be updated
     //        $updateLangIds = array_intersect($selectedLangIds, $presentLangIds);
     // define which languages of the content pages have to be created
     //        $newLangIds = array_diff($selectedLangIds, $updateLangIds);
     // define which languages of the content pages have to be removed
     $deleteLangIds = array_diff($presentLangIds, $selectedLangIds);
     $langIdsOfAssociatedPagesOfPageNode = array();
     foreach ($presentLangIds as $langId) {
         $langIdsOfAssociatedPagesOfPageNode[$pages[$langId]->getId()] = array_keys($pages[$langId]->getNode()->getPagesByLang());
     }
     foreach ($selectedLangIds as $langId) {
         $page = null;
         $node = null;
         if (isset($pages[$langId])) {
             // Content page already exists, so we will update this very page
             $page = $pages[$langId];
             \DBG::msg("Page in lang {$langId} exists -> Update()");
         } else {
             // Content page of the frontend language $langId doesn't exist yet.
             // Therefore we will either have to create a new node or we will have to
             // find a node to which we can attach our new page.
             // We will prefer the latter.
             \DBG::msg("Page doesn't exist in lang {$langId} -> Create()");
             // Check if there exists already a content page to who's node we could attach our new page
             if (count($pages)) {
                 if ($langId != \FWLanguage::getDefaultLangId() && isset($pages[\FWLanguage::getDefaultLangId()]) && !in_array($langId, $langIdsOfAssociatedPagesOfPageNode[$pages[\FWLanguage::getDefaultLangId()]->getId()])) {
                     // if that is the case, we will attach our new page to this node
                     $node = $pages[\FWLanguage::getDefaultLangId()]->getNode();
                     \DBG::msg("Page does exists in the default language");
                     \DBG::msg("Attach page to node {$node->getId()} of the page of the lang " . \FWLanguage::getDefaultLangId());
                 } else {
                     foreach ($pages as $langIdOfPage => $page) {
                         // Skip the page of the default frontend language - we just checked this page's node before
                         if ($langIdOfPage == \FWLanguage::getDefaultLangId()) {
                             continue;
                         }
                         // Check if there exists a node of the pages in the other languages that don't
                         // have an associated page in the language we're going to create a new page
                         if (!in_array($langId, $langIdsOfAssociatedPagesOfPageNode[$pages[$langIdOfPage]->getId()])) {
                             $node = $pages[$langIdOfPage]->getNode();
                             \DBG::msg("Attach page to node {$node->getId()} of the page of the lang {$langIdOfPage}");
                             break;
                         }
                     }
                 }
             }
             if ($node === null) {
                 \DBG::msg("No node found -> Create()");
                 $root = $this->em->getRepository('Cx\\Core\\ContentManager\\Model\\Entity\\Node')->getRoot();
                 // Create a new node
                 $node = new \Cx\Core\ContentManager\Model\Entity\Node();
                 $node->setParent($root);
                 $this->em->persist($node);
             }
             // Create a new Page
             $page = new \Cx\Core\ContentManager\Model\Entity\Page();
             $page->setNode($node);
             // Set the following attributes only on new pages
             $page->setTitle($this->arrForms[$formId]['lang'][$langId]['name']);
             $page->setDisplay(false);
             $page->setActive(true);
             $page->setLang($langId);
         }
         $page->setType(\Cx\Core\ContentManager\Model\Entity\Page::TYPE_APPLICATION);
         $page->setModule('Contact');
         $page->setCmd($formId);
         $content = $page->getContent();
         //if the content is missing the placeholder {APPLICATION_DATA}, append the placeholder to the page's content.
         if (!preg_match_all('/\\{APPLICATION_DATA\\}/xi', $content, $matches)) {
             $content .= '{APPLICATION_DATA}';
         }
         $page->setContent($content);
         $page->setSourceMode(true);
         $this->em->persist($page);
         // Remember newly created pages. We will need this for the creating of other new pages above in this method.
         if (!isset($pages[$langId])) {
             $pages[$langId] = $page;
             $langIdsOfAssociatedPagesOfPageNode[$pages[$langId]->getId()] = array($langId);
         }
     }
     //$this->em->flush();
     // Delete those content pages of those languages that had been deactivated
     foreach ($deleteLangIds as $langId) {
         $page = $pages[$langId];
         $node = $page->getNode();
         \DBG::msg("Delete page: {$langId}");
         //DBG::dump(count($node->getPages()));
         $this->em->remove($page);
         //$this->em->persist($page);
         //$this->em->flush();
         //DBG::dump(count($node->getPages()));
         // TODO: Delete empty nodes
     }
     $this->em->flush();
 }