Code

dd2c2be08cf8646f8af5eda5ff6cb82228a642c2
[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   /*! macro base  */
21   var $base= "";
22   
23   /*! This array contains all Parameter defined for the macro*/
24   var $goFonMacroParameter =array();
26   /*! This are the available types for a macro */
27   var $type_shortcut= array("string" => array("selected", "", ""),
28       "combo"  => array("", "selected", ""),
29       "bool"   => array("", "", "selected"));
31   /*! attribute list for save action */
32   var $attributes= array("base","goFonMacroParameter");
33   
34   /*! Objectclasses needed by the class*/  
35   var $objectclasses= array("top", "goFonMacro");
37   //! The konstructor of macroParameter    
38   /*! The konstructor of macroParameter...
39      - reads goFonMacroParameter and parses them to an array 
40      - Set attributes from openldap (edit)
41      - Set attributes from default (new)
42   */
43   function macroParameter ($config, $dn= NULL)
44   {
45     plugin::plugin ($config, $dn);
47     $tmp = array();  // temporary Var 
48     $tmp2 = array(); // temporary Var ...
49     $tmp3 = "";
50     $ldap= $config->get_ldap_link();
52     $this->dn = $dn;
54     /* This is always an account */
55     $this->is_account= TRUE;
57     /* Edit or new one ?*/
58     if ($this->dn == "new"){
59       $ui= get_userinfo();
60       $this->base= dn2base($ui->dn);
61     } else {
62       $this->base= dn2base($this->dn);
63     }
65     /* initialising macro parameter */
66     if(isset($this->attrs['goFonMacroParameter']) &&
67         isset($this->attrs['goFonMacroParameter']['count'])){
68       unset($this->attrs['goFonMacroParameter']['count']);
69     }
71     /* Set Parameters, or a new array if ther are no parameters */
72     if(isset($this->attrs['goFonMacroParameter'])){
73       $this->goFonMacroParameter = $this->attrs['goFonMacroParameter'];
74     }else{
75       $this->goFonMacroParameter =array();
76     }
78     /* Create an array for parameters if not given yet */
79     if(!is_array($this->goFonMacroParameter)){
80       $tmp3 = $this->goFonMacroParameter;
81       $this->goFonMacroParameter =array();      
82       if(!empty($tmp3)) {
83         $this->goFonMacroParameter[]  = $tmp3;
84       }
85     }
87     /* Load parametersettings*/
88     foreach($this->goFonMacroParameter as $para){
89       $tmp = split("!",$para);
90       $num = $tmp[0];
91       $tmp2[$num]['name']        = $tmp[1];
92       $tmp2[$num]['type']        = $tmp[2];
93       $tmp2[$num]['default']     = $tmp[3];
94       $tmp2[$num]['var']         = "var".$num;
95     }
97     /* Assign this array */
98     $this->goFonMacroParameter = $tmp2;
99   }
101   //! Perform Parameter check 
102   /*! 
103       Compares the given parameters (goFonMacroParameters) with the parameters defined in goFonContent\n 
104       -> Decide which attrs are new and which are unused\n
105       -> Sort result array (containing both parameters 'goFonMacroParameters/goFonContent' and new / unused info)\n
106       \param $content The given goFonContent for this macro\n
107       \param $goFonMacroParameter Array with the already given parameters \n
108    */
109   function check_paras($content,$goFonMacroParameter)
110   { 
111     /* Check contents for parameters */
112     preg_match_all("/[$]\{ARG[0-9]*\}/",$content,$res,PREG_OFFSET_CAPTURE);
114     $new = array();
116     /* Detect parameters with positions */
117     foreach($res[0] as $val){
118       $num = preg_replace("/[^0-9]/","",$val[0]); 
119       $new[$num]['val'] = $val[0];
120       $new[$num]['num'] = $num;
121     }
123     /* Compare content parameter and macro parameter */
124     foreach($goFonMacroParameter as $gokey => $goval){
125       foreach($new as $nkey => $nval){
126         if($gokey == $nval['num']){
127           /* sign this as OK */
128           $goFonMacroParameter[$gokey]['check']= true;
129         }
130       }
131     }
133     /* Now check if there is new parameter in the content, which is not assigned yet */
134     foreach($new as $key => $val){
135       /* Assign std values */
136       $goFonMacroParameter[$key]['var']="var".$key;
137       $goFonMacroParameter[$key]['check']= true;
139       /* If this is a new Parameter, name it ${ARG#} by default*/
140       if((!isset($goFonMacroParameter[$key]['name']))||(empty($goFonMacroParameter[$key]['name']))){
141         $goFonMacroParameter[$key]['name']="\${ARG".$key."}";
142       }
143     }  
145     foreach($goFonMacroParameter as $key => $val){
146       /* All attributes with check == false, are unneeded so mark them with ['check']= false */
147       if(!isset($goFonMacroParameter[$key]['check'])){
148         $goFonMacroParameter[$key]['check']= false;
149       }
150       /* Ah no default given assign ="" to prevent unsigned index  */
151       if(!isset($goFonMacroParameter[$key]['default'])){
152         $goFonMacroParameter[$key]['default'] = "";
153       }
154     }
156     /* Sort output for better reading */
157     asort($goFonMacroParameter);
158     return($goFonMacroParameter);
160   }
162   //! Execute this Plugin
163   /*! 
164       Perform Parameter check \n
165       Draw paramter table\n
166       Show tpl   \n 
167   */
168   function execute()
169   {
170     /* Variables */
171     $vars       = "";
172     $tmp        = array();
173     $number = 0; 
175     $content = $_SESSION['macroManagment']->macrotabs->by_object['macro']->goFonMacroContent;
177     if(strstr($content,"ARG")){
178       $vorpos = strpos($content,"ARG");
179       $rest   = substr($content,$vorpos, strlen($content));
180     }    
182     /* Do we represent a valid group? */
183     if (!$this->is_account && $this->parent == NULL){
184       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
185         _("This 'dn' is no phone macro.")."</b>";
186       return ($display);
187     }
189     /* Fill templating stuff */
190     $smarty= get_smarty();
192     /* Assign all vars to Smarty */
193     foreach($this->attributes as $ar){
194       $smarty->assign($ar, $this->$ar);
195     }
197     /* Add an empty Parameter */
198     if(isset($_POST['addvar'])){
199       if(!is_array($this->goFonMacroParameter)){
200         $vars = $this->goFonMacroParameter;
201         $this->goFonMacroParameter = array();
202         $this->goFonMacroParameter[]= $vars;
203       }
204       $number= count($this->goFonMacroParameter);
205       $number++;
206       $this->goFonMacroParameter[]=array("var"=>"var","name"=>"new","type"=>"string","default"=>"test");
207     }
209     /*generate Table which shows als parameters */
210     $FonParas = $this->check_paras($content,$this->goFonMacroParameter); 
212     /* Sort by Parameterid, and keep keys */    
213     ksort($FonParas);
215     foreach($FonParas as $key=>$para)   {
217       /* Select correct item of combobox */
218       if(isset($para['type'])){
219         list($sel1, $sel2, $sel3)= $this->type_shortcut[$para['type']];
220       }else{
221         list($sel1, $sel2, $sel3)= array("", "", "");
222       }
224       /* Assemble output table */
225       $vars .="<tr>
226         <td>
227           <input name=\"number".$key."\" value='".$key."' type='hidden'>
228           <input name='var".$key."' type='hidden'   value='".$para['var']."'>ARG".$key."
229         </td>
230         <td><input size=\"45\" name='varname".$key."'  value='".$para['name']."'></td>
231         <td>
232           <select name='vartype".$key."'>
233             <option  value='string' ".$sel1.">"._("String")."&nbsp;</option>
234             <option  value='combo'   ".$sel2.">"._("Combobox")."&nbsp;</option>
235             <option  value='bool'   ".$sel3.">"._("Bool")."&nbsp;</option>
236           </select>
237         </td>
238         <td><input size=\"45\" name='default".$key."'   value='".$para['default']."'></td>
239         <td>&nbsp;";
240       if($para['check']==false) {
241         $vars.="<input name='del".$key."' value='"._("Delete unused")."' type='submit'>";
242       }
244       $vars.=" </td></tr>";
245     }
247     /* Checkboxes */
248     $smarty->assign("base_select", $this->base);
249     $smarty->assign("vars", $vars);
251     /* Show main page */
252     return($smarty->fetch (get_template_path('parameter.tpl', TRUE)));
253   }
254   
255   //! Unused here 
256   /*!
257       Unused here because goFonMacro will remove this Macro\n 
258   */
259   function remove_from_parent()
260   {
261   }
263   //! Save our data
264   /*! 
265       Save POST data to object \n
266       This gives us the possibility to leave a tab, without losing our typed informations\n
267       \n
268       Read the POST fields for the parameters and saves their info the the class\n
269   */
270   function save_object()
271   {
272     if (isset($_POST['phoneparameters'])){
273       plugin::save_object();
274     }
275     /* read out post data, and assign it to the parameters */
276     /* And or delete */
277     foreach($_POST as $name=>$value){
279       /* Test if there is a variable begining with "del" */
280       if(preg_match("/del/",$name)){
282         /* Extract entry id to delete */
283         $nr = str_replace("del","",$name) ;
285         /* unset entry */
286         unset($this->goFonMacroParameter[$nr]);
288       }elseif(preg_match("/number/",$name)){
290         /* Set Post vars */
291         $key = $_POST[$name];
293         $this->goFonMacroParameter[$key]['var']   = $_POST["var".$key];
294         $this->goFonMacroParameter[$key]['name']   = $_POST["varname".$key];
295         $this->goFonMacroParameter[$key]['type']   = $_POST["vartype".$key];
296         $this->goFonMacroParameter[$key]['default']= $_POST["default".$key];
297       }
298     }
300   }
303   //! Checks given values 
304   /*! 
305       Check values\n 
306       If a user enters an invalid value, then this function will output an error msg\n
307       (In better words :prepare the errormessages that will be put out )\n
308   */
309   function check()
310   {
311     $message = array();
313     foreach($this->attributes as $attr){
314       if(chkacl($this->acl,$attr)){
315         $str =  sprintf( _("Insufficient permissions, can't change attribute '%s' in goFonMacro."),$attr) ;
316         return(array($str));
317       }
318     }
321     foreach($this->goFonMacroParameter as $key=>$val){
322       if((strstr($val['default'],"!"))||(strstr($val['default'],"#"))) {
323         $message[] = sprintf(_("The parameter %s contains invalid char. '!,#' is used as delimiter"),$val['name']);
324       }
325       switch($val['type']){
326         case 'bool'   :   $possible = array("","0","1");
327                           if(!in_array($val['default'],$possible)) {
328                             $message[] = sprintf(_("The parameter %s has incorrect value for type bool."),$val['name']);
329                           };break;
330         case 'string' :
331         case 'combo'  : 
332         default : ;
334       }
335     }
336     return $message;
337   }
339   //! Save changes to openldap
340   /*!
341       Save to LDAP 
342       This function saves given attributes to the ldap
343   */
344   function save()
345   {
346     /* Post checks */
348     plugin::save();
350     $this->attrs['goFonMacroParameter']=array();
352     foreach($this->goFonMacroParameter as $key=>$fonpara){
353       $this->attrs['goFonMacroParameter'][]=$key."!".$fonpara['name']."!".$fonpara['type']."!".$fonpara['default'];
354     }
356     unset($this->attrs['base']);
358     /* Write back to ldap */
359     $ldap= $this->config->get_ldap_link();
360     $ldap->cat($this->dn);
361     $a= $ldap->fetch();
363     if (count($a)){
364       $ldap->cd($this->dn);
365       $ldap->modify($this->attrs);
366       $this->handle_post_events("modify");
367     } else {
368       if(count($this->attrs['goFonMacroParameter']==0)){
369         unset($this->attrs['goFonMacroParameter']);
370       }         
371       $ldap->cd($this->dn);
372       $ldap->create_missing_trees( $this->dn);
373       $ldap->cd($this->dn);
374       $ldap->add($this->attrs);
375       $this->handle_post_events("add");
376     }
377     show_ldap_error($ldap->get_error());
378   }
381 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
382 ?>