Skip to content

Commit

Permalink
feat(file): multiple file upload for a single file field
Browse files Browse the repository at this point in the history
  • Loading branch information
LEANDRO Barbosa Teles authored and btry committed Aug 29, 2018
1 parent d0a56e5 commit 30e4057
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 36 deletions.
12 changes: 9 additions & 3 deletions inc/fields/filefield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
}
Expand All @@ -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;
Expand Down
63 changes: 38 additions & 25 deletions inc/form_answer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
16 changes: 9 additions & 7 deletions inc/targetbase.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<br />' . implode('<br />', $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);
}
Expand Down
26 changes: 25 additions & 1 deletion install/update_2.6_2.7.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'");
}
}

// 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'");
}
}
}

0 comments on commit 30e4057

Please sign in to comment.