Skip to content

Commit

Permalink
feat(dropdownfield,glpiselectfield): allow customization of base entity
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <[email protected]>
  • Loading branch information
btry committed Jun 14, 2021
1 parent f91363c commit e974fb1
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 4 deletions.
95 changes: 93 additions & 2 deletions inc/field/dropdownfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use CommonITILObject;
use CommonTreeDropdown;
use ITILCategory;
use Entity;
use User;
use Group;
use Group_User;
Expand All @@ -54,6 +55,19 @@

class DropdownField extends PluginFormcreatorAbstractField
{

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

public function getEnumEntityRestriction() {
return [
self::ENTITY_RESTRICT_USER => __('User', 'formcreator'),
self::ENTITY_RESTRICT_FORM => __('Form', 'formcreator'),
self::ENTITY_RESTRICT_BOTH => __('User and form', 'formcreator'),
];
}

public function isPrerequisites(): bool {
$itemtype = $this->getSubItemtype();

Expand Down Expand Up @@ -126,7 +140,7 @@ public function getDesignSpecializationField(): array {
'change' => __('Change categories', 'formcreator'),
'all' => __('All'),
];
$additions .= dropdown::showFromArray('show_ticket_categories', $ticketCategoriesOptions, [
$additions .= Dropdown::showFromArray('show_ticket_categories', $ticketCategoriesOptions, [
'rand' => $rand,
'value' => isset($decodedValues['show_ticket_categories'])
? $decodedValues['show_ticket_categories']
Expand All @@ -146,6 +160,7 @@ public function getDesignSpecializationField(): array {
// This row will be generated by an AJAX request
$additions .= '</tr>';
$additions .= Html::scriptBlock("plugin_formcreator_changeDropdownItemtype($rand);");
$additions .= $this->getEntityRestrictSettiing();

// Service level specific
$additions .= '<tr class="plugin_formcreator_question_specific plugin_formcreator_dropdown_service_level">';
Expand Down Expand Up @@ -199,7 +214,7 @@ public function buildParams($rand = null) {
'value' => $this->value,
'display' => false,
'comments' => false,
'entity' => $form->fields['entities_id'],
'entity' => $this->getEntityRestriction(),
//'entity_sons' => (bool) $form->isRecursive(),
'displaywith' => ['id'],
];
Expand Down Expand Up @@ -533,6 +548,13 @@ public function prepareQuestionInputForSave($input) {
unset($input['show_service_level_types']);
}

// Params for entity restrictables itemtypes
$itemtype = $input['dropdown_values'];
if ((new $itemtype)->isEntityAssign()) {
$input['values']['entity_restrict'] = $input['entity_restrict'];
}
unset($input['entity_restrict']);

$input['values'] = json_encode($input['values']);

$input['default_values'] = isset($input['dropdown_default_value']) ? $input['dropdown_default_value'] : '';
Expand Down Expand Up @@ -834,4 +856,73 @@ public static function getSubItemtypeForValues($values) {

return $decodedValues['itemtype'];
}

/**
* get HTML code to show entity restriction policy
* @return string HTML code
*/
protected function getEntityRestrictSettiing()
{
$restrictionPolicy = self::ENTITY_RESTRICT_FORM;
$decodedValues = json_decode($this->question->fields['values'], JSON_OBJECT_AS_ARRAY);
if (isset($decodedValues['entity_restrict'])) {
$restrictionPolicy = $decodedValues['entity_restrict'];
}

$html = '';

$html .= '<tr class="plugin_formcreator_entity_assignable">';
$html .= '<td>';
$html .= '<label for="entity_restrict">' . __('Entity restriction', 'formcreator') . '</label>';
$html .= '</td>';
$html .= '<td>';
$settings = $this->getEnumEntityRestriction();
$html .= Dropdown::showFromArray(
'entity_restrict',
$settings,
['display' => false, 'value' => $restrictionPolicy]
);
$html .= '</td>';
$html .= '</tr>';
return $html;
}

/**
* Get the entity restriction for item availability in the field
*
* @return void
*/
protected function getEntityRestriction()
{
$decodedValues = json_decode($this->question->fields['values'], JSON_OBJECT_AS_ARRAY);
$restrictionPolicy = self::ENTITY_RESTRICT_FORM;
if (isset($decodedValues['entity_restrict'])) {
$restrictionPolicy = $decodedValues['entity_restrict'];
}

switch ($restrictionPolicy) {
case self::ENTITY_RESTRICT_FORM:
$form = new PluginFormcreatorForm();
$form->getFromDBByQuestion($this->getQuestion());
$formEntities = [$form->fields['entities_id']];
if ($form->fields['is_recursive']) {
$formEntities = $formEntities + (new DBUtils())->getSonsof(Entity::getTable(), $form->fields['entities_id']);
}
return $formEntities;
break;

case self::ENTITY_RESTRICT_BOTH:
$form = new PluginFormcreatorForm();
$form->getFromDBByQuestion($this->getQuestion());
$formEntities = [$form->fields['entities_id']];
if ($form->fields['is_recursive']) {
$formEntities = $formEntities + (new DBUtils())->getSonsof(Entity::getTable(), $form->fields['entities_id']);
}
// If no entityes are in common, the result will be empty
return array_intersect_key($_SESSION['glpiactiveentities'], $formEntities);
break;
}

return $_SESSION['glpiactiveentities'];
}
}
40 changes: 38 additions & 2 deletions install/upgrade_to_2.12.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ class PluginFormcreatorUpgradeTo2_12 {
* @param Migration $migration
*/
public function upgrade(Migration $migration) {
global $DB;

$this->migration = $migration;

// Convert datetime to timestamp
Expand All @@ -54,6 +52,8 @@ public function upgrade(Migration $migration) {
$this->migration->addField($table, 'is_search_visible', 'integer', ['after' => 'is_kb_separated']);
$this->migration->addField($table, 'is_header_visible', 'integer', ['after' => 'is_search_visible']);
$this->migration->addField($table, 'header', 'text', ['after' => 'is_header_visible']);

$this->migrateReferenceEntity();
}

/**
Expand Down Expand Up @@ -89,4 +89,40 @@ public function changeDropdownTreeSettings() {
$DB->update($table, ['values' => $newValues], ['id' => $row['id']]);
}
}

public function migrateReferenceEntity() {
global $DB;

$questionTable = 'glpi_plugin_formcreator_questions';
$request = [
'SELECT' => ['id', 'default_values', 'values'],
'FROM' => $questionTable,
'WHERE' => ['fieldtype' => ['droprown']],
];
foreach ($DB->request($request) as $row) {
$newAnswer = json_decode($row['values']);
if ($newAnswer === null) {
$newAnswer = ['itemtype' => $row['answer']];
$newAnswer['entity_restrict'] = 2;
} else {
if (!isset($newAnswer['entity_restrict'])) {
$newAnswer['entity_restrict'] = 'form';
}
switch ($newAnswer['entity_restrict']) {
case 'user':
$newAnswer['entity_restrict'] = 1;
break;
case 'both':
$newAnswer['entity_restrict'] = 3;
break;
default:
$newAnswer['entity_restrict'] = 2;
break;
}
}
$newAnswer = json_encode($newAnswer, JSON_OBJECT_AS_ARRAY);
$newAnswer = Toolbox::addslashes_deep($newAnswer);
$DB->update($questionTable, ['values' => $newAnswer], ['id' => $row['id']]);
}
}
}
32 changes: 32 additions & 0 deletions js/scripts.js.php
Original file line number Diff line number Diff line change
Expand Up @@ -1474,6 +1474,38 @@ function plugin_formcreator_changeDropdownItemtype(rand) {
$('.plugin_formcreator_dropdown').html("");
$('.plugin_formcreator_dropdown').toggle(false);
});

var entityAssignable = [
'Location',
'TaskCategory',
'TaskTemplate',
'SolutionType',
'SolutionTemplate',
'ProjectTaskTemplate',
'SoftwareLicenseType',
'CertificateType',
'RackType',
'PDUType',
'ClusterType',
'BusinessCriticity',
'KnowbaseItemCategory',
'Calendar',
'Holiday',
'Netpoint',
'Vlan',
'LineOperator',
'DomainType',
'DomainRecordType',
'DomainRelation',
'IPNetwork',
'FQDN',
'WifiNetwork',
'NetworkName',
'Fieldblacklist',
'ApplianceType'
];
var showEntityAssignable = (entityAssignable.indexOf(dropdown_type) >= 0);
$('.plugin_formcreator_entity_assignable').toggle(showEntityAssignable);
});
}

Expand Down

0 comments on commit e974fb1

Please sign in to comment.