Template.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
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
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
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
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
00464 $this->mime_type = 'application/octet-stream';
00465
00466 return( $this->mime_type );
00467 }
00468
00469 }
00470
00471 ?>