Skip to content

Commit

Permalink
fix(filefield): uploaded files lost
Browse files Browse the repository at this point in the history
When a requester uploads a file then trigger a field visibility update, files are processed early and moved.
When saving answers, the file is no longer available for building the answer and the generated targets

Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry committed Apr 19, 2019
1 parent 04791f4 commit 1cec1e0
Show file tree
Hide file tree
Showing 23 changed files with 32 additions and 28 deletions.
3 changes: 2 additions & 1 deletion inc/fieldinterface.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,10 @@ public function prepareQuestionInputForSave($input);
/**
* Read the value of the field from answers
* @param array $input answers of all questions of the form
* @param boolean $nonDestructive for File field, ensure that the file uploads imported as document
* @return boolean true on sucess, false otherwise
*/
public function parseAnswerValues($input);
public function parseAnswerValues($input, $nonDestructive = false);

/**
* Prepares an answer value for output in a target object
Expand Down
2 changes: 1 addition & 1 deletion inc/fields.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ public static function updateVisibility($input) {
$question->fields['fieldtype'],
$question
);
$fields[$id]->parseAnswerValues($input);
$fields[$id]->parseAnswerValues($input, true);
}

$questionToShow = [];
Expand Down
2 changes: 1 addition & 1 deletion inc/fields/actorfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public function prepareQuestionInputForSave($input) {
return $input;
}

public function parseAnswerValues($input) {
public function parseAnswerValues($input, $nonDestructive = false) {
$key = 'formcreator_field_' . $this->fields['id'];
if (!isset($input[$key])) {
$this->value = [];
Expand Down
2 changes: 1 addition & 1 deletion inc/fields/checkboxesfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function getValueForDesign() {
return implode("\r\n", $this->value);
}

public function parseAnswerValues($input) {
public function parseAnswerValues($input, $nonDestructive = false) {
$key = 'formcreator_field_' . $this->fields['id'];
if (!isset($input[$key])) {
$input[$key] = [];
Expand Down
2 changes: 1 addition & 1 deletion inc/fields/datefield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public function lessThan($value) {
return !$this->greaterThan($value) && !$this->equals($value);
}

public function parseAnswerValues($input) {
public function parseAnswerValues($input, $nonDestructive = false) {

$key = 'formcreator_field_' . $this->fields['id'];
if (!is_string($input[$key])) {
Expand Down
2 changes: 1 addition & 1 deletion inc/fields/datetimefield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public function lessThan($value) {
return !$this->greaterThan($value) && !$this->equals($value);
}

public function parseAnswerValues($input) {
public function parseAnswerValues($input, $nonDestructive = false) {
$key = 'formcreator_field_' . $this->fields['id'];
if (!is_string($input[$key])) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion inc/fields/descriptionfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function lessThan($value) {
throw new PluginFormcreatorComparisonException('Meaningless comparison');
}

public function parseAnswerValues($input) {
public function parseAnswerValues($input, $nonDestructive = false) {
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion inc/fields/dropdownfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ public function lessThan($value) {
return !$this->greaterThan($value) && !$this->equals($value);
}

public function parseAnswerValues($input) {
public function parseAnswerValues($input, $nonDestructive = false) {
$key = 'formcreator_field_' . $this->fields['id'];
if (!isset($input[$key])) {
$input[$key] = '0';
Expand Down
2 changes: 1 addition & 1 deletion inc/fields/emailfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function prepareQuestionInputForSave($input) {
return $input;
}

public function parseAnswerValues($input) {
public function parseAnswerValues($input, $nonDestructive = false) {
$key = 'formcreator_field_' . $this->fields['id'];
if (!is_string($input[$key])) {
return false;
Expand Down
16 changes: 10 additions & 6 deletions inc/fields/filefield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ private function saveDocument($file, $prefix) {
return null;
}

public function parseAnswerValues($input) {
public function parseAnswerValues($input, $nonDestructive = false) {
$key = 'formcreator_field_' . $this->fields['id'];
if (isset($input["_$key"])) {
if (!is_array($input["_$key"])) {
Expand All @@ -197,12 +197,16 @@ public function parseAnswerValues($input) {

$answer_value = [];
$index = 0;
foreach ($input["_$key"] as $document) {
if (is_file(GLPI_TMP_DIR . '/' . $document)) {
$prefix = $input['_prefix_formcreator_field_' . $this->fields['id']][$index];
$answer_value[] = $this->saveDocument($document, $prefix);
if ($nonDestructive) {
$index = count($input["_$key"]);
} else {
foreach ($input["_$key"] as $document) {
if (is_file(GLPI_TMP_DIR . '/' . $document)) {
$prefix = $input['_prefix_formcreator_field_' . $this->fields['id']][$index];
$answer_value[] = $this->saveDocument($document, $prefix);
}
$index++;
}
$index++;
}
$this->uploadData = $answer_value;
$this->value = __('Attached document', 'formcreator');
Expand Down
2 changes: 1 addition & 1 deletion inc/fields/floatfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public function prepareQuestionInputForSave($input) {
return $input;
}

public function parseAnswerValues($input) {
public function parseAnswerValues($input, $nonDestructive = false) {
$key = 'formcreator_field_' . $this->fields['id'];
if (!is_string($input[$key])) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion inc/fields/hiddenfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static function getPrefs() {
];
}

public function parseAnswerValues($input) {
public function parseAnswerValues($input, $nonDestructive = false) {
$key = 'formcreator_field_' . $this->fields['id'];
if (!is_string($input[$key])) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion inc/fields/hostnamefield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static function getPrefs() {
];
}

public function parseAnswerValues($input) {
public function parseAnswerValues($input, $nonDestructive = false) {
$key = 'formcreator_field_' . $this->fields['id'];
if (!is_string($input[$key])) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion inc/fields/integerfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public function getEmptyParameters() {
];
}

public function parseAnswerValues($input) {
public function parseAnswerValues($input, $nonDestructive = false) {
$key = 'formcreator_field_' . $this->fields['id'];
if (!is_string($input[$key])) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion inc/fields/ipfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public static function getPrefs() {
];
}

public function parseAnswerValues($input) {
public function parseAnswerValues($input, $nonDestructive = false) {
$key = 'formcreator_field_' . $this->fields['id'];
if (!is_string($input[$key])) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion inc/fields/ldapselectfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public static function getJSFields() {
return "tab_fields_fields['ldapselect'] = 'showFields(" . implode(', ', $prefs) . ");';";
}

public function parseAnswerValues($input) {
public function parseAnswerValues($input, $nonDestructive = false) {
$key = 'formcreator_field_' . $this->fields['id'];
if (!isset($input[$key])) {
$input[$key] = '';
Expand Down
2 changes: 1 addition & 1 deletion inc/fields/multiselectfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public static function getPrefs() {
];
}

public function parseAnswerValues($input) {
public function parseAnswerValues($input, $nonDestructive = false) {
$key = 'formcreator_field_' . $this->fields['id'];
if (!isset($input[$key])) {
$input[$key] = [];
Expand Down
2 changes: 1 addition & 1 deletion inc/fields/radiosfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function prepareQuestionInputForSave($input) {
return $input;
}

public function parseAnswerValues($input) {
public function parseAnswerValues($input, $nonDestructive = false) {
$key = 'formcreator_field_' . $this->fields['id'];
if (isset($input[$key])) {
if (!is_string($input[$key])) {
Expand Down
2 changes: 1 addition & 1 deletion inc/fields/selectfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function prepareQuestionInputForSave($input) {
return $input;
}

public function parseAnswerValues($input) {
public function parseAnswerValues($input, $nonDestructive = false) {
$key = 'formcreator_field_' . $this->fields['id'];
if (!is_string($input[$key])) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion inc/fields/tagfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function prepareQuestionInputForSave($input) {
return $input;
}

public function parseAnswerValues($input) {
public function parseAnswerValues($input, $nonDestructive = false) {
$key = 'formcreator_field_' . $this->fields['id'];
if (!isset($input[$key])) {
$input[$key] = [];
Expand Down
2 changes: 1 addition & 1 deletion inc/fields/textfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public static function getJSFields() {
return "tab_fields_fields['text'] = 'showFields(" . implode(', ', $prefs) . ");';";
}

public function parseAnswerValues($input) {
public function parseAnswerValues($input, $nonDestructive = false) {
$key = 'formcreator_field_' . $this->fields['id'];
if (!isset($input[$key])) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion inc/fields/urgencyfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function prepareQuestionInputForSave($input) {
return $input;
}

public function parseAnswerValues($input) {
public function parseAnswerValues($input, $nonDestructive = false) {
$key = 'formcreator_field_' . $this->fields['id'];
if (!isset($input[$key])) {
$input[$key] = '3';
Expand Down
1 change: 0 additions & 1 deletion inc/formanswer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,6 @@ public function saveAnswers(PluginFormcreatorForm $form, $data, $fields) {
'plugin_formcreator_questions_id' => $questionId,
]);
$answer->update([

'id' => $answer->getID(),
'answer' => $fields[$questionId]->serializeValue(),
], 0);
Expand Down

0 comments on commit 1cec1e0

Please sign in to comment.