Code

Updated listing table summary
[gosa.git] / gosa-plugins / opsi / admin / opsiLicenses / class_opsiLicenseHandler.inc
index 7082a109360e79e79631671c65687ddcb2f777b5..fd8b01f472f7580ace9d88cc517779db0045c6d8 100644 (file)
@@ -18,31 +18,35 @@ class opsiLicenceHandler extends opsi  {
   {
     $data= array();
     $res = $this->send_data("gosa_opsi_getLicensePools_listOfHashes",$this->target,$data,TRUE);
-    
     $items  = array();
-    if(isset($res['XML'][0]['RESULT'][0]['HIT'])){
-      $items = $res['XML'][0]['RESULT'][0]['HIT'];
-      $data =array();
-      foreach($items as $item){
-        $entry = array();
-        foreach(
-          array(
-            "DESCRIPTION"       => "description",
-            "LICENSEPOOLID"     => "cn",
-            "PRODUCTIDS"        => "productId",
-            "WINDOWSSOFTWAREIDS"=> "softwareId") as $source => $dest){
-        
-          if(isset($item[$source])){
-            $entry[$dest] = array('count' => 0);
-            foreach($item[$source] as $obj){
-              $entry[$dest][] = $obj['VALUE'];
-            }
-            $entry[$dest]['count'] = (count($entry[$dest]) -1 );
-          } 
+    if(isset($res['XML'][0]['ANSWER_OPSI_GETLICENSEPOOLS_LISTOFHASHES'])){
+      if(isset($res['XML'][0]['RESULT'][0]['HIT'])){
+        $items = $res['XML'][0]['RESULT'][0]['HIT'];
+        $data =array();
+        foreach($items as $item){
+          $entry = array();
+          foreach(
+              array(
+                "DESCRIPTION"       => "description",
+                "LICENSEPOOLID"     => "cn",
+                "PRODUCTIDS"        => "productId",
+                "WINDOWSSOFTWAREIDS"=> "softwareId") as $source => $dest){
+
+            if(isset($item[$source])){
+              $entry[$dest] = array('count' => 0);
+              foreach($item[$source] as $obj){
+                $entry[$dest][] = $obj['VALUE'];
+              }
+              $entry[$dest]['count'] = (count($entry[$dest]) -1 );
+            } 
+          }
+          $data[] =$entry;
         }
-        $data[] =$entry;
+        return($data);
+      }else{
+        // No entries, but got an answer. We probably do not have any pools yet.
+        return(array());
       }
-      return($data);
     }
     return(FALSE);
   }
@@ -141,6 +145,51 @@ class opsiLicenceHandler extends opsi  {
    * LICENSES
    *******************/
 
+  
+  function listLicenses()
+  {
+    $data= array();
+    $res = $this->send_data("gosa_opsi_getAllSoftwareLicenses",$this->target,$data,TRUE);
+
+    if(isset($res['XML'][0]['ANSWER_OPSI_GETALLSOFTWARELICENSES'])){
+
+      $licenses = array();
+      if(isset($res['XML'][0]['LICENSES'][0]['HIT'])){
+        foreach($res['XML'][0]['LICENSES'][0]['HIT'] as $entry){
+
+          $item = array();
+
+          // License keys are indexed by the pool id, map it here.
+          if(isset($entry['LICENSEPOOLIDS'][0]['VALUE'])){
+            $n = strtoupper($entry['LICENSEPOOLIDS'][0]['VALUE']);
+            $entry['LICENSEKEYS'] = $entry['LICENSEKEYS'][0][$n];
+          }
+
+          // Now fake an ldap like result
+          foreach(array(
+                "BOUNDTOHOST"         => "boundToHost",
+                "LICENSEKEYS"         => "licenseKey",
+                "LICENSEPOOLIDS"      => "licensePoolId",
+                "LICENSETYPE"         => "licenseType",
+                "SOFTWARELICENSEID"   => "softwareLicenseId") as $source => $target){
+            if(isset($entry[$source])){
+              $item[$target] = array('count' => 0);
+              foreach($entry[$source] as $data){
+                $item[$target][] = $data['VALUE'];
+              }
+              $item[$target]['count'] = count($item[$target]) -1 ;
+            }
+          }
+
+          $licenses[] = $item;
+        } 
+      }
+      return($licenses);
+    }
+    return(FALSE);
+  }
+
+
   /*  
    * @brief 
    *     Create a license contract, create a software 
@@ -157,14 +206,14 @@ class opsiLicenceHandler extends opsi  {
    * @param boundToHost The name of the client the license is bound to (optional).
    * @param expirationDate The date when the license is running down (optional).
    */
-  function createLicense($poolId, $licenseId, $licenseKey,$licenseTyp = "",  
+  function createLicense($poolId, $licenseId, $licenseKey,$licenseType = "",  
         $partner = "",
         $conclusionDate = "",
         $notificationDate ="",
         $notes = "", 
         $softwareLicenseId = "",
         $maxInstallations = "",
-        $boudToHost = "",
+        $boundToHost = "",
         $expirationDate = "")
   {
 
@@ -175,7 +224,7 @@ class opsiLicenceHandler extends opsi  {
 
     // Append optional attributes 
     foreach(array("partner","conclusionDate","notificationDate","notes","softwareLicenseId",
-          "licenseTyp","maxInstallations","boudToHost","expirationDate") as $attr){
+          "licenseType","maxInstallations","boundToHost","expirationDate") as $attr){
       if(!empty($$attr)){
         $data[$attr] = $$attr;
       }
@@ -206,6 +255,40 @@ class opsiLicenceHandler extends opsi  {
   }
 
 
+  function getReservedLicensesForHost($hostId)
+  {
+    $data= array();
+    if(!empty($hostId)){
+      $data['hostId'] = htmlentities($hostId);
+    }
+    $res = $this->send_data("gosa_opsi_getReservedLicenses",$this->target,$data,TRUE);
+    if(isset($res['XML'][0]['ANSWER_OPSI_GETRESERVEDLICENSES'])){
+
+      $items = array();
+      if(isset($res['XML'][0]['LICENSES'][0]['HIT'])){
+        foreach($res['XML'][0]['LICENSES'][0]['HIT'] as $entry){
+          $item = array();
+          foreach(array(
+                "LICENSEPOOLIDS"      => "licensePoolId",
+                "SOFTWARELICENSEID"   => "softwareLicenseId") as $source => $target){
+            if(isset($entry[$source])){
+
+              $item[$target] = array('count' => 0);
+              foreach($entry[$source] as $data){
+                $item[$target][] = $data['VALUE'];
+              }
+              $item[$target]['count'] = count($item[$target]) -1 ;
+            }
+          }
+          $items[]  = $item;
+        }
+      }
+      return($items);
+    }
+    return(FALSE);
+  }
+
+
   /* 
    * @brief Returns softwareLicenseId, notes, licenseKey, hostId and licensePoolId for optional given licensePoolId and hostId
    * @param hostid Something like client_1.intranet.mydomain.de (optional).
@@ -221,28 +304,30 @@ class opsiLicenceHandler extends opsi  {
       $data['licensePoolId'] = htmlentities($licensePoolId);
     }
 
-    $res = $this->send_data("gosa_opsi_getSoftwareLicenseUsages_listOfHashes",$this->target,$data,TRUE);
-    if(isset($res['XML'][0]['ANSWER_OPSI_GETSOFTWARELICENSEUSAGES_LISTOFHASHES'])){
+    $res = $this->send_data("gosa_opsi_getSoftwareLicenseUsages",$this->target,$data,TRUE);
+    if(isset($res['XML'][0]['ANSWER_OPSI_GETSOFTWARELICENSEUSAGES'])){
 
       $items = array();
-      foreach($res['XML'][0]['RESULT'][0]['HIT'] as $entry){
-        $item = array();
-        foreach(array(
-              "HOSTID"              => "hostId",
-              "LICENSEKEY"          => "licenseKey",
-              "LICENSEPOOLID"       => "licensePoolId",
-              "NOTES"               => "notes",
-              "SOFTWARELICENSEID"   => "softwareLicenseId") as $source => $target){
-          if(isset($entry[$source])){
-
-            $item[$target] = array('count' => 0);
-            foreach($entry[$source] as $data){
-              $item[$target][] = $data['VALUE'];
+      if(isset($res['XML'][0]['RESULT'][0]['HIT'])){
+        foreach($res['XML'][0]['RESULT'][0]['HIT'] as $entry){
+          $item = array();
+          foreach(array(
+                "HOSTID"              => "hostId",
+                "LICENSEKEY"          => "licenseKey",
+                "LICENSEPOOLID"       => "licensePoolId",
+                "NOTES"               => "notes",
+                "SOFTWARELICENSEID"   => "softwareLicenseId") as $source => $target){
+            if(isset($entry[$source])){
+
+              $item[$target] = array('count' => 0);
+              foreach($entry[$source] as $data){
+                $item[$target][] = $data['VALUE'];
+              }
+              $item[$target]['count'] = count($item[$target]) -1 ;
             }
-            $item[$target]['count'] = count($item[$target]) -1 ;
           }
+          $items[]  = $item;
         }
-        $items[]  = $item;
       }
       return($items);
     }
@@ -267,6 +352,41 @@ class opsiLicenceHandler extends opsi  {
   }
 
 
+  /* @brief   Reserve a software license to a host
+   * @param   softwareLicenseId 
+   * @param   hostId Something like client_1.intranet.mydomain.de
+   */
+  function reserverLicenseForHost($softwareLicenseId,$hostId)
+  {
+    $data= array();
+    $data['softwareLicenseId'] = htmlentities($softwareLicenseId);
+    $data['hostId'] = htmlentities($hostId);
+    $res = $this->send_data("gosa_opsi_boundHostToLicense",$this->target,$data,TRUE);
+    if(isset($res['XML'][0]['ANSWER_OPSI_BOUNDHOSTTOLICENSE'])){
+      return(TRUE);
+    }
+    return(FALSE);
+  }
+
+
+  /* 
+   * @brief   Remove software licnese reservation for a host.
+   * @param   softwareLicenseId
+   * @param   hostid Something like client_1.intranet.mydomain.de
+   */
+  function removeLicenseReservationFromHost($softwareLicenseId,$hostId)
+  {
+    $data= array();
+    $data['softwareLicenseId'] = htmlentities($softwareLicenseId);
+    $data['hostId'] = htmlentities($hostId);
+    $res = $this->send_data("gosa_opsi_unboundHostFromLicense",$this->target,$data,TRUE);
+    if(isset($res['XML'][0]['ANSWER_OPSI_UNBOUNDHOSTFROMLICENSE'])){
+      return(TRUE);
+    }
+    return(FALSE);
+  }
+
+
   /* 
    * @brief   Unassign a software license from a host.
    * @param   hostid Something like client_1.intranet.mydomain.de
@@ -276,7 +396,7 @@ class opsiLicenceHandler extends opsi  {
   {
     $data= array();
     $data['licensePoolId'] = htmlentities($licensePoolId);
-    $data['hostId'] = htmlentities($hostid);
+    $data['hostId'] = htmlentities($hostId);
     $res = $this->send_data("gosa_opsi_unassignSoftwareLicenseFromHost",$this->target,$data,TRUE);
     if(isset($res['XML'][0]['ANSWER_OPSI_UNASSIGNSOFTWARELICENSEFROMHOST'])){
       return(TRUE);
@@ -285,6 +405,26 @@ class opsiLicenceHandler extends opsi  {
   }
 
 
+  /* 
+   * @brief   Removes a single license from a license pool
+   *          Attention, the software license has to exists 
+   *           otherwise it will lead to an Opsi internal server error.
+   * @param softwareLicenseId
+   * @param licensePoolId
+   */
+  function removeLicenseFromPool($licensePoolId,$softwareLicenseId)
+  {
+    $data= array();
+    $data['licensePoolId'] = htmlentities($licensePoolId);
+    $data['softwareLicenseId'] = htmlentities($softwareLicenseId);
+    $res = $this->send_data("gosa_opsi_removeLicense",$this->target,$data,TRUE);
+    if(isset($res['XML'][0]['ANSWER_OPSI_REMOVELICENSE'])){
+      return(TRUE);
+    }
+    return(FALSE);
+  }
+
+
   /* 
    * @brief Unassign all software licenses from a host
    * @param hostid Something like client_1.intranet.mydomain.de
@@ -311,9 +451,12 @@ class opsiLicenceHandler extends opsi  {
   {
     $data= array();
     $data['productId'] = htmlentities($productId);
-    $res = $this->send_data("gosa_opsi_getLicenseInformationForProduct",$this->target,$data,TRUE);
-    if(isset($res['XML'][0]['ANSWER_OPSI_UNASSIGNALLSOFTWARELICENSESFROMHOST'])){
-      return($res['XML'][0]);
+    $res = $this->send_data("gosa_opsi_getSoftwareLicenseUsagesForProductId",$this->target,$data,TRUE);
+    if(isset($res['XML'][0]['ANSWER_OPSI_GETSOFTWARELICENSEUSAGESFORPRODUCTID'])){
+      if(isset($res['XML'][0]['RESULT'][0]['HIT'])){
+        return($res['XML'][0]['RESULT'][0]['HIT']);
+      } 
+      return(array());
     }
     return(FALSE);
   }