Code

- Correcting spelling
[gosa.git] / plugins / admin / systems / class_servDHCP.inc
index 6538952b1e1840ecf26a676013b46b498c10dc94..d883bf4ae9173537b704eee070159493f9c8463d 100644 (file)
@@ -19,6 +19,10 @@ class servdhcp extends plugin
 
   var $orig_dn = "";
 
+  var $dhcp_server_list   = array("ENTRIES"=> array(),"FOR_LIST"=> array());
+  var $take_over_id       = -1;
+  var $display_warning  = TRUE;
+
   function servdhcp ($config, $dn= NULL, $parent= NULL)
   {
     plugin::plugin ($config, $dn, $parent);
@@ -47,10 +51,32 @@ class servdhcp extends plugin
     $this->reload(); 
     if (!count($this->dhcpSections)){
       $this->is_account= FALSE;
+      $this->dhcp_server_list = $this->get_list_of_dhcp_servers();
     }
-               
   }
 
+  
+  function get_list_of_dhcp_servers()
+  {
+    $ret = array("ENTRIES"=> array(),"FOR_LIST"=> array());
+    $ldap = $this->config->get_ldap_link();
+    $ldap->cd($this->config->current['BASE']);
+    $ldap->search("(&(objectClass=goServer)(dhcpServiceDN=*))",array("dn","cn","dhcpServiceDN"));
+    while($attrs = $ldap->fetch()){
+
+      /* Skip own config */
+      if($this->dn != "new" && preg_match("/".normalizePreg($this->dn)."$/",$attrs['dn'])){
+        continue;
+      }
+  
+      $ret['ENTRIES'][] = $attrs;
+    }
+    foreach($ret['ENTRIES'] as $key => $data){
+      $ret['FOR_LIST'][$key] = $data['cn'][0];
+    }
+    return($ret);
+  }  
+
 
   function execute()
   {
@@ -59,8 +85,56 @@ class servdhcp extends plugin
 
     /* Fill templating stuff */
     $smarty= get_smarty();
+    $smarty->assign("dns_take_over",FALSE);
     $display= "";
 
+
+    /*****************/
+    /* Handle Take Over Actions 
+    /*****************/
+
+    /* Give smarty the required informations */
+    $smarty->assign("dhcp_server_list", $this->dhcp_server_list['FOR_LIST']);
+    $smarty->assign("dhcp_server_list_cnt", count($this->dhcp_server_list['FOR_LIST']));
+    
+    /* Take over requested, save id */
+    if(isset($_POST['take_over_src']) && isset($_POST['take_over'])){
+      $id = $_POST['take_over_src'];
+      if(isset($this->dhcp_server_list['ENTRIES'][$id])){
+        $this->take_over_id = $id;      
+      }
+    }
+    /* Abort take over action */ 
+    if(isset($_POST['cancel_take_over'])){
+      $this->dialog =false;
+      $this->take_over_id = -1;
+      $this->dhcp_server_list = $this->get_list_of_dhcp_servers();
+    }
+
+    /* Display informartion about take over that will be started when saving this server 
+     *  and hide default dhcp output
+     */
+    if($this->take_over_id != -1){
+      $this->dialog = FALSE;
+      $id = $this->take_over_id;
+      $smarty->assign("dns_take_over",TRUE);
+
+     $warning = sprintf(_("You are going to migrate the DHCP setup from server '%s'."),$this->dhcp_server_list['ENTRIES'][$id]['cn'][0]);
+     $warning.= " "._("The migration will be started when you save this system. To cancel this action, use the cancel button below.");
+
+      if($this->display_warning){
+        print_red($warning);
+        $this->display_warning = FALSE;
+      }
+      return($smarty->fetch(get_template_path('servdhcp.tpl', TRUE)));
+    }
+
+    
+    /*****************/
+    /* List handling  
+    /*****************/
+
     /* Section Creation? */
     if (isset($_POST['create_section']) && isset($_POST['section'])){
       $section= $_POST['section'];
@@ -204,6 +278,16 @@ class servdhcp extends plugin
 
     }
 
+    if(isset($_GET['act']) && $_GET['act']=="edit" && isset($_GET['id'])){
+      $dn = base64_decode($_GET['id']);
+      if (isset($this->dhcpObjectCache[$dn])){
+        $section= $this->objectType($dn);
+        $this->current_object= $dn;
+        $this->dialog= new $section($this->dhcpObjectCache[$dn]);
+      }
+    }
+    
+
     /* Do we need to flip is_account state? */
     if (isset($_POST['modify_state'])){
       $this->is_account= !$this->is_account;
@@ -261,7 +345,9 @@ class servdhcp extends plugin
        
     $tmp = new dhcpNewSectionDialog(NULL);
     foreach($this->dhcpSections as $section => $values ){
-       
+    
+      $values = "<a href='?plug=".$_GET['plug']."&act=edit&id=".base64_encode($section)."'>".$values."</a>";
+       
       if (count($tmp->sectionMap[$this->objectType($section)])){
         if ($this->objectType($section) == "dhcpService"){
           $DhcpList->AddEntry(array(
@@ -345,8 +431,21 @@ class servdhcp extends plugin
   /* Save to LDAP */
   function save()
   {
-    $ldap= $this->config->get_ldap_link();
+    /* Take over handling 
+     * - Load servdhcp class and dhcpObjectCache for the source dhcp setup.
+     * - Assign dhcpObjectCache to this configuration. 
+     * - Save this setup and remove source setup from ldap.
+     */
+    if($this->take_over_id != -1){
+      $id = $this->take_over_id;
+      $src = preg_replace("/cn=dhcp,/","",$this->dhcp_server_list['ENTRIES'][$id]['dn']);
+      $tmp = new servdhcp ($this->config, $src);
+      $this->orig_dn = $src;
+      $this->dhcpObjectCache =  $tmp->dhcpObjectCache;
+    }
 
+    /* Save dhcp setttings */
+    $ldap= $this->config->get_ldap_link();
     foreach ($this->dhcpObjectCache as $dn => $data){
 
       if($this->dn != $this->orig_dn){
@@ -419,8 +518,15 @@ class servdhcp extends plugin
       }
     }
 
-    /* Self modify and place service dn entry */
-    $this->dhcpServiceDN= $this->dn;
+    $this->dhcpServiceDN= $this->serviceDN;
+    if($this->dn != $this->orig_dn){
+      $this->dhcpServiceDN= preg_replace("/".normalizePreg($this->orig_dn)."$/i",$this->dn,$this->dhcpServiceDN);
+    }
+
+    /* Replace 'new' dn */ 
+    if(preg_match("/new$/",$this->dhcpServiceDN)){
+      $this->dhcpServiceDN = preg_replace("/new$/",$this->dn,$this->dhcpServiceDN);
+    }
 
     plugin::save();
     
@@ -439,7 +545,16 @@ class servdhcp extends plugin
     } else {
       $this->handle_post_events("add");
     }
+
+    /* Take over handling
+     * - Remove old dhcp config from source server 
+     */
+    if($this->take_over_id != -1){
+      $id = $this->take_over_id;
+      $src = $this->dhcp_server_list['ENTRIES'][$id]['dn'];
+      $tmp = new servdhcp ($this->config, $src);
+      $tmp->remove_from_parent();
+    }
   }