TemplateModel.class.php (4822B)
1 <?php 2 namespace cms\model; 3 4 5 use cms\base\DB; 6 7 /** 8 * Templatemodell. Enthält den Template-Sourcecode und die Extension. 9 * 10 * @author: Jan Dankert 11 */ 12 class TemplateModel extends ModelBase 13 { 14 /** 15 * Primary Key. 16 * @var 17 */ 18 public $templatemodelid; 19 20 21 /** 22 * Dateierweiterung dieses Templates (abh?ngig von der Projektvariante) 23 * @type String 24 */ 25 public $extension=''; 26 27 /** 28 * Inhalt des Templates (abh?ngig von der Projektvariante) 29 * @type String 30 */ 31 public $src =''; 32 33 public $modelid; 34 public $templateid; 35 36 /** 37 * @var int 38 */ 39 private $contentid; 40 41 public $public; 42 43 /** 44 * TemplateModel constructor. 45 * @param $templateid 46 * @param $modelid 47 */ 48 function __construct( $templateid,$modelid ) 49 { 50 $this->templateid = $templateid; 51 $this->modelid = $modelid; 52 } 53 54 55 /** 56 * Laden des Templatemodells aus der Datenbank und füllen der Objekteigenschaften. 57 */ 58 function load() 59 { 60 $db = \cms\base\DB::get(); 61 62 $stmt = $db->sql( <<<SQL 63 SELECT {{templatemodel}}.*,{{value}}.text,{{value}}.publish FROM {{templatemodel}} 64 LEFT JOIN {{value}} 65 ON {{value}}.contentid = {{templatemodel}}.contentid AND {{value}}.active = 1 66 WHERE templateid = {templateid} 67 AND projectmodelid = {modelid} 68 SQL 69 ); 70 $stmt->setInt( 'templateid',$this->templateid ); 71 $stmt->setInt( 'modelid' ,$this->modelid ); 72 $row = $stmt->getRow(); 73 74 if ( $row ) 75 { 76 $this->templatemodelid = $row['id' ]; 77 $this->extension = $row['extension']; 78 $this->src = $row['text' ]; 79 $this->contentid = $row['contentid']; 80 $this->public = $row['publish' ]; 81 } 82 else 83 { 84 $this->extension = null; 85 $this->src = null; 86 } 87 } 88 89 90 91 public function loadForPublic() { 92 $this->load(); 93 94 $value = new Value(); 95 $value->contentid = $this->contentid; 96 $value->loadPublished(); 97 $this->src = $value->text; 98 } 99 100 101 public function isPersistent() 102 { 103 return intval( $this->templatemodelid ) > 0; 104 } 105 106 107 108 /** 109 * Abspeichern des Templates in der Datenbank 110 */ 111 public function save() 112 { 113 // Vorlagen-Quelltext existiert für diese Varianten schon. 114 $stmt = Db::sql( <<<SQL 115 UPDATE {{templatemodel}} 116 SET extension={extension} 117 WHERE id={id} 118 SQL 119 ); 120 121 $stmt->setInt ( 'id' ,$this->templatemodelid ); 122 $stmt->setString( 'extension' ,$this->extension ); 123 124 $stmt->execute(); 125 126 $value = new Value(); 127 $value->contentid = $this->contentid; 128 $value->text = $this->src; 129 $value->publish = $this->public; 130 $value->persist(); 131 } 132 133 134 /** 135 * Abspeichern des Templates in der Datenbank 136 */ 137 protected function add() 138 { 139 // Vorlagen-Quelltext wird neu angelegt. 140 $stmt = Db::sql('SELECT MAX(id) FROM {{templatemodel}}'); 141 $nextid = intval($stmt->getOne())+1; 142 143 $content = new Content(); 144 $content->persist(); 145 $this->contentid = $content->getId(); 146 147 $stmt = Db::sql( 'INSERT INTO {{templatemodel}}'. 148 ' (id,contentid,templateid,projectmodelid,extension) '. 149 ' VALUES ({id},{contentid},{templateid},{modelid},{extension}) '); 150 $stmt->setInt ( 'id',$nextid ); 151 152 $stmt->setString( 'extension' ,$this->extension ); 153 $stmt->setInt ( 'contentid' ,$this->contentid ); 154 $stmt->setInt ( 'templateid' ,$this->templateid ); 155 $stmt->setInt ( 'modelid' ,$this->modelid ); 156 157 $stmt->execute(); 158 159 $this->templatemodelid = $nextid; 160 } 161 162 163 /** 164 * Loeschen des Templates 165 * 166 * Entfernen alle Templateinhalte und des Templates selber 167 */ 168 public function delete() 169 { 170 $db = \cms\base\DB::get(); 171 172 $content = new Content( $this->contentid ); 173 $content->delete(); 174 175 $stmt = $db->sql(<<<SQL 176 DELETE FROM {{templatemodel}} 177 WHERE id={id} 178 SQL 179 ); 180 $stmt->setInt( 'id',$this->templatemodelid ); 181 $stmt->execute(); 182 } 183 184 185 /** 186 * Ermittelt den Mime-Type zu diesem Templatemodell. 187 * 188 * Es wird die Extension des Templates betrachtet und dann mit Hilfe der 189 * Konfigurationsdatei 'mime-types.ini' der Mime-Type bestimmt. 190 * 191 * @return String Mime-Type 192 */ 193 public function mimeType() 194 { 195 // Nur den letzten Teil der Extension auswerten: 196 // Aus 'mobile.html' wird nur 'html' verwendet. 197 $parts = explode('.',$this->extension); 198 $extension = strtolower(array_pop($parts)); 199 200 $this->mime_type = File::getMimeType($extension); 201 202 return( $this->mime_type ); 203 } 204 205 206 public function getName() 207 { 208 return ''; 209 } 210 211 212 public function getId() 213 { 214 return $this->templatemodelid; 215 } 216 217 218 /** 219 * @return int 220 */ 221 public function getContentid() 222 { 223 return $this->contentid; 224 } 225 } 226