Code

Updated licenseLists to use the new sortable listing
[gosa.git] / gosa-plugins / opsi / admin / opsiLicenses / class_licenseUsageByHost.inc
index 4618df364ef36fac0139f2a198c11476d46e45a0..ff37456fa04fec76902eae495ff99253c0da7173 100644 (file)
@@ -47,6 +47,27 @@ class licenseUsageByHost extends plugin
 
     // Extract pool name out of the fake dn.
     $this->init();
+
+    // Prepare lists
+    $this->reservedList = new sortableListing();
+    $this->reservedList->setDeleteable(true);
+    $this->reservedList->setEditable(false);
+    $this->reservedList->setWidth("100%");
+    $this->reservedList->setHeight("220px");
+    $this->reservedList->setColspecs(array('200px','*'));
+    $this->reservedList->setHeader(array(_("Pool"),_("License ID")));
+    $this->reservedList->setDefaultSortColumn(1);
+    $this->reservedList->setAcl('rwcdm'); // All ACLs, we filter on our own here.
+
+    $this->usedList = new sortableListing();
+    $this->usedList->setDeleteable(false);
+    $this->usedList->setEditable(false);
+    $this->usedList->setWidth("100%");
+    $this->usedList->setHeight("220px");
+    $this->usedList->setColspecs(array('200px','*'));
+    $this->usedList->setHeader(array(_("Key"),_("Pool"),_("License ID")));
+    $this->usedList->setDefaultSortColumn(1);
+    $this->usedList->setAcl('rwcdm'); // All ACLs, we filter on our own here.
   }
 
   
@@ -62,6 +83,7 @@ class licenseUsageByHost extends plugin
       // Get license usage  
       $res = $this->si->getLicenseUsage($this->cn);
       if($this->si->is_error()){
+        msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
         $this->init_successfull = FALSE;
         return;
       }
@@ -70,23 +92,27 @@ class licenseUsageByHost extends plugin
       // Get reservations
       $res = $this->si->getReservedLicensesForHost($this->cn);
       if($this->si->is_error()){
+        msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
         $this->init_successfull = FALSE;
         return;
       }
       foreach($res as $pool){
-        $this->reservedLicenses[] = $pool['licensePoolId'][0];
+        $l = $pool['softwareLicenseId'][0];
+        $this->reservedLicenses[$l] = $l;
       }
       $this->init_reservedLicenses = $this->reservedLicenses;
 
       // Get list of license pools 
-      $res = $this->si->listPools();
+      $res = $this->si->listLicenses();
       if($this->si->is_error()){
+        msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
         $this->init_successfull = FALSE;
         return;
       }
       $this->availableLicenses = array();
-      foreach($res as $pool){
-        $this->availableLicenses[] = $pool['cn'][0];
+      foreach($res as $license){
+        if(!isset($license['licensePoolId'])) $license['licensePoolId'][0] = '-';
+        $this->availableLicenses[$license['softwareLicenseId'][0]] = $license;
       }
 
       $this->init_successfull = TRUE;
@@ -96,6 +122,8 @@ class licenseUsageByHost extends plugin
 
   function execute()
   {
+    plugin::execute();
+
     // Handle initialization failures.
     if(isset($_POST['retry_init'])) $this->init();
     if(!$this->init_successfull){
@@ -103,29 +131,32 @@ class licenseUsageByHost extends plugin
       $smarty->assign("init_successfull", $this->init_successfull);
       return($smarty->fetch(get_template_path('licenseGeneric.tpl',TRUE,dirname(__FILE__))));
     }
-    
+   
     // Create usage list
-    $list = new divSelectBox("licenseUsage");
-    $list->setHeight(150);
+    $data = array();
+    $this->usedList->setAcl($this->getacl('hostId'));
     foreach($this->licenseUses as $license){
-      $f3 = array("string" => $license['licenseKey'][0]);
-      $f2 = array("string" => $license['licensePoolId'][0]);
-      $f4 = array("string" => $license['softwareLicenseId'][0]);
-      $list->addEntry(array($f2,$f3,$f4));
+        $data[] = array('data' => array(
+                    $license['licenseKey'][0],
+                    $license['licensePoolId'][0],
+                    $license['softwareLicenseId'][0]));
     } 
+    $this->usedList->setListData($data,$data);
+    $this->usedList->update();
 
     // Create reserved list
-    $list2 = new divSelectBox("reservedLicenses");
-    $list2->setHeight(150);
+    $data = $lData = array();
+    $this->reservedList->setAcl($this->getacl('boundToHost'));
     foreach($this->reservedLicenses as $key => $license){
-      $action = "<input class='center' type='image' src='images/lists/trash.png' 
-        name='removeReservation_{$key}'>";
-      $f1 = array("string" => $license);
-      $f4 = array("string" => $action,
-                  "attach" => "style='border-right:0px; width:16px;'");
-      $list2->addEntry(array($f1,$f4));
+        $l = $this->availableLicenses[$key];
+        $data[$key] = $key;
+        $lData[$key] = array('data' => array(
+                    $l['softwareLicenseId'][0],
+                    $l['licensePoolId'][0]));
     } 
-
+    $this->reservedList->setListData($data,$lData);
+    $this->reservedList->update();
+    
     $smarty = get_smarty();
 
     // Assign ACls 
@@ -137,10 +168,18 @@ class licenseUsageByHost extends plugin
       $smarty->assign($attr,$this->$attr);
     }
 
-    $smarty->assign("licenseUses", $list->DrawList());
-    $smarty->assign("licenseReserved", $list2->DrawList());
+    // build up a available licenses list. 
+    $licenses = array();
+    foreach($this->availableLicenses as $key => $license){
+      if(!in_array($license['softwareLicenseId'][0], $this->reservedLicenses)){
+        $licenses[$key] = $license['softwareLicenseId'][0]." [".$license['licensePoolId'][0]."]";
+      }
+    }
+
+    $smarty->assign("licenseUses", $this->usedList->render());
+    $smarty->assign("licenseReserved", $this->reservedList->render());
     $smarty->assign("init_successfull", $this->init_successfull);
-    $smarty->assign("availableLicenses", array_diff($this->availableLicenses, $this->reservedLicenses));
+    $smarty->assign("availableLicenses", $licenses);
     $smarty->assign("initially_was_account", $this->initially_was_account);
     return($smarty->fetch(get_template_path('licenseUsageByHost.tpl',TRUE,dirname(__FILE__))));
   }
@@ -153,22 +192,23 @@ class licenseUsageByHost extends plugin
     if(isset($_POST['opsiLicenseUsagePosted'])){
       plugin::save_object();  
   
-      // Check if we've to remove reservations 
-      foreach($_POST as $name => $value){
-        if(preg_match("/^removeReservation_/", $name)){
-          $id = preg_replace("/^removeReservation_(.*)_.$/", "\\1" ,$name);
-          if(isset($this->reservedLicenses[$id])) {
-            unset($this->reservedLicenses[$id]);
-          }
-          break;
+      if(preg_match("/w/i",$this->getacl('boundToHost'))){
+
+        $this->reservedList->save_object();
+        $action = $this->reservedList->getAction();
+        if($action['action'] == "delete") {
+            $id = $this->reservedList->getKey($action['targets'][0]);
+            if(isset($this->reservedLicenses[$id])) {
+                unset($this->reservedLicenses[$id]);
+            }
         }
-      } 
 
-      // Check if we've to add reservations
-      if(isset($_POST['availableLicense']) && isset($_POST['addReservation'])){
-        $id = get_post('availableLicense');
-        if(isset($this->availableLicenses[$id])){
-          $this->reservedLicenses[] =  $this->availableLicenses[$id];
+        // Check if we've to add reservations
+        if(isset($_POST['availableLicense']) && isset($_POST['addReservation'])){
+          $id = get_post('availableLicense');
+          if(isset($this->availableLicenses[$id]) && !in_array($this->availableLicenses[$id],$this->reservedLicenses)){
+            $this->reservedLicenses[$id] =  $this->availableLicenses[$id]['softwareLicenseId'][0];
+          }
         }
       }
     }
@@ -189,15 +229,15 @@ class licenseUsageByHost extends plugin
     $del = array_diff($this->init_reservedLicenses, $this->reservedLicenses);
     $add = array_diff($this->reservedLicenses, $this->init_reservedLicenses);
 
-    foreach($del as $pool){
-      $this->si->removeLicenseFromHost($pool, $this->cn);
+    foreach($del as $license){
+      $this->si->removeLicenseReservationFromHost($license, $this->cn);
       if($this->si->is_error()){
         msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
       }
     }
 
-    foreach($add as $pool){
-      $this->si->addLicenseToHost($pool, $this->cn);
+    foreach($add as $license){
+      $this->si->reserverLicenseForHost($license, $this->cn);
       if($this->si->is_error()){
         msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG);
       }
@@ -211,16 +251,16 @@ class licenseUsageByHost extends plugin
   static function plInfo()
   {
     return (array(
-          "plShortName"   => _("Usage"),
-          "plDescription" => _("License usage"),
+          "plShortName"   => _("Usage by host"),
+          "plDescription" => _("License usage by host"),
           "plSelfModify"  => FALSE,
           "plDepends"     => array(),
-          "plPriority"    => 1,
+          "plPriority"    => 13,
           "plSection"     => array("administration"),
           "plCategory"    => array("opsi"),
           "plProvidedAcls"=> array(
-            "cn"                => _("Name"),
-            "description" => _("Description"))
+            "hostId" => _("Used by"),
+            "boundToHost" => _("License reservation"))
           ));
   }
 }