Code

Renamed opsi class.
[gosa.git] / gosa-plugins / opsi / admin / opsi / class_opsigeneric.inc
index ea511b15a7a3514a16901ca191b609f2c97bf098..acc0789f2bd4ad856d26d7166d891ab141aa30a6 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 
-class opsigeneric extends plugin
+class opsiGeneric extends plugin
 {
   private $opsi;
   private $hostId;  
@@ -20,22 +20,35 @@ 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)
+  public $parent = NULL;
+
+  public function __construct($config,$hostId,&$parent = NULL)
   {
     $this->opsi = new opsi($config); 
     $this->is_account =TRUE;
-  
+    
+    /* Check if we are are part of a windows workstation 
+     */
+    $this->parent = $parent;
+    if($parent instanceof wingeneric){
+      $this->parent_mode = FALSE;
+    }
+
     /* Get hostId */
     if($hostId != "new"){
-      $this->initially_was_account = TRUE;
-      $this->hostId = preg_replace("/^opsi:=([^,]*),.*$/","\\1",$hostId);
+      if(preg_match("/^opsi:/",$hostId)){
+        $this->hostId = preg_replace("/^opsi:=([^,]*),.*$/","\\1",$hostId);
+      }elseif($this->parent instanceof wingeneric){
+        $this->hostId = $this->parent->cn;
+        $this->hostId = preg_replace('/\$$/',"",$this->hostId);
+      }
     }
   
     /* Try to plugin */
@@ -46,15 +59,17 @@ class opsigeneric extends plugin
   {
     $err = FALSE;
     $this->init_failed = FALSE;
+    $this->initially_was_account = 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){  
+        if(preg_match("/^".normalizePreg($this->hostId)."$/i",$entry['NAME'][0]['VALUE'])){
+          $this->initially_was_account = TRUE; 
           foreach(array("description" => "DESCRIPTION","mac" => "MAC", "note" => "NOTES") as $des => $src){
             $this->$des = $entry[$src][0]['VALUE'];
           } 
-
           break;
         }
       }
@@ -106,6 +121,7 @@ class opsigeneric extends plugin
     }else{
 
       /* Remember initial settings */ 
+      $this->is_account = $this->initially_was_account;
       $this->a_initial_selectedLocalProducts = $this->a_selectedLocalProducts;
       $this->s_initial_selectedNetbootProduct = $this->s_selectedNetbootProduct;
     }
@@ -113,11 +129,12 @@ class opsigeneric extends plugin
 
   public function check()
   {
+    return(array());
     $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)){
+    if(!tests::is_mac($this->mac) || empty($this->mac)){
       $messages[] = msgPool::invalid(_("MAC address"),$this->mac,"","00:0C:7F:31:33:F1");
     }
     return($messages);
@@ -125,6 +142,11 @@ class opsigeneric extends plugin
 
   public function execute()
   {
+    $display ="";
+
+    /* The pluign initialization failed due to communication problems with the gosa daemon. 
+       A retry button will be displayed here.
+     */
     if($this->init_failed){
       $smarty = get_smarty();
       $smarty->assign("init_failed",TRUE);
@@ -132,18 +154,44 @@ class opsigeneric extends plugin
       return($smarty->fetch(get_template_path("generic.tpl",TRUE,dirname(__FILE__))));
     }  
 
+    /* If we are not a stand alone opsi client, we must be a samba client 
+       which has the opsi tab enabled.
+       Check if the opsi is added or removed and display state buttons.
+     */
+    if(!$this->parent_mode){
+      if(isset($_POST['modify_state'])){
+        if($this->is_account){
+          $this->is_account= FALSE;
+        }elseif(!$this->is_account){
+          $this->is_account= TRUE;
+        }
+      }
+      if($this->is_account){
+        $display = $this->show_disable_header(msgPool::removeFeaturesButton(_("Opsi")), 
+            msgPool::featuresEnabled(_("Opsi")));
+      }else{
+        $display = $this->show_enable_header(msgPool::addFeaturesButton(_("Opsi")), 
+            msgPool::featuresDisabled(_("Opsi")));
+        return($display);
+      } 
+    } 
+
+    /* Check if we have a sub dialog opened
+     */
     if(is_object($this->dialog)){
       $this->dialog->save_object();
       return($this->dialog->execute());
     }
 
+    /* Create HTML output of this plugin
+     */
     $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();
@@ -190,15 +238,12 @@ class opsigeneric extends plugin
     $smarty->assign("divALP", $divALP->DrawList());
     $smarty->assign("SNP", $this->s_selectedNetbootProduct);
     $smarty->assign("ANP", $this->a_availableNetbootProducts);
-    return($smarty->fetch(get_template_path("generic.tpl",TRUE,dirname(__FILE__))));
+    return($display.$smarty->fetch(get_template_path("generic.tpl",TRUE,dirname(__FILE__))));
   }
 
 
   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()){
@@ -223,15 +268,18 @@ class opsigeneric extends plugin
       }
     }
     foreach($add as $name => $data){
+      echo "Adding '$name' to ".$this->hostId."<br>";
       $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;
+      if(!empty($data['CFG'])){
+        $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;
+        }
       }
     }
 
@@ -264,7 +312,13 @@ class opsigeneric extends plugin
   }
 
   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()
@@ -276,7 +330,7 @@ class opsigeneric extends plugin
     if(isset($_POST['cancel_properties']) && is_object($this->dialog)){
       $this->dialog = NULL;
     }
-    if(isset($_POST['save_properties']) && ($this->dialog instanceof opsi_product_config)){
+    if(isset($_POST['save_properties']) && ($this->dialog instanceof opsiProperties)){
       $this->dialog->save_object();
       $pro = $this->dialog->get_product();
       $CFG = $this->dialog->get_cfg();
@@ -286,7 +340,7 @@ class opsigeneric extends plugin
       $this->dialog = NULL;
     }
 
-    if(isset($_POST['opsigeneric_posted'])){
+    if(isset($_POST['opsiGeneric_posted'])){
 
       plugin::save_object();
 
@@ -294,6 +348,18 @@ class opsigeneric extends plugin
         $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']);
         if(isset($this->a_availableNetbootProducts[$SNP])){
@@ -320,7 +386,7 @@ class opsigeneric extends plugin
         }
         if(preg_match("/^edit_lp_/",$name)){
           $product = preg_replace("/^edit_lp_(.*)_.$/","\\1",$name);
-          $this->dialog = new opsi_product_config($this->config,
+          $this->dialog = new opsiProperties($this->config,
               $product,$this->a_selectedLocalProducts[$product]['CFG'],$this->hostId);
           break;
         }
@@ -328,6 +394,13 @@ class opsigeneric extends plugin
     }
   }
 
+
+  function install_client()
+  {
+
+  }
+
+
   /* Return plugin informations for acl handling */
   static function plInfo()
   {