diff --git a/inc/fieldinterface.class.php b/inc/fieldinterface.class.php index 418117933..6d0d6f155 100644 --- a/inc/fieldinterface.class.php +++ b/inc/fieldinterface.class.php @@ -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 diff --git a/inc/fields/actorfield.class.php b/inc/fields/actorfield.class.php index c901d5064..7748fd61f 100644 --- a/inc/fields/actorfield.class.php +++ b/inc/fields/actorfield.class.php @@ -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])) { diff --git a/inc/fields/checkboxesfield.class.php b/inc/fields/checkboxesfield.class.php index 36122d9d1..9902ef19d 100644 --- a/inc/fields/checkboxesfield.class.php +++ b/inc/fields/checkboxesfield.class.php @@ -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(); diff --git a/inc/fields/datefield.class.php b/inc/fields/datefield.class.php index eea213244..c8f0194af 100644 --- a/inc/fields/datefield.class.php +++ b/inc/fields/datefield.class.php @@ -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 []; } diff --git a/inc/fields/datetimefield.class.php b/inc/fields/datetimefield.class.php index 7558357b1..2d74580fe 100644 --- a/inc/fields/datetimefield.class.php +++ b/inc/fields/datetimefield.class.php @@ -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)); } diff --git a/inc/fields/descriptionfield.class.php b/inc/fields/descriptionfield.class.php index 49ccd104f..80ccd6ebc 100644 --- a/inc/fields/descriptionfield.class.php +++ b/inc/fields/descriptionfield.class.php @@ -97,6 +97,10 @@ public function prepareQuestionInputForSave($input) { return $input; } + public function hasInput($input) { + return false; + } + public static function canRequire() { return false; } diff --git a/inc/fields/dropdownfield.class.php b/inc/fields/dropdownfield.class.php index 94f68621f..2587ac466 100644 --- a/inc/fields/dropdownfield.class.php +++ b/inc/fields/dropdownfield.class.php @@ -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; } diff --git a/inc/fields/emailfield.class.php b/inc/fields/emailfield.class.php index aff41ed59..098515cca 100644 --- a/inc/fields/emailfield.class.php +++ b/inc/fields/emailfield.class.php @@ -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])) { diff --git a/inc/fields/filefield.class.php b/inc/fields/filefield.class.php index 262ad6643..9008781bb 100644 --- a/inc/fields/filefield.class.php +++ b/inc/fields/filefield.class.php @@ -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; } @@ -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(); @@ -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 @@ -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 = []; diff --git a/inc/fields/floatfield.class.php b/inc/fields/floatfield.class.php index 6487b0788..8a5ae87fc 100644 --- a/inc/fields/floatfield.class.php +++ b/inc/fields/floatfield.class.php @@ -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); @@ -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])) { diff --git a/inc/fields/hiddenfield.class.php b/inc/fields/hiddenfield.class.php index 51fe1eb7f..870d999f6 100644 --- a/inc/fields/hiddenfield.class.php +++ b/inc/fields/hiddenfield.class.php @@ -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 []; } diff --git a/inc/fields/hostnamefield.class.php b/inc/fields/hostnamefield.class.php index ab6a07047..d903cbc5b 100644 --- a/inc/fields/hostnamefield.class.php +++ b/inc/fields/hostnamefield.class.php @@ -50,6 +50,10 @@ public function getValueForTargetText($richText) { return Toolbox::addslashes_deep($this->value); } + public function hasInput($input) { + return false; + } + public function getDocumentsForTarget() { return []; } diff --git a/inc/fields/integerfield.class.php b/inc/fields/integerfield.class.php index 47560c210..b22b0f330 100644 --- a/inc/fields/integerfield.class.php +++ b/inc/fields/integerfield.class.php @@ -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; } diff --git a/inc/fields/ipfield.class.php b/inc/fields/ipfield.class.php index e0d2d95a4..f1268b947 100644 --- a/inc/fields/ipfield.class.php +++ b/inc/fields/ipfield.class.php @@ -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; } diff --git a/inc/fields/ldapselectfield.class.php b/inc/fields/ldapselectfield.class.php index 6285050c9..d805f3fbe 100644 --- a/inc/fields/ldapselectfield.class.php +++ b/inc/fields/ldapselectfield.class.php @@ -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; } diff --git a/inc/fields/multiselectfield.class.php b/inc/fields/multiselectfield.class.php index 373d346d5..7cd7d9e38 100644 --- a/inc/fields/multiselectfield.class.php +++ b/inc/fields/multiselectfield.class.php @@ -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 = []; diff --git a/inc/fields/radiosfield.class.php b/inc/fields/radiosfield.class.php index 6c353bba4..e9a8c9dcb 100644 --- a/inc/fields/radiosfield.class.php +++ b/inc/fields/radiosfield.class.php @@ -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])) { diff --git a/inc/fields/tagfield.class.php b/inc/fields/tagfield.class.php index f3269799a..97cb41681 100644 --- a/inc/fields/tagfield.class.php +++ b/inc/fields/tagfield.class.php @@ -49,7 +49,7 @@ public function getDesignSpecializationField() { 'may_be_required' => true, ]; } - + public function displayField($canEdit = true) { global $DB; @@ -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])) { diff --git a/inc/fields/textareafield.class.php b/inc/fields/textareafield.class.php index 6fcb9c84a..f5623428c 100644 --- a/inc/fields/textareafield.class.php +++ b/inc/fields/textareafield.class.php @@ -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(); @@ -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'); @@ -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) { diff --git a/inc/fields/textfield.class.php b/inc/fields/textfield.class.php index 7c728ebde..9672c6819 100644 --- a/inc/fields/textfield.class.php +++ b/inc/fields/textfield.class.php @@ -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; } diff --git a/inc/fields/timefield.class.php b/inc/fields/timefield.class.php index 903bd9ca9..d3327bdea 100644 --- a/inc/fields/timefield.class.php +++ b/inc/fields/timefield.class.php @@ -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; } diff --git a/inc/fields/urgencyfield.class.php b/inc/fields/urgencyfield.class.php index b0def2218..17a754987 100644 --- a/inc/fields/urgencyfield.class.php +++ b/inc/fields/urgencyfield.class.php @@ -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])) { diff --git a/inc/form.class.php b/inc/form.class.php index 758c9bea9..f7492b5d6 100644 --- a/inc/form.class.php +++ b/inc/form.class.php @@ -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']); diff --git a/tests/fixture/PluginFormcreatorDependentField.php b/tests/fixture/PluginFormcreatorDependentField.php index a30c9127f..b5c9061ba 100644 --- a/tests/fixture/PluginFormcreatorDependentField.php +++ b/tests/fixture/PluginFormcreatorDependentField.php @@ -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 '';