openrat-cms

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

Model.class.php (5345B)


      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 /**
     23  * Diese Klasse stellt ein Projektmodell dar. Ein Projektmodell ist eine Darstellungsart
     24  * der Inhalte. Zu jedem Projektmodell gibt es einen anderen Templatequelltext.
     25  * Beispiel: Neben HTML gibt es ein Projektmodell fuer WML oder XML. Die Inhalte sind gleich,
     26  * aber die Art der Ausgabe ist jeweils anders.
     27  *
     28  * @package openrat.objects
     29  * @author $Author$
     30  * @version $Rev: $
     31  */
     32 class Model extends ModelBase
     33 {
     34 	var $modelid = 0;
     35 	var $error      = '';
     36 	var $projectid;
     37 
     38 	var $name      = '';
     39 	var $isDefault = false;
     40 
     41 
     42 	/**
     43 	 * Konstruktor
     44 	 */
     45 	function __construct( $modelid='' )
     46 	{
     47 		if   ( is_numeric($modelid) )
     48 			$this->modelid = $modelid;
     49 	}
     50 
     51 	
     52 	/**
     53 	 * Stellt fest, ob die angegebene Id existiert.
     54 	 */
     55 	function available( $id )
     56 	{
     57 		$db = db_connection();
     58 
     59 		$sql = $db->sql('SELECT 1 FROM {{projectmodel}} '.
     60 		               ' WHERE id={id}');
     61 		$sql->setInt('id' ,$id  );
     62 
     63 		return intval($sql->getOne()) == 1;
     64 	}
     65 	
     66 
     67 	
     68 
     69 	/**
     70 	 * Lesen aus der Datenbank
     71 	 */
     72 	function load()
     73 	{
     74 		$db = db_connection();
     75 
     76 		$sql = $db->sql( 'SELECT * FROM {{projectmodel}}'.
     77 		                ' WHERE id={modelid}' );
     78 		$sql->setInt( 'modelid',$this->modelid );
     79 
     80 		$row = $sql->getRow();
     81 
     82 		$this->name      = $row['name'     ];
     83 		$this->projectid = $row['projectid'];
     84 	
     85 		if	( $row['is_default'] == '1' )
     86 			$this->isDefault = true;
     87 		else $this->isDefault = false;
     88 	}
     89 
     90 
     91 	/**
     92 	 * Speichern des Projektmodells
     93 	 */
     94 	function save()
     95 	{
     96 		$db = db_connection();
     97 
     98 		// Gruppe speichern		
     99 		$sql = $db->sql( 'UPDATE {{projectmodel}} '.
    100 		                '  SET name      = {name} '.
    101 		                '  WHERE id={modelid}' );
    102 		$sql->setString( 'name'     ,$this->name    );
    103 
    104 		$sql->setInt( 'modelid',$this->modelid );
    105 
    106 		// Datenbankabfrage ausfuehren
    107 		$sql->query();
    108 	}
    109 
    110 
    111 	/**
    112 	 * Alle notwendigen Eigenschaften dieses Projektmodells
    113 	 * werden als Array zurueckgegeben
    114 	 *
    115 	 * @return Array
    116 	 */
    117 	function getProperties()
    118 	{
    119 		return Array( 'modelid'  =>$this->modelid,
    120 		              'projectid'=>$this->projectid,
    121 		              'isDefault'=>$this->isDefault,
    122 		              'name'     =>$this->name );
    123 	}
    124 
    125 
    126 	/**
    127 	 * Modell hinzufuegen
    128 	 * @param String Name des Modells (optional)
    129 	 */
    130 	function add( $name = '' )
    131 	{
    132 		if	( $name != '' )
    133 			$this->name = $name;
    134 		
    135 		$db = db_connection();
    136 
    137 		$sql = $db->sql('SELECT MAX(id) FROM {{projectmodel}}');
    138 		$this->modelid = intval($sql->getOne())+1;
    139 
    140 		// Modell hinzuf?gen
    141 		$sql = $db->sql( 'INSERT INTO {{projectmodel}} '.
    142 		                "(id,projectid,name,extension,is_default) VALUES( {modelid},{projectid},{name},'',0 )");
    143 
    144 		$sql->setInt   ('modelid'  ,$this->modelid   );
    145 		$sql->setInt   ('projectid',$this->projectid );
    146 		$sql->setString('name'     ,$this->name      );
    147 
    148 		// Datenbankbefehl ausfuehren
    149 		$sql->query();
    150 	}
    151 
    152 
    153 
    154 	// Diese Sprache als 'default' markieren.
    155 	function setDefault()
    156 	{
    157 		global $SESS;
    158 		$db = db_connection();
    159 
    160 		// Zuerst alle auf nicht-Standard setzen
    161 		$sql = $db->sql( 'UPDATE {{projectmodel}} '.
    162 		                '  SET is_default = 0 '.
    163 		                '  WHERE projectid={projectid}' );
    164 		$sql->setInt('projectid',$this->projectid );
    165 		$sql->query();
    166 	
    167 		// Jetzt die gew?nschte Sprachvariante auf Standard setzen
    168 		$sql = $db->sql( 'UPDATE {{projectmodel}} '.
    169 		                '  SET is_default = 1 '.
    170 		                '  WHERE id={modelid}' );
    171 		$sql->setInt('modelid',$this->modelid );
    172 		$sql->query();
    173 	}
    174 
    175 
    176 	/**
    177 	 * Entfernen des Projektmodells aus der Datenbank
    178 	 * Es wird dabei nicht geprueft, ob noch ein anders Projektmodell
    179 	 * vorhanden ist.
    180 	 */
    181 	function delete()
    182 	{
    183 		$db = db_connection();
    184 
    185 		// Vorlagen zu dieseem Modell loeschen
    186 		$sql = $db->sql( <<<SQL
    187 	DELETE FROM {{templatemodel}}
    188 	 WHERE projectmodelid = {modelid}
    189 SQL
    190 );
    191 		$sql->setInt( 'modelid',$this->modelid );
    192 		$sql->query();
    193 		
    194 		// Dieses Modell löschen
    195 		$sql = $db->sql( <<<SQL
    196 	DELETE FROM {{projectmodel}}
    197 	 WHERE id={modelid}
    198 SQL
    199 );
    200 		$sql->setInt( 'modelid',$this->modelid );
    201 		$sql->query();
    202 
    203 		// Anderes Modell auf "Default" setzen (sofern vorhanden)
    204 		if	( $this->isDefault )
    205 		{
    206 			$sql = $db->sql( 'SELECT id FROM {{projectmodel}} WHERE projectid={projectid}' );
    207 			$sql->setInt( 'projectid',$this->projectid );
    208 			$new_default_modelid = $sql->getOne();
    209 	
    210 			$sql = $db->sql( 'UPDATE {{projectmodel}} SET is_default=1 WHERE id={modelid}' );
    211 			$sql->setInt( 'modelid',$new_default_modelid );
    212 			$sql->query();
    213 		}
    214 	}
    215 
    216     public function getName()
    217     {
    218         return $this->name;
    219     }
    220 }
    221 
    222 ?>