openrat-cms

OpenRat Content Management System
git clone http://git.code.weiherhei.de/openrat-cms.git
Log | Files | Refs | README

Folder.class.php (16774B)


      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 	public 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 	public function load()
     69 	{
     70 //		$db = \cms\base\DB::get();
     71 //
     72 //		$sql = $db->sql('SELECT * FROM {{folder}} WHERE objectid={objectid}');
     73 //		$sql->setInt('objectid',$this->objectid);
     74 //
     75 //		$row = $sql->getRow( $sql );
     76 //
     77 		$this->objectLoad();
     78 		
     79 //		$this->folderid = $row['id' ];
     80 	}
     81 
     82 
     83 
     84 	function save()
     85 	{
     86 		parent::save();
     87 	}
     88 
     89 
     90 	
     91 	function setOrderId( $orderid )
     92 	{
     93 		$db = \cms\base\DB::get();
     94 
     95 		$sql = $db->sql('UPDATE {{folder}} '.
     96 		               '  SET orderid={orderid}'.
     97 		               '  WHERE id={folderid}');
     98 		$sql->setInt('folderid',$this->folderid);
     99 		$sql->setInt('orderid' ,$orderid       );
    100 
    101 		$sql->execute();
    102 	}
    103 
    104 
    105 
    106 //	function getSubFolders()
    107 //	{
    108 //		global $SESS;
    109 //		$db = \cms\base\DB::get();
    110 //		
    111 //		$sql = $db->sql('SELECT id FROM {{folder}}'.
    112 //		               '  WHERE parentid={folderid}'.
    113 //		               '    AND projectid={projectid}'.
    114 //		               '  ORDER BY orderid ASC' );
    115 //		$sql->setInt('folderid' ,$SESS['folderid' ]);
    116 //		$sql->setInt('projectid',$SESS['projectid']);
    117 //		
    118 //		return( $sql->getCol( $sql ));
    119 //	}
    120 
    121 	
    122 	// Liest alle Objekte in diesem Ordner
    123 	function getObjectIds()
    124 	{
    125 		$db = \cms\base\DB::get();
    126 
    127 		$sql = $db->sql('SELECT id FROM {{object}}'.
    128 		               '  WHERE parentid={objectid}'.
    129 		               '  ORDER BY orderid ASC' );
    130 		$sql->setInt('objectid' ,$this->objectid  );
    131 		
    132 		return( $sql->getCol() );
    133 	}
    134 
    135 
    136 
    137 	/**
    138 	 * Liest alle Objekte in diesem Ordner
    139      * @return array[Object] Objekte
    140 	 */
    141 	function getObjects()
    142 	{
    143 		$db = \cms\base\DB::get();
    144 
    145 		$sql = $db->sql('SELECT {{object}}.*,{{name}}.name,{{name}}.descr'.
    146 		               '  FROM {{object}}'.
    147 		               ' LEFT JOIN {{name}} '.
    148 		               '   ON {{object}}.id={{name}}.objectid AND {{name}}.languageid={languageid} '.
    149 		               '  WHERE parentid={objectid}'.
    150 		               '  ORDER BY orderid ASC' );
    151 		$sql->setInt('languageid',$this->languageid );
    152 		$sql->setInt('objectid'  ,$this->objectid   );
    153 		
    154 		$liste = array();
    155 		$res = $sql->getAll();
    156 		foreach( $res as $row )
    157 		{
    158 			$o = new BaseObject( $row['id'] );
    159 			$o->setDatabaseRow( $row );
    160 			$liste[] = $o;
    161 		}
    162 
    163 		return $liste;
    164 	}
    165 
    166 
    167 	// Liest alle Objekte in diesem Ordner
    168 	function getObjectIdsByType()
    169 	{
    170 		$db = \cms\base\DB::get();
    171 
    172 		$sql = $db->sql('SELECT id FROM {{object}}'.
    173 		               '  WHERE parentid={objectid}'.
    174 		               '  ORDER BY typeid,orderid ASC' );
    175 		$sql->setInt('objectid' ,$this->objectid  );
    176 		
    177 		return( $sql->getCol() );
    178 	}
    179 
    180 
    181 	// Liest alle Objekte in diesem Ordner sortiert nach dem Namen (nicht Dateinamen!)
    182 	function getChildObjectIdsByName()
    183 	{
    184 		$db = \cms\base\DB::get();
    185 
    186 		$sql = $db->sql('SELECT {{object}}.id FROM {{object}}'.
    187 		               '  LEFT JOIN {{name}} ON {{object}}.id={{name}}.objectid AND {{name}}.languageid={languageid} '.
    188                        ' WHERE parentid={objectid}'.
    189                        ' ORDER BY {{name}}.name,{{object}}.filename ASC');
    190 		$sql->setInt('objectid'  , $this->objectid  );
    191 		$sql->setInt('languageid', $this->languageid);
    192 		return( $sql->getCol() );
    193 	}
    194 
    195 
    196 	// Liest alle Objekte in diesem Ordner
    197 	function getObjectIdsByLastChange()
    198 	{
    199 		$db = \cms\base\DB::get();
    200 
    201 		$sql = $db->sql('SELECT id FROM {{object}}'.
    202 		               '  WHERE parentid={objectid}'.
    203 		               '  ORDER BY lastchange_date,orderid ASC' );
    204 		$sql->setInt('projectid',$this->projectid );
    205 		$sql->setInt('objectid' ,$this->objectid  );
    206 		
    207 		return( $sql->getCol() );
    208 	}
    209 
    210 
    211 	function getObjectIdByFileName( $filename )
    212 	{
    213 		$db = \cms\base\DB::get();
    214 		
    215 		$sql = $db->sql('SELECT id FROM {{object}}'.
    216 		               '  WHERE parentid={objectid}'.
    217 		               '    AND filename={filename}' );
    218 		$sql->setInt   ('objectid' ,$this->objectid );
    219 		$sql->setString('filename' ,$filename       );
    220 		
    221 		return( intval($sql->getOne()) );
    222 	}
    223 
    224 
    225 	
    226 	public function getPages()
    227 	{
    228 		$db = \cms\base\DB::get();
    229 
    230 		$sql = $db->sql('SELECT id FROM {{object}} '.
    231 		               '  WHERE parentid={objectid} AND typeid='.BaseObject::TYPEID_PAGE.
    232 		               '  ORDER BY orderid ASC' );
    233 		$sql->setInt( 'objectid' ,$this->objectid  );
    234 
    235 		return $sql->getCol();
    236 	}
    237 
    238 	
    239 	/**
    240 	 * Ermittelt die erste Seite oder Verkn�pfung in diesem Ordner.
    241 	 *
    242 	 * @return BaseObject Objekt
    243 	 */
    244 	public function getFirstPage()
    245 	{
    246 		$db = \cms\base\DB::get();
    247 
    248 		$sql = $db->sql('SELECT id FROM {{object}} '.
    249 		               '  WHERE parentid={objectid}'.
    250 		               '    AND (typeid='.BaseObject::TYPEID_PAGE.')'.
    251 		               '  ORDER BY orderid ASC' );
    252 		$sql->setInt( 'objectid' ,$this->objectid  );
    253 
    254 		$oid = intval($sql->getOne());
    255 		
    256 		if	( $oid != 0 )
    257 			$o = new BaseObject($oid);
    258 		else
    259 			$o = null;
    260 
    261 		return $o;
    262 	}
    263 
    264 
    265 	/**
    266 	 * Ermittelt die erste Seite oder Verkn�pfung in diesem Ordner.
    267 	 *
    268 	 * @return BaseObject Objekt
    269 	 */
    270 	function getFirstPageOrLink()
    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 ASC' );
    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 	function getLastPageOrLink()
    292 	{
    293 		$db = \cms\base\DB::get();
    294 
    295 		$sql = $db->sql('SELECT id FROM {{object}} '.
    296 		               '  WHERE parentid={objectid}'.
    297 		               '    AND (typeid='.BaseObject::TYPEID_PAGE.' OR typeid='.BaseObject::TYPEID_LINK.')'.
    298 		               '  ORDER BY orderid DESC' );
    299 		$sql->setInt( 'objectid' ,$this->objectid  );
    300 
    301 		$oid = intval($sql->getOne());
    302 		
    303 		if	( $oid != 0 )
    304 			$o = new BaseObject($oid);
    305 		else
    306 			$o = null;
    307 
    308 		return $o;
    309 	}
    310 
    311 
    312 	/**
    313 	 * Returns a list of files.
    314 	 * @return array
    315 	 */
    316 	function getFiles()
    317 	{
    318 		$db = \cms\base\DB::get();
    319 
    320 		$sql = $db->sql('SELECT id FROM {{object}} '.
    321 		               '  WHERE parentid={objectid} AND typeid='.BaseObject::TYPEID_FILE.
    322 		               '  ORDER BY orderid ASC' );
    323 		$sql->setInt( 'objectid' ,$this->objectid  );
    324 
    325 		return $sql->getCol();
    326 	}
    327 
    328 
    329 	
    330 	/**
    331 	 * Liefert eine Liste von allen Dateien in diesem Ordner.
    332 	 *
    333 	 * @return array Schl�ssel=Objekt-Id, Wert=Dateiname
    334 	 */
    335 	function getFileFilenames()
    336 	{
    337 		$db = \cms\base\DB::get();
    338 
    339 		$sql = $db->sql('SELECT id,filename FROM {{object}} '.
    340 		               '  WHERE parentid={objectid} AND typeid='.BaseObject::TYPEID_FILE.
    341 		               '  ORDER BY orderid ASC' );
    342 		$sql->setInt( 'objectid' ,$this->objectid  );
    343 
    344 		return $sql->getAssoc();
    345 	}
    346 
    347 	
    348 	function getLinks()
    349 	{
    350 		$db = \cms\base\DB::get();
    351 
    352 		$sql = $db->sql('SELECT id FROM {{object}} '.
    353 		               '  WHERE parentid={objectid} AND typeid='.BaseObject::TYPEID_LINK.
    354 		               '  ORDER BY orderid ASC' );
    355 		$sql->setInt( 'objectid' ,$this->objectid  );
    356 
    357 		return $sql->getCol();
    358 	}
    359 
    360 	
    361 
    362 	/**
    363 	 * Rechte f?r diesen Ordner hinzuf?gen.
    364 	 * @param $rights
    365 	 * @param bool $inherit
    366 	 * @deprecated unused? bad code.
    367 	 */
    368 	function addrights( $rights,$inherit = true )
    369 	{
    370 		global $SESS;
    371 
    372 		$SESS['rights'][$rights['projectid']][$this->folderid]['show'] = true;
    373 
    374 		if   ($rights['read'] == '1')
    375 			$SESS['rights'][$rights['projectid']][$this->folderid]['read'] = 1;
    376 		if   ($rights['write'] == '1')
    377 			$SESS['rights'][$rights['projectid']][$this->folderid]['write'] = 1;
    378 		if   ($rights['create'] == '1')
    379 			$SESS['rights'][$rights['projectid']][$this->folderid]['create'] = 1;
    380 		if   ($rights['delete'] == '1')
    381 			$SESS['rights'][$rights['projectid']][$this->folderid]['delete'] = 1;
    382 		if   ($rights['publish'] == '1')
    383 			$SESS['rights'][$rights['projectid']][$this->folderid]['publish'] = 1;
    384 		
    385 		// Rechte auf Unterordner vererben
    386 		// sowie f?r ?bergeordnete Ordner die Anzeige erzwingen 	
    387 		if   ( $inherit )
    388 		{
    389 			// ?bergeordnete Ordner ermitteln
    390 			$parentfolder = $this->parentObjectFileNames();
    391 
    392 			// ?bergeordnete Ordner immer anzeigen (Schalter 'show'=true)
    393 			foreach( $parentfolder as $folderid=>$name )
    394 			{
    395 				$f = new Folder( $folderid );
    396 				$f->projectid = $this->projectid;
    397 				$f->addrights( array('projectid'=>$rights['projectid']),false );
    398 				unset($f);
    399 			}
    400 
    401 			$f = new Folder( 'null' );
    402 			$f->projectid = $this->projectid;
    403 			$f->addrights( array('projectid'=>$rights['projectid']),false );
    404 			unset($f);
    405 
    406 
    407 			// Unterordner ermitteln
    408 			//echo "Kurz vor subfolderberechnung, folderid ist ".$this->folderid.'<br>';
    409 			$subfolder = $this->subfolder();
    410 
    411 			// Rechte weitergeben
    412 			foreach( $subfolder as $folderid=>$name )
    413 			{
    414 				$f = new Folder( $folderid );
    415 				$f->projectid = $this->projectid;
    416 				$f->addrights( $rights,false );
    417 				unset($f);
    418 			}
    419 		}
    420 	}
    421 
    422 
    423     /**
    424      * Ermitteln des Dateinamens.
    425      * @return String Dateiname
    426      */
    427     public function filename()
    428     {
    429         $filenameConfig = Configuration::subset('filename');
    430 
    431         if	( $filenameConfig->is('edit',true ) )
    432         {
    433             if   ( $this->filename == '' )
    434                 // Filename ist eigentlich ein Pflichtfeld, daher kann dies nahezu nie auftreten.
    435                 // Rein technisch kann der Filename aber leer sein.
    436                 return $this->objectid;
    437             else
    438                 return BaseObject::urlify($this->name);
    439         }
    440         else
    441         {
    442             // Filename is not edited, so we are generating a pleasant filename.
    443             switch( $filenameConfig->get('style','short') )
    444             {
    445                 case 'longid':
    446                     // Eine etwas laengere ID als Dateinamen benutzen
    447                     return base_convert(str_pad($this->objectid,6,'a'),11,10);
    448 
    449                 case 'short':
    450                     // So kurz wie moeglich: Erhoehen der Basis vom 10 auf 36.
    451                     // Beispiele:
    452                     // 1  -> 1
    453                     // 10 -> a
    454                     return base_convert($this->objectid,10,36);
    455 
    456                 case 'md5':
    457                     // MD5-Summe als Dateinamen verwenden
    458                     // Achtung: Kollisionen sind unwahrscheinlich, aber theoretisch möglich.
    459                     return  md5(md5($this->objectid));
    460 
    461                 case  'ss':
    462                     // Imitieren von "StoryServer" URLs. Wers braucht.
    463                     return $this->objectid;
    464 
    465                 case  'title':
    466                     // Achtung: Kollisionen sind möglich.
    467                     // COLLISION ALARM! THIS IS NOT A GOOD IDEA!
    468                     return  BaseObject::urlify($this->name);
    469 
    470                 case 'id':
    471                 default:
    472                     // Einfach die Objekt-Id als Dateinamen verwenden.
    473                     return $this->objectid;
    474 
    475             }
    476         }
    477     }
    478 
    479 
    480 
    481 
    482 	// Ermitteln aller Unterordner
    483 	//
    484 	public function subfolder()
    485 	{
    486 		$stmt = Db::sql(<<<SQL
    487 
    488 SELECT id FROM {{object}}
    489 		                 WHERE parentid={objectid} AND typeid={typeid}
    490 		                 ORDER BY orderid ASC
    491 SQL
    492         );
    493 
    494 		$stmt->setInt( 'objectid' ,$this->objectid        );
    495 		$stmt->setInt( 'typeid'   ,BaseObject::TYPEID_FOLDER );
    496 
    497 		$this->subfolders = $stmt->getCol();
    498 
    499 		return $this->subfolders;
    500 	}
    501 
    502 	
    503 	
    504 	public function getSubfolderFilenames()
    505 	{
    506 		$stmt = Db::sql(<<<SQL
    507 SELECT id,filename FROM {{object}}
    508 		                 WHERE parentid={objectid} AND typeid={typeid}
    509 		                 ORDER BY orderid ASC
    510 SQL
    511         );
    512 
    513 		$stmt->setInt( 'objectid' ,$this->objectid        );
    514         $stmt->setInt( 'typeid'   ,BaseObject::TYPEID_FOLDER );
    515 
    516 		return $stmt->getAssoc();
    517 	}
    518 
    519 	
    520 	
    521 	/**
    522 	 * Ermitteln aller Unterordner (rekursives Absteigen).
    523 	 * 
    524 	 */
    525 	function getAllSubFolderIds()
    526 	{
    527 		$ids = array();
    528 
    529 		foreach( $this->getSubFolderIds() as $id )
    530 		{
    531 			$ids[] = $id;
    532 
    533 			$f = new Folder( $id );
    534 			if	( !empty($this->projectid) )
    535 				$f->projectid = $this->projectid;
    536 
    537 			// Rekursiver Aufruf für alle Unterordner
    538 			foreach( $f->getAllSubFolderIds() as $xid )
    539 			{
    540 				$ids[] = $xid;
    541 			}
    542 		}
    543 
    544 		return $ids;
    545 	}
    546 
    547 
    548 	/**
    549 	 * Loeschen dieses Ordners.
    550 	 * Der Ordner wird nur geloescht, wenn er keine Unterelemente mehr enth?lt.
    551 	 * Zum Loeschen inklusive Unterelemente dient die Methode deleteAll()
    552      *
    553      * @throws \Exception if folder contains elements
    554 	 */
    555 	function delete()
    556 	{
    557 		$db = \cms\base\DB::get();
    558 
    559 		// Nur loeschen, wenn es keine Unterelemente gibt
    560 		if	( count( $this->getObjectIds() ) == 0 )
    561 		{
    562 			$sql = $db->sql( 'UPDATE {{element}} '.
    563 			                '  SET folderobjectid=NULL '.
    564 			                '  WHERE folderobjectid={objectid}' );
    565 			$sql->setInt('objectid',$this->objectid);
    566 			$sql->execute();
    567 	
    568 			$sql = $db->sql( 'DELETE FROM {{folder}} '.
    569 			                '  WHERE objectid={objectid}' );
    570 			$sql->setInt('objectid',$this->objectid);
    571 			$sql->execute();
    572 
    573 			parent::delete();
    574 		}
    575 		else {
    576 		    throw new \RuntimeException('There are children in the folder '.$this->objectid.'.');
    577         }
    578 	}
    579 
    580 	
    581 	/**
    582 	 * Rekursives loeschen aller Inhalte
    583 	 *
    584 	 * Loeschen aller Inhalte dieses Ordners
    585 	 * inclusive aller Unterelemente
    586 	 */
    587 	function deleteAll()
    588 	{
    589 		$db = \cms\base\DB::get();
    590 
    591 		// L?schen aller Unterordner
    592 		foreach( $this->subfolder() as $folderid )
    593 		{
    594 			$folder = new Folder( $folderid );
    595 			{
    596 				$folder->deleteAll();
    597 			}
    598 		}
    599 		
    600 		// L?schen aller Seiten,Verknuepfungen und Dateien in
    601 		// diesem Ordner
    602 		foreach( $this->getObjectIds() as $oid )
    603 		{
    604 			$object = new BaseObject( $oid );
    605 			{
    606 				$object->load();
    607 
    608 				if	( $object->isPage )
    609 				{
    610 					$page = new Page( $oid );
    611 					$page->load();
    612 					$page->delete();
    613 				}
    614 
    615 				if	( $object->isLink )
    616 				{
    617 					$link = new Link( $oid );
    618 					$link->load();
    619 					$link->delete();
    620 				}
    621 
    622 				if	( $object->isUrl )
    623 				{
    624 					$url = new Url( $oid );
    625                     $url->load();
    626                     $url->delete();
    627 				}
    628 
    629 				if	( $object->isFile )
    630 				{
    631 					$file = new File( $oid );
    632 					$file->load();
    633 					$file->delete();
    634 				}
    635 				if	( $object->isImage )
    636 				{
    637 					$file = new Image( $oid );
    638 					$file->load();
    639 					$file->delete();
    640 				}
    641 				if	( $object->isText )
    642 				{
    643 					$file = new Text( $oid );
    644 					$file->load();
    645 					$file->delete();
    646 				}
    647 			}
    648 		}
    649 
    650 		// Zum Abschluss den aktuellen Ordner loeschen
    651 		$this->delete();
    652 	}
    653 
    654 	
    655 	function getSubFolderIds()
    656 	{
    657 		return $this->subfolder();
    658 	}
    659 	
    660 	
    661 	
    662 	/**
    663 	 * Ermittelt die letzten Änderung in diesem Ordner.
    664 	 * @return array[Objektid]=Array())
    665 	 */
    666 	public function getLastChanges()
    667 	{
    668 		$sql = DB::sql( <<<SQL
    669 		SELECT {{object}}.id       as objectid,
    670 		       {{object}}.lastchange_date as lastchange_date,
    671 		       {{object}}.filename as filename,
    672 		       {{object}}.typeid   as typeid,
    673 		       {{name}}.name       as name,
    674 		       {{user}}.name       as username,
    675 		       {{user}}.id         as userid,
    676 		       {{user}}.mail       as usermail,
    677 		       {{user}}.fullname   as userfullname
    678 		  FROM {{object}}
    679 		  LEFT JOIN {{name}}
    680 		         ON {{name}}.objectid = {{object}}.id
    681 				AND {{name}}.languageid = {languageid}
    682 		  LEFT JOIN {{user}}
    683 		         ON {{user}}.id = {{object}}.lastchange_userid
    684 			  WHERE {{object}}.parentid = {folderid}
    685 		   ORDER BY {{object}}.lastchange_date DESC
    686 SQL
    687 		);
    688 	
    689 		// Variablen setzen.
    690 		$sql->setInt( 'folderid', $this->objectid );
    691 	
    692 		$sql->setInt( 'languageid', $this->languageid );
    693 	
    694 		return $sql->getAll();
    695 	}
    696 
    697 
    698     /**
    699      * Stellt fest, ob der Ordner Unterelemente besitzt.
    700      * @return bool
    701      */
    702 	public function hasChildren()
    703     {
    704         return count($this->getObjectIds()) > 0;
    705     }
    706 
    707 }
    708