Skip to content

Commit f336f0a

Browse files
committed
Merge branch 'support/2.13.0' into support/2.14.0
2 parents f6d65dc + 2d58ba2 commit f336f0a

9 files changed

Lines changed: 182 additions & 79 deletions

css/styles.scss

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,15 @@ form#plugin_formcreator_form {
320320
padding: initial;
321321
padding-left: 40px;
322322
}
323+
324+
.label-radio, .label-checkbox {
325+
display: inline;
326+
margin-bottom: 0.2em;
327+
}
328+
329+
.radio, .checkbox {
330+
padding-bottom: 1em;
331+
}
323332
}
324333
}
325334

inc/field/checkboxesfield.class.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public function getRenderedHtml($domain, $canEdit = true): string {
101101
foreach ($values as $value) {
102102
if ((trim($value) != '')) {
103103
$i++;
104+
$translated_value = __($value, $domain);
104105
$html .= "<div class='checkbox'>";
105106
$html .= Html::getCheckbox([
106107
'title' => htmlentities($value, ENT_QUOTES),
@@ -110,8 +111,8 @@ public function getRenderedHtml($domain, $canEdit = true): string {
110111
'zero_on_empty' => false,
111112
'checked' => in_array($value, $this->value)
112113
]);
113-
$html .= '<label for="' . $domId . '_' . $i . '">';
114-
$html .= '&nbsp;' . __($value, $domain);
114+
$html .= '<label for="' . $domId . '_' . $i . '" class="label-checkbox" title="' . $translated_value . '">';
115+
$html .= '&nbsp;' . $translated_value;
115116
$html .= '</label>';
116117
$html .= "</div>";
117118
}

inc/field/radiosfield.class.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,13 @@ public function getRenderedHtml($domain, $canEdit = true): string {
8686
'id' => $domId . '_' . $i,
8787
'value' => $value
8888
] + $checked);
89-
$html .= '<label class="label-radio" title="' . $value . '" for="' . $domId . '_' . $i . '">';
89+
$translated_value = __($value, $domain);
90+
$html .= '<label for="' . $domId . '_' . $i . '" class="label-radio" title="' . $translated_value . '">';
9091
$html .= '<span class="box"></span>';
9192
$html .= '<span class="check"></span>';
93+
$html .= '&nbsp;' . $translated_value;
9294
$html .= '</label>';
9395
$html .= '</span>';
94-
$html .= '<label for="' . $domId . '_' . $i . '">';
95-
$html .= '&nbsp;' . __($value, $domain);
96-
$html .= '</label>';
9796
$html .= '</div>';
9897
}
9998
}

inc/form.class.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,7 @@ public function defineTabs($options = []) {
565565
$this->addStandardTab(self::class, $ong, $options);
566566
$this->addStandardTab(PluginFormcreatorFormAnswer::class, $ong, $options);
567567
$this->addStandardTab(PluginFormcreatorForm_Language::class, $ong, $options);
568+
$this->addStandardTab(Document_Item::class, $ong, $options);
568569
$this->addStandardTab(Log::class, $ong, $options);
569570
return $ong;
570571
}

inc/formanswer.class.php

Lines changed: 26 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -120,32 +120,26 @@ public function canViewItem() {
120120
}
121121

122122
$approvers = $this->getApprovers();
123-
if ($approvers === null) {
124-
return false;
125-
}
126-
127-
// Check if the current user is a validator user
128-
$validatorUsers = array_keys($approvers[User::getType()]);
129-
if (in_array($currentUser, $validatorUsers)) {
130-
return true;
131-
}
132-
133-
// Check if the current user is a member of a validator group
134-
$groupList = Group_User::getUserGroups($currentUser);
135-
$validatorGroups = array_keys($approvers[Group::getType()]);
136-
foreach ($groupList as $group) {
137-
if (in_array($group['id'], $validatorGroups)) {
138-
// one of the groups of the user is a validator group
123+
if ($approvers !== null) {
124+
// Check if the current user is a validator user
125+
$validatorUsers = array_keys($approvers[User::getType()]);
126+
if (in_array($currentUser, $validatorUsers)) {
139127
return true;
140128
}
141-
}
142129

143-
// $groups = Group_User::getUserGroups($currentUser);
144-
// if (in_array($this->fields['users_id_validator'], $groups)) {
145-
// return true;
146-
// }
130+
// Check if the current user is a member of a validator group
131+
$groups = Group_User::getUserGroups($currentUser);
132+
$validatorGroups = array_keys($approvers[Group::getType()]);
133+
foreach ($groups as $group) {
134+
if (in_array($group['id'], $validatorGroups)) {
135+
// one of the groups of the user is a validator group
136+
return true;
137+
}
138+
}
139+
}
147140

148141
if (version_compare(GLPI_VERSION, '10.1') >= 0) {
142+
// Check if the user is a substitute of a validator
149143
$request = [
150144
'SELECT' => self::getTableField('id'),
151145
'FROM' => self::getTable(),
@@ -160,71 +154,42 @@ public function canViewItem() {
160154
}
161155
}
162156

163-
$request = [
164-
'SELECT' => PluginFormcreatorForm_Validator::getTable() . '.*',
165-
'FROM' => $this::getTable(),
166-
'INNER JOIN' => [
167-
PluginFormcreatorForm::getTable() => [
168-
'FKEY' => [
169-
PluginFormcreatorForm::getTable() => PluginFormcreatorForm::getIndexName(),
170-
$this::getTable() => PluginFormcreatorForm::getForeignKeyField(),
171-
],
172-
],
173-
PluginFormcreatorForm_Validator::getTable() => [
174-
'FKEY' => [
175-
PluginFormcreatorForm::getTable() => PluginFormcreatorForm::getIndexName(),
176-
PluginFormcreatorForm_Validator::getTable() => PluginFormcreatorForm::getForeignKeyField()
177-
]
178-
]
179-
],
180-
'WHERE' => [$this::getTable() . '.id' => $this->getID()],
181-
];
182-
foreach ($DB->request($request) as $row) {
183-
if ($row['itemtype'] == User::class) {
184-
if ($currentUser == $row['items_id']) {
185-
return true;
186-
}
187-
} else {
188-
foreach ($groups as $group) {
189-
if ($group['id'] == $row['items_id']) {
190-
return true;
191-
}
192-
}
193-
}
194-
}
195-
196157
// Check if the current user is a requester of a ticket linked to a form answer typed
197158
// Matches search option 42, 43 and 44 of PluginFormcreatorIssue (requester, watcher, assigned)
198159
$ticket_table = Ticket::getTable();
199160
$ticket_user_table = Ticket_User::getTable();
200161
$item_ticket_table = Item_Ticket::getTable();
201162
$request = [
202-
'SELECT' => Ticket_User::getTableField(User::getForeignKeyField()),
163+
'SELECT' => [
164+
Ticket_User::getTableField(User::getForeignKeyField()),
165+
Ticket::getTableField('id'),
166+
],
203167
'FROM' => $ticket_user_table,
204168
'INNER JOIN' => [
205169
$ticket_table => [
206170
'FKEY' => [
207171
$ticket_table => 'id',
208172
$ticket_user_table => 'tickets_id',
173+
['AND' => [
174+
Ticket_User::getTableField(User::getForeignKeyField()) => $currentUser,
175+
]],
209176
],
210177
],
211178
$item_ticket_table => [
212179
'FKEY' => [
213180
$item_ticket_table => 'tickets_id',
214181
$ticket_table => 'id',
215182
['AND' => [
216-
Item_Ticket::getTableField('itemtype') => self::getType(),
183+
Item_Ticket::getTableField('itemtype') => self::getType(),
184+
Item_Ticket::getTableField('items_id') => $this->getID(),
217185
]],
218186
],
219187
],
220-
221188
]
222189
];
223190

224-
foreach ($DB->request($request) as $row) {
225-
if ($row[User::getForeignKeyField()] == $currentUser) {
226-
return true;
227-
}
191+
if ($DB->request($request)->count() > 0) {
192+
return true;
228193
}
229194

230195
return false;

setup.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
// Schema version of this version (major.minor only)
3838
define('PLUGIN_FORMCREATOR_SCHEMA_VERSION', '2.14');
3939
// is or is not an official release of the plugin
40-
define('PLUGIN_FORMCREATOR_IS_OFFICIAL_RELEASE', true);
40+
define('PLUGIN_FORMCREATOR_IS_OFFICIAL_RELEASE', false);
4141

4242
// Minimal GLPI version, inclusive
4343
define ('PLUGIN_FORMCREATOR_GLPI_MIN_VERSION', '10.0.5');
@@ -133,6 +133,7 @@ function plugin_init_formcreator() {
133133

134134
array_push($CFG_GLPI["ticket_types"], PluginFormcreatorFormAnswer::class);
135135
array_push($CFG_GLPI["document_types"], PluginFormcreatorFormAnswer::class);
136+
array_push($CFG_GLPI["document_types"], PluginFormcreatorForm::class);
136137

137138
$plugin = new Plugin();
138139
if (!$plugin->isActivated('formcreator')) {

tests/3-unit/PluginFormcreatorFormAnswer.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
use PluginFormcreatorTargetProblem;
5252
use Problem;
5353
use Session;
54+
use PluginFormcreatorForm_Validator;
5455
use Ticket;
5556
use TicketValidation;
5657
use Toolbox;
@@ -973,4 +974,112 @@ public function testParseTags($instance, $template, $expected) {
973974
$output = $instance->parseTags($template, $ticket, true);
974975
$this->string($output)->isEqualTo($expected);
975976
}
977+
978+
public function providerCanViewItem() {
979+
$this->login('glpi', 'glpi');
980+
$form = $this->getForm();
981+
$formAnswer = $this->getFormAnswer([
982+
'plugin_formcreator_forms_id' => $form->getID(),
983+
]);
984+
$this->logout();
985+
986+
yield 'Not authenticated' => [
987+
'formAnswer' => $formAnswer,
988+
'expected' => false,
989+
];
990+
991+
$this->login('glpi', 'glpi');
992+
993+
yield 'User granted to edit forms' => [
994+
'formAnswer' => $formAnswer,
995+
'expected' => true,
996+
];
997+
998+
$this->login('normal', 'normal');
999+
$formAnswer = $this->getFormAnswer([
1000+
'plugin_formcreator_forms_id' => $form->getID(),
1001+
]);
1002+
1003+
yield 'User is the requester' => [
1004+
'formAnswer' => $formAnswer,
1005+
'expected' => true,
1006+
];
1007+
1008+
$this->login('tech', 'tech');
1009+
1010+
yield 'User is not the requester' => [
1011+
'formAnswer' => $formAnswer,
1012+
'expected' => false,
1013+
];
1014+
1015+
$form->update([
1016+
'id' => $form->getID(),
1017+
'validation_required' => PluginFormcreatorForm_Validator::VALIDATION_USER,
1018+
'_validator_users' => [
1019+
User::getIdByName('tech'),
1020+
],
1021+
]);
1022+
$formAnswer = $this->getFormAnswer([
1023+
'plugin_formcreator_forms_id' => $form->getID(),
1024+
]);
1025+
1026+
yield 'User is the validator' => [
1027+
'formAnswer' => $formAnswer,
1028+
'expected' => true,
1029+
];
1030+
1031+
$this->login('normal', 'normal');
1032+
1033+
yield 'User is not the validator' => [
1034+
'formAnswer' => $formAnswer,
1035+
'expected' => false,
1036+
];
1037+
1038+
$group = $this->getGlpiCoreItem(Group::class, [
1039+
'name' => 'group' . $this->getUniqueString()
1040+
]);
1041+
$user = $this->getGlpiCoreItem(User::class, [
1042+
'name' => 'user' . $this->getUniqueString(),
1043+
'password' => 'password',
1044+
'password2' => 'password',
1045+
]);
1046+
1047+
$form->update([
1048+
'id' => $form->getID(),
1049+
'validation_required' => PluginFormcreatorForm_Validator::VALIDATION_GROUP,
1050+
'_validator_groups' => [
1051+
$group->getID(),
1052+
],
1053+
]);
1054+
$this->login('normal', 'normal');
1055+
$formAnswer = $this->getFormAnswer([
1056+
'plugin_formcreator_forms_id' => $form->getID(),
1057+
]);
1058+
$this->login($user->fields['name'], 'password');
1059+
1060+
yield 'User is not a member of validator group' => [
1061+
'formAnswer' => $formAnswer,
1062+
'expected' => false,
1063+
];
1064+
1065+
$groupUser = new Group_User();
1066+
$groupUser->add([
1067+
'groups_id' => $group->getID(),
1068+
'users_id' => $user->getID(),
1069+
]);
1070+
1071+
yield 'User is a member of validator group' => [
1072+
'formAnswer' => $formAnswer,
1073+
'expected' => true,
1074+
];
1075+
}
1076+
1077+
/**
1078+
* @dataProvider providerCanViewItem
1079+
*/
1080+
public function testCanViewItem($formAnswer, bool $expected) {
1081+
/** @var \PluginFormcreatorFormAnswer $formAnswer */
1082+
$output = $formAnswer->canViewItem();
1083+
$this->boolean($output)->isEqualTo($expected);
1084+
}
9761085
}

0 commit comments

Comments
 (0)