File modules/cms/generator/PageGenerator.class.php
Last commit: Wed Feb 15 00:22:28 2023 +0100 Jan Dankert Fix: Changing the Longtext format was not possible.
1 <?php 2 3 4 namespace cms\generator; 5 6 7 use cms\base\Configuration; 8 use cms\generator\PageContext; 9 use cms\model\File; 10 use cms\model\Folder; 11 use cms\model\Language; 12 use cms\model\Link; 13 use cms\model\Page; 14 use cms\model\Project; 15 use cms\model\Template; 16 use cms\model\TemplateModel; 17 use cms\model\Value; 18 use logger\Logger; 19 use template_engine\components\html\component_else\ElseComponent; 20 use util\exception\GeneratorException; 21 use util\exception\ObjectNotFoundException; 22 use util\Mustache; 23 use util\Text; 24 use util\text\TextMessage; 25 26 27 class PageGenerator extends BaseGenerator 28 { 29 30 /** 31 * PageGenerator constructor. 32 * @param $pageContext PageContext 33 */ 34 public function __construct($pageContext ) 35 { 36 $this->context = $pageContext; 37 } 38 39 40 41 /** 42 * Erzeugen der Inhalte zu allen Elementen dieser Seite 43 * wird von generate() aufgerufen. 44 45 * @param $page Page page 46 * @return array 47 * @throws GeneratorException 48 */ 49 protected function generatePageElements( $page ) 50 { 51 $values = array(); 52 53 $elements = $page->getTemplate()->getElements(); 54 55 foreach( $elements as $elementid=>$element ) 56 { 57 // neues Inhaltobjekt erzeugen 58 $valueContext = new ValueContext($this->context); 59 $valueContext->elementid = $elementid; 60 $valueGenerator = new ValueGenerator( $valueContext ); 61 try { 62 $values[$elementid] = $valueGenerator->getCache()->get(); 63 } catch( \Exception $e ) { 64 // Unrecoverable Error while generating the content. 65 Logger::info( $e ); 66 67 throw new GeneratorException("Element '".$element->name."' could not be generated.",$e); 68 } 69 70 } 71 72 return $values; 73 } 74 75 76 77 /** 78 * Erzeugen des Inhaltes der gesamten Seite. 79 * 80 * @return String Inhalt 81 * @throws GeneratorException|ObjectNotFoundException 82 */ 83 private function generatePageValue() 84 { 85 // Setzen der 'locale', damit sprachabhängige Systemausgaben (wie z.B. die 86 // Ausgabe von strftime()) in der korrekten Sprache dargestellt werden. 87 $language = new Language($this->context->languageId); 88 $language->load(); 89 90 $language->setCurrentLocale(); 91 92 $page = new Page( $this->context->objectId ); 93 $page->load(); 94 95 $tplGenerator = new TemplateGenerator( $page->templateid,$this->context->modelId, $this->context->scheme ); 96 97 try { 98 $pageValues = $this->generatePageElements($page); // generating the value of all page elements. 99 } 100 catch( \Exception $e ) { 101 throw new GeneratorException("Page '".$page->getName()."' could not be generated.",$e); 102 } 103 104 // Template should have access to the page properties. 105 // Template should have access to the settings of this node object. 106 $data = []; 107 $data['_page' ] = $page->getProperties() ; 108 $data['_localsettings'] = $page->getSettings() ; 109 $data['_settings' ] = $page->getTotalSettings(); 110 111 foreach( $page->getTemplate()->getElements() as $elementId=>$element ) 112 $data[ $element->name ] = $pageValues[$elementId]; 113 114 return $tplGenerator->generateValue( $data ); 115 } 116 117 protected function generate() 118 { 119 try { 120 return $this->generatePageValue(); 121 } catch( GeneratorException $e ) { 122 123 throw new GeneratorException('Could not render page '.$this->context->getObjectId(),$e ); 124 } 125 126 } 127 128 129 /** 130 * Creating the public filename of a page. 131 * 132 * @return string 133 * @throws \util\exception\ObjectNotFoundException 134 */ 135 public function getPublicFilename() 136 { 137 $page = new Page( $this->context->sourceObjectId ); 138 $page->load(); 139 140 $parentFolder = new Folder( $page->parentid ); 141 $parentFolder->load(); 142 143 $project = $page->getProject(); 144 $project->load(); 145 146 $publishConfig = Configuration::subset('publish'); 147 $format = $publishConfig->get('format','{filename}{language_sep}{language}{type_sep}{type}'); 148 $format = str_replace('{filename}',$page->filename(),$format ); 149 150 $allLanguages = $project->getLanguageIds(); 151 $allModels = $project->getModelIds(); 152 153 $withLanguage = count($allLanguages) > 1 || $publishConfig->get('filename_language','auto' ) == 'always'; 154 $withModel = count($allModels ) > 1 || $publishConfig->get('filename_type' ,'always' ) == 'always'; 155 156 $languagePart = ''; 157 $typePart = ''; 158 159 if ( $withLanguage ) { 160 $l = new Language( $this->context->languageId ); 161 $l->load(); 162 163 $languagePart = $l->isoCode; 164 } 165 166 if ( $withModel ) { 167 $templateModel = new TemplateModel( $page->templateid, $this->context->modelId ); 168 $templateModel->load(); 169 170 $typePart = $templateModel->extension; 171 } 172 173 $languageSep = $languagePart? $publishConfig->get('language_sep','.') :''; 174 $typeSep = $typePart ? $publishConfig->get('type_sep' ,'.') :''; 175 176 $format = str_replace('{language}' ,$languagePart ,$format ); 177 $format = str_replace('{language_sep}',$languageSep ,$format ); 178 $format = str_replace('{type}' ,$typePart ,$format ); 179 $format = str_replace('{type_sep}' ,$typeSep ,$format ); 180 181 return $page->path().'/'.$format; 182 } 183 184 185 186 public function getMimeType() 187 { 188 $page = new Page( $this->context->sourceObjectId ); 189 $page->load(); 190 $templateModel = new TemplateModel( $page->templateid,$this->context->modelId ); 191 $templateModel->load(); 192 193 return $templateModel->mimeType(); 194 } 195 }
Downloadmodules/cms/generator/PageGenerator.class.php
History Wed, 15 Feb 2023 00:22:28 +0100 Jan Dankert Fix: Changing the Longtext format was not possible. Sat, 28 Jan 2023 19:10:47 +0100 Jan Dankert New: Templates may be rendered as Mustache, Script and Script template. Sat, 2 Jul 2022 00:37:22 +0200 Jan Dankert Fix: Public Filename must consider the filename style. Fri, 18 Mar 2022 22:38:42 +0100 dankert Refactoring: Extracted the TemplateGenerator out of the PageGenerator. Fri, 11 Mar 2022 19:26:18 +0100 dankert Performance: Do "print_r" only in development mode. Sun, 5 Dec 2021 22:26:39 +0100 dankert Fixed a type in template source of new projects. Sun, 5 Dec 2021 20:33:24 +0100 dankert Cleanup: Removed unusable properties from class 'Value' and 'BaseObject'. Tue, 9 Nov 2021 23:52:56 +0100 Jan Dankert Some fixes for reading content from the new content table. Sun, 14 Mar 2021 02:23:39 +0100 Jan Dankert Fix: Pasted crap... Sun, 14 Mar 2021 02:14:31 +0100 Jan Dankert Fix: The public filename of files must contain their path... Thu, 4 Mar 2021 02:24:45 +0100 Jan Dankert New: The calculation of the mime types should be done in the generators. Sat, 27 Feb 2021 02:10:16 +0100 Jan Dankert Fix: Using the correct object-id for generating the page. Sun, 1 Nov 2020 03:08:55 +0100 Jan Dankert Replaced the calls to "Configuration::rawConfig()" with the OO style calls; Cleanup LoginAction. Sun, 1 Nov 2020 00:36:50 +0100 Jan Dankert Refactoring: Only using the configuration object. Mon, 26 Oct 2020 22:21:42 +0100 Jan Dankert Refactoring: Using TextMessage for creating Messages with user content. Tue, 29 Sep 2020 22:17:11 +0200 Jan Dankert Refactoring: Do not use global constants. Sat, 26 Sep 2020 10:32:02 +0200 Jan Dankert Refactoring: No global $conf array any more. Sat, 26 Sep 2020 04:26:55 +0200 Jan Dankert Refactoring: read configuration values with a class. Wed, 23 Sep 2020 01:04:05 +0200 Jan Dankert Cleanup of deprecated methods and deprecated class attributes. Mon, 21 Sep 2020 22:48:59 +0200 Jan Dankert Complexe refactoring: Moving all generation logic from the model (Value,Page,File) to generators classes.