openrat-cms

# OpenRat Content Management System
git clone http://git.code.weiherhei.de/openrat-cms.git
Log | Files | Refs

TemplateModel.class.php (4158B)


      1 <?php
      2 namespace cms\model;
      3 
      4 
      5 /**
      6  * Templatemodell. Enthält den Template-Sourcecode und die Extension.
      7  *
      8  * @author: Jan Dankert
      9  */
     10 class TemplateModel extends ModelBase
     11 {
     12     /**
     13      * Primary Key.
     14      * @var
     15      */
     16     public $templatemodelid;
     17 
     18 
     19     /**
     20 	 * Dateierweiterung dieses Templates (abh?ngig von der Projektvariante)
     21 	 * @type String
     22 	 */
     23 	public $extension='';
     24 
     25 	/**
     26 	 * Inhalt des Templates (abh?ngig von der Projektvariante)
     27 	 * @type String
     28 	 */
     29 	public $src      ='';
     30 
     31     public $modelid;
     32     public $templateid;
     33 
     34 
     35     /**
     36      * TemplateModel constructor.
     37      * @param $templateid
     38      * @param $modelid
     39      */
     40 	function __construct( $templateid,$modelid )
     41 	{
     42 	    $this->templateid = $templateid;
     43 	    $this->modelid    = $modelid;
     44 	}
     45 
     46 
     47 	/**
     48  	 * Laden des Templatemodells aus der Datenbank und füllen der Objekteigenschaften.
     49  	 */
     50 	function load()
     51 	{
     52 		$db = db_connection();
     53 
     54 		$stmt = $db->sql( 'SELECT * FROM {{templatemodel}}'.
     55 		                ' WHERE templateid={templateid}'.
     56 		                '   AND projectmodelid={modelid}' );
     57 		$stmt->setInt( 'templateid',$this->templateid );
     58 		$stmt->setInt( 'modelid'   ,$this->modelid    );
     59 		$row = $stmt->getRow();
     60 
     61 		if	( isset($row['id']) )
     62 		{
     63 			$this->templatemodelid = $row['id'];
     64 			$this->extension       = $row['extension'];
     65 			$this->src             = $row['text'];
     66 		}
     67 		else
     68 		{
     69 			$this->extension = null;
     70 			$this->src       = null;
     71 		}
     72 	}
     73 
     74 
     75 
     76 	public function isPersistent()
     77     {
     78 	    return intval( $this->templatemodelid ) > 0;
     79     }
     80 
     81 
     82 
     83     /**
     84  	 * Abspeichern des Templates in der Datenbank
     85  	 */
     86 	function save()
     87 	{
     88 		$db = db_connection();
     89 
     90         // Vorlagen-Quelltext existiert für diese Varianten schon.
     91         $stmt = $db->sql( 'UPDATE {{templatemodel}}'.
     92                         '  SET extension={extension},'.
     93                         '      text={src} '.
     94                         ' WHERE id={id}' );
     95 
     96         $stmt->setInt   ( 'id'    ,$this->templatemodelid        );
     97 
     98         $stmt->setString( 'extension'     ,$this->extension      );
     99         $stmt->setString( 'src'           ,$this->src            );
    100 
    101 		$stmt->query();
    102 	}
    103 
    104 
    105 	/**
    106  	 * Abspeichern des Templates in der Datenbank
    107  	 */
    108 	function add()
    109 	{
    110 		$db = db_connection();
    111 
    112         // Vorlagen-Quelltext wird neu angelegt.
    113         $stmt = $db->sql('SELECT MAX(id) FROM {{templatemodel}}');
    114         $nextid = intval($stmt->getOne())+1;
    115 
    116         $stmt = $db->sql( 'INSERT INTO {{templatemodel}}'.
    117                         '        (id,templateid,projectmodelid,extension,text) '.
    118                         ' VALUES ({id},{templateid},{modelid},{extension},{src}) ');
    119         $stmt->setInt   ( 'id',$nextid         );
    120 
    121 		$stmt->setString( 'extension'     ,$this->extension      );
    122 		$stmt->setString( 'src'           ,$this->src            );
    123 		$stmt->setInt   ( 'templateid'    ,$this->templateid     );
    124 		$stmt->setInt   ( 'modelid'       ,$this->modelid        );
    125 
    126 		$stmt->query();
    127 
    128         $this->templatemodelid = $nextid;
    129     }
    130 
    131 
    132 	/**
    133  	 * Loeschen des Templates
    134  	 *
    135  	 * Entfernen alle Templateinhalte und des Templates selber
    136  	 */
    137 	public function delete()
    138 	{
    139 		$db = db_connection();
    140 		
    141 		$stmt = $db->sql( 'DELETE FROM {{templatemodel}}'.
    142 		                ' WHERE id={id}' );
    143 		$stmt->setInt( 'id',$this->templatemodelid );
    144 		$stmt->query();
    145 	}
    146 	
    147 	
    148 	/**
    149 	 * Ermittelt den Mime-Type zu diesem Templatemodell.
    150 	 * 
    151 	 * Es wird die Extension des Templates betrachtet und dann mit Hilfe der
    152 	 * Konfigurationsdatei 'mime-types.ini' der Mime-Type bestimmt. 
    153 	 *
    154 	 * @return String Mime-Type  
    155 	 */
    156 	public function mimeType()
    157 	{
    158 		global $conf;
    159 		$mime_types = $conf['mime-types'];
    160 
    161 		// Nur den letzten Teil der Extension auswerten:
    162 		// Aus 'mobile.html' wird nur 'html' verwendet.
    163 		$parts = explode('.',$this->extension);
    164 		$extension = strtolower(array_pop($parts));
    165 
    166 		if	( !empty($mime_types[$extension]) )
    167 			$this->mime_type = $mime_types[$extension];
    168 		else
    169 			// Wenn kein Mime-Type gefunden, dann Standardwert setzen
    170 			$this->mime_type = 'application/octet-stream';
    171 			
    172 		return( $this->mime_type );
    173 	}
    174 
    175 
    176     public function getName()
    177     {
    178         return '';
    179     }
    180 
    181 }
    182