Skip to content

Commit

Permalink
Added a walk array deep to be sure to get any types
Browse files Browse the repository at this point in the history
fixes #8
  • Loading branch information
tomolimo committed Aug 24, 2020
1 parent 546ce89 commit eb90cb0
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 166 deletions.
4 changes: 2 additions & 2 deletions impacts.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
<compatibility>9.3</compatibility>
</version>
<version>
<num>1.3.5</num>
<compatibility>9.4</compatibility>
<num>1.3.6</num>
<compatibility>9.3</compatibility>
<compatibility>9.4</compatibility>
</version>
</versions>
<langs>
Expand Down
328 changes: 165 additions & 163 deletions inc/config.class.php
Original file line number Diff line number Diff line change
@@ -1,163 +1,165 @@
<?php
/*
* -------------------------------------------------------------------------
Impacts plugin
Copyright (C) 2018 by Raynet SAS a company of A.Raymond Network.
http://www.araymond.com
-------------------------------------------------------------------------
LICENSE
This file is part of Impacts plugin for GLPI.
This file is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
GLPI is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GLPI. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/

if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access this file directly");
}

class PluginImpactsConfig extends CommonDBTM {

static private $_instance = null;
static $rightname = "config";

static function canCreate() {
return self::canUpdate();
}

/**
* Summary of getTypeName
* @param mixed $nb plural
* @return mixed
*/
static function getTypeName($nb = 0) {
return __("Asset impact setup", "impacts");
}

/**
* Summary of getName
* @param mixed $with_comment with comment
* @return mixed
*/
function getName($with_comment = 0) {
return self::getTypeName();
}

/**
* Summary of getInstance
* @return PluginImpactsConfig
*/
static function getInstance() {
if (!isset(self::$_instance)) {
self::$_instance = new self();
if (!self::$_instance->getFromDB(1)) {
self::$_instance->getEmpty();
}
// convert asset_list into PHP array
self::$_instance->fields['assets'] = importArrayFromDB(self::$_instance->fields['assets']);
}
return self::$_instance;
}


/**
* Summary of showConfigForm
* @param mixed $item is the config
* @return boolean
*/
static function showConfigForm($item) {
$config = self::getInstance();
$config->showFormHeader();

echo "<tr class='tab_bg_2'>";
echo "<td>" . __('Asset list', 'impacts') . "</td>";
echo "<td>";
Dropdown::showFromArray('assets', self::getAssetList(true), [
'values' => $config->fields['assets'],
'width' => '100%',
'multiple' => true
]);
echo "</td></tr>\n";

$config->showFormButtons(['candel'=>false]);

return false;
}

/**
* Prepare input datas for updating the item
*
* @see CommonDBTM::prepareInputForUpdate()
*
* @param $input array of datas used to update the item
*
* @return the modified $input array
**/
function prepareInputForUpdate($input) {
// asset_list update
$input['assets'] = exportArrayToDB((isset($input['assets'])
? $input['assets'] : []));
return $input;
}

function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) {
if ($item::getType() == 'Config') {
return self::getTypeName();
}
return '';
}


static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) {
if ($item::getType() == 'Config') {
self::showConfigForm($item);
}
return true;
}


static function getAssetList($completelist = false) {
global $CFG_GLPI;

$regex = '/types$/m';
$listTemp = [];
foreach ($CFG_GLPI as $k => $type) {
if (is_array($type) && preg_match_all($regex, $k, $matches, PREG_SET_ORDER, 0)) {
foreach ($type as $val) {
if (strcmp($val, '*')) {
array_push($listTemp, $val);
}
}
}
}
$list = array_unique($listTemp);

if (!$completelist) {
$config = self::getInstance();
$list = $config->fields['assets'];
}
$ret = [];
foreach ($list as $lo) {
if (class_exists($lo)) {
$ret[$lo] = $lo::getTypeName(Session::getPluralNumber())." ($lo)";
}
}
asort($ret, SORT_STRING);
return $ret;
}

}
<?php
/*
* -------------------------------------------------------------------------
Impacts plugin
Copyright (C) 2018 by Raynet SAS a company of A.Raymond Network.
http://www.araymond.com
-------------------------------------------------------------------------
LICENSE
This file is part of Impacts plugin for GLPI.
This file is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
GLPI is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GLPI. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/

if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access this file directly");
}

class PluginImpactsConfig extends CommonDBTM {

static private $_instance = null;
static $rightname = "config";

static function canCreate() {
return self::canUpdate();
}

/**
* Summary of getTypeName
* @param mixed $nb plural
* @return mixed
*/
static function getTypeName($nb = 0) {
return __("Asset impact setup", "impacts");
}

/**
* Summary of getName
* @param mixed $with_comment with comment
* @return mixed
*/
function getName($with_comment = 0) {
return self::getTypeName();
}

/**
* Summary of getInstance
* @return PluginImpactsConfig
*/
static function getInstance() {
if (!isset(self::$_instance)) {
self::$_instance = new self();
if (!self::$_instance->getFromDB(1)) {
self::$_instance->getEmpty();
}
// convert asset_list into PHP array
self::$_instance->fields['assets'] = importArrayFromDB(self::$_instance->fields['assets']);
}
return self::$_instance;
}


/**
* Summary of showConfigForm
* @param mixed $item is the config
* @return boolean
*/
static function showConfigForm($item) {
$config = self::getInstance();
$config->showFormHeader();

echo "<tr class='tab_bg_2'>";
echo "<td>" . __('Asset list', 'impacts') . "</td>";
echo "<td>";
Dropdown::showFromArray('assets', self::getAssetList(true), [
'values' => $config->fields['assets'],
'width' => '100%',
'multiple' => true
]);
echo "</td></tr>\n";

$config->showFormButtons(['candel'=>false]);

return false;
}

/**
* Prepare input datas for updating the item
*
* @see CommonDBTM::prepareInputForUpdate()
*
* @param $input array of datas used to update the item
*
* @return the modified $input array
**/
function prepareInputForUpdate($input) {
// asset_list update
$input['assets'] = exportArrayToDB((isset($input['assets'])
? $input['assets'] : []));
return $input;
}

function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) {
if ($item::getType() == 'Config') {
return self::getTypeName();
}
return '';
}


static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) {
if ($item::getType() == 'Config') {
self::showConfigForm($item);
}
return true;
}


static function getAssetList($completelist = false) {
global $CFG_GLPI;

function printValue($value, $key, $userData) {
if (strcmp($value, '*')) {
$userData[] = $value;
}
}

$regex = '/types$/m';
$listTemp = new ArrayObject();
foreach ($CFG_GLPI as $k => $type) {
if (is_array($type) && preg_match_all($regex, $k, $matches, PREG_SET_ORDER, 0)) {
array_walk_recursive($type, 'printValue', $listTemp);
}
}
$list = array_unique($listTemp->getArrayCopy());

if (!$completelist) {
$config = self::getInstance();
$list = $config->fields['assets'];
}
$ret = [];
foreach ($list as $lo) {
if (class_exists($lo)) {
$ret[$lo] = $lo::getTypeName(Session::getPluralNumber())." ($lo)";
}
}
asort($ret, SORT_STRING);
return $ret;
}

}
Binary file modified pics/Supplier.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
// ----------------------------------------------------------------------


define('IMPACTS_VERSION', '1.3.5');
define('IMPACTS_VERSION', '1.3.6');

// Minimal GLPI version, inclusive
define("IMPACTS_MIN_GLPI", "9.3");
Expand Down

0 comments on commit eb90cb0

Please sign in to comment.