Model.class.php
gehe zur Dokumentation dieser Datei00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
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
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
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
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
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
00210 function setDefault()
00211 {
00212 global $SESS;
00213 $db = db_connection();
00214
00215
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
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
00241 $sql = new Sql( 'DELETE FROM {t_model} WHERE id={modelid}' );
00242 $sql->setInt( 'modelid',$this->modelid );
00243 $db->query( $sql->query );
00244
00245
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 ?>