Code

Added execute methods
[gosa.git] / plugins / gofon / macro / class_gofonMacroParameters.inc
1 <?php
2 //! This class handles the goFonMacroParameter
3 /*! In this class all parameters from goFonMacroContent \n
4     and all (if given) already defined parameters are managed \n
5     \n
6     Parameters will be saved to openldap like this :\n
7     goFonMacroParameter: ID!NAME:!TYPE(string:combo:bool)!DEFAULT_VALUE\n    
8     In Case of type=combo the DEFAULT_VALUE specifies the entries in the listbox, like this\n
9     "first:second:third:last" \n
10 */
11 class macroParameter extends plugin
12 {
13   /*! CLI vars */
14   var $cli_summary= "Handling of GOsa's application object";
15   /*! CLI vars */
16   var $cli_description= "Some longer text\nfor help";
17   /*! CLI vars */
18   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
20   /* Parameter Count*/
21   var $para_count = 0 ; 
22   /*! macro base  */
23   var $base= "";
24   
25   /*! This array contains all Parameter defined for the macro*/
26   var $goFonMacroParameter =array();
28   /*! This are the available types for a macro */
29   var $type_shortcut= array("string" => array("selected", "", ""),
30       "combo"  => array("", "selected", ""),
31       "bool"   => array("", "", "selected"));
33   /*! attribute list for save action */
34   var $attributes= array("base","goFonMacroParameter");
35   
36   /*! Objectclasses needed by the class*/  
37   var $objectclasses= array("top", "goFonMacro");
39   //! The konstructor of macroParameter    
40   /*! The konstructor of macroParameter...
41      - reads goFonMacroParameter and parses them to an array 
42      - Set attributes from openldap (edit)
43      - Set attributes from default (new)
44   */
45   function macroParameter ($config, $dn= NULL)
46   {
47     plugin::plugin ($config, $dn);
49     $tmp = array();  // temporary Var 
50     $tmp2 = array(); // temporary Var ...
51     $tmp3 = "";
52     $ldap= $config->get_ldap_link();
54     $this->dn = $dn;
56     /* This is always an account */
57     $this->is_account= TRUE;
59     /* Edit or new one ?*/
60     if ($this->dn == "new"){
61       $ui= get_userinfo();
62       $this->base= dn2base($ui->dn);
63     } else {
64       $this->base= dn2base($this->dn);
65     }
67     /* initialising macro parameter */
68     if(isset($this->attrs['goFonMacroParameter']) &&
69         isset($this->attrs['goFonMacroParameter']['count'])){
70       unset($this->attrs['goFonMacroParameter']['count']);
71     }
73     /* Set Parameters, or a new array if ther are no parameters */
74     if(isset($this->attrs['goFonMacroParameter'])){
75       $this->goFonMacroParameter = $this->attrs['goFonMacroParameter'];
76     }else{
77       $this->goFonMacroParameter =array();
78     }
80     /* Create an array for parameters if not given yet */
81     if(!is_array($this->goFonMacroParameter)){
82       $tmp3 = $this->goFonMacroParameter;
83       $this->goFonMacroParameter =array();      
84       if(!empty($tmp3)) {
85         $this->goFonMacroParameter[]  = $tmp3;
86       }
87     }
89     /* Load parametersettings*/
90     foreach($this->goFonMacroParameter as $para){
91       $tmp = split("!",$para);
92       $num = $tmp[0];
93       $tmp2[$num]['name']        = base64_decode($tmp[1]);
94       $tmp2[$num]['type']        = $tmp[2];
95       $tmp2[$num]['default']     = $tmp[3];
96       $tmp2[$num]['var']         = "var".$num;
97     }
99     
100     /* Assign this array */
101     $this->goFonMacroParameter = $tmp2;
103     $this->para_count = count ($tmp2);
104     
105   }
107   //! Perform Parameter check 
108   /*! 
109       Compares the given parameters (goFonMacroParameters) with the parameters defined in goFonContent\n 
110       -> Decide which attrs are new and which are unused\n
111       -> Sort result array (containing both parameters 'goFonMacroParameters/goFonContent' and new / unused info)\n
112       \param $content The given goFonContent for this macro\n
113       \param $goFonMacroParameter Array with the already given parameters \n
114    */
115   function check_paras($content,$goFonMacroParameter)
116   { 
117     /* Check contents for parameters */
118     preg_match_all("/[$]\{ARG[0-9]*\}/",$content,$res,PREG_OFFSET_CAPTURE);
120     $new = array();
122     /* Detect parameters with positions */
123     foreach($res[0] as $val){
124       $num = preg_replace("/[^0-9]/","",$val[0]); 
125       $new[$num]['val'] = $val[0];
126       $new[$num]['num'] = $num;
127     }
129     /* Compare content parameter and macro parameter */
130     foreach($goFonMacroParameter as $gokey => $goval){
131       foreach($new as $nkey => $nval){
132         if($gokey == $nval['num']){
133           /* sign this as OK */
134           $goFonMacroParameter[$gokey]['check']= true;
135         }
136       }
137     }
139     /* Now check if there is new parameter in the content, which is not assigned yet */
140     foreach($new as $key => $val){
141       /* Assign std values */
142       $goFonMacroParameter[$key]['var']="var".$key;
143       $goFonMacroParameter[$key]['check']= true;
145       /* If this is a new Parameter, name it ${ARG#} by default*/
146       if((!isset($goFonMacroParameter[$key]['name']))||(empty($goFonMacroParameter[$key]['name']))){
147         $goFonMacroParameter[$key]['name']="\${ARG".$key."}";
148       }
149     }  
151     foreach($goFonMacroParameter as $key => $val){
152       /* All attributes with check == false, are unneeded so mark them with ['check']= false */
153       if(!isset($goFonMacroParameter[$key]['check'])){
154         $goFonMacroParameter[$key]['check']= false;
155       }
156       /* Ah no default given assign ="" to prevent unsigned index  */
157       if(!isset($goFonMacroParameter[$key]['default'])){
158         $goFonMacroParameter[$key]['default'] = "";
159       }
160     }
162     /* Sort output for better reading */
163     asort($goFonMacroParameter);
164     return($goFonMacroParameter);
166   }
168   //! Execute this Plugin
169   /*! 
170       Perform Parameter check \n
171       Draw paramter table\n
172       Show tpl   \n 
173   */
174   function execute()
175   {
176         /* Call parent execute */
177         plugin::execute();
178     /* Variables */
179     $vars       = "";
180     $tmp        = array();
181     $number = 0; 
182   
183     $content = $_SESSION['macroManagment']->macrotabs->by_object['macro']->goFonMacroContent;
185     if(strstr($content,"ARG")){
186       $vorpos = strpos($content,"ARG");
187       $rest   = substr($content,$vorpos, strlen($content));
188     }    
190     /* Do we represent a valid group? */
191     if (!$this->is_account && $this->parent == NULL){
192       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
193         _("This 'dn' is no phone macro.")."</b>";
194       return ($display);
195     }
197     /* Fill templating stuff */
198     $smarty= get_smarty();
200     /* Assign all vars to Smarty */
201     foreach($this->attributes as $ar){
202       $smarty->assign($ar, $this->$ar);
203     }
205     /* Add an empty Parameter */
206     if(isset($_POST['addvar'])){
207       if(!is_array($this->goFonMacroParameter)){
208         $vars = $this->goFonMacroParameter;
209         $this->goFonMacroParameter = array();
210         $this->goFonMacroParameter[]= $vars;
211       }
212       $number= count($this->goFonMacroParameter);
213       $number++;
214       $this->goFonMacroParameter[]=array("var"=>"var","name"=>"new","type"=>"string","default"=>"test");
215     }
217     /*generate Table which shows als parameters */
218     $FonParas = $this->check_paras($content,$this->goFonMacroParameter); 
220     /* Sort by Parameterid, and keep keys */    
221     ksort($FonParas);
223     foreach($FonParas as $key=>$para)   {
225       /* Select correct item of combobox */
226       if(isset($para['type'])){
227         list($sel1, $sel2, $sel3)= $this->type_shortcut[$para['type']];
228       }else{
229         list($sel1, $sel2, $sel3)= array("", "", "");
230       }
232       /* Assemble output table */
233       $vars .="<tr>
234         <td>
235           <input name=\"number".$key."\" value='".$key."' type='hidden'>
236           <input name='var".$key."' type='hidden'   value='".$para['var']."'>ARG".$key."
237         </td>
238         <td><input size=\"45\" name='varname".$key."'  value='".$para['name']."'></td>
239         <td>
240           <select name='vartype".$key."'>
241             <option  value='string' ".$sel1.">"._("String")."&nbsp;</option>
242             <option  value='combo'   ".$sel2.">"._("Combobox")."&nbsp;</option>
243             <option  value='bool'   ".$sel3.">"._("Bool")."&nbsp;</option>
244           </select>
245         </td>
246         <td><input size=\"45\" name='default".$key."'   value='".$para['default']."'></td>
247         <td>&nbsp;";
248       if($para['check']==false) {
249         $vars.="<input name='del".$key."' value='"._("Delete unused")."' type='submit'>";
250       }
252       $vars.=" </td></tr>";
253     }
255     /* Checkboxes */
256     $smarty->assign("base_select", $this->base);
257     $smarty->assign("vars", $vars);
259     /* Show main page */
260     return($smarty->fetch (get_template_path('parameter.tpl', TRUE)));
261   }
262   
263   //! Unused here 
264   /*!
265       Unused here because goFonMacro will remove this Macro\n 
266   */
267   function remove_from_parent()
268   {
269   }
271   //! Save our data
272   /*! 
273       Save POST data to object \n
274       This gives us the possibility to leave a tab, without losing our typed informations\n
275       \n
276       Read the POST fields for the parameters and saves their info the the class\n
277   */
278   function save_object()
279   {
280     if (isset($_POST['phoneparameters'])){
281       plugin::save_object();
282     }
283     /* read out post data, and assign it to the parameters */
284     /* And or delete */
285     foreach($_POST as $name=>$value){
287       /* Test if there is a variable begining with "del" */
288       if(preg_match("/del/",$name)){
290         /* Extract entry id to delete */
291         $nr = str_replace("del","",$name) ;
293         /* unset entry */
294         unset($this->goFonMacroParameter[$nr]);
296       }elseif(preg_match("/number/",$name)){
298         /* Set Post vars */
299         $key = $_POST[$name];
301         $this->goFonMacroParameter[$key]['var']   = $_POST["var".$key];
302         $this->goFonMacroParameter[$key]['name']   = $_POST["varname".$key];
303         $this->goFonMacroParameter[$key]['type']   = $_POST["vartype".$key];
304         $this->goFonMacroParameter[$key]['default']= $_POST["default".$key];
305       }
306     }
308   }
311   //! Checks given values 
312   /*! 
313       Check values\n 
314       If a user enters an invalid value, then this function will output an error msg\n
315       (In better words :prepare the errormessages that will be put out )\n
316   */
317   function check()
318   {
319     $message = array();
321     foreach($this->attributes as $attr){
322       if(chkacl($this->acl,$attr)){
323         $str =  sprintf( _("Insufficient permissions, can't change attribute '%s' in goFonMacro."),$attr) ;
324         return(array($str));
325       }
326     }
329     foreach($this->goFonMacroParameter as $key=>$val){
330       if((strstr($val['default'],"!"))||(strstr($val['default'],"#"))) {
331         $message[] = sprintf(_("The parameter %s contains invalid char. '!,#' is used as delimiter"),$val['name']);
332       }
333       switch($val['type']){
334         case 'bool'   :   $possible = array("","0","1");
335                           if(!in_array($val['default'],$possible)) {
336                             $message[] = sprintf(_("The parameter %s has incorrect value for type bool."),$val['name']);
337                           };break;
338         case 'string' :
339         case 'combo'  : 
340         default : ;
342       }
343     }
344     return $message;
345   }
347   //! Save changes to openldap
348   /*!
349       Save to LDAP 
350       This function saves given attributes to the ldap
351   */
352   function save()
353   {
354     /* Post checks */
356     plugin::save();
358     $this->attrs['goFonMacroParameter']=array();
360     foreach($this->goFonMacroParameter as $key=>$fonpara){
361       $this->attrs['goFonMacroParameter'][]=$key."!".base64_encode($fonpara['name'])."!".$fonpara['type']."!".$fonpara['default'];
362     }
364     if($this->para_count != count($this->attrs['goFonMacroParameter'])){
365       print_red(_("Parameter count of the macro changed, you must update each user which are using this macro '%s'."),$this->dn);
366     }
368     unset($this->attrs['base']);
370     /* Write back to ldap */
371     $ldap= $this->config->get_ldap_link();
372     $ldap->cat($this->dn);
373     $a= $ldap->fetch();
375     if (count($a)){
376       $ldap->cd($this->dn);
377       $ldap->modify($this->attrs);
378       $this->handle_post_events("modify");
379     } else {
380       if(count($this->attrs['goFonMacroParameter']==0)){
381         unset($this->attrs['goFonMacroParameter']);
382       }         
383       $ldap->cd($this->dn);
384       $ldap->create_missing_trees( $this->dn);
385       $ldap->cd($this->dn);
386       $ldap->add($this->attrs);
387       $this->handle_post_events("add");
388     }
389     show_ldap_error($ldap->get_error());
390   }
393 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
394 ?>