Skip to content

Commit 338d5fe

Browse files
committed
feat: use twig to edit questions
1 parent 0c83da0 commit 338d5fe

File tree

66 files changed

+2514
-901
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+2514
-901
lines changed

ajax/question.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,13 @@
3232
include ('../../../inc/includes.php');
3333
Session::checkRight('entity', UPDATE);
3434

35+
$question_id = $_REQUEST['id'] ?? 0;
3536
$question = new PluginFormcreatorQuestion();
36-
if (empty($_REQUEST['question_id'])) {
37-
$question_id = 0;
37+
if ($question_id == 0) {
3838
$question->getEmpty();
3939
$sectionFk = PluginFormcreatorSection::getForeignKeyField();
4040
$question->fields[$sectionFk] = (int) $_REQUEST['plugin_formcreator_sections_id'];
4141
} else {
42-
$question_id = (int) $_REQUEST['question_id'];
4342
$question->getFromDB($question_id);
4443
}
4544
$question->showForm($question_id);

ajax/question_design.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,35 +32,36 @@
3232
include ('../../../inc/includes.php');
3333
Session::checkRight('entity', UPDATE);
3434

35-
if (!isset($_REQUEST['questionId'])) {
35+
if (!isset($_REQUEST['id'])) {
3636
http_response_code(400);
3737
exit();
3838
}
39-
if (!isset($_REQUEST['questionType'])) {
39+
if (!isset($_REQUEST['fieldtype'])) {
4040
http_response_code(400);
4141
exit();
4242
}
4343

4444
$question = new PluginFormcreatorQuestion();
4545
$question->getEmpty();
46-
if (!$question->isNewID((int) $_REQUEST['questionId']) && !$question->getFromDB((int) $_REQUEST['questionId'])) {
46+
if (!$question->isNewID((int) $_REQUEST['id']) && !$question->getFromDB((int) $_REQUEST['id'])) {
4747
http_response_code(400);
4848
exit();
4949
}
5050

51-
$question->fields['fieldtype'] = $_REQUEST['questionType'];
51+
// Modify the question to reflect changes in the form
52+
$question->fields['plugin_formcreator_sections_id'] = (int) $_REQUEST['plugin_formcreator_sections_id'];
53+
foreach (array_keys($question->fields) as $key) {
54+
if (!isset($_REQUEST[$key])) {
55+
continue;
56+
}
57+
$question->fields[$key] = $_REQUEST[$key];
58+
}
5259
$field = PluginFormcreatorFields::getFieldInstance(
53-
$question->fields['fieldtype'],
60+
$_REQUEST['fieldtype'],
5461
$question
5562
);
56-
$json = [
57-
'label' => '',
58-
'field' => '',
59-
'additions' => '',
60-
'may_be_empty' => false,
61-
];
63+
$question->fields['fieldtype'] = '';
6264
if ($field !== null) {
63-
$field->deserializeValue($question->fields['default_values']);
64-
$json = $field->getDesignSpecializationField();
65+
$question->fields['fieldtype'] = $_REQUEST['fieldtype'];
6566
}
66-
echo json_encode($json);
67+
$question->showForm($question->getID());

ajax/target_actor.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
die();
4343
}
4444

45-
4645
switch ($_POST['action']) {
4746
case 'add':
4847
$id = (int) $_POST['id'];

inc/abstractfield.class.php

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,6 @@ public function __construct(PluginFormcreatorQuestion $question) {
5252
$this->question = $question;
5353
}
5454

55-
public function getDesignSpecializationField(): array {
56-
return [
57-
'label' => '',
58-
'field' => '',
59-
'additions' => $this->getParametersHtmlForDesign(),
60-
'may_be_empty' => false,
61-
'may_be_required' => static::canRequire(),
62-
];
63-
}
64-
6555
public function prepareQuestionInputForSave($input) {
6656
$this->value = $input['default_values'];
6757
return $input;
@@ -268,43 +258,11 @@ protected function getParametersHtmlForDesign() {
268258
return '';
269259
}
270260

271-
$question = new PluginFormcreatorQuestion();
272-
$question->getFromDB($this->question->getID());
273-
$form = PluginFormcreatorForm::getByItem($question);
274-
275-
/** @var integer $column 0 for 2 first columns, 1 for 2 right ones */
276-
$column = 0;
277-
$rowSize = 2;
278261
$additions = '';
279262
foreach ($parameters as $parameter) {
280-
if ($column == 0) {
281-
$additions .= '<tr class="plugin_formcreator_question_specific">';
282-
}
283-
$parameterSize = 1 + $parameter->getParameterFormSize();
284-
if ($column + $parameterSize > $rowSize) {
285-
// The parameter needs more room than available in the current row
286-
if ($column < $rowSize) {
287-
// fill the remaining of the row
288-
$additions .= str_repeat('<td></td><td></td>', $rowSize - $column);
289-
// Close current row and open an new one
290-
$additions .= '</tr><tr class="plugin_formcreator_question_specific">';
291-
$column = 0;
292-
}
293-
}
294-
$additions .= $parameter->getParameterForm($form, $question);
295-
$column += $parameterSize;
296-
if ($column == $rowSize) {
297-
// Finish the row
298-
$additions .= '</tr>';
299-
$column = 0;
300-
}
301-
}
302-
if ($column < $rowSize) {
303-
// fill the remaining of the row
304-
$additions .= str_repeat('<td></td><td></td>', $rowSize - $column);
305-
// Close current row and open an new one
306-
$additions .= "</tr>";
263+
$additions .= $parameter->getParameterForm($this->question);
307264
}
265+
308266
return $additions;
309267
}
310268

inc/field/actorfield.class.php

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use User;
3838
use Session;
3939
use GlpiPlugin\Formcreator\Exception\ComparisonException;
40+
use Glpi\Application\View\TemplateRenderer;
4041

4142
/**
4243
* Actors field is a field which accepts several users. Those users may be
@@ -49,44 +50,15 @@ public function isPrerequisites(): bool {
4950
return true;
5051
}
5152

52-
public function getDesignSpecializationField(): array {
53-
$rand = mt_rand();
54-
55-
$label = '';
56-
$field = '';
57-
58-
$additions = '<tr class="plugin_formcreator_question_specific">';
59-
$additions .= '<td>';
60-
$additions .= '<label for="dropdown_default_values' . $rand . '">';
61-
$additions .= __('Default values');
62-
$additions .= '<small>(' . __('One per line', 'formcreator') . ')</small>';
63-
$additions .= '</label>';
64-
$additions .= '</td>';
65-
$additions .= '<td>';
66-
$additions .= Html::textarea([
67-
'name' => 'default_values',
68-
'id' => 'default_values',
69-
'value' => Html::entities_deep($this->getValueForDesign()),
70-
'cols' => '50',
71-
'display' => false,
53+
public function showForm(array $options): void {
54+
$template = '@formcreator/field/' . $this->question->fields['fieldtype'] . 'field.html.twig';
55+
56+
$this->question->fields['default_values'] = Html::entities_deep($this->getValueForDesign());
57+
$this->deserializeValue($this->question->fields['default_values']);
58+
TemplateRenderer::getInstance()->display($template, [
59+
'item' => $this->question,
60+
'params' => $options,
7261
]);
73-
$additions .= '</td>';
74-
$additions .= '<td>';
75-
$additions .= '</td>';
76-
$additions .= '<td>';
77-
$additions .= '</td>';
78-
$additions .= '</tr>';
79-
80-
$common = parent::getDesignSpecializationField();
81-
$additions .= $common['additions'];
82-
83-
return [
84-
'label' => $label,
85-
'field' => $field,
86-
'additions' => $additions,
87-
'may_be_empty' => false,
88-
'may_be_required' => static::canRequire(),
89-
];
9062
}
9163

9264
public static function getName(): string {

inc/field/checkboxesfield.class.php

Lines changed: 19 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -37,66 +37,30 @@
3737
use Toolbox;
3838
use Session;
3939
use PluginFormcreatorQuestionRange;
40+
use Glpi\Application\View\TemplateRenderer;
4041

4142
class CheckboxesField extends PluginFormcreatorAbstractField
4243
{
4344
public function isPrerequisites(): bool {
4445
return true;
4546
}
4647

47-
public function getDesignSpecializationField(): array {
48-
$rand = mt_rand();
49-
50-
$label = '';
51-
$field = '';
52-
53-
$additions = '<tr class="plugin_formcreator_question_specific">';
54-
$additions .= '<td>';
55-
$additions .= '<label for="default_values' . $rand . '">';
56-
$additions .= __('Default values');
57-
$additions .= '<small>(' . __('One per line', 'formcreator') . ')</small>';
58-
$additions .= '</label>';
59-
$additions .= '</td>';
60-
$additions .= '<td>';
61-
$additions .= Html::textarea([
62-
'name' => 'default_values',
63-
'id' => 'default_values',
64-
'value' => Html::entities_deep($this->getValueForDesign()),
65-
'cols' => '50',
66-
'display' => false,
67-
]);
68-
$additions .= '</td>';
69-
$additions .= '<td>';
70-
$additions .= '<label for="values' . $rand . '">';
71-
$additions .= __('Values');
72-
$additions .= '<small>(' . __('One per line', 'formcreator') . ')</small>';
73-
$additions .= '</label>';
74-
$additions .= '</td>';
75-
$additions .= '<td>';
76-
$value = json_decode($this->question->fields['values']);
77-
if ($value === null) {
78-
$value = [];
79-
}
80-
$additions .= Html::textarea([
81-
'name' => 'values',
82-
'id' => 'values',
83-
'value' => implode("\r\n", $value),
84-
'cols' => '50',
85-
'display' => false,
86-
]);
87-
$additions .= '</td>';
88-
$additions .= '</tr>';
8948

90-
$common = parent::getDesignSpecializationField();
91-
$additions .= $common['additions'];
49+
public function showForm(array $options): void {
50+
$template = '@formcreator/field/' . $this->question->fields['fieldtype'] . 'field.html.twig';
9251

93-
return [
94-
'label' => $label,
95-
'field' => $field,
96-
'additions' => $additions,
97-
'may_be_empty' => false,
98-
'may_be_required' => static::canRequire(),
99-
];
52+
$this->question->fields['values'] = json_decode($this->question->fields['values']);
53+
$this->question->fields['values'] = is_array($this->question->fields['values']) ? $this->question->fields['values'] : [];
54+
$this->question->fields['values'] = implode("\r\n", $this->question->fields['values']);
55+
$this->question->fields['default_values'] = Html::entities_deep($this->getValueForDesign());
56+
$this->deserializeValue($this->question->fields['default_values']);
57+
58+
$parameters = $this->getParameters();
59+
TemplateRenderer::getInstance()->display($template, [
60+
'item' => $this->question,
61+
'question_params' => $parameters,
62+
'params' => $options,
63+
]);
10064
}
10165

10266
public function getRenderedHtml($domain, $canEdit = true): string {
@@ -322,7 +286,10 @@ public function getEmptyParameters(): array {
322286
$range = new PluginFormcreatorQuestionRange();
323287
$range->setField($this, [
324288
'fieldName' => 'range',
325-
'label' => __('Range', 'formcreator'),
289+
'label' => [
290+
__('Range min', 'formcreator'),
291+
__('Range max', 'formcreator'),
292+
],
326293
'fieldType' => ['text'],
327294
]);
328295
return [

inc/field/datefield.class.php

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,9 @@
3535
use PluginFormcreatorAbstractField;
3636
use Html;
3737
use DateTime;
38-
use Toolbox;
3938
use Session;
4039
use GlpiPlugin\Formcreator\Exception\ComparisonException;
41-
40+
use Glpi\Application\View\TemplateRenderer;
4241

4342
class DateField extends PluginFormcreatorAbstractField
4443
{
@@ -48,40 +47,50 @@ public function isPrerequisites(): bool {
4847
return true;
4948
}
5049

51-
public function getDesignSpecializationField(): array {
52-
$rand = mt_rand();
53-
54-
$label = '';
55-
$field = '';
56-
57-
$additions = '<tr class="plugin_formcreator_question_specific">';
58-
$additions .= '<td>';
59-
$additions .= '<label for="dropdown_default_values' . $rand . '">';
60-
$additions .= __('Default values');
61-
$additions .= '</label>';
62-
$additions .= '</td>';
63-
$additions .= '<td>';
64-
$value = Html::entities_deep($this->question->fields['default_values']);
65-
$additions .= Html::showDateField('default_values', ['value' => $value, 'display' => false]);
66-
$additions .= '</td>';
67-
$additions .= '<td>';
68-
$additions .= '</td>';
69-
$additions .= '<td>';
70-
$additions .= '</td>';
71-
$additions .= '</tr>';
72-
73-
$common = parent::getDesignSpecializationField();
74-
$additions .= $common['additions'];
75-
76-
return [
77-
'label' => $label,
78-
'field' => $field,
79-
'additions' => $additions,
80-
'may_be_empty' => false,
81-
'may_be_required' => static::canRequire(),
82-
];
50+
public function showForm(array $options): void {
51+
$template = '@formcreator/field/' . $this->question->fields['fieldtype'] . 'field.html.twig';
52+
53+
$this->question->fields['default_values'] = Html::entities_deep($this->question->fields['default_values']);
54+
$this->deserializeValue($this->question->fields['default_values']);
55+
TemplateRenderer::getInstance()->display($template, [
56+
'item' => $this->question,
57+
'params' => $options,
58+
]);
8359
}
8460

61+
// public function getDesignSpecializationField(): string {
62+
// $templateFile = '@formcreator/field/datefield.html.twig';
63+
// $question = $this->question;
64+
// $question->fields['default_values'] = Html::entities_deep($question->fields['default_values']);
65+
66+
// $additions = TemplateRenderer::getInstance()->renderBlock(
67+
// $templateFile,
68+
// 'additions',
69+
// [
70+
// 'item' => $question,
71+
// ]
72+
// );
73+
74+
// $common = parent::getDesignSpecializationField();
75+
// $additions .= $common['additions'];
76+
77+
// return [
78+
// 'field' => TemplateRenderer::getInstance()->renderBlock(
79+
// $templateFile,
80+
// 'subtype'
81+
// ),
82+
// 'additions' => $additions,
83+
// 'required' => TemplateRenderer::getInstance()->renderBlock(
84+
// $templateFile,
85+
// 'required'
86+
// ),
87+
// 'empty' => TemplateRenderer::getInstance()->renderBlock(
88+
// $templateFile,
89+
// 'empty'
90+
// ),
91+
// ];
92+
// }
93+
8594
public function getRenderedHtml($domain, $canEdit = true): string {
8695
if (!$canEdit) {
8796
return $this->value;

0 commit comments

Comments
 (0)