Code

Reverted last changes
authorhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Thu, 20 Oct 2005 07:03:44 +0000 (07:03 +0000)
committerhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Thu, 20 Oct 2005 07:03:44 +0000 (07:03 +0000)
git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@1627 594d385d-05f5-0310-b6e9-bd551577e9d8

plugins/admin/fai/class_faiScript.inc
plugins/admin/fai/class_faiScriptEntry.inc
plugins/admin/fai/faiScript.tpl
plugins/admin/fai/faiScriptEntry.tpl
plugins/admin/fai/tabsScript.inc

index 197fdc44bcdf142da2ed98c2937918f4f35ca660..57de6487616fb21044f49822a5f157f949d99f71 100644 (file)
@@ -16,6 +16,17 @@ class faiScript extends plugin
   /* ObjectClasses for this Object*/
   var $objectclasses    = array("top","FAIclass","FAIscript");
 
+  /* Class name of the Ldap ObjectClass for the Sub Object */
+  var $subClass         = "FAIscriptEntry";
+  var $subClasses       = array("top","FAIclass","FAIscriptEntry");
+
+  /* Class name of the php class which allows us to edit a Sub Object */
+  var $subClassName     = "faiScriptEntry";      
+
+  /* Attributes to initialise for each subObject */
+  var $subAttributes    = array("cn","description","FAIpriority","FAIscript"); 
+  var $sub64coded       = array("FAIscript");
+
   /* Specific attributes */
   var $cn               = "";       // The class name for this object
   var $description      = "";       // The description for this set of partitions
@@ -23,13 +34,47 @@ class faiScript extends plugin
   var $dialog           = NULL;     // a dialog, e.g. new disk dialog
   var $SubObjects       = array();  // All leafobjects of this object
 
-  /* new dn*/
-  var $use_dn ="";
-
   function faiScript ($config, $dn= NULL)
   {
     /* Load Attributes */
     plugin::plugin ($config, $dn);
+
+    /* 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 SubObjects from ldap ... and then the partition definitions for the SubObjects.
+     */
+    if($dn != "new"){
+      $this->dn =$dn;
+
+      /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
+       */
+      $ldap     = $this->config->get_ldap_link();
+      $ldap->cd ($this->dn);
+      $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$this->subAttributes);
+
+      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];
+          }
+        }
+     
+        foreach($this->sub64coded as $codeIt){
+          $this->SubObjects[$object['cn'][0]][$codeIt]=(base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]));
+        }
+
+        foreach($this->subAttributes as $attrs){
+          $this->SubObjects[$object['cn'][0]][$attrs]=addslashes($this->SubObjects[$object['cn'][0]][$attrs]);
+        }
+        $this->SubObjects[$object['cn'][0]]['status']      = "edited";
+        $this->SubObjects[$object['cn'][0]]['dn']          = $object['dn'];
+      }
+    }
   }
 
   function execute()
@@ -40,23 +85,23 @@ class faiScript extends plugin
 
     /* Add new sub object */
     if(isset($_POST['AddSubObject'])){
-      $this->dialog= new faiScriptEntry($this->config,"new");
-      $this->dialog->parent = &$this;
+      $this->dialog= new $this->subClassName($this->config,"new");
       $this->is_dialog=true;
     }
 
     /* Edit selected Sub Object */
     if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
-      $this->dialog= new faiScriptEntry($this->config,$this->SubObjects[$_POST['SubObject']]['dn']);
-      $this->dialog->parent = &$this;
+      $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$_POST['SubObject']]);
       $this->is_dialog=true;
     }
     
     /* Remove Sub object */
     if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
-      $tmp = new faiScriptEntry($this->config,$this->SubObjects[$_POST['SubObject']]['dn']);
-      $tmp->remove_from_parent();
-      unset($tmp);
+      if($this->SubObjects[$_POST['SubObject']]['status'] == "edited"){
+        $this->SubObjects[$_POST['SubObject']]['status']= "delete";
+      }else{
+        unset($this->SubObjects[$_POST['SubObject']]);
+      }
     }
 
     /* Save Dialog */
@@ -68,8 +113,19 @@ class faiScript extends plugin
           print_red($msg);
         }
       }else{
-        $this->dialog->save_object();
-        $this->dialog->save();
+        $obj = $this->dialog->save();
+        if(isset($obj['remove'])){
+          if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
+            $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
+          }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
+            unset($this->SubObjects[$obj['remove']['from']]);
+          }
+          $obj['status'] = "new";
+          $this->SubObjects[$obj['remove']['to']] = $obj;
+          unset($this->SubObjects[$obj['remove']['to']]['remove']);
+        }else{
+          $this->SubObjects[$obj['cn']]=$obj;
+        }
         $this->is_dialog=false;
         unset($this->dialog);
         $this->dialog=NULL;
@@ -90,9 +146,8 @@ class faiScript extends plugin
       return($display);
     }
 
-    $buffer = $this->getList();
-    $smarty->assign("SubObjects",$buffer);
-    $smarty->assign("SubObjectKeys",array_flip($buffer));
+    $smarty->assign("SubObjects",$this->getList());
+    $smarty->assign("SubObjectKeys",array_flip($this->getList()));
      
     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
     * If we post the escaped strings they will be escaped again
@@ -112,25 +167,14 @@ class faiScript extends plugin
   /* Generate listbox friendly SubObject list
   */
   function getList(){
-    $a_return         = array();
-    $this->SubObjects = array();
-
-    $ldap     = $this->config->get_ldap_link();
-    $ldap->cd ($this->config->current['BASE']);
-    $ldap->cd ($this->dn);
-  
-    $ldap->search("(objectClass=FAIscriptEntry)",array("cn"));
-    
-    while($entry = $ldap->fetch()){
-      $tmp = array();
-      $tmp['cn'] = $entry['cn'][0]; 
-      $tmp['dn']=$entry['dn']; 
-      $this->SubObjects[$tmp['cn']] = $tmp;
-
-      if(isset($entry['description'][0])){
-        $a_return[$tmp['cn']] = $tmp['cn']." [".$entry['description'][0]."]";
-      }else{
-        $a_return[$tmp['cn']] = $tmp['cn'];
+    $a_return=array();
+    foreach($this->SubObjects as $obj){
+      if($obj['status'] != "delete"){
+        if((isset($obj['description']))&&(!empty($obj['description']))){
+          $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
+        }else{
+          $a_return[$obj['cn']]= $obj['cn'];
+        }
       }
     }
     return($a_return);
@@ -143,7 +187,6 @@ class faiScript extends plugin
     $ldap = $this->config->get_ldap_link();
     $ldap->cd ($this->dn);
     $ldap->rmdir_recursive($this->dn);
-    show_ldap_error($ldap->get_error());
     $this->handle_post_events("remove");    
   }
 
@@ -152,12 +195,10 @@ class faiScript extends plugin
    */
   function save_object()
   {
-    if(isset($_POST['FAIscript_submit'])){
-      plugin::save_object();
-      foreach($this->attributes as $attrs){
-        if(isset($_POST[$attrs])){
-          $this->$attrs = $_POST[$attrs];
-        }
+    plugin::save_object();
+    foreach($this->attributes as $attrs){
+      if(isset($_POST[$attrs])){
+        $this->$attrs = $_POST[$attrs];
       }
     }
   }
@@ -175,12 +216,9 @@ class faiScript extends plugin
   function save()
   {
     plugin::save();
-
-    if($this->dn == "new"){
-      $this->dn = $this->use_dn;
-    }
  
     $ldap = $this->config->get_ldap_link();
+  
     $ldap->cat($this->dn);
     if($ldap->count()!=0){
       /* Write FAIscript to ldap*/
@@ -194,6 +232,76 @@ class faiScript extends plugin
       $ldap->add($this->attrs);
     }
     show_ldap_error($ldap->get_error());
+    /* Prepare FAIscriptEntry to write it to ldap
+     * First sort array.
+     *  Because we must delete old entries first.
+     * After deletion, we perform add and modify 
+     */
+    $Objects = array();
+    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; 
+      }
+    }
+
+    foreach($Objects as $name => $obj){
+
+      foreach($this->sub64coded as $codeIt){
+        $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
+      }
+
+      $tmp = array();
+      foreach($this->subAttributes as $attrs){
+        if(empty($obj[$attrs])){
+          $obj[$attrs] = array();
+        }
+        if(!is_array($obj[$attrs])){
+          $tmp[$attrs] = stripslashes($obj[$attrs]);
+        }else{
+          $tmp[$attrs] = $obj[$attrs];
+        }
+      }    
+        
+      $tmp['objectClass'] = $this->subClasses;
+
+      $sub_dn = "cn=".$obj['cn'].",".$this->dn;
+
+      if($obj['status']=="new"){
+        $ldap->cat($sub_dn);
+        if($ldap->count()){
+          $obj['status']="modify";
+        }
+      }
+      if($obj['status'] == "delete"){
+        $ldap->cd($sub_dn);
+        $ldap->rmdir_recursive($sub_dn);
+        $this->handle_post_events("remove");
+      }elseif($obj['status'] == "edited"){
+        $ldap->cd($sub_dn);
+        $ldap->modify($tmp);
+        $this->handle_post_events("modify");
+      }elseif($obj['status']=="new"){
+        if($tmp['description']==array()){
+          unset($tmp['description']);
+        }
+        if($tmp['FAIscript']==array()){
+          $tmp['FAIscript']=" ";
+        }
+        $ldap->cd($this->config->current['BASE']);
+        $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
+        $ldap->cd($sub_dn);
+        $ldap->add($tmp); 
+        $this->handle_post_events("add");
+      }
+      show_ldap_error($ldap->get_error()); 
+    }
   }
 }
 
index e2148014f10384477609556c510980887142ac0d..1fb0239d9088aa3a47b1b1d011c65f719399a986 100644 (file)
@@ -3,36 +3,37 @@
 class faiScriptEntry 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");
+  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;
-  var $attributes   = array("cn","description","FAIpriority","FAIscript");
-  var $objectclasses= array("FAIscriptEntry");
+  var $attributes   = array("Object_cn","Object_description","Object_FAIpriority","Object_FAIscript");
+  var $objectclasses= array();
 
-  var $orig_cn          = "";
-  var $dn               = "";
-  var $cn               = "";
-  var $FAIpriority      = "";
-  var $FAIscript        = "";
-  var $description      = "";
-  var $status           = "new";
-  var $parent           = false;
-  var $is_parent_saved  = false;
+  var $orig_cn              = "";
+
+  var $Object_dn            = "";
+  var $Object_cn            = "";
+  var $Object_FAIpriority   = "";
+  var $Object_FAIscript     = "";
+  var $Object_description   = "";
+  var $Object_status        = "new";
   
-  function faiScriptEntry ($config, $dn)
+  function faiScriptEntry ($config, $dn= NULL,$object=false)
   {
-    /* Dn is dn currently assigned to this object 
-        'new' if object wasn't saved yet
-     */
     plugin::plugin ($config, $dn);
     if($dn != "new"){
-      $this->orig_cn         = $this->cn;
+      $this->orig_cn= $object['cn'];
+      $this->dn=$object['dn'];
+      foreach($object as $name=>$value){
+        $oname = "Object_".$name;
+        $this->$oname=$value;
+      }
     }else{
-      $this->status          = "new";
-      $this->orig_cn         = false;
+      $this->Object_status = "new";
+      $this->orig_cn       = false;
     }
   }
 
@@ -50,9 +51,10 @@ class faiScriptEntry extends plugin
         print_red(_("Selected file is empty."));
       }else{
         $str = utf8_encode(file_get_contents($_FILES['ImportFile']['tmp_name']));
-        $this->FAIscript = $str;
+        $this->Object_FAIscript = $str;
       }
     }
+
   
     foreach($this->attributes as $attrs){
       if(get_magic_quotes_gpc()){
@@ -63,25 +65,13 @@ class faiScriptEntry extends plugin
     }
 
     for($i =0 ; $i < 100 ; $i++){
-      $FAIprioritys[$i]=$i;
+      $Object_FAIprioritys[$i]=$i;
     }
-    $smarty->assign("FAIprioritys",$FAIprioritys);
+    $smarty->assign("Object_FAIprioritys",$Object_FAIprioritys);
     $display.= $smarty->fetch(get_template_path('faiScriptEntry.tpl', TRUE));
     return($display);
   }
 
-  /* Delete me, and all my subtrees
-  */
-  function remove_from_parent()
-  {
-    $ldap = $this->config->get_ldap_link();
-    $ldap->cd ($this->dn);
-    $ldap->rmdir_recursive($this->dn);
-    show_ldap_error($ldap->get_error());
-    $this->handle_post_events("remove");
-  }
-
-
   /* Save data to object */
   function save_object()
   {
@@ -101,11 +91,11 @@ class faiScriptEntry extends plugin
   {
     $message= array();
   
-    if(empty($this->cn)){
+    if(empty($this->Object_cn)){
       $message[] = _("Please enter a name.");
     }
 
-    if(preg_match("/[^0-9a-z]/i",$this->cn)){
+    if(preg_match("/[^0-9a-z]/i",$this->Object_cn)){
       $message[] = _("Please enter a valid name. Only a-Z 0-9 are allowed.");
     }
  
@@ -114,45 +104,20 @@ class faiScriptEntry extends plugin
  
   function save()
   {
-    plugin::save();
-
-    /* Get ldap connection */
-    $ldap = $this->config->get_ldap_link();
-    $ldap->cd ($this->config->current['BASE']);
-
-    /* First : Check if parent was already saved ... */
-    $ldap->cd($this->parent->dn);
-    $ldap->cat($this->parent->dn);
-    
-    /* no entry was found. So save our base object first */
-    if($ldap->count()==0){
-      $this->parent->save();
-    } 
-
-    /* Generate this->dn */ 
-      /* if !orig_dn -> This is a new object */
-    if($this->orig_cn == false){
-      $this->dn = "cn=".$this->cn.",".$this->parent->dn;
-      $mode = "add";
-    }else{
-      /* This is an already existing entry, check if cn was changed */
-      if($this->orig_cn != $this->cn ){
-        /* rename */
-        $ldap->cd($this->dn);
-        $ldap->rmdir($this->dn);
-        $this->dn = "cn=".$this->cn.",".$this->parent->dn;
-        $mode = "add";
-      }else{
-        /* modify */
-        $mode = "modify";
-      }
+    $tmp=array();
+    foreach($this->attributes as $attrs){ 
+      $attr = preg_replace("/^Object_/","",$attrs);
+      $tmp[$attr] = $this->$attrs;
     }
 
-    $ldap->cd($this->config->current['BASE']);
-    $ldap->create_missing_trees($this->parent->dn);
-    $ldap->cd ($this->dn);
-    $ldap->$mode($this->attrs);
-    show_ldap_error($ldap->get_error());
+    if(($this->orig_cn)&&($tmp['cn']!=$this->orig_cn)){
+      $tmp['remove']['from']  = $this->orig_cn;
+      $tmp['remove']['to']    = $tmp['cn'];
+    }
+  
+    $tmp['dn']      = $this->dn;  
+    $tmp['status']  = $this->Object_status; 
+    return($tmp);
   }
 }
 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
index 43000d7ee30ab57f9042b3338fd3f198cafe93e5..7896626c7f0e3e344c1d87db8dd671d2544a5ff5 100644 (file)
@@ -49,4 +49,3 @@
                </td>
        </tr>
 </table>
-<input type="hidden" value="1" name="FAIscript_submit">
index 6a137a4bafe4c0f05599989b364847fe51fadd17..0017e6095012e2b3bb2d43019a4dc1c5887bcddb 100644 (file)
@@ -9,7 +9,7 @@
                                                        {t}Name{/t}{$must}&nbsp;
                                                </td>
                                                <td>
-                                                       <input value="{$cn}" name="cn">
+                                                       <input value="{$Object_cn}" name="Object_cn">
                                                </td>
                                        </tr>
                                        <tr>
@@ -17,7 +17,7 @@
                                                        {t}Description{/t}&nbsp;
                                                </td>
                                                <td>
-                                                       <input size=40 value="{$description}" name="description">
+                                                       <input size=40 value="{$Object_description}" name="Object_description">
                                                </td>
                                        </tr>
                                </table>
                                <table width="100%">
                                        <tr>
                                                <td>
-                                                       <LABEL for="FAIpriority">
+                                                       <LABEL for="Object_FAIpriority">
                                                        {t}Priority{/t}
                                                        </LABEL>
                                                </td>
                                                <td>
-                                                   <select name="FAIpriority" title="{t}Choose a priority{/t}" FAIpriority>
-                                               {html_options values=$FAIprioritys output=$FAIprioritys selected=$FAIpriority}
+                                                   <select name="Object_FAIpriority" title="{t}Choose a priority{/t}" Object_FAIpriority>
+                                               {html_options values=$Object_FAIprioritys output=$Object_FAIprioritys selected=$Object_FAIpriority}
                                        </select>
                                                </td>
                                        </tr>
 </table>
 <p class="seperator">&nbsp;</p>
 <h2><img alt="" src="images/fai_script.png" align="middle" title="{t}Script attributes{/t}">&nbsp;
-       <LABEL for="FAIscript">
+       <LABEL for="Object_FAIscript">
                {t}Script{/t}
        </LABEL>
        </h2>
 <table width="99%">
     <tr>
         <td>
-            <textarea name="FAIscript" style="width:100%;height:300px;" id="FAIscript">{$FAIscript}</textarea>
+            <textarea name="Object_FAIscript" style="width:100%;height:300px;" id="Object_FAIscript">{$Object_FAIscript}</textarea>
         </td>
     </tr>
 </table>
index 75f7e39d08f0f598f2eed6b2f723de28d775ad4c..b9a63d34bf78265dc162219924903188022d5a6e 100644 (file)
@@ -4,22 +4,16 @@ class tabsScript extends tabs
 {
   var $base= "";
 
-  function tabsScript($config, $data, $dn, $new_cn = "")
+  function tabsScript($config, $data, $dn)
   {
        tabs::tabs($config, $data, $dn);
-
-       /* Directly assign a valid dn 
-     * cn can't be changed anymore, since the object was created
-     */
-       $baseobject= $this->by_object['faiScript'];
-    $use_dn= 'cn='.$new_cn.",ou=scripts,ou=fai,ou=configs,ou=systems,".$_SESSION['faifilter']['base'];
-       $this->by_object['faiScript']->use_dn=$use_dn;
   }
 
   function check()
   {
        return (tabs::check(TRUE));
   }
+  
 
   function save()
   {