openrat-cms

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

FolderCreateimageAction.class.php (2552B)


      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\Image;
      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 FolderCreateimageAction 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 		$image       = new Image();
     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 			$image->filename  = BaseObject::urlify( basename($url) );
     52 			$image->size      = strlen($http->body);
     53 			$image->value     = $http->body;
     54 			$image->parentid  = $this->folder->objectid;
     55 		}
     56 		else
     57 		{
     58 			$upload = new Upload();
     59 
     60             try
     61             {
     62                 $upload->processUpload();
     63             }
     64             catch( \Exception $e )
     65             {
     66                 // technical error.
     67                 throw new \RuntimeException('Exception while processing the upload: '.$e->getMessage(), 0, $e);
     68 
     69                 //throw new \ValidationException( $upload->parameterName );
     70             }
     71 
     72             $image->filename  = BaseObject::urlify( $upload->filename );
     73             $image->extension = $upload->extension;
     74             $image->size      = $upload->size;
     75             $image->parentid  = $this->folder->objectid;
     76             $image->projectid = $this->folder->projectid;
     77 
     78             $image->value     = $upload->value;
     79 		}
     80 
     81 		$image->persist(); // Datei hinzufuegen
     82 		$this->addNoticeFor( $image, Messages::ADDED );
     83         $image->setNameForAllLanguages( $name,$description );
     84 		$this->setTemplateVar('objectid',$image->objectid);
     85 
     86 		$this->folder->setTimestamp();
     87     }
     88 }