Skip to content

Commit b3fb080

Browse files
committed
feat(glpiselectfield): refactor entity_restriction
1 parent 2fd6cf5 commit b3fb080

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

inc/field/dropdownfield.class.php

+13-8
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
use Toolbox;
4040
use Session;
4141
use DBUtils;
42+
use Document;
4243
use Dropdown;
4344
use CommonITILActor;
4445
use CommonITILObject;
@@ -62,11 +63,12 @@
6263
use Glpi\Application\View\TemplateRenderer;
6364
class DropdownField extends PluginFormcreatorAbstractField
6465
{
65-
6666
const ENTITY_RESTRICT_USER = 1;
6767
const ENTITY_RESTRICT_FORM = 2;
6868
const ENTITY_RESTRICT_BOTH = 3;
6969

70+
protected static $noEntityRrestrict = [Entity::class, Document::class];
71+
7072
public function getEnumEntityRestriction() {
7173
return [
7274
self::ENTITY_RESTRICT_USER => User::getTypeName(1),
@@ -94,10 +96,14 @@ public function showForm(array $options): void {
9496
$this->question->fields['_entity_restrict'] = $decodedValues['entity_restrict'] ?? self::ENTITY_RESTRICT_FORM;
9597
$this->question->fields['_is_tree'] = '0';
9698
$this->question->fields['_is_entity_restrict'] = '0';
99+
if (isset($this->question->fields['itemtype']) && is_subclass_of($this->question->fields['itemtype'], CommonDBTM::class)) {
100+
if (!in_array($this->question->fields['itemtype'], self::$noEntityRrestrict)) {
101+
$item = new $this->question->fields['itemtype'];
102+
$this->question->fields['_is_entity_restrict'] = $item->isEntityAssign() ? '1' : '0';
103+
}
104+
}
97105
if (isset($this->question->fields['itemtype']) && is_subclass_of($this->question->fields['itemtype'], CommonTreeDropdown::class)) {
98106
$this->question->fields['_is_tree'] = '1';
99-
$item = new $this->question->fields['itemtype'];
100-
$this->question->fields['_is_entity_restrict'] = $item->isEntityAssign() ? '1' : '0';
101107
}
102108
$this->question->fields['default_values'] = Html::entities_deep($this->question->fields['default_values']);
103109
$this->deserializeValue($this->question->fields['default_values']);
@@ -136,6 +142,10 @@ public function buildParams($rand = null) {
136142
JSON_OBJECT_AS_ARRAY
137143
);
138144

145+
if (in_array($itemtype, self::$noEntityRrestrict)) {
146+
unset($dparams['entity']);
147+
}
148+
139149
switch ($itemtype) {
140150
case SLA::class:
141151
case OLA::class:
@@ -145,11 +155,6 @@ public function buildParams($rand = null) {
145155
}
146156
break;
147157

148-
case Entity::class:
149-
case Document::class:
150-
unset($dparams['entity']);
151-
break;
152-
153158
case User::class:
154159
$dparams['right'] = 'all';
155160
$currentEntity = Session::getActiveEntity();

inc/field/glpiselectfield.class.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ public function showForm(array $options): void {
5858
$this->question->fields['_is_tree'] = '0';
5959
$this->question->fields['_is_entity_restrict'] = '0';
6060
if (isset($this->question->fields['itemtype']) && is_subclass_of($this->question->fields['itemtype'], CommonDBTM::class)) {
61-
$item = new $this->question->fields['itemtype'];
62-
$this->question->fields['_is_entity_restrict'] = $item->isEntityAssign() ? '1' : '0';
61+
if (!in_array($this->question->fields['itemtype'], self::$noEntityRrestrict)) {
62+
$item = new $this->question->fields['itemtype'];
63+
$this->question->fields['_is_entity_restrict'] = $item->isEntityAssign() ? '1' : '0';
64+
}
6365
}
6466
if (isset($this->question->fields['itemtype']) && $this->question->fields['itemtype'] == User::class) {
6567
$this->question->fields['_is_entity_restrict'] = '1';

0 commit comments

Comments
 (0)