/**
  * Handles the loading of a page, more in a functional than in an oop style
  *
  * @throws class_exception
  * @return string the generated page
  * @permissions view
  */
 protected function actionGeneratePage()
 {
     //Determine the pagename
     $objPageData = $this->getPageData();
     //react on portaleditor commands
     //pe to display, or pe to disable?
     if ($this->getParam("pe") == "false") {
         $this->objSession->setSession("pe_disable", "true");
     }
     if ($this->getParam("pe") == "true") {
         $this->objSession->setSession("pe_disable", "false");
     }
     //if using the pe, the cache shouldn't be used, otherwise strange things might happen.
     //the system could frighten your cat or eat up all your cheese with marshmallows...
     //get the current state of the portal editor
     $bitPeRequested = false;
     if (class_module_system_setting::getConfigValue("_pages_portaleditor_") == "true" && $this->objSession->getSession("pe_disable") != "true" && $this->objSession->isAdmin() && $objPageData->rightEdit()) {
         $bitPeRequested = true;
     }
     //If we reached up till here, we can begin loading the elements to fill
     if ($bitPeRequested) {
         $arrElementsOnPage = class_module_pages_pageelement::getElementsOnPage($objPageData->getSystemid(), false, $this->getStrPortalLanguage());
     } else {
         $arrElementsOnPage = class_module_pages_pageelement::getElementsOnPage($objPageData->getSystemid(), true, $this->getStrPortalLanguage());
     }
     //If there's a master-page, load elements on that, too
     $objMasterData = class_module_pages_page::getPageByName("master");
     $bitEditPermissionOnMasterPage = false;
     if ($objMasterData != null) {
         if ($bitPeRequested) {
             $arrElementsOnMaster = class_module_pages_pageelement::getElementsOnPage($objMasterData->getSystemid(), false, $this->getStrPortalLanguage());
         } else {
             $arrElementsOnMaster = class_module_pages_pageelement::getElementsOnPage($objMasterData->getSystemid(), true, $this->getStrPortalLanguage());
         }
         //and merge them
         $arrElementsOnPage = array_merge($arrElementsOnPage, $arrElementsOnMaster);
         if ($objMasterData->rightEdit()) {
             $bitEditPermissionOnMasterPage = true;
         }
     }
     //Load the template from the filesystem to get the placeholders
     $strTemplateID = $this->objTemplate->readTemplate("/module_pages/" . $objPageData->getStrTemplate(), "", false, true);
     //bit include the masters-elements!!
     $arrRawPlaceholders = array_merge($this->objTemplate->getElements($strTemplateID, 0), $this->objTemplate->getElements($strTemplateID, 1));
     $arrPlaceholders = array();
     //and retransform
     foreach ($arrRawPlaceholders as $arrOneRawPlaceholder) {
         $arrPlaceholders[] = $arrOneRawPlaceholder["placeholder"];
     }
     //Initialize the caches internal cache :)
     class_cache::fillInternalCache("class_element_portal", $this->getPagename(), null, $this->getStrPortalLanguage());
     //try to load the additional title from cache
     $strAdditionalTitleFromCache = "";
     $intMaxCacheDuration = 0;
     $objCachedTitle = class_cache::getCachedEntry(__CLASS__, $this->getPagename(), $this->generateHash2Sum(), $this->getStrPortalLanguage());
     if ($objCachedTitle != null) {
         $strAdditionalTitleFromCache = $objCachedTitle->getStrContent();
         self::$strAdditionalTitle = $strAdditionalTitleFromCache;
     }
     //copy for the portaleditor
     $arrPlaceholdersFilled = array();
     //Iterate over all elements and pass control to them
     //Get back the filled element
     //Build the array to fill the template
     $arrTemplate = array();
     /** @var class_module_pages_pageelement $objOneElementOnPage */
     foreach ($arrElementsOnPage as $objOneElementOnPage) {
         //element really available on the template?
         if (!in_array($objOneElementOnPage->getStrPlaceholder(), $arrPlaceholders)) {
             //next one, plz
             continue;
         } else {
             //create a protocol of placeholders filled
             //remove from pe-additional-array, pe code is injected by element directly
             $arrPlaceholdersFilled[] = array("placeholder" => $objOneElementOnPage->getStrPlaceholder(), "name" => $objOneElementOnPage->getStrName(), "element" => $objOneElementOnPage->getStrElement(), "repeatable" => $objOneElementOnPage->getIntRepeat());
         }
         //Build the class-name for the object
         /** @var  class_element_portal $objElement  */
         $objElement = $objOneElementOnPage->getConcretePortalInstance();
         //let the element do the work and earn the output
         if (!isset($arrTemplate[$objOneElementOnPage->getStrPlaceholder()])) {
             $arrTemplate[$objOneElementOnPage->getStrPlaceholder()] = "";
         }
         //cache-handling. load element from cache.
         //if the element is re-generated, save it back to cache.
         if (class_module_system_setting::getConfigValue("_pages_cacheenabled_") == "true" && $this->getParam("preview") != "1" && $objPageData->getStrName() != class_module_system_setting::getConfigValue("_pages_errorpage_")) {
             $strElementOutput = "";
             //if the portaleditor is disabled, do the regular cache lookups in storage. otherwise regenerate again and again :)
             if ($bitPeRequested) {
                 $strElementOutput = $objElement->getElementOutput();
             } else {
                 //pe not to be taken into account --> full support of caching
                 $strElementOutput = $objElement->getElementOutputFromCache();
                 if ($objOneElementOnPage->getIntCachetime() > $intMaxCacheDuration) {
                     $intMaxCacheDuration = $objOneElementOnPage->getIntCachetime();
                 }
                 if ($strElementOutput === false) {
                     $strElementOutput = $objElement->getElementOutput();
                     $objElement->saveElementToCache($strElementOutput);
                 }
             }
         } else {
             $strElementOutput = $objElement->getElementOutput();
         }
         //if element is disabled & the pe is requested, wrap the content
         if ($bitPeRequested && $objOneElementOnPage->getIntRecordStatus() == 0) {
             $arrPeElement = array();
             $arrPeElement["title"] = $this->getLang("pe_inactiveElement", "pages") . " (" . $objOneElementOnPage->getStrElement() . ")";
             $strElementOutput = $this->objToolkit->getPeInactiveElement($arrPeElement);
             $strElementOutput = class_element_portal::addPortalEditorSetActiveCode($strElementOutput, $objElement->getSystemid(), array());
         }
         $arrTemplate[$objOneElementOnPage->getStrPlaceholder()] .= $strElementOutput;
     }
     //pe-code to add new elements on unfilled placeholders --> only if pe is visible?
     if ($bitPeRequested) {
         //loop placeholders on template in order to remove already filled ones not being repeatable
         $arrRawPlaceholdersForPe = $arrRawPlaceholders;
         foreach ($arrPlaceholdersFilled as $arrOnePlaceholder) {
             foreach ($arrRawPlaceholdersForPe as &$arrOneRawPlaceholder) {
                 if ($arrOneRawPlaceholder["placeholder"] == $arrOnePlaceholder["placeholder"]) {
                     foreach ($arrOneRawPlaceholder["elementlist"] as $intElementKey => $arrOneRawElement) {
                         if ($arrOnePlaceholder["element"] == $arrOneRawElement["element"]) {
                             if (uniSubstr($arrOneRawElement["name"], 0, 5) == "master") {
                                 $arrOneRawPlaceholder["elementlist"][$intElementKey] = null;
                             } else {
                                 if ($arrOnePlaceholder["repeatable"] == "0") {
                                     $arrOneRawPlaceholder["elementlist"][$intElementKey] = null;
                                 }
                             }
                         }
                     }
                 }
             }
         }
         //array is now set up. loop again to create new-buttons
         $arrPePlaceholdersDone = array();
         $arrPeNewButtons = array();
         foreach ($arrRawPlaceholdersForPe as $arrOneRawPlaceholderForPe) {
             $strPeNewPlaceholder = $arrOneRawPlaceholderForPe["placeholder"];
             foreach ($arrOneRawPlaceholderForPe["elementlist"] as $arrOnePeNewElement) {
                 if ($arrOnePeNewElement == null) {
                     continue;
                 }
                 //check if the linked element exists
                 $objPeNewElement = class_module_pages_element::getElement($arrOnePeNewElement["element"]);
                 if ($objPeNewElement == null) {
                     continue;
                 }
                 //placeholder processed before?
                 $strArrayKey = $strPeNewPlaceholder . $objPeNewElement->getStrName();
                 if (in_array($strArrayKey, $arrPePlaceholdersDone)) {
                     continue;
                 } else {
                     $arrPePlaceholdersDone[] = $strArrayKey;
                 }
                 //create and register the button to add a new element
                 if (!isset($arrPeNewButtons[$strPeNewPlaceholder])) {
                     $arrPeNewButtons[$strPeNewPlaceholder] = "";
                 }
                 if (uniStripos($strArrayKey, "master") !== false) {
                     $strLink = "";
                     if ($objMasterData !== null) {
                         $strLink = class_element_portal::getPortaleditorNewCode($objMasterData->getSystemid(), $strPeNewPlaceholder, $objPeNewElement);
                     }
                 } else {
                     $strLink = class_element_portal::getPortaleditorNewCode($objPageData->getSystemid(), $strPeNewPlaceholder, $objPeNewElement);
                 }
                 $arrPeNewButtons[$strPeNewPlaceholder] .= $strLink;
             }
         }
         //loop pe-new code in order to add the wrappers and assign the code to the matching placeholder
         foreach ($arrPeNewButtons as $strPlaceholderName => $strNewButtons) {
             if (!isset($arrTemplate[$strPlaceholderName])) {
                 $arrTemplate[$strPlaceholderName] = "";
             }
             if ($strNewButtons != "") {
                 $strNewButtons = class_element_portal::getPortaleditorNewWrapperCode($strPlaceholderName, $strNewButtons);
             }
             $arrTemplate[$strPlaceholderName] .= $strNewButtons;
         }
         // add placeholder wrapping
         foreach ($arrTemplate as $strPlaceholder => $strContent) {
             $arrTemplate[$strPlaceholder] = class_carrier::getInstance()->getObjToolkit("portal")->getPePlaceholderWrapper($strPlaceholder, $strContent);
         }
     }
     //check if the additional title has to be saved to the cache
     if (self::$strAdditionalTitle != "" && self::$strAdditionalTitle != $strAdditionalTitleFromCache) {
         $objCacheEntry = class_cache::getCachedEntry(__CLASS__, $this->getPagename(), $this->generateHash2Sum(), $this->getStrPortalLanguage(), true);
         $objCacheEntry->setStrContent(self::$strAdditionalTitle);
         $objCacheEntry->setIntLeasetime(time() + $intMaxCacheDuration);
         $objCacheEntry->updateObjectToDb();
     }
     $arrTemplate["description"] = $objPageData->getStrDesc();
     $arrTemplate["keywords"] = $objPageData->getStrKeywords();
     $arrTemplate["title"] = $objPageData->getStrBrowsername();
     $arrTemplate["additionalTitle"] = self::$strAdditionalTitle;
     $arrTemplate["canonicalUrl"] = class_link::getLinkPortalHref($objPageData->getStrName(), "", $this->getParam("action"), "", $this->getParam("systemid"));
     //Include the $arrGlobal Elements
     $arrGlobal = array();
     $strPath = class_resourceloader::getInstance()->getPathForFile("/portal/global_includes.php");
     if ($strPath !== false) {
         include _realpath_ . $strPath;
     }
     $arrTemplate = array_merge($arrTemplate, $arrGlobal);
     //fill the template. the template was read before
     $strPageContent = $this->fillTemplate($arrTemplate, $strTemplateID);
     $strPageContent = $this->renderPortalEditorCode($objPageData, $bitEditPermissionOnMasterPage, $strPageContent);
     //insert the copyright headers. Due to our licence, you are NOT allowed to remove those lines.
     $strHeader = "<!--\n";
     $strHeader .= "Website powered by Kajona Open Source Content Management Framework\n";
     $strHeader .= "For more information about Kajona see http://www.kajona.de\n";
     $strHeader .= "-->\n";
     $intBodyPos = uniStripos($strPageContent, "</head>");
     $intPosXml = uniStripos($strPageContent, "<?xml");
     if ($intBodyPos !== false) {
         $intBodyPos += 0;
         $strPageContent = uniSubstr($strPageContent, 0, $intBodyPos) . $strHeader . uniSubstr($strPageContent, $intBodyPos);
     } else {
         if ($intPosXml !== false) {
             $intBodyPos = uniStripos($strPageContent, "?>");
             $intBodyPos += 2;
             $strPageContent = uniSubstr($strPageContent, 0, $intBodyPos) . $strHeader . uniSubstr($strPageContent, $intBodyPos);
         } else {
             $strPageContent = $strHeader . $strPageContent;
         }
     }
     return $strPageContent;
 }