File modules/cms/model/Folder.class.php
Last commit: Sun Jun 5 22:14:24 2022 +0200 Jan Dankert Some fixups: New Icons; better support classes for DSL.
1 <?php 2 3 namespace cms\model; 4 5 use cms\base\Configuration; 6 use cms\base\DB as Db; 7 use Exception; 8 9 10 /** 11 * Darstellen eines Ordners. 12 * 13 * @author Jan Dankert 14 */ 15 class Folder extends BaseObject 16 { 17 public $folderid; 18 19 var $subfolders = array(); 20 var $filenames = true; 21 22 23 function __construct( $objectid='' ) 24 { 25 parent::__construct( $objectid ); 26 $this->isFolder = true; 27 $this->typeid = BaseObject::TYPEID_FOLDER; 28 } 29 30 31 protected function add() 32 { 33 parent::add(); 34 35 $db = \cms\base\DB::get(); 36 37 $sql = $db->sql('SELECT MAX(id) FROM {{folder}}'); 38 $this->folderid = intval($sql->getOne())+1; 39 40 $sql = $db->sql('INSERT INTO {{folder}}'. 41 ' (id,objectid)'. 42 ' VALUES( {folderid},{objectid} )' ); 43 $sql->setInt ('folderid' ,$this->folderid ); 44 $sql->setInt ('objectid' ,$this->objectid ); 45 46 $sql->execute(); 47 } 48 49 50 51 public function hasFilename( $filename ) 52 { 53 $db = \cms\base\DB::get(); 54 55 $sql = $db->sql('SELECT COUNT(*) FROM {{object}}'.' WHERE parentid={objectid} AND filename={filename}'); 56 57 if ( intval($this->objectid)== 0 ) 58 $sql->setNull('objectid'); 59 else 60 $sql->setString('objectid', $this->objectid); 61 62 $sql->setString('filename', $filename ); 63 64 return( $sql->getOne() > 0 ); 65 } 66 67 68 69 70 public function setOrderId( $orderid ) 71 { 72 $db = \cms\base\DB::get(); 73 74 $sql = $db->sql('UPDATE {{folder}} '. 75 ' SET orderid={orderid}'. 76 ' WHERE id={folderid}'); 77 $sql->setInt('folderid',$this->folderid); 78 $sql->setInt('orderid' ,$orderid ); 79 80 $sql->execute(); 81 } 82 83 84 85 // function getSubFolders() 86 // { 87 // global $SESS; 88 // $db = \cms\base\DB::get(); 89 // 90 // $sql = $db->sql('SELECT id FROM {{folder}}'. 91 // ' WHERE parentid={folderid}'. 92 // ' AND projectid={projectid}'. 93 // ' ORDER BY orderid ASC' ); 94 // $sql->setInt('folderid' ,$SESS['folderid' ]); 95 // $sql->setInt('projectid',$SESS['projectid']); 96 // 97 // return( $sql->getCol( $sql )); 98 // } 99 100 101 // Liest alle Objekte in diesem Ordner 102 function getObjectIds() 103 { 104 $db = \cms\base\DB::get(); 105 106 $sql = $db->sql('SELECT id FROM {{object}}'. 107 ' WHERE parentid={objectid}'. 108 ' ORDER BY orderid ASC' ); 109 $sql->setInt('objectid' ,$this->objectid ); 110 111 return( $sql->getCol() ); 112 } 113 114 115 116 /** 117 * Liest alle Objekte in diesem Ordner 118 * @return array[Object] Objekte 119 */ 120 function getObjects() 121 { 122 $db = \cms\base\DB::get(); 123 124 $sql = $db->sql( <<<SQL 125 SELECT {{object}}.* 126 FROM {{object}} 127 WHERE parentid={objectid} 128 ORDER BY orderid ASC 129 SQL 130 ); 131 $sql->setInt('objectid' ,$this->objectid ); 132 $res = $sql->getAll(); 133 134 $liste = array(); 135 foreach( $res as $row ) 136 { 137 $o = new BaseObject( $row['id'] ); 138 $o->setDatabaseRow( $row ); 139 $liste[] = $o; 140 } 141 142 return $liste; 143 } 144 145 146 // Liest alle Objekte in diesem Ordner 147 function getObjectIdsByType() 148 { 149 $db = \cms\base\DB::get(); 150 151 $sql = $db->sql('SELECT id FROM {{object}}'. 152 ' WHERE parentid={objectid}'. 153 ' ORDER BY typeid,orderid ASC' ); 154 $sql->setInt('objectid' ,$this->objectid ); 155 156 return( $sql->getCol() ); 157 } 158 159 160 // Liest alle Objekte in diesem Ordner sortiert nach dem Namen (nicht Dateinamen!) 161 function getChildObjectIdsByName( $languageId ) 162 { 163 $db = \cms\base\DB::get(); 164 165 $sql = $db->sql('SELECT {{object}}.id FROM {{object}}'. 166 ' LEFT JOIN {{name}} ON {{object}}.id={{name}}.objectid AND {{name}}.languageid={languageid} '. 167 ' WHERE parentid={objectid}'. 168 ' ORDER BY {{name}}.name,{{object}}.filename ASC'); 169 $sql->setInt('objectid' , $this->objectid ); 170 $sql->setInt('languageid', $languageId); 171 return( $sql->getCol() ); 172 } 173 174 175 // Liest alle Objekte in diesem Ordner 176 function getObjectIdsByLastChange() 177 { 178 $db = \cms\base\DB::get(); 179 180 $sql = $db->sql('SELECT id FROM {{object}}'. 181 ' WHERE parentid={objectid}'. 182 ' ORDER BY lastchange_date,orderid ASC' ); 183 $sql->setInt('projectid',$this->projectid ); 184 $sql->setInt('objectid' ,$this->objectid ); 185 186 return( $sql->getCol() ); 187 } 188 189 190 function getObjectIdByFileName( $filename ) 191 { 192 $db = \cms\base\DB::get(); 193 194 $sql = $db->sql('SELECT id FROM {{object}}'. 195 ' WHERE parentid={objectid}'. 196 ' AND filename={filename}' ); 197 $sql->setInt ('objectid' ,$this->objectid ); 198 $sql->setString('filename' ,$filename ); 199 200 return( intval($sql->getOne()) ); 201 } 202 203 204 205 public function getPages() 206 { 207 $db = \cms\base\DB::get(); 208 209 $sql = $db->sql('SELECT id FROM {{object}} '. 210 ' WHERE parentid={objectid} AND typeid='.BaseObject::TYPEID_PAGE. 211 ' ORDER BY orderid ASC' ); 212 $sql->setInt( 'objectid' ,$this->objectid ); 213 214 return $sql->getCol(); 215 } 216 217 218 /** 219 * Ermittelt die erste Seite oder Verkn�pfung in diesem Ordner. 220 * 221 * @return BaseObject Objekt 222 */ 223 public function getFirstPage() 224 { 225 $db = \cms\base\DB::get(); 226 227 $sql = $db->sql('SELECT id FROM {{object}} '. 228 ' WHERE parentid={objectid}'. 229 ' AND (typeid='.BaseObject::TYPEID_PAGE.')'. 230 ' ORDER BY orderid ASC' ); 231 $sql->setInt( 'objectid' ,$this->objectid ); 232 233 $oid = intval($sql->getOne()); 234 235 if ( $oid != 0 ) 236 $o = new BaseObject($oid); 237 else 238 $o = null; 239 240 return $o; 241 } 242 243 244 /** 245 * Ermittelt die erste Seite oder Verkn�pfung in diesem Ordner. 246 * 247 * @return BaseObject Objekt 248 */ 249 function getFirstPageOrLink() 250 { 251 $db = \cms\base\DB::get(); 252 253 $sql = $db->sql('SELECT id FROM {{object}} '. 254 ' WHERE parentid={objectid}'. 255 ' AND (typeid='.BaseObject::TYPEID_PAGE.' OR typeid='.BaseObject::TYPEID_LINK.')'. 256 ' ORDER BY orderid ASC' ); 257 $sql->setInt( 'objectid' ,$this->objectid ); 258 259 $oid = intval($sql->getOne()); 260 261 if ( $oid != 0 ) 262 $o = new BaseObject($oid); 263 else 264 $o = null; 265 266 return $o; 267 } 268 269 270 function getLastPageOrLink() 271 { 272 $db = \cms\base\DB::get(); 273 274 $sql = $db->sql('SELECT id FROM {{object}} '. 275 ' WHERE parentid={objectid}'. 276 ' AND (typeid='.BaseObject::TYPEID_PAGE.' OR typeid='.BaseObject::TYPEID_LINK.')'. 277 ' ORDER BY orderid DESC' ); 278 $sql->setInt( 'objectid' ,$this->objectid ); 279 280 $oid = intval($sql->getOne()); 281 282 if ( $oid != 0 ) 283 $o = new BaseObject($oid); 284 else 285 $o = null; 286 287 return $o; 288 } 289 290 291 /** 292 * Returns a list of files. 293 * @return array 294 */ 295 public function getFiles() 296 { 297 $sql = DB::sql( <<<SQL 298 SELECT id FROM {{object}} 299 WHERE parentid={objectid} 300 AND typeid IN ( {typefile},{typeimage},{typetext} ) 301 ORDER BY orderid ASC 302 SQL 303 ); 304 $sql->setInt( 'objectid' ,$this->objectid ); 305 $sql->setInt( 'typefile' ,BaseObject::TYPEID_FILE ); 306 $sql->setInt( 'typeimage' ,BaseObject::TYPEID_IMAGE ); 307 $sql->setInt( 'typetext' ,BaseObject::TYPEID_TEXT ); 308 309 return $sql->getCol(); 310 } 311 312 313 314 /** 315 * Liefert eine Liste von allen Dateien in diesem Ordner. 316 * 317 * @return array Schl�ssel=Objekt-Id, Wert=Dateiname 318 */ 319 function getFileFilenames() 320 { 321 $db = \cms\base\DB::get(); 322 323 $sql = $db->sql('SELECT id,filename FROM {{object}} '. 324 ' WHERE parentid={objectid} AND typeid='.BaseObject::TYPEID_FILE. 325 ' ORDER BY orderid ASC' ); 326 $sql->setInt( 'objectid' ,$this->objectid ); 327 328 return $sql->getAssoc(); 329 } 330 331 332 function getLinks() 333 { 334 $db = \cms\base\DB::get(); 335 336 $sql = $db->sql('SELECT id FROM {{object}} '. 337 ' WHERE parentid={objectid} AND typeid='.BaseObject::TYPEID_LINK. 338 ' ORDER BY orderid ASC' ); 339 $sql->setInt( 'objectid' ,$this->objectid ); 340 341 return $sql->getCol(); 342 } 343 344 345 346 /** 347 * Rechte f?r diesen Ordner hinzuf?gen. 348 * @param $rights 349 * @param bool $inherit 350 * @deprecated unused? bad code. 351 */ 352 function addrights( $rights,$inherit = true ) 353 { 354 global $SESS; 355 356 $SESS['rights'][$rights['projectid']][$this->folderid]['show'] = true; 357 358 if ($rights['read'] == '1') 359 $SESS['rights'][$rights['projectid']][$this->folderid]['read'] = 1; 360 if ($rights['write'] == '1') 361 $SESS['rights'][$rights['projectid']][$this->folderid]['write'] = 1; 362 if ($rights['create'] == '1') 363 $SESS['rights'][$rights['projectid']][$this->folderid]['create'] = 1; 364 if ($rights['delete'] == '1') 365 $SESS['rights'][$rights['projectid']][$this->folderid]['delete'] = 1; 366 if ($rights['publish'] == '1') 367 $SESS['rights'][$rights['projectid']][$this->folderid]['publish'] = 1; 368 369 // Rechte auf Unterordner vererben 370 // sowie f?r ?bergeordnete Ordner die Anzeige erzwingen 371 if ( $inherit ) 372 { 373 // ?bergeordnete Ordner ermitteln 374 $parentfolder = $this->parentObjectFileNames(); 375 376 // ?bergeordnete Ordner immer anzeigen (Schalter 'show'=true) 377 foreach( $parentfolder as $folderid=>$name ) 378 { 379 $f = new Folder( $folderid ); 380 $f->projectid = $this->projectid; 381 $f->addrights( array('projectid'=>$rights['projectid']),false ); 382 unset($f); 383 } 384 385 $f = new Folder( 'null' ); 386 $f->projectid = $this->projectid; 387 $f->addrights( array('projectid'=>$rights['projectid']),false ); 388 unset($f); 389 390 391 // Unterordner ermitteln 392 //echo "Kurz vor subfolderberechnung, folderid ist ".$this->folderid.'<br>'; 393 $subfolder = $this->subfolder(); 394 395 // Rechte weitergeben 396 foreach( $subfolder as $folderid=>$name ) 397 { 398 $f = new Folder( $folderid ); 399 $f->projectid = $this->projectid; 400 $f->addrights( $rights,false ); 401 unset($f); 402 } 403 } 404 } 405 406 407 408 409 // Ermitteln aller Unterordner 410 // 411 public function subfolder() 412 { 413 $stmt = Db::sql(<<<SQL 414 415 SELECT id FROM {{object}} 416 WHERE parentid={objectid} AND typeid={typeid} 417 ORDER BY orderid ASC 418 SQL 419 ); 420 421 $stmt->setInt( 'objectid' ,$this->objectid ); 422 $stmt->setInt( 'typeid' ,BaseObject::TYPEID_FOLDER ); 423 424 $this->subfolders = $stmt->getCol(); 425 426 return $this->subfolders; 427 } 428 429 430 431 public function getSubfolderFilenames() 432 { 433 $stmt = Db::sql(<<<SQL 434 SELECT id,filename FROM {{object}} 435 WHERE parentid={objectid} AND typeid={typeid} 436 ORDER BY orderid ASC 437 SQL 438 ); 439 440 $stmt->setInt( 'objectid' ,$this->objectid ); 441 $stmt->setInt( 'typeid' ,BaseObject::TYPEID_FOLDER ); 442 443 return $stmt->getAssoc(); 444 } 445 446 447 448 /** 449 * Ermitteln aller Unterordner (rekursives Absteigen). 450 * 451 */ 452 function getAllSubFolderIds() 453 { 454 $ids = array(); 455 456 foreach( $this->getSubFolderIds() as $id ) 457 { 458 $ids[] = $id; 459 460 $f = new Folder( $id ); 461 if ( !empty($this->projectid) ) 462 $f->projectid = $this->projectid; 463 464 // Rekursiver Aufruf für alle Unterordner 465 foreach( $f->getAllSubFolderIds() as $xid ) 466 { 467 $ids[] = $xid; 468 } 469 } 470 471 return $ids; 472 } 473 474 475 /** 476 * Loeschen dieses Ordners. 477 * Der Ordner wird nur geloescht, wenn er keine Unterelemente mehr enth?lt. 478 * Zum Loeschen inklusive Unterelemente dient die Methode deleteAll() 479 * 480 * @throws \Exception if folder contains elements 481 */ 482 function delete() 483 { 484 $db = \cms\base\DB::get(); 485 486 // Nur loeschen, wenn es keine Unterelemente gibt 487 if ( count( $this->getObjectIds() ) == 0 ) 488 { 489 $sql = $db->sql( 'UPDATE {{element}} '. 490 ' SET folderobjectid=NULL '. 491 ' WHERE folderobjectid={objectid}' ); 492 $sql->setInt('objectid',$this->objectid); 493 $sql->execute(); 494 495 $sql = $db->sql( 'DELETE FROM {{folder}} '. 496 ' WHERE objectid={objectid}' ); 497 $sql->setInt('objectid',$this->objectid); 498 $sql->execute(); 499 500 parent::delete(); 501 } 502 else { 503 throw new \RuntimeException('There are children in the folder '.$this->objectid.'.'); 504 } 505 } 506 507 508 /** 509 * Rekursives loeschen aller Inhalte 510 * 511 * Loeschen aller Inhalte dieses Ordners 512 * inclusive aller Unterelemente 513 */ 514 function deleteAll() 515 { 516 $db = \cms\base\DB::get(); 517 518 // L?schen aller Unterordner 519 foreach( $this->subfolder() as $folderid ) 520 { 521 $folder = new Folder( $folderid ); 522 { 523 $folder->deleteAll(); 524 } 525 } 526 527 // L?schen aller Seiten,Verknuepfungen und Dateien in 528 // diesem Ordner 529 foreach( $this->getObjectIds() as $oid ) 530 { 531 $object = new BaseObject( $oid ); 532 { 533 $object->load(); 534 535 if ( $object->isPage ) 536 { 537 $page = new Page( $oid ); 538 $page->load(); 539 $page->delete(); 540 } 541 542 if ( $object->isLink ) 543 { 544 $link = new Link( $oid ); 545 $link->load(); 546 $link->delete(); 547 } 548 549 if ( $object->isUrl ) 550 { 551 $url = new Url( $oid ); 552 $url->load(); 553 $url->delete(); 554 } 555 556 if ( $object->isFile ) 557 { 558 $file = new File( $oid ); 559 $file->load(); 560 $file->delete(); 561 } 562 if ( $object->isImage ) 563 { 564 $file = new Image( $oid ); 565 $file->load(); 566 $file->delete(); 567 } 568 if ( $object->isText ) 569 { 570 $file = new Text( $oid ); 571 $file->load(); 572 $file->delete(); 573 } 574 } 575 } 576 577 // Zum Abschluss den aktuellen Ordner loeschen 578 $this->delete(); 579 } 580 581 582 function getSubFolderIds() 583 { 584 return $this->subfolder(); 585 } 586 587 588 589 /** 590 * Ermittelt die letzten Änderung in diesem Ordner. 591 * @return array[Objektid]=Array()) 592 */ 593 public function getLastChanges() 594 { 595 $sql = DB::sql( <<<SQL 596 SELECT {{object}}.id as objectid, 597 {{object}}.lastchange_date as lastchange_date, 598 {{object}}.filename as filename, 599 {{object}}.typeid as typeid, 600 {{user}}.name as username, 601 {{user}}.id as userid, 602 {{user}}.mail as usermail, 603 {{user}}.fullname as userfullname 604 FROM {{object}} 605 LEFT JOIN {{user}} 606 ON {{user}}.id = {{object}}.lastchange_userid 607 WHERE {{object}}.parentid = {folderid} 608 ORDER BY {{object}}.lastchange_date DESC 609 SQL 610 ); 611 612 // Variablen setzen. 613 $sql->setInt( 'folderid', $this->objectid ); 614 615 return $sql->getAll(); 616 } 617 618 619 /** 620 * Stellt fest, ob der Ordner Unterelemente besitzt. 621 * @return bool 622 */ 623 public function hasChildren() 624 { 625 return count($this->getObjectIds()) > 0; 626 } 627 628 } 629
Downloadmodules/cms/model/Folder.class.php
History Sun, 5 Jun 2022 22:14:24 +0200 Jan Dankert Some fixups: New Icons; better support classes for DSL. Mon, 25 Apr 2022 03:26:21 +0200 Jan Dankert New: New Project may be created with sample data. Sun, 5 Dec 2021 20:33:24 +0100 dankert Cleanup: Removed unusable properties from class 'Value' and 'BaseObject'. Sun, 14 Mar 2021 02:14:31 +0100 Jan Dankert Fix: The public filename of files must contain their path... Sun, 7 Mar 2021 00:10:20 +0100 Jan Dankert Refactoring: Hopefully more performance while accessing the database resultsets. Wed, 18 Nov 2020 01:46:36 +0100 Jan Dankert Refactoring of model classes: New method persist() and some other cleanups. Sun, 1 Nov 2020 00:36:50 +0100 Jan Dankert Refactoring: Only using the configuration object. Sat, 26 Sep 2020 12:20:43 +0200 Jan Dankert Refactoring: No global variables like $SESS any more. All constants are capsulated by classes. Sat, 26 Sep 2020 04:26:55 +0200 Jan Dankert Refactoring: read configuration values with a class. Sat, 26 Sep 2020 02:26:39 +0200 Jan Dankert Refactoring: No global functions any more, the database object is read from the Db class. Wed, 23 Sep 2020 01:04:05 +0200 Jan Dankert Cleanup of deprecated methods and deprecated class attributes. Mon, 21 Sep 2020 22:48:59 +0200 Jan Dankert Complexe refactoring: Moving all generation logic from the model (Value,Page,File) to generators classes. Fri, 18 Sep 2020 23:04:13 +0200 Jan Dankert Refactoring: Renaming module "cms/publish" to "cms/generator" Sun, 23 Feb 2020 04:01:30 +0100 Jan Dankert Refactoring with Namespaces for the cms modules, part 1: moving.