From 30e405724afd5e8075fcf74b4c2d87009c965f58 Mon Sep 17 00:00:00 2001
From: LEANDRO Barbosa Teles <01075947146@poupex.com.br>
Date: Wed, 21 Mar 2018 14:49:31 -0300
Subject: [PATCH] feat(file): multiple file upload for a single file field
---
inc/fields/filefield.class.php | 12 +++++--
inc/form_answer.class.php | 63 ++++++++++++++++++++--------------
inc/targetbase.class.php | 16 +++++----
install/update_2.6_2.7.php | 26 +++++++++++++-
4 files changed, 81 insertions(+), 36 deletions(-)
diff --git a/inc/fields/filefield.class.php b/inc/fields/filefield.class.php
index ea0af00fb..31cab6a58 100644
--- a/inc/fields/filefield.class.php
+++ b/inc/fields/filefield.class.php
@@ -43,13 +43,19 @@ public function displayField($canEdit = true) {
echo Html::file([
'name' => 'formcreator_field_' . $this->fields['id'],
'display' => false,
+ 'multiple' => 'multiple',
]);
} else {
$doc = new Document();
$answer = $this->getAnswer();
- if ($doc->getFromDB($answer)) {
- echo $doc->getDownloadLink();
+ if (is_numeric($answer)) {
+ $answer = [$answer];
+ }
+ foreach ($answer as $item) {
+ if ($doc->getFromDB($item)) {
+ echo $doc->getDownloadLink();
+ }
}
}
}
@@ -62,7 +68,7 @@ public function isValid($value) {
}
if (is_array($_POST['_formcreator_field_' . $this->fields['id']])
- && count($_POST['_formcreator_field_' . $this->fields['id']]) === 1) {
+ && count($_POST['_formcreator_field_' . $this->fields['id']]) > 0) {
$file = current($_POST['_formcreator_field_' . $this->fields['id']]);
if (is_file(GLPI_TMP_DIR . '/' . $file)) {
return true;
diff --git a/inc/form_answer.class.php b/inc/form_answer.class.php
index 405da2481..47823ffa3 100644
--- a/inc/form_answer.class.php
+++ b/inc/form_answer.class.php
@@ -654,19 +654,24 @@ public function saveAnswers($data) {
// $answer_value may be still null if the field type is file and no file was uploaded
if ($answer_value !== null) {
- // Update the answer to the question
- $questionId = $question->getID();
- $answer = new PluginFormcreatorAnswer();
- $answer->getFromDBByCrit([
- 'AND' => [
- 'plugin_formcreator_forms_answers_id' => $formanswers_id,
- 'plugin_formcreator_questions_id' => $questionId
- ]
- ]);
- $answer->update([
- 'id' => $answer->getID(),
- 'answer' => $answer_value,
- ], 0);
+ if (!is_array(answer_value)) {
+ $answer_value = [$answer_value];
+ }
+ foreach ($answer_value as $value) {
+ // Update the answer to the question
+ $questionId = $question->getID();
+ $answer = new PluginFormcreatorAnswer();
+ $answer->getFromDBByCrit([
+ 'AND' => [
+ 'plugin_formcreator_forms_answers_id' => $formanswers_id,
+ 'plugin_formcreator_questions_id' => $questionId
+ ]
+ ]);
+ $answer->update([
+ 'id' => $answer->getID(),
+ 'answer' => $value,
+ ], 0);
+ }
}
}
}
@@ -718,12 +723,17 @@ public function saveAnswers($data) {
$answer_value = $this->transformAnswerValue($question, $data['formcreator_field_' . $question->getID()]);
if ($answer_value !== null) {
- // Save the answer to the question
- $answer->add([
- 'plugin_formcreator_forms_answers_id' => $id,
- 'plugin_formcreator_questions_id' => $question->getID(),
- 'answer' => $answer_value,
- ], [], 0);
+ if (!is_array($answer_value)) {
+ $answer_value = [$answer_value];
+ }
+ foreach ($answer_value as $val) {
+ // Save the answer to the question
+ $answer->add([
+ 'plugin_formcreator_forms_answers_id' => $id,
+ 'plugin_formcreator_questions_id' => $question->getID(),
+ 'answer' => $val,
+ ], [], 0);
+ }
}
}
$is_newFormAnswer = true;
@@ -921,12 +931,15 @@ private function transformAnswerValue(PluginFormcreatorQuestion $question, $valu
} else {
$answer_value = '';
}
- } else if (is_array($_POST['_formcreator_field_' . $question->getID()])
- && count($_POST['_formcreator_field_' . $question->getID()]) === 1) {
- $file = current($_POST['_formcreator_field_' . $question->getID()]);
- if (is_file(GLPI_TMP_DIR . '/' . $file)) {
- $answer_value = $this->saveDocument($form, $question, $file);
+ } else if (isset($_POST['_formcreator_field_' . $question->getID()])) {
+ $documents = $_POST['_formcreator_field_' . $question->getID()];
+ $answer_value = [];
+ foreach ($documents as $document) {
+ if (is_file(GLPI_TMP_DIR . '/' . $document)) {
+ $answer_value[] = $this->saveDocument($form, $question, $document);
+ }
}
+ $answer_value = json_encode($answer_value);
}
return $answer_value;
@@ -957,7 +970,7 @@ private function saveDocument(PluginFormcreatorForm $form, PluginFormcreatorQues
if ($docID = $doc->add($file_data)) {
$docID = intval($docID);
$table = Document::getTable();
- $filename = addslashes($file);
+ $filename = substr(addslashes($file), 23);
$query = "UPDATE `$table` SET `filename` = '$filename'
WHERE `id` = '$docID'";
$DB->query($query);
diff --git a/inc/targetbase.class.php b/inc/targetbase.class.php
index 147b4092a..5d232ee92 100644
--- a/inc/targetbase.class.php
+++ b/inc/targetbase.class.php
@@ -809,25 +809,27 @@ protected function parseTags($content, PluginFormcreatorForm_Answer $formanswer,
$name = $question_line['name'];
$value = $fieldObject->prepareQuestionInputForTarget($fieldObject->getValue());
}
- if (is_array($value)) {
+
+ if ($question_line['fieldtype'] !== 'file') {
if (version_compare(PluginFormcreatorCommon::getGlpiVersion(), 9.4) >= 0 || $CFG_GLPI['use_rich_text']) {
$value = '
' . implode('
', $value);
} else {
$value = "\r\n" . implode("\r\n", $value);
}
- }
-
- if ($question_line['fieldtype'] !== 'file') {
$content = str_replace('##question_' . $id . '##', addslashes($name), $content);
$content = str_replace('##answer_' . $id . '##', $value, $content);
} else {
if (strpos($content, '##answer_' . $id . '##') !== false) {
$content = str_replace('##question_' . $id . '##', addslashes($name), $content);
- if ($value !== '') {
+ if (!is_array($value)) {
+ $value = [$value];
+ }
+ if (count($value)) {
$content = str_replace('##answer_' . $id . '##', __('Attached document', 'formcreator'), $content);
-
// keep the ID of the document
- $this->attachedDocuments[$value] = true;
+ foreach ($value as $documentId) {
+ $this->attachedDocuments[$documentId] = true;
+ }
} else {
$content = str_replace('##answer_' . $id . '##', '', $content);
}
diff --git a/install/update_2.6_2.7.php b/install/update_2.6_2.7.php
index 353e2cd56..a558f96a8 100644
--- a/install/update_2.6_2.7.php
+++ b/install/update_2.6_2.7.php
@@ -108,4 +108,28 @@ function plugin_formcreator_update_2_7(Migration $migration) {
$values = json_encode($values);
$DB->query("UPDATE `glpi_plugin_formcreator_questions` SET `values`='$values' WHERE `id` = '$id'");
}
-}
\ No newline at end of file
+
+ // multiple files upload per field
+ $request = [
+ 'SELECT' => 'glpi_plugin_formcreator_answers.*',
+ 'FROM' => 'glpi_plugin_formcreator_answers',
+ 'LEFT JOIN' => [
+ 'glpi_plugin_formcreator_questions' => [
+ 'FKEY' => [
+ 'glpi_plugin_formcreator_questions' => 'id',
+ 'glpi_plugin_formcreator_answers' => 'plugin_formcreator_questions_id'
+ ]
+ ]
+ ],
+ 'WHERE' => [
+ 'fieldtype' => 'file',
+ ]
+ ];
+ foreach ($DB->request($request) as $row) {
+ if (!is_array(json_decode($row['answer']))) {
+ $id = $row['id'];
+ $answer = json_encode([$row['answer']]);
+ $DB->query("UPDATE `glpi_plugin_formcreator_answers` SET `answer` = '$answer' WHERE `id` = '$id'");
+ }
+ }
+}