Code

Parameters are sorted now
[gosa.git] / plugins / gofon / macro / class_gofonMacroParameters.inc
index 19fc6204029562310dbfb182a0a4f471a234c158..3aaa420f37ded51e8c64c6fdb73fda37e5f196ac 100755 (executable)
@@ -1,4 +1,5 @@
 <?php
+
 class macroParameter extends plugin
 {
   /* CLI vars */
@@ -9,11 +10,15 @@ class macroParameter extends plugin
   /* application attributes */
   var $base= "";
   var $goFonMacroParameter =array();
+  var $type_shortcut= array("string" => array("selected", "", ""),
+      "combo"  => array("", "selected", ""),
+      "bool"   => array("", "", "selected"));
 
   /* attribute list for save action */
   var $attributes= array("base","goFonMacroParameter");
   var $objectclasses= array("top", "goFonMacro");
 
+
   function macroParameter ($config, $dn= NULL)
   {
     plugin::plugin ($config, $dn);
@@ -51,23 +56,78 @@ class macroParameter extends plugin
       $tmp3 = $this->goFonMacroParameter;
       $this->goFonMacroParameter =array();     
       if(!empty($tmp3)) {
-        $this->goFonMacroParameter[] = $tmp3;
+        $this->goFonMacroParameter[]  = $tmp3;
       }
     }
 
-    /* Create a better sorted array */
-    foreach($this->goFonMacroParameter as $para)    {
+    /* Load parametersettings*/
+    foreach($this->goFonMacroParameter as $para){
       $tmp = split("!",$para);
-      $tmp2[$tmp[0]]['name']           = $tmp[1];  
-      $tmp2[$tmp[0]]['type']           = $tmp[2];  
-      $tmp2[$tmp[0]]['default']        = $tmp[3];  
+      $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 */
-    $this->goFonMacroParameter = $tmp2;        
+    $this->goFonMacroParameter = $tmp2;
+  }
+
+  function check_paras($content,$goFonMacroParameter)
+  { 
+    /* Check contents for parameters */
+    preg_match_all("/[$]\{ARG[0-9]*\}/",$content,$res,PREG_OFFSET_CAPTURE);
+
+    $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);
 
   }
 
+
   function execute()
   {
     /* Variables */
@@ -75,10 +135,17 @@ class macroParameter extends plugin
     $tmp       = array();
     $number = 0; 
 
+    $content = $_SESSION['macroManagment']->macrotabs->by_object['macro']->goFonMacroContent;
+
+    if(strstr($content,"ARG")){
+      $vorpos = strpos($content,"ARG");
+      $rest   = substr($content,$vorpos, strlen($content));
+    }    
+
     /* Do we represent a valid group? */
     if (!$this->is_account && $this->parent == NULL){
       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
-        _("This 'dn' is no application.")."</b>";
+        _("This 'dn' is no phone macro.")."</b>";
       return ($display);
     }
 
@@ -99,102 +166,90 @@ 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");
     }
 
-    /* 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 */
-    foreach($this->goFonMacroParameter as $key=>$para) {
+    $FonParas = $this->check_paras($content,$this->goFonMacroParameter); 
 
-      if($para['type']=="string") {
-        $sel1 = "selected";
-        $sel2 = "";
+    /* Sort by Parameterid, and keep keys */    
+    ksort($FonParas);
+    
+    foreach($FonParas as $key=>$para)  {
+
+      /* Select correct item of combobox */
+      if(isset($para['type'])){
+        list($sel1, $sel2, $sel3)= $this->type_shortcut[$para['type']];
       }else{
-        $sel1 = "";
-        $sel2 = "selected";
+        list($sel1, $sel2, $sel3)= array("", "", "");
       }
 
+      /* Assemble output table */
       $vars .="<tr>
-        <td>".$key."<input name=\"number".$key."\" value='".$key."' type='hidden'> </td>
-        <td><input name='varname".$key."'   value='".$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>
-        <option name='vartype".$key."' value='combo'   ".$sel2.">combo</option>
-        <option name='vartype".$key."' value='bool'   ".$sel2.">bool</option>
+        <option name='vartype".$key."' value='string' ".$sel1.">"._("String")."</option>
+        <option name='vartype".$key."' value='combo'   ".$sel2.">"._("Combobox")."</option>
+        <option name='vartype".$key."' value='bool'   ".$sel3.">"._("Bool")."</option>
         </select>
         </td>
-        <td><input name='default".$key."'   value='".$para['default']."'></td>
-        <td><input name='del".$key."' value='delete' type='submit'></td>
-        </tr>";
-
+        <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.=" </td></tr>";
     }
 
-    /* Display add renew button */
-    $vars .="<tr>      
-      <td colspan=5>
-      <input name='addvar' value="._("Add")." type='submit'>
-      <input name='actvar' value="._("Refresh")." type='submit'>
-      </td>
-      </tr>";
     /* Checkboxes */
     $smarty->assign("base_select", $this->base);
     $smarty->assign("vars", $vars);
 
-
     /* Show main page */
     return($smarty->fetch (get_template_path('parameter.tpl', TRUE)));
   }
 
+
   function remove_from_parent()
   {
-    $ldap= $this->config->get_ldap_link();
-    $ldap->rmDir($this->dn);
-
-    /* Optionally execute a command after we're done */
-    $this->handle_post_events("remove");
-
-    /* Delete references to object groups */
-    $ldap->cd ($this->config->current['BASE']);
-    $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
-    while ($ldap->fetch()){
-      $og= new ogroup($this->config, $ldap->getDN());
-      unset($og->member[$this->dn]);
-      $og->save ();
-    }
-
   }
 
 
   /* Save data to object */
   function save_object()
   {
-    if (isset($_POST['cn'])){
+    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];
+      }
     }
+
   }
 
 
@@ -202,13 +257,30 @@ class macroParameter extends plugin
   function check()
   {
     $message = array();
-    
+
     foreach($this->attributes as $attr){
       if(chkacl($this->acl,$attr)){
-        $str =  sprintf( _("Insufficient permissions, can't change attribute '%s' in goFonMacro"),$attr) ;
+        $str =  sprintf( _("Insufficient permissions, can't change attribute '%s' in goFonMacro."),$attr) ;
         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;
   }
 
@@ -217,7 +289,6 @@ class macroParameter extends plugin
   function save()
   {
     /* Post checks */
-    $this->execute();
 
     plugin::save();