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