Template.class.php

gehe zur Dokumentation dieser Datei
00001 <?php
00002 // ---------------------------------------------------------------------------
00003 // $Id$
00004 // ---------------------------------------------------------------------------
00005 // OpenRat Content Management System
00006 // Copyright (C) 2002 Jan Dankert, jandankert@jandankert.de
00007 //
00008 // This program is free software; you can redistribute it and/or
00009 // modify it under the terms of the GNU General Public License
00010 // as published by the Free Software Foundation; either version 2
00011 // of the License, or (at your option) any later version.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the Free Software
00020 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00021 // ---------------------------------------------------------------------------
00022 // $Log$
00023 // Revision 1.16  2009-04-20 23:24:36  dankert
00024 // Überflüssiges load() in mimeType() entfernt.
00025 //
00026 // Revision 1.15  2007-11-24 14:18:12  dankert
00027 // MimeType in Template ermitteln.
00028 //
00029 // Revision 1.14  2007-11-07 23:29:05  dankert
00030 // Wenn Seite direkt aufgerufen wird, dann sofort Seitenelement anzeigen.
00031 //
00032 // Revision 1.13  2007-10-10 19:08:55  dankert
00033 // Beim Hinzuf?gen von Vorlagen das Kopieren einer anderen Vorlage erlauben. Korrektur beim L?schen von Vorlagen.
00034 //
00035 // Revision 1.12  2006-01-29 17:27:27  dankert
00036 // Methode addElement() mit 2 weiteren Parametern
00037 //
00038 // Revision 1.11  2004/12/30 23:31:52  dankert
00039 // Werte vorbelegen
00040 //
00041 // Revision 1.10  2004/12/30 23:23:21  dankert
00042 // Werte vorbelegen
00043 //
00044 // Revision 1.9  2004/12/27 23:34:20  dankert
00045 // Korrektur add()
00046 //
00047 // Revision 1.8  2004/12/26 01:06:31  dankert
00048 // Perfomanceverbesserung Seite/Elemente
00049 //
00050 // Revision 1.7  2004/12/19 15:23:56  dankert
00051 // Anpassung Session-Funktionen
00052 //
00053 // Revision 1.6  2004/12/18 00:37:50  dankert
00054 // Projekt aus Session lesen
00055 //
00056 // Revision 1.5  2004/12/15 23:16:26  dankert
00057 // Anpassung an Session-Funktionen
00058 //
00059 // Revision 1.4  2004/09/30 20:20:54  dankert
00060 // Beim Speichern Sicherstellen, dass ein Name vorhanden ist
00061 //
00062 // Revision 1.3  2004/05/02 14:41:31  dankert
00063 // Einf?gen package-name (@package)
00064 //
00065 // Revision 1.2  2004/04/25 17:31:46  dankert
00066 // Bei L?schen auch Elemente entfernen
00067 //
00068 // Revision 1.1  2004/04/24 15:15:12  dankert
00069 // Initiale Version
00070 //
00071 // ---------------------------------------------------------------------------
00072 
00073 
00081 class Template
00082 {
00087      var $templateid = 0;
00088 
00093      var $projectid = 0;
00094 
00099      var $name = 'unnamed';
00100      
00105      var $modelid = 0;
00106 
00111      var $extension='';
00112 
00117      var $src='';
00118      
00119      // Konstruktor
00120      function Template( $templateid='' )
00121      {
00122           $model   = Session::getProjectModel();
00123           $project = Session::getProject();
00124           $this->modelid   = $model->modelid;
00125           $this->projectid = $project->projectid;
00126 
00127           if   ( is_numeric($templateid) )
00128                $this->templateid = $templateid;
00129      }
00130 
00131 
00136      function getAll()
00137      {
00138           global $SESS;
00139           $db = db_connection();
00140 
00141           $sql = new Sql( 'SELECT id,name FROM {t_template}'.
00142                           ' WHERE projectid={projectid}'.
00143                           ' ORDER BY name ASC '  );
00144           if   ( isset($this->projectid) )
00145                $sql->setInt( 'projectid',$this->projectid   );
00146           else
00147           {
00148                $project = Session::getProject();
00149                $sql->setInt( 'projectid',$project->projectid );
00150           }
00151 
00152           return $db->getAssoc( $sql->query );
00153      }
00154 
00155 
00159      function load()
00160      {
00161           global $SESS;
00162           $db = db_connection();
00163 
00164           $sql = new Sql( 'SELECT * FROM {t_template}'.
00165                           ' WHERE id={templateid}' );
00166           $sql->setInt( 'templateid',$this->templateid );
00167           $row = $db->getRow( $sql->query );
00168 
00169           $this->name      = $row['name'     ];
00170           $this->projectid = $row['projectid'];
00171 
00172           $sql = new Sql( 'SELECT * FROM {t_templatemodel}'.
00173                           ' WHERE templateid={templateid}'.
00174                           '   AND projectmodelid={modelid}' );
00175           $sql->setInt( 'templateid',$this->templateid );
00176           $sql->setInt( 'modelid'   ,$this->modelid    );
00177           $row = $db->getRow( $sql->query );
00178 
00179           if   ( isset($row['extension']) )
00180           {
00181                $this->extension = $row['extension'];
00182                $this->src       = $row['text'];
00183           }
00184           
00185      }
00186 
00187 
00191      function save()
00192      {
00193           if   ( $this->name == "" )
00194                $this->name = lang('GLOBAL_TEMPLATE').' #'.$this->templateid;
00195 
00196           $db = db_connection();
00197 
00198           $sql = new Sql( 'UPDATE {t_template}'.
00199                           '  SET name={name}'.
00200                           '  WHERE id={templateid}' );
00201           $sql->setString( 'name'      ,$this->name       );
00202           $sql->setInt   ( 'templateid',$this->templateid );
00203           $db->query( $sql->query );
00204 
00205           $sql = new Sql( 'SELECT COUNT(*) FROM {t_templatemodel}'.
00206                           ' WHERE templateid={templateid}'.
00207                           '   AND projectmodelid={modelid}' );
00208           $sql->setInt   ( 'templateid'    ,$this->templateid     );
00209           $sql->setInt   ( 'modelid'       ,$this->modelid );
00210 
00211           if   ( intval($db->getOne($sql->query)) > 0 )
00212           {         
00213                $sql = new Sql( 'UPDATE {t_templatemodel}'.
00214                                '  SET extension={extension},'.
00215                                '      text={src} '.
00216                                ' WHERE templateid={templateid}'.
00217                                '   AND projectmodelid={modelid}' );
00218           }
00219           else
00220           {
00221                $sql = new Sql('SELECT MAX(id) FROM {t_templatemodel}');
00222                $nextid = intval($db->getOne($sql->query))+1;
00223                $sql = new Sql( 'INSERT INTO {t_templatemodel}'.
00224                                '        (id,templateid,projectmodelid,extension,text) '.
00225                                ' VALUES ({id},{templateid},{modelid},{extension},{src}) ');
00226                $sql->setInt   ( 'id',$nextid         );
00227           }
00228 
00229           $sql->setString( 'extension'     ,$this->extension      );
00230           $sql->setString( 'src'           ,$this->src            );
00231           $sql->setInt   ( 'templateid'    ,$this->templateid     );
00232           $sql->setInt   ( 'modelid'       ,$this->modelid        );
00233           
00234           $db->query( $sql->query );
00235      }
00236 
00237 
00243      function getTemplateIdsByValue( $text )
00244      {
00245           $db = db_connection();
00246 
00247           $sql = new Sql( 'SELECT templateid FROM {t_templatemodel}'.
00248                           ' WHERE text LIKE {text} '.
00249                           '   AND projectmodelid={modelid}' );
00250 
00251           $sql->setInt   ( 'modelid',$this->modelid );
00252           $sql->setString( 'text'   ,'%'.$text.'%'  );
00253           
00254           return $db->getCol( $sql->query );
00255      }
00256 
00257 
00263      function getElementIds()
00264      {
00265           $db = db_connection();
00266 
00267           $sql = new Sql( 'SELECT id FROM {t_element}'.
00268                           '  WHERE templateid={templateid}'.
00269                           '  ORDER BY name ASC' );
00270           $sql->setInt( 'templateid',$this->templateid );
00271           return $db->getCol( $sql->query );
00272      }
00273 
00274 
00275 
00281      function getElements()
00282      {
00283           $list = array();
00284           $db = db_connection();
00285 
00286           $sql = new Sql( 'SELECT * FROM {t_element}'.
00287                           '  WHERE templateid={templateid}'.
00288                           '  ORDER BY name ASC' );
00289           $sql->setInt( 'templateid',$this->templateid );
00290           foreach( $db->getAll( $sql->query ) as $row )
00291           {
00292                $e = new Element( $row['id'] );
00293                $e->setDatabaseRow( $row );
00294                
00295                $list[$e->elementid] = $e;
00296                unset($e);
00297           }
00298           return $list;
00299      }
00300 
00301 
00302 
00308      function getWritableElements()
00309      {
00310           $list = array();
00311           $db = db_connection();
00312 
00313           $sql = new Sql( <<<SQL
00314 SELECT * FROM {t_element}
00315   WHERE templateid={templateid}
00316     AND writable=1
00317     AND type NOT IN ({readonlyList})
00318   ORDER BY name ASC
00319 SQL
00320 );
00321           $sql->setInt       ( 'templateid'  ,$this->templateid        );
00322           $e = new Element();
00323           $sql->setStringList( 'readonlyList',$e->readonlyElementNames );
00324           foreach( $db->getAll( $sql->query ) as $row )
00325           {
00326                $e = new Element( $row['id'] );
00327                $e->setDatabaseRow( $row );
00328                
00329                $list[$e->elementid] = $e;
00330                unset($e);
00331           }
00332           return $list;
00333      }
00334 
00335 
00336 
00342      function getElementNames()
00343      {
00344           $db = db_connection();
00345 
00346           $sql = new Sql( 'SELECT id,name FROM {t_element}'.
00347                           '  WHERE templateid={templateid}'.
00348                           '  ORDER BY name ASC' );
00349           $sql->setInt( 'templateid',$this->templateid );
00350 
00351           return $db->getAssoc( $sql->query );
00352      }
00353 
00354 
00359      function addElement( $name,$description='',$type='text' )
00360      {
00361           $element = new Element();
00362           $element->name       = $name;
00363           $element->desc       = $description;
00364           $element->type       = $type;
00365           $element->templateid = $this->templateid;
00366           $element->wiki       = true;
00367           $element->writable   = true;
00368           $element->add();
00369      }
00370 
00371 
00376      function add( $name='' )
00377      {
00378           if   ( !empty($name) )
00379                $this->name = $name;
00380 
00381           $db = db_connection();
00382 
00383           $sql = new Sql('SELECT MAX(id) FROM {t_template}');
00384           $this->templateid = intval($db->getOne($sql->query))+1;
00385 
00386           $sql = new Sql( 'INSERT INTO {t_template}'.
00387                           ' (id,name,projectid)'.
00388                           ' VALUES({templateid},{name},{projectid})' );
00389           $sql->setInt   ('templateid',$this->templateid   );
00390           $sql->setString('name'      ,$name               );
00391 
00392           // Wenn Projektid nicht vorhanden, dann aus Session lesen
00393           if   ( !isset($this->projectid) || intval($this->projectid) == 0 )
00394           {
00395                $project = Session::getProject();
00396                $this->projectid = $project->projectid;
00397           }
00398 
00399           $sql->setInt   ('projectid' ,$this->projectid );
00400 
00401           $db->query( $sql->query );
00402      }
00403 
00404 
00409      function getDependentObjectIds()
00410      {
00411           $db = db_connection();
00412 
00413           $sql = new Sql( 'SELECT objectid FROM {t_page}'.
00414                           '  WHERE templateid={templateid}' );
00415           $sql->setInt( 'templateid',$this->templateid );
00416 
00417           return $db->getCol( $sql->query );
00418      }
00419 
00420 
00426      function delete()
00427      {
00428           $db = db_connection();
00429           
00430           foreach( $this->getElementIds() as $elementid )
00431           {
00432                $element = new Element( $elementid );
00433                $element->delete();
00434           }
00435 
00436           $sql = new Sql( 'DELETE FROM {t_templatemodel}'.
00437                           ' WHERE templateid={templateid}' );
00438           $sql->setInt( 'templateid',$this->templateid );
00439           $db->query( $sql->query );
00440 
00441           $sql = new Sql( 'DELETE FROM {t_template}'.
00442                           ' WHERE id={templateid}' );
00443           $sql->setInt( 'templateid',$this->templateid );
00444           $db->query( $sql->query );
00445      }
00446      
00447      
00453      function mimeType()
00454      {
00455           global $conf;
00456           $mime_types = $conf['mime-types'];
00457 
00458           $extension = strtolower($this->extension);
00459 
00460           if   ( !empty($mime_types[$extension]) )
00461                $this->mime_type = $mime_types[$extension];
00462           else
00463                // Wenn kein Mime-Type gefunden, dann Standartwert setzen
00464                $this->mime_type = 'application/octet-stream';
00465                
00466           return( $this->mime_type );
00467      }
00468      
00469 }
00470 
00471 ?>

Erzeugt am Thu May 14 00:55:48 2009 für OpenRat von  doxygen 1.5.8