Skip to content

Commit 7149aa0

Browse files
stonebuzzbtry
authored andcommitted
feat(core): manage fields plugin
1 parent 45e43e3 commit 7149aa0

9 files changed

+2293
-9
lines changed

ajax/dropdown_fields_values.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* ---------------------------------------------------------------------
4+
* Formcreator is a plugin which allows creation of custom forms of
5+
* easy access.
6+
* ---------------------------------------------------------------------
7+
* LICENSE
8+
*
9+
* This file is part of Formcreator.
10+
*
11+
* Formcreator is free software; you can redistribute it and/or modify
12+
* it under the terms of the GNU General Public License as published by
13+
* the Free Software Foundation; either version 2 of the License, or
14+
* (at your option) any later version.
15+
*
16+
* Formcreator is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU General Public License
22+
* along with Formcreator. If not, see <http://www.gnu.org/licenses/>.
23+
* ---------------------------------------------------------------------
24+
* @copyright Copyright © 2011 - 2021 Teclib'
25+
* @license http://www.gnu.org/licenses/gpl.txt GPLv3+
26+
* @link https://github.com/pluginsGLPI/formcreator/
27+
* @link https://pluginsglpi.github.io/formcreator/
28+
* @link http://plugins.glpi-project.org/#/plugin/formcreator
29+
* ---------------------------------------------------------------------
30+
*/
31+
32+
use GlpiPlugin\Formcreator\Field\FieldsField;
33+
34+
include ('../../../inc/includes.php');
35+
36+
Session::checkRight('entity', UPDATE);
37+
38+
if (isset($_REQUEST['block_id'])
39+
&& $_REQUEST['block_id'] != '0') {
40+
$block_id = $_REQUEST['block_id'];
41+
$fields = FieldsField::getFieldsFromBlock($block_id);
42+
43+
//get value in case of update
44+
$selectedValue = 0;
45+
if (isset($_REQUEST['question_id'])){
46+
$question = new PluginFormcreatorQuestion();
47+
$question->getFromDB((int) $_REQUEST['question_id']);
48+
$decodedValues = json_decode($question->fields['values'], JSON_OBJECT_AS_ARRAY);
49+
$selectedValue = $decodedValues['dropdown_fields_field'] ?? '0';
50+
}
51+
52+
Dropdown::showFromArray('dropdown_fields_field',
53+
$fields, [
54+
'display_emptychoice' => false,
55+
'value' => $selectedValue,
56+
]
57+
);
58+
}

inc/abstractfield.class.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,6 @@ public function getRawValue() {
8787
public function show(string $domain, bool $canEdit = true): string {
8888
$html = '';
8989

90-
if ($this->isVisibleField()) {
91-
$html .= '<label for="formcreator_field_' . $this->question->getID() . '">';
92-
$html .= __($this->getLabel(), $domain);
93-
if ($canEdit && $this->question->fields['required']) {
94-
$html .= ' <span class="red">*</span>';
95-
}
96-
$html .= '</label>';
97-
}
9890
if ($this->isEditableField() && !empty($this->question->fields['description'])) {
9991
$description = $this->question->fields['description'];
10092
foreach (PluginFormcreatorCommon::getDocumentsFromTag($description) as $document) {
@@ -110,7 +102,22 @@ public function show(string $domain, bool $canEdit = true): string {
110102
$html .= $this->getRenderedHtml($domain, $canEdit);
111103
$html .= '</div>';
112104

113-
return $html;
105+
106+
//Determine if fields is mandatory after generate it's HTML
107+
//usefull for fields plugin
108+
//because fields plugin manage it's own mandatory system and can overload $this->question->fields['required']
109+
//when HTML is generated (see $this->getRenderedHtml)
110+
$label = '';
111+
if ($this->isVisibleField()) {
112+
$label .= '<label for="formcreator_field_' . $this->question->getID() . '">';
113+
$label .= __($this->getLabel(), $domain);
114+
if ($canEdit && $this->question->fields['required']) {
115+
$label .= ' <span class="red">*</span>';
116+
}
117+
$label .= '</label>';
118+
}
119+
120+
return $label.$html;
114121
}
115122

116123
public function getRenderedHtml($domain, $canEdit = true): string {

0 commit comments

Comments
 (0)