Skip to content

Commit 488e2d5

Browse files
committed
fix(textarea): better file uplaods handling
when answers are rejected, file uploads are lost Relies on glpi-project/glpi#6936 for GLPI Signed-off-by: Thierry Bugier <[email protected]>
1 parent fb37c46 commit 488e2d5

24 files changed

+144
-27
lines changed

inc/fieldinterface.class.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ public function getDocumentsForTarget();
113113
*/
114114
public function prepareQuestionInputForSave($input);
115115

116+
/**
117+
* Do the argument has an user input ?
118+
* @param array $input answers of all questions of the form
119+
* @return boolean
120+
*/
121+
public function hasInput($input);
122+
116123
/**
117124
* Read the value of the field from answers
118125
* @param array $input answers of all questions of the form

inc/fields/actorfield.class.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,10 @@ public function prepareQuestionInputForSave($input) {
280280
return $input;
281281
}
282282

283+
public function hasInput($input) {
284+
return isset($input['formcreator_field_' . $this->question->getID()]);
285+
}
286+
283287
public function parseAnswerValues($input, $nonDestructive = false) {
284288
$key = 'formcreator_field_' . $this->question->getID();
285289
if (!isset($input[$key])) {

inc/fields/checkboxesfield.class.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,10 @@ public function prepareQuestionInputForSave($input) {
243243
return $input;
244244
}
245245

246+
public function hasInput($input) {
247+
return isset($input['formcreator_field_' . $this->question->getID()]);
248+
}
249+
246250
public function getValueForTargetText($richText) {
247251
$value = [];
248252
$values = $this->getAvailableValues();

inc/fields/datefield.class.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ public function getValueForTargetText($richText) {
7070
return Toolbox::addslashes_deep(Html::convDate($this->value));
7171
}
7272

73+
public function hasInput($input) {
74+
return isset($input['formcreator_field_' . $this->question->getID()]);
75+
}
76+
7377
public function getDocumentsForTarget() {
7478
return [];
7579
}

inc/fields/datetimefield.class.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ public function getValueForDesign() {
6969
return $this->value;
7070
}
7171

72+
public function hasInput($input) {
73+
return isset($input['formcreator_field_' . $this->question->getID()]);
74+
}
75+
7276
public function getValueForTargetText($richText) {
7377
return Toolbox::addslashes_deep(Html::convDateTime($this->value));
7478
}

inc/fields/descriptionfield.class.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ public function prepareQuestionInputForSave($input) {
9797
return $input;
9898
}
9999

100+
public function hasInput($input) {
101+
return false;
102+
}
103+
100104
public static function canRequire() {
101105
return false;
102106
}

inc/fields/dropdownfield.class.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,10 @@ public function prepareQuestionInputForSave($input) {
408408
return $input;
409409
}
410410

411+
public function hasInput($input) {
412+
return isset($input['formcreator_field_' . $this->question->getID()]);
413+
}
414+
411415
public static function canRequire() {
412416
return true;
413417
}

inc/fields/emailfield.class.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ public function prepareQuestionInputForSave($input) {
119119
return $input;
120120
}
121121

122+
public function hasInput($input) {
123+
return isset($input['formcreator_field_' . $this->question->getID()]);
124+
}
125+
122126
public function parseAnswerValues($input, $nonDestructive = false) {
123127
$key = 'formcreator_field_' . $this->question->getID();
124128
if (!is_string($input[$key])) {

inc/fields/filefield.class.php

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,16 @@
3131

3232
class 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 = [];

inc/fields/floatfield.class.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public function isValid() {
138138
private function isValidValue($value) {
139139
if (strlen($value) == 0) {
140140
return true;
141-
}
141+
}
142142

143143
if (!empty($value) && !is_numeric($value)) {
144144
Session::addMessageAfterRedirect(sprintf(__('This is not a number: %s', 'formcreator'), $this->question->fields['name']), false, ERROR);
@@ -209,6 +209,10 @@ public function prepareQuestionInputForSave($input) {
209209
return $input;
210210
}
211211

212+
public function hasInput($input) {
213+
return isset($input['formcreator_field_' . $this->question->getID()]);
214+
}
215+
212216
public function parseAnswerValues($input, $nonDestructive = false) {
213217
$key = 'formcreator_field_' . $this->question->getID();
214218
if (!is_string($input[$key])) {

0 commit comments

Comments
 (0)