Code

Added action install
[gosa.git] / gosa-plugins / opsi / admin / opsi / class_opsigeneric.inc
index a36c388469a19ff046f58a5d547a8866f7356698..d3c2743bf6c21cfb49053c03ac78f014c7ad8974 100644 (file)
@@ -19,14 +19,23 @@ class opsigeneric extends plugin
 
   private $init_failed = FALSE;
 
+  private $parent_mode = TRUE;
+  private $is_installed = FALSE;
+
+  public $mac          = "";
+  public $note         = "";
+  public $description  = "";
+
+  public $attributes = array("mac","note","description");
+
   public function __construct($config,$hostId)
   {
     $this->opsi = new opsi($config); 
     $this->is_account =TRUE;
-    $this->initially_was_account = TRUE;
   
     /* Get hostId */
     if($hostId != "new"){
+      $this->initially_was_account = TRUE;
       $this->hostId = preg_replace("/^opsi:=([^,]*),.*$/","\\1",$hostId);
     }
   
@@ -39,6 +48,19 @@ class opsigeneric extends plugin
     $err = FALSE;
     $this->init_failed = FALSE;
 
+    if($this->hostId != "new"){
+      $list = $this->opsi->list_clients($this->hostId);
+      $err |= $this->opsi->is_error();
+      foreach($list as $entry){
+        if($entry['NAME'][0]['VALUE'] == $this->hostId){  
+          foreach(array("description" => "DESCRIPTION","mac" => "MAC", "note" => "NOTES") as $des => $src){
+            $this->$des = $entry[$src][0]['VALUE'];
+          } 
+          break;
+        }
+      }
+    }
+
     /* Get product settings */     
     if(!$err){
       $this->a_availableNetbootProducts = $this->opsi->get_netboot_products();
@@ -52,11 +74,13 @@ class opsigeneric extends plugin
     /* Get selected products */
     if(!$err && !empty($this->hostId)) {
       $tmp = array_keys($this->opsi->get_netboot_products($this->hostId));
-      $this->s_selectedNetbootProduct = $tmp[0];
+      if(count($tmp)){
+        $this->s_selectedNetbootProduct = $tmp[0];
+      }
       $err |= $this->opsi->is_error();
     }
     if(!$err && !empty($this->hostId)) {
-      $tmp = $this->opsi->get_local_products($this->hostId);  
+      $tmp = $this->opsi->get_local_products($this->hostId); 
       $err |= $this->opsi->is_error();
       $this->a_selectedLocalProducts = $tmp;
     }
@@ -88,23 +112,40 @@ class opsigeneric extends plugin
     }
   }
 
+  public function check()
+  {
+    $messages = plugin::check();
+    if(!preg_match("/\./",$this->hostId)){
+      $messages[] = msgPool::invalid(_("Name"),$this->hostId,"",_("The client name must contain a domain part (e.g. '.company.de')."));
+    }
+    if(!tests::is_mac($this->mac) || empty($this->mac)){
+      $messages[] = msgPool::invalid(_("MAC address"),$this->mac,"","00:0C:7F:31:33:F1");
+    }
+    return($messages);
+  }
+
   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();
+
+    foreach($this->attributes as $attr){
+      $smarty->assign($attr,$this->$attr);
+    }
+
+    $smarty->assign("parent_mode", $this->parent_mode);
+    $smarty->assign("is_installed", $this->is_installed);
     $smarty->assign("init_failed",FALSE);
     $divSLP = new divSelectBox();
     $divALP = new divSelectBox();
@@ -145,11 +186,11 @@ class opsigeneric extends plugin
       $divSLP->AddEntry(array($name_tab,$desc_tab,$opt_tab));
     }
 
+    ksort($this->a_availableNetbootProducts);
+    $smarty->assign("hostId", $this->hostId);
     $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__))));
   }
@@ -157,15 +198,43 @@ class opsigeneric extends plugin
 
   public function save()
   {
+    /* Check if this a new opsi client 
+        -Do we have to create this client first?
+     */
+    if(!$this->initially_was_account && $this->is_account){
+      $res = $this->opsi->add_client($this->hostId,$this->mac,$this->note,$this->description);
+      if($this->opsi->is_error()){
+        msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);    
+        return;
+      }
+    }
+    $this->opsi->modify_client($this->hostId,$this->mac,$this->note,$this->description);
+    if($this->opsi->is_error()){
+      msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);
+      return;
+    }
+
     $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);
+      if($this->opsi->is_error()){
+        msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);    
+        return;
+      }
     }
     foreach($add as $name => $data){
       $this->opsi->add_product_to_client($name,$this->hostId);
+      if($this->opsi->is_error()){
+        msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);    
+        return;
+      }
       $this->opsi->set_product_properties($name,$data['CFG'],$this->hostId);
+      if($this->opsi->is_error()){
+        msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);    
+        return;
+      }
     }
 
     foreach($this->a_selectedLocalProducts as $name => $data){
@@ -173,24 +242,42 @@ class opsigeneric extends plugin
       $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->opsi->is_error()){
+          msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);    
+          return;
+        }
       }
     }
-  
+
     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);
+        if($this->opsi->is_error()){
+          msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);    
+          return;
+        }
+      }
+      $this->opsi->add_product_to_client($this->s_selectedNetbootProduct,$this->hostId);
+      if($this->opsi->is_error()){
+        msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);    
+        return;
       }
     }
   }
 
   public function remove_from_parent()
-  {}
+  {
+    $this->opsi->del_client($this->hostId);
+    if($this->opsi->is_error()){
+      msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);
+      return;
+    }
+  }
 
 
   public function save_object()
   {
-    if(isset($_POST['reinit'])){
+    if(isset($_POST['reinit']) && $this->init_failed){
       $this->init();
     }
 
@@ -207,7 +294,25 @@ class opsigeneric extends plugin
       $this->dialog = NULL;
     }
 
-    if(isset($_POST['opsigeneric'])){
+    if(isset($_POST['opsigeneric_posted'])){
+
+      plugin::save_object();
+
+      if(isset($_POST['hostId']) && $this->parent_mode){
+        $this->hostId = get_post('hostId');
+      }
+
+      /* Send actions like 'install' or 'wake' to the si server 
+       */
+      if(isset($_POST['opsi_action']) && isset($_POST['opsi_trigger_action']) && $this->parent_mode){
+        $action = $_POST['opsi_action'];
+        if(in_array($action,array("wake","install"))){
+          $this->opsi->send_action($action,$this->hostId,$this->mac);
+          if($this->opsi->is_error()){
+            msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);
+          }
+        }
+      }
 
       if(isset($_POST['opsi_netboot_product'])){
         $SNP = trim($_POST['opsi_netboot_product']);
@@ -243,6 +348,13 @@ class opsigeneric extends plugin
     }
   }
 
+
+  function install_client()
+  {
+
+  }
+
+
   /* Return plugin informations for acl handling */
   static function plInfo()
   {