hi,

oh, I forgot to tell, that I was testing with the current stable release
(0.72.3).

I had a short look at the code. For that case, recursive software is
just ignored. 

I created a patch to correct this (for 0.72.3).
(ok, it got bigger than I thought ;) I only wanted to reach all possible
cases)

Can some one test it?


greetings,

Bernhard



On Thu, 2009-12-10 at 09:04 +0100, Bernhard Denner wrote:
> Hi,
> 
> I have a small problem with the sync process of OCS -> GLPI.
> 
> We have our inventory split in several entities (different sites). If I
> import some software packages from OCS, for example "7-Zip 0.47", GLPI
> creates a software "7-Zip 0.47" in every entity. 
> So I tried to first create an software package with that name in the
> parent entity and set the "recursive" flag (-> so the software is
> visible for all child entities too). 
> But if I sync with OCS again it recreates software packages in every
> entity. It seems, that GLPI does not recognize, that there is already a
> software package visible with that name.
> 
> As the license management is done in our site (corresponding to the
> parent entity), I don't want to have different software entries for the
> different sites/entities.  
> 
> Is GLPI behaving in this case a designed, or is it just a bug?
> 
> greetings 
> Bernhard
> 
> 
> 
> _______________________________________________
> Glpi-dev mailing list
> Glpi-dev@gna.org
> https://mail.gna.org/listinfo/glpi-dev
diff -Bru /tmp/glpi/inc/ocsng.function.php glpi/inc/ocsng.function.php
--- /tmp/glpi/inc/ocsng.function.php	2009-10-27 18:18:21.000000000 +0100
+++ glpi/inc/ocsng.function.php	2009-12-10 18:45:21.000000000 +0100
@@ -3035,7 +3035,7 @@
 							//---- The software doesn't exists in this version for this computer -----//
 							//----------------------------------------------------------------------------------------------------------------//
 
-							$isNewSoft= addSoftwareOrRestoreFromTrash($modified_name,$manufacturer,$entity);
+							$isNewSoft= addSoftwareOrRestoreFromTrash($modified_name,$manufacturer,$entity, '', 1);
 							//Import version for this software
 							$versionID = ocsImportVersion($isNewSoft,$modified_version);
 		
diff -Bru /tmp/glpi/inc/software.function.php glpi/inc/software.function.php
--- /tmp/glpi/inc/software.function.php	2009-10-27 18:18:21.000000000 +0100
+++ glpi/inc/software.function.php	2009-12-10 18:42:57.000000000 +0100
@@ -1369,22 +1369,70 @@
  * @param manufacturer the software's manufacturer
  * @param entity the entity in which the software must be added
  * @param comments comments
+ * @param recursive also check for recursive software before adding
  */
-function addSoftwareOrRestoreFromTrash($name,$manufacturer,$entity,$comments='') {
+function addSoftwareOrRestoreFromTrash($name,$manufacturer,$entity,$comments='',$recursive=0) {
 	global $DB;
 	//Look for the software by his name in GLPI for a specific entity
-	$query_search = "SELECT glpi_software.ID as ID, glpi_software.deleted as deleted
+	$query_search = "SELECT glpi_software.ID as ID, 
+				glpi_software.deleted as deleted,
+				glpi_software.FK_entities as FK_entities,
+				glpi_software.recursive as recursive
 			FROM glpi_software
-			WHERE name = '".$name."' AND is_template='0' AND FK_entities='".$entity."'";
+			WHERE name = '".$name."' AND 
+				  is_template='0' AND ";
+	
+	if ($recursive) {
+		$query_search .= "(FK_entities='".$entity."' OR recursive = '1')
+			ORDER BY recursive";
+	} else {
+		$query_search .= "FK_entities='".$entity."'";
+	}
+	
 	$result_search = $DB->query($query_search);
 	if ($DB->numrows($result_search) > 0) {
-		//Software already exists for this entity, get his ID
-		$data = $DB->fetch_array($result_search);
-		$ID = $data["ID"];
-
-		// restore software
-		if ($data['deleted'])
-			removeSoftwareFromTrash($ID);
+		$anchestor_levels = NULL;
+		$best = NULL;
+		$best_level = -1;
+		while ($data = $DB->fetch_array($result_search)) {
+			// if we find a non recursive software, it is in the specified entity
+			// or the software in this entity is a recursive one
+			// -> we prefer this one
+			if ($data['recursive'] == 0 || $data['FK_entities'] == $entity) {
+				$best = $data;
+				break;
+			}
+				
+			// if we are looking for a recursive software, we calculate the levels of 
+			// the anchestors of this entity (needed to choose the software with the closesed anchestor entity)
+			if (is_null($anchestor_levels)) {
+				$a = getEntityAncestors($entity);
+				$a[$entity] = $entity;
+				foreach ($a as $e) {
+					$anchestor_levels[$e] = count(getEntityAncestors($e));
+				}
+			}
+			
+			// if this software is in an anchestor entity and it is closer as the current best one
+			// we make this software the best one 
+			if (in_array($data['FK_entities'], getEntityAncestors($entity)) && 
+				$anchestor_levels[$data['FK_entities']] > $best_level) 
+			{
+				$best_level = $anchestor_levels[$data['FK_entities']];
+				$best = $data;
+			}
+		}
+		if (!is_null($best)) {
+			$data = $best;
+			//Software already exists for this entity, get his ID
+			$ID = $data["ID"];
+
+			// restore software
+			if ($data['deleted'])
+				removeSoftwareFromTrash($ID);
+		} else {
+			$ID = 0;
+		}
 	} else {
 		$ID = 0;
 	}
_______________________________________________
Glpi-dev mailing list
Glpi-dev@gna.org
https://mail.gna.org/listinfo/glpi-dev

Reply via email to