3131
3232class PluginFormcreatorFileField extends PluginFormcreatorField
3333{
34+ /**@var $uploadData array uploads saved as documents */
3435 private $ uploadData = [];
3536
37+ /** @var $uploads array uploaded files on form submit */
38+ private $ uploads = [
39+ '_filename ' => [],
40+ '_prefix_filename ' => [],
41+ '_tag_filename ' => [],
42+ ];
43+
3644 public function isPrerequisites () {
3745 return true ;
3846 }
@@ -43,6 +51,7 @@ public function displayField($canEdit = true) {
4351 'name ' => 'formcreator_field_ ' . $ this ->question ->getID (),
4452 'display ' => false ,
4553 'multiple ' => 'multiple ' ,
54+ 'uploads ' => $ this ->uploads ,
4655 ]);
4756 } else {
4857 $ doc = new Document ();
@@ -115,6 +124,25 @@ public static function canRequire() {
115124 return true ;
116125 }
117126
127+ public function saveUploads ($ input ) {
128+ $ key = 'formcreator_field_ ' . $ this ->question ->getID ();
129+ $ index = 0 ;
130+ $ answer_value = [];
131+ foreach ($ input ["_ $ key " ] as $ document ) {
132+ $ document = Toolbox::stripslashes_deep ($ document );
133+ if (is_file (GLPI_TMP_DIR . '/ ' . $ document )) {
134+ $ prefix = $ input ['_prefix_formcreator_field_ ' . $ this ->question ->getID ()][$ index ];
135+ $ answer_value [] = $ this ->saveDocument ($ document , $ prefix );
136+ }
137+ $ index ++;
138+ }
139+ $ this ->uploadData = $ answer_value ;
140+ }
141+
142+ public function hasInput ($ input ) {
143+ return isset ($ input ['_formcreator_field_ ' . $ this ->question ->getID ()]);
144+ }
145+
118146 /**
119147 * Save an uploaded file into a document object, link it to the answers
120148 * and returns the document ID
@@ -172,27 +200,19 @@ private function saveDocument($file, $prefix) {
172200
173201 public function parseAnswerValues ($ input , $ nonDestructive = false ) {
174202 $ key = 'formcreator_field_ ' . $ this ->question ->getID ();
203+ if (isset ($ input ['_tag_ ' . $ key ]) && isset ($ input ['_ ' . $ key ]) && isset ($ input ['_prefix_ ' . $ key ])) {
204+ $ this ->uploads ['_ ' . $ key ] = $ input ['_ ' . $ key ];
205+ $ this ->uploads ['_prefix_ ' . $ key ] = $ input ['_prefix_ ' . $ key ];
206+ $ this ->uploads ['_tag_ ' . $ key ] = $ input ['_tag_ ' . $ key ];
207+ }
175208 if (isset ($ input ["_ $ key " ])) {
176209 if (!is_array ($ input ["_ $ key " ])) {
177210 return false ;
178211 }
179212
180- $ answer_value = [];
181- $ index = 0 ;
182- if ($ nonDestructive ) {
183- $ index = count ($ input ["_ $ key " ]);
184- } else {
185- foreach ($ input ["_ $ key " ] as $ document ) {
186- $ document = Toolbox::stripslashes_deep ($ document );
187- if (is_file (GLPI_TMP_DIR . '/ ' . $ document )) {
188- $ prefix = $ input ['_prefix_formcreator_field_ ' . $ this ->question ->getID ()][$ index ];
189- $ answer_value [] = $ this ->saveDocument ($ document , $ prefix );
190- }
191- $ index ++;
192- }
213+ if (isset ($ input ["_ $ key " ])) {
214+ $ this ->value = __ ('Attached document ' , 'formcreator ' );
193215 }
194- $ this ->uploadData = $ answer_value ;
195- $ this ->value = __ ('Attached document ' , 'formcreator ' );
196216 return true ;
197217 }
198218 $ this ->uploadData = [];
0 commit comments