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 /* Parameter Count*/
14 var $para_count = 0 ;
15 /*! macro base */
16 var $base= "";
18 /*! This array contains all Parameter defined for the macro*/
19 var $goFonMacroParameter =array();
21 /*! This are the available types for a macro */
22 var $type_shortcut= array("string" => array("selected", "", ""),
23 "combo" => array("", "selected", ""),
24 "bool" => array("", "", "selected"));
26 /*! attribute list for save action */
27 var $attributes= array("base","goFonMacroParameter");
29 /*! Objectclasses needed by the class*/
30 var $objectclasses= array("top", "goFonMacro");
31 var $ui;
33 //! The konstructor of macroParameter
34 /*! The konstructor of macroParameter...
35 - reads goFonMacroParameter and parses them to an array
36 - Set attributes from openldap (edit)
37 - Set attributes from default (new)
38 */
39 function macroParameter (&$config, $dn= NULL, $parent= NULL)
40 {
41 plugin::plugin ($config, $dn, $parent);
43 $tmp = array(); // temporary Var
44 $tmp2 = array(); // temporary Var ...
45 $tmp3 = "";
46 $ldap= $config->get_ldap_link();
48 $this->dn = $dn;
50 /* This is always an account */
51 $this->is_account= TRUE;
53 /* Edit or new one ?*/
54 if ($this->dn == "new"){
55 $ui= get_userinfo();
56 $this->base= dn2base($ui->dn);
57 } else {
58 $this->base= dn2base($this->dn);
59 }
61 /* initialising macro parameter */
62 if(isset($this->attrs['goFonMacroParameter']) &&
63 isset($this->attrs['goFonMacroParameter']['count'])){
64 unset($this->attrs['goFonMacroParameter']['count']);
65 }
67 /* Set Parameters, or a new array if ther are no parameters */
68 if(isset($this->attrs['goFonMacroParameter'])){
69 $this->goFonMacroParameter = $this->attrs['goFonMacroParameter'];
70 }else{
71 $this->goFonMacroParameter =array();
72 }
74 /* Create an array for parameters if not given yet */
75 if(!is_array($this->goFonMacroParameter)){
76 $tmp3 = $this->goFonMacroParameter;
77 $this->goFonMacroParameter =array();
78 if(!empty($tmp3)) {
79 $this->goFonMacroParameter[] = $tmp3;
80 }
81 }
83 /* Load parametersettings*/
84 foreach($this->goFonMacroParameter as $para){
85 $tmp = split("!",$para);
86 $num = $tmp[0];
87 $tmp2[$num]['name'] = base64_decode($tmp[1]);
88 $tmp2[$num]['type'] = $tmp[2];
89 $tmp2[$num]['default'] = $tmp[3];
90 $tmp2[$num]['var'] = "var".$num;
91 }
94 /* Assign this array */
95 $this->goFonMacroParameter = $tmp2;
97 $this->para_count = count ($tmp2);
99 $this->ui= get_userinfo();
100 }
102 //! Perform Parameter check
103 /*!
104 Compares the given parameters (goFonMacroParameters) with the parameters defined in goFonContent\n
105 -> Decide which attrs are new and which are unused\n
106 -> Sort result array (containing both parameters 'goFonMacroParameters/goFonContent' and new / unused info)\n
107 \param $content The given goFonContent for this macro\n
108 \param $goFonMacroParameter Array with the already given parameters \n
109 */
110 function check_paras($content,$goFonMacroParameter)
111 {
112 /* Check contents for parameters */
113 preg_match_all("/[$]\{ARG[0-9]*\}/",$content,$res,PREG_OFFSET_CAPTURE);
115 $new = array();
117 /* Detect parameters with positions */
118 foreach($res[0] as $val){
119 $num = preg_replace("/[^0-9]/","",$val[0]);
120 $new[$num]['val'] = $val[0];
121 $new[$num]['num'] = $num;
122 }
124 /* Compare content parameter and macro parameter */
125 foreach($goFonMacroParameter as $gokey => $goval){
126 foreach($new as $nkey => $nval){
127 if($gokey == $nval['num']){
128 /* sign this as OK */
129 $goFonMacroParameter[$gokey]['check']= true;
130 }
131 }
132 }
134 /* Now check if there is new parameter in the content, which is not assigned yet */
135 foreach($new as $key => $val){
136 /* Assign std values */
137 $goFonMacroParameter[$key]['var']="var".$key;
138 $goFonMacroParameter[$key]['check']= true;
140 /* If this is a new Parameter, name it ${ARG#} by default*/
141 if((!isset($goFonMacroParameter[$key]['name']))||(empty($goFonMacroParameter[$key]['name']))){
142 $goFonMacroParameter[$key]['name']="\${ARG".$key."}";
143 }
144 }
146 foreach($goFonMacroParameter as $key => $val){
147 /* All attributes with check == false, are unneeded so mark them with ['check']= false */
148 if(!isset($goFonMacroParameter[$key]['check'])){
149 $goFonMacroParameter[$key]['check']= false;
150 }
151 /* Ah no default given assign ="" to prevent unsigned index */
152 if(!isset($goFonMacroParameter[$key]['default'])){
153 $goFonMacroParameter[$key]['default'] = "";
154 }
155 }
157 /* Sort output for better reading */
158 asort($goFonMacroParameter);
159 return($goFonMacroParameter);
161 }
163 //! Execute this Plugin
164 /*!
165 Perform Parameter check \n
166 Draw paramter table\n
167 Show tpl \n
168 */
169 function execute()
170 {
171 /* Call parent execute */
172 plugin::execute();
174 /* Variables */
175 $vars = "";
176 $tmp = array();
177 $number = 0;
179 /* Get acls for this tab,
180 there is only one attribute to write,
181 so we use the acls from gofon/marco */
182 $ACLs = $this->ui->get_permissions($this->dn,"gofonmacro/macro","goFonMarcoContent");
184 /* get current content */
185 $macroManagment = session::get('macroManagment') ;
186 $content = $macroManagment->macrotabs->by_object['macro']->goFonMacroContent;
188 if(strstr($content,"ARG")){
189 $vorpos = strpos($content,"ARG");
190 $rest = substr($content,$vorpos, strlen($content));
191 }
193 /* Do we represent a valid group? */
194 if (!$this->is_account && $this->parent === NULL){
195 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
196 msgPool::noValidExtension(_("Phone macro"))."</b>";
197 return ($display);
198 }
200 /* Fill templating stuff */
201 $smarty= get_smarty();
203 /* Add an empty Parameter */
204 if(isset($_POST['addvar']) && preg_match("/w/",$ACLs)){
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);
223 if(!preg_match("/r/",$ACLs)){
224 $smarty->assign("readable",false);
225 }else{
226 $smarty->assign("readable",true);
227 foreach($FonParas as $key=>$para) {
229 /* Select correct item of combobox */
230 if(isset($para['type'])){
231 list($sel1, $sel2, $sel3)= $this->type_shortcut[$para['type']];
232 }else{
233 list($sel1, $sel2, $sel3)= array("", "", "");
234 }
236 /* Disable all input fields if we are not allowed to change the parameters */
237 $disabled = "";
238 if(!preg_match("/w/",$ACLs)){
239 $key = "";
240 $disabled = " disabled ";
241 }
243 /* Assemble output table */
244 $vars .="<tr>
245 <td>
246 <input name=\"number".$key."\" value='".$key."' type='hidden' ".$disabled.">
247 <input name='var".$key."' type='hidden' value='".$para['var']."' ".$disabled.">ARG".$key."
248 </td>
249 <td><input size=\"45\" name='varname".$key."' value='".$para['name']."' ".$disabled."></td>
250 <td>
251 <select name='vartype".$key."' ".$disabled.">
252 <option value='string' ".$sel1.">"._("String")." </option>
253 <option value='combo' ".$sel2.">"._("Combobox")." </option>
254 <option value='bool' ".$sel3.">"._("Bool")." </option>
255 </select>
256 </td>
257 <td><input size=\"45\" name='default".$key."' value='".$para['default']."' ".$disabled."></td>
258 <td> ";
259 if($para['check']==false) {
260 $vars.="<input name='del".$key."' value='"._("Delete unused")."' type='submit'>";
261 }
263 $vars.=" </td></tr>";
264 }
265 }
267 /* Checkboxes */
268 $smarty->assign("base_select", $this->base);
269 $smarty->assign("vars", $vars);
271 /* Show main page */
272 return($smarty->fetch (get_template_path('parameter.tpl', TRUE)));
273 }
275 //! Unused here
276 /*!
277 Unused here because goFonMacro will remove this Macro\n
278 */
279 function remove_from_parent()
280 {
281 }
283 //! Save our data
284 /*!
285 Save POST data to object \n
286 This gives us the possibility to leave a tab, without losing our typed informations\n
287 \n
288 Read the POST fields for the parameters and saves their info the the class\n
289 */
290 function save_object()
291 {
292 if (isset($_POST['phoneparameters'])){
293 plugin::save_object();
294 }
295 /* read out post data, and assign it to the parameters */
296 /* And or delete */
297 foreach($_POST as $name=>$value){
299 /* Test if there is a variable begining with "del" */
300 if(preg_match("/del/",$name)){
302 /* Extract entry id to delete */
303 $nr = str_replace("del","",$name) ;
305 /* unset entry */
306 unset($this->goFonMacroParameter[$nr]);
308 }elseif(preg_match("/number/",$name)){
310 /* Set Post vars */
311 $key = $_POST[$name];
313 $this->goFonMacroParameter[$key]['var'] = $_POST["var".$key];
314 $this->goFonMacroParameter[$key]['name'] = $_POST["varname".$key];
315 $this->goFonMacroParameter[$key]['type'] = $_POST["vartype".$key];
316 $this->goFonMacroParameter[$key]['default']= $_POST["default".$key];
317 }
318 }
320 }
323 //! Checks given values
324 /*!
325 Check values\n
326 If a user enters an invalid value, then this function will output an error msg\n
327 (In better words :prepare the errormessages that will be put out )\n
328 */
329 function check()
330 {
331 /* Call common method to give check the hook */
332 $message= plugin::check();
334 foreach($this->goFonMacroParameter as $key=>$val){
335 if((strstr($val['default'],"!"))||(strstr($val['default'],"#"))) {
336 $message[] = sprintf(_("Parameter %s contains invalid character. '!,#' is used as delimiter!"),$val['name']);
337 }
338 switch($val['type']){
339 case 'bool' : $possible = array("","0","1");
340 if(!in_array($val['default'],$possible)) {
341 $message[] = sprintf(_("Parameter %s is invalid!"),$val['name']);
342 };break;
343 case 'string' :
344 case 'combo' :
345 default : ;
347 }
348 }
349 return $message;
350 }
352 //! Save changes to openldap
353 /*!
354 Save to LDAP
355 This function saves given attributes to the ldap
356 */
357 function save()
358 {
359 /* Post checks */
361 plugin::save();
363 $this->attrs['goFonMacroParameter']=array();
365 foreach($this->goFonMacroParameter as $key=>$fonpara){
366 $this->attrs['goFonMacroParameter'][]=$key."!".base64_encode($fonpara['name'])."!".$fonpara['type']."!".$fonpara['default'];
367 }
369 if($this->para_count != count($this->attrs['goFonMacroParameter'])){
370 msg_dialog::display(_("Information"), _("Number of parameters for this macro has changed. Please update all users using it!"), INFO_DIALOG);
371 }
373 unset($this->attrs['base']);
375 /* Write back to ldap */
376 $ldap= $this->config->get_ldap_link();
377 $ldap->cat($this->dn, array('dn'));
378 $a= $ldap->fetch();
380 if (count($a)){
381 $ldap->cd($this->dn);
382 $this->cleanup();
383 $ldap->modify ($this->attrs);
385 $this->handle_post_events("modify");
386 } else {
387 if(count($this->attrs['goFonMacroParameter']==0)){
388 unset($this->attrs['goFonMacroParameter']);
389 }
390 $ldap->cd($this->dn);
391 $ldap->create_missing_trees( $this->dn);
392 $ldap->cd($this->dn);
393 $ldap->add($this->attrs);
394 $this->handle_post_events("add");
395 }
396 if (!$ldap->success()){
397 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
398 }
399 }
401 function PrepareForCopyPaste($source)
402 {
403 plugin::PrepareForCopyPaste($source);
405 $source_o = new macroParameter($this->config,$source['dn']);
406 $this->goFonMacroParameter = $source_o-> goFonMacroParameter;
407 }
408 }
409 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
410 ?>