Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 116 |
|
0.00% |
0 / 12 |
CRAP | |
0.00% |
0 / 1 |
| tao_helpers_form_elements_xhtml_GenerisAsyncFile | |
0.00% |
0 / 116 |
|
0.00% |
0 / 12 |
1122 | |
0.00% |
0 / 1 |
| feed | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
72 | |||
| render | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
30 | |||
| getEvaluatedValue | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| buildDeleterBehaviour | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| buildUploaderBehaviour | |
0.00% |
0 / 38 |
|
0.00% |
0 / 1 |
132 | |||
| buildWidgetName | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| buildDeleteButtonId | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| buildWidgetContainerId | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| embedBehaviour | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| buildDownloadButtonId | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| buildDownloaderBehaviour | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
2 | |||
| buildIframeId | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung |
| 19 | * (under the project TAO-TRANSFER); |
| 20 | * 2009-2016 (update and modification) Public Research Centre Henri Tudor |
| 21 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | use oat\tao\helpers\form\elements\xhtml\XhtmlRenderingTrait; |
| 26 | |
| 27 | /** |
| 28 | * Short description of class tao_helpers_form_elements_xhtml_GenerisAsyncFile |
| 29 | * |
| 30 | * @access public |
| 31 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
| 32 | * @package tao |
| 33 | */ |
| 34 | class tao_helpers_form_elements_xhtml_GenerisAsyncFile extends tao_helpers_form_elements_GenerisAsyncFile |
| 35 | { |
| 36 | use XhtmlRenderingTrait; |
| 37 | |
| 38 | /** |
| 39 | * Short description of method feed |
| 40 | * |
| 41 | * @access public |
| 42 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
| 43 | * @return mixed |
| 44 | */ |
| 45 | public function feed() |
| 46 | { |
| 47 | if (isset($_POST[$this->name])) { |
| 48 | $structure = json_decode($_POST[$this->name], true); |
| 49 | if ($structure !== false) { |
| 50 | $description = new tao_helpers_form_data_UploadFileDescription( |
| 51 | array_key_exists( |
| 52 | 'name', |
| 53 | $structure |
| 54 | ) ? $structure['name'] : null, |
| 55 | array_key_exists('size', $structure) ? $structure['size'] : null, |
| 56 | array_key_exists('type', $structure) ? $structure['type'] : null, |
| 57 | array_key_exists('uploaded_file', $structure) ? $structure['uploaded_file'] : null, |
| 58 | array_key_exists('action', $structure) ? $structure['action'] : null |
| 59 | ); |
| 60 | $this->setValue($description); |
| 61 | } else { |
| 62 | // else, no file was selected by the end user. |
| 63 | // set the value as empty in order |
| 64 | $this->setValue($_POST[$this->name]); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Short description of method render |
| 71 | * |
| 72 | * @access public |
| 73 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
| 74 | * @return string |
| 75 | */ |
| 76 | public function render() |
| 77 | { |
| 78 | $widgetContainerId = $this->buildWidgetContainerId(); |
| 79 | |
| 80 | $returnValue = $this->renderLabel(); |
| 81 | $returnValue .= "<div id='${widgetContainerId}' class='form-elt-container file-uploader'>"; |
| 82 | |
| 83 | if ( |
| 84 | $this->value instanceof tao_helpers_form_data_UploadFileDescription |
| 85 | && $this->value->getAction() == tao_helpers_form_data_UploadFileDescription::FORM_ACTION_DELETE |
| 86 | ) { |
| 87 | // File deleted, nothing to render |
| 88 | } elseif ( |
| 89 | $this->value instanceof tao_helpers_form_data_FileDescription |
| 90 | && ($file = $this->value->getFile()) != null |
| 91 | ) { |
| 92 | // A file is stored or has just been uploaded. |
| 93 | $shownFileName = $this->value->getName(); |
| 94 | $shownFileSize = $this->value->getSize(); |
| 95 | $shownFileSize = number_format($shownFileSize / 1000, 2); // to kb. |
| 96 | $shownFileTxt = sprintf(__('%s (%s kb)'), $shownFileName, $shownFileSize); |
| 97 | $deleteButtonTitle = __("Delete"); |
| 98 | $deleteButtonId = $this->buildDeleteButtonId(); |
| 99 | $downloadButtonTitle = __("Download"); |
| 100 | $downloadButtonId = $this->buildDownloadButtonId(); |
| 101 | $iFrameId = $this->buildIframeId(); |
| 102 | $returnValue .= "<span class=\"widget_AsyncFile_fileinfo\">${shownFileTxt}</span>"; |
| 103 | $returnValue .= "<button id=\"${downloadButtonId}\" type=\"button\" " |
| 104 | . "class=\"download btn-neutral small icon-download\" title=\"${downloadButtonTitle}\">"; |
| 105 | $returnValue .= "<button id=\"${deleteButtonId}\" type=\"button\" " |
| 106 | . "class=\"delete btn-error small icon-bin\" title=\"${deleteButtonTitle}\"/>"; |
| 107 | $returnValue .= "<iframe style=\"display:none\" id=\"${iFrameId}\" frameborder=\"0\"/>"; |
| 108 | |
| 109 | // Inject behaviour of the Delete/Download buttons component in response. |
| 110 | $returnValue .= self::embedBehaviour($this->buildDeleterBehaviour() . $this->buildDownloaderBehaviour()); |
| 111 | } else { |
| 112 | // No file stored yet. |
| 113 | // Inject behaviour of the AsyncFileUpload component in response. |
| 114 | $returnValue .= self::embedBehaviour($this->buildUploaderBehaviour()); |
| 115 | } |
| 116 | |
| 117 | $returnValue .= "</div>"; |
| 118 | |
| 119 | return (string) $returnValue; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Short description of method getEvaluatedValue |
| 124 | * |
| 125 | * @access public |
| 126 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
| 127 | * @return mixed |
| 128 | */ |
| 129 | public function getEvaluatedValue() |
| 130 | { |
| 131 | return $this->getRawValue(); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Short description of method buildDeleterBehaviour |
| 136 | * |
| 137 | * @access public |
| 138 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
| 139 | * @return string |
| 140 | */ |
| 141 | public function buildDeleterBehaviour() |
| 142 | { |
| 143 | return '$(document).ready(function() { |
| 144 | $("#' . $this->buildDeleteButtonId() . '").click(function() { |
| 145 | var $form = $(this).parents("form"), |
| 146 | $fileHandling = $form.find("[name=\'' . $this->getName() . '\']"); |
| 147 | if(!$fileHandling.length) { |
| 148 | $fileHandling = $("<input>", { name: "' . $this->getName() . '", type: "hidden" }); |
| 149 | $form.prepend($fileHandling); |
| 150 | } |
| 151 | $fileHandling.val("{\\"action\\":\\"delete\\"}"); |
| 152 | $("#' . $this->buildWidgetContainerId() . '").empty(); |
| 153 | ' . $this->buildUploaderBehaviour(true) . ' |
| 154 | }); |
| 155 | });'; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Short description of method buildUploaderBehaviour |
| 160 | * |
| 161 | * @access public |
| 162 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
| 163 | * @param boolean deleted |
| 164 | * @return string |
| 165 | */ |
| 166 | public function buildUploaderBehaviour($deleted = false) |
| 167 | { |
| 168 | $returnValue = (string) ''; |
| 169 | |
| 170 | $widgetName = $this->buildWidgetName(); |
| 171 | |
| 172 | // get the upload max size (the min of those 3 directives) |
| 173 | $max_upload = (int) (ini_get('upload_max_filesize')); |
| 174 | $max_post = (int) (ini_get('post_max_size')); |
| 175 | $memory_limit = (int) (ini_get('memory_limit')); |
| 176 | $fileSize = min($max_upload, $max_post, $memory_limit) * 1024 * 1024; |
| 177 | |
| 178 | $mimetypes = []; |
| 179 | |
| 180 | // add a client validation |
| 181 | foreach ($this->validators as $validator) { |
| 182 | // get the valid file extensions |
| 183 | if ($validator instanceof tao_helpers_form_validators_FileMimeType) { |
| 184 | $options = $validator->getOptions(); |
| 185 | if (isset($options['mimetype'])) { |
| 186 | $mimetypes = $options['mimetype']; |
| 187 | } |
| 188 | } |
| 189 | // get the max file size |
| 190 | if ($validator instanceof tao_helpers_form_validators_FileSize) { |
| 191 | $options = $validator->getOptions(); |
| 192 | if (isset($options['max'])) { |
| 193 | $validatorMax = (int) $options['max']; |
| 194 | if ($validatorMax > 0 && $validatorMax < $fileSize) { |
| 195 | $fileSize = $validatorMax; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | // default value for 'auto' is 'true': |
| 202 | $auto = 'true'; |
| 203 | if (isset($this->attributes['auto'])) { |
| 204 | if (! $this->attributes['auto'] || $this->attributes['auto'] === 'false') { |
| 205 | $auto = 'false'; |
| 206 | } |
| 207 | unset($this->attributes['auto']); |
| 208 | } |
| 209 | |
| 210 | // initialize the Uploader Js component |
| 211 | $returnValue .= ' |
| 212 | require([\'jquery\', \'i18n\', \'ui/feedback\', \'ui/uploader\'], function($, __, feedback){ |
| 213 | $("#' . $this->buildWidgetContainerId() . '").uploader({ |
| 214 | uploadUrl: "' . ROOT_URL . 'tao/File/upload", |
| 215 | autoUpload: "' . $auto . '" , |
| 216 | showResetButton: "' . ! $auto . '" , |
| 217 | showUploadButton: "' . ! $auto . '" , |
| 218 | fileSelect : function(files, done){ |
| 219 | var error = [], |
| 220 | givenLength = files.length, |
| 221 | filters = "' . implode(',', $mimetypes) |
| 222 | . '".split(",").filter(function(e){return e.length}); |
| 223 | |
| 224 | if (filters.length){ |
| 225 | |
| 226 | files = _.filter(files, function(file){ |
| 227 | return _.includes(filters, file.type); |
| 228 | }); |
| 229 | |
| 230 | if(files.length !== givenLength){ |
| 231 | error.push( |
| 232 | "' . __("Unauthorized files have been removed") . '" |
| 233 | ); |
| 234 | } |
| 235 | |
| 236 | } |
| 237 | |
| 238 | files = _.filter(files, function(file){ |
| 239 | return file.size <= ' . $fileSize . '; |
| 240 | }); |
| 241 | |
| 242 | if(files.length !== givenLength && !error.length){ |
| 243 | error.push( "Size limit is ' . $fileSize . ' bytes"); |
| 244 | } |
| 245 | |
| 246 | if (error.length){ |
| 247 | feedback().error(error.join(",")); |
| 248 | } |
| 249 | |
| 250 | done(files); |
| 251 | if ( ' . $auto . ' ){ |
| 252 | $(this).uploader("upload"); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | }).on("upload.uploader", function(e, file, result){ |
| 257 | if ( result && result.uploaded ){ |
| 258 | var $container = $(e.target); |
| 259 | var $form = $container.parents("form"); |
| 260 | var $fileHandling = $form.find("[name=\'file_handling\']"); |
| 261 | $container.append( |
| 262 | $("<input type=\'hidden\' name=\'' . $this->getName() . '\'/>").val(result.data) |
| 263 | ); |
| 264 | } |
| 265 | }) |
| 266 | });'; |
| 267 | |
| 268 | return (string) $returnValue; |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Short description of method buildWidgetName |
| 273 | * |
| 274 | * @access public |
| 275 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
| 276 | * @return string |
| 277 | */ |
| 278 | public function buildWidgetName() |
| 279 | { |
| 280 | $returnValue = (string) ''; |
| 281 | |
| 282 | $returnValue = 'AsyncFileUploader_' . md5($this->name); |
| 283 | |
| 284 | return (string) $returnValue; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Short description of method buildDeleteButtonId |
| 289 | * |
| 290 | * @access public |
| 291 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
| 292 | * @return string |
| 293 | */ |
| 294 | public function buildDeleteButtonId() |
| 295 | { |
| 296 | $returnValue = (string) ''; |
| 297 | |
| 298 | $returnValue = $this->buildWidgetName() . '_deleter'; |
| 299 | |
| 300 | return (string) $returnValue; |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Short description of method buildWidgetContainerId |
| 305 | * |
| 306 | * @access public |
| 307 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
| 308 | * @return string |
| 309 | */ |
| 310 | public function buildWidgetContainerId() |
| 311 | { |
| 312 | $returnValue = (string) ''; |
| 313 | |
| 314 | $returnValue = $this->buildWidgetName() . '_container'; |
| 315 | |
| 316 | return (string) $returnValue; |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Short description of method embedBehaviour |
| 321 | * |
| 322 | * @access public |
| 323 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
| 324 | * @param string behaviour |
| 325 | * @return string |
| 326 | */ |
| 327 | public function embedBehaviour($behaviour) |
| 328 | { |
| 329 | $returnValue = (string) ''; |
| 330 | |
| 331 | $returnValue = '<script type="text/javascript">' . $behaviour . '</script>'; |
| 332 | |
| 333 | return (string) $returnValue; |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Short description of method buildDownloadButtonId |
| 338 | * |
| 339 | * @access public |
| 340 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
| 341 | * @return string |
| 342 | */ |
| 343 | public function buildDownloadButtonId() |
| 344 | { |
| 345 | $returnValue = (string) ''; |
| 346 | |
| 347 | $returnValue = $this->buildWidgetName() . '_downloader'; |
| 348 | |
| 349 | return (string) $returnValue; |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Short description of method buildDownloaderBehaviour |
| 354 | * |
| 355 | * @access public |
| 356 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
| 357 | * @return string |
| 358 | */ |
| 359 | public function buildDownloaderBehaviour() |
| 360 | { |
| 361 | $returnValue = (string) ''; |
| 362 | |
| 363 | $downloadButtonId = $this->buildDownloadButtonId(); |
| 364 | $iFrameId = $this->buildIframeId(); |
| 365 | $serial = $this->value->getFileSerial(); |
| 366 | |
| 367 | $returnValue .= '$(document).ready(function() {'; |
| 368 | $returnValue .= ' $("#' . $downloadButtonId . '").click(function() {'; |
| 369 | $returnValue .= ' $("#' . $iFrameId . '").attr("src", ' |
| 370 | . json_encode(_url('downloadFile', 'File', 'tao', ['id' => $serial])) . ')'; |
| 371 | $returnValue .= ' });'; |
| 372 | $returnValue .= '});'; |
| 373 | |
| 374 | return (string) $returnValue; |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Short description of method buildIframeId |
| 379 | * |
| 380 | * @access public |
| 381 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
| 382 | * @return string |
| 383 | */ |
| 384 | public function buildIframeId() |
| 385 | { |
| 386 | $returnValue = (string) ''; |
| 387 | |
| 388 | $returnValue = $this->buildWidgetName() . '_iframe'; |
| 389 | |
| 390 | return (string) $returnValue; |
| 391 | } |
| 392 | } |