Code

Fixed dhcp saving for renamed objects
[gosa.git] / plugins / admin / fai / class_faiTemplate.inc
index 0b57d6c578e99b0cf4a8c6cf283e3fb75d3ab5cb..bb552443cfa8a9690ec4711ccc25f151abc6d5e8 100644 (file)
@@ -2,11 +2,6 @@
 
 class faiTemplate extends plugin
 {
-  /* CLI vars */
-  var $cli_summary      = "Manage server basic objects";
-  var $cli_description  = "Some longer text\nfor help";
-  var $cli_parameters   = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
-
   /* attribute list for save action */
   var $ignore_account   = TRUE;
 
@@ -24,8 +19,10 @@ class faiTemplate extends plugin
   var $subClassName     = "faiTemplateEntry";      
 
   /* Attributes to initialise for each subObject */
-  var $subAttributes    = array("cn","description","FAItemplateFile","FAItemplatePath","FAIowner","FAImode"); 
+  var $subAttributes    = array("cn","description","FAItemplatePath","FAIowner","FAImode"); 
   var $sub64coded       = array();
+  var $sub_Load_Later   = array("FAItemplateFile");
+  var $subBinary        = array("FAItemplateFile");
 
   /* Specific attributes */
   var $cn               = "";       // The class name for this object
@@ -35,6 +32,12 @@ class faiTemplate extends plugin
   var $SubObjects       = array();  // All leafobjects of this object
 
   var $FAIstate         = "";
+  var $base             = "";
+  var $release          = "";
+  var $copy_paste_mode  = false;
+  var $cut_paste_mode   = false;
+
+  var $CopyPasteVars  = array("SubObjects");
 
   function faiTemplate ($config, $dn= NULL)
   {
@@ -47,7 +50,7 @@ class faiTemplate extends plugin
      * Else we must read all objects from ldap which belong to this entry.
      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
      */
-    if($dn != "new"){
+     if($dn != "new"){
       $this->dn =$dn;
 
       /* Set acls
@@ -67,31 +70,53 @@ class faiTemplate extends plugin
        */
       $ldap     = $this->config->get_ldap_link();
       $ldap->cd ($this->dn);
-      $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$this->subAttributes);
+      $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",array("dn"));
 
       while($object = $ldap->fetch()){
+
         /* Set status for save management */
-  
-        foreach($this->subAttributes as $attrs){
-          if(!isset($object[$attrs][0])){
-            $this->SubObjects[$object['cn'][0]][$attrs]="";
-          }else{
-            $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
-          }
+        $objects = array();
+        $objects['status']      = "FreshLoaded";
+        $objects['dn']          = $object['dn'];
+        $objects                = $this->get_object_attributes($objects,$this->subAttributes);
+        $this->SubObjects[$objects['cn']] = $objects;
+      }
+    }
+  }
+
+
+  /* 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);
         }
 
-        foreach($this->sub64coded as $codeIt){
-          $this->SubObjects[$object['cn'][0]][$codeIt]= base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
-        }       
+        /*  check if this is a binary entry */
+        if(in_array_ics($attrs,$this->subBinary)){
+          $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
+        }
 
-        $this->SubObjects[$object['cn'][0]]['FAItemplateFile']= $ldap->get_attribute($object['dn'], "FAItemplateFile",$r_array=0);
-        
-        $this->SubObjects[$object['cn'][0]]['status']= "edited";
-        $this->SubObjects[$object['cn'][0]]['dn']= $object['dn'];
+        /* Fix slashes */
+        $var = addslashes($var);
+
+        $object[$attrs] = $var;
       }
     }
+    return($object);
   }
 
+
   function execute()
   {
        /* Call parent execute */
@@ -109,9 +134,15 @@ class faiTemplate extends plugin
         $once = false;
         $entry = preg_replace("/^editscript_/","",$name);
         $entry = base64_decode(preg_replace("/_.*/","",$entry));
-        $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$entry]);
+
+        $obj  = $this->SubObjects[$entry];
+        if($obj['status'] == "FreshLoaded"){
+          $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
+        }
+
+        $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
         $this->dialog->acl = $this->acl;
-        $_SESSION['objectinfo'] = $this->SubObjects[$entry]['dn'];
+        $_SESSION['objectinfo'] = $obj['dn'];
         $this->dialog->parent = &$this;
         $this->is_dialog=true;
       }
@@ -119,7 +150,9 @@ class faiTemplate extends plugin
         $once = false;
         $entry = preg_replace("/^deletescript_/","",$name);
         $entry = base64_decode(preg_replace("/_.*/","",$entry));
-        if($this->SubObjects[$entry]['status'] == "edited"){
+
+        $status = $this->SubObjects[$entry]['status'];
+        if($status == "edited" || $status == "FreshLoaded"){
           $this->SubObjects[$entry]['status']= "delete";
         }else{
           unset($this->SubObjects[$entry]);
@@ -127,10 +160,26 @@ class faiTemplate extends plugin
       }
     }
 
+    /* Edit entries via GET */
+    if(isset($_GET['act']) && isset($_GET['id'])){
+      if($_GET['act'] == "edit" && isset($this->SubObjects[$_GET['id']])){
+        $obj = $this->SubObjects[$_GET['id']];
+          if($obj['status'] == "FreshLoaded"){
+          $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
+        }
+        $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
+        $this->dialog->acl = $this->acl;
+        $_SESSION['objectinfo'] = $obj['dn'];
+        $this->dialog->parent = &$this;
+        $this->is_dialog=true;
+      }
+    }
+
     /* Add new sub object */
     if(isset($_POST['AddSubObject'])){
       $this->dialog= new $this->subClassName($this->config,"new");
       $this->dialog->acl = $this->acl;
+      $this->dialog->parent = &$this;
       $this->is_dialog=true;
     }
 
@@ -147,9 +196,14 @@ class faiTemplate extends plugin
           print_red($msg);
         }
       }else{
+
+        /* Get return object */
         $obj = $this->dialog->save();
         if(isset($obj['remove'])){
-          if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
+          $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
+
+          /* Depending on status, set new status */
+          if($old_stat == "edited" || $old_stat == "FreshLoaded"){
             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
             unset($this->SubObjects[$obj['remove']['from']]);
@@ -158,8 +212,12 @@ class faiTemplate extends plugin
           $this->SubObjects[$obj['remove']['to']] = $obj;
           unset($this->SubObjects[$obj['remove']['to']]['remove']);
         }else{
+          if($obj['status'] == "FreshLoaded"){
+            $obj['status'] = "edited";
+          }
           $this->SubObjects[$obj['cn']]=$obj;
         }
+
         $this->is_dialog=false;
         unset($this->dialog);
         $this->dialog=NULL;
@@ -191,10 +249,9 @@ class faiTemplate extends plugin
       return($display);
     }
 
-     /* Divlist            added 28.02.2006
-       Containing FAIscripts
-     */
+     /* Divlist Containing FAItemplates */
     $divlist = new divSelectBox("FAItemplates");
+    $divlist->setHeight(400);
     if((chkacl($this->acl,"cn")!="") || ($this->FAIstate == "freeze")){
       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
       $img_remo = "";
@@ -211,12 +268,14 @@ class faiTemplate extends plugin
   
         $dn = $this->SubObjects[$key]['dn'];       
  
-        $down = "<a href='getFAIscript.php?is_template&id=".base64_encode($dn)."' target='_blank'>
+        $down = "<a href='getFAIscript.php?is_template&id=".base64_encode($dn)."'>
           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
           </a>";
       }
 
-      $divlist->AddEntry(array( array("string"=>$name),
+      $edit_link = "<a href='?plug=".$_GET['plug']."&act=edit&id=".$key."'>".$name."</a>";
+
+      $divlist->AddEntry(array( array("string"=> $edit_link),
             array("string"=>$down , "attach" => "style='width:20px;'"),
             array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
               "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
@@ -257,7 +316,7 @@ class faiTemplate extends plugin
           if(strlen($obj['description']) > 40){
             $obj['description'] = substr($obj['description'],0,40)."...";
           }
-          $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
+          $a_return[$obj['cn']]= $obj['cn']." [".stripslashes( $obj['description'] )."]";
         }else{
           $a_return[$obj['cn']]= $obj['cn'];
         }
@@ -298,6 +357,19 @@ class faiTemplate extends plugin
     /* Call common method to give check the hook */
     $message= plugin::check();
 
+    /* If this is a new script, check if a script with this name already exists */
+    if(!empty($this->release) && ($this->copy_paste_mode || $this->cut_paste_mode) ){
+
+      /* Check if current name is already used for fai scripts in selected release */
+      $dn = 'cn='.$this->cn.",ou=templates,".$this->release;
+      $ldap = $this->config->get_ldap_link();
+      $ldap->cat($dn);
+      if($ldap->count()){
+
+        $r =convert_department_dn($this->release);;
+        $message[] = sprintf(_("Can't insert a fai template named '%s' in '%s' there is already a template with the given name."),$this->cn,$r);
+      }
+    }
     return ($message);
   }
 
@@ -309,7 +381,12 @@ class faiTemplate extends plugin
 
     $ldap = $this->config->get_ldap_link();
 
-    $ldap->cat($this->dn);
+    /* Copy & Paste : Ensure that FAIstate is copied too */
+    if($this->copy_paste_mode && preg_match("/freeze/",$this->FAIstate)){
+      $this->attrs['FAIstate'] = $this->FAIstate;
+    }
+
+    $ldap->cat($this->dn,array("objectClass"));
     if($ldap->count()!=0){
       /* Write FAIscript to ldap*/
       $ldap->cd($this->dn);
@@ -323,7 +400,10 @@ class faiTemplate extends plugin
       $ldap->cd($this->dn);
       $ldap->add($this->attrs);
     }
-    show_ldap_error($ldap->get_error());
+    show_ldap_error($ldap->get_error(), _("Saving FAI template base failed"));
+
+    /* Do object tagging */
+    $this->handle_object_tagging();
 
     /* Prepare FAIscriptEntry to write it to ldap
      * First sort array.
@@ -331,11 +411,25 @@ class faiTemplate extends plugin
      * After deletion, we perform add and modify 
      */
     $Objects = array();
+
+    /* We do not need to save untouched objects */
+    foreach($this->SubObjects as $name => $obj){
+      if($obj['status'] == "FreshLoaded"){
+        if($this->copy_paste_mode){
+          $this->SubObjects[$name] = $this->get_object_attributes($obj,$this->sub_Load_Later);
+          $this->SubObjects[$name]['status'] = "new";
+        }else{
+          unset($this->SubObjects[$name]);
+        }
+      }
+    }
+
     foreach($this->SubObjects as $name => $obj){
       if($obj['status'] == "delete"){
         $Objects[$name] = $obj; 
       }
     }
+
     foreach($this->SubObjects as $name => $obj){
       if($obj['status'] != "delete"){
         $Objects[$name] = $obj; 
@@ -348,11 +442,14 @@ class faiTemplate extends plugin
         $obj[$codeIt]=base64_encode($obj[$codeIt]);
       }
       $tmp = array();
-      foreach($this->subAttributes as $attrs){
+      $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
+      foreach($attributes as $attrs){
         if(empty($obj[$attrs])){
           $obj[$attrs] = array();
+          $tmp[$attrs] = $obj[$attrs];
+        }else{
+          $tmp[$attrs] = stripslashes ($obj[$attrs]);
         }
-        $tmp[$attrs] = $obj[$attrs];
       }    
         
       $tmp['objectClass'] = $this->subClasses;
@@ -360,9 +457,20 @@ class faiTemplate extends plugin
       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
 
       if($obj['status']=="new"){
-        $ldap->cat($sub_dn);
+        $ldap->cat($sub_dn,array("objectClass"));
         if($ldap->count()){
-          $obj['status']="modify";
+          $obj['status']="edited";
+        }
+      }
+
+       /* Check if gosaAdministrativeUnitTag is required as object class */
+      if($obj['status'] == "edited"){
+        $ldap->cat($sub_dn,array("objectClass"));
+        $attrs = $ldap->fetch();
+        if(isset($attrs['objectClass'])){
+          if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
+            $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
+          }
         }
       }
 
@@ -370,10 +478,12 @@ class faiTemplate extends plugin
         $ldap->cd($sub_dn);
         $ldap->rmdir_recursive($sub_dn);
         $this->handle_post_events("remove");
+        show_ldap_error($ldap->get_error(), _("Removing FAI template entry failed")); 
       }elseif($obj['status'] == "edited"){
         $ldap->cd($sub_dn);
         $this->cleanup();
-$ldap->modify ($tmp); 
+        $ldap->modify ($tmp); 
+        show_ldap_error($ldap->get_error(), _("Modifying FAI template entry failed")); 
 
         $this->handle_post_events("modify");
       }elseif($obj['status']=="new"){
@@ -386,8 +496,32 @@ $ldap->modify ($tmp);
         $ldap->cd($sub_dn);
         $ldap->add($tmp); 
         $this->handle_post_events("add");
+        show_ldap_error($ldap->get_error(), _("Creating FAI template entry failed")); 
       }
-      show_ldap_error($ldap->get_error()); 
+
+      $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
+    }
+  }
+  
+  /* return copy & paste dialog
+   */
+  function getCopyDialog()
+  {
+    /* Ask for cn */
+    $smarty = get_smarty();
+    $smarty->assign("cn" ,$this->cn);
+    $str = $smarty->fetch(get_template_path("paste_fai_object.tpl",TRUE));
+    $ret = array();
+    $ret['string'] = $str;
+    $ret['status'] = "";
+    return($ret);
+  }
+
+  /* Get posted cn */
+  function saveCopyDialog()
+  {
+    if(isset($_POST['cn'])){
+      $this->cn = $_POST['cn'];
     }
   }
 }