Code

Script management changed.
authorhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Thu, 20 Oct 2005 06:00:30 +0000 (06:00 +0000)
committerhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Thu, 20 Oct 2005 06:00:30 +0000 (06:00 +0000)
1  FAIscriptEntry will be saved directly,
   instead of only saving the changes if the base object (FAIscript) was saved.

git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@1626 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 57de6487616fb21044f49822a5f157f949d99f71..197fdc44bcdf142da2ed98c2937918f4f35ca660 100644 (file)
@@ -16,17 +16,6 @@ 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
@@ -34,47 +23,13 @@ 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()
@@ -85,23 +40,23 @@ class faiScript extends plugin
 
     /* Add new sub object */
     if(isset($_POST['AddSubObject'])){
-      $this->dialog= new $this->subClassName($this->config,"new");
+      $this->dialog= new faiScriptEntry($this->config,"new");
+      $this->dialog->parent = &$this;
       $this->is_dialog=true;
     }
 
     /* Edit selected Sub Object */
     if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
-      $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$_POST['SubObject']]);
+      $this->dialog= new faiScriptEntry($this->config,$this->SubObjects[$_POST['SubObject']]['dn']);
+      $this->dialog->parent = &$this;
       $this->is_dialog=true;
     }
     
     /* Remove Sub object */
     if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
-      if($this->SubObjects[$_POST['SubObject']]['status'] == "edited"){
-        $this->SubObjects[$_POST['SubObject']]['status']= "delete";
-      }else{
-        unset($this->SubObjects[$_POST['SubObject']]);
-      }
+      $tmp = new faiScriptEntry($this->config,$this->SubObjects[$_POST['SubObject']]['dn']);
+      $tmp->remove_from_parent();
+      unset($tmp);
     }
 
     /* Save Dialog */
@@ -113,19 +68,8 @@ class faiScript extends plugin
           print_red($msg);
         }
       }else{
-        $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->dialog->save_object();
+        $this->dialog->save();
         $this->is_dialog=false;
         unset($this->dialog);
         $this->dialog=NULL;
@@ -146,8 +90,9 @@ class faiScript extends plugin
       return($display);
     }
 
-    $smarty->assign("SubObjects",$this->getList());
-    $smarty->assign("SubObjectKeys",array_flip($this->getList()));
+    $buffer = $this->getList();
+    $smarty->assign("SubObjects",$buffer);
+    $smarty->assign("SubObjectKeys",array_flip($buffer));
      
     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
     * If we post the escaped strings they will be escaped again
@@ -167,14 +112,25 @@ class faiScript extends plugin
   /* Generate listbox friendly SubObject list
   */
   function getList(){
-    $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'];
-        }
+    $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'];
       }
     }
     return($a_return);
@@ -187,6 +143,7 @@ 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");    
   }
 
@@ -195,10 +152,12 @@ class faiScript extends plugin
    */
   function save_object()
   {
-    plugin::save_object();
-    foreach($this->attributes as $attrs){
-      if(isset($_POST[$attrs])){
-        $this->$attrs = $_POST[$attrs];
+    if(isset($_POST['FAIscript_submit'])){
+      plugin::save_object();
+      foreach($this->attributes as $attrs){
+        if(isset($_POST[$attrs])){
+          $this->$attrs = $_POST[$attrs];
+        }
       }
     }
   }
@@ -216,9 +175,12 @@ 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*/
@@ -232,76 +194,6 @@ 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 1fb0239d9088aa3a47b1b1d011c65f719399a986..e2148014f10384477609556c510980887142ac0d 100644 (file)
@@ -3,37 +3,36 @@
 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("Object_cn","Object_description","Object_FAIpriority","Object_FAIscript");
-  var $objectclasses= array();
+  var $attributes   = array("cn","description","FAIpriority","FAIscript");
+  var $objectclasses= array("FAIscriptEntry");
 
-  var $orig_cn              = "";
-
-  var $Object_dn            = "";
-  var $Object_cn            = "";
-  var $Object_FAIpriority   = "";
-  var $Object_FAIscript     = "";
-  var $Object_description   = "";
-  var $Object_status        = "new";
+  var $orig_cn          = "";
+  var $dn               = "";
+  var $cn               = "";
+  var $FAIpriority      = "";
+  var $FAIscript        = "";
+  var $description      = "";
+  var $status           = "new";
+  var $parent           = false;
+  var $is_parent_saved  = false;
   
-  function faiScriptEntry ($config, $dn= NULL,$object=false)
+  function faiScriptEntry ($config, $dn)
   {
+    /* 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= $object['cn'];
-      $this->dn=$object['dn'];
-      foreach($object as $name=>$value){
-        $oname = "Object_".$name;
-        $this->$oname=$value;
-      }
+      $this->orig_cn         = $this->cn;
     }else{
-      $this->Object_status = "new";
-      $this->orig_cn       = false;
+      $this->status          = "new";
+      $this->orig_cn         = false;
     }
   }
 
@@ -51,10 +50,9 @@ class faiScriptEntry extends plugin
         print_red(_("Selected file is empty."));
       }else{
         $str = utf8_encode(file_get_contents($_FILES['ImportFile']['tmp_name']));
-        $this->Object_FAIscript = $str;
+        $this->FAIscript = $str;
       }
     }
-
   
     foreach($this->attributes as $attrs){
       if(get_magic_quotes_gpc()){
@@ -65,13 +63,25 @@ class faiScriptEntry extends plugin
     }
 
     for($i =0 ; $i < 100 ; $i++){
-      $Object_FAIprioritys[$i]=$i;
+      $FAIprioritys[$i]=$i;
     }
-    $smarty->assign("Object_FAIprioritys",$Object_FAIprioritys);
+    $smarty->assign("FAIprioritys",$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()
   {
@@ -91,11 +101,11 @@ class faiScriptEntry extends plugin
   {
     $message= array();
   
-    if(empty($this->Object_cn)){
+    if(empty($this->cn)){
       $message[] = _("Please enter a name.");
     }
 
-    if(preg_match("/[^0-9a-z]/i",$this->Object_cn)){
+    if(preg_match("/[^0-9a-z]/i",$this->cn)){
       $message[] = _("Please enter a valid name. Only a-Z 0-9 are allowed.");
     }
  
@@ -104,20 +114,45 @@ class faiScriptEntry extends plugin
  
   function save()
   {
-    $tmp=array();
-    foreach($this->attributes as $attrs){ 
-      $attr = preg_replace("/^Object_/","",$attrs);
-      $tmp[$attr] = $this->$attrs;
-    }
+    plugin::save();
 
-    if(($this->orig_cn)&&($tmp['cn']!=$this->orig_cn)){
-      $tmp['remove']['from']  = $this->orig_cn;
-      $tmp['remove']['to']    = $tmp['cn'];
+    /* 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['dn']      = $this->dn;  
-    $tmp['status']  = $this->Object_status; 
-    return($tmp);
+
+    $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());
   }
 }
 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
index 7896626c7f0e3e344c1d87db8dd671d2544a5ff5..43000d7ee30ab57f9042b3338fd3f198cafe93e5 100644 (file)
@@ -49,3 +49,4 @@
                </td>
        </tr>
 </table>
+<input type="hidden" value="1" name="FAIscript_submit">
index 0017e6095012e2b3bb2d43019a4dc1c5887bcddb..6a137a4bafe4c0f05599989b364847fe51fadd17 100644 (file)
@@ -9,7 +9,7 @@
                                                        {t}Name{/t}{$must}&nbsp;
                                                </td>
                                                <td>
-                                                       <input value="{$Object_cn}" name="Object_cn">
+                                                       <input value="{$cn}" name="cn">
                                                </td>
                                        </tr>
                                        <tr>
@@ -17,7 +17,7 @@
                                                        {t}Description{/t}&nbsp;
                                                </td>
                                                <td>
-                                                       <input size=40 value="{$Object_description}" name="Object_description">
+                                                       <input size=40 value="{$description}" name="description">
                                                </td>
                                        </tr>
                                </table>
                                <table width="100%">
                                        <tr>
                                                <td>
-                                                       <LABEL for="Object_FAIpriority">
+                                                       <LABEL for="FAIpriority">
                                                        {t}Priority{/t}
                                                        </LABEL>
                                                </td>
                                                <td>
-                                                   <select name="Object_FAIpriority" title="{t}Choose a priority{/t}" Object_FAIpriority>
-                                               {html_options values=$Object_FAIprioritys output=$Object_FAIprioritys selected=$Object_FAIpriority}
+                                                   <select name="FAIpriority" title="{t}Choose a priority{/t}" FAIpriority>
+                                               {html_options values=$FAIprioritys output=$FAIprioritys selected=$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="Object_FAIscript">
+       <LABEL for="FAIscript">
                {t}Script{/t}
        </LABEL>
        </h2>
 <table width="99%">
     <tr>
         <td>
-            <textarea name="Object_FAIscript" style="width:100%;height:300px;" id="Object_FAIscript">{$Object_FAIscript}</textarea>
+            <textarea name="FAIscript" style="width:100%;height:300px;" id="FAIscript">{$FAIscript}</textarea>
         </td>
     </tr>
 </table>
index b9a63d34bf78265dc162219924903188022d5a6e..75f7e39d08f0f598f2eed6b2f723de28d775ad4c 100644 (file)
@@ -4,16 +4,22 @@ class tabsScript extends tabs
 {
   var $base= "";
 
-  function tabsScript($config, $data, $dn)
+  function tabsScript($config, $data, $dn, $new_cn = "")
   {
        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()
   {