Code

Created needed structure
[gosa.git] / gosa-plugins / gofon / 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");
38   var $ui;
40   //! The konstructor of macroParameter    
41   /*! The konstructor of macroParameter...
42      - reads goFonMacroParameter and parses them to an array 
43      - Set attributes from openldap (edit)
44      - Set attributes from default (new)
45   */
46   function macroParameter (&$config, $dn= NULL, $parent= NULL)
47   {
48     plugin::plugin ($config, $dn, $parent);
50     $tmp = array();  // temporary Var 
51     $tmp2 = array(); // temporary Var ...
52     $tmp3 = "";
53     $ldap= $config->get_ldap_link();
55     $this->dn = $dn;
57     /* This is always an account */
58     $this->is_account= TRUE;
60     /* Edit or new one ?*/
61     if ($this->dn == "new"){
62       $ui= get_userinfo();
63       $this->base= dn2base($ui->dn);
64     } else {
65       $this->base= dn2base($this->dn);
66     }
68     /* initialising macro parameter */
69     if(isset($this->attrs['goFonMacroParameter']) &&
70         isset($this->attrs['goFonMacroParameter']['count'])){
71       unset($this->attrs['goFonMacroParameter']['count']);
72     }
74     /* Set Parameters, or a new array if ther are no parameters */
75     if(isset($this->attrs['goFonMacroParameter'])){
76       $this->goFonMacroParameter = $this->attrs['goFonMacroParameter'];
77     }else{
78       $this->goFonMacroParameter =array();
79     }
81     /* Create an array for parameters if not given yet */
82     if(!is_array($this->goFonMacroParameter)){
83       $tmp3 = $this->goFonMacroParameter;
84       $this->goFonMacroParameter =array();      
85       if(!empty($tmp3)) {
86         $this->goFonMacroParameter[]  = $tmp3;
87       }
88     }
90     /* Load parametersettings*/
91     foreach($this->goFonMacroParameter as $para){
92       $tmp = split("!",$para);
93       $num = $tmp[0];
94       $tmp2[$num]['name']        = base64_decode($tmp[1]);
95       $tmp2[$num]['type']        = $tmp[2];
96       $tmp2[$num]['default']     = $tmp[3];
97       $tmp2[$num]['var']         = "var".$num;
98     }
100     
101     /* Assign this array */
102     $this->goFonMacroParameter = $tmp2;
104     $this->para_count = count ($tmp2);
105    
106     $this->ui= get_userinfo();
107   }
109   //! Perform Parameter check 
110   /*! 
111       Compares the given parameters (goFonMacroParameters) with the parameters defined in goFonContent\n 
112       -> Decide which attrs are new and which are unused\n
113       -> Sort result array (containing both parameters 'goFonMacroParameters/goFonContent' and new / unused info)\n
114       \param $content The given goFonContent for this macro\n
115       \param $goFonMacroParameter Array with the already given parameters \n
116    */
117   function check_paras($content,$goFonMacroParameter)
118   { 
119     /* Check contents for parameters */
120     preg_match_all("/[$]\{ARG[0-9]*\}/",$content,$res,PREG_OFFSET_CAPTURE);
122     $new = array();
124     /* Detect parameters with positions */
125     foreach($res[0] as $val){
126       $num = preg_replace("/[^0-9]/","",$val[0]); 
127       $new[$num]['val'] = $val[0];
128       $new[$num]['num'] = $num;
129     }
131     /* Compare content parameter and macro parameter */
132     foreach($goFonMacroParameter as $gokey => $goval){
133       foreach($new as $nkey => $nval){
134         if($gokey == $nval['num']){
135           /* sign this as OK */
136           $goFonMacroParameter[$gokey]['check']= true;
137         }
138       }
139     }
141     /* Now check if there is new parameter in the content, which is not assigned yet */
142     foreach($new as $key => $val){
143       /* Assign std values */
144       $goFonMacroParameter[$key]['var']="var".$key;
145       $goFonMacroParameter[$key]['check']= true;
147       /* If this is a new Parameter, name it ${ARG#} by default*/
148       if((!isset($goFonMacroParameter[$key]['name']))||(empty($goFonMacroParameter[$key]['name']))){
149         $goFonMacroParameter[$key]['name']="\${ARG".$key."}";
150       }
151     }  
153     foreach($goFonMacroParameter as $key => $val){
154       /* All attributes with check == false, are unneeded so mark them with ['check']= false */
155       if(!isset($goFonMacroParameter[$key]['check'])){
156         $goFonMacroParameter[$key]['check']= false;
157       }
158       /* Ah no default given assign ="" to prevent unsigned index  */
159       if(!isset($goFonMacroParameter[$key]['default'])){
160         $goFonMacroParameter[$key]['default'] = "";
161       }
162     }
164     /* Sort output for better reading */
165     asort($goFonMacroParameter);
166     return($goFonMacroParameter);
168   }
170   //! Execute this Plugin
171   /*! 
172       Perform Parameter check \n
173       Draw paramter table\n
174       Show tpl   \n 
175   */
176   function execute()
177   {
178     /* Call parent execute */
179     plugin::execute();
181     /* Variables */
182     $vars       = "";
183     $tmp        = array();
184     $number = 0; 
185  
186     /* Get acls for this tab, 
187         there is only one attribute to write,
188          so we use the acls from gofon/marco */
189     $ACLs = $this->ui->get_permissions($this->dn,"gofonmacro/macro","goFonMarcoContent");
191     /* get current content */
192     $macroManagment = session::get('macroManagment') ;
193     $content = $macroManagment->macrotabs->by_object['macro']->goFonMacroContent;
195     if(strstr($content,"ARG")){
196       $vorpos = strpos($content,"ARG");
197       $rest   = substr($content,$vorpos, strlen($content));
198     }    
200     /* Do we represent a valid group? */
201     if (!$this->is_account && $this->parent === NULL){
202       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
203         _("This 'dn' is no phone macro.")."</b>";
204       return ($display);
205     }
207     /* Fill templating stuff */
208     $smarty= get_smarty();
210     /* Add an empty Parameter */
211     if(isset($_POST['addvar']) && preg_match("/w/",$ACLs)){
212       if(!is_array($this->goFonMacroParameter)){
213         $vars = $this->goFonMacroParameter;
214         $this->goFonMacroParameter = array();
215         $this->goFonMacroParameter[]= $vars;
216       }
217       $number= count($this->goFonMacroParameter);
218       $number++;
219       $this->goFonMacroParameter[]=array("var"=>"var","name"=>"new","type"=>"string","default"=>"test");
220     }
222     /*generate Table which shows als parameters */
223     $FonParas = $this->check_paras($content,$this->goFonMacroParameter); 
225     /* Sort by Parameterid, and keep keys */    
226     ksort($FonParas);
228     
230     if(!preg_match("/r/",$ACLs)){
231       $smarty->assign("readable",false);      
232     }else{
233       $smarty->assign("readable",true);      
234       foreach($FonParas as $key=>$para) {
236         /* Select correct item of combobox */
237         if(isset($para['type'])){
238           list($sel1, $sel2, $sel3)= $this->type_shortcut[$para['type']];
239         }else{
240           list($sel1, $sel2, $sel3)= array("", "", "");
241         }
242    
243         /* Disable all input fields if we are not allowed to change the parameters */ 
244         $disabled = "";
245         if(!preg_match("/w/",$ACLs)){
246           $key = "";
247           $disabled = " disabled ";
248         }
250         /* Assemble output table */
251         $vars .="<tr>
252           <td>
253             <input name=\"number".$key."\" value='".$key."' type='hidden' ".$disabled.">
254             <input name='var".$key."' type='hidden'   value='".$para['var']."' ".$disabled.">ARG".$key."
255           </td>
256           <td><input size=\"45\" name='varname".$key."'  value='".$para['name']."' ".$disabled."></td>
257           <td>
258             <select name='vartype".$key."'  ".$disabled.">
259               <option  value='string' ".$sel1.">"._("String")."&nbsp;</option>
260               <option  value='combo'   ".$sel2.">"._("Combobox")."&nbsp;</option>
261               <option  value='bool'   ".$sel3.">"._("Bool")."&nbsp;</option>
262             </select>
263           </td>
264           <td><input size=\"45\" name='default".$key."'   value='".$para['default']."'  ".$disabled."></td>
265           <td>&nbsp;";
266         if($para['check']==false) {
267           $vars.="<input name='del".$key."' value='"._("Delete unused")."' type='submit'>";
268         }
270         $vars.=" </td></tr>";
271       }
272     }
274     /* Checkboxes */
275     $smarty->assign("base_select", $this->base);
276     $smarty->assign("vars", $vars);
278     /* Show main page */
279     return($smarty->fetch (get_template_path('parameter.tpl', TRUE)));
280   }
281   
282   //! Unused here 
283   /*!
284       Unused here because goFonMacro will remove this Macro\n 
285   */
286   function remove_from_parent()
287   {
288   }
290   //! Save our data
291   /*! 
292       Save POST data to object \n
293       This gives us the possibility to leave a tab, without losing our typed informations\n
294       \n
295       Read the POST fields for the parameters and saves their info the the class\n
296   */
297   function save_object()
298   {
299     if (isset($_POST['phoneparameters'])){
300       plugin::save_object();
301     }
302     /* read out post data, and assign it to the parameters */
303     /* And or delete */
304     foreach($_POST as $name=>$value){
306       /* Test if there is a variable begining with "del" */
307       if(preg_match("/del/",$name)){
309         /* Extract entry id to delete */
310         $nr = str_replace("del","",$name) ;
312         /* unset entry */
313         unset($this->goFonMacroParameter[$nr]);
315       }elseif(preg_match("/number/",$name)){
317         /* Set Post vars */
318         $key = $_POST[$name];
319     
320         $this->goFonMacroParameter[$key]['var']   = $_POST["var".$key];
321         $this->goFonMacroParameter[$key]['name']   = $_POST["varname".$key];
322         $this->goFonMacroParameter[$key]['type']   = $_POST["vartype".$key];
323         $this->goFonMacroParameter[$key]['default']= $_POST["default".$key];
324       }
325     }
327   }
330   //! Checks given values 
331   /*! 
332       Check values\n 
333       If a user enters an invalid value, then this function will output an error msg\n
334       (In better words :prepare the errormessages that will be put out )\n
335   */
336   function check()
337   {
338     /* Call common method to give check the hook */
339     $message= plugin::check();
341     foreach($this->goFonMacroParameter as $key=>$val){
342       if((strstr($val['default'],"!"))||(strstr($val['default'],"#"))) {
343         $message[] = sprintf(_("The parameter %s contains invalid char. '!,#' is used as delimiter"),$val['name']);
344       }
345       switch($val['type']){
346         case 'bool'   :   $possible = array("","0","1");
347                           if(!in_array($val['default'],$possible)) {
348                             $message[] = sprintf(_("The parameter %s has incorrect value for type bool."),$val['name']);
349                           };break;
350         case 'string' :
351         case 'combo'  : 
352         default : ;
354       }
355     }
356     return $message;
357   }
359   //! Save changes to openldap
360   /*!
361       Save to LDAP 
362       This function saves given attributes to the ldap
363   */
364   function save()
365   {
366     /* Post checks */
368     plugin::save();
370     $this->attrs['goFonMacroParameter']=array();
372     foreach($this->goFonMacroParameter as $key=>$fonpara){
373       $this->attrs['goFonMacroParameter'][]=$key."!".base64_encode($fonpara['name'])."!".$fonpara['type']."!".$fonpara['default'];
374     }
376     if($this->para_count != count($this->attrs['goFonMacroParameter'])){
377       print_red(_("Parameter count of the macro changed, you must update each user which are using this macro '%s'."),$this->dn);
378     }
380     unset($this->attrs['base']);
382     /* Write back to ldap */
383     $ldap= $this->config->get_ldap_link();
384     $ldap->cat($this->dn, array('dn'));
385     $a= $ldap->fetch();
387     if (count($a)){
388       $ldap->cd($this->dn);
389       $this->cleanup();
390       $ldap->modify ($this->attrs); 
392       $this->handle_post_events("modify");
393     } else {
394       if(count($this->attrs['goFonMacroParameter']==0)){
395         unset($this->attrs['goFonMacroParameter']);
396       }         
397       $ldap->cd($this->dn);
398       $ldap->create_missing_trees( $this->dn);
399       $ldap->cd($this->dn);
400       $ldap->add($this->attrs);
401       $this->handle_post_events("add");
402     }
403     show_ldap_error($ldap->get_error(), sprintf(_("Removing of goFonMacro/generic account with dn '%s' failed."),$this->dn));
404   }
406   function PrepareForCopyPaste($source)
407   {
408     plugin::PrepareForCopyPaste($source);
410     $source_o = new macroParameter($this->config,$source['dn']);
411     $this->goFonMacroParameter = $source_o-> goFonMacroParameter;
412   }
414 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
415 ?>