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

Last commit: Sat Jan 28 19:10:47 2023 +0100	Jan Dankert	New: Templates may be rendered as Mustache, Script and Script template.
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 /** 59 * TemplateModel constructor. 60 * @param $templateid 61 * @param $modelid 62 */ 63 function __construct( $templateid,$modelid ) 64 { 65 $this->templateid = $templateid; 66 $this->modelid = $modelid; 67 } 68 69 70 /** 71 * Laden des Templatemodells aus der Datenbank und füllen der Objekteigenschaften. 72 */ 73 function load() 74 { 75 $db = \cms\base\DB::get(); 76 77 $stmt = $db->sql( <<<SQL 78 SELECT {{templatemodel}}.*,{{value}}.text,{{value}}.publish,{{value}}.format FROM {{templatemodel}} 79 LEFT JOIN {{value}} 80 ON {{value}}.contentid = {{templatemodel}}.contentid AND {{value}}.active = 1 81 WHERE templateid = {templateid} 82 AND projectmodelid = {modelid} 83 SQL 84 ); 85 $stmt->setInt( 'templateid',$this->templateid ); 86 $stmt->setInt( 'modelid' ,$this->modelid ); 87 $row = $stmt->getRow(); 88 89 if ( $row ) 90 { 91 $this->templatemodelid = $row['id' ]; 92 $this->extension = $row['extension']; 93 $this->src = $row['text' ]; 94 $this->contentid = $row['contentid']; 95 $this->public = $row['publish' ]; 96 $this->format = $row['format' ]; 97 } 98 else 99 { 100 $this->extension = null; 101 $this->src = null; 102 $this->format = null; 103 } 104 } 105 106 107 108 public function loadForPublic() { 109 $this->load(); 110 111 $value = new Value(); 112 $value->contentid = $this->contentid; 113 $value->loadPublished(); 114 $this->src = $value->text; 115 $this->format = $value->format; 116 } 117 118 119 public function isPersistent() 120 { 121 return intval( $this->templatemodelid ) > 0; 122 } 123 124 125 126 /** 127 * Abspeichern des Templates in der Datenbank 128 */ 129 public function save() 130 { 131 // Vorlagen-Quelltext existiert für diese Varianten schon. 132 $stmt = Db::sql( <<<SQL 133 UPDATE {{templatemodel}} 134 SET extension={extension} 135 WHERE id={id} 136 SQL 137 ); 138 139 $stmt->setInt ( 'id' ,$this->templatemodelid ); 140 $stmt->setString( 'extension',$this->extension ); 141 142 $stmt->execute(); 143 144 $value = new Value(); 145 $value->contentid = $this->contentid; 146 $value->text = $this->src; 147 $value->format = $this->format; 148 $value->publish = $this->public; 149 150 $value->persist(); 151 } 152 153 154 /** 155 * Abspeichern des Templates in der Datenbank 156 */ 157 protected function add() 158 { 159 // Vorlagen-Quelltext wird neu angelegt. 160 $stmt = Db::sql('SELECT MAX(id) FROM {{templatemodel}}'); 161 $nextid = intval($stmt->getOne())+1; 162 163 $content = new Content(); 164 $content->persist(); 165 $this->contentid = $content->getId(); 166 167 $stmt = Db::sql( 'INSERT INTO {{templatemodel}}'. 168 ' (id,contentid,templateid,projectmodelid,extension) '. 169 ' VALUES ({id},{contentid},{templateid},{modelid},{extension}) '); 170 $stmt->setInt ( 'id',$nextid ); 171 172 $stmt->setString( 'extension' ,$this->extension ); 173 $stmt->setInt ( 'contentid' ,$this->contentid ); 174 $stmt->setInt ( 'templateid' ,$this->templateid ); 175 $stmt->setInt ( 'modelid' ,$this->modelid ); 176 177 $stmt->execute(); 178 179 $this->templatemodelid = $nextid; 180 } 181 182 183 /** 184 * Loeschen des Templates 185 * 186 * Entfernen alle Templateinhalte und des Templates selber 187 */ 188 public function delete() 189 { 190 $db = \cms\base\DB::get(); 191 192 $content = new Content( $this->contentid ); 193 $content->delete(); 194 195 $stmt = $db->sql(<<<SQL 196 DELETE FROM {{templatemodel}} 197 WHERE id={id} 198 SQL 199 ); 200 $stmt->setInt( 'id',$this->templatemodelid ); 201 $stmt->execute(); 202 } 203 204 205 /** 206 * Ermittelt den Mime-Type zu diesem Templatemodell. 207 * 208 * Es wird die Extension des Templates betrachtet und dann mit Hilfe der 209 * Konfigurationsdatei 'mime-types.ini' der Mime-Type bestimmt. 210 * 211 * @return String Mime-Type 212 */ 213 public function mimeType() 214 { 215 // Nur den letzten Teil der Extension auswerten: 216 // Aus 'mobile.html' wird nur 'html' verwendet. 217 $parts = explode('.',$this->extension); 218 $extension = strtolower(array_pop($parts)); 219 220 $this->mime_type = File::getMimeType($extension); 221 222 return( $this->mime_type ); 223 } 224 225 226 public function getName() 227 { 228 return ''; 229 } 230 231 232 public function getId() 233 { 234 return $this->templatemodelid; 235 } 236 237 238 /** 239 * @return int 240 */ 241 public function getContentid() 242 { 243 return $this->contentid; 244 } 245 246 247 /** 248 * @return int 249 */ 250 public function getFormat() 251 { 252 if ( $this->format ) 253 return $this->format; 254 else 255 return self::FORMAT_MUSTACHE_TEMPLATE; 256 } 257 258 259 /** 260 * @return string 261 */ 262 public function getSource() 263 { 264 return $this->src; 265 } 266 } 267
Download modules/cms/model/TemplateModel.class.php
History 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.