Skip to content

Commit

Permalink
feat(glpiselectfield): refactor entity_restriction
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed Mar 30, 2023
1 parent 2fd6cf5 commit b3fb080
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
21 changes: 13 additions & 8 deletions inc/field/dropdownfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use Toolbox;
use Session;
use DBUtils;
use Document;
use Dropdown;
use CommonITILActor;
use CommonITILObject;
Expand All @@ -62,11 +63,12 @@
use Glpi\Application\View\TemplateRenderer;
class DropdownField extends PluginFormcreatorAbstractField
{

const ENTITY_RESTRICT_USER = 1;
const ENTITY_RESTRICT_FORM = 2;
const ENTITY_RESTRICT_BOTH = 3;

protected static $noEntityRrestrict = [Entity::class, Document::class];

public function getEnumEntityRestriction() {
return [
self::ENTITY_RESTRICT_USER => User::getTypeName(1),
Expand Down Expand Up @@ -94,10 +96,14 @@ public function showForm(array $options): void {
$this->question->fields['_entity_restrict'] = $decodedValues['entity_restrict'] ?? self::ENTITY_RESTRICT_FORM;
$this->question->fields['_is_tree'] = '0';
$this->question->fields['_is_entity_restrict'] = '0';
if (isset($this->question->fields['itemtype']) && is_subclass_of($this->question->fields['itemtype'], CommonDBTM::class)) {
if (!in_array($this->question->fields['itemtype'], self::$noEntityRrestrict)) {
$item = new $this->question->fields['itemtype'];
$this->question->fields['_is_entity_restrict'] = $item->isEntityAssign() ? '1' : '0';
}
}
if (isset($this->question->fields['itemtype']) && is_subclass_of($this->question->fields['itemtype'], CommonTreeDropdown::class)) {
$this->question->fields['_is_tree'] = '1';
$item = new $this->question->fields['itemtype'];
$this->question->fields['_is_entity_restrict'] = $item->isEntityAssign() ? '1' : '0';
}
$this->question->fields['default_values'] = Html::entities_deep($this->question->fields['default_values']);
$this->deserializeValue($this->question->fields['default_values']);
Expand Down Expand Up @@ -136,6 +142,10 @@ public function buildParams($rand = null) {
JSON_OBJECT_AS_ARRAY
);

if (in_array($itemtype, self::$noEntityRrestrict)) {
unset($dparams['entity']);
}

switch ($itemtype) {
case SLA::class:
case OLA::class:
Expand All @@ -145,11 +155,6 @@ public function buildParams($rand = null) {
}
break;

case Entity::class:
case Document::class:
unset($dparams['entity']);
break;

case User::class:
$dparams['right'] = 'all';
$currentEntity = Session::getActiveEntity();
Expand Down
6 changes: 4 additions & 2 deletions inc/field/glpiselectfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ public function showForm(array $options): void {
$this->question->fields['_is_tree'] = '0';
$this->question->fields['_is_entity_restrict'] = '0';
if (isset($this->question->fields['itemtype']) && is_subclass_of($this->question->fields['itemtype'], CommonDBTM::class)) {
$item = new $this->question->fields['itemtype'];
$this->question->fields['_is_entity_restrict'] = $item->isEntityAssign() ? '1' : '0';
if (!in_array($this->question->fields['itemtype'], self::$noEntityRrestrict)) {
$item = new $this->question->fields['itemtype'];
$this->question->fields['_is_entity_restrict'] = $item->isEntityAssign() ? '1' : '0';
}
}
if (isset($this->question->fields['itemtype']) && $this->question->fields['itemtype'] == User::class) {
$this->question->fields['_is_entity_restrict'] = '1';
Expand Down

0 comments on commit b3fb080

Please sign in to comment.