Skip to content

Commit 1afc675

Browse files
committed
feat(ldapselectfield): lazy loading
1 parent 8a437fb commit 1afc675

7 files changed

+107
-17
lines changed

ajax/gettranslationsvalues.php

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333

3434
// Direct access to file
3535
if (strpos($_SERVER['PHP_SELF'], 'gettranslationsvalues.php')) {
36-
include ('../../../inc/includes.php');
3736
header('Content-Type: application/json; charset=UTF-8');
3837
Html::header_nocache();
3938
} else if (!defined('GLPI_ROOT')) {

css/styles.scss

+1
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ form#plugin_formcreator_form {
421421
border-left: 4px solid #999;
422422
margin: 10px 15px;
423423
word-wrap: break-word;
424+
padding-left: 12px;
424425
}
425426

426427
#plugin_formcreator_form .form-group .form_field > input,

inc/field/descriptionfield.class.php

+5
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ public function isPrerequisites(): bool {
4343
return true;
4444
}
4545

46+
public function show($domain, $canEdit = true) {
47+
48+
return '<div class="description_field">' . $this->getRenderedHtml($domain, $canEdit) . '</div>';
49+
}
50+
4651
public function getDesignSpecializationField(): array {
4752
$common = parent::getDesignSpecializationField();
4853
$additions = $common['additions'];

inc/field/ldapselectfield.class.php

+34
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
use QuerySubQuery;
4242
use Session;
4343
use RuleRightParameter;
44+
use PluginFormcreatorQuestion;
45+
use PluginFormcreatorLdapDropdown;
4446

4547
class LdapselectField extends SelectField
4648
{
@@ -55,6 +57,7 @@ public function getDesignSpecializationField(): array {
5557
if ($ldap_values === null) {
5658
$ldap_values = [];
5759
}
60+
5861
$current_entity = Session::getActiveEntity();
5962
$auth_ldap_condition = '';
6063
if ($current_entity != 0) {
@@ -124,6 +127,37 @@ public function getDesignSpecializationField(): array {
124127
];
125128
}
126129

130+
public function getRenderedHtml($domain, $canEdit = true): string {
131+
if (!$canEdit) {
132+
return $this->value . PHP_EOL;
133+
}
134+
135+
$html = '';
136+
$id = $this->question->getID();
137+
$rand = mt_rand();
138+
$fieldName = 'formcreator_field_' . $id;
139+
140+
if (!empty($this->question->fields['values'])) {
141+
$options = [
142+
'name' => $fieldName,
143+
'value' => $this->value,
144+
'rand' => $rand,
145+
'multiple' => false,
146+
'display' => false,
147+
'condition' => [
148+
PluginFormcreatorQuestion::getForeignKeyField() => $this->question->getID()
149+
]
150+
];
151+
$html .= PluginFormcreatorLdapDropdown::dropdown($options);
152+
}
153+
$html .= PHP_EOL;
154+
$html .= Html::scriptBlock("$(function() {
155+
pluginFormcreatorInitializeSelect('$fieldName', '$rand');
156+
});");
157+
158+
return $html;
159+
}
160+
127161
public function getAvailableValues() {
128162
if (empty($this->question->fields['values'])) {
129163
return [];

inc/form.class.php

+53
Original file line numberDiff line numberDiff line change
@@ -2808,4 +2808,57 @@ public function getBestLanguage() {
28082808
}
28092809
return \Locale::lookup($availableLanguages, $_SESSION['glpilanguage'], false, $defaultLanguage);
28102810
}
2811+
2812+
/**
2813+
* Can the current user show the form to fill an assistance request
2814+
*
2815+
* @return boolean true if the user can use the form
2816+
*/
2817+
public function canViewForRequest(): bool {
2818+
global $PLUGIN_HOOKS;
2819+
2820+
if ($this->isNewItem()) {
2821+
return false;
2822+
}
2823+
2824+
// Public forms -> always visible
2825+
if ($this->fields['access_rights'] == self::ACCESS_PUBLIC) {
2826+
return true;
2827+
}
2828+
2829+
// Restricted and private forms -> Check session
2830+
if (Session::getLoginUserID() === false || !$this->checkEntity(true)) {
2831+
return false;
2832+
}
2833+
2834+
// Form administrators can always access any forms
2835+
if (self::canCreate()) {
2836+
return true;
2837+
}
2838+
2839+
// Check restrictions if needed
2840+
if ($this->fields['access_rights'] == self::ACCESS_RESTRICTED
2841+
&& !PluginFormcreatorFormAccessType::canSeeRestrictedForm($this)
2842+
) {
2843+
return false;
2844+
}
2845+
2846+
// Check plugins restrictions
2847+
if (isset($PLUGIN_HOOKS['formcreator_restrict_form'])) {
2848+
foreach ($PLUGIN_HOOKS['formcreator_restrict_form'] as $plugin => $callable) {
2849+
// Skip if invalid hook
2850+
if (!is_callable($callable)) {
2851+
trigger_error("formcreator_restrict_form[$plugin]: not a callable", E_USER_WARNING);
2852+
continue;
2853+
}
2854+
2855+
if (!call_user_func($callable, $this)) {
2856+
return false;
2857+
}
2858+
}
2859+
}
2860+
2861+
// All checks were succesful, display form
2862+
return true;
2863+
}
28112864
}

inc/item_targetticket.class.php

+11-13
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function export(bool $remove_uuid = false) : array {
9696
return $item_targetTicket;
9797
}
9898

99-
public static function import(PluginFormcreatorLinker $linker, $input = [], $containerId = 0, $dryRun = false) {
99+
public static function import(PluginFormcreatorLinker $linker, $input = [], $containerId = 0) {
100100
if (!isset($input['uuid']) && !isset($input['id'])) {
101101
throw new ImportFailureException(sprintf('UUID or ID is mandatory for %1$s', static::getTypeName(1)));
102102
}
@@ -120,19 +120,17 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con
120120
}
121121

122122
// set ID for linked objects
123-
if (!$dryRun) {
124-
$linkedItemtype = $input['itemtype'];
125-
$linkedItemId = $input['items_id'];
126-
$linkedItem = $linker->findObject($linkedItemtype, $linkedItemId, $idKey);
127-
if ($linkedItem->isNewItem()) {
128-
if (strpos($linkedItemtype, 'PluginFormcreator') === 0) {
129-
// the linnked object belongs to the plugin, maybe the item will be imported later
130-
$linker->postpone($input[$idKey], $item->getType(), $input, $containerId);
131-
return false;
132-
}
133-
// linked item is not an object of Formcreator, it will not be imported
134-
throw new ImportFailureException('Failed to find a linked object to a target ticket');
123+
$linkedItemtype = $input['itemtype'];
124+
$linkedItemId = $input['items_id'];
125+
$linkedItem = $linker->findObject($linkedItemtype, $linkedItemId, $idKey);
126+
if ($linkedItem->isNewItem()) {
127+
if (strpos($linkedItemtype, 'PluginFormcreator') === 0) {
128+
// the linnked object belongs to the plugin, maybe the item will be imported later
129+
$linker->postpone($input[$idKey], $item->getType(), $input, $containerId);
130+
return false;
135131
}
132+
// linked item is not an object of Formcreator, it will not be imported
133+
throw new ImportFailureException('Failed to find a linked object to a target ticket');
136134
}
137135

138136
// Add or update

inc/translation.class.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
* ---------------------------------------------------------------------
3030
*/
3131

32+
use Glpi\Toolbox\Sanitizer;
33+
3234
if (!defined('GLPI_ROOT')) {
3335
die("Sorry. You can't access this file directly");
3436
}
@@ -88,9 +90,7 @@ public static function getDropdownValue($post, $json = true) {
8890
if (!$formLanguage->getFromDB($formLanguageId)) {
8991
return [];
9092
}
91-
if (!isset($post['searchText'])) {
92-
$post['searchText'] = '';
93-
}
93+
$post['searchText'] = $post['searchText'] ?? '';
9494

9595
$form = new PluginFormcreatorForm();
9696
$form->getFromDB($formLanguage->fields['plugin_formcreator_forms_id']);

0 commit comments

Comments
 (0)