Code

Converted a couple of == / === NULL references
[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");
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     $content = $_SESSION['macroManagment']->macrotabs->by_object['macro']->goFonMacroContent;
194     if(strstr($content,"ARG")){
195       $vorpos = strpos($content,"ARG");
196       $rest   = substr($content,$vorpos, strlen($content));
197     }    
199     /* Do we represent a valid group? */
200     if (!$this->is_account && $this->parent === NULL){
201       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
202         _("This 'dn' is no phone macro.")."</b>";
203       return ($display);
204     }
206     /* Fill templating stuff */
207     $smarty= get_smarty();
209     /* Add an empty Parameter */
210     if(isset($_POST['addvar']) && preg_match("/w/",$ACLs)){
211       if(!is_array($this->goFonMacroParameter)){
212         $vars = $this->goFonMacroParameter;
213         $this->goFonMacroParameter = array();
214         $this->goFonMacroParameter[]= $vars;
215       }
216       $number= count($this->goFonMacroParameter);
217       $number++;
218       $this->goFonMacroParameter[]=array("var"=>"var","name"=>"new","type"=>"string","default"=>"test");
219     }
221     /*generate Table which shows als parameters */
222     $FonParas = $this->check_paras($content,$this->goFonMacroParameter); 
224     /* Sort by Parameterid, and keep keys */    
225     ksort($FonParas);
227     
229     if(!preg_match("/r/",$ACLs)){
230       $smarty->assign("readable",false);      
231     }else{
232       $smarty->assign("readable",true);      
233       foreach($FonParas as $key=>$para) {
235         /* Select correct item of combobox */
236         if(isset($para['type'])){
237           list($sel1, $sel2, $sel3)= $this->type_shortcut[$para['type']];
238         }else{
239           list($sel1, $sel2, $sel3)= array("", "", "");
240         }
241    
242         /* Disable all input fields if we are not allowed to change the parameters */ 
243         $disabled = "";
244         if(!preg_match("/w/",$ACLs)){
245           $key = "";
246           $disabled = " disabled ";
247         }
249         /* Assemble output table */
250         $vars .="<tr>
251           <td>
252             <input name=\"number".$key."\" value='".$key."' type='hidden' ".$disabled.">
253             <input name='var".$key."' type='hidden'   value='".$para['var']."' ".$disabled.">ARG".$key."
254           </td>
255           <td><input size=\"45\" name='varname".$key."'  value='".$para['name']."' ".$disabled."></td>
256           <td>
257             <select name='vartype".$key."'  ".$disabled.">
258               <option  value='string' ".$sel1.">"._("String")."&nbsp;</option>
259               <option  value='combo'   ".$sel2.">"._("Combobox")."&nbsp;</option>
260               <option  value='bool'   ".$sel3.">"._("Bool")."&nbsp;</option>
261             </select>
262           </td>
263           <td><input size=\"45\" name='default".$key."'   value='".$para['default']."'  ".$disabled."></td>
264           <td>&nbsp;";
265         if($para['check']==false) {
266           $vars.="<input name='del".$key."' value='"._("Delete unused")."' type='submit'>";
267         }
269         $vars.=" </td></tr>";
270       }
271     }
273     /* Checkboxes */
274     $smarty->assign("base_select", $this->base);
275     $smarty->assign("vars", $vars);
277     /* Show main page */
278     return($smarty->fetch (get_template_path('parameter.tpl', TRUE)));
279   }
280   
281   //! Unused here 
282   /*!
283       Unused here because goFonMacro will remove this Macro\n 
284   */
285   function remove_from_parent()
286   {
287   }
289   //! Save our data
290   /*! 
291       Save POST data to object \n
292       This gives us the possibility to leave a tab, without losing our typed informations\n
293       \n
294       Read the POST fields for the parameters and saves their info the the class\n
295   */
296   function save_object()
297   {
298     if (isset($_POST['phoneparameters'])){
299       plugin::save_object();
300     }
301     /* read out post data, and assign it to the parameters */
302     /* And or delete */
303     foreach($_POST as $name=>$value){
305       /* Test if there is a variable begining with "del" */
306       if(preg_match("/del/",$name)){
308         /* Extract entry id to delete */
309         $nr = str_replace("del","",$name) ;
311         /* unset entry */
312         unset($this->goFonMacroParameter[$nr]);
314       }elseif(preg_match("/number/",$name)){
316         /* Set Post vars */
317         $key = $_POST[$name];
318     
319         $this->goFonMacroParameter[$key]['var']   = $_POST["var".$key];
320         $this->goFonMacroParameter[$key]['name']   = $_POST["varname".$key];
321         $this->goFonMacroParameter[$key]['type']   = $_POST["vartype".$key];
322         $this->goFonMacroParameter[$key]['default']= $_POST["default".$key];
323       }
324     }
326   }
329   //! Checks given values 
330   /*! 
331       Check values\n 
332       If a user enters an invalid value, then this function will output an error msg\n
333       (In better words :prepare the errormessages that will be put out )\n
334   */
335   function check()
336   {
337     /* Call common method to give check the hook */
338     $message= plugin::check();
340     foreach($this->goFonMacroParameter as $key=>$val){
341       if((strstr($val['default'],"!"))||(strstr($val['default'],"#"))) {
342         $message[] = sprintf(_("The parameter %s contains invalid char. '!,#' is used as delimiter"),$val['name']);
343       }
344       switch($val['type']){
345         case 'bool'   :   $possible = array("","0","1");
346                           if(!in_array($val['default'],$possible)) {
347                             $message[] = sprintf(_("The parameter %s has incorrect value for type bool."),$val['name']);
348                           };break;
349         case 'string' :
350         case 'combo'  : 
351         default : ;
353       }
354     }
355     return $message;
356   }
358   //! Save changes to openldap
359   /*!
360       Save to LDAP 
361       This function saves given attributes to the ldap
362   */
363   function save()
364   {
365     /* Post checks */
367     plugin::save();
369     $this->attrs['goFonMacroParameter']=array();
371     foreach($this->goFonMacroParameter as $key=>$fonpara){
372       $this->attrs['goFonMacroParameter'][]=$key."!".base64_encode($fonpara['name'])."!".$fonpara['type']."!".$fonpara['default'];
373     }
375     if($this->para_count != count($this->attrs['goFonMacroParameter'])){
376       print_red(_("Parameter count of the macro changed, you must update each user which are using this macro '%s'."),$this->dn);
377     }
379     unset($this->attrs['base']);
381     /* Write back to ldap */
382     $ldap= $this->config->get_ldap_link();
383     $ldap->cat($this->dn, array('dn'));
384     $a= $ldap->fetch();
386     if (count($a)){
387       $ldap->cd($this->dn);
388       $this->cleanup();
389       $ldap->modify ($this->attrs); 
391       $this->handle_post_events("modify");
392     } else {
393       if(count($this->attrs['goFonMacroParameter']==0)){
394         unset($this->attrs['goFonMacroParameter']);
395       }         
396       $ldap->cd($this->dn);
397       $ldap->create_missing_trees( $this->dn);
398       $ldap->cd($this->dn);
399       $ldap->add($this->attrs);
400       $this->handle_post_events("add");
401     }
402     show_ldap_error($ldap->get_error(), sprintf(_("Removing of goFonMacro/generic account with dn '%s' failed."),$this->dn));
403   }
405   function PrepareForCopyPaste($source)
406   {
407     plugin::PrepareForCopyPaste($source);
409     $source_o = new macroParameter($this->config,$source['dn']);
410     $this->goFonMacroParameter = $source_o-> goFonMacroParameter;
411   }
413 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
414 ?>