Code

Added empty lines
[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();
179     /* Variables */
180     $vars       = "";
181     $tmp        = array();
182     $number = 0; 
183   
184     $content = $_SESSION['macroManagment']->macrotabs->by_object['macro']->goFonMacroContent;
186     if(strstr($content,"ARG")){
187       $vorpos = strpos($content,"ARG");
188       $rest   = substr($content,$vorpos, strlen($content));
189     }    
191     /* Do we represent a valid group? */
192     if (!$this->is_account && $this->parent == NULL){
193       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
194         _("This 'dn' is no phone macro.")."</b>";
195       return ($display);
196     }
198     /* Fill templating stuff */
199     $smarty= get_smarty();
201     /* Assign all vars to Smarty */
202     foreach($this->attributes as $ar){
203       $smarty->assign($ar, $this->$ar);
204     }
206     /* Add an empty Parameter */
207     if(isset($_POST['addvar'])){
208       if(!is_array($this->goFonMacroParameter)){
209         $vars = $this->goFonMacroParameter;
210         $this->goFonMacroParameter = array();
211         $this->goFonMacroParameter[]= $vars;
212       }
213       $number= count($this->goFonMacroParameter);
214       $number++;
215       $this->goFonMacroParameter[]=array("var"=>"var","name"=>"new","type"=>"string","default"=>"test");
216     }
218     /*generate Table which shows als parameters */
219     $FonParas = $this->check_paras($content,$this->goFonMacroParameter); 
221     /* Sort by Parameterid, and keep keys */    
222     ksort($FonParas);
224     foreach($FonParas as $key=>$para)   {
226       /* Select correct item of combobox */
227       if(isset($para['type'])){
228         list($sel1, $sel2, $sel3)= $this->type_shortcut[$para['type']];
229       }else{
230         list($sel1, $sel2, $sel3)= array("", "", "");
231       }
233       /* Assemble output table */
234       $vars .="<tr>
235         <td>
236           <input name=\"number".$key."\" value='".$key."' type='hidden'>
237           <input name='var".$key."' type='hidden'   value='".$para['var']."'>ARG".$key."
238         </td>
239         <td><input size=\"45\" name='varname".$key."'  value='".$para['name']."'></td>
240         <td>
241           <select name='vartype".$key."'>
242             <option  value='string' ".$sel1.">"._("String")."&nbsp;</option>
243             <option  value='combo'   ".$sel2.">"._("Combobox")."&nbsp;</option>
244             <option  value='bool'   ".$sel3.">"._("Bool")."&nbsp;</option>
245           </select>
246         </td>
247         <td><input size=\"45\" name='default".$key."'   value='".$para['default']."'></td>
248         <td>&nbsp;";
249       if($para['check']==false) {
250         $vars.="<input name='del".$key."' value='"._("Delete unused")."' type='submit'>";
251       }
253       $vars.=" </td></tr>";
254     }
256     /* Checkboxes */
257     $smarty->assign("base_select", $this->base);
258     $smarty->assign("vars", $vars);
260     /* Show main page */
261     return($smarty->fetch (get_template_path('parameter.tpl', TRUE)));
262   }
263   
264   //! Unused here 
265   /*!
266       Unused here because goFonMacro will remove this Macro\n 
267   */
268   function remove_from_parent()
269   {
270   }
272   //! Save our data
273   /*! 
274       Save POST data to object \n
275       This gives us the possibility to leave a tab, without losing our typed informations\n
276       \n
277       Read the POST fields for the parameters and saves their info the the class\n
278   */
279   function save_object()
280   {
281     if (isset($_POST['phoneparameters'])){
282       plugin::save_object();
283     }
284     /* read out post data, and assign it to the parameters */
285     /* And or delete */
286     foreach($_POST as $name=>$value){
288       /* Test if there is a variable begining with "del" */
289       if(preg_match("/del/",$name)){
291         /* Extract entry id to delete */
292         $nr = str_replace("del","",$name) ;
294         /* unset entry */
295         unset($this->goFonMacroParameter[$nr]);
297       }elseif(preg_match("/number/",$name)){
299         /* Set Post vars */
300         $key = $_POST[$name];
302         $this->goFonMacroParameter[$key]['var']   = $_POST["var".$key];
303         $this->goFonMacroParameter[$key]['name']   = $_POST["varname".$key];
304         $this->goFonMacroParameter[$key]['type']   = $_POST["vartype".$key];
305         $this->goFonMacroParameter[$key]['default']= $_POST["default".$key];
306       }
307     }
309   }
312   //! Checks given values 
313   /*! 
314       Check values\n 
315       If a user enters an invalid value, then this function will output an error msg\n
316       (In better words :prepare the errormessages that will be put out )\n
317   */
318   function check()
319   {
320     $message = array();
322     foreach($this->attributes as $attr){
323       if(chkacl($this->acl,$attr)){
324         $str =  sprintf( _("Insufficient permissions, can't change attribute '%s' in goFonMacro."),$attr) ;
325         return(array($str));
326       }
327     }
330     foreach($this->goFonMacroParameter as $key=>$val){
331       if((strstr($val['default'],"!"))||(strstr($val['default'],"#"))) {
332         $message[] = sprintf(_("The parameter %s contains invalid char. '!,#' is used as delimiter"),$val['name']);
333       }
334       switch($val['type']){
335         case 'bool'   :   $possible = array("","0","1");
336                           if(!in_array($val['default'],$possible)) {
337                             $message[] = sprintf(_("The parameter %s has incorrect value for type bool."),$val['name']);
338                           };break;
339         case 'string' :
340         case 'combo'  : 
341         default : ;
343       }
344     }
345     return $message;
346   }
348   //! Save changes to openldap
349   /*!
350       Save to LDAP 
351       This function saves given attributes to the ldap
352   */
353   function save()
354   {
355     /* Post checks */
357     plugin::save();
359     $this->attrs['goFonMacroParameter']=array();
361     foreach($this->goFonMacroParameter as $key=>$fonpara){
362       $this->attrs['goFonMacroParameter'][]=$key."!".base64_encode($fonpara['name'])."!".$fonpara['type']."!".$fonpara['default'];
363     }
365     if($this->para_count != count($this->attrs['goFonMacroParameter'])){
366       print_red(_("Parameter count of the macro changed, you must update each user which are using this macro '%s'."),$this->dn);
367     }
369     unset($this->attrs['base']);
371     /* Write back to ldap */
372     $ldap= $this->config->get_ldap_link();
373     $ldap->cat($this->dn);
374     $a= $ldap->fetch();
376     if (count($a)){
377       $ldap->cd($this->dn);
378       $ldap->modify($this->attrs);
379       $this->handle_post_events("modify");
380     } else {
381       if(count($this->attrs['goFonMacroParameter']==0)){
382         unset($this->attrs['goFonMacroParameter']);
383       }         
384       $ldap->cd($this->dn);
385       $ldap->create_missing_trees( $this->dn);
386       $ldap->cd($this->dn);
387       $ldap->add($this->attrs);
388       $this->handle_post_events("add");
389     }
390     show_ldap_error($ldap->get_error());
391   }
394 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
395 ?>