Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
tao_helpers_form_elements_xhtml_AsyncFile
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 3
240
0.00% covered (danger)
0.00%
0 / 1
 feed
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 render
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 1
132
 getEvaluatedValue
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
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-2012 (update and modification) Public Research Centre Henri Tudor
21 *                         (under the project TAO-SUSTAIN & TAO-DEV);
22 *
23 */
24
25use oat\generis\Helper\SystemHelper;
26use oat\tao\helpers\form\elements\xhtml\XhtmlRenderingTrait;
27
28/**
29 * Short description of class tao_helpers_form_elements_xhtml_AsyncFile
30 *
31 * @access public
32 * @author Joel Bout, <joel.bout@tudor.lu>
33 * @package tao
34 */
35class tao_helpers_form_elements_xhtml_AsyncFile extends tao_helpers_form_elements_AsyncFile
36{
37    use XhtmlRenderingTrait;
38
39    /**
40     * Short description of method feed
41     *
42     * @access public
43     * @author Joel Bout, <joel.bout@tudor.lu>
44     */
45    public function feed()
46    {
47        common_Logger::t('Evaluating AsyncFile ' . $this->getName(), [
48            'TAO']);
49        if (isset($_POST[$this->name])) {
50            $struct = json_decode($_POST[$this->name], true);
51            if ($struct !== false) {
52                $this->setValue($struct);
53            } else {
54                common_Logger::w('Could not unserialise AsyncFile field ' . $this->getName(), [
55                    'TAO']);
56            }
57        }
58    }
59
60    /**
61     * Short description of method render
62     *
63     * @access public
64     * @author Joel Bout, <joel.bout@tudor.lu>
65     * @return string
66     */
67    public function render()
68    {
69        $widgetName = 'Uploader_' . md5($this->name);
70
71        $returnValue = $this->renderLabel();
72        $returnValue .= "<input type='hidden' name='{$this->name}' id='{$this->name}' value='' />";
73        $returnValue .= "<div id='{$widgetName}_container' class='form-elt-container file-uploader'>";
74
75        // get the upload max size
76        $fileSize = SystemHelper::getFileUploadLimit();
77
78        $mimetypes = [];
79
80        // add a client validation
81        foreach ($this->validators as $validator) {
82            // get the valid file extensions
83            if ($validator instanceof tao_helpers_form_validators_FileMimeType) {
84                $options = $validator->getOptions();
85                if (isset($options['mimetype'])) {
86                    $mimetypes = $options['mimetype'];
87                }
88            }
89            // get the max file size
90            if ($validator instanceof tao_helpers_form_validators_FileSize) {
91                $options = $validator->getOptions();
92                if (isset($options['max'])) {
93                    $validatorMax = (int) $options['max'];
94                    if ($validatorMax > 0 && $validatorMax < $fileSize) {
95                        $fileSize = $validatorMax;
96                    }
97                }
98            }
99        }
100
101        // default value for 'auto' is 'true':
102        $auto = 'true';
103        if (isset($this->attributes['auto'])) {
104            if (! $this->attributes['auto'] || $this->attributes['auto'] === 'false') {
105                $auto = 'false';
106            }
107            unset($this->attributes['auto']);
108        }
109
110        // initialize the Uploader Js component
111        $returnValue .= '<script type="text/javascript">
112                require([\'jquery\', \'i18n\', \'ui/feedback\', \'ui/uploader\'], function($, __, feedback){
113                     $("#' . $widgetName . '_container").uploader({
114                             uploadUrl: "' . ROOT_URL . 'tao/File/upload",
115                            autoUpload: "' . $auto . '"  ,
116                            showResetButton: "' . ! $auto . '" ,
117                            showUploadButton: "' . ! $auto . '" ,
118                            fileSelect  : function(files, done){
119                                            var error = [],
120                                                files = files.filter(_.isObject),// due to Chrome drag\'n\'drop issue
121                                                givenLength = files.length,
122                                                filters = "' . implode(',', $mimetypes) . '"
123                                                    .split(",")
124                                                    .filter(function(e){return e.length});
125
126                                                if (filters.length){
127
128                                                    files = _.filter(files, function(file){
129                                                        // IE9 doesnt detect type, so lets rely on server validation
130                                                        return !file.type
131                                                            || _.includes(filters, file.type.replace(/[\'"]+/g, \'\'));
132                                                    });
133
134                                                    if(files.length !== givenLength){
135                                                        error.push(
136                                                            "' . __("Unauthorized files have been removed") . '"
137                                                        );
138                                                    }
139
140                                                }
141
142                                                files = _.filter(files, function(file){
143                                                        return file.size <= ' . $fileSize . ';
144                                                    });
145
146                                                if(files.length !== givenLength && !error.length){
147                                                    error.push( "Size limit is ' . $fileSize . ' bytes");
148                                                }
149
150                                                if (error.length){
151                                                    feedback().error(error.join(","));
152                                                }
153
154                                                done(files);
155                                                if ( ' . $auto . ' ){
156                                                    $(this).uploader("upload");
157                                                }
158                                         }
159
160                     }).on("upload.uploader", function(e, file, result){
161                         if ( result && result.uploaded ){
162                            $("input[name=\'' . $this->getName() . '\']").val(result.data);
163                        }
164                     })
165            });
166            </script>';
167        $returnValue .= "</div>";
168
169        return (string) $returnValue;
170    }
171
172    /**
173     * Short description of method getEvaluatedValue
174     *
175     * @access public
176     * @author Joel Bout, <joel.bout@tudor.lu>
177     * @return mixed
178     */
179    public function getEvaluatedValue()
180    {
181        return $this->getRawValue();
182    }
183}