Code

Fixed mode switch
[gosa.git] / include / sieve / class_sieveManagement.inc
index cfbbb748ec2520d443a5d2751e14df5ab754cf23..0bf128d216c0566b0b5ad79df7f28f4de21f0b39 100644 (file)
@@ -37,6 +37,7 @@ class sieveManagement extends plugin
   var $script_to_delete =-1;
   var $sieve_handle = NULL; 
 
+  var $create_script = FALSE;
 
   /* Initialize the class and load all sieve scripts 
    *  try to parse them and display errors 
@@ -90,13 +91,17 @@ class sieveManagement extends plugin
       }
 
       $this->scripts[$key]['SCRIPT'] = $script;
+      $this->scripts[$key]['ORIG_SCRIPT'] = $script;
       $this->scripts[$key]['MSG']   = "";
       $ret = $p->parse($script);
       if(!$ret){
-        $this->scripts[$key]['MSG']   = "<font color='red'>".$p->status_text."</font>";
-        $this->scripts[$key]['STATUS'] = _("Parse failed")."<font color='red'>".$p->status_text."</font>";
+        $this->scripts[$key]['STATUS']   = FALSE;
+        $this->scripts[$key]['MODE']    = "Source-Only";
+        $this->scripts[$key]['MSG'] = _("Parse failed")."<font color='red'>".$p->status_text."</font>";
       }else{
-        $this->scripts[$key]['STATUS'] = _("Parse successful");
+        $this->scripts[$key]['STATUS']   = TRUE;
+        $this->scripts[$key]['MODE']    = "Structured";
+        $this->scripts[$key]['MSG'] = _("Parse successful");
       }
       $this->scripts[$key]['PARSER'] = $p;
       $this->scripts[$key]['EDITED'] = FALSE;
@@ -109,6 +114,64 @@ class sieveManagement extends plugin
    */
   function execute()
   {
+    if(isset($_POST['create_new_script'])){
+      $this->create_script = TRUE;
+    }
+
+    if(isset($_POST['create_script_cancel'])){
+      $this->create_script = FALSE;
+    }
+
+    if($this->create_script){
+      $smarty = get_smarty();
+    
+      $name = "";
+      if(isset($_POST['NewScriptName'])){
+        $name = $_POST['NewScriptName'];
+      }
+  
+      $err = "";
+      if($name != strtolower($name)){
+        $err = _("Only lower case names are allowed here.");
+      }
+  
+      if(preg_match("/[^a-z]/i",$name)){
+        $err = _("Only a-z are allowed in script names.");
+      }
+
+      if($this->create_script && isset($_POST['create_script_save']) && $err == "" ){
+        $this->create_script = FALSE;
+
+        $script = "/*New script */".
+                  "stop;";
+        $p = new My_Parser;
+
+        $sc['SCRIPT'] = $script;
+        $sc['ORIG_SCRIPT'] = $script;
+        $sc['MSG']   = "";
+  
+        $ret = $p->parse($script);
+        if(!$ret){
+          $sc['STATUS']   = FALSE;
+          $sc['MODE']    = "Source-Only";
+          $sc['MSG'] = _("Parse failed")."<font color='red'>".$p->status_text."</font>";
+        }else{
+          $sc['STATUS']   = TRUE;
+          $sc['MODE']    = "Strucktured";
+          $sc['MSG'] = _("Parse successful");
+        }
+        $sc['PARSER'] = $p;
+        $sc['EDITED'] = TRUE;
+        $sc['ACTIVE'] = FALSE;
+        $sc['NAME']   = $name;
+        $this->scripts[$name] = $sc;
+      }else{
+        $smarty->assign("NewScriptName",$name);
+        $smarty->assign("Error",$err);
+        return($smarty->fetch(get_template_path("templates/create_script.tpl",TRUE,dirname(__FILE__))));
+      }
+    }
+
     $once = TRUE;
     foreach($_POST as $name => $value){
       if(preg_match("/^editscript_/",$name) && $once && !$this->current_handler){
@@ -205,22 +268,54 @@ class sieveManagement extends plugin
     /* Save currently edited sieve script. */
     if(isset($_POST['save_sieve_changes'])){
       $chk = $this->current_handler->check();
+      $chk =array();
       if(!count($chk)){
-        $this->scripts[$this->current_script]['PARSER'] = $this->current_handler;
-        $this->scripts[$this->current_script]['EDITED'] = TRUE;
-        $this->current_handler = NULL; 
+
+        $sc = $this->scripts[$this->current_script]['SCRIPT'];
+        $p = new My_Parser;
+        if($p -> parse($sc)){
+
+          if($this->scripts[$this->current_script]['MODE'] == "Source-Only"){
+            $this->scripts[$this->current_script]['MODE'] = "Source";
+          }
+  
+          $this->scripts[$this->current_script]['PARSER'] = $p;
+          $this->scripts[$this->current_script]['EDITED'] = TRUE;
+          $this->scripts[$this->current_script]['STATUS'] = TRUE;
+          $this->scripts[$this->current_script]['MSG'] = _("Edited");
+          $this->current_handler = NULL;
+        }else{
+          print_red($p->status_text);;
+        }
       }else{
-        print_a($chk);
         print_red(_("Please fix all errors before saving."));
       }
     }
 
-    /* Create output for currently opened sieve script */
+
+    /*************
+     * Display edit dialog 
+     *************/
+
+    /* Display edit dialog, depending on Mode display different uis
+     */
     if($this->current_handler){
-      $ret = $this->current_handler->execute();
+
+      $current = $this->scripts[$this->current_script];
+
+      /* Create html results */
+      $smarty = get_smarty();
+      $smarty->assign("Mode",$current['MODE']);
+      if($current['MODE'] == "Structured"){
+        $smarty->assign("Contents",$this->current_handler->tree_->execute());
+      }else{
+        $smarty->assign("Contents",$current['SCRIPT']);
+      }
+      $ret = $smarty->fetch(get_template_path("templates/edit_frame_base.tpl",TRUE,dirname(__FILE__)));
       return($ret);
     }
 
+
     /* Create list of available sieve scripts 
      */
     $List = new divSelectBox("sieveManagement");
@@ -236,7 +331,7 @@ class sieveManagement extends plugin
                         "attach" => "style='width:20px;'");  
       }
       $field2 = array("string" => $script['NAME']);  
-      $field3 = array("string" => $script['STATUS']);
+      $field3 = array("string" => $script['MSG']);
       $field4 = array("string" => _("Script length")."&nbsp;:&nbsp;".strlen($script['SCRIPT']));
 
       if($edited){
@@ -261,6 +356,7 @@ class sieveManagement extends plugin
   
     $display ="<h2>Sieve script management</h2>";
     $display .= _("Be careful. All your changes will be saved directly to sieve, if you use the save button below.");
+    $display .= "<br><input type='submit' name='create_new_script' value='"._("Create new script")."'>";
     $display .=  $List->DrawList();
     
     $display .= "<p style=\"text-align:right\">\n";
@@ -274,6 +370,44 @@ class sieveManagement extends plugin
   function save_object()
   {
     if($this->current_handler){
+
+      $Mode = $this->scripts[$this->current_script]['MODE'];
+      $skip_mode_change = false;
+      if(in_array($Mode,array("Source-Only","Source"))){
+        if(isset($_POST['script_contents'])){
+          $sc = stripslashes($_POST['script_contents']);
+          $this->scripts[$this->current_script]['SCRIPT'] = $sc;
+          $p = new My_Parser;
+          if($p -> parse($sc)){
+            $this->current_handler = $p;
+          } else {
+            print_red($p->status_text);
+            $skip_mode_change = TRUE;
+          }
+        }
+      }
+      if(in_array($Mode,array("Structured"))){
+        $this->current_handler->save_object();
+        $sc = $this->current_handler->get_sieve_script();
+        $this->scripts[$this->current_script]['SCRIPT'] = $sc;
+        $p = new My_Parser;
+        if($p -> parse($sc)){
+          $this->current_handler = $p;
+        } else {
+          print_red($p->status_text);
+          $skip_mode_change = TRUE;
+        }
+      }
+      if(!$skip_mode_change){
+        if($this->scripts[$this->current_script]['MODE'] != "Source-Only"){
+          if(isset($_POST['View_Source'])){
+            $this->scripts[$this->current_script]['MODE'] = "Source";
+          }
+          if(isset($_POST['View_Structured'])){
+            $this->scripts[$this->current_script]['MODE'] = "Structured";
+          }
+        }
+      }
       $this->current_handler->save_object();
     }
   }
@@ -300,16 +434,15 @@ class sieveManagement extends plugin
 
     foreach($this->scripts as $key => $script){
       if($script['EDITED']){
-        $data = $script['PARSER']->get_sieve_script();
+        $data = $this->scripts[$key]['SCRIPT'];
         if(!$this->sieve_handle->sieve_sendscript($script['NAME'], $data)){
           gosa_log("Failed to save sieve script named '".$script['NAME']."': ".to_string($this->sieve_handle->error_raw));
           $everything_went_fine = FALSE;
           print_red(to_string($this->sieve_handle->error_raw));
-          $this->scripts[$key]['STATUS'] = "<font color='red'>".
+          $this->scripts[$key]['MSG'] = "<font color='red'>".
                                            _("Failed to save sieve script").": ".
                                            to_string($this->sieve_handle->error_raw).
                                            "</font>";
-#          echo nl2br($data);
         }
       }
     }