Skip to content

Commit

Permalink
feat(ldapselectfield): lazy loading
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed Aug 31, 2022
1 parent 8a437fb commit 1afc675
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 17 deletions.
1 change: 0 additions & 1 deletion ajax/gettranslationsvalues.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

// Direct access to file
if (strpos($_SERVER['PHP_SELF'], 'gettranslationsvalues.php')) {
include ('../../../inc/includes.php');
header('Content-Type: application/json; charset=UTF-8');
Html::header_nocache();
} else if (!defined('GLPI_ROOT')) {
Expand Down
1 change: 1 addition & 0 deletions css/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ form#plugin_formcreator_form {
border-left: 4px solid #999;
margin: 10px 15px;
word-wrap: break-word;
padding-left: 12px;
}

#plugin_formcreator_form .form-group .form_field > input,
Expand Down
5 changes: 5 additions & 0 deletions inc/field/descriptionfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public function isPrerequisites(): bool {
return true;
}

public function show($domain, $canEdit = true) {

return '<div class="description_field">' . $this->getRenderedHtml($domain, $canEdit) . '</div>';
}

public function getDesignSpecializationField(): array {
$common = parent::getDesignSpecializationField();
$additions = $common['additions'];
Expand Down
34 changes: 34 additions & 0 deletions inc/field/ldapselectfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
use QuerySubQuery;
use Session;
use RuleRightParameter;
use PluginFormcreatorQuestion;
use PluginFormcreatorLdapDropdown;

class LdapselectField extends SelectField
{
Expand All @@ -55,6 +57,7 @@ public function getDesignSpecializationField(): array {
if ($ldap_values === null) {
$ldap_values = [];
}

$current_entity = Session::getActiveEntity();
$auth_ldap_condition = '';
if ($current_entity != 0) {
Expand Down Expand Up @@ -124,6 +127,37 @@ public function getDesignSpecializationField(): array {
];
}

public function getRenderedHtml($domain, $canEdit = true): string {
if (!$canEdit) {
return $this->value . PHP_EOL;
}

$html = '';
$id = $this->question->getID();
$rand = mt_rand();
$fieldName = 'formcreator_field_' . $id;

if (!empty($this->question->fields['values'])) {
$options = [
'name' => $fieldName,
'value' => $this->value,
'rand' => $rand,
'multiple' => false,
'display' => false,
'condition' => [
PluginFormcreatorQuestion::getForeignKeyField() => $this->question->getID()
]
];
$html .= PluginFormcreatorLdapDropdown::dropdown($options);
}
$html .= PHP_EOL;
$html .= Html::scriptBlock("$(function() {
pluginFormcreatorInitializeSelect('$fieldName', '$rand');
});");

return $html;
}

public function getAvailableValues() {
if (empty($this->question->fields['values'])) {
return [];
Expand Down
53 changes: 53 additions & 0 deletions inc/form.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2808,4 +2808,57 @@ public function getBestLanguage() {
}
return \Locale::lookup($availableLanguages, $_SESSION['glpilanguage'], false, $defaultLanguage);
}

/**
* Can the current user show the form to fill an assistance request
*
* @return boolean true if the user can use the form
*/
public function canViewForRequest(): bool {
global $PLUGIN_HOOKS;

if ($this->isNewItem()) {
return false;
}

// Public forms -> always visible
if ($this->fields['access_rights'] == self::ACCESS_PUBLIC) {
return true;
}

// Restricted and private forms -> Check session
if (Session::getLoginUserID() === false || !$this->checkEntity(true)) {
return false;
}

// Form administrators can always access any forms
if (self::canCreate()) {
return true;
}

// Check restrictions if needed
if ($this->fields['access_rights'] == self::ACCESS_RESTRICTED
&& !PluginFormcreatorFormAccessType::canSeeRestrictedForm($this)
) {
return false;
}

// Check plugins restrictions
if (isset($PLUGIN_HOOKS['formcreator_restrict_form'])) {
foreach ($PLUGIN_HOOKS['formcreator_restrict_form'] as $plugin => $callable) {
// Skip if invalid hook
if (!is_callable($callable)) {
trigger_error("formcreator_restrict_form[$plugin]: not a callable", E_USER_WARNING);
continue;
}

if (!call_user_func($callable, $this)) {
return false;
}
}
}

// All checks were succesful, display form
return true;
}
}
24 changes: 11 additions & 13 deletions inc/item_targetticket.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function export(bool $remove_uuid = false) : array {
return $item_targetTicket;
}

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

// set ID for linked objects
if (!$dryRun) {
$linkedItemtype = $input['itemtype'];
$linkedItemId = $input['items_id'];
$linkedItem = $linker->findObject($linkedItemtype, $linkedItemId, $idKey);
if ($linkedItem->isNewItem()) {
if (strpos($linkedItemtype, 'PluginFormcreator') === 0) {
// the linnked object belongs to the plugin, maybe the item will be imported later
$linker->postpone($input[$idKey], $item->getType(), $input, $containerId);
return false;
}
// linked item is not an object of Formcreator, it will not be imported
throw new ImportFailureException('Failed to find a linked object to a target ticket');
$linkedItemtype = $input['itemtype'];
$linkedItemId = $input['items_id'];
$linkedItem = $linker->findObject($linkedItemtype, $linkedItemId, $idKey);
if ($linkedItem->isNewItem()) {
if (strpos($linkedItemtype, 'PluginFormcreator') === 0) {
// the linnked object belongs to the plugin, maybe the item will be imported later
$linker->postpone($input[$idKey], $item->getType(), $input, $containerId);
return false;
}
// linked item is not an object of Formcreator, it will not be imported
throw new ImportFailureException('Failed to find a linked object to a target ticket');
}

// Add or update
Expand Down
6 changes: 3 additions & 3 deletions inc/translation.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
* ---------------------------------------------------------------------
*/

use Glpi\Toolbox\Sanitizer;

if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access this file directly");
}
Expand Down Expand Up @@ -88,9 +90,7 @@ public static function getDropdownValue($post, $json = true) {
if (!$formLanguage->getFromDB($formLanguageId)) {
return [];
}
if (!isset($post['searchText'])) {
$post['searchText'] = '';
}
$post['searchText'] = $post['searchText'] ?? '';

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

0 comments on commit 1afc675

Please sign in to comment.