Skip to content

Commit

Permalink
feat(glpiselectfield): attach existing documents to targets
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed Oct 23, 2023
1 parent c5e3077 commit 8142b21
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
24 changes: 24 additions & 0 deletions inc/abstractitiltarget.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* ---------------------------------------------------------------------
*/

use Document;
use Glpi\Toolbox\Sanitizer;
use GlpiPlugin\Formcreator\Field\TextareaField;

Expand Down Expand Up @@ -2375,6 +2376,29 @@ protected function prepareUploadsFromTextarea(array $data, PluginFormcreatorForm
return $data;
}

/**
* Undocumented function
*
* @param array $data
* @param PluginFormcreatorFormAnswer $formanswer
* @return array
*/
protected function setDocuments($data, PluginFormcreatorFormAnswer $formanswer): array {
foreach ($formanswer->getQuestionFields($formanswer->getForm()->getID()) ?? [] as $field) {
$question = $field->getQuestion();
if ($question->fields['fieldtype'] !== 'glpiselect') {
continue;
}
if ($question->fields['itemtype'] !== Document::class) {
continue;
}

$data['_documents_id'][] = $field->getRawValue();
}

return $data;
}

/**
* Emulate file uploads for documents provided to file questions
*
Expand Down
12 changes: 10 additions & 2 deletions inc/field/dropdownfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
use OLA;
use QueryExpression;
use QuerySubQuery;
use QueryUnion;
use GlpiPlugin\Formcreator\Exception\ComparisonException;
use Glpi\Application\View\TemplateRenderer;
use Plugin;
Expand Down Expand Up @@ -366,6 +365,10 @@ public function getRenderedHtml($domain, $canEdit = true): string {
case User::class:
$value = (new DbUtils())->getUserName($item->getID());
break;
case Document::class:
/** @var Document $item */
$value = $item->getDownloadLink($this->form_answer);
break;
}
}
}
Expand Down Expand Up @@ -427,7 +430,12 @@ public function moveUploads() {
}

public function getDocumentsForTarget(): array {
return [];
$itemtype = $this->getSubItemtype();
if ($itemtype !== Document::class) {
return [];
}

return [$this->value]; // Array of a single document ID
}

public static function getName(): string {
Expand Down
2 changes: 1 addition & 1 deletion inc/formanswer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1720,7 +1720,7 @@ private function updateIssue() {
/**
* get all fields from a form
*
* @param int $formId ID of the form where come the fileds to load
* @param int $formId ID of the form where come the fields to load
* @return PluginFormcreatorAbstractField[]
*/
public function getQuestionFields($formId) : array {
Expand Down
1 change: 1 addition & 0 deletions inc/targetticket.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,7 @@ public function save(PluginFormcreatorFormAnswer $formanswer): ?CommonDBTM {
$data = $this->assignedGroups + $data;
}

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

$data = $this->appendFieldsData($data, $formanswer);
Expand Down

0 comments on commit 8142b21

Please sign in to comment.