PageGenerator.class.php (4605B)
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\Page; 13 use cms\model\Project; 14 use cms\model\Template; 15 use cms\model\TemplateModel; 16 use cms\model\Value; 17 use logger\Logger; 18 use util\exception\GeneratorException; 19 use util\Mustache; 20 use util\text\TextMessage; 21 22 23 class PageGenerator extends BaseGenerator 24 { 25 26 /** 27 * PageGenerator constructor. 28 * @param $pageContext PageContext 29 */ 30 public function __construct($pageContext ) 31 { 32 $this->context = $pageContext; 33 } 34 35 36 37 /** 38 * Erzeugen der Inhalte zu allen Elementen dieser Seite 39 * wird von generate() aufgerufen. 40 41 * @param $page Page page 42 * @return array 43 * @throws GeneratorException 44 */ 45 protected function generatePageElements( $page ) 46 { 47 $values = array(); 48 49 $elements = $page->getTemplate()->getElements(); 50 51 foreach( $elements as $elementid=>$element ) 52 { 53 // neues Inhaltobjekt erzeugen 54 $valueContext = new ValueContext($this->context); 55 $valueContext->elementid = $elementid; 56 $valueGenerator = new ValueGenerator( $valueContext ); 57 try { 58 $values[$elementid] = $valueGenerator->getCache()->get(); 59 } catch( \Exception $e ) { 60 // Unrecoverable Error while generating the content. 61 throw new GeneratorException('Could not generate Value',$e ); 62 } 63 64 } 65 66 return $values; 67 } 68 69 70 71 /** 72 * Erzeugen des Inhaltes der gesamten Seite. 73 * 74 * @return String Inhalt 75 */ 76 private function generatePageValue() 77 { 78 // Setzen der 'locale', damit sprachabhängige Systemausgaben (wie z.B. die 79 // Ausgabe von strftime()) in der korrekten Sprache dargestellt werden. 80 $language = new Language($this->context->languageId); 81 $language->load(); 82 83 $language->setCurrentLocale(); 84 85 $page = new Page( $this->context->objectId ); 86 $page->load(); 87 88 $tplGenerator = new TemplateGenerator( $page->templateid,$this->context->modelId, $this->context->scheme ); 89 90 $pageValues = $this->generatePageElements( $page ); // generating the value of all page elements. 91 92 // Template should have access to the page properties. 93 // Template should have access to the settings of this node object. 94 $data = []; 95 $data['_page' ] = $page->getProperties() ; 96 $data['_localsettings'] = $page->getSettings() ; 97 $data['_settings' ] = $page->getTotalSettings(); 98 99 foreach( $page->getTemplate()->getElements() as $elementId=>$element ) 100 $data[ $element->name ] = $pageValues[$elementId]; 101 102 return $tplGenerator->generateValue( $data ); 103 } 104 105 protected function generate() 106 { 107 return $this->generatePageValue(); 108 } 109 110 111 /** 112 * Creating the public filename of a page. 113 * 114 * @return string 115 * @throws \util\exception\ObjectNotFoundException 116 */ 117 public function getPublicFilename() 118 { 119 $page = new Page( $this->context->sourceObjectId ); 120 $page->load(); 121 122 $parentFolder = new Folder( $page->parentid ); 123 $parentFolder->load(); 124 125 $project = $page->getProject(); 126 $project->load(); 127 128 $publishConfig = Configuration::subset('publish'); 129 $format = $publishConfig->get('format','{filename}{language_sep}{language}{type_sep}{type}'); 130 $format = str_replace('{filename}',$page->filename(),$format ); 131 132 $allLanguages = $project->getLanguageIds(); 133 $allModels = $project->getModelIds(); 134 135 $withLanguage = count($allLanguages) > 1 || $publishConfig->get('filename_language','auto' ) == 'always'; 136 $withModel = count($allModels ) > 1 || $publishConfig->get('filename_type' ,'always' ) == 'always'; 137 138 $languagePart = ''; 139 $typePart = ''; 140 141 if ( $withLanguage ) { 142 $l = new Language( $this->context->languageId ); 143 $l->load(); 144 145 $languagePart = $l->isoCode; 146 } 147 148 if ( $withModel ) { 149 $templateModel = new TemplateModel( $page->templateid, $this->context->modelId ); 150 $templateModel->load(); 151 152 $typePart = $templateModel->extension; 153 } 154 155 $languageSep = $languagePart? $publishConfig->get('language_sep','.') :''; 156 $typeSep = $typePart ? $publishConfig->get('type_sep' ,'.') :''; 157 158 $format = str_replace('{language}' ,$languagePart ,$format ); 159 $format = str_replace('{language_sep}',$languageSep ,$format ); 160 $format = str_replace('{type}' ,$typePart ,$format ); 161 $format = str_replace('{type_sep}' ,$typeSep ,$format ); 162 163 return $page->path().'/'.$format; 164 } 165 166 167 168 public function getMimeType() 169 { 170 $page = new Page( $this->context->sourceObjectId ); 171 $page->load(); 172 $templateModel = new TemplateModel( $page->templateid,$this->context->modelId ); 173 $templateModel->load(); 174 175 return $templateModel->mimeType(); 176 } 177 }