Code

Replaced in_array calls for gosa-plugins
[gosa.git] / gosa-plugins / fai / admin / fai / class_faiPartitionTable.inc
index 2ef87bc1f8d696f5b36d15561f951df0bcbafd6e..9ed849507a86cef41340c67ce77f003287dfcf89 100644 (file)
@@ -4,16 +4,23 @@ class faiPartitionTable extends plugin
 {
   /* attribute list for save action */
   var $ignore_account   = TRUE;
-  var $attributes       = array("cn","description");
+  var $attributes       = array("cn","description", "FAIpartitionMethod");
   var $objectclasses    = array("top","FAIclass","FAIpartitionTable");
 
+  var $subAttributes      = array("cn","description");
+  var $subPartAttributes  = array("cn","FAIpartitionNr","FAIpartitionSize","FAImountPoint","FAIfsType","FAIpartitionType","FAImountOptions","FAIfsOptions","FAIpartitionFlags","description","FAIfsCreateOptions","FAIfsTuneOptions","FAIfsOptions","FAIpartitionFlags","FAIlvmDevice");
+
+  var $sub64coded = array();
+  var $subBinary = array();
+
   /* Specific attributes */
-  var $cn               = "";       // The class name for this object
-  var $description      = "";       // The description for this set of partitions
-  var $disks            = array();  // All defined Disks 
-  var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
+  var $cn                 = "";       // The class name for this object
+  var $description        = "";       // The description for this set of partitions
+  var $FAIpartitionMethod = "";       // "setup-storage" or not assigned
+  var $disks              = array();  // All defined Disks 
+  var $is_dialog          = false;    // specifies which buttons will be shown to save or abort
 
-  var $FAIstate         = "";
+  var $FAIstate           = "";
   var $ui;
 
   var $view_logged      = FALSE;
@@ -23,88 +30,88 @@ class faiPartitionTable extends plugin
     /* Load Attributes */
     plugin::plugin ($config, $dn);
 
-    $this->acl ="#all#";
-
-    $this->ui = get_userinfo();    
-
     /* If "dn==new" we try to create a new entry
      * Else we must read all objects from ldap which belong to this entry.
-     * First read disks from ldap ... and then the partition definitions for the disks.
      */
+    $this->ui = get_userinfo();
     if($dn != "new"){
       $this->dn =$dn;
 
-      /* Get FAIstate
+      /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
        */
-      if(isset($this->attrs['FAIstate'][0])){
-        $this->FAIstate = $this->attrs['FAIstate'][0];
-      }
+      $res = FAI::get_all_objects_for_given_base($this->dn,"(&(objectClass=FAIclass)(objectClass=FAIpartitionDisk))");
+      foreach($res as $obj){
+
+        /* Skip not relevant objects */
+        if(!preg_match("/".preg_quote($this->dn, '/')."$/i",$obj['dn'])) continue;
+        $objects = array();
+        $objects['description']  = "";
+
+        // Transform disk type into image later...
+        if (!isset($obj['FAIdiskType'])){
+          $objects['FAIdiskType']        = "old";
+        } else {
+          $objects['FAIdiskType']        = $obj['FAIdiskType'];
+        }
 
-      /* Read all disks from ldap taht are defined fot this partition table 
-       */
-      $ldap = $this->config->get_ldap_link();
-      $ldap->cd ($this->dn);
-      $ldap->search("(&(objectClass=FAIclass)(objectClass=FAIpartitionDisk))",array("*"));
-      while($object = $ldap->fetch()){
-
-        /* Skip objects, that are tagged as removed */
-        if(isset($object['FAIstate'][0])){
-          if(preg_match("/removed$/",$object['FAIstate'][0])){
-            continue;
+        // Get disk options, without 'count' index. 
+        $objects['FAIdiskOption'] = array();
+        if (isset($obj['FAIdiskOption'])){
+          for($i=0;$i<$obj['FAIdiskOption']['count'];$i++){ 
+            $objects['FAIdiskOption'][] = $obj['FAIdiskOption'][$i];
           }
         }
 
-        $this->disks[$object['cn'][0]]['status']      = "edited";
-        $this->disks[$object['cn'][0]]['dn']          = $object['dn'];
-        $this->disks[$object['cn'][0]]['cn']          = $object['cn'][0];
-        if(isset($object['description'][0])){
-          $this->disks[$object['cn'][0]]['description'] = $object['description'][0];
-        }else{
-          $this->disks[$object['cn'][0]]['description'] = "";
+        // Transform potential lvm information
+        if (isset($obj['FAIlvmDevice'])){
+          for($i=0;$i<$obj['FAIlvmDevice']['count'];$i++){
+            $name = $obj['FAIlvmDevice'][$i]; 
+            $objects['FAIlvmDevice'][$name] = $name;
+          }
         }
-        $this->disks[$object['cn'][0]]['partitions']   = array();
+
+        $objects['dn']          = $obj['dn'];
+        $objects                = $this->get_object_attributes($objects,$this->subAttributes);
+        $this->disks[$objects['cn']] = $objects;
+        $this->disks[$objects['cn']]['partitions'] = array();
       }
-  
+
       /* read all partitions for each disk 
        */
       foreach($this->disks as $name => $disk){
-        $ldap->cd ($disk['dn']);
-        $ldap->search("(&(objectClass=FAIclass)(objectClass=FAIpartitionEntry))",array("*"));
-        while($partition = $ldap->fetch()){
-
-          /* Skip objects, that are tagged as removed */
-          if(isset($partition['FAIstate'][0])){
-            if(preg_match("/removed$/",$partition['FAIstate'][0])){
-              continue;
-            }
-          }
+        $res = FAI::get_all_objects_for_given_base($disk['dn'],"(&(objectClass=FAIclass)(objectClass=FAIpartitionEntry))");
+        foreach($res as $obj){
 
-          /* remove count ... from ldap result 
-           */
-          foreach($partition as $key=>$val){
-            if((is_numeric($key))||($key=="count")||($key=="dn")){
-              unset($partition[$key]);
-            }else{
-              $partition[$key] = $val[0];
-            }
-          }
+          /* Skip not relevant objects */
+          if(!preg_match("/".preg_quote($disk['dn'], '/')."$/i",$obj['dn'])) continue;
 
-          /* Append fetched partitions
-           */
-          $partition['status']="edited";
-          $this->disks[$name]['partitions'][$partition['FAIpartitionNr']] = $partition; 
-        }  
+          $objects = array();
+          $objects['dn']          = $obj['dn'];
+          $objects                = $this->get_object_attributes($objects,$this->subPartAttributes);
+          unset($objects['dn']);;
+          $this->disks[$name]['partitions'][$objects['FAIpartitionNr']] = $objects;
+        }
       }
     }
+
+    /* Keep track of disk changes, by comparing the initial and resulting 
+     *  disk setup.
+     */
+    $this->initial_disks = $this->disks;
+
+    $this->is_new = FALSE;
+    if($this->dn == "new"){
+      $this->is_new =TRUE;
+    }
     ksort($this->disks);
   }
 
 
   function acl_base_for_current_object($dn)
   {
-    if($dn == "new"){
+    if($dn == "new" || $dn == ""){
       if($this->dn == "new"){
-        $dn = session::get('CurrentMainBase');
+        $dn= $this->parent->parent->acl_base;
       }else{
         $dn = $this->dn;
       }
@@ -131,15 +138,26 @@ class faiPartitionTable extends plugin
      * This code adds a new HDD to the disks 
      * A new Dialog will be opened 
      */
-    if(isset($_POST['AddDisk'])){
+    if((isset($_POST['AddDisk']) || isset($_POST['AddRaid']) || isset($_POST['AddVolgroup'])) && 
+        !preg_match("/freeze/i",$this->FAIstate)){
       $usedDiskNames =array();
       foreach($this->disks as $key=>$disk){
         $usedDiskNames[]= $key;
       }
-      $this->dialog = new faiPartitionTableEntry($this->config,$this->dn,$usedDiskNames); 
+      if ($this->FAIpartitionMethod == "setup-storage") {
+        if(isset($_POST['AddDisk'])) $type = "disk";
+        if(isset($_POST['AddRaid'])) $type = "raid";
+        if(isset($_POST['AddVolgroup'])) $type = "lvm";
+        $this->dialog = new faiDiskEntry($this->config,$this->dn,$this, array(),$type); 
+      } else {
+        $this->dialog = new faiPartitionTableEntry($this->config,$this->dn,$this); 
+      }
+
       $this->dialog->set_acl_base($this->acl_base_for_current_object($this->dn));
       $this->dialog->set_acl_category("fai");
       $this->dialog->FAIstate = $this->FAIstate;
+
+
       $this->is_dialog = true;
     }
 
@@ -148,34 +166,123 @@ class faiPartitionTable extends plugin
      */    
 
     if($this->dn != "new"){
-      session::set('objectinfo',$this->dn);
+      set_object_info($this->dn);
     }
 
-    if((isset($_POST['EditDisk']))&&(isset($_POST['disks']))){
-      $usedDiskNames =array();
-      $Udisk = $_POST['disks'][0];
-      if(isset($this->disks[$Udisk])){
+    /* Edit entries via GET */
+    $s_action = "";
+    $s_entry = "";
+    if(isset($_GET['act']) && isset($_GET['id'])){
+      if($_GET['act'] == "edit" && isset($this->disks[$_GET['id']])){
+        $s_entry= $_GET['id'];
+        $s_action= "edit";
+      }
+    }
+
+    /* New Listhandling */
+    foreach($_POST as $name => $value){
+      if(preg_match("/^edit_[^_]*_[xy]$/",$name)){
+        $s_entry = preg_replace("/^edit_/","",$name);
+        $s_entry = base64_decode(preg_replace("/_.*/","",$s_entry));
+        $s_action = "edit"; 
+        break;
+      }
+      if (!preg_match("/freeze/i", $this->FAIstate) && preg_match("/^delete_[^_]*_/",$name)){
+        $s_entry = preg_replace("/^delete_/","",$name);
+        $s_entry = base64_decode(preg_replace("/_.*/","",$s_entry));
+        $s_action = "remove";
+        break;
+      }
+    }
+
+
+    /* Disk remove was requested. 
+     * Now check if the disk is still in use, in this case 
+     *  display a warning message and abort the removal. 
+     * If the disk is not used anymore, then remove it. 
+     */
+    if($s_action == "remove"){
+      if(isset($this->disks[$s_entry])){
 
-        foreach($this->disks  as $key=>$disk){
-          if($key != $Udisk){
-            $usedDiskNames[]= $key;
+        /* Create a list of all partitions that are used in 
+         *  lvm or raid compilations. 
+         */
+        $list = array();
+        foreach($this->disks as $dname => $disk){
+          if($disk['FAIdiskType'] != "disk" && $dname != $s_entry){
+            if($disk['FAIdiskType'] == "lvm"){
+              foreach($disk['FAIlvmDevice'] as $partname){
+                $list[preg_replace("/:.*$/","",$partname)][] = $disk;
+              }
+            }
+            foreach($disk['partitions'] as $partkey => $part){
+              if($disk['FAIdiskType'] == "raid"){
+                foreach(explode(",",$part['FAIpartitionSize']) as $partname){
+                  $list[preg_replace("/:.*$/","",$partname)][] = $disk;
+                }
+              }
+            }  
           }
         }
 
-        /* Set object info string, which will be displayed in plugin info line */ 
-        if(isset($this->disks[$Udisk]['dn'])){
-          session::set('objectinfo',$this->disks[$Udisk]['dn']);
-          $dn = $this->disks[$Udisk]['dn'];
-        }else{
-          session::set('objectinfo',"");
-          $dn = "new";
+        /* Now that we've a list of all partition references, lets check if
+         *  one of the partitions we are going to remove is still in use.
+         */
+        $used = array();
+        foreach($this->disks[$s_entry]['partitions'] as $part){
+          if(isset($list[$part['cn']])){    
+            foreach($list[$part['cn']] as $disk){
+              $used[$disk['cn']] = $disk['cn'];
+            }
+          }
         }
 
-        $this->dialog = new faiPartitionTableEntry($this->config,$this->dn,$usedDiskNames,$this->disks[$Udisk]); 
+        /* Skip removal while disk is in use. 
+         */
+        if(count($used)){
+          $used = implode(",",$used);
+          msg_dialog::display(_("Error"), 
+              sprintf(_("The disk cannot be deleted while it is used in the '%s' disk definition!"), 
+                $used), ERROR_DIALOG);
+        }else{
+
+          /* Everything is ok, we can remove the disk now.
+           */
+          unset($this->disks[$s_entry]);
+        } 
+      } 
+    }
+
+    
+
+    if($s_action == "edit"){
+
+      /* Set object info string, which will be displayed in plugin info line */ 
+      if(isset($this->disks[$s_entry]['dn'])){
+        set_object_info($this->disks[$s_entry]['dn']);
+        $dn = $this->disks[$s_entry]['dn'];
+      }else{
+        set_object_info("");
+        $dn = "new";
+      }
+
+      $type ="old"; 
+      if(isset($this->disks[$s_entry]['FAIdiskType'])){
+        $type = $this->disks[$s_entry]['FAIdiskType'];
+      }
+      if(in_array_strict($type,array('raid','lvm','disk'))){
+        $this->dialog = new faiDiskEntry(
+            $this->config,$this->dn,$this,$this->disks[$s_entry], 
+            $this->disks[$s_entry]['FAIdiskType']); 
+
+      }else{
+        $this->dialog = new faiPartitionTableEntry(
+            $this->config,$this->dn,$this,$this->disks[$s_entry]); 
+      }
+      if($this->dialog){
         $this->dialog->set_acl_base($this->acl_base_for_current_object($dn));
         $this->dialog->set_acl_category("fai");
         $this->dialog->FAIstate = $this->FAIstate;
-
         $this->is_dialog = true;
       }
     }
@@ -193,7 +300,7 @@ class faiPartitionTable extends plugin
      */
     if(isset($_POST['SaveDisk'])){
 
-      if($this->FAIstate != "freeze"){
+      if (!preg_match("/freeze/i", $this->FAIstate)){
         $this->dialog->save_object();
         if(count($this->dialog->check())){
           foreach($this->dialog->check() as $msg){
@@ -202,19 +309,7 @@ class faiPartitionTable extends plugin
         }else{
           $disk = $this->dialog->save();
           if(isset($disk['rename'])){
-            if($this->disks[$disk['rename']['from']]['status']=="edited"){
-              $this->disks[$disk['rename']['from']]['status']="delete";
-            }else{
-              unset($this->disks[$disk['rename']['from']]);
-            }
-
-            foreach($disk['partitions'] as $key => $val){
-              if($disk['partitions'][$key]['status']!="delete"){
-                $disk['partitions'][$key]['status']= "new";
-              }
-            }
-
-            $disk['status']="new";
+            unset($this->disks[$disk['rename']['from']]);
             $disk['cn']= $disk['rename']['to'];
           }
 
@@ -230,34 +325,6 @@ class faiPartitionTable extends plugin
       }
     }
 
-    /* Delete selected disk drive from list
-     * Assign delete status for all its partitions      
-     */
-    if((isset($_POST['DelDisk']))&&(!empty($_POST['disks']))){
-      if($this->FAIstate != "freeze"){
-        foreach($_POST['disks'] as $disk) {
-
-          if(isset($this->disks[$disk])){
-            if($this->disks[$disk]['status']=="edited"){
-              $this->disks[$disk."-delete"]=$this->disks[$disk];
-              unset($this->disks[$disk]);
-              $disk = $disk."-delete";        
-              $this->disks[$disk]['status']="delete";
-              foreach($this->disks[$disk]['partitions'] as $name => $value ){
-                if($value['status']=="edited"){
-                  $this->disks[$disk]['partitions'][$name]['status']="delete"; 
-                }else{
-                  unset($this->disks[$disk]['partitions'][$name]);
-                }
-              }
-            }else{
-              unset($this->disks[$disk]);
-            }
-          }
-        }
-      }
-    }
-
     /* Display dialog if one is defined
      */
     if(is_object($this->dialog)){
@@ -280,23 +347,39 @@ class faiPartitionTable extends plugin
     $smarty->assign("sub_object_is_addable",
         preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiPartitionTableEntry")) &&
         !preg_match("/freeze/",$this->FAIstate));
+    $smarty->assign("sub_object_is_removeable",
+        preg_match("/d/",$this->ui->get_permissions($dn,"fai/faiPartitionTableEntry")) &&
+        !preg_match("/freeze/",$this->FAIstate));
 
     $tmp = $this->plInfo();
     foreach($tmp['plProvidedAcls'] as $name => $translated){
-      $smarty->assign($name."ACL",$this->getacl($name));
+      $smarty->assign($name."ACL",$this->getacl($name,preg_match("/freeze/i",$this->FAIstate)));
     }
-    $disks = $this->getDisks();
-    $smarty->assign("disks"   ,$disks);
-    $display.= $smarty->fetch(get_template_path('faiPartitionTable.tpl', TRUE));
-    return($display);
-  }
 
-  function getDisks(){
-    /* Return all available disks for this partition table
-     * Return in listBox friendly array
-     */
-    $a_return = array();
+    /* Assign mode */
+    if ($this->FAIpartitionMethod == ""){
+      $smarty->assign("mode", "");
+      $smarty->assign("storage_mode", "disabled");
+    } else {
+      $smarty->assign("mode", "checked");
+      $smarty->assign("storage_mode", "");
+    }
+    if (!count($this->disks)) {
+      $smarty->assign("lockmode", "");
+    } else {
+      $smarty->assign("lockmode", "disabled");
+    }
+    if (isset($this->disks['raid'])){
+      $smarty->assign("addraid", "disabled");
+    } else {
+      $smarty->assign("addraid", "");
+    }
+
+    /* Divlist containing disks */
+    $divlist = new divSelectBox("FAItemplates");
+    $divlist->setHeight(400);
     foreach($this->disks as $key => $disk){
+      $act = "";
 
       $dn = "new";
       if(isset($obj['dn'])){
@@ -306,30 +389,40 @@ class faiPartitionTable extends plugin
       $acl = $this->ui->get_permissions($dn,"fai/faiPartitionTableEntry");
       if(preg_match("/(r|w)/",$acl)) {
 
-        if($disk['status'] != "delete"){
-          $cnt=0;
-          foreach($disk['partitions'] as $val){
-            if($val['status']!="delete"){
-              $cnt ++;
-            }
-          }
-          if(!empty($disk['description'])){
-            if($cnt == 1){
-              $a_return[$key]=  $disk['cn']." [".$disk['description']."], ".sprintf(_("%s partition"), $cnt);
-            }else{
-              $a_return[$key]=  $disk['cn']." [".$disk['description']."], ".sprintf(_("%s partition(s)"), $cnt);
-            }
-          }else{
-            if($cnt == 1){
-              $a_return[$key]=  $disk['cn'].", ".sprintf(_("%s partition"), $cnt);
-            }else{
-              $a_return[$key]=  $disk['cn'].", ".sprintf(_("%s partition(s)"), $cnt);
-            }
-          }
+        $act .= "<input type='image' src='images/lists/edit.png'   name='edit_%s'    title='"._("edit")."' alt='"._("edit")."'>";
+        if(preg_match("/d/",$acl) && !preg_match("/freeze/", $this->FAIstate)){
+          $act .="<input type='image' src='images/lists/trash.png' name='delete_%s'  title='"._("delete")."' alt='"._("delete")."'>";
         }
+
+        $cnt= count($disk['partitions']);
+
+        $edit_link = "<a href='?plug=".$_GET['plug']."&amp;act=edit&amp;id=".$key."'>".$key."</a>";
+        $types= array("old" => "plugins/fai/images/fai_partitionTable.png", "disk" => "plugins/fai/images/fai_partitionTable.png",
+            "raid" => "plugins/fai/images/raid.png", "lvm" => "plugins/ogroups/images/list_ogroup.png");
+        $type = isset($disk['FAIdiskType'])?$types[$disk['FAIdiskType']]:$types['old'];
+        $divlist->AddEntry(array( 
+              array("string"=> "<img border='0' src='".$type."'>", "attach"=>"style='width:16px'"),
+              array("string"=> $edit_link, "attach"=>"style='width:100px'"),
+              array("string"=> $disk['description']),
+              array("string"=> $cnt,  "attach"=>"style='width:16px'"),
+              array("string"=>str_replace("%s",base64_encode($key),$act),
+                "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
       }
     }
-    return($a_return);
+    $smarty->assign("Entry_divlist",$divlist->DrawList());
+
+    $display.= $smarty->fetch(get_template_path('faiPartitionTable.tpl', TRUE));
+    return($display);
+  }
+
+
+  function getUsedDiskNames()
+  {
+    $ret = array();
+    foreach($this->disks as $disk){
+      $ret[] = $disk['cn'];
+    }
+    return($ret);  
   }
 
 
@@ -340,32 +433,18 @@ class faiPartitionTable extends plugin
     $ldap = $this->config->get_ldap_link();
     $ldap->cd ($this->dn);
 
-    $faifilter = session::get('faifilter');
-    $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $this->dn);
-    if($faifilter['branch'] == "main"){
-      $use_dn = $this->dn;
-    }
+    $release = $this->parent->parent->fai_release;
+    $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $this->dn);
 
     FAI::prepare_to_save_FAI_object($use_dn,array(),true);
-
     new log("remove","fai/".get_class($this),$use_dn,$this->attributes);   
-    foreach($this->disks as $disk){
-
+    foreach($this->initial_disks as $disk){
       $disk_dn = "cn=".$disk['cn'].",".$this->dn;
-      $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $disk_dn);
-      if($faifilter['branch'] == "main"){
-        $use_dn = $disk_dn;
-      }
+      $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i",$release, $disk_dn);
       FAI::prepare_to_save_FAI_object($use_dn,array(),true);
-
       foreach($disk['partitions'] as $key => $partition){    
-     
         $partition_dn= "FAIpartitionNr=".$partition['FAIpartitionNr'].",".$disk_dn;      
-        $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $partition_dn);
-        if($faifilter['branch'] == "main"){
-          $use_dn = $disk_dn;
-        }
+        $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $partition_dn);
         FAI::prepare_to_save_FAI_object($use_dn,array(),true);
       }
     }
@@ -376,11 +455,20 @@ class faiPartitionTable extends plugin
    */
   function save_object()
   {
-    if($this->FAIstate == "freeze") return;
-    plugin::save_object();
-    foreach($this->attributes as $attrs){
-      if(isset($_POST[$attrs])){
-        $this->$attrs = $_POST[$attrs];
+    if(isset($_POST['FAIpartitionTablePosted'])){
+      if (preg_match("/freeze/", $this->FAIstate)) return;
+      plugin::save_object();
+      foreach($this->attributes as $attrs){
+        if(isset($_POST[$attrs])){
+          $this->$attrs = $_POST[$attrs];
+        }
+      }
+      if(!count($this->disks)){
+        if(isset($_POST['mode'])){
+          $this->FAIpartitionMethod = "setup-storage";
+        }else{
+          $this->FAIpartitionMethod = "";
+        }
       }
     }
   }
@@ -392,6 +480,16 @@ class faiPartitionTable extends plugin
     /* Call common method to give check the hook */
     $message= plugin::check();
 
+    /* Ensure that we do not overwrite an allready existing entry 
+     */
+    if($this->is_new){
+      $release = $this->parent->parent->fai_release;
+      $new_dn= 'cn='.$this->cn.",".get_ou('faiPartitionRDN').get_ou('faiBaseRDN').$release;
+      $res = faiManagement::check_class_name("FAIpartitionTable",$this->cn,$new_dn);
+      if(isset($res[$this->cn])){
+        $message[] = msgPool::duplicated(_("Name"));
+      }
+    }
     return ($message);
   }
 
@@ -399,8 +497,8 @@ class faiPartitionTable extends plugin
   /* Save to LDAP */
   function save()
   {
-
     plugin::save();
+
     /* Save current settings.
      * 1 : We must save the partition table, with its description and cn 
      * 2 : Append Disk with cn and  description.
@@ -413,10 +511,15 @@ class faiPartitionTable extends plugin
 
     if($this->initially_was_account){
       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
+      @DEBUG (DEBUG_FAI, __LINE__, __FUNCTION__, __FILE__,$this->dn , "Saving disk: ");
     }else{
       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
+      @DEBUG (DEBUG_FAI, __LINE__, __FUNCTION__, __FILE__,$this->dn , "Adding disk: ");
     }
  
+    // Prepare disks to be saved - The 'status' attribute is added here.
+    $this->prepareDiskToBeSave(); 
     /* Sort entries, because we must delete entries with status="delete" first */
     $order = array();
     foreach($this->disks as $key => $disk){
@@ -430,12 +533,15 @@ class faiPartitionTable extends plugin
       }
     }
 
+
     /* Append all disks to ldap */
     foreach($order as $cn=>$disk){
+
       $disk_dn                    = "cn=".$disk['cn'].",".$this->dn;
+      $short_dn                   = "cn=".$disk['cn'].",...";
       $disk_attrs['cn']           =  $disk['cn'];
-      $disk_attrs['description']  =  $disk['description']; 
       $disk_attrs['objectClass']  =  array("top","FAIclass","FAIpartitionDisk");
+  
 
       if($disk['status']=="new"){
         $ldap->cat($disk_dn,array("objectClass"));
@@ -444,23 +550,52 @@ class faiPartitionTable extends plugin
         }
       }
 
+      foreach(array("description","FAIdiskType","FAIdiskOption","FAIlvmDevice") as $attr){
+        if($disk['status'] == "new"){
+          if(isset($disk_attrs[$attr])) unset($disk_attrs[$attr]);
+          if(isset($disk[$attr]) && !empty($disk[$attr])){
+            if(is_array($disk[$attr])){
+              $disk_attrs[$attr] = array_values($disk[$attr]);
+            }else{
+              $disk_attrs[$attr] = $disk[$attr];
+            }
+          }
+        }else{
+          if(isset($disk[$attr]) && !empty($disk[$attr])){
+            if(is_array($disk[$attr])){
+              $disk_attrs[$attr] = array_values($disk[$attr]);
+            }else{
+              $disk_attrs[$attr] = $disk[$attr];
+            }
+          }else{
+            $disk_attrs[$attr] = array();
+          }
+        }
+      }
+
+
       /* Tag object */
       $this->tag_attrs($disk_attrs, $disk_dn, $this->gosaUnitTag);
 
       if($disk['status'] == "delete"){
+        @DEBUG (DEBUG_FAI, __LINE__, __FUNCTION__, __FILE__,$short_dn , "Removing disk: ");
         FAI::prepare_to_save_FAI_object($disk_dn,array(),true);
         $this->handle_post_events("remove");
+        unset($this->disks[$cn]);
       }elseif($disk['status'] == "edited"){
+        @DEBUG (DEBUG_FAI, __LINE__, __FUNCTION__, __FILE__,$short_dn , "Updating disk: ");
         FAI::prepare_to_save_FAI_object($disk_dn,$disk_attrs);
         $this->handle_post_events("modify");
       }elseif($disk['status']=="new"){
+        @DEBUG (DEBUG_FAI, __LINE__, __FUNCTION__, __FILE__,$short_dn , "Adding disk: ");
         FAI::prepare_to_save_FAI_object($disk_dn,$disk_attrs);
         $this->handle_post_events("add");
       }
 
       if($disk['status']!="delete")
+
       /* Add all partitions */
-      foreach($disk['partitions'] as $key => $partition){
+      foreach($disk['partitions'] as $pkey => $partition){
         $partition_attrs = array();
 
         foreach($partition as $key => $value){
@@ -471,9 +606,10 @@ class faiPartitionTable extends plugin
           }
         }
 
-        $partition_dn= "FAIpartitionNr=".$partition_attrs['FAIpartitionNr'].",".$disk_dn;      
+        $partition_dn= "FAIpartitionNr=".$partition_attrs['FAIpartitionNr'].",".$disk_dn;     
+        $short_dn= "FAIpartitionNr=".$partition_attrs['FAIpartitionNr'].",...";
+     
         $partition_attrs['objectClass']= array("top","FAIclass","FAIpartitionEntry");
-        $partition_attrs['cn']= $partition_attrs['FAIpartitionNr'];
         
         unset($partition_attrs['status']);
         unset($partition_attrs['old_cn']);
@@ -489,90 +625,155 @@ class faiPartitionTable extends plugin
           $partition_attrs['FAImountPoint']="swap";
         }
 
-       /* Tag object */
-       $this->tag_attrs($partition_attrs, $partition_dn, $this->gosaUnitTag);
+        /* Tag object */
+        $this->tag_attrs($partition_attrs, $partition_dn, $this->gosaUnitTag);
 
+        
         if($partition['status'] == "delete"){
+          @DEBUG (DEBUG_FAI, __LINE__, __FUNCTION__, __FILE__,$short_dn , "Removing partition: ");
           FAI::prepare_to_save_FAI_object($partition_dn,array(),true);
           $this->handle_post_events("remove");
+          unset($this->disks[$cn]['partitions'][$pkey]);
         }elseif($partition['status'] == "edited"){
+          @DEBUG (DEBUG_FAI, __LINE__, __FUNCTION__, __FILE__,$short_dn , "Updating partition: ");
           FAI::prepare_to_save_FAI_object($partition_dn,$partition_attrs);
           $this->handle_post_events("modify");
         }elseif($partition['status']=="new"){
+          @DEBUG (DEBUG_FAI, __LINE__, __FUNCTION__, __FILE__,$short_dn , "Adding partition: ");
           FAI::prepare_to_save_FAI_object($partition_dn,$partition_attrs);
           $this->handle_post_events("add");
         }
+        
+        // We do not need the status flag any longer 
+        if(isset($this->disks[$cn]['partitions'][$pkey]['status'])){
+          unset($this->disks[$cn]['partitions'][$pkey]['status']);
+        }
+      }
+      // We do not need the status flag any longer 
+      if(isset($this->disks[$cn]['status'])){
+        unset($this->disks[$cn]['status']);
       }
     }
+    $this->initial_disks = $this->disks;
     $this->handle_post_events("add");
   }
 
 
-  function PrepareForCopyPaste($source)
+  function prepareDiskToBeSave()
   {
-    plugin::PrepareForCopyPaste($source);
-    /* Get FAIstate
-     */
-    if(isset($source['FAIstate'][0])){
-      $this->FAIstate = $source['FAIstate'][0];
+    foreach($this->disks as $id => $disk){
+      
+      /* Correct FAIpartitionNr.
+       * If we've only primary partition then set the partition numbers from  
+       *  1 to 4, else set the primary from 1 to 3 and logical >= 5 
+       * 
+       */
+      if(!isset($disk['partitions'])){
+        $disk['partitions'] = array();
+      }
+      $newSetup = array();
+
+      if($disk['FAIdiskType'] == "disk"){
+        $primary = $logical = array();
+        foreach($disk['partitions'] as $partid => $part){
+          if($part['FAIpartitionType'] == "primary"){
+            $primary[$partid] = $part;
+          }elseif($part['FAIpartitionType'] == "logical"){
+            $logical[$partid] = $part;
+          }else{
+            trigger_error("Fatal: unknown disk type? ".$part['FAIpartitionType']); 
+          }
+        }
+        $cnt = 1;
+        foreach($primary as $part){
+          $part['FAIpartitionNr'] = $cnt;
+          $part['cn'] = $disk['cn'].$cnt;
+          $newSetup[$cnt] = $part;
+          $cnt ++;
+        } 
+        $cnt = 5;
+        foreach($logical as $part){
+          $part['FAIpartitionNr'] = $cnt;
+          $part['cn'] = $disk['cn'].$cnt;
+          $newSetup[$cnt] = $part;
+          $cnt ++;
+        }
+        $this->disks[$disk['cn']]['partitions'] = $newSetup; 
+      }
     }
 
-    /* Read all disks from ldap taht are defined fot this partition table 
+    # FAIpartitionNr have to be used as index for this->disks
+    #  else the next operation will fail. 
+
+    /* Check if there are disks to be removed, edited or added.
+     * We compare the initial disk setup with the current setup and
+     *  and add a status flag, which will then be used to perform the 
+     *  correct action - add, edited, remove.
      */
-    $ldap = $this->config->get_ldap_link();
-    $ldap->cd ($source['dn']);
-    $ldap->search("(&(objectClass=FAIclass)(objectClass=FAIpartitionDisk))",array("*"));
-    while($object = $ldap->fetch()){
-
-      /* Skip objects, that are tagged as removed */
-      if(isset($object['FAIstate'][0])){
-        if(preg_match("/removed$/",$object['FAIstate'][0])){
-          continue;
-        }
-      }
+    foreach($this->disks as $key => $disk){
 
-      $this->disks[$object['cn'][0]]['status']      = "edited";
-      $this->disks[$object['cn'][0]]['dn']          = $object['dn'];
-      $this->disks[$object['cn'][0]]['cn']          = $object['cn'][0];
-      if(isset($object['description'][0])){
-        $this->disks[$object['cn'][0]]['description'] = $object['description'][0];
+      // - A complete NEW disk
+      if(!isset($this->initial_disks[$disk['cn']])){
+        $this->disks[$key]['status'] = "new";
+        foreach($disk['partitions'] as $pkey => $part){
+          $this->disks[$disk['cn']]['partitions'][$pkey]['status'] = "new";
+        }
       }else{
-        $this->disks[$object['cn'][0]]['description'] = "";
+    
+        // - Disk was "EDITED" 
+        $this->disks[$key]['status'] = "edited";
+        foreach($disk['partitions'] as $pkey => $part){
+
+          // - Check whether partition is "NEW" or "EDITED" 
+          if(!isset($this->initial_disks[$key]['partitions'][$pkey])){
+            $this->disks[$key]['partitions'][$pkey]['status'] = "new";
+          }else{
+            $this->disks[$key]['partitions'][$pkey]['status'] = "edited";
+          }
+        }
       }
-      $this->disks[$object['cn'][0]]['partitions']   = array();
     }
-
-    /* read all partitions for each disk 
-     */
-    foreach($this->disks as $name => $disk){
-      $ldap->cd ($disk['dn']);
-      $ldap->search("(&(objectClass=FAIclass)(objectClass=FAIpartitionEntry))",array("*"));
-      while($partition = $ldap->fetch()){
-
-        /* Skip objects, that are tagged as removed */
-        if(isset($partition['FAIstate'][0])){
-          if(preg_match("/removed$/",$partition['FAIstate'][0])){
-            continue;
-          }
+     
+    /* Check which partitions havbe to be removed. 
+     * (They intially existed, but are now gone.)
+     */ 
+    foreach($this->initial_disks as $ikey => $idisk){
+      
+      // - Complete disk was REMOVED.
+      if(!isset($this->disks[$idisk['cn']])){
+        $this->disks[$idisk['cn']] = $idisk;
+        $this->disks[$idisk['cn']]['status'] = "delete";
+        foreach($idisk['partitions'] as $pkey=>$part){
+          $this->disks[$idisk['cn']]['partitions'][$pkey] = $part;
+          $this->disks[$idisk['cn']]['partitions'][$pkey]["status"] = "delete";
         }
-
-        /* remove count ... from ldap result 
-         */
-        foreach($partition as $key=>$val){
-          if((is_numeric($key))||($key=="count")||($key=="dn")){
-            unset($partition[$key]);
-          }else{
-            $partition[$key] = $val[0];
+      }else{
+        foreach($idisk['partitions'] as $pkey=>$part){
+          if(!isset($this->disks[$idisk['cn']]['partitions'][$pkey])){
+            $this->disks[$idisk['cn']]['partitions'][$pkey] = $part;
+            $this->disks[$idisk['cn']]['partitions'][$pkey]["status"] = "delete";
           }
         }
-
-        /* Append fetched partitions
-         */
-        $partition['status']="edited";
-        $this->disks[$name]['partitions'][$partition['FAIpartitionNr']] = $partition; 
-      }  
+      }
     }
-    ksort($this->disks);
+  }
+  
+
+  function PrepareForCopyPaste($source)
+  {
+    plugin::PrepareForCopyPaste($source);
+
+    /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
+     */
+
+    /* To be sure to copy disks and partitions correctly, just create a 
+     *  new PartitionTable object and use the 'disks' attribute 
+     *  from this it. This is much easier and less code.
+     */
+    $obj = new faiPartitionTable($this->config, $source['dn']);
+    $this->disks = $obj->disks;
   }
 
 
@@ -592,6 +793,65 @@ class faiPartitionTable extends plugin
             "description"       => _("Description"))
           ));
   }
+
+
+  /*! \brief  Used for copy & paste.
+    Returns a HTML input mask, which allows to change the cn of this entry.
+    @param  Array   Array containing current status && a HTML template.
+   */
+  function getCopyDialog()
+  {
+    $vars = array("cn");
+    $smarty = get_smarty();
+    $smarty->assign("cn", htmlentities($this->cn));
+    $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
+    $ret = array();
+    $ret['string'] = $str;
+    $ret['status'] = "";
+    return($ret);
+  }
+
+
+  /*! \brief  Used for copy & paste.
+    Some entries must be renamed to avaoid duplicate entries.
+   */
+  function saveCopyDialog()
+  {
+    if(isset($_POST['cn'])){
+      $this->cn = get_post('cn');
+    }
+  }
+
+  /* Reload some attributes */
+  function get_object_attributes($object,$attributes)
+  {
+    $ldap = $this->config->get_ldap_link();
+    $ldap->cd($this->config->current['BASE']);
+    $ldap->cat($object['dn'],$attributes);
+    $tmp  = $ldap->fetch();
+
+    foreach($attributes as $attrs){
+      if(isset($tmp[$attrs][0])){
+        $var = $tmp[$attrs][0];
+
+        /* Check if we must decode some attributes */
+        if(in_array_ics($attrs,$this->sub64coded)){
+          $var = base64_decode($var);
+        }
+
+        /*  check if this is a binary entry */
+        if(in_array_ics($attrs,$this->subBinary)){
+          $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
+        }
+
+        /* Fix slashes */
+        $var = addslashes($var);
+        $object[$attrs] = $var;
+      }
+    }
+    return($object);
+  }
+
 }
 
 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler: