ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
AbstractCtrlAwareUploadHandler.php
Go to the documentation of this file.
1 <?php declare(strict_types=1);
2 
4 
7 
14 {
15  protected const CMD_UPLOAD = 'upload';
16  protected const CMD_REMOVE = 'remove';
17  protected const CMD_INFO = 'info';
21  protected $http;
25  protected $ctrl;
29  protected $upload;
30 
31 
35  public function __construct()
36  {
37  global $DIC;
38  $this->ctrl = $DIC->ctrl();
39  $this->upload = $DIC->upload();
40  $this->http = $DIC->http();
41  }
42 
43 
47  public function getFileIdentifierParameterName() : string
48  {
49  return self::DEFAULT_FILE_ID_PARAMETER;
50  }
51 
52 
56  public function getUploadURL() : string
57  {
58  return $this->ctrl->getLinkTargetByClass([static::class], self::CMD_UPLOAD);
59  }
60 
61 
65  public function getExistingFileInfoURL() : string
66  {
67  return $this->ctrl->getLinkTargetByClass([static::class], self::CMD_INFO);
68  }
69 
70 
74  public function getFileRemovalURL() : string
75  {
76  return $this->ctrl->getLinkTargetByClass([static::class], self::CMD_REMOVE);
77  }
78 
79 
80  public function executeCommand() : void
81  {
82  switch ($this->ctrl->getCmd()) {
83  case self::CMD_UPLOAD:
84  // Here you must save the file and tell the input item the
85  // file-id which will be a FileStorage-ID in a later version
86  // of ILIAS and for now you must implement an own ID which allows
87  // identifying the file after the request
88  $content = json_encode($this->getUploadResult());
89  break;
90  case self::CMD_REMOVE:
91  // here you delete the previously uploaded file again, you know
92  // which file to delete since you defined what 'my_file_id' is.
93  $file_identifier = $this->http->request()->getQueryParams()[$this->getFileIdentifierParameterName()];
94  $content = json_encode($this->getRemoveResult($file_identifier));
95  break;
96  case self::CMD_INFO:
97  // here you give info about an already existing file
98  // return a JsonEncoded \ILIAS\FileUpload\Handler\FileInfoResult
99  $file_identifier = $this->http->request()->getQueryParams()[$this->getFileIdentifierParameterName()];
100  $content = json_encode($this->getInfoResult($file_identifier));
101  break;
102  default:
103  $content = '';
104  break;
105  }
106  $response = $this->http->response()->withBody(Streams::ofString($content));
107  $this->http->saveResponse($response);
108  $this->http->sendResponse();
109  $this->http->close();
110  }
111 
112 
113  abstract protected function getUploadResult() : HandlerResult;
114 
115 
116  abstract protected function getRemoveResult(string $identifier) : HandlerResult;
117 
118 
119  abstract protected function getInfoResult(string $identifier) : FileInfoResult;
120 
121 
122  abstract public function getInfoForExistingFiles(array $file_ids) : array;
123 }
executeCommand()
Since this is a ilCtrl aware UploadHandler executeCommand MUST be implemented.
static http()
Fetches the global http state from ILIAS.
global $DIC
Definition: goto.php:24
static ofString($string)
Creates a new stream with an initial value.
Definition: Streams.php:25
Exercise XML Parser which completes/updates a given file by an xml string.
$response