Skip to content

Commit 39a6cd7

Browse files
committed
feat(targetticket,targetchange,targetproblem): add documents to targets before creating them
This should allow notifications for created targets to contain the files
1 parent 0130897 commit 39a6cd7

File tree

5 files changed

+114
-6
lines changed

5 files changed

+114
-6
lines changed

inc/abstractitiltarget.class.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2201,4 +2201,43 @@ public function getDefaultData(PluginFormcreatorFormAnswer $formanswer): array {
22012201
$data = array_merge($data, $predefined_fields);
22022202
return $data;
22032203
}
2204+
2205+
/**
2206+
* Emulate file uploads for documents provided to file questions
2207+
*
2208+
* @param array $data
2209+
* @param PluginFormcreatorFormAnswer $formanswer a form answer
2210+
* @return array input $data updated with (fake) file uploads
2211+
*/
2212+
protected function prepareUploadedFiles(array $data, $formanswer): array {
2213+
$saved_documents = $formanswer->getFileProperties();
2214+
2215+
if ($saved_documents) {
2216+
foreach ($formanswer->getFileFields() as $questionId) {
2217+
$data["_filename"] = array_merge($data["_filename"], $saved_documents["_filename"][$questionId]);
2218+
$data["_tag_filename"] = array_merge($data["_tag_filename"], $saved_documents["_tag_filename"][$questionId]);
2219+
2220+
foreach ($saved_documents["_filename"][$questionId] as $key => $filename) {
2221+
$uploaded_filename = $formanswer->getFileName($questionId, $key);
2222+
if ($uploaded_filename != '') {
2223+
copy(GLPI_DOC_DIR . '/' . $uploaded_filename, GLPI_TMP_DIR . '/' . $filename);
2224+
}
2225+
}
2226+
}
2227+
} else {
2228+
foreach ($formanswer->getFileFields() as $questionId) {
2229+
$data["_filename"] = array_merge($data["_filename"], $formanswer->input["_formcreator_field_" . $questionId]);
2230+
$data["_prefix_filename"] = array_merge($data["_prefix_filename"], $formanswer->input["_prefix_formcreator_field_" . $questionId]);
2231+
$data["_tag_filename"] = array_merge($data["_tag_filename"], $formanswer->input["_tag_formcreator_field_" . $questionId]);
2232+
foreach ($formanswer->input["_formcreator_field_" . $questionId] as $key => $filename) {
2233+
$uploaded_filename = $formanswer->getFileName($questionId, $key);
2234+
if ($uploaded_filename != '') {
2235+
copy(GLPI_DOC_DIR . '/' . $uploaded_filename, GLPI_TMP_DIR . '/' . $filename);
2236+
}
2237+
}
2238+
}
2239+
}
2240+
2241+
return $data;
2242+
}
22042243
}

inc/formanswer.class.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,4 +1993,73 @@ public function updateStatus(int $status): bool {
19931993
'items_id' => $this->getID(),
19941994
]);
19951995
}
1996+
1997+
/**
1998+
* Get the filename of a document for a question of this formanswer
1999+
*
2000+
* @param int $questionId
2001+
* @param int $index
2002+
*
2003+
* @return string
2004+
*/
2005+
public function getFileName($questionId, $index): string {
2006+
$document = Document::getById($this->questionFields[$questionId]->getDocumentsForTarget()[$index]);
2007+
if (!is_object($document)) {
2008+
return '';
2009+
}
2010+
2011+
return $document->fields['filepath'];
2012+
}
2013+
2014+
/**
2015+
* Get ID of questions of type 'file'
2016+
*
2017+
* @return int[]
2018+
*/
2019+
public function getFileFields(): array {
2020+
$filefields = [];
2021+
2022+
$form = $this->getForm();
2023+
foreach ($this->getQuestionFields($form) as $questionId => $field) {
2024+
$question = $field->getQuestion();
2025+
if ($question->fields['fieldtype'] != 'file') {
2026+
continue;
2027+
}
2028+
$filefields[] = $questionId;
2029+
}
2030+
2031+
return $filefields;
2032+
}
2033+
2034+
/**
2035+
* get properties of file uploads
2036+
*
2037+
* @return array
2038+
*
2039+
*/
2040+
public function getFileProperties(): array {
2041+
$file_names = [];
2042+
$file_tags = [];
2043+
2044+
foreach ($this->getQuestionFields($this->getForm()->getID()) as $question_id => $field) {
2045+
if ($field->getQuestion()->fields['fieldtype'] != 'file') {
2046+
continue;
2047+
}
2048+
2049+
foreach ($field->getDocumentsForTarget() as $question_id) {
2050+
$document = Document::getById($question_id);
2051+
if (!is_object($document)) {
2052+
// If the document no longer exists
2053+
continue;
2054+
}
2055+
$file_names[$question_id][] = $document->fields['filename'];
2056+
$file_tags[$question_id][] = $document->fields['tag'];
2057+
}
2058+
}
2059+
2060+
return [
2061+
"_filename" => $file_names,
2062+
"_tag_filename" => $file_tags
2063+
];
2064+
}
19962065
}

inc/targetchange.class.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,8 @@ public function save(PluginFormcreatorFormAnswer $formanswer): ?CommonDBTM {
722722
$data = $this->requesters + $this->observers + $this->assigned + $this->assignedSuppliers + $data;
723723
$data = $this->requesterGroups + $this->observerGroups + $this->assignedGroups + $data;
724724

725+
$data = $this->prepareUploadedFiles($data, $formanswer);
726+
725727
// Create the target change
726728
if (!$changeID = $change->add($data)) {
727729
return null;
@@ -737,8 +739,6 @@ public function save(PluginFormcreatorFormAnswer $formanswer): ?CommonDBTM {
737739
'changes_id' => $changeID,
738740
]);
739741

740-
$this->attachDocument($formanswer->getID(), Change::class, $changeID);
741-
742742
return $change;
743743
}
744744

inc/targetproblem.class.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ public function save(PluginFormcreatorFormAnswer $formanswer): ?CommonDBTM {
219219
$data = $this->requesters + $this->observers + $this->assigned + $this->assignedSuppliers + $data;
220220
$data = $this->requesterGroups + $this->observerGroups + $this->assignedGroups + $data;
221221

222+
$data = $this->prepareUploadedFiles($data, $formanswer);
223+
222224
// Create the target problem
223225
if (!$problemID = $problem->add($data)) {
224226
return null;
@@ -234,8 +236,6 @@ public function save(PluginFormcreatorFormAnswer $formanswer): ?CommonDBTM {
234236
'problems_id' => $problemID,
235237
]);
236238

237-
$this->attachDocument($formanswer->getID(), Problem::class, $problem);
238-
239239
return $problem;
240240
}
241241

inc/targetticket.class.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,8 @@ public function save(PluginFormcreatorFormAnswer $formanswer): ?CommonDBTM {
902902
$data['_tag_content'][] = $document['tag'];
903903
}
904904

905+
$data = $this->prepareUploadedFiles($data, $formanswer);
906+
905907
// Create the target ticket
906908
$data['_auto_import'] = true;
907909
if (!$ticketID = $ticket->add($data)) {
@@ -918,8 +920,6 @@ public function save(PluginFormcreatorFormAnswer $formanswer): ?CommonDBTM {
918920
'tickets_id' => $ticketID,
919921
]);
920922

921-
$this->attachDocument($formanswer->getID(), Ticket::class, $ticketID);
922-
923923
// Attach validation message as first ticket followup if validation is required and
924924
// if is set in ticket target configuration
925925
if ($form->validationRequired() && $this->fields['validation_followup']) {

0 commit comments

Comments
 (0)