Skip to content

Commit

Permalink
fix(textarea): better file uplaods handling
Browse files Browse the repository at this point in the history
when answers are rejected, file uploads are lost

Relies on glpi-project/glpi#6936 for GLPI

Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry committed May 6, 2020
1 parent fb37c46 commit 488e2d5
Show file tree
Hide file tree
Showing 24 changed files with 144 additions and 27 deletions.
7 changes: 7 additions & 0 deletions inc/fieldinterface.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ public function getDocumentsForTarget();
*/
public function prepareQuestionInputForSave($input);

/**
* Do the argument has an user input ?
* @param array $input answers of all questions of the form
* @return boolean
*/
public function hasInput($input);

/**
* Read the value of the field from answers
* @param array $input answers of all questions of the form
Expand Down
4 changes: 4 additions & 0 deletions inc/fields/actorfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ public function prepareQuestionInputForSave($input) {
return $input;
}

public function hasInput($input) {
return isset($input['formcreator_field_' . $this->question->getID()]);
}

public function parseAnswerValues($input, $nonDestructive = false) {
$key = 'formcreator_field_' . $this->question->getID();
if (!isset($input[$key])) {
Expand Down
4 changes: 4 additions & 0 deletions inc/fields/checkboxesfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ public function prepareQuestionInputForSave($input) {
return $input;
}

public function hasInput($input) {
return isset($input['formcreator_field_' . $this->question->getID()]);
}

public function getValueForTargetText($richText) {
$value = [];
$values = $this->getAvailableValues();
Expand Down
4 changes: 4 additions & 0 deletions inc/fields/datefield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public function getValueForTargetText($richText) {
return Toolbox::addslashes_deep(Html::convDate($this->value));
}

public function hasInput($input) {
return isset($input['formcreator_field_' . $this->question->getID()]);
}

public function getDocumentsForTarget() {
return [];
}
Expand Down
4 changes: 4 additions & 0 deletions inc/fields/datetimefield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ public function getValueForDesign() {
return $this->value;
}

public function hasInput($input) {
return isset($input['formcreator_field_' . $this->question->getID()]);
}

public function getValueForTargetText($richText) {
return Toolbox::addslashes_deep(Html::convDateTime($this->value));
}
Expand Down
4 changes: 4 additions & 0 deletions inc/fields/descriptionfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ public function prepareQuestionInputForSave($input) {
return $input;
}

public function hasInput($input) {
return false;
}

public static function canRequire() {
return false;
}
Expand Down
4 changes: 4 additions & 0 deletions inc/fields/dropdownfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,10 @@ public function prepareQuestionInputForSave($input) {
return $input;
}

public function hasInput($input) {
return isset($input['formcreator_field_' . $this->question->getID()]);
}

public static function canRequire() {
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions inc/fields/emailfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ public function prepareQuestionInputForSave($input) {
return $input;
}

public function hasInput($input) {
return isset($input['formcreator_field_' . $this->question->getID()]);
}

public function parseAnswerValues($input, $nonDestructive = false) {
$key = 'formcreator_field_' . $this->question->getID();
if (!is_string($input[$key])) {
Expand Down
50 changes: 35 additions & 15 deletions inc/fields/filefield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,16 @@

class PluginFormcreatorFileField extends PluginFormcreatorField
{
/**@var $uploadData array uploads saved as documents */
private $uploadData = [];

/** @var $uploads array uploaded files on form submit */
private $uploads = [
'_filename' => [],
'_prefix_filename' => [],
'_tag_filename' => [],
];

public function isPrerequisites() {
return true;
}
Expand All @@ -43,6 +51,7 @@ public function displayField($canEdit = true) {
'name' => 'formcreator_field_' . $this->question->getID(),
'display' => false,
'multiple' => 'multiple',
'uploads' => $this->uploads,
]);
} else {
$doc = new Document();
Expand Down Expand Up @@ -115,6 +124,25 @@ public static function canRequire() {
return true;
}

public function saveUploads($input) {
$key = 'formcreator_field_' . $this->question->getID();
$index = 0;
$answer_value = [];
foreach ($input["_$key"] as $document) {
$document = Toolbox::stripslashes_deep($document);
if (is_file(GLPI_TMP_DIR . '/' . $document)) {
$prefix = $input['_prefix_formcreator_field_' . $this->question->getID()][$index];
$answer_value[] = $this->saveDocument($document, $prefix);
}
$index++;
}
$this->uploadData = $answer_value;
}

public function hasInput($input) {
return isset($input['_formcreator_field_' . $this->question->getID()]);
}

/**
* Save an uploaded file into a document object, link it to the answers
* and returns the document ID
Expand Down Expand Up @@ -172,27 +200,19 @@ private function saveDocument($file, $prefix) {

public function parseAnswerValues($input, $nonDestructive = false) {
$key = 'formcreator_field_' . $this->question->getID();
if (isset($input['_tag_' . $key]) && isset($input['_' . $key]) && isset($input['_prefix_' . $key])) {
$this->uploads['_' . $key] = $input['_' . $key];
$this->uploads['_prefix_' . $key] = $input['_prefix_' . $key];
$this->uploads['_tag_' . $key] = $input['_tag_' . $key];
}
if (isset($input["_$key"])) {
if (!is_array($input["_$key"])) {
return false;
}

$answer_value = [];
$index = 0;
if ($nonDestructive) {
$index = count($input["_$key"]);
} else {
foreach ($input["_$key"] as $document) {
$document = Toolbox::stripslashes_deep($document);
if (is_file(GLPI_TMP_DIR . '/' . $document)) {
$prefix = $input['_prefix_formcreator_field_' . $this->question->getID()][$index];
$answer_value[] = $this->saveDocument($document, $prefix);
}
$index++;
}
if (isset($input["_$key"])) {
$this->value = __('Attached document', 'formcreator');
}
$this->uploadData = $answer_value;
$this->value = __('Attached document', 'formcreator');
return true;
}
$this->uploadData = [];
Expand Down
6 changes: 5 additions & 1 deletion inc/fields/floatfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function isValid() {
private function isValidValue($value) {
if (strlen($value) == 0) {
return true;
}
}

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

public function hasInput($input) {
return isset($input['formcreator_field_' . $this->question->getID()]);
}

public function parseAnswerValues($input, $nonDestructive = false) {
$key = 'formcreator_field_' . $this->question->getID();
if (!is_string($input[$key])) {
Expand Down
4 changes: 4 additions & 0 deletions inc/fields/hiddenfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ public function getValueForTargetText($richText) {
return str_replace("\n", '\r\n', Toolbox::addslashes_deep($this->value));
}

public function hasInput($input) {
return isset($input['formcreator_field_' . $this->question->getID()]);
}

public function getDocumentsForTarget() {
return [];
}
Expand Down
4 changes: 4 additions & 0 deletions inc/fields/hostnamefield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public function getValueForTargetText($richText) {
return Toolbox::addslashes_deep($this->value);
}

public function hasInput($input) {
return false;
}

public function getDocumentsForTarget() {
return [];
}
Expand Down
4 changes: 4 additions & 0 deletions inc/fields/integerfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ public function prepareQuestionInputForSave($input) {
return $input;
}

public function hasInput($input) {
return isset($input['formcreator_field_' . $this->question->getID()]);
}

public static function canRequire() {
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions inc/fields/ipfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ public function parseAnswerValues($input, $nonDestructive = false) {
return true;
}

public function hasInput($input) {
return isset($input['formcreator_field_' . $this->question->getID()]);
}

public function equals($value) {
return $this->value == $value;
}
Expand Down
4 changes: 4 additions & 0 deletions inc/fields/ldapselectfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ public function prepareQuestionInputForSave($input) {
return $input;
}

public function hasInput($input) {
return isset($input['formcreator_field_' . $this->question->getID()]);
}

public static function canRequire() {
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions inc/fields/multiselectfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ public function prepareQuestionInputForSave($input) {
return $input;
}

public function hasInput($input) {
return isset($input['formcreator_field_' . $this->question->getID()]);
}

public function getValueForTargetText($richText) {
$input = $this->value;
$value = [];
Expand Down
4 changes: 4 additions & 0 deletions inc/fields/radiosfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ public function prepareQuestionInputForSave($input) {
return $input;
}

public function hasInput($input) {
return isset($input['formcreator_field_' . $this->question->getID()]);
}

public function parseAnswerValues($input, $nonDestructive = false) {
$key = 'formcreator_field_' . $this->question->getID();
if (isset($input[$key])) {
Expand Down
6 changes: 5 additions & 1 deletion inc/fields/tagfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function getDesignSpecializationField() {
'may_be_required' => true,
];
}

public function displayField($canEdit = true) {
global $DB;

Expand Down Expand Up @@ -151,6 +151,10 @@ public function prepareQuestionInputForSave($input) {
return $input;
}

public function hasInput($input) {
return isset($input['formcreator_field_' . $this->question->getID()]);
}

public function parseAnswerValues($input, $nonDestructive = false) {
$key = 'formcreator_field_' . $this->question->getID();
if (!isset($input[$key])) {
Expand Down
28 changes: 19 additions & 9 deletions inc/fields/textareafield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@

class PluginFormcreatorTextareaField extends PluginFormcreatorTextField
{
/** @var $uploads array uploaded files on form submit */
private $uploads = [
'_filename' => [],
'_prefix_filename' => [],
'_tag_filename' => [],
];

public function getDesignSpecializationField() {
$rand = mt_rand();

Expand Down Expand Up @@ -86,6 +93,7 @@ public function displayField($canEdit = true) {
'display' => false,
'enable_richtext' => $useRichText,
'enable_fileupload' => false,
'uploads' => $this->uploads
]);
echo Html::scriptBlock("$(function() {
pluginFormcreatorInitializeTextarea('$fieldName', '$rand');
Expand Down Expand Up @@ -145,16 +153,18 @@ public function prepareQuestionInputForSave($input) {
return $input;
}

public function hasInput($input) {
return isset($input['formcreator_field_' . $this->question->getID()]);
}

public function parseAnswerValues($input, $nonDestructive = false) {
$input = $this->question->addFiles(
$input,
[
'force_update' => true,
'content_field' => 'formcreator_field_' . $this->question->getID(),
]
);

return parent::parseAnswerValues($input, $nonDestructive);
parent::parseAnswerValues($input, $nonDestructive);
$key = 'formcreator_field_' . $this->question->getID();
if (isset($input['_tag_' . $key]) && isset($input['_' . $key]) && isset($input['_prefix_' . $key])) {
$this->uploads['_' . $key] = $input['_' . $key];
$this->uploads['_prefix_' . $key] = $input['_prefix_' . $key];
$this->uploads['_tag_' . $key] = $input['_tag_' . $key];
}
}

public function getValueForTargetText($richText) {
Expand Down
4 changes: 4 additions & 0 deletions inc/fields/textfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ public function prepareQuestionInputForSave($input) {
return $input;
}

public function hasInput($input) {
return isset($input['formcreator_field_' . $this->question->getID()]);
}

public static function canRequire() {
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions inc/fields/timefield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ public static function getName() {
return __('Time', 'formcreator');
}

public function hasInput($input) {
return isset($input['formcreator_field_' . $this->question->getID()]);
}

public static function canRequire() {
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions inc/fields/urgencyfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ public function prepareQuestionInputForSave($input) {
return $input;
}

public function hasInput($input) {
return isset($input['formcreator_field_' . $this->question->getID()]);
}

public function parseAnswerValues($input, $nonDestructive = false) {
$key = 'formcreator_field_' . $this->question->getID();
if (!isset($input[$key])) {
Expand Down
2 changes: 1 addition & 1 deletion inc/form.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1214,7 +1214,7 @@ class='formcreator_form form_horizontal'>";
if (!$field->isPrerequisites()) {
continue;
}
if (isset($data['formcreator_field_' . $question->getID()])) {
if ($field->hasInput($data)) {
$field->parseAnswerValues($data);
} else {
$field->deserializeValue($question->fields['default_values']);
Expand Down
4 changes: 4 additions & 0 deletions tests/fixture/PluginFormcreatorDependentField.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ public function prepareQuestionInputForSave($input) {
return $input;
}

public function hasInput($input) {
return isset($input['formcreator_field_' . $this->question->getID()]);
}

public function serializeValue() {
if ($this->value === null || $this->value === '') {
return '';
Expand Down

0 comments on commit 488e2d5

Please sign in to comment.