File modules/cms/model/TemplateModel.class.php

Last commit: Sat Oct 17 01:14:30 2026 +0200	Jan Dankert	New: Using a structured class for the API-Response. The output for GET-Requests is now without the "output" layer.
1 <?php 2 namespace cms\model; 3 4 5 use cms\base\DB; 6 7 /** 8 * Templatemodel. 9 * 10 * Contains mainly the source code. 11 * For every model exists a template model. 12 * 13 * @author: Jan Dankert 14 */ 15 class TemplateModel extends ModelBase 16 { 17 const FORMAT_MUSTACHE_TEMPLATE = 1; 18 const FORMAT_RATSCRIPT = 2; 19 const FORMAT_RATSCRIPT_TEMPLATE = 3; 20 21 22 /** 23 * Primary Key. 24 * @var 25 */ 26 public $templatemodelid; 27 28 29 /** 30 * Dateierweiterung dieses Templates (abh?ngig von der Projektvariante) 31 * @type String 32 */ 33 public $extension=''; 34 35 /** 36 * Inhalt des Templates (abh?ngig von der Projektvariante) 37 * @type String 38 */ 39 public $src =''; 40 41 public $modelid; 42 public $templateid; 43 44 /** 45 * @var int 46 */ 47 private $contentid; 48 49 public $public; 50 51 /** 52 * The format of the template, f.e. mustache, etc. 53 * See the FORMAT_*-constants in this class. 54 * @var int 55 */ 56 public $format; 57 /** 58 * @var mixed|string 59 */ 60 private string|null $mime_type = null; 61 62 /** 63 * TemplateModel constructor. 64 * @param $templateid 65 * @param $modelid 66 */ 67 function __construct( $templateid,$modelid ) 68 { 69 $this->templateid = $templateid; 70 $this->modelid = $modelid; 71 } 72 73 74 /** 75 * Laden des Templatemodells aus der Datenbank und füllen der Objekteigenschaften. 76 */ 77 function load() 78 { 79 $db = \cms\base\DB::get(); 80 81 $stmt = $db->sql( <<<SQL 82 SELECT {{templatemodel}}.*,{{value}}.text,{{value}}.publish,{{value}}.format FROM {{templatemodel}} 83 LEFT JOIN {{value}} 84 ON {{value}}.contentid = {{templatemodel}}.contentid AND {{value}}.active = 1 85 WHERE templateid = {templateid} 86 AND projectmodelid = {modelid} 87 SQL 88 ); 89 $stmt->setInt( 'templateid',$this->templateid ); 90 $stmt->setInt( 'modelid' ,$this->modelid ); 91 $row = $stmt->getRow(); 92 93 if ( $row ) 94 { 95 $this->templatemodelid = $row['id' ]; 96 $this->extension = $row['extension']; 97 $this->src = $row['text' ]; 98 $this->contentid = $row['contentid']; 99 $this->public = $row['publish' ]; 100 $this->format = $row['format' ]; 101 } 102 else 103 { 104 $this->extension = null; 105 $this->src = null; 106 $this->format = null; 107 } 108 } 109 110 111 112 public function loadForPublic() { 113 $this->load(); 114 115 $value = new Value(); 116 $value->contentid = $this->contentid; 117 $value->loadPublished(); 118 $this->src = $value->text; 119 $this->format = $value->format; 120 } 121 122 123 public function isPersistent() 124 { 125 return intval( $this->templatemodelid ) > 0; 126 } 127 128 129 130 /** 131 * Abspeichern des Templates in der Datenbank 132 */ 133 public function save() 134 { 135 // Vorlagen-Quelltext existiert für diese Varianten schon. 136 $stmt = Db::sql( <<<SQL 137 UPDATE {{templatemodel}} 138 SET extension={extension} 139 WHERE id={id} 140 SQL 141 ); 142 143 $stmt->setInt ( 'id' ,$this->templatemodelid ); 144 $stmt->setString( 'extension',$this->extension ); 145 146 $stmt->execute(); 147 148 $value = new Value(); 149 $value->contentid = $this->contentid; 150 $value->text = $this->src; 151 $value->format = $this->format; 152 $value->publish = $this->public; 153 154 $value->persist(); 155 } 156 157 158 /** 159 * Abspeichern des Templates in der Datenbank 160 */ 161 protected function add() 162 { 163 // Vorlagen-Quelltext wird neu angelegt. 164 $stmt = Db::sql('SELECT MAX(id) FROM {{templatemodel}}'); 165 $nextid = intval($stmt->getOne())+1; 166 167 $content = new Content(); 168 $content->persist(); 169 $this->contentid = $content->getId(); 170 171 $stmt = Db::sql( 'INSERT INTO {{templatemodel}}'. 172 ' (id,contentid,templateid,projectmodelid,extension) '. 173 ' VALUES ({id},{contentid},{templateid},{modelid},{extension}) '); 174 $stmt->setInt ( 'id',$nextid ); 175 176 $stmt->setString( 'extension' ,$this->extension ); 177 $stmt->setInt ( 'contentid' ,$this->contentid ); 178 $stmt->setInt ( 'templateid' ,$this->templateid ); 179 $stmt->setInt ( 'modelid' ,$this->modelid ); 180 181 $stmt->execute(); 182 183 $this->templatemodelid = $nextid; 184 } 185 186 187 /** 188 * Loeschen des Templates 189 * 190 * Entfernen alle Templateinhalte und des Templates selber 191 */ 192 public function delete() 193 { 194 $db = \cms\base\DB::get(); 195 196 $content = new Content( $this->contentid ); 197 $content->delete(); 198 199 $stmt = $db->sql(<<<SQL 200 DELETE FROM {{templatemodel}} 201 WHERE id={id} 202 SQL 203 ); 204 $stmt->setInt( 'id',$this->templatemodelid ); 205 $stmt->execute(); 206 } 207 208 209 /** 210 * Ermittelt den Mime-Type zu diesem Templatemodell. 211 * 212 * Es wird die Extension des Templates betrachtet und dann mit Hilfe der 213 * Konfigurationsdatei 'mime-types.ini' der Mime-Type bestimmt. 214 * 215 * @return String Mime-Type 216 */ 217 public function mimeType() 218 { 219 if ( $this->mime_type ) 220 return $this->mime_type; 221 222 // Nur den letzten Teil der Extension auswerten: 223 // Aus 'mobile.html' wird nur 'html' verwendet. 224 $parts = explode('.',$this->extension); 225 $extension = strtolower(array_pop($parts)); 226 227 $this->mime_type = File::getMimeType($extension); 228 229 return( $this->mime_type ); 230 } 231 232 233 public function getName() 234 { 235 return ''; 236 } 237 238 239 public function getId() 240 { 241 return $this->templatemodelid; 242 } 243 244 245 /** 246 * @return int 247 */ 248 public function getContentid() 249 { 250 return $this->contentid; 251 } 252 253 254 /** 255 * @return int 256 */ 257 public function getFormat() 258 { 259 if ( $this->format ) 260 return $this->format; 261 else 262 return self::FORMAT_MUSTACHE_TEMPLATE; 263 } 264 265 266 /** 267 * @return string 268 */ 269 public function getSource() 270 { 271 return $this->src; 272 } 273 } 274
Download modules/cms/model/TemplateModel.class.php
History Sat, 17 Oct 2026 01:14:30 +0200 Jan Dankert New: Using a structured class for the API-Response. The output for GET-Requests is now without the "output" layer. Sat, 28 Jan 2023 19:10:47 +0100 Jan Dankert New: Templates may be rendered as Mustache, Script and Script template. Sat, 27 Nov 2021 04:38:28 +0100 Jan Dankert New: Option to explicitly release a new template source. Sat, 27 Nov 2021 00:11:56 +0100 Jan Dankert New: History for files and templates. Tue, 9 Nov 2021 23:52:56 +0100 Jan Dankert Some fixes for reading content from the new content table. Tue, 9 Nov 2021 00:35:42 +0100 Jan Dankert Fixes: Reading and writing template sources with the new content table. Mon, 8 Nov 2021 01:05:19 +0100 Jan Dankert Refactoring: Reading values from the new content table. Sun, 7 Mar 2021 00:10:20 +0100 Jan Dankert Refactoring: Hopefully more performance while accessing the database resultsets. Wed, 18 Nov 2020 01:46:36 +0100 Jan Dankert Refactoring of model classes: New method persist() and some other cleanups. Fri, 23 Oct 2020 23:09:52 +0200 Jan Dankert Refactoring: Using the new config classes. Fri, 2 Oct 2020 23:11:48 +0200 Jan Dankert Cleanup: No '.inputholder' any more, notices with links to objects. Sat, 26 Sep 2020 10:32:02 +0200 Jan Dankert Refactoring: No global $conf array any more. Sat, 26 Sep 2020 02:26:39 +0200 Jan Dankert Refactoring: No global functions any more, the database object is read from the Db class. Fri, 25 Sep 2020 01:01:41 +0200 Jan Dankert Fix: The templatemodel source must be not null. Wed, 23 Sep 2020 01:04:05 +0200 Jan Dankert Cleanup of deprecated methods and deprecated class attributes. Sun, 23 Feb 2020 04:01:30 +0100 Jan Dankert Refactoring with Namespaces for the cms modules, part 1: moving.