Skip to content

Commit 152ba27

Browse files
committed
feat(filefield): allow access to document from self service
1 parent 044bf3a commit 152ba27

9 files changed

+60
-28
lines changed

inc/abstractfield.class.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,30 @@ abstract class PluginFormcreatorAbstractField implements PluginFormcreatorFieldI
4444
/** @var mixed $answer Value of the field */
4545
protected $value = null;
4646

47+
/**
48+
* the form answer to source the values from
49+
*
50+
* @var PluginFormcreatorFormAnswer|null
51+
*/
52+
protected ?PluginFormcreatorFormAnswer $form_answer = null;
53+
4754
/**
4855
*
4956
* @param array $question PluginFormcreatorQuestion instance
5057
*/
5158
public function __construct(PluginFormcreatorQuestion $question) {
52-
$this->question = $question;
59+
$this->question = $question;
60+
}
61+
62+
public function setFormAnswer(PluginFormcreatorFormAnswer $form_answer): void {
63+
$this->form_answer = $form_answer;
64+
if ($this->hasInput($this->form_answer->getAnswers())) {
65+
// Parse an HTML input
66+
$this->parseAnswerValues($this->form_answer->getAnswers());
67+
} else {
68+
// Deserialize the default value from DB
69+
$this->deserializeValue($this->fields['default_values']);
70+
}
5371
}
5472

5573
public function prepareQuestionInputForSave($input) {
@@ -66,7 +84,7 @@ public function getRawValue() {
6684
* @param string $domain Translation domain of the form
6785
* @param boolean $canEdit is the field editable ?
6886
*/
69-
public function show($domain, $canEdit = true) {
87+
public function show(string $domain, bool $canEdit = true): string {
7088
$html = '';
7189

7290
if ($this->isVisibleField()) {

inc/field/filefield.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function getRenderedHtml($domain, $canEdit = true): string {
8181
}
8282
foreach ($answer as $item) {
8383
if (is_numeric($item) && $doc->getFromDB($item)) {
84-
$html .= $doc->getDownloadLink();
84+
$html .= $doc->getDownloadLink($this->form_answer);
8585
}
8686
}
8787
return $html;

inc/field/hiddenfield.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function showForm(array $options): void {
5454
]);
5555
}
5656

57-
public function show($domain, $canEdit = true) {
57+
public function show(string $domain, bool $canEdit = true): string {
5858
if (!$canEdit) {
5959
return '';
6060
}

inc/field/hostnamefield.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function prepareQuestionInputForSave($input) {
5858
return $input;
5959
}
6060

61-
public function show($domain, $canEdit = true) {
61+
public function show(string $domain, bool $canEdit = true): string {
6262
if (!$canEdit) {
6363
return parent::show($canEdit);
6464
}

inc/field/ipfield.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function prepareQuestionInputForSave($input) {
5959
return $input;
6060
}
6161

62-
public function show($domain, $canEdit = true) {
62+
public function show(string $domain, bool $canEdit = true): string {
6363
if (!$canEdit) {
6464
return '';
6565
}

inc/fieldinterface.class.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,5 +302,13 @@ public function getQuestion();
302302
* @param boolean $canEdit true if the field is editable
303303
* @return string HTML of the field
304304
*/
305-
public function show(string $domain, bool $canEdit): string;
305+
public function show(string $domain, bool $canEdit = true): string;
306+
307+
/**
308+
* Set the form answer containing the value of the field
309+
*
310+
* @param PluginFormcreatorFormAnswer $form_answer
311+
* @return void
312+
*/
313+
public function setFormAnswer(PluginFormcreatorFormAnswer $form_answer): void;
306314
}

inc/formanswer.class.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ class PluginFormcreatorFormAnswer extends CommonDBTM
6666
/** @var PluginFormcreatorForm $form The form attached to the object */
6767
private $form = null;
6868

69+
/** @var array $answers set od answers */
70+
private array $answers = [];
71+
6972
public static function getStatuses() {
7073
return [
7174
self::STATUS_WAITING => __('Waiting', 'formcreator'),
@@ -554,9 +557,9 @@ public function showForm($ID, $options = []) {
554557
$domain = PluginFormcreatorForm::getTranslationDomain($_SESSION['glpilanguage'], $form->getID());
555558

556559
// Get fields populated with answers
557-
$answers = $this->getAnswers($this->getID());
558-
$answers['plugin_formcreator_forms_id'] = $form->getID();
559-
$visibility = PluginFormcreatorFields::updateVisibility($answers);
560+
$this->loadAnswers();
561+
$this->answers['plugin_formcreator_forms_id'] = $form->getID();
562+
$visibility = PluginFormcreatorFields::updateVisibility($this->answers);
560563

561564
$sections = (new PluginFormcreatorSection)->getSectionsFromForm($form->getID());
562565
foreach ($sections as $section) {
@@ -596,7 +599,7 @@ public function showForm($ID, $options = []) {
596599
}
597600
}
598601
}
599-
echo $question->getRenderedHtml($domain, $editMode, $answers, $visibility[$question->getType()][$question->getID()]);
602+
echo $question->getRenderedHtml($domain, $editMode, $this, $visibility[$question->getType()][$question->getID()]);
600603
$lastQuestion = $question;
601604
}
602605
echo '</div>';
@@ -878,24 +881,32 @@ public function generateTarget(): bool {
878881
/**
879882
* Gets answers of all fields of a form answer
880883
*
881-
* @param int $formAnswerId
882-
* @return array
884+
* @return void
883885
*/
884-
public function getAnswers($formAnswerId): array {
886+
public function loadAnswers(): void {
885887
global $DB;
886888

889+
$this->answers = [];
890+
if ($this->isNewItem()) {
891+
return;
892+
}
893+
887894
$answers = $DB->request([
888895
'SELECT' => ['plugin_formcreator_questions_id', 'answer'],
889896
'FROM' => PluginFormcreatorAnswer::getTable(),
890897
'WHERE' => [
891-
'plugin_formcreator_formanswers_id' => $formAnswerId
898+
'plugin_formcreator_formanswers_id' => $this->getID()
892899
]
893900
]);
894901
$answers_values = [];
895902
foreach ($answers as $found_answer) {
896903
$answers_values['formcreator_field_' . $found_answer['plugin_formcreator_questions_id']] = $found_answer['answer'];
897904
}
898-
return $answers_values;
905+
$this->answers = $answers_values;
906+
}
907+
908+
public function getAnswers() {
909+
return $this->answers;
899910
}
900911

901912
/**
@@ -1650,7 +1661,8 @@ public function deserializeAnswers() : bool {
16501661
return false;
16511662
}
16521663

1653-
$answers_values = $this->getAnswers($this->getID());
1664+
$this->loadAnswers();
1665+
$answers_values = $this->getAnswers();
16541666
foreach (array_keys($this->questionFields) as $id) {
16551667
if (!$this->questionFields[$id]->hasInput($answers_values)) {
16561668
continue;

inc/question.class.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,12 @@ public function getDesignHtml() : string {
259259
* Get the HTML to display the question for a requester
260260
* @param string $domain Translation domain of the form
261261
* @param boolean $canEdit Can the requester edit the field of the question ?
262-
* @param array $value Values all fields of the form
262+
* @param PluginFormcreatorFormAnswer $value Values all fields of the form
263263
* @param bool $isVisible is the question visible by default ?
264264
*
265265
* @return string
266266
*/
267-
public function getRenderedHtml($domain, $canEdit = true, $value = [], $isVisible = true): string {
267+
public function getRenderedHtml($domain, $canEdit = true, ?PluginFormcreatorFormAnswer $form_answer = null, $isVisible = true): string {
268268
if ($this->isNewItem()) {
269269
return '';
270270
}
@@ -276,13 +276,7 @@ public function getRenderedHtml($domain, $canEdit = true, $value = [], $isVisibl
276276
return '';
277277
}
278278

279-
if ($field->hasInput($value)) {
280-
// Parse an HTML input
281-
$field->parseAnswerValues($value);
282-
} else {
283-
// Deserialize the default value from DB
284-
$field->deserializeValue($this->fields['default_values']);
285-
}
279+
$field->setFormAnswer($form_answer);
286280

287281
$required = ($this->fields['required']) ? ' required' : '';
288282
$x = $this->fields['col'];

tests/fixture/PluginFormcreatorDependentField.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public function deserializeValue($value) {
116116
: '';
117117
}
118118

119-
public function show($domain, $canEdit = true) {
119+
function show(string $domain, bool $canEdit = true): string {
120120
parent::show($canEdit);
121121
$questionId = $this->fields['id'];
122122
$domId = "input[name=\"formcreator_field_$questionId\"]";
@@ -129,7 +129,7 @@ public function show($domain, $canEdit = true) {
129129
}
130130
$firstnameQuestionId = $parameters['firstname']->getField('plugin_formcreator_questions_id_1');
131131
$lastnameQuestionId = $parameters['lastname']->getField('plugin_formcreator_questions_id_2');
132-
echo Html::scriptBlock("$(function() {
132+
return Html::scriptBlock("$(function() {
133133
plugin_formcreator_field_$questionId()
134134
$('input[name=\"formcreator_field_$firstnameQuestionId\"]').on('input', plugin_formcreator_field_$questionId)
135135
$('input[name=\"formcreator_field_$lastnameQuestionId\"]').on('input', plugin_formcreator_field_$questionId)

0 commit comments

Comments
 (0)