Model.class.php

gehe zur Dokumentation dieser Datei
00001 <?php
00002 // ---------------------------------------------------------------------------
00003 // $Id$
00004 // ---------------------------------------------------------------------------
00005 // OpenRat Content Management System
00006 // Copyright (C) 2002-2004 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.5  2007-11-24 12:16:15  dankert
00024 // Methoden "available()" zum Pr?fen auf die Existenz der Id.
00025 //
00026 // Revision 1.4  2004/12/15 23:18:36  dankert
00027 // Anpassung an Session-Funktionen
00028 //
00029 // Revision 1.3  2004/11/10 22:46:27  dankert
00030 // *** empty log message ***
00031 //
00032 // Revision 1.2  2004/04/25 12:58:24  dankert
00033 // Spalte "selflink" entfernt
00034 //
00035 // ---------------------------------------------------------------------------
00036 
00047 class Model
00048 {
00049      var $modelid = 0;
00050      var $error      = '';
00051      var $projectid;
00052 
00053      var $name      = '';
00054      var $isDefault = false;
00055 
00056 
00060      function Model( $modelid='' )
00061      {
00062           if   ( is_numeric($modelid) )
00063                $this->modelid = $modelid;
00064      }
00065 
00066      
00070      function available( $id )
00071      {
00072           $db = db_connection();
00073 
00074           $sql = new Sql('SELECT 1 FROM {t_model} '.
00075                          ' WHERE id={id}');
00076           $sql->setInt('id' ,$id  );
00077 
00078           return intval($db->getOne($sql->query)) == 1;
00079      }
00080      
00081 
00082      
00083 
00087      function getAll()
00088      {
00089           global $SESS;
00090           $db = db_connection();
00091 
00092           $sql = new Sql( "SELECT id,name FROM {t_model} ".
00093                           "   WHERE projectid = {projectid} ".
00094                           "   ORDER BY name" );
00095 
00096           if   ( isset($this) )
00097                $sql->setInt('projectid',$this->projectid );
00098           else $sql->setInt('projectid',$SESS['projectid'] );
00099 
00100           return $db->getAssoc( $sql->query );
00101      }
00102 
00103 
00107      function load()
00108      {
00109           $db = db_connection();
00110 
00111           $sql = new Sql( 'SELECT * FROM {t_model}'.
00112                           ' WHERE id={modelid}' );
00113           $sql->setInt( 'modelid',$this->modelid );
00114 
00115           $row = $db->getRow( $sql->query );
00116 
00117           $this->name      = $row['name'     ];
00118           $this->projectid = $row['projectid'];
00119      
00120           if   ( $row['is_default'] == '1' )
00121                $this->isDefault = true;
00122           else $this->isDefault = false;
00123      }
00124 
00125 
00129      function save()
00130      {
00131           $db = db_connection();
00132 
00133           // Gruppe speichern      
00134           $sql = new Sql( 'UPDATE {t_model} '.
00135                           '  SET name      = {name} '.
00136                           '  WHERE id={modelid}' );
00137           $sql->setString( 'name'     ,$this->name    );
00138 
00139           $sql->setInt( 'modelid',$this->modelid );
00140 
00141           // Datenbankabfrage ausfuehren
00142           $db->query( $sql->query );
00143      }
00144 
00145 
00152      function getProperties()
00153      {
00154           return Array( 'modelid'  =>$this->modelid,
00155                         'projectid'=>$this->projectid,
00156                         'isDefault'=>$this->isDefault,
00157                         'name'     =>$this->name );
00158      }
00159 
00160 
00165      function add( $name = '' )
00166      {
00167           if   ( $name != '' )
00168                $this->name = $name;
00169           
00170           $db = db_connection();
00171 
00172           $sql = new Sql('SELECT MAX(id) FROM {t_model}');
00173           $this->modelid = intval($db->getOne($sql->query))+1;
00174 
00175           // Modell hinzuf?gen
00176           $sql = new Sql( 'INSERT INTO {t_model} '.
00177                           "(id,projectid,name,extension,is_default) VALUES( {modelid},{projectid},{name},'',0 )");
00178 
00179           $sql->setInt   ('modelid'  ,$this->modelid   );
00180           $sql->setInt   ('projectid',$this->projectid );
00181           $sql->setString('name'     ,$this->name      );
00182 
00183           // Datenbankbefehl ausfuehren
00184           $db->query( $sql->query );
00185      }
00186 
00187 
00188      function getDefaultId()
00189      {
00190           global $SESS;
00191           $db = db_connection();
00192 
00193           $sql = new Sql( 'SELECT id FROM {t_model} '.
00194                           '  WHERE projectid={projectid}'.
00195                           '   ORDER BY is_default DESC' );
00196           if   ( isset($this->projectid) )
00197                $sql->setInt('projectid',$this->projectid );
00198           else
00199           {
00200                $project = Session::getProject();
00201                $sql->setInt('projectid',$project->projectid);
00202           }
00203           
00204           return $db->getOne( $sql->query );
00205      }
00206 
00207 
00208 
00209      // Diese Sprache als 'default' markieren.
00210      function setDefault()
00211      {
00212           global $SESS;
00213           $db = db_connection();
00214 
00215           // Zuerst alle auf nicht-Standard setzen
00216           $sql = new Sql( 'UPDATE {t_model} '.
00217                           '  SET is_default = 0 '.
00218                           '  WHERE projectid={projectid}' );
00219           $sql->setInt('projectid',$this->projectid );
00220           $db->query( $sql->query );
00221      
00222           // Jetzt die gew?nschte Sprachvariante auf Standard setzen
00223           $sql = new Sql( 'UPDATE {t_model} '.
00224                           '  SET is_default = 1 '.
00225                           '  WHERE id={modelid}' );
00226           $sql->setInt('modelid',$this->modelid );
00227           $db->query( $sql->query );
00228      }
00229 
00230 
00236      function delete()
00237      {
00238           $db = db_connection();
00239 
00240           // Modell l?schen
00241           $sql = new Sql( 'DELETE FROM {t_model} WHERE id={modelid}' );
00242           $sql->setInt( 'modelid',$this->modelid );
00243           $db->query( $sql->query );
00244 
00245           // Anderes Modell auf "Default" setzen (sofern vorhanden)
00246           $sql = new Sql( 'SELECT id FROM {t_model} WHERE projectid={projectid}' );
00247           $sql->setInt( 'projectid',$this->projectid );
00248           $new_default_modelid = $db->getOne( $sql->query );
00249 
00250           $sql = new Sql( 'UPDATE {t_model} SET is_default=1 WHERE id={modelid}' );
00251           $sql->setInt( 'modelid',$new_default_modelid );
00252           $db->query( $sql->query );
00253      }
00254 }
00255 
00256 ?>

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