File modules/cms/model/Project.class.php

Last commit: Thu Feb 16 01:04:38 2023 +0100	Jan Dankert	New: Tags for base objects.
1 <?php 2 3 namespace cms\model; 4 5 use cms\base\DB; 6 use database\Database; 7 use logger\Logger; 8 use util\FileUtils; 9 use util\Request; 10 use util\Session; 11 12 13 /** 14 * Darstellen eines Projektes 15 * 16 * @author Jan Dankert 17 * @package openrat.objects 18 */ 19 class Project extends ModelBase 20 { 21 22 const FLAG_CUT_INDEX = 1; 23 const FLAG_CONTENT_NEGOTIATION = 2; 24 const FLAG_PUBLISH_FILE_EXTENSION = 4; 25 const FLAG_PUBLISH_PAGE_EXTENSION = 8; 26 const FLAG_LINK_ABSOLUTE = 16; 27 28 // Eigenschaften 29 public $projectid; 30 public $name; 31 public $target_dir; 32 public $ftp_url; 33 34 /** 35 * Hostname 36 * @var string 37 */ 38 public $url; 39 40 public $ftp_passive; 41 public $cmd_after_publish; 42 43 44 /** 45 * @var boolean 46 */ 47 public $content_negotiation = false; 48 49 /** 50 * Should links to index pages contain the filename? 51 * 52 * @var boolean 53 */ 54 public $cut_index = false; 55 56 /** 57 * Link to files with extension 58 * 59 * @var boolean 60 */ 61 public $publishFileExtension = true; 62 63 /** 64 * link to pages with extension 65 * @var boolean 66 */ 67 public $publishPageExtension = true; 68 69 public $linkAbsolute; 70 71 72 73 // Konstruktor 74 public function __construct( $projectid='' ) 75 { 76 if ( intval($projectid) != 0 ) 77 $this->projectid = $projectid; 78 } 79 80 81 private static $cache = array(); 82 83 /** 84 * @param $projectid 85 * @return Project 86 * @throws \util\exception\ObjectNotFoundException 87 */ 88 public static function create($projectid) 89 { 90 if ( empty( Project::$cache[ $projectid ] ) ) 91 { 92 $project = new Project( $projectid ); 93 94 Project::$cache[ $projectid ] = $project->load(); 95 } 96 return Project::$cache[ $projectid ]; 97 } 98 99 100 /** 101 * Stellt fest, ob die angegebene Projekt-Id existiert. 102 * @param $id int Projekt-Id 103 * @return boolean 104 * 105 */ 106 public function isAvailable($id ) 107 { 108 $db = \cms\base\DB::get(); 109 110 $sql = $db->sql('SELECT 1 FROM {{project}} '. 111 ' WHERE id={id}'); 112 $sql->setInt('id' ,$id ); 113 114 return intval($sql->getOne()) == 1; 115 } 116 117 118 /** 119 * Liefert alle verf?gbaren Projekte. 120 * @return array 121 */ 122 public static function getAllProjects() 123 { 124 $db = \cms\base\DB::get(); 125 $sql = $db->sql( 'SELECT id,name FROM {{project}} '. 126 ' ORDER BY name' ); 127 128 return $sql->getAssoc(); 129 } 130 131 132 // Liefert alle verf?gbaren Projekt-Ids 133 public function getAllProjectIds() 134 { 135 $db = \cms\base\DB::get(); 136 $sql = $db->sql( 'SELECT id FROM {{project}} '. 137 ' ORDER BY name' ); 138 139 return $sql->getCol(); 140 } 141 142 143 /** 144 * Liefert die Sprachen des Projektes. 145 * 146 * @return array Id->Name 147 */ 148 public function getLanguages() 149 { 150 $db = \cms\base\DB::get(); 151 152 $sql = $db->sql( 'SELECT id,name FROM {{language}}'. 153 ' WHERE projectid={projectid} '. 154 ' ORDER BY name' ); 155 $sql->setInt ('projectid',$this->projectid); 156 157 return $sql->getAssoc(); 158 } 159 160 161 162 /** 163 * Get all tags 164 * @return array 165 */ 166 public function getTags() 167 { 168 $sql = Db::sql( <<<SQL 169 SELECT id,name 170 FROM {{tag}} 171 WHERE projectid = {projectid} 172 ORDER BY name 173 SQL 174 ); 175 $sql->setInt('projectid',$this->projectid); 176 177 return $sql->getAssoc(); 178 } 179 180 181 182 183 /** 184 * Liefert die Sprachen des Projektes. 185 * 186 * @return array Id->Name 187 */ 188 public function getLanguageISOCodes() 189 { 190 $db = \cms\base\DB::get(); 191 192 $sql = $db->sql( 'SELECT id,isocode FROM {{language}}'. 193 ' WHERE projectid={projectid} '. 194 ' ORDER BY isocode' ); 195 $sql->setInt ('projectid',$this->projectid); 196 197 return $sql->getAssoc(); 198 } 199 200 201 /** 202 * @return int[] 203 */ 204 public function getLanguageIds() 205 { 206 return array_keys( $this->getLanguages() ); 207 } 208 209 210 /** 211 * Liefert die Projektmodelle als Array mit ID->Name. 212 * 213 * @return array 214 */ 215 public function getModels() 216 { 217 $db = \cms\base\DB::get(); 218 219 $sql = $db->sql( 'SELECT id,name FROM {{projectmodel}}'. 220 ' WHERE projectid= {projectid} '. 221 ' ORDER BY name' ); 222 $sql->setInt ('projectid',$this->projectid); 223 224 return $sql->getAssoc(); 225 } 226 227 228 public function getModelIds() 229 { 230 return array_keys( $this->getModels() ); 231 } 232 233 234 public function getTemplateIds() 235 { 236 $db = \cms\base\DB::get(); 237 238 $sql = $db->sql( 'SELECT id FROM {{template}}'. 239 ' WHERE projectid= {projectid} ' ); 240 $sql->setInt ('projectid',$this->projectid); 241 242 return $sql->getCol(); 243 } 244 245 246 public function getTemplates() 247 { 248 $sql = Db::sql( 'SELECT id,name FROM {{template}}'. 249 ' WHERE projectid= {projectid} ' ); 250 $sql->setInt ('projectid',$this->projectid); 251 252 return $sql->getAssoc(); 253 } 254 255 256 /** 257 * Ermitteln des Wurzel-Ordners fuer dieses Projekt. 258 * 259 * Der Wurzelordner ist der einzige Ordnerhat in diesem 260 * Projekt, der kein Elternelement besitzt. 261 * 262 * @return Objekt-Id des Wurzelordners 263 */ 264 public function getRootObjectId() 265 { 266 $db = \cms\base\DB::get(); 267 268 $sql = $db->sql('SELECT id FROM {{object}}'. 269 ' WHERE parentid IS NULL'. 270 ' AND projectid={projectid}' ); 271 272 $sql->setInt('projectid',$this->projectid); 273 274 return( $sql->getOne() ); 275 } 276 277 278 279 // Laden 280 281 /** 282 * @throws \util\exception\ObjectNotFoundException 283 */ 284 public function load() 285 { 286 $sql = Db::sql( 'SELECT * FROM {{project}} '. 287 ' WHERE id={projectid}' ); 288 $sql->setInt( 'projectid',$this->projectid ); 289 290 $row = $sql->getRow(); 291 292 if ( empty($row) ) 293 throw new \util\exception\ObjectNotFoundException('project '.$this->projectid.' not found'); 294 295 $this->name = $row['name' ]; 296 $this->url = $row['url' ]; 297 $this->target_dir = $row['target_dir' ]; 298 $this->ftp_url = $row['ftp_url' ]; 299 $this->ftp_passive = $row['ftp_passive' ]; 300 $this->cmd_after_publish = $row['cmd_after_publish' ]; 301 $this->cut_index = $row['flags']&self::FLAG_CUT_INDEX; 302 $this->content_negotiation = $row['flags']&self::FLAG_CONTENT_NEGOTIATION; 303 $this->publishFileExtension = $row['flags']&self::FLAG_PUBLISH_FILE_EXTENSION; 304 $this->publishPageExtension = $row['flags']&self::FLAG_PUBLISH_PAGE_EXTENSION; 305 $this->linkAbsolute = $row['flags']&self::FLAG_LINK_ABSOLUTE; 306 307 return $this; 308 } 309 310 311 312 // Speichern 313 public function save() 314 { 315 $stmt = DB::sql( <<<SQL 316 UPDATE {{project}} 317 SET name = {name}, 318 target_dir = {target_dir}, 319 ftp_url = {ftp_url}, 320 ftp_passive = {ftp_passive}, 321 url = {url}, 322 flags = {flags}, 323 cmd_after_publish = {cmd_after_publish} 324 WHERE id= {projectid} 325 SQL 326 ); 327 328 $stmt->setString('ftp_url' ,$this->ftp_url ); 329 $stmt->setString('url' ,$this->url ); 330 $stmt->setString('name' ,$this->name ); 331 $stmt->setString('target_dir' ,$this->target_dir ); 332 $stmt->setInt ('ftp_passive' ,$this->ftp_passive ); 333 $stmt->setString('cmd_after_publish' ,$this->cmd_after_publish ); 334 335 $flags = 0; 336 if( $this->cut_index ) $flags |= self::FLAG_CUT_INDEX; 337 if( $this->content_negotiation ) $flags |= self::FLAG_CONTENT_NEGOTIATION; 338 if( $this->publishFileExtension) $flags |= self::FLAG_PUBLISH_FILE_EXTENSION; 339 if( $this->publishPageExtension) $flags |= self::FLAG_PUBLISH_PAGE_EXTENSION; 340 if( $this->linkAbsolute ) $flags |= self::FLAG_LINK_ABSOLUTE; 341 342 $stmt->setInt ('flags' ,$flags ); 343 $stmt->setInt ('projectid' ,$this->projectid ); 344 345 $stmt->execute(); 346 347 try 348 { 349 $rootFolder = new Folder( $this->getRootObjectId() ); 350 $rootFolder->load(); 351 $rootFolder->filename = $this->name; 352 $rootFolder->save(); 353 } 354 catch( \Exception $e ) 355 { 356 Logger::warn('Project '.$this->projectid.' has not a root folder'."\n".$e->getTraceAsString()); 357 } 358 } 359 360 361 /** 362 * Liefert alle Eigenschaften des Projektes. 363 */ 364 public function getProperties() 365 { 366 return parent::getProperties(); 367 } 368 369 370 /** 371 * Add a project to the database. 372 */ 373 public function add() 374 { 375 $db = \cms\base\DB::get(); 376 377 $sql = $db->sql('SELECT MAX(id) FROM {{project}}'); 378 $this->projectid = intval($sql->getOne())+1; 379 380 381 // Projekt hinzuf?gen 382 $sql = $db->sql( 'INSERT INTO {{project}} (id,name,target_dir,ftp_url,ftp_passive,cmd_after_publish,flags) '. 383 " VALUES( {projectid},{name},'','',0,'',0 ) " ); 384 $sql->setInt ('projectid',$this->projectid ); 385 $sql->setString('name' ,$this->name ); 386 387 $sql->execute(); 388 389 // Every projects needs a root folder. The root folder has no parent. 390 $folder = new Folder(); 391 $folder->projectid = $this->projectid; 392 $folder->filename = $this->name; 393 $folder->persist(); 394 } 395 396 397 // Projekt aus Datenbank entfernen 398 public function delete() 399 { 400 $db = \cms\base\DB::get(); 401 402 // Root-Ordner rekursiv samt Inhalten loeschen 403 $folder = new Folder( $this->getRootObjectId() ); 404 $folder->deleteAll(); 405 406 407 foreach( $this->getLanguageIds() as $languageid ) 408 { 409 $language = new Language( $languageid ); 410 $language->delete(); 411 } 412 413 414 foreach( $this->getTemplateIds() as $templateid ) 415 { 416 $template = new Template( $templateid ); 417 $template->delete(); 418 } 419 420 421 foreach( $this->getModelIds() as $modelid ) 422 { 423 $model = new Model( $modelid ); 424 $model->delete(); 425 } 426 427 428 // Deleting the project 429 $sql = $db->sql( 'DELETE FROM {{project}}'. 430 ' WHERE id= {projectid} ' ); 431 $sql->setInt( 'projectid',$this->projectid ); 432 $sql->execute(); 433 } 434 435 436 /** 437 * Liefert die Standard-Sprach-Id. If there is no default language, the first language-id will be used. 438 * @return String 439 */ 440 public function getDefaultLanguageId() 441 { 442 // ORDER BY deswegen, damit immer mind. eine Sprache 443 // gelesen wird 444 $sql = \cms\base\Db::sql( 'SELECT id FROM {{language}} '. 445 ' WHERE projectid={projectid}'. 446 ' ORDER BY is_default DESC, name ASC' ); 447 448 $sql->setInt('projectid',$this->projectid ); 449 450 return $sql->getOne(); 451 } 452 453 454 public function getDefaultModelId() 455 { 456 // ORDER BY deswegen, damit immer mind. eine Sprache 457 // gelesen wird 458 $sql = \cms\base\Db::sql( 'SELECT id FROM {{projectmodel}} '. 459 ' WHERE projectid={projectid}'. 460 ' ORDER BY is_default DESC' ); 461 $sql->setInt('projectid',$this->projectid ); 462 463 return $sql->getOne(); 464 } 465 466 467 468 /** 469 * Entfernt nicht mehr notwendige Inhalte aus dem Archiv. 470 */ 471 public function checkLimit() 472 { 473 // not necessary any more. 474 // this is done while saving a value! 475 } 476 477 478 479 /** 480 * Testet die Integrität der Datenbank. 481 * 482 * @return array activities (empty if nothing bad found) 483 */ 484 public function checkLostFiles() 485 { 486 $log = array(); 487 488 // Ordnerstruktur prüfen. 489 $stmt = \cms\base\Db::sql( <<<EOF 490 SELECT thistab.id FROM {{object}} AS thistab 491 LEFT JOIN {{object}} AS parenttab 492 ON parenttab.id = thistab.parentid 493 WHERE thistab.projectid={projectid} AND thistab.parentid IS NOT NULL AND parenttab.id IS NULL 494 EOF 495 ); 496 $stmt->setInt('projectid',$this->projectid); 497 498 $idList = $stmt->getCol(); 499 500 if ( count( $idList ) > 0 ) 501 { 502 $lostAndFoundFolder = new Folder(); 503 $lostAndFoundFolder->projectid = $this->projectid; 504 $lostAndFoundFolder->filename = "lostandfound"; 505 $lostAndFoundFolder->parentid = $this->getRootObjectId(); 506 $lostAndFoundFolder->persist(); 507 508 foreach( $idList as $id ) 509 { 510 $log[] = 'Lost file! Moving '.$id.' to lost+found.'; 511 $obj = new BaseObject( $id ); 512 $obj->setParentId( $lostAndFoundFolder->objectid ); 513 } 514 } 515 516 517 // Prüfe, ob die Verbindung Projekt->Template->Templatemodell->Projectmodell->Projekt konsistent ist. 518 $stmt = \cms\base\Db::sql( <<<EOF 519 SELECT DISTINCT projectid FROM {{projectmodel}} WHERE id IN (SELECT projectmodelid from {{templatemodel}} WHERE templateid in (SELECT id from {{template}} WHERE projectid={projectid})) 520 EOF 521 ); 522 $stmt->setInt('projectid',$this->projectid); 523 524 $idList = $stmt->getCol(); 525 526 if ( count( $idList ) > 1 ) 527 { 528 Logger::warn('Inconsistence found: Reference circle project<->template<->templatemodel<->projectmodel<->project is not consistent.'); 529 $log[] = 'Inconsistence found: Reference circle project<->template<->templatemodel<->projectmodel<->project is not consistent.'; 530 } 531 532 return $log; 533 } 534 535 536 537 /** 538 * Kopiert ein Projekt von einer Datenbank zu einer anderen.<br> 539 * <br> 540 * Alle Projektinhalte werden kopiert, die Fremdschluesselbeziehungen werden entsprechend angepasst.<br> 541 * <br> 542 * Alle Beziehungen zu Benutzern, z.B. "Zuletzt geaendert von", "angelegt von" sowie<br> 543 * alle Berechtigungsinformationen gehen verloren!<br> 544 * 545 * @param string $dbid_destination ID der Ziel-Datenbank 546 * @param string $name 547 */ 548 public function copy( $dbid_destination,$name='' ) 549 { 550 Logger::debug( 'Copying project '.$this->name.' to database '.$dbid_destination ); 551 552 $conf = \cms\base\Configuration::rawConfig(); 553 $zeit = date('Y-m-d\TH:i:sO'); 554 555 $db_src = \cms\base\DB::get(); 556 $db_dest = new Database( $conf['database'][$dbid_destination] ); 557 $db_dest->id = $dbid_destination; 558 $db_dest->start(); 559 560 $sameDB = ( $db_dest->id == $db_src->id ); 561 562 // ------------------------------------------------------- 563 $mapping = array(); 564 $ids = array('project' => array('foreign_keys'=>array(), 565 'primary_key' =>'id', 566 'unique_idx' =>'name', 567 'erase' =>array() 568 ), 569 'language' => array('foreign_keys'=>array('projectid'=>'project'), 570 'primary_key' =>'id' 571 ), 572 'projectmodel' => array('foreign_keys'=>array('projectid'=>'project'), 573 'primary_key' =>'id' 574 ), 575 'template' => array('foreign_keys'=>array('projectid'=>'project'), 576 'primary_key' =>'id' 577 ), 578 'object' => array('foreign_keys'=>array('projectid' =>'project' ), 579 'self_key' =>'parentid', 580 'primary_key' =>'id', 581 'erase' =>array('create_userid','lastchange_userid') 582 ), 583 'element' => array('foreign_keys'=>array('templateid' =>'template', 584 'folderobjectid' =>'object', 585 'default_objectid'=>'object' ), 586 'primary_key' =>'id' 587 ), 588 'templatemodel'=> array('foreign_keys'=>array('projectmodelid'=>'projectmodel', 589 'templateid' =>'template' ), 590 'primary_key' =>'id', 591 'replace' =>array('text'=>'element') 592 ), 593 'name' => array('foreign_keys'=>array('objectid' =>'object', 594 'languageid'=>'language' ), 595 'primary_key' =>'id' 596 ), 597 'page' => array('foreign_keys'=>array('objectid' =>'object', 598 'templateid'=>'template' ), 599 'primary_key' =>'id' 600 ), 601 'value' => array('foreign_keys'=>array('pageid' =>'page', 602 'languageid'=>'language', 603 'elementid'=>'element', 604 'linkobjectid'=>'object' ), 605 'erase' =>array('lastchange_userid'), 606 'replace' =>array('text'=>'object'), 607 'primary_key' =>'id' 608 ), 609 'link' => array('foreign_keys'=>array('objectid' =>'object', 610 'link_objectid'=>'object' ), 611 'primary_key' =>'id' 612 ), 613 'folder' => array('foreign_keys'=>array('objectid' =>'object' ), 614 'primary_key' =>'id' 615 ), 616 'file' => array('foreign_keys'=>array('objectid' =>'object' ), 617 'primary_key' =>'id', 618 'binary' =>'value' 619 ), 620 621 ); 622 623 if ( $sameDB ) 624 $ids['acl'] = array('foreign_keys'=>array('objectid' => 'object', 625 'languageid' => 'language' ), 626 'primary_key' =>'id' 627 ); 628 629 foreach( $ids as $tabelle=>$data ) 630 { 631 Logger::debug( 'Copying table '.$tabelle.' ...' ); 632 $mapping[$tabelle] = array(); 633 $idcolumn = $data['primary_key']; 634 635 // Naechste freie Id in der Zieltabelle ermitteln. 636 $stmt = $db_dest->sql( 'SELECT MAX('.$idcolumn.') FROM {t_'.$tabelle.'}'); 637 $maxid = intval($stmt->getOne()); 638 $nextid = $maxid; 639 640 // Zu �bertragende IDs ermitteln. 641 if ( count($data['foreign_keys'])==0 ) 642 { 643 $where = ' WHERE id='.$this->projectid; 644 } 645 else 646 { 647 foreach( $data['foreign_keys'] as $fkey_column=>$target_tabelle ) 648 { 649 $where = ' WHERE '.$fkey_column.' IN ('.join(array_keys($mapping[$target_tabelle]),',').')'; 650 break; 651 } 652 } 653 $stmt = $db_src->sql( 'SELECT '.$idcolumn.' FROM {t_'.$tabelle.'} '.$where); 654 655 foreach( $stmt->getCol() as $srcid ) 656 { 657 Logger::debug('Id '.$srcid.' of table '.$tabelle); 658 $mapping[$tabelle][$srcid] = ++$nextid; 659 660 $stmt = $db_src->sql( 'SELECT * FROM {t_'.$tabelle.'} WHERE id={id}'); 661 $stmt->setInt('id',$srcid); 662 $row = $stmt->getRow(); 663 664 // Wert des Prim�rschl�ssels �ndern. 665 $row[$idcolumn] = $mapping[$tabelle][$srcid]; 666 667 // Fremdschl�sselbeziehungen auf neue IDn korrigieren. 668 foreach( $data['foreign_keys'] as $fkey_column=>$target_tabelle) 669 { 670 Logger::debug($fkey_column.' '.$target_tabelle.' '.$row[$fkey_column]); 671 672 if ( intval($row[$fkey_column]) != 0 ) 673 $row[$fkey_column] = $mapping[$target_tabelle][$row[$fkey_column]]; 674 } 675 676 foreach( array_keys($row) as $key ) 677 { 678 if ( isset($data['unique_idx']) && $key == $data['unique_idx'] ) 679 { 680 // Nachschauen, ob es einen UNIQUE-Key in der Zieltabelle schon gibt. 681 $stmt = $db_dest->sql( 'SELECT 1 FROM {t_'.$tabelle.'} WHERE '.$key."='".$row[$key]."'"); 682 683 if ( intval($stmt->getOne()) == 1 ) 684 $row[$key] = $row[$key].$zeit; 685 686 } 687 688 if ( !$sameDB && isset($data['erase']) && in_array($key,$data['erase']) ) 689 $row[$key] = null; 690 691 if ( isset($data['self_key']) && $key == $data['self_key'] && intval($row[$key]) > 0 ) 692 $row[$key] = $row[$key]+$maxid; 693 } 694 695 if ( isset($data['replace']) ) 696 { 697 foreach( $data['replace'] as $repl_column=>$repl_tabelle) 698 foreach( $mapping[$repl_tabelle] as $oldid=>$newid) 699 { 700 $row[$repl_column] = str_replace('{'.$oldid.'}','{'.$newid.'}' ,$row[$repl_column]); 701 $row[$repl_column] = str_replace('"'.$oldid.'"','"'.$newid.'"' ,$row[$repl_column]); 702 $row[$repl_column] = str_replace('->'.$oldid ,'->"'.$newid.'"',$row[$repl_column]); 703 } 704 } 705 706 if ( isset($data['binary']) ) 707 { 708 if ( !$db_src->conf['base64'] && $db_dest->conf['base64'] ) 709 $row[$data['binary']] = base64_encode($row[$data['binary']]); 710 elseif ( $db_src->conf['base64'] && !$db_dest->conf['base64'] ) 711 $row[$data['binary']] = base64_decode($row[$data['binary']]); 712 } 713 714 // Daten in Zieltabelle einf�gen. 715 $stmt = $db_dest->sql( 'INSERT INTO {t_'.$tabelle.'} ('.join(array_keys($row),',').') VALUES({'.join(array_keys($row),'},{').'})',$dbid_destination); 716 foreach( $row as $key=>$value ) 717 { 718 if ( !$sameDB && isset($data['erase']) && in_array($key,$data['erase']) ) 719 $stmt->setNull($key); 720 else 721 { 722 if(is_bool($value)) 723 $stmt->setBoolean($key,$value); 724 elseif(is_int($value)) 725 $stmt->setInt($key,$value); 726 elseif(is_string($value)) 727 $stmt->setString($key,$value); 728 } 729 } 730 //$sql = $db->sql( 'INSERT INTO {t_'.$tabelle.'} ('.join(array_keys($row),',').') VALUES('.join($row,',').')',$dbid_destination); 731 $stmt->execute(); 732 } 733 734 if ( isset($data['self_key']) ) 735 { 736 foreach( $mapping[$tabelle] as $oldid=>$newid ) 737 { 738 $stmt = $db_dest->sql( 'UPDATE {t_'.$tabelle.'} SET '.$data['self_key'].'='.$newid.' WHERE '.$data['self_key'].'='.($oldid+$maxid),$dbid_destination ); 739 $stmt->execute(); 740 } 741 } 742 } 743 744 Logger::debug( 'Finished copying project' ); 745 746 $db_dest->commit(); 747 } 748 749 750 751 /** 752 * Ermittelt die Anzahl aller Objekte in diesem Projekt. 753 * @return int Anzahl 754 */ 755 public function countObjects() 756 { 757 $db = \cms\base\DB::get(); 758 $sql = $db->sql( 'SELECT COUNT(*) FROM {{object}} '. 759 ' WHERE projectid = {projectid}' ); 760 $sql->setInt( 'projectid', $this->projectid ); 761 762 return $sql->getOne(); 763 764 } 765 766 767 768 /** 769 * Ermittelt die Gr��e aller Dateien in diesem Projekt. 770 * @return int Summe aller Dateigroessen 771 */ 772 public function size() 773 { 774 $db = \cms\base\DB::get(); 775 776 $sql = $db->sql( <<<SQL 777 SELECT SUM(size) FROM {{file}} 778 LEFT JOIN {{object}} 779 ON {{file}}.objectid = {{object}}.id 780 WHERE projectid = {projectid} 781 SQL 782 ); 783 $sql->setInt( 'projectid', $this->projectid ); 784 785 return $sql->getOne(); 786 } 787 788 789 790 /** 791 * Liefert alle verf?gbaren Projekt-Ids 792 */ 793 public function info() 794 { 795 $info = array(); 796 797 $info['count_objects'] = $this->countObjects(); 798 $info['sum_filesize' ] = $this->size(); 799 800 801 return $info; 802 } 803 804 805 806 807 /** 808 * Ermittelt projektübergreifend die letzten Änderungen des angemeldeten Benutzers. 809 * 810 * @return array <string, unknown> 811 */ 812 public function getMyLastChanges() 813 { 814 815 $db = \cms\base\DB::get(); 816 817 818 $sql = $db->sql( <<<SQL 819 SELECT {{object}}.id as objectid, 820 {{object}}.filename as filename, 821 {{object}}.typeid as typeid, 822 {{object}}.lastchange_date as lastchange_date, 823 {{name}}.name as name 824 FROM {{object}} 825 LEFT JOIN {{name}} 826 ON {{name}}.objectid = {{object}}.id 827 AND {{name}}.languageid = {languageid} 828 LEFT JOIN {{project}} 829 ON {{object}}.projectid = {{project}}.id 830 WHERE {{object}}.projectid = {projectid} 831 AND {{object}}.lastchange_userid = {userid} 832 ORDER BY {{object}}.lastchange_date DESC; 833 SQL 834 ); 835 836 // Variablen setzen. 837 $sql->setInt( 'projectid', $this->projectid ); 838 839 $sql->setInt( 'languageid', 0 ); 840 841 $user = Request::getUser(); 842 $sql->setInt( 'userid', $user->userid ); 843 844 return $sql->getAll(); 845 } 846 847 848 /** 849 * Ermittelt projektübergreifend die letzten Änderungen. 850 * 851 * @return array 852 */ 853 public static function getAllLastChanges() 854 { 855 $db = \cms\base\DB::get(); 856 857 $sql = $db->sql( <<<SQL 858 SELECT {{object}}.id as objectid, 859 {{object}}.lastchange_date as lastchange_date, 860 {{object}}.filename as filename, 861 {{project}}.id as projectid, 862 {{project}}.name as projectname, 863 {{user}}.name as username, 864 {{user}}.id as userid, 865 {{user}}.mail as usermail, 866 {{user}}.fullname as userfullname 867 FROM {{object}} 868 LEFT JOIN {{project}} 869 ON {{object}}.projectid = {{project}}.id 870 LEFT JOIN {{user}} 871 ON {{user}}.id = {{object}}.lastchange_userid 872 ORDER BY {{object}}.lastchange_date DESC 873 LIMIT 50 874 SQL 875 ); 876 877 return $sql->getAll(); 878 } 879 880 881 882 /** 883 * Ermittelt die letzten Änderung im Projekt. 884 * @return array 885 */ 886 public function getLastChanges() 887 { 888 889 $db = \cms\base\DB::get(); 890 891 $sql = $db->sql( <<<SQL 892 SELECT {{object}}.id as objectid, 893 {{object}}.lastchange_date as lastchange_date, 894 {{object}}.filename as filename, 895 {{object}}.typeid as typeid, 896 {{name}}.name as name, 897 {{user}}.name as username, 898 {{user}}.id as userid, 899 {{user}}.mail as usermail, 900 {{user}}.fullname as userfullname 901 FROM {{object}} 902 LEFT JOIN {{name}} 903 ON {{name}}.objectid = {{object}}.id 904 AND {{name}}.languageid = {languageid} 905 LEFT JOIN {{user}} 906 ON {{user}}.id = {{object}}.lastchange_userid 907 WHERE {{object}}.projectid = {projectid} 908 ORDER BY {{object}}.lastchange_date DESC 909 SQL 910 ); 911 912 // Variablen setzen. 913 $sql->setInt( 'projectid', $this->projectid ); 914 915 $languageid = $this->getDefaultLanguageId(); 916 $sql->setInt( 'languageid', $languageid ); 917 918 return $sql->getAll(); 919 } 920 921 /** 922 * Ermittelt alle Objekte vom gew�nschten Typ, die sic in 923 * diesem Projekt befinden. 924 * 925 * @see objectClasses/Object#getAllObjectIds() 926 * @param array types Array 927 * @return array Liste von Object-Ids 928 */ 929 public function getAllObjectIds( $types=array('folder','page','link','file','image','url','text') ) 930 { 931 $stmt = \cms\base\Db::sql( <<<SQL 932 SELECT id FROM {{object}} 933 WHERE projectid={projectid} 934 AND ( typeid ={is_folder} 935 OR typeid ={is_file} 936 OR typeid ={is_image} 937 OR typeid ={is_text} 938 OR typeid ={is_page} 939 OR typeid ={is_link} 940 OR typeid ={is_url} ) 941 ORDER BY orderid ASC 942 SQL 943 ); 944 945 $stmt->setInt('projectid',$this->projectid ); 946 $stmt->setInt('is_folder',in_array('folder',$types)?BaseObject::TYPEID_FOLDER:0); 947 $stmt->setInt('is_file' ,in_array('file' ,$types)?BaseObject::TYPEID_FILE :0); 948 $stmt->setInt('is_image' ,in_array('image' ,$types)?BaseObject::TYPEID_IMAGE :0); 949 $stmt->setInt('is_text' ,in_array('text' ,$types)?BaseObject::TYPEID_TEXT :0); 950 $stmt->setInt('is_page' ,in_array('page' ,$types)?BaseObject::TYPEID_PAGE :0); 951 $stmt->setInt('is_link' ,in_array('link' ,$types)?BaseObject::TYPEID_LINK :0); 952 $stmt->setInt('is_url' ,in_array('url' ,$types)?BaseObject::TYPEID_URL :0); 953 954 return( $stmt->getCol() ); 955 } 956 957 958 /** 959 * Liefert die Ids aller Ordner in diesem Projekt. 960 * 961 * @return array 962 */ 963 public function getAllFolders() 964 { 965 $stmt = DB::sql( <<<SQL 966 SELECT id FROM {{object}} 967 WHERE typeid={typeid} 968 AND projectid={projectid} 969 SQL 970 ); 971 $stmt->setInt( 'typeid' ,BaseObject::TYPEID_FOLDER ); 972 $stmt->setInt( 'projectid',$this->projectid ); 973 974 return( $stmt->getCol() ); 975 } 976 977 978 /** 979 * @return array 980 */ 981 public function getAllFlatFolders() { 982 983 $folders = array(); 984 985 foreach( $this->getAllFolders() as $id ) 986 { 987 $o = new BaseObject( $id ); 988 $o->load(); 989 990 $folders[ $id ] = ''; 991 if ( !$o->isRoot() ) 992 { 993 $f = new Folder( $o->parentid ); 994 $f->load(); 995 $names = $f->parentObjectNames(true,true); 996 foreach( $names as $fid=>$name ) 997 $names[$fid] = \util\Text::maxLength($name,15,'..',STR_PAD_BOTH); 998 $folders[ $id ] = implode( \util\Text::FILE_SEP,$names ); 999 $folders[ $id ] .= \util\Text::FILE_SEP; 1000 } 1001 $folders[ $id ] .= $o->getName(); 1002 } 1003 1004 asort( $folders ); // Sortieren 1005 1006 return $folders; 1007 } 1008 1009 public function getName() 1010 { 1011 return $this->name; 1012 } 1013 1014 1015 /** 1016 * Cleans up the target url. 1017 */ 1018 public function getCleanTarget() 1019 { 1020 $target = parse_url( $this->target_dir ); 1021 1022 $scheme = isset($target['scheme']) ? $target['scheme'] . '://' : ''; 1023 if ( empty($scheme) ) 1024 $scheme = 'file:/'; 1025 1026 $host = isset($target['host']) ? $target['host'] : ''; 1027 $port = isset($target['port']) ? ':' . $target['port'] : ''; 1028 $user = isset($target['user']) ? $target['user'] : ''; 1029 $pass = isset($target['pass']) ? ':' . $target['pass'] : ''; 1030 $pass = ($user || $pass) ? "$pass@" : ''; 1031 $path = isset($target['path']) ? $target['path'] : ''; 1032 $query = isset($target['query']) ? '?' . $target['query'] : ''; 1033 $fragment = isset($target['fragment']) ? '#' . $target['fragment'] : ''; 1034 1035 return "$scheme$user$pass$host$port$path$query$fragment"; 1036 } 1037 1038 1039 1040 public function getId() 1041 { 1042 return $this->projectid; 1043 } 1044 1045 1046 } 1047
Download modules/cms/model/Project.class.php
History Thu, 16 Feb 2023 01:04:38 +0100 Jan Dankert New: Tags for base objects. Tue, 14 Feb 2023 00:23:13 +0100 Jan Dankert New filters: Robots (for robots.txt) and Sitemap. Sat, 2 Jul 2022 00:37:22 +0200 Jan Dankert Fix: Public Filename must consider the filename style. Mon, 25 Apr 2022 03:26:21 +0200 Jan Dankert New: New Project may be created with sample data. Fri, 15 Apr 2022 14:51:22 +0200 dankert Refactoring: User,Config and Database info is now stored in the Request, because so there is no session required for clients which are using Basic Authorization. Fri, 11 Mar 2022 11:32:38 +0100 dankert New: Edit all languages for a page element. Sun, 5 Dec 2021 22:26:39 +0100 dankert Fixed a type in template source of new projects. Sun, 5 Dec 2021 20:33:24 +0100 dankert Cleanup: Removed unusable properties from class 'Value' and 'BaseObject'. Sun, 5 Dec 2021 15:33:29 +0100 dankert Cleanup: Removed unusable properties from class 'Value'. 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. Sat, 14 Nov 2020 22:02:21 +0100 Jan Dankert Fixed: Notices may display a message. Sun, 1 Nov 2020 03:08:55 +0100 Jan Dankert Replaced the calls to "Configuration::rawConfig()" with the OO style calls; Cleanup LoginAction. Mon, 26 Oct 2020 23:09:24 +0100 Jan Dankert Cleanup UI for adding templates and adding projects. Wed, 7 Oct 2020 23:01:31 +0200 Jan Dankert Cleanup: Refactored file seperator char with an unicode char. Fri, 2 Oct 2020 23:11:48 +0200 Jan Dankert Cleanup: No '.inputholder' any more, notices with links to objects. Tue, 29 Sep 2020 22:17:11 +0200 Jan Dankert Refactoring: Do not use global constants. Sat, 26 Sep 2020 10:32:02 +0200 Jan Dankert Refactoring: No global $conf array any more. Sat, 26 Sep 2020 03:03:47 +0200 Jan Dankert Refactoring: less global functions. 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 22:48:46 +0200 Jan Dankert Refactoring: Every project has 1 publishing target. Sun, 23 Feb 2020 04:01:30 +0100 Jan Dankert Refactoring with Namespaces for the cms modules, part 1: moving.