Code

Parameters are sorted now
[gosa.git] / plugins / gofon / macro / class_gofonMacroParameters.inc
index f8f93a19ab9de8848c48de1d609dea0334b2b65b..3aaa420f37ded51e8c64c6fdb73fda37e5f196ac 100755 (executable)
@@ -1,4 +1,5 @@
 <?php
+
 class macroParameter extends plugin
 {
   /* CLI vars */
@@ -10,8 +11,8 @@ class macroParameter extends plugin
   var $base= "";
   var $goFonMacroParameter =array();
   var $type_shortcut= array("string" => array("selected", "", ""),
-                            "combo"  => array("", "selected", ""),
-                            "bool"   => array("", "", "selected"));
+      "combo"  => array("", "selected", ""),
+      "bool"   => array("", "", "selected"));
 
   /* attribute list for save action */
   var $attributes= array("base","goFonMacroParameter");
@@ -59,12 +60,14 @@ class macroParameter extends plugin
       }
     }
 
+    /* Load parametersettings*/
     foreach($this->goFonMacroParameter as $para){
       $tmp = split("!",$para);
-      $num = preg_replace("/[^0-9]/","",$tmp[1]);
+      $num = $tmp[0];
       $tmp2[$num]['name']        = $tmp[1];
       $tmp2[$num]['type']        = $tmp[2];
       $tmp2[$num]['default']     = $tmp[3];
+      $tmp2[$num]['var']         = "var".$num;
     }
 
     /* Assign this array */
@@ -72,40 +75,53 @@ class macroParameter extends plugin
   }
 
   function check_paras($content,$goFonMacroParameter)
-  {
+  { 
+    /* Check contents for parameters */
     preg_match_all("/[$]\{ARG[0-9]*\}/",$content,$res,PREG_OFFSET_CAPTURE);
 
-    $anz = count($res[0]);
     $new = array();
 
+    /* Detect parameters with positions */
     foreach($res[0] as $val){
       $num = preg_replace("/[^0-9]/","",$val[0]); 
       $new[$num]['val'] = $val[0];
       $new[$num]['num'] = $num;
     }
 
+    /* Compare content parameter and macro parameter */
     foreach($goFonMacroParameter as $gokey => $goval){
       foreach($new as $nkey => $nval){
         if($gokey == $nval['num']){
+          /* sign this as OK */
           $goFonMacroParameter[$gokey]['check']= true;
         }
       }
     }
+
+    /* Now check if there is new parameter in the content, which is not assigned yet */
     foreach($new as $key => $val){
+      /* Assign std values */
+      $goFonMacroParameter[$key]['var']="var".$key;
       $goFonMacroParameter[$key]['check']= true;
+
+      /* If this is a new Parameter, name it ${ARG#} by default*/
       if((!isset($goFonMacroParameter[$key]['name']))||(empty($goFonMacroParameter[$key]['name']))){
         $goFonMacroParameter[$key]['name']="\${ARG".$key."}";
       }
     }  
 
     foreach($goFonMacroParameter as $key => $val){
+      /* All attributes with check == false, are unneeded so mark them with ['check']= false */
       if(!isset($goFonMacroParameter[$key]['check'])){
         $goFonMacroParameter[$key]['check']= false;
       }
+      /* Ah no default given assign ="" to prevent unsigned index  */
       if(!isset($goFonMacroParameter[$key]['default'])){
         $goFonMacroParameter[$key]['default'] = "";
       }
     }
+
+    /* Sort output for better reading */
     asort($goFonMacroParameter);
     return($goFonMacroParameter);
 
@@ -150,40 +166,15 @@ class macroParameter extends plugin
       }
       $number= count($this->goFonMacroParameter);
       $number++;
-      $this->goFonMacroParameter[]=array("name"=>"new","type"=>"string","default"=>"test");
+      $this->goFonMacroParameter[]=array("var"=>"var","name"=>"new","type"=>"string","default"=>"test");
     }
 
-
-    #FIXME: Der Transfer der momentanen Variablen muss nach save_object(),
-    #       damit kann in save() auf den execute() Aufruf verzichtet werden!
-    
-    /* read out post data, and assign it to the parameters */
-    /* And or delete */
-    foreach($_POST as $name=>$value){  
-
-      /* Test if there is a variable begining with "del" */
-      if(preg_match("/del/",$name)){
-
-        /* Extract entry id to delete */
-        $nr = str_replace("del","",$name)      ;
-
-        /* unset entry */                      
-        unset($this->goFonMacroParameter[$nr]);
-
-      }elseif(preg_match("/number/",$name)){
-
-        /* Set Post vars */
-        $key = $_POST[$name];
-        $this->goFonMacroParameter[$key]['name']        = $_POST["varname".$key];              
-        $this->goFonMacroParameter[$key]['type']        = $_POST["vartype".$key];              
-        $this->goFonMacroParameter[$key]['default']= $_POST["default".$key];           
-      }
-    } 
-
     /*generate Table which shows als parameters */
-
     $FonParas = $this->check_paras($content,$this->goFonMacroParameter); 
 
+    /* Sort by Parameterid, and keep keys */    
+    ksort($FonParas);
+    
     foreach($FonParas as $key=>$para)  {
 
       /* Select correct item of combobox */
@@ -193,13 +184,11 @@ class macroParameter extends plugin
         list($sel1, $sel2, $sel3)= array("", "", "");
       }
 
-      #FIXME: Der Name des Parameters ist nicht gesetzt
-      #       -> Argument (ARG#)    Name [....]    Typ [...^]    Default [....]
-
       /* Assemble output table */
       $vars .="<tr>
-        <td><input name=\"number".$key."\" value='".$key."' type='hidden'> </td>
-        <td><input name='varname".$key."' type='hidden'   value='".$para['name']."'>".$para['name']."</td>
+        <input name=\"number".$key."\" value='".$key."' type='hidden'>
+        <td><input name='var".$key."' type='hidden'   value='".$para['var']."'>".$para['var']."</td>
+        <td><input size=\"45\" name='varname".$key."'  value='".$para['name']."'></td>
         <td>
         <select name='vartype".$key."'>
         <option name='vartype".$key."' value='string' ".$sel1.">"._("String")."</option>
@@ -207,17 +196,13 @@ class macroParameter extends plugin
         <option name='vartype".$key."' value='bool'   ".$sel3.">"._("Bool")."</option>
         </select>
         </td>
-        <td><input name='default".$key."'   value='".$para['default']."'></td>
-        <td>";
-      if($para['check']==true) {
-        $vars.=_("Ok");
-        $vars.=" <td></td>";
-      }else{
-        $vars.="<b>"._("Unused")."</b>";
-        $vars.=" <td><input name='del".$key."' value='delete' type='submit'></td>";
+        <td><input size=\"45\" name='default".$key."'   value='".$para['default']."'></td>
+        <td>&nbsp;";
+      if($para['check']==false) {
+        $vars.="<input name='del".$key."' value='"._("Delete unused")."' type='submit'>";
       }
 
-      $vars.=" </tr>";
+      $vars.=" </td></tr>";
     }
 
     /* Checkboxes */
@@ -240,6 +225,31 @@ class macroParameter extends plugin
     if (isset($_POST['phoneparameters'])){
       plugin::save_object();
     }
+    /* read out post data, and assign it to the parameters */
+    /* And or delete */
+    foreach($_POST as $name=>$value){
+
+      /* Test if there is a variable begining with "del" */
+      if(preg_match("/del/",$name)){
+
+        /* Extract entry id to delete */
+        $nr = str_replace("del","",$name) ;
+
+        /* unset entry */
+        unset($this->goFonMacroParameter[$nr]);
+
+      }elseif(preg_match("/number/",$name)){
+
+        /* Set Post vars */
+        $key = $_POST[$name];
+
+        $this->goFonMacroParameter[$key]['var']   = $_POST["var".$key];
+        $this->goFonMacroParameter[$key]['name']   = $_POST["varname".$key];
+        $this->goFonMacroParameter[$key]['type']   = $_POST["vartype".$key];
+        $this->goFonMacroParameter[$key]['default']= $_POST["default".$key];
+      }
+    }
+
   }
 
 
@@ -254,6 +264,23 @@ class macroParameter extends plugin
         return(array($str));
       }
     }
+
+
+    foreach($this->goFonMacroParameter as $key=>$val){
+      if((strstr($val['default'],"!"))||(strstr($val['default'],"#"))) {
+        $message[] = sprintf(_("The parameter %s contains invalid char. '!,#' is used as delimiter"),$val['name']);
+      }
+      switch($val['type']){
+        case 'bool'   :   $possible = array("","0","1");
+                          if(!in_array($val['default'],$possible)) {
+                            $message[] = sprintf(_("The parameter %s has incorrect value for type bool."),$val['name']);
+                          };break;
+        case 'string' :
+        case 'combo'  : 
+        default : ;
+
+      }
+    }
     return $message;
   }
 
@@ -263,9 +290,6 @@ class macroParameter extends plugin
   {
     /* Post checks */
 
-    # FIXME: das hat hier nix zu suchen...
-    $this->execute();
-
     plugin::save();
 
     $this->attrs['goFonMacroParameter']=array();