Code

Some changes.
[gosa.git] / include / sieve / class_My_Tree.inc
index 9499fd6416135bd576254eef51f8dda75ea26fc9..a8321a4502bef0c73afe47e197e0ff0135c85b6a 100644 (file)
@@ -32,14 +32,22 @@ class My_Tree extends Tree
     }
 
     /* Create html results */
-    $this->dump_ ="<table width='100%'><tr><td style='background-color:#BBBBBB;border: 0px;padding-left:20px;'>";
+    $smarty = get_smarty();
+    $smarty->fetch(get_template_path("templates/element_stop.tpl",TRUE,dirname(__FILE__)));
+
+    $this -> dump_ = "";
     foreach($this->pap as $key => $object){
       if(is_object($object)){
         $this->dump_ .= preg_replace("/>/",">\n",$object->execute()); 
       }
     }
-    $this->dump_ .= "</td></tr></table>";
-    return $this->dump_;
+    
+
+    /* Create html results */
+    $smarty = get_smarty();
+    $smarty->assign("Contents",$this->dump_);
+    $ret = $smarty->fetch(get_template_path("templates/edit_frame_base.tpl",TRUE,dirname(__FILE__)));
+    return ($ret);
   }
 
 
@@ -141,9 +149,279 @@ class My_Tree extends Tree
       if(in_array(get_class($obj),array("sieve_if","sieve_elsif","sieve_vacation","sieve_comment","sieve_reject","sieve_fileinto","sieve_require","sieve_redirect"))){
         $this->pap[$key]->save_object();
       }
+
+      $once = TRUE;
+      foreach($_POST as $name => $value){
+
+        if(isset($obj->object_id) && preg_match("/^Remove_Object_".$obj->object_id."_/",$name) && $once){
+          $once = FALSE;
+          $this->remove_object($key);
+        }
+        if(isset($obj->object_id) && preg_match("/^Move_Up_Object_".$obj->object_id."_/",$name) && $once){
+          $this->move_up_down($key,"up");
+          $once = FALSE;
+        }
+        if(isset($obj->object_id) && preg_match("/^Move_Down_Object_".$obj->object_id."_/",$name) && $once){
+          $this->move_up_down($key,"down");
+          $once = FALSE;
+        }
+      }
     }
   }
 
+
+  function remove_object($key_id){
+    unset($this->pap[$key_id]);
+  }
+
+
+  /* This function moves a given element to another position.
+   * Single elements like "keep;" will simply be moved one posisition down/up.
+   * Multiple elements like if-elsif-else will be moved as block. 
+   * 
+   *  $key_id     specified the element that should be moved.
+   *  $direction  specifies to move elements "up" or "down"
+   */
+  function move_up_down($key_id,$direction = "down")
+  {
+     
+    /* Get the current element to decide what to move. */ 
+    $e_class = get_class($this->pap[$key_id]);
+      
+    
+    if(in_array($e_class,array("sieve_if"))){
+      echo "move block";
+    }
+
+    if(in_array($e_class,array("sieve_stop","sieve_keep","sieve_require", "sieve_stop", "sieve_reject", "sieve_fileinto", "sieve_redirect", "sieve_discard"))){
+      echo "move single ".$key_id." to ".$this->_get_next_free_move_slot($key_id,$direction)."<br>";
+      $this->move_single_element_to($key_id,$this->_get_next_free_move_slot($key_id,$direction));
+    }
+  }
+
+
+  function move_single_element_to($from,$to)
+  {
+    if($from == $to) return;
+
+    $ret = array();
+
+
+    $tmp = $this->pap;
+
+#    $tmp = array();
+ #  foreach($this->pap as $class){
+  #    $tmp[] = get_class($class);
+   # }
+
+    if($from > $to ){
+
+      $element = $this->pap[$from];
+
+      $begin = array();
+      $middle = array();
+      $end = array();
+
+      /* Get all element in fron to element to move */    
+      if($from  != 0){
+        $begin = array_slice($tmp,0,$to);
+      }
+
+      /* Get all elements between */
+      $middle = array_slice($tmp,$to , ($from - ($to) ));  
+    
+      /* Get the rest */ 
+      $end  = array_slice($tmp,$from+1);
+      foreach($begin as $data){
+        $ret[] = $data;
+      }
+      $ret[] = $element;
+      foreach($middle as $data){
+        $ret[] = $data;
+      }
+      foreach($end as $data){
+        $ret[] = $data;
+      }
+
+#      print_a(array("Anfang" => $begin ,$element, "middle" => $middle, "end" => $end));
+
+      $this->pap = $ret;
+    }
+    if($from < $to ){
+
+      $element = $this->pap[$from];
+
+      $begin = array();
+      $middle = array();
+      $end = array();
+
+      /* Get all element in fron to element to move */    
+      if($from  != 0){
+        $begin = array_slice($tmp,0,$from);
+      }
+
+      /* Get all elements between */
+      $middle = array_slice($tmp,$from+1 , ($to - ($from)));  
+    
+      /* Get the rest */ 
+      $end  = array_slice($tmp,$to+1);
+      foreach($begin as $data){
+        $ret[] = $data;
+      }
+      foreach($middle as $data){
+        $ret[] = $data;
+      }
+      $ret[] = $element;
+      foreach($end as $data){
+        $ret[] = $data;
+      }
+      $this->pap = $ret;
+    }
+  }
+
+
+  function _get_next_free_move_slot($key_id,$direction)
+  {
+    $last_class = "";
+    $current_class ="";
+    $next_class = "";
+
+    $allowed_to_add_after = array("sieve_keep",
+                                  "sieve_require", 
+                                  "sieve_stop", 
+                                  "sieve_reject", 
+                                  "sieve_fileinto", 
+                                  "sieve_redirect", 
+                                  "sieve_discard",
+                                  "sieve_block_end");
+
+    if($direction == "down"){
+      $test = $this->pap;
+
+
+      while($key_id < count($test)){
+       
+        if(($key_id+1) == count($test)) return($key_id);
+
+        $key_id ++;
+
+        $current_class  = get_class($test[$key_id]);
+        if(in_array($current_class, $allowed_to_add_after)){
+          return($key_id);
+        } 
+      }
+    }else{
+
+      $test = $this->pap;
+
+      if($key_id == 0) return($key_id);
+    
+      $key_id --;
+      while($key_id >=0 ){
+
+        $current_class  = get_class($test[$key_id]);
+        if(in_array($current_class, $allowed_to_add_after)){
+          return($key_id);
+        } 
+        $key_id --;
+      }
+    }
+  }
+
+  function move_object_up($key_id)
+  {
+    $new = array();
+    $add_now = NULL;
+    $key_id --;
+
+    if(!$key_id < 0) return;
+  
+    foreach($this->pap as $key => $data){
+      if($key == $key_id){
+        $add_now = $data;
+        continue;
+      }else{
+        $new[] = $data;
+      }
+
+      if($add_now != NULL){
+          
+        $new[] = $add_now;
+        $add_now = NULL;
+      }
+    }
+    if($add_now != NULL){
+      $new[] = $add_now;
+    }
+    $this->pap = $new;
+  }
+
+
+  /* This function moves the given element one position down 
+   *  if the next position is between 
+   *   '}' 'else[if]' or 'if' '{' use next available
+   */
+  function move_object_down($key_id)
+  {
+    $new = array();
+    $add_now = NULL;
+
+    /* Walk through all elements, till we found the given id.
+     *  If we found it, skip adding the current element,
+     *  first add the next element followed by the current.
+     */
+    foreach($this->pap as $key => $data){
+
+      /* Have we found the given id */
+      if($key == $key_id){
+        $add_now = $data;
+        $last_class = get_class($data);
+        continue;
+      }else{
+      
+        /* Add entry */
+        $new[] = $data;
+      }
+
+      /* We have skipped adding an element before,
+       *  try to add it now, if the position allows this.
+       */
+      if($add_now != NULL){
+
+        /* Don't allow adding an element directly after 
+         *  if/else/elsif 
+         */
+        if(in_array(get_class($data),array("sieve_if","sieve_elsif","sieve_else"))){
+          continue;
+        }
+
+        /* If this is an block end, check if there 
+         *  follows an if/else/elsif and skip adding the element in this case.
+         */
+        $next ="";
+        if(isset($this->pap[$key+1])){
+          $next = get_class($this->pap[$key+1]);
+        }
+        if(in_array(get_class($data),array("sieve_block_end")) && in_array($next,array("sieve_elsif","sieve_else"))){
+          continue;
+        }
+  
+        /* Add element, position seems to be ok */
+        $new[] = $add_now;
+        $add_now = NULL;
+      }
+    }
+
+    /* Element wasn't added, add it as last element */
+    if($add_now != NULL){
+      $new[] = $add_now;
+    }
+    $this->pap = $new;
+  }
+
+
   /* Need to be reviewed */
   function get_sieve_script()
   {
@@ -177,6 +455,12 @@ class My_Tree extends Tree
     }
     return($tmp);
   }
+
+  function Add_Element()
+  {
+    $tmp = array("ELEMENTS" => array(array("class" => "qouted-string","text"=> "Bla bla, later more")));
+    $this->pap[] = new sieve_comment($tmp,rand(1000,100000));
+  }
 }