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 30, 2023
1 parent c5e3077 commit 63999d0
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 30 deletions.
23 changes: 23 additions & 0 deletions inc/abstractitiltarget.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2375,6 +2375,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
58 changes: 31 additions & 27 deletions tests/3-unit/GlpiPlugin/Formcreator/Field/DropdownField.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
* ---------------------------------------------------------------------
*/
namespace GlpiPlugin\Formcreator\Field\tests\units;

use GlpiPlugin\Formcreator\Tests\CommonTestCase;
use Computer;
use ITILCategory;
use Location;
class DropdownField extends CommonTestCase {
public function beforeTestMethod($method) {
Expand All @@ -52,14 +55,14 @@ public function providerPrepareQuestionInputForSave() {
[
'input' => [
'name' => $name,
'itemtype' => \Location::class,
'itemtype' => Location::class,
'show_tree_depth' => '5',
'show_tree_root' => '0',
'selectable_tree_root' => '0',
],
'expected' => [
'name' => $name,
'itemtype' => \Location::class,
'itemtype' => Location::class,
'values' => json_encode([
'show_tree_depth' => '5',
'show_tree_root' => '0',
Expand All @@ -71,7 +74,7 @@ public function providerPrepareQuestionInputForSave() {
[
'input' => [
'name' => $name,
'itemtype' => \ITILCategory::class,
'itemtype' => ITILCategory::class,
'show_ticket_categories' => '2',
'show_tree_depth' => '3',
'default_values' => '',
Expand Down Expand Up @@ -112,7 +115,7 @@ public function testisPublicFormCompatible() {

public function testIsPrerequisites() {
$instance = $this->newTestedInstance($this->getQuestion([
'itemtype' => \Computer::class
'itemtype' => Computer::class
]));
$output = $instance->isPrerequisites();
$this->boolean($output)->isEqualTo(true);
Expand All @@ -133,12 +136,13 @@ public function testGetValueForDesign() {
}

public function testGetDocumentsForTarget() {
$instance = $this->newTestedInstance(new \PluginFormcreatorQuestion());
$question = $this->getQuestion();
$instance = $question->getSubField();
$this->array($instance->getDocumentsForTarget())->hasSize(0);
}

public function providerIsValid() {
$location = new \Location();
$location = new Location();
$locationId = $location->import([
'completename' => 'foo',
'entities_id' => $_SESSION['glpiactive_entity']
Expand All @@ -148,13 +152,13 @@ public function providerIsValid() {
[
'question' => $this->getQuestion([
'name' => 'fieldname',
'itemtype' => \Location::class,
'itemtype' => Location::class,
'values' => '',
'required' => '0',
'default_values' => '0',
]),
'input' => [
'dropdown_values' => \Location::class,
'dropdown_values' => Location::class,
'dropdown_default_value' => '0',
'show_tree_depth' => '5',
'show_tree_root' => '0',
Expand All @@ -164,12 +168,12 @@ public function providerIsValid() {
[
'question' => $this->getQuestion([
'name' => 'fieldname',
'itemtype' => \Location::class,
'itemtype' => Location::class,
'values' => '',
'required' => '1',
]),
'input' => [
'dropdown_values' => \Location::class,
'dropdown_values' => Location::class,
'dropdown_default_value' => '0',
'show_tree_depth' => '5',
'show_tree_root' => '0',
Expand All @@ -179,13 +183,13 @@ public function providerIsValid() {
[
'question' => $this->getQuestion([
'name' => 'fieldname',
'itemtype' => \Location::class,
'itemtype' => Location::class,
'values' => '',
'required' => '1',
'default_values' => '',
]),
'input' => [
'dropdown_values' => \Location::class,
'dropdown_values' => Location::class,
'dropdown_default_value' => '42',
'show_tree_depth' => '5',
'show_tree_root' => '0',
Expand All @@ -195,13 +199,13 @@ public function providerIsValid() {
[
'question' => $this->getQuestion([
'name' => 'fieldname',
'itemtype' => \Location::class,
'itemtype' => Location::class,
'values' => '',
'required' => '1',
'default_values' => $locationId,
]),
'input' => [
'dropdown_values' => \Location::class,
'dropdown_values' => Location::class,
'dropdown_default_value' => '42',
'show_tree_depth' => '5',
'show_tree_root' => '0',
Expand All @@ -222,18 +226,18 @@ public function testIsValid($question, $input, $expectedValidity) {
}

public function providerGetValueForTargetText() {
$location = new \Location();
$location = new Location();
$location->add([
'name' => $this->getUniqueString(),
]);
return [
[
'fields' => $this->getQuestion([
'name' => 'fieldname',
'itemtype' => \Location::class,
'itemtype' => Location::class,
'values' => '',
'required' => '1',
'dropdown_values' => \Location::class,
'dropdown_values' => Location::class,
'dropdown_default_value' => '42',
]),
'value' => "",
Expand All @@ -242,10 +246,10 @@ public function providerGetValueForTargetText() {
[
'fields' => $this->getQuestion([
'name' => 'fieldname',
'itemtype' =>\Location::class,
'itemtype' =>Location::class,
'values' =>'',
'required' => '1',
'dropdown_values' => \Location::class,
'dropdown_values' => Location::class,
'dropdown_default_value' => '',
]),
'value' => $location->getID(),
Expand All @@ -266,8 +270,8 @@ public function testGetValueForTargetText($fields, $value, $expected) {
}

public function providerEquals() {
$location1 = new \Location();
$location2 = new \Location();
$location1 = new Location();
$location2 = new Location();
$location1Id = $location1->add([
'name' => $this->getUniqueString()
]);
Expand All @@ -278,15 +282,15 @@ public function providerEquals() {
return [
[
'fields' => $this->getQuestion([
'itemtype' => \Location::class,
'itemtype' => Location::class,
]),
'value' => $location1->fields['completename'],
'answer' => (string) $location1Id,
'expected' => true,
],
[
'fields' => $this->getQuestion([
'itemtype' => \Location::class,
'itemtype' => Location::class,
]),
'value' => $location2->fields['completename'],
'answer' => (string) $location1Id,
Expand All @@ -305,8 +309,8 @@ public function testEquals($fields, $value, $answer, $expected) {
}

public function providerNotEquals() {
$location1 = new \Location();
$location2 = new \Location();
$location1 = new Location();
$location2 = new Location();
$location1Id = $location1->add([
'name' => $this->getUniqueString()
]);
Expand All @@ -317,15 +321,15 @@ public function providerNotEquals() {
return [
[
'fields' => $this->getQuestion([
'itemtype' => \Location::class,
'itemtype' => Location::class,
]),
'value' => $location1->fields['completename'],
'answer' => (string) $location1Id,
'expected' => false,
],
[
'fields' => $this->getQuestion([
'itemtype' => \Location::class,
'itemtype' => Location::class,
]),
'value' => $location2->fields['completename'],
'answer' => (string) $location1Id,
Expand Down

0 comments on commit 63999d0

Please sign in to comment.