File modules/cms/model/Template.class.php
Last commit: Thu Mar 10 10:28:14 2022 +0100 dankert Fix: Fulltext-Search was broken due to the last Content-Refactoring
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 use cms\base\DB; 20 use language\Messages; 21 use util\exception\ObjectNotFoundException; 22 23 24 /** 25 * Logische Darstellung eines Templates 26 * 27 * @author: $Author$ 28 * @version: $Revision$ 29 * @package openrat.objects 30 */ 31 class Template extends ModelBase 32 { 33 const TYPE_PUBLISH = 1; 34 35 /** 36 * ID dieses Templates 37 * @type Integer 38 */ 39 var $templateid = 0; 40 41 /** 42 * Projekt-ID des aktuell ausgew?hlten Projektes 43 * @type Integer 44 */ 45 var $projectid = 0; 46 47 /** 48 * Logischer Name 49 * @type String 50 */ 51 var $name = 'unnamed'; 52 53 /** 54 * Should this template be published? 55 * 56 * @var bool 57 */ 58 public $publish = true; 59 60 // Konstruktor 61 function __construct( $templateid='' ) 62 { 63 if ( is_numeric($templateid) ) 64 $this->templateid = $templateid; 65 } 66 67 68 /** 69 * Laden des Templates aus der Datenbank und f?llen der Objekteigenschaften 70 */ 71 function load() 72 { 73 $stmt = Db::sql( 'SELECT * FROM {{template}}'. 74 ' WHERE id={templateid}' ); 75 $stmt->setInt( 'templateid',$this->templateid ); 76 $row = $stmt->getRow(); 77 78 if ( empty($row) ) 79 throw new ObjectNotFoundException("Template not found: ".$this->templateid); 80 81 $this->name = $row['name' ]; 82 $this->projectid = $row['projectid']; 83 $this->publish = $row['type'] & self::TYPE_PUBLISH; 84 } 85 86 87 public function loadTemplateModelFor( $modelid ) { 88 89 $templateModel = new TemplateModel( $this->templateid, $modelid ); 90 $templateModel->load(); 91 92 return $templateModel; 93 } 94 95 /** 96 * Abspeichern des Templates in der Datenbank 97 */ 98 function save() 99 { 100 if ( $this->name == "" ) 101 $this->name = \cms\base\Language::lang(Messages::TEMPLATE).' #'.$this->templateid; 102 103 $stmt = Db::sql( <<<SQL 104 UPDATE {{template}} 105 SET name={name}, 106 type={type} 107 WHERE id={templateid} 108 SQL 109 ); 110 $stmt->setString( 'name' ,$this->name ); 111 $stmt->setInt ( 'templateid',$this->templateid ); 112 113 $type = 0; 114 if( $this->publish ) $type |= self::TYPE_PUBLISH; 115 116 $stmt->setInt ( 'type' ,$type ); 117 118 $stmt->execute(); 119 } 120 121 122 /** 123 * Es werden Templates mit einem Inhalt gesucht 124 * @param String Suchbegriff 125 * @return array Liste der gefundenen Template-IDs 126 */ 127 public static function getTemplateIdsByValue( $text ) 128 { 129 $stmt = DB::sql( <<<SQL 130 SELECT templateid FROM {{templatemodel}} 131 LEFT JOIN {{content}} 132 ON {{content}}.id = {{templatemodel}}.contentid 133 LEFT JOIN {{value}} 134 ON {{value}}.contentid = {{content}}.id 135 WHERE {{value}}.text LIKE {text} 136 SQL 137 ); 138 139 $stmt->setString( 'text' ,'%'.$text.'%' ); 140 141 return $stmt->getCol(); 142 } 143 144 145 /** 146 * Ermitteln aller Elemente zu diesem Template 147 * Es wird eine Liste nur mit den Element-IDs ermittelt und zur?ckgegeben 148 * @return array 149 */ 150 public function getElementIds() 151 { 152 $stmt = DB::sql( 'SELECT id FROM {{element}}'. 153 ' WHERE templateid={templateid}'. 154 ' ORDER BY name ASC' ); 155 $stmt->setInt( 'templateid',$this->templateid ); 156 return $stmt->getCol(); 157 } 158 159 160 161 /** 162 * Ermitteln aller Elemente zu diesem Template 163 * Es wird eine Liste mit den kompletten Elementen ermittelt und zurueckgegeben 164 * @return array 165 */ 166 function getElements() 167 { 168 $list = array(); 169 $db = \cms\base\DB::get(); 170 171 $sql = $db->sql( 'SELECT * FROM {{element}}'. 172 ' WHERE templateid={templateid}'. 173 ' ORDER BY name ASC' ); 174 $sql->setInt( 'templateid',$this->templateid ); 175 foreach($sql->getAll() as $row ) 176 { 177 $e = new Element( $row['id'] ); 178 $e->setDatabaseRow( $row ); 179 180 $list[$e->elementid] = $e; 181 unset($e); 182 } 183 return $list; 184 } 185 186 187 188 /** 189 * Ermitteln aller Elemente zu diesem Template 190 * Es wird eine Liste mit den kompletten Elementen ermittelt und zurueckgegeben 191 * @return array 192 */ 193 function getWritableElements() 194 { 195 $list = array(); 196 $e = new Element(); 197 $readonlyList = implode(',',Element::$readonlyElementTypeIds); 198 199 $db = \cms\base\DB::get(); 200 201 $sql = $db->sql( <<<SQL 202 SELECT * FROM {{element}} 203 WHERE templateid={templateid} 204 AND typeid NOT IN ($readonlyList) 205 ORDER BY name ASC 206 SQL 207 ); 208 $sql->setInt ( 'templateid' ,$this->templateid ); 209 foreach($sql->getAll() as $row ) 210 { 211 $e = new Element( $row['id'] ); 212 $e->setDatabaseRow( $row ); 213 214 if (!$e->writable) 215 continue; 216 217 $list[$e->elementid] = $e; 218 unset($e); 219 } 220 return $list; 221 } 222 223 224 225 /** 226 * Ermitteln aller Elemente zu diesem Template 227 * Es wird eine Liste mit den Element-Namen zur?ckgegeben 228 * @return array 229 */ 230 public function getElementNames() 231 { 232 $db = \cms\base\DB::get(); 233 234 $sql = $db->sql( 'SELECT id,name FROM {{element}}'. 235 ' WHERE templateid={templateid}'. 236 ' ORDER BY name ASC' ); 237 $sql->setInt( 'templateid',$this->templateid ); 238 239 return $sql->getAssoc(); 240 } 241 242 243 /** 244 * Hinzuf?gen eines Elementes 245 * @param String Name des Elementes 246 */ 247 public function addElement($name, $description='', $typeid=Element::ELEMENT_TYPE_TEXT ) 248 { 249 $element = new Element(); 250 $element->name = $name; 251 $element->label = $name; 252 $element->desc = $description; 253 $element->typeid = $typeid; 254 $element->templateid = $this->templateid; 255 $element->format = Element::ELEMENT_FORMAT_TEXT; 256 $element->writable = true; 257 $element->persist(); 258 259 return $element; 260 } 261 262 263 /** 264 * Hinzufuegen eines Templates 265 * @param String Name des Templates (optional) 266 */ 267 function add() 268 { 269 $db = \cms\base\DB::get(); 270 271 $sql = $db->sql('SELECT MAX(id) FROM {{template}}'); 272 $this->templateid = intval($sql->getOne())+1; 273 274 $sql = $db->sql( 'INSERT INTO {{template}}'. 275 ' (id,name,projectid)'. 276 ' VALUES({templateid},{name},{projectid})' ); 277 $sql->setInt ('templateid',$this->templateid ); 278 $sql->setString('name' ,$this->name ); 279 280 $sql->setInt ('projectid' ,$this->projectid ); 281 282 $sql->execute(); 283 } 284 285 286 /** 287 * Ermitteln alles Objekte (=Seiten), welche auf diesem Template basieren. 288 * 289 * @return array Liste von Objekt-IDs 290 */ 291 function getDependentObjectIds() 292 { 293 $db = \cms\base\DB::get(); 294 295 $sql = $db->sql( 'SELECT objectid FROM {{page}}'. 296 ' WHERE templateid={templateid}' ); 297 $sql->setInt( 'templateid',$this->templateid ); 298 299 return $sql->getCol(); 300 } 301 302 303 /** 304 * Loeschen des Templates 305 * 306 * Entfernen alle Templateinhalte und des Templates selber 307 */ 308 public function delete() 309 { 310 $db = \cms\base\DB::get(); 311 312 foreach( $this->getElementIds() as $elementid ) 313 { 314 $element = new Element( $elementid ); 315 $element->delete(); 316 } 317 318 $stmt = $db->sql( 'DELETE FROM {{templatemodel}}'. 319 ' WHERE templateid={templateid}' ); 320 $stmt->setInt( 'templateid',$this->templateid ); 321 $stmt->execute(); 322 323 $stmt = $db->sql( 'DELETE FROM {{template}}'. 324 ' WHERE id={templateid}' ); 325 $stmt->setInt( 'templateid',$this->templateid ); 326 327 $stmt->execute(); 328 329 $this->templateid = 0; 330 } 331 332 333 public function getName() 334 { 335 return $this->name; 336 } 337 338 public function getId() 339 { 340 return $this->templateid; 341 } 342 343 344 }
Downloadmodules/cms/model/Template.class.php
History Thu, 10 Mar 2022 10:28:14 +0100 dankert Fix: Fulltext-Search was broken due to the last Content-Refactoring Mon, 6 Dec 2021 22:33:10 +0100 dankert Some fixes for deleting objects. Sun, 7 Mar 2021 00:10:20 +0100 Jan Dankert Refactoring: Hopefully more performance while accessing the database resultsets. Sat, 20 Feb 2021 01:34:41 +0100 Jan Dankert New: Publish-switch for templates. Sat, 20 Feb 2021 00:49:37 +0100 Jan Dankert Cleanup of templates. Wed, 18 Nov 2020 01:46:36 +0100 Jan Dankert Refactoring of model classes: New method persist() and some other cleanups. Mon, 26 Oct 2020 23:09:24 +0100 Jan Dankert Cleanup UI for adding templates and adding projects. Fri, 2 Oct 2020 23:11:48 +0200 Jan Dankert Cleanup: No '.inputholder' any more, notices with links to objects. Tue, 29 Sep 2020 22:17:11 +0200 Jan Dankert Refactoring: Do not use global constants. Sat, 26 Sep 2020 04:03:53 +0200 Jan Dankert Refactoring: read language keys with a class. 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. Wed, 23 Sep 2020 01:04:05 +0200 Jan Dankert Cleanup of deprecated methods and deprecated class attributes. Mon, 17 Aug 2020 22:52:37 +0200 Jan Dankert Cleanup: Killing the old odd 'GLOBAL_' message prefixes. Sun, 23 Feb 2020 04:01:30 +0100 Jan Dankert Refactoring with Namespaces for the cms modules, part 1: moving.