commit b6e591db79081478b119633225763e10dca69778
parent bcaba9037bb16c526bbb537eb8d2066741ff1d79
Author: Jan Dankert <develop@jandankert.de>
Date: Thu, 16 May 2019 21:05:39 +0200
Refactoring: use class constants instead of global constants.
Diffstat:
4 files changed, 468 insertions(+), 486 deletions(-)
diff --git a/modules/cms-core/action/ElementAction.class.php b/modules/cms-core/action/ElementAction.class.php
@@ -11,27 +11,11 @@ use cms\model\BaseObject;
use Text;
-// OpenRat Content Management System
-// Copyright (C) 2002-2012 Jan Dankert, cms@jandankert.de
-//
-// This program is free software; you can redistribute it and/or
-// modify it under the terms of the GNU General Public License
-// as published by the Free Software Foundation; version 2.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with this program; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
/**
* Action-Klasse fuer die Bearbeitung eines Template-Elementes.
*
* @author Jan Dankert
- * @package openrat.actions
*/
class ElementAction extends Action
{
@@ -225,7 +209,7 @@ class ElementAction extends Action
$convertToLang = false;
switch( $this->element->typeid )
{
- case ELEMENT_TYPE_INFO:
+ case Element::ELEMENT_TYPE_INFO:
$subtypes = Array('db_id',
'db_name',
'project_id',
@@ -259,15 +243,15 @@ class ElementAction extends Action
$convertToLang = true;
break;
- case ELEMENT_TYPE_INFODATE:
- case ELEMENT_TYPE_LINKDATE:
+ case Element::ELEMENT_TYPE_INFODATE:
+ case Element::ELEMENT_TYPE_LINKDATE:
$subtypes = Array('date_published',
'date_saved',
'date_created' );
$convertToLang = true;
break;
- case ELEMENT_TYPE_LINK:
+ case Element::ELEMENT_TYPE_LINK:
$subtypes = Array(
'file',
'image',
@@ -278,7 +262,7 @@ class ElementAction extends Action
$convertToLang = true;
break;
- case ELEMENT_TYPE_LINKINFO:
+ case Element::ELEMENT_TYPE_LINKINFO:
$subtypes = Array('width',
'height',
'id',
@@ -300,13 +284,13 @@ class ElementAction extends Action
$convertToLang = true;
break;
- case ELEMENT_TYPE_INSERT:
+ case Element::ELEMENT_TYPE_INSERT:
$subtypes = Array('inline',
'ssi' );
$convertToLang = true;
break;
- case ELEMENT_TYPE_DYNAMIC:
+ case Element::ELEMENT_TYPE_DYNAMIC:
$files = Array();
$macroFiles = \FileUtils::readDir(__DIR__.'/../../cms-macros/macro');
@@ -373,12 +357,12 @@ class ElementAction extends Action
switch( $this->element->typeid )
{
- case ELEMENT_TYPE_LONGTEXT:
+ case Element::ELEMENT_TYPE_LONGTEXT:
$this->setTemplateVar('default_longtext',$this->element->defaultText );
break;
- case ELEMENT_TYPE_SELECT:
- case ELEMENT_TYPE_TEXT:
+ case Element::ELEMENT_TYPE_SELECT:
+ case Element::ELEMENT_TYPE_TEXT:
$this->setTemplateVar('default_text' ,$this->element->defaultText );
break;
}
@@ -391,8 +375,8 @@ class ElementAction extends Action
$formats = Element::getAvailableFormats();
// Für einfache Textelemente gibt es keinen HTML-Editor
- if ( $this->element->typeid != ELEMENT_TYPE_LONGTEXT )
- unset( $formats[ ELEMENT_FORMAT_HTML ] );
+ if ( $this->element->typeid != Element::ELEMENT_TYPE_LONGTEXT )
+ unset( $formats[ Element::ELEMENT_FORMAT_HTML ] );
foreach( $formats as $t=>$v )
$formats[$t] = array('lang'=>'EL_PROP_FORMAT_'.$v);
@@ -457,11 +441,11 @@ class ElementAction extends Action
switch( $this->element->typeid )
{
- case ELEMENT_TYPE_SELECT:
+ case Element::ELEMENT_TYPE_SELECT:
$this->setTemplateVar('select_items',$this->element->code );
break;
- case ELEMENT_TYPE_DYNAMIC:
+ case Element::ELEMENT_TYPE_DYNAMIC:
$className = $this->element->subtype;
$fileName = OR_DYNAMICCLASSES_DIR.'/'.$className.'.class.'.PHP_EXT;
@@ -511,7 +495,7 @@ class ElementAction extends Action
break;
- case ELEMENT_TYPE_CODE:
+ case Element::ELEMENT_TYPE_CODE:
if ( $conf['security']['disable_dynamic_code'] )
$this->addNotice('element',$this->element->name,'CODE_DISABLED',OR_NOTICE_WARN);
@@ -551,12 +535,12 @@ class ElementAction extends Action
switch( $this->element->typeid )
{
- case ELEMENT_TYPE_LINK:
+ case Element::ELEMENT_TYPE_LINK:
if ( ! in_array( $o->typeid, array(BaseObject::TYPEID_PAGE,BaseObject::TYPEID_IMAGE,BaseObject::TYPEID_FILE,BaseObject::TYPEID_LINK,BaseObject::TYPEID_URL,BaseObject::TYPEID_TEXT ) ) )
continue 2;
break;
//Change tobias
- case ELEMENT_TYPE_INSERT:
+ case Element::ELEMENT_TYPE_INSERT:
if ( ! in_array( $o->typeid, array(BaseObject::TYPEID_FOLDER,BaseObject::TYPEID_PAGE,BaseObject::TYPEID_IMAGE,BaseObject::TYPEID_FILE,BaseObject::TYPEID_LINK,BaseObject::TYPEID_URL,BaseObject::TYPEID_TEXT ) ) )
continue 2;
break;
@@ -689,4 +673,3 @@ class ElementAction extends Action
}
}
-?>-
\ No newline at end of file
diff --git a/modules/cms-core/action/TemplateAction.class.php b/modules/cms-core/action/TemplateAction.class.php
@@ -271,13 +271,13 @@ class TemplateAction extends Action
// Code-Element nur fuer Administratoren (da voller Systemzugriff!)
if ( !$this->userIsAdmin() )
- unset( $types[ELEMENT_TYPE_CODE] );
+ unset( $types[Element::ELEMENT_TYPE_CODE] );
// Auswahlmoeglichkeiten:
$this->setTemplateVar('types',$types);
// Vorbelegung:
- $this->setTemplateVar('typeid',ELEMENT_TYPE_TEXT);
+ $this->setTemplateVar('typeid',Element::ELEMENT_TYPE_TEXT);
}
diff --git a/modules/cms-core/model/Element.class.php b/modules/cms-core/model/Element.class.php
@@ -1,32 +1,6 @@
<?php
namespace cms\model;
-define('ELEMENT_TYPE_DATE' , 1);
-define('ELEMENT_TYPE_NUMBER' , 2);
-define('ELEMENT_TYPE_TEXT' , 3);
-define('ELEMENT_TYPE_INFO' , 4);
-define('ELEMENT_TYPE_INFODATE', 5);
-define('ELEMENT_TYPE_LINK' , 6);
-define('ELEMENT_TYPE_LONGTEXT', 7);
-define('ELEMENT_TYPE_CODE' , 8);
-define('ELEMENT_TYPE_DYNAMIC' , 9);
-define('ELEMENT_TYPE_SELECT' ,10);
-define('ELEMENT_TYPE_COPY' ,11);
-define('ELEMENT_TYPE_LINKINFO',12);
-define('ELEMENT_TYPE_LINKDATE',13);
-define('ELEMENT_TYPE_INSERT' ,14);
-
-define('ELEMENT_FORMAT_TEXT' ,0);
-define('ELEMENT_FORMAT_HTML' ,1);
-define('ELEMENT_FORMAT_WIKI' ,2);
-define('ELEMENT_FORMAT_MARKDOWN',3);
-
-define('ELEMENT_FLAG_HTML_ALLOWED' , 1);
-define('ELEMENT_FLAG_ALL_LANGUAGES', 2);
-define('ELEMENT_FLAG_WRITABLE' , 4);
-define('ELEMENT_FLAG_WITH_ICON' , 8);
-define('ELEMENT_FLAG_INHERIT' ,16);
-
/**
* Diese Objektklasse stellt ein Element das.
@@ -39,7 +13,33 @@ define('ELEMENT_FLAG_INHERIT' ,16);
*/
class Element
{
- /**
+ const ELEMENT_TYPE_DATE = 1;
+ const ELEMENT_TYPE_NUMBER = 2;
+ const ELEMENT_TYPE_TEXT = 3;
+ const ELEMENT_TYPE_INFO = 4;
+ const ELEMENT_TYPE_INFODATE = 5;
+ const ELEMENT_TYPE_LINK = 6;
+ const ELEMENT_TYPE_LONGTEXT = 7;
+ const ELEMENT_TYPE_CODE = 8;
+ const ELEMENT_TYPE_DYNAMIC = 9;
+ const ELEMENT_TYPE_SELECT = 10;
+ const ELEMENT_TYPE_COPY = 11;
+ const ELEMENT_TYPE_LINKINFO = 12;
+ const ELEMENT_TYPE_LINKDATE = 13;
+ const ELEMENT_TYPE_INSERT = 14;
+
+ const ELEMENT_FORMAT_TEXT = 0;
+ const ELEMENT_FORMAT_HTML = 1;
+ const ELEMENT_FORMAT_WIKI = 2;
+ const ELEMENT_FORMAT_MARKDOWN = 3;
+
+ const ELEMENT_FLAG_HTML_ALLOWED = 1;
+ const ELEMENT_FLAG_ALL_LANGUAGES = 2;
+ const ELEMENT_FLAG_WRITABLE = 4;
+ const ELEMENT_FLAG_WITH_ICON = 8;
+ const ELEMENT_FLAG_INHERIT = 16;
+
+ /**
* Eindeutige ID dieses Elementes
* @type Integer
*/
@@ -133,7 +133,7 @@ class Element
var $allLanguages;
public static $readonlyElementTypeIds = array(
- ELEMENT_TYPE_COPY,ELEMENT_TYPE_LINKINFO,ELEMENT_TYPE_LINKDATE,ELEMENT_TYPE_INFO,ELEMENT_TYPE_INFODATE,ELEMENT_TYPE_CODE,ELEMENT_TYPE_DYNAMIC
+ self::ELEMENT_TYPE_COPY,self::ELEMENT_TYPE_LINKINFO,self::ELEMENT_TYPE_LINKDATE,self::ELEMENT_TYPE_INFO,self::ELEMENT_TYPE_INFODATE,self::ELEMENT_TYPE_CODE,self::ELEMENT_TYPE_DYNAMIC
);
@@ -151,7 +151,7 @@ class Element
*/
var $wiki = false;
- public $format = ELEMENT_FORMAT_TEXT;
+ public $format = self::ELEMENT_FORMAT_TEXT;
var $html = false;
var $decimals = 0;
@@ -188,7 +188,7 @@ class Element
" VALUES ( {elementid},{templateid},{name},{description},{typeid},{flags} ) " );
$flags = 0;
- $flags += ELEMENT_FLAG_WRITABLE * intval($this->writable);
+ $flags += self::ELEMENT_FLAG_WRITABLE * intval($this->writable);
$sql->setInt ( 'elementid' ,$this->elementid );
$sql->setString ( 'name' ,$this->name );
@@ -242,13 +242,13 @@ SQL
$this->subtype = $prop['subtype' ];
$this->dateformat = $prop['dateformat'];
- $this->wiki = $prop['format'] == ELEMENT_FORMAT_WIKI;
+ $this->wiki = $prop['format'] == self::ELEMENT_FORMAT_WIKI;
$this->format = $prop['format'];
- $this->withIcon = $prop['flags'] & ELEMENT_FLAG_WITH_ICON;
- $this->html = $prop['flags'] & ELEMENT_FLAG_HTML_ALLOWED;
- $this->allLanguages = $prop['flags'] & ELEMENT_FLAG_ALL_LANGUAGES;
- $this->writable = $prop['flags'] & ELEMENT_FLAG_WRITABLE;
- $this->inherit = $prop['flags'] & ELEMENT_FLAG_INHERIT;
+ $this->withIcon = $prop['flags'] & self::ELEMENT_FLAG_WITH_ICON;
+ $this->html = $prop['flags'] & self::ELEMENT_FLAG_HTML_ALLOWED;
+ $this->allLanguages = $prop['flags'] & self::ELEMENT_FLAG_ALL_LANGUAGES;
+ $this->writable = $prop['flags'] & self::ELEMENT_FLAG_WRITABLE;
+ $this->inherit = $prop['flags'] & self::ELEMENT_FLAG_INHERIT;
if ( !$this->writable)
$this->withIcon = false;
@@ -291,11 +291,11 @@ SQL
' WHERE id={elementid}' );
$flags = 0;
- $flags += ELEMENT_FLAG_WITH_ICON * intval($this->withIcon );
- $flags += ELEMENT_FLAG_HTML_ALLOWED * intval($this->html );
- $flags += ELEMENT_FLAG_ALL_LANGUAGES * intval($this->allLanguages);
- $flags += ELEMENT_FLAG_WRITABLE * intval($this->writable );
- $flags += ELEMENT_FLAG_INHERIT * intval($this->inherit );
+ $flags += self::ELEMENT_FLAG_WITH_ICON * intval($this->withIcon );
+ $flags += self::ELEMENT_FLAG_HTML_ALLOWED * intval($this->html );
+ $flags += self::ELEMENT_FLAG_ALL_LANGUAGES * intval($this->allLanguages);
+ $flags += self::ELEMENT_FLAG_WRITABLE * intval($this->writable );
+ $flags += self::ELEMENT_FLAG_INHERIT * intval($this->inherit );
$sql->setInt ( 'elementid' ,$this->elementid );
$sql->setInt ( 'templateid' ,$this->templateid );
@@ -452,20 +452,20 @@ SQL
public static function getAvailableTypes()
{
return array(
- ELEMENT_TYPE_TEXT => 'text',
- ELEMENT_TYPE_LONGTEXT => 'longtext',
- ELEMENT_TYPE_SELECT => 'select',
- ELEMENT_TYPE_NUMBER => 'number',
- ELEMENT_TYPE_LINK => 'link',
- ELEMENT_TYPE_DATE => 'date',
- ELEMENT_TYPE_INSERT => 'insert',
- ELEMENT_TYPE_COPY => 'copy',
- ELEMENT_TYPE_LINKINFO => 'linkinfo',
- ELEMENT_TYPE_LINKDATE => 'linkdate',
- ELEMENT_TYPE_CODE => 'code',
- ELEMENT_TYPE_DYNAMIC => 'dynamic',
- ELEMENT_TYPE_INFO => 'info',
- ELEMENT_TYPE_INFODATE => 'infodate'
+ self::ELEMENT_TYPE_TEXT => 'text',
+ self::ELEMENT_TYPE_LONGTEXT => 'longtext',
+ self::ELEMENT_TYPE_SELECT => 'select',
+ self::ELEMENT_TYPE_NUMBER => 'number',
+ self::ELEMENT_TYPE_LINK => 'link',
+ self::ELEMENT_TYPE_DATE => 'date',
+ self::ELEMENT_TYPE_INSERT => 'insert',
+ self::ELEMENT_TYPE_COPY => 'copy',
+ self::ELEMENT_TYPE_LINKINFO => 'linkinfo',
+ self::ELEMENT_TYPE_LINKDATE => 'linkdate',
+ self::ELEMENT_TYPE_CODE => 'code',
+ self::ELEMENT_TYPE_DYNAMIC => 'dynamic',
+ self::ELEMENT_TYPE_INFO => 'info',
+ self::ELEMENT_TYPE_INFODATE => 'infodate'
);
}
@@ -478,10 +478,10 @@ SQL
public static function getAvailableFormats()
{
return array(
- ELEMENT_FORMAT_TEXT => 'text',
- ELEMENT_FORMAT_WIKI => 'wiki',
- ELEMENT_FORMAT_HTML => 'html',
- ELEMENT_FORMAT_MARKDOWN => 'markdown'
+ self::ELEMENT_FORMAT_TEXT => 'text',
+ self::ELEMENT_FORMAT_WIKI => 'wiki',
+ self::ELEMENT_FORMAT_HTML => 'html',
+ self::ELEMENT_FORMAT_MARKDOWN => 'markdown'
);
}
diff --git a/modules/cms-core/model/Template.class.php b/modules/cms-core/model/Template.class.php
@@ -1,391 +1,391 @@
-<?php
-namespace cms\model;
-// OpenRat Content Management System
-// Copyright (C) 2002-2012 Jan Dankert, cms@jandankert.de
-//
-// This program is free software; you can redistribute it and/or
-// modify it under the terms of the GNU General Public License
-// as published by the Free Software Foundation; either version 2
-// of the License, or (at your option) any later version.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with this program; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-
-/**
- * Logische Darstellung eines Templates
- *
- * @author: $Author$
- * @version: $Revision$
- * @package openrat.objects
- */
-class Template
-{
- /**
- * ID dieses Templates
- * @type Integer
- */
- var $templateid = 0;
-
- /**
- * Projekt-ID des aktuell ausgew?hlten Projektes
- * @type Integer
- */
- var $projectid = 0;
-
- /**
- * Logischer Name
- * @type String
- */
- var $name = 'unnamed';
-
- /**
- * ID der Projektvariante
- * @deprecated Zugriff über TemplateModel
- * @type Integer
- */
- var $modelid = 0;
-
- /**
- * Dateierweiterung dieses Templates (abh?ngig von der Projektvariante)
- * @deprecated Zugriff über TemplateModel
- * @type String
- */
- var $extension='';
-
- /**
- * Inhalt des Templates (abh?ngig von der Projektvariante)
- * @deprecated Zugriff über TemplateModel
- * @type String
- */
- var $src='';
-
- // Konstruktor
- function __construct( $templateid='' )
- {
- if ( is_numeric($templateid) )
- $this->templateid = $templateid;
- }
-
-
- /**
- * Laden des Templates aus der Datenbank und f?llen der Objekteigenschaften
- */
- function load()
- {
- $stmt = db()->sql( 'SELECT * FROM {{template}}'.
- ' WHERE id={templateid}' );
- $stmt->setInt( 'templateid',$this->templateid );
- $row = $stmt->getRow();
-
- if ( empty($row) )
- throw new \ObjectNotFoundException("Template not found: ".$this->templateid);
-
- $this->name = $row['name' ];
- $this->projectid = $row['projectid'];
-
- $templateModel = new TemplateModel( $this->templateid, $this->modelid );
- $templateModel->load();
-
- $this->extension = $templateModel->extension;
- $this->src = $templateModel->src;
- }
-
-
- /**
- * Abspeichern des Templates in der Datenbank
- */
- function save()
- {
- if ( $this->name == "" )
- $this->name = lang('GLOBAL_TEMPLATE').' #'.$this->templateid;
-
- $db = db_connection();
-
- $stmt = $db->sql( 'UPDATE {{template}}'.
- ' SET name={name}'.
- ' WHERE id={templateid}' );
- $stmt->setString( 'name' ,$this->name );
- $stmt->setInt ( 'templateid',$this->templateid );
- $stmt->query();
-
- $stmt = $db->sql( 'SELECT COUNT(*) FROM {{templatemodel}}'.
- ' WHERE templateid={templateid}'.
- ' AND projectmodelid={modelid}' );
- $stmt->setInt ( 'templateid' ,$this->templateid );
- $stmt->setInt ( 'modelid' ,$this->modelid );
-
- if ( intval($stmt->getOne()) > 0 )
- {
- // Vorlagen-Quelltext existiert für diese Varianten schon.
- $stmt = $db->sql( 'UPDATE {{templatemodel}}'.
- ' SET extension={extension},'.
- ' text={src} '.
- ' WHERE templateid={templateid}'.
- ' AND projectmodelid={modelid}' );
- }
- else
- {
- // Vorlagen-Quelltext wird für diese Varianten neu angelegt.
- $stmt = $db->sql('SELECT MAX(id) FROM {{templatemodel}}');
- $nextid = intval($stmt->getOne())+1;
-
- $stmt = $db->sql( 'INSERT INTO {{templatemodel}}'.
- ' (id,templateid,projectmodelid,extension,text) '.
- ' VALUES ({id},{templateid},{modelid},{extension},{src}) ');
- $stmt->setInt ( 'id',$nextid );
- }
-
- $stmt->setString( 'extension' ,$this->extension );
- $stmt->setString( 'src' ,$this->src );
- $stmt->setInt ( 'templateid' ,$this->templateid );
- $stmt->setInt ( 'modelid' ,$this->modelid );
-
- $stmt->query();
- }
-
-
- /**
- * Es werden Templates mit einem Inhalt gesucht
- * @param String Suchbegriff
- * @return array Liste der gefundenen Template-IDs
- */
- public static function getTemplateIdsByValue( $text )
- {
- $db = db_connection();
-
- $stmt = $db->sql( 'SELECT templateid FROM {{templatemodel}}'.
- ' WHERE text LIKE {text} ' );
-
- $stmt->setString( 'text' ,'%'.$text.'%' );
-
- return $stmt->getCol();
- }
-
-
- /**
- * Ermitteln aller Elemente zu diesem Template
- * Es wird eine Liste nur mit den Element-IDs ermittelt und zur?ckgegeben
- * @return Array
- */
- function getElementIds()
- {
- $db = db_connection();
-
- $stmt = $db->sql( 'SELECT id FROM {{element}}'.
- ' WHERE templateid={templateid}'.
- ' ORDER BY name ASC' );
- $stmt->setInt( 'templateid',$this->templateid );
- return $stmt->getCol();
- }
-
-
-
- /**
- * Ermitteln aller Elemente zu diesem Template
- * Es wird eine Liste mit den kompletten Elementen ermittelt und zurueckgegeben
- * @return Array
- */
- function getElements()
- {
- $list = array();
- $db = db_connection();
-
- $sql = $db->sql( 'SELECT * FROM {{element}}'.
- ' WHERE templateid={templateid}'.
- ' ORDER BY name ASC' );
- $sql->setInt( 'templateid',$this->templateid );
- foreach($sql->getAll() as $row )
- {
- $e = new Element( $row['id'] );
- $e->setDatabaseRow( $row );
-
- $list[$e->elementid] = $e;
- unset($e);
- }
- return $list;
- }
-
-
-
- /**
- * Ermitteln aller Elemente zu diesem Template
- * Es wird eine Liste mit den kompletten Elementen ermittelt und zurueckgegeben
- * @return Array
- */
- function getWritableElements()
- {
- $list = array();
- $e = new Element();
- $readonlyList = implode(',',Element::$readonlyElementTypeIds);
-
- $db = db_connection();
-
- $sql = $db->sql( <<<SQL
-SELECT * FROM {{element}}
- WHERE templateid={templateid}
- AND typeid NOT IN ($readonlyList)
- ORDER BY name ASC
-SQL
-);
- $sql->setInt ( 'templateid' ,$this->templateid );
- foreach($sql->getAll() as $row )
- {
- $e = new Element( $row['id'] );
- $e->setDatabaseRow( $row );
-
- if (!$e->writable)
- continue;
-
- $list[$e->elementid] = $e;
- unset($e);
- }
- return $list;
- }
-
-
-
- /**
- * Ermitteln aller Elemente zu diesem Template
- * Es wird eine Liste mit den Element-Namen zur?ckgegeben
- * @return Array
- */
- public function getElementNames()
- {
- $db = db_connection();
-
- $sql = $db->sql( 'SELECT id,name FROM {{element}}'.
- ' WHERE templateid={templateid}'.
- ' ORDER BY name ASC' );
- $sql->setInt( 'templateid',$this->templateid );
-
- return $sql->getAssoc();
- }
-
-
- /**
- * Hinzuf?gen eines Elementes
- * @param String Name des Elementes
- */
- public function addElement($name, $description='', $typeid=ELEMENT_TYPE_TEXT )
- {
- $element = new Element();
- $element->name = $name;
- $element->label = $name;
- $element->desc = $description;
- $element->typeid = $typeid;
- $element->templateid = $this->templateid;
- $element->format = ELEMENT_FORMAT_TEXT;
- $element->writable = true;
- $element->add();
-
- return $element;
- }
-
-
- /**
- * Hinzufuegen eines Templates
- * @param String Name des Templates (optional)
- */
- function add( $name='' )
- {
- if ( !empty($name) )
- $this->name = $name;
-
- $db = db_connection();
-
- $sql = $db->sql('SELECT MAX(id) FROM {{template}}');
- $this->templateid = intval($sql->getOne())+1;
-
- $sql = $db->sql( 'INSERT INTO {{template}}'.
- ' (id,name,projectid)'.
- ' VALUES({templateid},{name},{projectid})' );
- $sql->setInt ('templateid',$this->templateid );
- $sql->setString('name' ,$name );
-
- $sql->setInt ('projectid' ,$this->projectid );
-
- $sql->query();
- }
-
-
- /**
- * Ermitteln alles Objekte (=Seiten), welche auf diesem Template basieren.
- *
- * @return Array Liste von Objekt-IDs
- */
- function getDependentObjectIds()
- {
- $db = db_connection();
-
- $sql = $db->sql( 'SELECT objectid FROM {{page}}'.
- ' WHERE templateid={templateid}' );
- $sql->setInt( 'templateid',$this->templateid );
-
- return $sql->getCol();
- }
-
-
- /**
- * Loeschen des Templates
- *
- * Entfernen alle Templateinhalte und des Templates selber
- */
- function delete()
- {
- $db = db_connection();
-
- foreach( $this->getElementIds() as $elementid )
- {
- $element = new Element( $elementid );
- $element->delete();
- }
-
- $stmt = $db->sql( 'DELETE FROM {{templatemodel}}'.
- ' WHERE templateid={templateid}' );
- $stmt->setInt( 'templateid',$this->templateid );
- $stmt->query();
-
- $stmt = $db->sql( 'DELETE FROM {{template}}'.
- ' WHERE id={templateid}' );
- $stmt->setInt( 'templateid',$this->templateid );
- $stmt->query();
- }
-
-
- /**
- * Ermittelt den Mime-Type zu diesem Template.
- *
- * Es wird die Extension des Templates betrachtet und dann mit Hilfe der
- * Konfigurationsdatei 'mime-types.ini' der Mime-Type bestimmt.
- *
- * @return String Mime-Type
- */
- function mimeType()
- {
- global $conf;
- $mime_types = $conf['mime-types'];
-
- // Nur den letzten Teil der Extension auswerten:
- // Aus 'mobile.html' wird nur 'html' verwendet.
- $parts = explode('.',$this->extension);
- $extension = strtolower(array_pop($parts));
-
- if ( !empty($mime_types[$extension]) )
- $this->mime_type = $mime_types[$extension];
- else
- // Wenn kein Mime-Type gefunden, dann Standardwert setzen
- $this->mime_type = 'application/octet-stream';
-
- return( $this->mime_type );
- }
-
-}
-
+<?php
+namespace cms\model;
+// OpenRat Content Management System
+// Copyright (C) 2002-2012 Jan Dankert, cms@jandankert.de
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License
+// as published by the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+
+/**
+ * Logische Darstellung eines Templates
+ *
+ * @author: $Author$
+ * @version: $Revision$
+ * @package openrat.objects
+ */
+class Template
+{
+ /**
+ * ID dieses Templates
+ * @type Integer
+ */
+ var $templateid = 0;
+
+ /**
+ * Projekt-ID des aktuell ausgew?hlten Projektes
+ * @type Integer
+ */
+ var $projectid = 0;
+
+ /**
+ * Logischer Name
+ * @type String
+ */
+ var $name = 'unnamed';
+
+ /**
+ * ID der Projektvariante
+ * @deprecated Zugriff über TemplateModel
+ * @type Integer
+ */
+ var $modelid = 0;
+
+ /**
+ * Dateierweiterung dieses Templates (abh?ngig von der Projektvariante)
+ * @deprecated Zugriff über TemplateModel
+ * @type String
+ */
+ var $extension='';
+
+ /**
+ * Inhalt des Templates (abh?ngig von der Projektvariante)
+ * @deprecated Zugriff über TemplateModel
+ * @type String
+ */
+ var $src='';
+
+ // Konstruktor
+ function __construct( $templateid='' )
+ {
+ if ( is_numeric($templateid) )
+ $this->templateid = $templateid;
+ }
+
+
+ /**
+ * Laden des Templates aus der Datenbank und f?llen der Objekteigenschaften
+ */
+ function load()
+ {
+ $stmt = db()->sql( 'SELECT * FROM {{template}}'.
+ ' WHERE id={templateid}' );
+ $stmt->setInt( 'templateid',$this->templateid );
+ $row = $stmt->getRow();
+
+ if ( empty($row) )
+ throw new \ObjectNotFoundException("Template not found: ".$this->templateid);
+
+ $this->name = $row['name' ];
+ $this->projectid = $row['projectid'];
+
+ $templateModel = new TemplateModel( $this->templateid, $this->modelid );
+ $templateModel->load();
+
+ $this->extension = $templateModel->extension;
+ $this->src = $templateModel->src;
+ }
+
+
+ /**
+ * Abspeichern des Templates in der Datenbank
+ */
+ function save()
+ {
+ if ( $this->name == "" )
+ $this->name = lang('GLOBAL_TEMPLATE').' #'.$this->templateid;
+
+ $db = db_connection();
+
+ $stmt = $db->sql( 'UPDATE {{template}}'.
+ ' SET name={name}'.
+ ' WHERE id={templateid}' );
+ $stmt->setString( 'name' ,$this->name );
+ $stmt->setInt ( 'templateid',$this->templateid );
+ $stmt->query();
+
+ $stmt = $db->sql( 'SELECT COUNT(*) FROM {{templatemodel}}'.
+ ' WHERE templateid={templateid}'.
+ ' AND projectmodelid={modelid}' );
+ $stmt->setInt ( 'templateid' ,$this->templateid );
+ $stmt->setInt ( 'modelid' ,$this->modelid );
+
+ if ( intval($stmt->getOne()) > 0 )
+ {
+ // Vorlagen-Quelltext existiert für diese Varianten schon.
+ $stmt = $db->sql( 'UPDATE {{templatemodel}}'.
+ ' SET extension={extension},'.
+ ' text={src} '.
+ ' WHERE templateid={templateid}'.
+ ' AND projectmodelid={modelid}' );
+ }
+ else
+ {
+ // Vorlagen-Quelltext wird für diese Varianten neu angelegt.
+ $stmt = $db->sql('SELECT MAX(id) FROM {{templatemodel}}');
+ $nextid = intval($stmt->getOne())+1;
+
+ $stmt = $db->sql( 'INSERT INTO {{templatemodel}}'.
+ ' (id,templateid,projectmodelid,extension,text) '.
+ ' VALUES ({id},{templateid},{modelid},{extension},{src}) ');
+ $stmt->setInt ( 'id',$nextid );
+ }
+
+ $stmt->setString( 'extension' ,$this->extension );
+ $stmt->setString( 'src' ,$this->src );
+ $stmt->setInt ( 'templateid' ,$this->templateid );
+ $stmt->setInt ( 'modelid' ,$this->modelid );
+
+ $stmt->query();
+ }
+
+
+ /**
+ * Es werden Templates mit einem Inhalt gesucht
+ * @param String Suchbegriff
+ * @return array Liste der gefundenen Template-IDs
+ */
+ public static function getTemplateIdsByValue( $text )
+ {
+ $db = db_connection();
+
+ $stmt = $db->sql( 'SELECT templateid FROM {{templatemodel}}'.
+ ' WHERE text LIKE {text} ' );
+
+ $stmt->setString( 'text' ,'%'.$text.'%' );
+
+ return $stmt->getCol();
+ }
+
+
+ /**
+ * Ermitteln aller Elemente zu diesem Template
+ * Es wird eine Liste nur mit den Element-IDs ermittelt und zur?ckgegeben
+ * @return Array
+ */
+ function getElementIds()
+ {
+ $db = db_connection();
+
+ $stmt = $db->sql( 'SELECT id FROM {{element}}'.
+ ' WHERE templateid={templateid}'.
+ ' ORDER BY name ASC' );
+ $stmt->setInt( 'templateid',$this->templateid );
+ return $stmt->getCol();
+ }
+
+
+
+ /**
+ * Ermitteln aller Elemente zu diesem Template
+ * Es wird eine Liste mit den kompletten Elementen ermittelt und zurueckgegeben
+ * @return Array
+ */
+ function getElements()
+ {
+ $list = array();
+ $db = db_connection();
+
+ $sql = $db->sql( 'SELECT * FROM {{element}}'.
+ ' WHERE templateid={templateid}'.
+ ' ORDER BY name ASC' );
+ $sql->setInt( 'templateid',$this->templateid );
+ foreach($sql->getAll() as $row )
+ {
+ $e = new Element( $row['id'] );
+ $e->setDatabaseRow( $row );
+
+ $list[$e->elementid] = $e;
+ unset($e);
+ }
+ return $list;
+ }
+
+
+
+ /**
+ * Ermitteln aller Elemente zu diesem Template
+ * Es wird eine Liste mit den kompletten Elementen ermittelt und zurueckgegeben
+ * @return Array
+ */
+ function getWritableElements()
+ {
+ $list = array();
+ $e = new Element();
+ $readonlyList = implode(',',Element::$readonlyElementTypeIds);
+
+ $db = db_connection();
+
+ $sql = $db->sql( <<<SQL
+SELECT * FROM {{element}}
+ WHERE templateid={templateid}
+ AND typeid NOT IN ($readonlyList)
+ ORDER BY name ASC
+SQL
+);
+ $sql->setInt ( 'templateid' ,$this->templateid );
+ foreach($sql->getAll() as $row )
+ {
+ $e = new Element( $row['id'] );
+ $e->setDatabaseRow( $row );
+
+ if (!$e->writable)
+ continue;
+
+ $list[$e->elementid] = $e;
+ unset($e);
+ }
+ return $list;
+ }
+
+
+
+ /**
+ * Ermitteln aller Elemente zu diesem Template
+ * Es wird eine Liste mit den Element-Namen zur?ckgegeben
+ * @return Array
+ */
+ public function getElementNames()
+ {
+ $db = db_connection();
+
+ $sql = $db->sql( 'SELECT id,name FROM {{element}}'.
+ ' WHERE templateid={templateid}'.
+ ' ORDER BY name ASC' );
+ $sql->setInt( 'templateid',$this->templateid );
+
+ return $sql->getAssoc();
+ }
+
+
+ /**
+ * Hinzuf?gen eines Elementes
+ * @param String Name des Elementes
+ */
+ public function addElement($name, $description='', $typeid=Element::ELEMENT_TYPE_TEXT )
+ {
+ $element = new Element();
+ $element->name = $name;
+ $element->label = $name;
+ $element->desc = $description;
+ $element->typeid = $typeid;
+ $element->templateid = $this->templateid;
+ $element->format = Element::ELEMENT_FORMAT_TEXT;
+ $element->writable = true;
+ $element->add();
+
+ return $element;
+ }
+
+
+ /**
+ * Hinzufuegen eines Templates
+ * @param String Name des Templates (optional)
+ */
+ function add( $name='' )
+ {
+ if ( !empty($name) )
+ $this->name = $name;
+
+ $db = db_connection();
+
+ $sql = $db->sql('SELECT MAX(id) FROM {{template}}');
+ $this->templateid = intval($sql->getOne())+1;
+
+ $sql = $db->sql( 'INSERT INTO {{template}}'.
+ ' (id,name,projectid)'.
+ ' VALUES({templateid},{name},{projectid})' );
+ $sql->setInt ('templateid',$this->templateid );
+ $sql->setString('name' ,$name );
+
+ $sql->setInt ('projectid' ,$this->projectid );
+
+ $sql->query();
+ }
+
+
+ /**
+ * Ermitteln alles Objekte (=Seiten), welche auf diesem Template basieren.
+ *
+ * @return Array Liste von Objekt-IDs
+ */
+ function getDependentObjectIds()
+ {
+ $db = db_connection();
+
+ $sql = $db->sql( 'SELECT objectid FROM {{page}}'.
+ ' WHERE templateid={templateid}' );
+ $sql->setInt( 'templateid',$this->templateid );
+
+ return $sql->getCol();
+ }
+
+
+ /**
+ * Loeschen des Templates
+ *
+ * Entfernen alle Templateinhalte und des Templates selber
+ */
+ function delete()
+ {
+ $db = db_connection();
+
+ foreach( $this->getElementIds() as $elementid )
+ {
+ $element = new Element( $elementid );
+ $element->delete();
+ }
+
+ $stmt = $db->sql( 'DELETE FROM {{templatemodel}}'.
+ ' WHERE templateid={templateid}' );
+ $stmt->setInt( 'templateid',$this->templateid );
+ $stmt->query();
+
+ $stmt = $db->sql( 'DELETE FROM {{template}}'.
+ ' WHERE id={templateid}' );
+ $stmt->setInt( 'templateid',$this->templateid );
+ $stmt->query();
+ }
+
+
+ /**
+ * Ermittelt den Mime-Type zu diesem Template.
+ *
+ * Es wird die Extension des Templates betrachtet und dann mit Hilfe der
+ * Konfigurationsdatei 'mime-types.ini' der Mime-Type bestimmt.
+ *
+ * @return String Mime-Type
+ */
+ function mimeType()
+ {
+ global $conf;
+ $mime_types = $conf['mime-types'];
+
+ // Nur den letzten Teil der Extension auswerten:
+ // Aus 'mobile.html' wird nur 'html' verwendet.
+ $parts = explode('.',$this->extension);
+ $extension = strtolower(array_pop($parts));
+
+ if ( !empty($mime_types[$extension]) )
+ $this->mime_type = $mime_types[$extension];
+ else
+ // Wenn kein Mime-Type gefunden, dann Standardwert setzen
+ $this->mime_type = 'application/octet-stream';
+
+ return( $this->mime_type );
+ }
+
+}
+
?>
\ No newline at end of file