File modules/cms/action/folder/FolderCreatefileAction.class.php

Last commit: Wed Mar 9 13:28:52 2022 +0100	dankert	Refactoring: Checkbox values are always sent to the server. In the actions we must test the value with 'isTrue()'
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 }
Download modules/cms/action/folder/FolderCreatefileAction.class.php
History Wed, 9 Mar 2022 13:28:52 +0100 dankert Refactoring: Checkbox values are always sent to the server. In the actions we must test the value with 'isTrue()' Sun, 5 Dec 2021 20:33:24 +0100 dankert Cleanup: Removed unusable properties from class 'Value' and 'BaseObject'. Sun, 14 Mar 2021 23:51:49 +0100 Jan Dankert Refactoring: Using the ValidationException where possible. Sat, 6 Mar 2021 03:42:38 +0100 Jan Dankert New: Better permission checks. Fri, 26 Feb 2021 01:06:01 +0100 Jan Dankert Refactoring accessing the request parameter values. Wed, 18 Nov 2020 01:46:36 +0100 Jan Dankert Refactoring of model classes: New method persist() and some other cleanups. Tue, 17 Nov 2020 23:51:00 +0100 Jan Dankert Refactoring: Every Actionmethod has now its own class.