Code

Update of opsi classes
authorhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Wed, 20 Aug 2008 13:56:54 +0000 (13:56 +0000)
committerhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Wed, 20 Aug 2008 13:56:54 +0000 (13:56 +0000)
- We are now able to add and remove products.
- We can configure products.
- BUT. I was able to add multiple netboot products..

git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@12254 594d385d-05f5-0310-b6e9-bd551577e9d8

gosa-plugins/opsi/admin/opsi/class_opsi.inc
gosa-plugins/opsi/admin/opsi/class_opsi_generic.inc
gosa-plugins/opsi/admin/opsi/class_opsi_product_config.inc [new file with mode: 0644]
gosa-plugins/opsi/admin/opsi/generic.tpl
gosa-plugins/opsi/admin/opsi/properties.tpl [new file with mode: 0644]

index 7be570879b615fb3d0d59d15014f40fe9c2ea8b8..ebbbf5941e0bcf2b7b30c03e3df83b4e86c81d2d 100644 (file)
@@ -101,7 +101,7 @@ class opsi extends gosaSupportDaemon
 
     $res    = $this->send_data("gosa_opsi_get_netboot_products",$this->target,$data,TRUE);
     $items  = array();
-    if($res['XML']['ITEM']['PRODUCTID']){
+    if(isset($res['XML']['ITEM']['PRODUCTID'])){
       if(!is_array($res['XML']['ITEM']['PRODUCTID'])){
         $items[$res['XML']['ITEM']['PRODUCTID']]['NAME'] = $res['XML']['ITEM']['PRODUCTID'];
         $items[$res['XML']['ITEM']['PRODUCTID']]['DESC'] = $res['XML']['ITEM']['DESCRIPTION'];
@@ -182,30 +182,78 @@ class opsi extends gosaSupportDaemon
     @param
     @return            
    */
-  public function set_product_properties()
+  public function set_product_properties($productId,$cfg,$hostId = "")
   {
-    /*         <xml> 
-        <header>gosa_opsi_set_product_properties</header> 
-        <source>GOSA</source> 
-        <target>00:01:6c:9d:b9:fa</target> 
-        <productId>firefox</productId> 
-        <item>
-        <name>askbeforeinst</name>
-        <value>false</value>
-        </item>
-        </xml>
-        <xml> 
-        <header>gosa_opsi_set_product_properties</header> 
-        <source>GOSA</source> 
-        <target>00:01:6c:9d:b9:fa</target> 
-        <hostId>limux-cl-2.intranet.gonicus.de</hostId> 
-        <productId>firefox</productId> 
-        <item> 
-        <name>askbeforeinst</name> 
-        <value>false</value> 
-        </item> 
-        </xml> 
+    $data = array("productId" => $productId);
+
+    /* Append host attribute to query data 
      */
+    if(!empty($hostId)){
+      $data['hostId'] = trim($hostId);
+    }
+   
+    /* Check parameter */ 
+    if(empty($productId)){
+      trigger_error("No valid product id given, check parameter 1.");
+      return(array());
+    }
+
+    if(!count($cfg)) return;
+    
+    /* Add properties */
+    $data['item'] = array();
+    foreach($cfg as $name => $value){
+      $data['item'][] = "<name>".$name."</name><value>".$value."</value>";
+    }  
+
+    /* Query SI server */
+    $res    = $this->send_data("gosa_opsi_set_product_properties",$this->target,$data,TRUE);
+  }
+
+
+  /*! \brief           Adds a given product to a client.
+    @param
+    @return            
+   */
+  public function add_product_to_client($productId,$hostId)
+  {
+    $data = array("productId" => $productId,"hostId" => $hostId);
+
+    /* Check parameter */ 
+    if(empty($productId)){
+      trigger_error("No valid product id given, check parameter 1.");
+      return;
+    }
+    if(empty($hostId)){
+      trigger_error("No valid host id given, check parameter 2.");
+      return;
+    }
+
+    /* Query SI server */
+    $res    = $this->send_data("gosa_opsi_add_product_to_client",$this->target,$data,TRUE);
+  }
+
+
+  /*! \brief      Removes a given product from a client.
+    @param
+    @return
+   */
+  public function del_product_from_client($productId,$hostId)
+  {
+    $data = array("productId" => $productId,"hostId" => $hostId);
+
+    /* Check parameter */ 
+    if(empty($productId)){
+      trigger_error("No valid product id given, check parameter 1.");
+      return;
+    }
+    if(empty($hostId)){
+      trigger_error("No valid host id given, check parameter 2.");
+      return;
+    }
+
+    /* Query SI server */
+    $res    = $this->send_data("gosa_opsi_del_product_from_client",$this->target,$data,TRUE);
   }
 
 
@@ -241,6 +289,8 @@ class opsi extends gosaSupportDaemon
   }
 
 
+
+
   /*! \brief           Returns a list of all opsi clients.
     @param
     @return            
@@ -319,40 +369,6 @@ class opsi extends gosaSupportDaemon
   }
 
 
-  /*! \brief           Adds a given product to a client.
-    @param
-    @return            
-   */
-  public function add_product_to_client()
-  {
-    /*  <xml> 
-        <header>gosa_opsi_add_product_to_client</header> 
-        <source>GOSA</source> 
-        <target>00:01:6c:9d:b9:fa</target> 
-        <macaddress>00:11:25:4b:8c:e5</macaddress> 
-        <hostId>limux-cl-2.intranet.gonicus.de</hostId> 
-        <productId>winxppro</productId> 
-        </xml>
-     */
-  }
-
-
-  /*! \brief      Removes a given product from a client.
-    @param
-    @return
-   */
-  public function del_product_from_client()
-  {
-    /* <xml> 
-       <header>gosa_opsi_del_product_from_client</header> 
-       <source>GOSA</source> 
-       <target>00:01:6c:9d:b9:fa</target> 
-       <hostId>limux-cl-1.intranet.gonicus.de</hostId> 
-       <macaddress>00:11:25:4b:8c:e5</macaddress> 
-       <productId>softprod</productId>  
-       </xml>
-     */
-  }
 }
 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
 ?>
index a4013e4d52c3e3dc13bbef48213a9c45716f9b95..65178cbc126f1133ae17aa0b0b9f405233418366 100644 (file)
@@ -9,39 +9,101 @@ class opsi_generic extends plugin
    */
   private $a_availableNetbootProducts = array();
   private $s_selectedNetbootProduct = "";  
+  private $s_initial_selectedNetbootProduct = "";  
 
   /* Contains a list of all available local products
    */
   private $a_availableLocalProducts = array();
   private $a_selectedLocalProducts = array();
-  
+  private $a_initial_selectedLocalProducts = array();
+
+  private $init_failed = FALSE;
+
   public function __construct($config,$hostId)
   {
     $this->opsi = new opsi($config); 
-    
+    $this->is_account =TRUE;
+    $this->initially_was_account = TRUE;
+  
     /* Get hostId */
     $this->hostId = preg_replace("/^opsi:=([^,]*),.*$/","\\1",$hostId);
+  
+    /* Try to plugin */
+    $this->init();
+  }
+  
+  private function init()
+  {
+    $err = FALSE;
+    $this->init_failed = FALSE;
 
     /* Get product settings */     
-    $this->a_availableNetbootProducts = $this->opsi->get_netboot_products();
-    $this->a_availableLocalProducts   = $this->opsi->get_local_products();
-  
+    if(!$err){
+      $this->a_availableNetbootProducts = $this->opsi->get_netboot_products();
+      $err |= $this->opsi->is_error();
+    }
+    if(!$err) {
+      $this->a_availableLocalProducts   = $this->opsi->get_local_products();
+      $err |= $this->opsi->is_error();
+    }
+
     /* Get selected products */
-    $tmp = array_keys($this->opsi->get_netboot_products($this->hostId));  
-    $this->s_selectedNetbootProduct = $tmp;
-    $tmp = $this->opsi->get_local_products($this->hostId);  
-    $this->a_selectedLocalProducts = $tmp;
+    if(!$err) {
+      $tmp = array_keys($this->opsi->get_netboot_products($this->hostId));
+      $this->s_selectedNetbootProduct = $tmp[0];
+      $err |= $this->opsi->is_error();
+    }
+    if(!$err) {
+      $tmp = $this->opsi->get_local_products($this->hostId);  
+      $err |= $this->opsi->is_error();
+      $this->a_selectedLocalProducts = $tmp;
+    }
 
     /* Load product configuration */
-    foreach($this->a_selectedLocalProducts as $name => $data){
-      $CFG = $this->opsi->get_product_properties($name,$this->hostId);
-      $this->a_selectedLocalProducts[$name]['CFG'] = $CFG;
+    if(!$err) {
+      foreach($this->a_selectedLocalProducts as $name => $data){
+        $CFG = $this->opsi->get_product_properties($name,$this->hostId);
+        $err |= $this->opsi->is_error();
+        $this->a_selectedLocalProducts[$name]['CFG'] = $CFG;
+      }
+    }
+  
+    /* Check if everything went fine else reset everything and display a retry button 
+     */
+    if($err){
+      $this->a_availableNetbootProducts = array();
+      $this->s_selectedNetbootProduct = "";  
+      $this->s_initial_selectedNetbootProduct = "";  
+      $this->a_availableLocalProducts = array();
+      $this->a_selectedLocalProducts = array();
+      $this->a_initial_selectedLocalProducts = array();
+      $this->init_failed = TRUE;
+    }else{
+
+      /* Remember initial settings */ 
+      $this->a_initial_selectedLocalProducts = $this->a_selectedLocalProducts;
+      $this->s_initial_selectedNetbootProduct = $this->s_selectedNetbootProduct;
     }
   }
 
   public function execute()
   {
+    if($this->init_failed){
+      
+      $smarty = get_smarty();
+      $smarty->assign("init_failed",TRUE);
+      $smarty->assign("message",$this->opsi->get_error());
+      return($smarty->fetch(get_template_path("generic.tpl",TRUE,dirname(__FILE__))));
+    }  
+
+
+    if(is_object($this->dialog)){
+      $this->dialog->save_object();
+      return($this->dialog->execute());
+    }
+
     $smarty = get_smarty();
+    $smarty->assign("init_failed",FALSE);
     $divSLP = new divSelectBox();
     $divALP = new divSelectBox();
 
@@ -54,7 +116,7 @@ class opsi_generic extends plugin
       $add_tab  = array("string"   => "<input type='image' src='images/back.png' name='add_lp_".$name."'>");
       $name_tab = array("string"   => $name);
       $desc_tab = array("string"   => "<div style='height: 14px;overflow:hidden;'>".$data['DESC']."</div>",
-                    "attach"   => "title='".$data['DESC']."' style='border-right:0px;'");
+          "attach"   => "title='".$data['DESC']."' style='border-right:0px;'");
       $divALP->AddEntry(array($add_tab,$name_tab,$desc_tab));
     }
 
@@ -62,7 +124,7 @@ class opsi_generic extends plugin
      */
     ksort($this->a_selectedLocalProducts);
     foreach($this->a_selectedLocalProducts as $name => $data){
-      
+
       $name_tab = array("string"   => $name);
       $desc_tab = array(
           "string" => "<div style='height: 14px;overflow:hidden;'>".$data['DESC']."</div>",
@@ -75,7 +137,7 @@ class opsi_generic extends plugin
         $edit = "<input type='image' src='images/lists/edit.png' name='edit_lp_".$name."'>";
       }
       $del  = "<input type='image' src='images/lists/trash.png' name='del_lp_".$name."'>";  
-  
+
       $opt_tab  = array("string" => $edit.$del,
           "attach" => "style='border-right:0px; width: 40px; text-align:right;'");
       $divSLP->AddEntry(array($name_tab,$desc_tab,$opt_tab));
@@ -84,13 +146,65 @@ class opsi_generic extends plugin
     $smarty->assign("divSLP", $divSLP->DrawList());
     $smarty->assign("divALP", $divALP->DrawList());
     $smarty->assign("SNP", $this->s_selectedNetbootProduct);
+  
+    ksort($this->a_availableNetbootProducts);
     $smarty->assign("ANP", $this->a_availableNetbootProducts);
     return($smarty->fetch(get_template_path("generic.tpl",TRUE,dirname(__FILE__))));
   }
 
 
+  public function save()
+  {
+    $add = array_diff_assoc($this->a_selectedLocalProducts,$this->a_initial_selectedLocalProducts);
+    $del = array_diff_assoc($this->a_initial_selectedLocalProducts,$this->a_selectedLocalProducts);
+
+    foreach($del as $name => $data){
+      $this->opsi->del_product_from_client($name,$this->hostId);
+    }
+    foreach($add as $name => $data){
+      $this->opsi->add_product_to_client($name,$this->hostId);
+      $this->opsi->set_product_properties($name,$data['CFG'],$this->hostId);
+    }
+
+    foreach($this->a_selectedLocalProducts as $name => $data){
+      if(isset($del[$name]) || isset($add[$name])) continue;
+      $diff = array_diff($data['CFG'],$this->a_initial_selectedLocalProducts[$name]['CFG']);
+      if(count($diff)){
+        $this->opsi->set_product_properties($name,$diff,$this->hostId);
+      }
+    }
+  
+    if($this->s_selectedNetbootProduct != $this->s_initial_selectedNetbootProduct){
+      if(!empty($this->s_initial_selectedNetbootProduct)){
+        $this->opsi->del_product_from_client($this->s_initial_selectedNetbootProduct,$this->hostId);
+        $this->opsi->add_product_to_client($this->s_selectedNetbootProduct,$this->hostId);
+      }
+    }
+  }
+
+  public function remove_from_parent()
+  {}
+
+
   public function save_object()
   {
+    if(isset($_POST['reinit'])){
+      $this->init();
+    }
+
+    if(isset($_POST['cancel_properties']) && is_object($this->dialog)){
+      $this->dialog = NULL;
+    }
+    if(isset($_POST['save_properties']) && ($this->dialog instanceof opsi_product_config)){
+      $this->dialog->save_object();
+      $pro = $this->dialog->get_product();
+      $CFG = $this->dialog->get_cfg();
+      if(isset($this->a_selectedLocalProducts[$pro])){
+        $this->a_selectedLocalProducts[$pro]['CFG'] = $CFG;
+      }
+      $this->dialog = NULL;
+    }
+
     if(isset($_POST['opsi_generic'])){
 
       if(isset($_POST['opsi_netboot_product'])){
@@ -99,7 +213,7 @@ class opsi_generic extends plugin
           $this->s_selectedNetbootProduct = $SNP;
         }
       }
-    
+
       foreach($_POST as $name => $value){
         if(preg_match("/^add_lp_/",$name)){
           $product = preg_replace("/^add_lp_(.*)_.$/","\\1",$name);
@@ -119,7 +233,8 @@ class opsi_generic extends plugin
         }
         if(preg_match("/^edit_lp_/",$name)){
           $product = preg_replace("/^edit_lp_(.*)_.$/","\\1",$name);
-          $this->dialog = new opsi_product_config($product,$this->hostId);
+          $this->dialog = new opsi_product_config($this->config,
+              $product,$this->a_selectedLocalProducts[$product]['CFG'],$this->hostId);
           break;
         }
       }   
diff --git a/gosa-plugins/opsi/admin/opsi/class_opsi_product_config.inc b/gosa-plugins/opsi/admin/opsi/class_opsi_product_config.inc
new file mode 100644 (file)
index 0000000..5a949bb
--- /dev/null
@@ -0,0 +1,54 @@
+<?php
+
+class opsi_product_config extends plugin
+{
+  private $product;
+  private $cfg;
+  private $hostId;
+
+  public function __construct($config,$product,$cfg = array() ,$hostId = "")
+  {
+    $this->config  = $config;
+    $this->product = $product;
+    $this->cfg     = $cfg;
+    $this->hostId  = $hostId;
+  }
+
+  public function execute()
+  {
+    $smarty = get_smarty();
+    $smarty->assign("cfg",$this->cfg);
+    return($smarty->fetch(get_template_path("properties.tpl",TRUE,dirname(__FILE__))));
+  }
+
+  public function save_object()
+  {
+    foreach($this->cfg as $name => $value){
+      if(isset($_POST['value_'.$name])){
+        $this->cfg[$name] = validate(get_post('value_'.$name));
+      }
+    }
+  }
+
+  public function get_cfg() 
+  {
+    return($this->cfg);
+  }
+
+  public function get_product()
+  {
+    return($this->product);
+  }
+
+  public function get_hostId()
+  {
+    return($this->hostId);
+  }
+}
+
+
+
+
+
+// vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
+?>
index abc2e862a7321e9b1a17a50dde1d4a1de3c54b74..6497c8e4c7e274edbd900ca7a8f7a089240a271e 100644 (file)
@@ -1,6 +1,13 @@
 
 <h2>Opsi host</h2>
 
+{if $init_failed}
+
+<font style='color: #FF0000;'>{msgPool type=siError p=$message}</font>
+
+<input type='submit' name='reinit' value="{t}Retry{/t}">
+
+{else}
 
 <table style="width: 100%;">
  <tr>
@@ -29,3 +36,4 @@
  </tr>
 </table> 
 <input type='hidden' name='opsi_generic' value='1'>
+{/if}
diff --git a/gosa-plugins/opsi/admin/opsi/properties.tpl b/gosa-plugins/opsi/admin/opsi/properties.tpl
new file mode 100644 (file)
index 0000000..4c7a8e5
--- /dev/null
@@ -0,0 +1,16 @@
+<h2>Opsi product properties</h2>
+
+<table>
+{foreach from=$cfg item=item key=key}
+       <tr>
+               <td>{$key}</td>
+               <td><input type='input' name='value_{$key}' value="{$item}"></td>
+       </tr>
+{/foreach}
+</table>
+<p class="seperator">&nbsp;</p>
+<div style='width:100%; text-align: right; padding:3px;'>
+       <input type='submit' name='save_properties' value='{msgPool type='saveButton'}'>
+       &nbsp;
+       <input type='submit' name='cancel_properties' value='{msgPool type='cancelButton'}'>
+</div>