Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Entity Based Generation #37

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions front/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,18 @@
---------------------------------------------------------------------- */

include ('../../../inc/includes.php');

$config = new PluginGeninventorynumberConfig();
$plugin = new Plugin();
$config->getFromDB(1);
$config->getFromDBByRequest(['entities_id' => $_SESSION['glpiactive_entity']]);
if ($plugin->isInstalled("geninventorynumber")
&& $plugin->isActivated("geninventorynumber")) {
if (!$config->getFromDBByRequest(['entities_id' => $_SESSION['glpiactive_entity']])) {
$newconfig['name'] = 'otherserial';
$newconfig['is_active'] = 1;
$newconfig['entities_id'] = $_SESSION['glpiactive_entity'];
$newconfig['index'] = 0;
$config->add($newconfig);
}
Html::header(__('Inventory number generation', 'geninventorynumber'),
$_SERVER['PHP_SELF'], "tools", "plugins", "geninventorynumber");
if (isset($_GET['glpi_tab'])) {
Expand Down
16 changes: 6 additions & 10 deletions inc/config.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) {
static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) {
switch ($tabnum) {
case 0:
$item->showForm(1);
$item->showForm($_SESSION['glpiactive_entity']);
break;
case 1:
PluginGeninventorynumberConfigField::showForConfig($item->getID());
Expand Down Expand Up @@ -115,12 +115,7 @@ function rawSearchOptions() {
function showForm($id, $options = []) {
global $CFG_GLPI;

if ($id > 0) {
$this->getFromDB($id);
} else {
$this->getEmpty();
}

$this->getFromDBByRequest(['entities_id' => $id]);
$this->showFormHeader($options);

echo "<tr class='tab_bg_1'>";
Expand Down Expand Up @@ -176,6 +171,7 @@ static function getNextIndex() {

static function install(Migration $migration) {
global $DB;
$entity = $_SESSION['glpiactive_entity'];

$table = getTableForItemType(__CLASS__);
if ($DB->tableExists("glpi_plugin_generateinventorynumber_config")) {
Expand Down Expand Up @@ -205,14 +201,14 @@ static function install(Migration $migration) {
`is_active` tinyint(1) NOT NULL default 0,
`index` int(11) NOT NULL default 0,
`comment` text COLLATE utf8_unicode_ci,
PRIMARY KEY (`id`)
PRIMARY KEY (`id`, `entities_id`)
) ENGINE=InnoDB CHARSET=utf8 COLLATE=utf8_unicode_ci;";
$DB->query($sql) or die($DB->error());

$tmp['id'] = 1;
$tmp['name'] = 'otherserial';
$tmp['is_active'] = 1;
$tmp['entities_id'] = 0;
$tmp['entities_id'] = $entity;
$tmp['index'] = 0;
$config = new self();
$config->add($tmp);
Expand Down Expand Up @@ -251,7 +247,7 @@ static function updateIndex() {

static function isGenerationActive() {
$config = new self();
$config->getFromDB(1);
$config->getFromDBByRequest(['entities_id' => $_SESSION['glpiactive_entity']]);
return $config->fields['is_active'];
}

Expand Down
39 changes: 33 additions & 6 deletions inc/configfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,16 @@ static function getTypeName($nb = 0) {
return __('GLPI\'s inventory items configuration', 'geninventorynumber');
}

static function getConfigFieldByItemType($itemtype) {
$infos = getAllDataFromTable(getTableForItemType(__CLASS__), ['itemtype' => $itemtype]);
static function getConfigFieldByItemType($itemtype, $entities_id = -1) {
global $DB;

$query = "SELECT `id`
FROM `glpi_plugin_geninventorynumber_configs`
WHERE `entities_id`='".$entities_id."'";
$result = $DB->query($query);
$configs_id = $DB->result($result, 0, "id");

$infos = getAllDataFromTable(getTableForItemType(__CLASS__), ['itemtype' => $itemtype], ['plugin_geninventorynumber_configs_id' => $configs_id]);
if (!empty($infos)) {
return array_pop($infos);
} else {
Expand Down Expand Up @@ -110,12 +118,24 @@ static function uninstall(Migration $migration) {
}

static function showForConfig($id) {
global $CFG_GLPI, $DB;
global $CFG_GLPI, $DB, $GENINVENTORYNUMBER_TYPES;;

$config = new PluginGeninventorynumberConfig();
$config->getFromDB($id);
$target = Toolbox::getItemTypeFormUrl(__CLASS__);

if (!countElementsInTable(getTableForItemType(__CLASS__), ['plugin_geninventorynumber_configs_id' => $id])) {
$field = new self();
foreach ($GENINVENTORYNUMBER_TYPES as $type) {
$input["plugin_geninventorynumber_configs_id"] = $id;
$input["itemtype"] = $type;
$input["template"] = "&lt;#######&gt;";
$input["is_active"] = 0;
$input["index"] = 0;
$field->add($input);
}
}

echo "<form name='form_core_config' method='post' action=\"$target\">";
echo "<div align='center'>";
echo "<table class='tab_cadre_fixe'>";
Expand All @@ -129,7 +149,7 @@ static function showForConfig($id) {
echo "<th>" . __('Use global index', 'geninventorynumber') . "</th>";
echo "<th colspan='2'>" . __('Global index position', 'geninventorynumber') . "</th></tr>";

foreach (getAllDataFromTable(getTableForItemType(__CLASS__)) as $value) {
foreach (getAllDataFromTable(getTableForItemType(__CLASS__), ['plugin_geninventorynumber_configs_id' => $id]) as $value) {
$itemtype = $value['itemtype'];
echo "<td class='tab_bg_1' align='center'>" . call_user_func([$itemtype, 'getTypeName']). "</td>";
echo "<td class='tab_bg_1'>";
Expand Down Expand Up @@ -208,13 +228,20 @@ static function updateIndex($itemtype) {
}

static function registerNewItemType($itemtype) {
global $DB;
if (!class_exists($itemtype)) {
return;
}

if (!countElementsInTable(getTableForItemType(__CLASS__), ['itemtype' => $itemtype])) {

$query = "SELECT `id`
FROM `glpi_plugin_geninventorynumber_configs`
WHERE `entities_id`='".$_SESSION['glpiactive_entity']."'";
$result = $DB->query($query);
$configs_id = $DB->result($result, 0, "id");

$config = new self();
$input["plugin_geninventorynumber_configs_id"] = 1;
$input["plugin_geninventorynumber_configs_id"] = $configs_id;
$input["itemtype"] = $itemtype;
$input["template"] = "&lt;#######&gt;";
$input["is_active"] = 0;
Expand Down
5 changes: 3 additions & 2 deletions inc/generation.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ static function autoName($config, CommonDBTM $item) {
* @override CommonDBTM::preItemAdd
*/
static function preItemAdd(CommonDBTM $item) {
$config = PluginGeninventorynumberConfigField::getConfigFieldByItemType(get_class($item));
$config = PluginGeninventorynumberConfigField::getConfigFieldByItemType(get_class($item), $item->getEntityID());

if (in_array(get_class($item), PluginGeninventorynumberConfigField::getEnabledItemTypes())) {
if ((!Session::haveRight("plugin_geninventorynumber", CREATE))) {
Expand Down Expand Up @@ -164,7 +164,8 @@ static function showMassiveActionsSubForm(MassiveAction $ma) {
* @param CommonDBTM $item Existing item to update
*/
static function doMassiveUpdate(CommonDBTM $item) {
$config = PluginGeninventorynumberConfigField::getConfigFieldByItemType(get_class($item));
global $CFG_GLPI;
$config = PluginGeninventorynumberConfigField::getConfigFieldByItemType(get_class($item), $item->getEntityID());

if (in_array(get_class($item), PluginGeninventorynumberConfigField::getEnabledItemTypes())) {
$tmp = clone $item;
Expand Down