You are not logged in.

Announcement

 Téléchargez la dernière version stable de GLPI      -     Et vous, que pouvez vous faire pour le projet GLPI ? :  Contribuer
 Download last stable version of GLPI                      -     What can you do for GLPI ? :  Contribute

#1 2024-05-02 12:44:32

scarletTornado
Member
Registered: 2024-04-12
Posts: 19

[SOLVED]Plugin development: store array as configuration value

I'm trying to store an array as a configuration value for my plugin. The code is as follows:

Config::setConfigurationValues('plugin:Assetlist', [
         'config_class'    => PluginAssetlistConfig::class,                   // Clase de configuración
         'menu'            => 'plugins',                                      // Menu de acceso para manejar listados
         //'assetlist_types' => $CFG_GLPI['appliance_types'],                   // Por defecto se admiten los mismos tipos que en Appliance
         //'assetlist_relation_types' => $CFG_GLPI['appliance_relation_types']  // Por defecto los mismos tipos que en Appliance
      ]);

I saw that on the impact tab from the general configuration page you could setup a dropdown with multiple choice selection. The problem is that when I pass the arguments to the form page it shows the following error in the log:

[2024-05-02 12:42:36] glpiphplog.CRITICAL:   *** Uncaught Exception TypeError: Glpi\Toolbox\Sanitizer::isNsClassOrCallableIdentifier(): Argument #1 ($value) must be of type string, array given, called in C:\Users\alumno\Desktop\development\glpi-test\src\DBmysql.php on line 1318 in C:\Users\alumno\Desktop\development\glpi-test\src\Toolbox\Sanitizer.php at line 228
  Backtrace :
  src\DBmysql.php:1318                               Glpi\Toolbox\Sanitizer::isNsClassOrCallableIdentifier()
  src\DBmysql.php:1356                               DBmysql::quoteValue()
  src\DBmysql.php:1381                               DBmysql->buildInsert()
  src\CommonDBTM.php:729                             DBmysql->insert()
  src\CommonDBTM.php:1342                            CommonDBTM->addToDB()
  src\Config.php:3071                                CommonDBTM->add()
  src\Config.php:173                                 Config::setConfigurationValues()
  src\CommonDBTM.php:1612                            Config->prepareInputForUpdate()
  front\config.form.php:73                           CommonDBTM->update()

What is the Impact class doing differently that allows it to store an array as a configuration value or how is it storing the information?

Last edited by scarletTornado (2024-05-02 16:39:33)

Offline

#2 2024-05-02 15:03:07

cconard96
Moderator
Registered: 2018-07-31
Posts: 3,267
Website

Re: [SOLVED]Plugin development: store array as configuration value

You have to JSON encode any array values before they are saved to the DB and then decode them when reading from the DB. It isn't done automatically by the DB class.


GLPI Collaborator and Plugin Developer.
My non-English comments are automated translations. Sorry for any confusion that causes.
Mes commentaires non anglais sont des traductions automatiques. Désolé pour toute confusion qui cause.
Mis comentarios que no están en inglés son traducciones automáticas. Perdón por cualquier confusión que cause.

Offline

#3 2024-05-02 15:40:04

scarletTornado
Member
Registered: 2024-04-12
Posts: 19

Re: [SOLVED]Plugin development: store array as configuration value

cconard96 wrote:

You have to JSON encode any array values before they are saved to the DB and then decode them when reading from the DB. It isn't done automatically by the DB class.

I've done it by changing the form URL to a custom php form file. Inside it I recover the options and build an array I can use for updating the configuration variables. I don't know if there is a way to pass $_POST variables to another page.

// Not sure if necessary to include
include ('../../../inc/includes.php');

// Check if plugin is enabled
$plugin = new Plugin();
if (!$plugin->isActivated('assetlist')) {
   Html::displayNotFoundError();
}

// Check if logged ing
Session::checkLoginUser();

// Check if I can update conf
Session::checkRight("config", UPDATE);

// Empty associative array -> used for updating configuration
$updateConf = [];

// Menu where access shall be displayed
if (!empty($_POST["menu"])) {
    $updateConf["menu"] = $_POST["menu"];
}

// allowed itemtypes for assetlists
if (!empty($_POST['assetlist_types'])) {
    $v = json_encode($_POST['assetlist_types']);
    $_POST['assetlist_types'] = $v;
    $updateConf['assetlist_types'] = $v;
}

// allowed relation types for assetlist items
if (!empty($_POST['assetlist_relation_types'])) {
    $v = json_encode($_POST['assetlist_relation_types']);
    $_POST['assetlist_relation_types'] = $v;
    $updateConf['assetlist_relation_types'] = $v;
}

//Html::redirect(Config::getFormURL(true));     // Redirect does not pass $_POST to next page

// Update plugin conf through array
Config::setConfigurationValues("plugin:Assetlist", $updateConf);

// Return to previous page
Html::back();

Last edited by scarletTornado (2024-05-02 15:40:24)

Offline

Board footer

Powered by FluxBB