Skip to content

Commit 63999d0

Browse files
committed
feat(glpiselectfield): attach existing documents to targets
1 parent c5e3077 commit 63999d0

File tree

5 files changed

+66
-30
lines changed

5 files changed

+66
-30
lines changed

inc/abstractitiltarget.class.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2375,6 +2375,29 @@ protected function prepareUploadsFromTextarea(array $data, PluginFormcreatorForm
23752375
return $data;
23762376
}
23772377

2378+
/**
2379+
* Undocumented function
2380+
*
2381+
* @param array $data
2382+
* @param PluginFormcreatorFormAnswer $formanswer
2383+
* @return array
2384+
*/
2385+
protected function setDocuments($data, PluginFormcreatorFormAnswer $formanswer): array {
2386+
foreach ($formanswer->getQuestionFields($formanswer->getForm()->getID()) ?? [] as $field) {
2387+
$question = $field->getQuestion();
2388+
if ($question->fields['fieldtype'] !== 'glpiselect') {
2389+
continue;
2390+
}
2391+
if ($question->fields['itemtype'] !== Document::class) {
2392+
continue;
2393+
}
2394+
2395+
$data['_documents_id'][] = $field->getRawValue();
2396+
}
2397+
2398+
return $data;
2399+
}
2400+
23782401
/**
23792402
* Emulate file uploads for documents provided to file questions
23802403
*

inc/field/dropdownfield.class.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
use OLA;
6161
use QueryExpression;
6262
use QuerySubQuery;
63-
use QueryUnion;
6463
use GlpiPlugin\Formcreator\Exception\ComparisonException;
6564
use Glpi\Application\View\TemplateRenderer;
6665
use Plugin;
@@ -366,6 +365,10 @@ public function getRenderedHtml($domain, $canEdit = true): string {
366365
case User::class:
367366
$value = (new DbUtils())->getUserName($item->getID());
368367
break;
368+
case Document::class:
369+
/** @var Document $item */
370+
$value = $item->getDownloadLink($this->form_answer);
371+
break;
369372
}
370373
}
371374
}
@@ -427,7 +430,12 @@ public function moveUploads() {
427430
}
428431

429432
public function getDocumentsForTarget(): array {
430-
return [];
433+
$itemtype = $this->getSubItemtype();
434+
if ($itemtype !== Document::class) {
435+
return [];
436+
}
437+
438+
return [$this->value]; // Array of a single document ID
431439
}
432440

433441
public static function getName(): string {

inc/formanswer.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1720,7 +1720,7 @@ private function updateIssue() {
17201720
/**
17211721
* get all fields from a form
17221722
*
1723-
* @param int $formId ID of the form where come the fileds to load
1723+
* @param int $formId ID of the form where come the fields to load
17241724
* @return PluginFormcreatorAbstractField[]
17251725
*/
17261726
public function getQuestionFields($formId) : array {

inc/targetticket.class.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,7 @@ public function save(PluginFormcreatorFormAnswer $formanswer): ?CommonDBTM {
893893
$data = $this->assignedGroups + $data;
894894
}
895895

896+
$data = $this->setDocuments($data, $formanswer);
896897
$data = $this->prepareUploadedFiles($data, $formanswer);
897898

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

tests/3-unit/GlpiPlugin/Formcreator/Field/DropdownField.php

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
* ---------------------------------------------------------------------
3030
*/
3131
namespace GlpiPlugin\Formcreator\Field\tests\units;
32+
3233
use GlpiPlugin\Formcreator\Tests\CommonTestCase;
34+
use Computer;
35+
use ITILCategory;
3336
use Location;
3437
class DropdownField extends CommonTestCase {
3538
public function beforeTestMethod($method) {
@@ -52,14 +55,14 @@ public function providerPrepareQuestionInputForSave() {
5255
[
5356
'input' => [
5457
'name' => $name,
55-
'itemtype' => \Location::class,
58+
'itemtype' => Location::class,
5659
'show_tree_depth' => '5',
5760
'show_tree_root' => '0',
5861
'selectable_tree_root' => '0',
5962
],
6063
'expected' => [
6164
'name' => $name,
62-
'itemtype' => \Location::class,
65+
'itemtype' => Location::class,
6366
'values' => json_encode([
6467
'show_tree_depth' => '5',
6568
'show_tree_root' => '0',
@@ -71,7 +74,7 @@ public function providerPrepareQuestionInputForSave() {
7174
[
7275
'input' => [
7376
'name' => $name,
74-
'itemtype' => \ITILCategory::class,
77+
'itemtype' => ITILCategory::class,
7578
'show_ticket_categories' => '2',
7679
'show_tree_depth' => '3',
7780
'default_values' => '',
@@ -112,7 +115,7 @@ public function testisPublicFormCompatible() {
112115

113116
public function testIsPrerequisites() {
114117
$instance = $this->newTestedInstance($this->getQuestion([
115-
'itemtype' => \Computer::class
118+
'itemtype' => Computer::class
116119
]));
117120
$output = $instance->isPrerequisites();
118121
$this->boolean($output)->isEqualTo(true);
@@ -133,12 +136,13 @@ public function testGetValueForDesign() {
133136
}
134137

135138
public function testGetDocumentsForTarget() {
136-
$instance = $this->newTestedInstance(new \PluginFormcreatorQuestion());
139+
$question = $this->getQuestion();
140+
$instance = $question->getSubField();
137141
$this->array($instance->getDocumentsForTarget())->hasSize(0);
138142
}
139143

140144
public function providerIsValid() {
141-
$location = new \Location();
145+
$location = new Location();
142146
$locationId = $location->import([
143147
'completename' => 'foo',
144148
'entities_id' => $_SESSION['glpiactive_entity']
@@ -148,13 +152,13 @@ public function providerIsValid() {
148152
[
149153
'question' => $this->getQuestion([
150154
'name' => 'fieldname',
151-
'itemtype' => \Location::class,
155+
'itemtype' => Location::class,
152156
'values' => '',
153157
'required' => '0',
154158
'default_values' => '0',
155159
]),
156160
'input' => [
157-
'dropdown_values' => \Location::class,
161+
'dropdown_values' => Location::class,
158162
'dropdown_default_value' => '0',
159163
'show_tree_depth' => '5',
160164
'show_tree_root' => '0',
@@ -164,12 +168,12 @@ public function providerIsValid() {
164168
[
165169
'question' => $this->getQuestion([
166170
'name' => 'fieldname',
167-
'itemtype' => \Location::class,
171+
'itemtype' => Location::class,
168172
'values' => '',
169173
'required' => '1',
170174
]),
171175
'input' => [
172-
'dropdown_values' => \Location::class,
176+
'dropdown_values' => Location::class,
173177
'dropdown_default_value' => '0',
174178
'show_tree_depth' => '5',
175179
'show_tree_root' => '0',
@@ -179,13 +183,13 @@ public function providerIsValid() {
179183
[
180184
'question' => $this->getQuestion([
181185
'name' => 'fieldname',
182-
'itemtype' => \Location::class,
186+
'itemtype' => Location::class,
183187
'values' => '',
184188
'required' => '1',
185189
'default_values' => '',
186190
]),
187191
'input' => [
188-
'dropdown_values' => \Location::class,
192+
'dropdown_values' => Location::class,
189193
'dropdown_default_value' => '42',
190194
'show_tree_depth' => '5',
191195
'show_tree_root' => '0',
@@ -195,13 +199,13 @@ public function providerIsValid() {
195199
[
196200
'question' => $this->getQuestion([
197201
'name' => 'fieldname',
198-
'itemtype' => \Location::class,
202+
'itemtype' => Location::class,
199203
'values' => '',
200204
'required' => '1',
201205
'default_values' => $locationId,
202206
]),
203207
'input' => [
204-
'dropdown_values' => \Location::class,
208+
'dropdown_values' => Location::class,
205209
'dropdown_default_value' => '42',
206210
'show_tree_depth' => '5',
207211
'show_tree_root' => '0',
@@ -222,18 +226,18 @@ public function testIsValid($question, $input, $expectedValidity) {
222226
}
223227

224228
public function providerGetValueForTargetText() {
225-
$location = new \Location();
229+
$location = new Location();
226230
$location->add([
227231
'name' => $this->getUniqueString(),
228232
]);
229233
return [
230234
[
231235
'fields' => $this->getQuestion([
232236
'name' => 'fieldname',
233-
'itemtype' => \Location::class,
237+
'itemtype' => Location::class,
234238
'values' => '',
235239
'required' => '1',
236-
'dropdown_values' => \Location::class,
240+
'dropdown_values' => Location::class,
237241
'dropdown_default_value' => '42',
238242
]),
239243
'value' => "",
@@ -242,10 +246,10 @@ public function providerGetValueForTargetText() {
242246
[
243247
'fields' => $this->getQuestion([
244248
'name' => 'fieldname',
245-
'itemtype' =>\Location::class,
249+
'itemtype' =>Location::class,
246250
'values' =>'',
247251
'required' => '1',
248-
'dropdown_values' => \Location::class,
252+
'dropdown_values' => Location::class,
249253
'dropdown_default_value' => '',
250254
]),
251255
'value' => $location->getID(),
@@ -266,8 +270,8 @@ public function testGetValueForTargetText($fields, $value, $expected) {
266270
}
267271

268272
public function providerEquals() {
269-
$location1 = new \Location();
270-
$location2 = new \Location();
273+
$location1 = new Location();
274+
$location2 = new Location();
271275
$location1Id = $location1->add([
272276
'name' => $this->getUniqueString()
273277
]);
@@ -278,15 +282,15 @@ public function providerEquals() {
278282
return [
279283
[
280284
'fields' => $this->getQuestion([
281-
'itemtype' => \Location::class,
285+
'itemtype' => Location::class,
282286
]),
283287
'value' => $location1->fields['completename'],
284288
'answer' => (string) $location1Id,
285289
'expected' => true,
286290
],
287291
[
288292
'fields' => $this->getQuestion([
289-
'itemtype' => \Location::class,
293+
'itemtype' => Location::class,
290294
]),
291295
'value' => $location2->fields['completename'],
292296
'answer' => (string) $location1Id,
@@ -305,8 +309,8 @@ public function testEquals($fields, $value, $answer, $expected) {
305309
}
306310

307311
public function providerNotEquals() {
308-
$location1 = new \Location();
309-
$location2 = new \Location();
312+
$location1 = new Location();
313+
$location2 = new Location();
310314
$location1Id = $location1->add([
311315
'name' => $this->getUniqueString()
312316
]);
@@ -317,15 +321,15 @@ public function providerNotEquals() {
317321
return [
318322
[
319323
'fields' => $this->getQuestion([
320-
'itemtype' => \Location::class,
324+
'itemtype' => Location::class,
321325
]),
322326
'value' => $location1->fields['completename'],
323327
'answer' => (string) $location1Id,
324328
'expected' => false,
325329
],
326330
[
327331
'fields' => $this->getQuestion([
328-
'itemtype' => \Location::class,
332+
'itemtype' => Location::class,
329333
]),
330334
'value' => $location2->fields['completename'],
331335
'answer' => (string) $location1Id,

0 commit comments

Comments
 (0)