openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

FolderCreatefileAction.class.php (2994B)


      1 <?php
      2 namespace cms\action\folder;
      3 use cms\action\FolderAction;
      4 use cms\action\Method;
      5 use cms\model\BaseObject;
      6 use cms\model\File;
      7 use cms\model\Permission;
      8 use language\Messages;
      9 use util\exception\ValidationException;
     10 use util\Http;
     11 use util\Upload;
     12 
     13 
     14 class FolderCreatefileAction extends FolderAction implements Method {
     15 	public function getRequiredPermission() {
     16 		return Permission::ACL_CREATE_FILE;
     17 	}
     18 
     19 	public function view() {
     20 		// Maximale Dateigroesse.
     21 		$maxSizeBytes = $this->maxFileSize();
     22 		$this->setTemplateVar('max_size' ,($maxSizeBytes/1024).' KB' );
     23 		$this->setTemplateVar('maxlength',$maxSizeBytes );
     24 
     25 		$this->setTemplateVar('objectid',$this->folder->objectid );
     26     }
     27 
     28 
     29     public function post() {
     30 		$type        = $this->request->getText('type'       );
     31 		$name        = $this->request->getText('name'       );
     32 		$filename    = $this->request->getText('filename'   );
     33 		$description = $this->request->getText('description');
     34 
     35 		$file   = new File();
     36 
     37 		// Die neue Datei wird über eine URL geladen und dann im CMS gespeichert.
     38 		if	( $url = $this->request->getText('url') )
     39 		{
     40 			$http = new Http();
     41 			$http->setUrl( $url );
     42 
     43 			$ok = $http->request();
     44 
     45 			if	( !$ok )
     46 			{
     47 				$this->addWarningFor( $this->folder,Messages::COMMON_VALIDATION_ERROR,[],$http->error);
     48 				throw new ValidationException( 'url' );
     49 			}
     50 
     51 			$file->filename  = BaseObject::urlify( $name );
     52 			$file->size      = strlen($http->body);
     53 			$file->value     = $http->body;
     54 			$file->parentid  = $this->folder->objectid;
     55             $file->projectid = $this->folder->projectid;
     56 		}
     57         elseif	( $value = $this->request->getText('value') )
     58         {
     59             // New file is inserted.
     60             $file->filename  = BaseObject::urlify( $filename );
     61             $file->value     = $value;
     62             $file->size      = strlen($file->value);
     63             $file->parentid  = $this->folder->objectid;
     64             $file->projectid = $this->folder->projectid;
     65         }
     66 		else
     67 		{
     68 		    // File was uploaded.
     69             $upload = new Upload('file');
     70 
     71 		    try
     72             {
     73                 $upload->processUpload();
     74             }
     75             catch( \Exception $e )
     76             {
     77                 // technical error.
     78                 throw new \RuntimeException('Exception while processing the upload: '.$e->getMessage(), 0, $e);
     79 
     80                 //throw new \ValidationException( $upload->parameterName );
     81             }
     82 
     83 			$file->filename  = BaseObject::urlify( $upload->filename );
     84             $file->extension = $upload->extension;
     85             $file->size      = $upload->size;
     86             $file->parentid  = $this->folder->objectid;
     87             $file->projectid = $this->folder->projectid;
     88 
     89             $file->value     = $upload->value;
     90 		}
     91 
     92 		$file->persist(); // Datei hinzufuegen
     93         $file->setNameForAllLanguages( $name,$description );
     94 
     95 		$this->addNoticeFor( $file, Messages::ADDED );
     96 		$this->setTemplateVar('objectid',$file->objectid);
     97 
     98 		$this->folder->setTimestamp();
     99     }
    100 }