openrat-cms

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

FolderCreatetextAction.class.php (2459B)


      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\Permission;
      7 use cms\model\Text;
      8 use language\Messages;
      9 use util\exception\ValidationException;
     10 use util\Http;
     11 use util\Upload;
     12 
     13 
     14 class FolderCreatetextAction 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 
     31 		$name        = $this->request->getText('name'       );
     32 		$description = $this->request->getText('description');
     33 
     34 		$text   = new Text();
     35 		$text->parentid  = $this->folder->objectid;
     36 		$text->projectid = $this->folder->projectid;
     37 
     38 		// Die neue Datei wird über eine URL geladen und dann im CMS gespeichert.
     39 		$this->request->handleText('url', function($url) use ($text)
     40 		{
     41 			$http = new Http();
     42 			$http->setUrl( $url );
     43 
     44 			$ok = $http->request();
     45 
     46 			if	( !$ok )
     47 			{
     48 				//$this->addNotice($http->error);
     49 				// TODO: What to do with $http->error ?
     50 				throw new ValidationException('url',Messages::COMMON_VALIDATION_ERROR);
     51 			}
     52 
     53 			$text->filename  = BaseObject::urlify( basename($url) );
     54 			$text->size      = strlen($http->body);
     55 			$text->value     = $http->body;
     56 		});
     57 
     58 		$this->request->handleText('text',function($value) use ($text) {
     59 			$text->filename  = $this->request->getRequiredText('filename' );
     60 			$text->extension = $this->request->getRequiredText('extension');
     61 			$text->value     = $this->request->getRequiredText('text'     );
     62 			$text->size      = strlen( $text->value );
     63 		});
     64 
     65 		$upload = new Upload();
     66 
     67 		if   ( $upload->isAvailable() ) {
     68 
     69 			try
     70 			{
     71 				$upload->processUpload();
     72 			}
     73 			catch( \Exception $e )
     74 			{
     75 				// TODO: make a UIException?
     76 				throw $e;
     77 			}
     78 
     79 			$text->filename  = BaseObject::urlify( $upload->filename );
     80 			$text->extension = $upload->extension;
     81 			$text->size      = $upload->size;
     82 
     83 			$text->value     = $upload->value;
     84 		}
     85 
     86 		$text->persist(); // Datei hinzufuegen
     87         $text->setNameForAllLanguages( $name,$description );
     88 
     89 		$this->addNoticeFor($text, Messages::ADDED);
     90 		$this->setTemplateVar('objectid',$text->objectid);
     91 
     92 		$this->folder->setTimestamp();
     93     }
     94 }