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

Last commit: Sat Jun 25 14:26:33 2022 +0200	Jan Dankert	New: Many Enhancements for the internal script language: More access to the data structure of pages, folders, templates, ...
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 = \cms\base\DB::get(); 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 = \cms\base\DB::get(); 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 return $this; 90 } 91 92 93 /** 94 * Speichern des Projektmodells 95 */ 96 function save() 97 { 98 $db = \cms\base\DB::get(); 99 100 // Gruppe speichern 101 $sql = $db->sql( 'UPDATE {{projectmodel}} '. 102 ' SET name = {name} '. 103 ' WHERE id={modelid}' ); 104 $sql->setString( 'name' ,$this->name ); 105 106 $sql->setInt( 'modelid',$this->modelid ); 107 108 // Datenbankabfrage ausfuehren 109 $sql->execute(); 110 } 111 112 113 /** 114 * Alle notwendigen Eigenschaften dieses Projektmodells 115 * werden als Array zurueckgegeben 116 * 117 * @return array 118 */ 119 function getProperties() 120 { 121 return Array( 'modelid' =>$this->modelid, 122 'projectid'=>$this->projectid, 123 'isDefault'=>$this->isDefault, 124 'name' =>$this->name ); 125 } 126 127 128 /** 129 * Modell hinzufuegen 130 * @param String Name des Modells (optional) 131 */ 132 function add( $name = '' ) 133 { 134 if ( $name != '' ) 135 $this->name = $name; 136 137 $db = \cms\base\DB::get(); 138 139 $sql = $db->sql('SELECT MAX(id) FROM {{projectmodel}}'); 140 $this->modelid = intval($sql->getOne())+1; 141 142 // Modell hinzuf?gen 143 $sql = $db->sql( 'INSERT INTO {{projectmodel}} '. 144 "(id,projectid,name,extension,is_default) VALUES( {modelid},{projectid},{name},'',0 )"); 145 146 $sql->setInt ('modelid' ,$this->modelid ); 147 $sql->setInt ('projectid',$this->projectid ); 148 $sql->setString('name' ,$this->name ); 149 150 // Datenbankbefehl ausfuehren 151 $sql->execute(); 152 } 153 154 155 156 // Diese Sprache als 'default' markieren. 157 function setDefault() 158 { 159 $db = \cms\base\DB::get(); 160 161 // Zuerst alle auf nicht-Standard setzen 162 $sql = $db->sql( 'UPDATE {{projectmodel}} '. 163 ' SET is_default = 0 '. 164 ' WHERE projectid={projectid}' ); 165 $sql->setInt('projectid',$this->projectid ); 166 $sql->execute(); 167 168 // Jetzt die gew?nschte Sprachvariante auf Standard setzen 169 $sql = $db->sql( 'UPDATE {{projectmodel}} '. 170 ' SET is_default = 1 '. 171 ' WHERE id={modelid}' ); 172 $sql->setInt('modelid',$this->modelid ); 173 $sql->execute(); 174 } 175 176 177 /** 178 * Entfernen des Projektmodells aus der Datenbank 179 * Es wird dabei nicht geprueft, ob noch ein anders Projektmodell 180 * vorhanden ist. 181 */ 182 function delete() 183 { 184 $db = \cms\base\DB::get(); 185 186 // Vorlagen zu dieseem Modell loeschen 187 $sql = $db->sql( <<<SQL 188 DELETE FROM {{templatemodel}} 189 WHERE projectmodelid = {modelid} 190 SQL 191 ); 192 $sql->setInt( 'modelid',$this->modelid ); 193 $sql->execute(); 194 195 // Dieses Modell löschen 196 $sql = $db->sql( <<<SQL 197 DELETE FROM {{projectmodel}} 198 WHERE id={modelid} 199 SQL 200 ); 201 $sql->setInt( 'modelid',$this->modelid ); 202 $sql->execute(); 203 204 // Anderes Modell auf "Default" setzen (sofern vorhanden) 205 if ( $this->isDefault ) 206 { 207 $sql = $db->sql( 'SELECT id FROM {{projectmodel}} WHERE projectid={projectid}' ); 208 $sql->setInt( 'projectid',$this->projectid ); 209 $new_default_modelid = $sql->getOne(); 210 211 $sql = $db->sql( 'UPDATE {{projectmodel}} SET is_default=1 WHERE id={modelid}' ); 212 $sql->setInt( 'modelid',$new_default_modelid ); 213 $sql->execute(); 214 } 215 } 216 217 public function getName() 218 { 219 return $this->name; 220 } 221 222 223 public function getId() 224 { 225 return $this->modelid; 226 } 227 228 229 }
Download modules/cms/model/Model.class.php
History Sat, 25 Jun 2022 14:26:33 +0200 Jan Dankert New: Many Enhancements for the internal script language: More access to the data structure of pages, folders, templates, ... 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, 2 Oct 2020 23:11:48 +0200 Jan Dankert Cleanup: No '.inputholder' any more, notices with links to objects. Sat, 26 Sep 2020 12:20:43 +0200 Jan Dankert Refactoring: No global variables like $SESS any more. All constants are capsulated by classes. 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. Sun, 23 Feb 2020 04:01:30 +0100 Jan Dankert Refactoring with Namespaces for the cms modules, part 1: moving.