pluginsGLPI / geninventorynumber

Inventorynumber generation
GNU General Public License v3.0
8 stars 14 forks source link

Duplicate Inventory Items and Deleted Templates #6

Open mgurm opened 8 years ago

mgurm commented 8 years ago

Hello

We ran into a strange bug concerning the preItemAdd hook in the geninventory plugin, so I thought I'd post it here.

We add new items into GLPI using the template feature (this means the insert form URL looks like /front/computer.form.php?id=123&withtemplate=2). Now when inventory number generation is deactivated for the item type that is inserted, the function preItemAdd performs an update on a DB entry before the insert. This happens in the else clause of the following code part:

     if (PluginGeninventorynumberConfig::isGenerationActive()
        && PluginGeninventorynumberConfigField::isActiveForItemType(get_class($item))) {
          // ...
     } else {
            $values['otherserial'] = '';
            $values['id']          = $item->getID();
            $tmp->update($values);
     }

However, this update uses the ID of the template, not the ID of the new item (which, as far as I understand, is not yet in the database at this point). The problem with this is that it sets is_template to false. This means that the template is transformed into a normal item. So in the end, I have two new items instead of one. And, of course, the template is gone.

I was able to fix this by removing the update call in the else clause, but I don't know if that is a good fix. I am not sure why this update is needed in the first place (maybe someone can explain it to me?).

Regards, mgurm