Skip to content

Commit

Permalink
feat(targetticket,targetchange,targetproblem): add documents to targe…
Browse files Browse the repository at this point in the history
…ts before creating them

This should allow notifications for created targets to contain the files
  • Loading branch information
btry committed Mar 30, 2022
1 parent 0130897 commit 39a6cd7
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 6 deletions.
39 changes: 39 additions & 0 deletions inc/abstractitiltarget.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2201,4 +2201,43 @@ public function getDefaultData(PluginFormcreatorFormAnswer $formanswer): array {
$data = array_merge($data, $predefined_fields);
return $data;
}

/**
* Emulate file uploads for documents provided to file questions
*
* @param array $data
* @param PluginFormcreatorFormAnswer $formanswer a form answer
* @return array input $data updated with (fake) file uploads
*/
protected function prepareUploadedFiles(array $data, $formanswer): array {
$saved_documents = $formanswer->getFileProperties();

if ($saved_documents) {
foreach ($formanswer->getFileFields() as $questionId) {
$data["_filename"] = array_merge($data["_filename"], $saved_documents["_filename"][$questionId]);
$data["_tag_filename"] = array_merge($data["_tag_filename"], $saved_documents["_tag_filename"][$questionId]);

foreach ($saved_documents["_filename"][$questionId] as $key => $filename) {
$uploaded_filename = $formanswer->getFileName($questionId, $key);
if ($uploaded_filename != '') {
copy(GLPI_DOC_DIR . '/' . $uploaded_filename, GLPI_TMP_DIR . '/' . $filename);
}
}
}
} else {
foreach ($formanswer->getFileFields() as $questionId) {
$data["_filename"] = array_merge($data["_filename"], $formanswer->input["_formcreator_field_" . $questionId]);
$data["_prefix_filename"] = array_merge($data["_prefix_filename"], $formanswer->input["_prefix_formcreator_field_" . $questionId]);
$data["_tag_filename"] = array_merge($data["_tag_filename"], $formanswer->input["_tag_formcreator_field_" . $questionId]);
foreach ($formanswer->input["_formcreator_field_" . $questionId] as $key => $filename) {
$uploaded_filename = $formanswer->getFileName($questionId, $key);
if ($uploaded_filename != '') {
copy(GLPI_DOC_DIR . '/' . $uploaded_filename, GLPI_TMP_DIR . '/' . $filename);
}
}
}
}

return $data;
}
}
69 changes: 69 additions & 0 deletions inc/formanswer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1993,4 +1993,73 @@ public function updateStatus(int $status): bool {
'items_id' => $this->getID(),
]);
}

/**
* Get the filename of a document for a question of this formanswer
*
* @param int $questionId
* @param int $index
*
* @return string
*/
public function getFileName($questionId, $index): string {
$document = Document::getById($this->questionFields[$questionId]->getDocumentsForTarget()[$index]);
if (!is_object($document)) {
return '';
}

return $document->fields['filepath'];
}

/**
* Get ID of questions of type 'file'
*
* @return int[]
*/
public function getFileFields(): array {
$filefields = [];

$form = $this->getForm();
foreach ($this->getQuestionFields($form) as $questionId => $field) {
$question = $field->getQuestion();
if ($question->fields['fieldtype'] != 'file') {
continue;
}
$filefields[] = $questionId;
}

return $filefields;
}

/**
* get properties of file uploads
*
* @return array
*
*/
public function getFileProperties(): array {
$file_names = [];
$file_tags = [];

foreach ($this->getQuestionFields($this->getForm()->getID()) as $question_id => $field) {
if ($field->getQuestion()->fields['fieldtype'] != 'file') {
continue;
}

foreach ($field->getDocumentsForTarget() as $question_id) {
$document = Document::getById($question_id);
if (!is_object($document)) {
// If the document no longer exists
continue;
}
$file_names[$question_id][] = $document->fields['filename'];
$file_tags[$question_id][] = $document->fields['tag'];
}
}

return [
"_filename" => $file_names,
"_tag_filename" => $file_tags
];
}
}
4 changes: 2 additions & 2 deletions inc/targetchange.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,8 @@ public function save(PluginFormcreatorFormAnswer $formanswer): ?CommonDBTM {
$data = $this->requesters + $this->observers + $this->assigned + $this->assignedSuppliers + $data;
$data = $this->requesterGroups + $this->observerGroups + $this->assignedGroups + $data;

$data = $this->prepareUploadedFiles($data, $formanswer);

// Create the target change
if (!$changeID = $change->add($data)) {
return null;
Expand All @@ -737,8 +739,6 @@ public function save(PluginFormcreatorFormAnswer $formanswer): ?CommonDBTM {
'changes_id' => $changeID,
]);

$this->attachDocument($formanswer->getID(), Change::class, $changeID);

return $change;
}

Expand Down
4 changes: 2 additions & 2 deletions inc/targetproblem.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ public function save(PluginFormcreatorFormAnswer $formanswer): ?CommonDBTM {
$data = $this->requesters + $this->observers + $this->assigned + $this->assignedSuppliers + $data;
$data = $this->requesterGroups + $this->observerGroups + $this->assignedGroups + $data;

$data = $this->prepareUploadedFiles($data, $formanswer);

// Create the target problem
if (!$problemID = $problem->add($data)) {
return null;
Expand All @@ -234,8 +236,6 @@ public function save(PluginFormcreatorFormAnswer $formanswer): ?CommonDBTM {
'problems_id' => $problemID,
]);

$this->attachDocument($formanswer->getID(), Problem::class, $problem);

return $problem;
}

Expand Down
4 changes: 2 additions & 2 deletions inc/targetticket.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,8 @@ public function save(PluginFormcreatorFormAnswer $formanswer): ?CommonDBTM {
$data['_tag_content'][] = $document['tag'];
}

$data = $this->prepareUploadedFiles($data, $formanswer);

// Create the target ticket
$data['_auto_import'] = true;
if (!$ticketID = $ticket->add($data)) {
Expand All @@ -918,8 +920,6 @@ public function save(PluginFormcreatorFormAnswer $formanswer): ?CommonDBTM {
'tickets_id' => $ticketID,
]);

$this->attachDocument($formanswer->getID(), Ticket::class, $ticketID);

// Attach validation message as first ticket followup if validation is required and
// if is set in ticket target configuration
if ($form->validationRequired() && $this->fields['validation_followup']) {
Expand Down

0 comments on commit 39a6cd7

Please sign in to comment.