openrat-cms

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

Template.class.php (9969B)


      1 <?php
      2 namespace cms\model;
      3 // OpenRat Content Management System
      4 // Copyright (C) 2002-2012 Jan Dankert, cms@jandankert.de
      5 //
      6 // This program is free software; you can redistribute it and/or
      7 // modify it under the terms of the GNU General Public License
      8 // as published by the Free Software Foundation; either version 2
      9 // of the License, or (at your option) any later version.
     10 //
     11 // This program is distributed in the hope that it will be useful,
     12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14 // GNU General Public License for more details.
     15 //
     16 // You should have received a copy of the GNU General Public License
     17 // along with this program; if not, write to the Free Software
     18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
     19 
     20 
     21 /**
     22  * Logische Darstellung eines Templates
     23  *
     24  * @author: $Author$
     25  * @version: $Revision$
     26  * @package openrat.objects
     27  */ 
     28 class Template extends ModelBase
     29 {
     30 	/**
     31 	 * ID dieses Templates
     32 	 * @type Integer
     33 	 */
     34 	var $templateid = 0;
     35 
     36 	/**
     37 	 * Projekt-ID des aktuell ausgew?hlten Projektes
     38 	 * @type Integer
     39 	 */
     40 	var $projectid = 0;
     41 
     42 	/**
     43 	 * Logischer Name
     44 	 * @type String
     45 	 */
     46 	var $name = 'unnamed';
     47 	
     48 	/**
     49 	 * ID der Projektvariante
     50      * @deprecated Zugriff über TemplateModel
     51 	 * @type Integer
     52 	 */
     53 	var $modelid = 0;
     54 
     55 	/**
     56 	 * Dateierweiterung dieses Templates (abh?ngig von der Projektvariante)
     57      * @deprecated Zugriff über TemplateModel
     58 	 * @type String
     59 	 */
     60 	var $extension='';
     61 
     62 	/**
     63 	 * Inhalt des Templates (abh?ngig von der Projektvariante)
     64      * @deprecated Zugriff über TemplateModel
     65 	 * @type String
     66 	 */
     67 	var $src='';
     68 	
     69 	// Konstruktor
     70 	function __construct( $templateid='' )
     71 	{
     72 		if   ( is_numeric($templateid) )
     73 			$this->templateid = $templateid;
     74 	}
     75 
     76 
     77 	/**
     78  	 * Laden des Templates aus der Datenbank und f?llen der Objekteigenschaften
     79  	 */
     80 	function load()
     81 	{
     82 		$stmt = db()->sql( 'SELECT * FROM {{template}}'.
     83 		                ' WHERE id={templateid}' );
     84 		$stmt->setInt( 'templateid',$this->templateid );
     85 		$row = $stmt->getRow();
     86 		
     87 		if	( empty($row) )
     88 			throw new \ObjectNotFoundException("Template not found: ".$this->templateid);
     89 
     90 		$this->name      = $row['name'     ];
     91 		$this->projectid = $row['projectid'];
     92 
     93 		$templateModel = new TemplateModel( $this->templateid, $this->modelid );
     94 		$templateModel->load();
     95 
     96         $this->extension = $templateModel->extension;
     97         $this->src       = $templateModel->src;
     98 	}
     99 
    100 
    101 	/**
    102  	 * Abspeichern des Templates in der Datenbank
    103  	 */
    104 	function save()
    105 	{
    106 		if	( $this->name == "" )
    107 			$this->name = lang('GLOBAL_TEMPLATE').' #'.$this->templateid;
    108 
    109 		$db = db_connection();
    110 
    111 		$stmt = $db->sql( 'UPDATE {{template}}'.
    112 		                '  SET name={name}'.
    113 		                '  WHERE id={templateid}' );
    114 		$stmt->setString( 'name'      ,$this->name       );
    115 		$stmt->setInt   ( 'templateid',$this->templateid );
    116 		$stmt->query();
    117 
    118 		$stmt = $db->sql( 'SELECT COUNT(*) FROM {{templatemodel}}'.
    119 		                ' WHERE templateid={templateid}'.
    120 		                '   AND projectmodelid={modelid}' );
    121 		$stmt->setInt   ( 'templateid'    ,$this->templateid     );
    122 		$stmt->setInt   ( 'modelid'       ,$this->modelid );
    123 
    124 		if	( intval($stmt->getOne()) > 0 )
    125 		{
    126 			// Vorlagen-Quelltext existiert für diese Varianten schon.
    127 			$stmt = $db->sql( 'UPDATE {{templatemodel}}'.
    128 			                '  SET extension={extension},'.
    129 			                '      text={src} '.
    130 			                ' WHERE templateid={templateid}'.
    131 			                '   AND projectmodelid={modelid}' );
    132 		}
    133 		else
    134 		{
    135 			// Vorlagen-Quelltext wird für diese Varianten neu angelegt.
    136 			$stmt = $db->sql('SELECT MAX(id) FROM {{templatemodel}}');
    137 			$nextid = intval($stmt->getOne())+1;
    138 
    139 			$stmt = $db->sql( 'INSERT INTO {{templatemodel}}'.
    140 			                '        (id,templateid,projectmodelid,extension,text) '.
    141 			                ' VALUES ({id},{templateid},{modelid},{extension},{src}) ');
    142 			$stmt->setInt   ( 'id',$nextid         );
    143 		}
    144 
    145 		$stmt->setString( 'extension'     ,$this->extension      );
    146 		$stmt->setString( 'src'           ,$this->src            );
    147 		$stmt->setInt   ( 'templateid'    ,$this->templateid     );
    148 		$stmt->setInt   ( 'modelid'       ,$this->modelid        );
    149 		
    150 		$stmt->query();
    151 	}
    152 
    153 
    154 	/**
    155 	  * Es werden Templates mit einem Inhalt gesucht
    156 	  * @param String Suchbegriff
    157 	  * @return array Liste der gefundenen Template-IDs
    158 	  */
    159 	public static function getTemplateIdsByValue( $text )
    160 	{
    161 		$db = db_connection();
    162 
    163 		$stmt = $db->sql( 'SELECT templateid FROM {{templatemodel}}'.
    164 		                ' WHERE text LIKE {text} ' );
    165 
    166 		$stmt->setString( 'text'   ,'%'.$text.'%'  );
    167 		
    168 		return $stmt->getCol();
    169 	}
    170 
    171 
    172 	/**
    173  	 * Ermitteln aller Elemente zu diesem Template
    174  	 * Es wird eine Liste nur mit den Element-IDs ermittelt und zur?ckgegeben
    175  	 * @return Array
    176  	 */
    177 	function getElementIds()
    178 	{
    179 		$db = db_connection();
    180 
    181 		$stmt = $db->sql( 'SELECT id FROM {{element}}'.
    182 		                '  WHERE templateid={templateid}'.
    183 		                '  ORDER BY name ASC' );
    184 		$stmt->setInt( 'templateid',$this->templateid );
    185 		return $stmt->getCol();
    186 	}
    187 
    188 
    189 
    190 	/**
    191  	 * Ermitteln aller Elemente zu diesem Template
    192  	 * Es wird eine Liste mit den kompletten Elementen ermittelt und zurueckgegeben
    193  	 * @return Array
    194  	 */
    195 	function getElements()
    196 	{
    197 		$list = array();
    198 		$db = db_connection();
    199 
    200 		$sql = $db->sql( 'SELECT * FROM {{element}}'.
    201 		                '  WHERE templateid={templateid}'.
    202 		                '  ORDER BY name ASC' );
    203 		$sql->setInt( 'templateid',$this->templateid );
    204 		foreach($sql->getAll() as $row )
    205 		{
    206 			$e = new Element( $row['id'] );
    207 			$e->setDatabaseRow( $row );
    208 			
    209 			$list[$e->elementid] = $e;
    210 			unset($e);
    211 		}
    212 		return $list;
    213 	}
    214 
    215 
    216 
    217 	/**
    218  	 * Ermitteln aller Elemente zu diesem Template
    219  	 * Es wird eine Liste mit den kompletten Elementen ermittelt und zurueckgegeben
    220  	 * @return Array
    221  	 */
    222 	function getWritableElements()
    223 	{
    224 		$list = array();
    225 		$e = new Element();
    226 		$readonlyList = implode(',',Element::$readonlyElementTypeIds);
    227 		
    228 		$db = db_connection();
    229 
    230 		$sql = $db->sql( <<<SQL
    231 SELECT * FROM {{element}}
    232   WHERE templateid={templateid}
    233     AND typeid NOT IN ($readonlyList)
    234   ORDER BY name ASC
    235 SQL
    236 );
    237 		$sql->setInt       ( 'templateid'  ,$this->templateid        );
    238 		foreach($sql->getAll() as $row )
    239 		{
    240 			$e = new Element( $row['id'] );
    241 			$e->setDatabaseRow( $row );
    242 
    243 			if (!$e->writable)
    244 			    continue;
    245 			
    246 			$list[$e->elementid] = $e;
    247 			unset($e);
    248 		}
    249 		return $list;
    250 	}
    251 
    252 
    253 
    254 	/**
    255  	 * Ermitteln aller Elemente zu diesem Template
    256  	 * Es wird eine Liste mit den Element-Namen zur?ckgegeben
    257  	 * @return array
    258  	 */
    259 	public function getElementNames()
    260 	{
    261 		$db = db_connection();
    262 
    263 		$sql = $db->sql( 'SELECT id,name FROM {{element}}'.
    264 		                '  WHERE templateid={templateid}'.
    265 		                '  ORDER BY name ASC' );
    266 		$sql->setInt( 'templateid',$this->templateid );
    267 
    268 		return $sql->getAssoc();
    269 	}
    270 
    271 
    272 	/**
    273  	 * Hinzuf?gen eines Elementes
    274  	 * @param String Name des Elementes
    275  	 */
    276 	public function addElement($name, $description='', $typeid=Element::ELEMENT_TYPE_TEXT )
    277 	{
    278 		$element = new Element();
    279 		$element->name       = $name;
    280 		$element->label      = $name;
    281 		$element->desc       = $description;
    282 		$element->typeid     = $typeid;
    283 		$element->templateid = $this->templateid;
    284 		$element->format     = Element::ELEMENT_FORMAT_TEXT;
    285 		$element->writable   = true;
    286 		$element->add();
    287 
    288 		return $element;
    289 	}
    290 
    291 
    292 	/**
    293  	 * Hinzufuegen eines Templates
    294  	 * @param String Name des Templates (optional)
    295  	 */
    296 	function add( $name='' )
    297 	{
    298 		if	( !empty($name) )
    299 			$this->name = $name;
    300 
    301 		$db = db_connection();
    302 
    303 		$sql = $db->sql('SELECT MAX(id) FROM {{template}}');
    304 		$this->templateid = intval($sql->getOne())+1;
    305 
    306 		$sql = $db->sql( 'INSERT INTO {{template}}'.
    307 		                ' (id,name,projectid)'.
    308 		                ' VALUES({templateid},{name},{projectid})' );
    309 		$sql->setInt   ('templateid',$this->templateid   );
    310 		$sql->setString('name'      ,$name               );
    311 
    312 		$sql->setInt   ('projectid' ,$this->projectid );
    313 
    314 		$sql->query();
    315 	}
    316 
    317 
    318 	/**
    319  	 * Ermitteln alles Objekte (=Seiten), welche auf diesem Template basieren.
    320  	 * 
    321  	 * @return Array Liste von Objekt-IDs
    322  	 */
    323 	function getDependentObjectIds()
    324 	{
    325 		$db = db_connection();
    326 
    327 		$sql = $db->sql( 'SELECT objectid FROM {{page}}'.
    328 		                '  WHERE templateid={templateid}' );
    329 		$sql->setInt( 'templateid',$this->templateid );
    330 
    331 		return $sql->getCol();
    332 	}
    333 
    334 
    335 	/**
    336  	 * Loeschen des Templates
    337  	 *
    338  	 * Entfernen alle Templateinhalte und des Templates selber
    339  	 */
    340 	function delete()
    341 	{
    342 		$db = db_connection();
    343 		
    344 		foreach( $this->getElementIds() as $elementid )
    345 		{
    346 			$element = new Element( $elementid );
    347 			$element->delete();
    348 		}
    349 
    350 		$stmt = $db->sql( 'DELETE FROM {{templatemodel}}'.
    351 		                ' WHERE templateid={templateid}' );
    352 		$stmt->setInt( 'templateid',$this->templateid );
    353 		$stmt->query();
    354 
    355 		$stmt = $db->sql( 'DELETE FROM {{template}}'.
    356 		                ' WHERE id={templateid}' );
    357 		$stmt->setInt( 'templateid',$this->templateid );
    358 		$stmt->query();
    359 	}
    360 	
    361 	
    362 	/**
    363 	 * Ermittelt den Mime-Type zu diesem Template.
    364 	 * 
    365 	 * Es wird die Extension des Templates betrachtet und dann mit Hilfe der
    366 	 * Konfigurationsdatei 'mime-types.ini' der Mime-Type bestimmt. 
    367 	 *
    368 	 * @return String Mime-Type  
    369 	 */
    370 	function mimeType()
    371 	{
    372 		global $conf;
    373 		$mime_types = $conf['mime-types'];
    374 
    375 		// Nur den letzten Teil der Extension auswerten:
    376 		// Aus 'mobile.html' wird nur 'html' verwendet.
    377 		$parts = explode('.',$this->extension);
    378 		$extension = strtolower(array_pop($parts));
    379 
    380 		if	( !empty($mime_types[$extension]) )
    381 			$this->mime_type = $mime_types[$extension];
    382 		else
    383 			// Wenn kein Mime-Type gefunden, dann Standardwert setzen
    384 			$this->mime_type = 'application/octet-stream';
    385 			
    386 		return( $this->mime_type );
    387 	}
    388 
    389     public function getName()
    390     {
    391         return $this->name;
    392     }
    393 
    394 }
    395 
    396 ?>