Code

fixed macro plugin image.
[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   /* Parameter Count*/
14   var $para_count = 0 ; 
15   /*! macro base  */
16   var $base= "";
17   
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");
28   
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     }
93     
94     /* Assign this array */
95     $this->goFonMacroParameter = $tmp2;
97     $this->para_count = count ($tmp2);
98    
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; 
178  
179     /* Get acls for this tab, 
180         there is only one attribute to write,
181          so we use the acls from gofon/marco */
182     if($this->is_new){
183       $ACLs = $this->ui->get_permissions($this->base,"gofonmacro/macro","goFonMacroContent");
184     }else{
185       $ACLs = $this->ui->get_permissions($this->dn,"gofonmacro/macro","goFonMacroContent");
186     }
188     /* get current content */
189     $macroManagment = session::get('macroManagement') ;
190     $content = $macroManagment->macrotabs->by_object['macro']->goFonMacroContent;
192     if(strstr($content,"ARG")){
193       $vorpos = strpos($content,"ARG");
194       $rest   = substr($content,$vorpos, strlen($content));
195     }    
197     /* Do we represent a valid group? */
198     if (!$this->is_account && $this->parent === NULL){
199       $display= "<img alt=\"\" src=\"images/small-error.png\" align=\"middle\">&nbsp;<b>".
200         msgPool::noValidExtension(_("Phone macro"))."</b>";
201       return ($display);
202     }
204     /* Fill templating stuff */
205     $smarty= get_smarty();
207     /* Add an empty Parameter */
208     if(isset($_POST['addvar']) && preg_match("/w/",$ACLs)){
209       if(!is_array($this->goFonMacroParameter)){
210         $vars = $this->goFonMacroParameter;
211         $this->goFonMacroParameter = array();
212         $this->goFonMacroParameter[]= $vars;
213       }
214       $number= count($this->goFonMacroParameter);
215       $number++;
216       $this->goFonMacroParameter[]=array("var"=>"var","name"=>"new","type"=>"string","default"=>"test");
217     }
219     /*generate Table which shows als parameters */
220     $FonParas = $this->check_paras($content,$this->goFonMacroParameter); 
222     /* Sort by Parameterid, and keep keys */    
223     ksort($FonParas);
225     
227     if(!preg_match("/r/",$ACLs)){
228       $smarty->assign("readable",false);      
229     }else{
230       $smarty->assign("readable",true);      
231       foreach($FonParas as $key=>$para) {
233         /* Select correct item of combobox */
234         if(isset($para['type'])){
235           list($sel1, $sel2, $sel3)= $this->type_shortcut[$para['type']];
236         }else{
237           list($sel1, $sel2, $sel3)= array("", "", "");
238         }
239    
240         /* Disable all input fields if we are not allowed to change the parameters */ 
241         $disabled = "";
242         if(!preg_match("/w/",$ACLs)){
243           $key = "";
244           $disabled = " disabled ";
245         }
247         /* Assemble output table */
248         $vars .="<tr>
249           <td>
250             <input name=\"number".$key."\" value='".$key."' type='hidden' ".$disabled.">
251             <input name='var".$key."' type='hidden'   value='".$para['var']."' ".$disabled.">ARG".$key."
252           </td>
253           <td><input size=\"45\" name='varname".$key."'  value='".$para['name']."' ".$disabled."></td>
254           <td>
255             <select name='vartype".$key."'  ".$disabled.">
256               <option  value='string' ".$sel1.">"._("String")."&nbsp;</option>
257               <option  value='combo'   ".$sel2.">"._("Combobox")."&nbsp;</option>
258               <option  value='bool'   ".$sel3.">"._("Bool")."&nbsp;</option>
259             </select>
260           </td>
261           <td><input size=\"45\" name='default".$key."'   value='".$para['default']."'  ".$disabled."></td>
262           <td>&nbsp;";
263         if($para['check']==false) {
264           $vars.="<input name='del".$key."' value='"._("Delete unused")."' type='submit'>";
265         }
267         $vars.=" </td></tr>";
268       }
269     }
271     /* Checkboxes */
272     $smarty->assign("base_select", $this->base);
273     $smarty->assign("vars", $vars);
275     /* Show main page */
276     return($smarty->fetch (get_template_path('parameter.tpl', TRUE)));
277   }
278   
279   //! Unused here 
280   /*!
281       Unused here because goFonMacro will remove this Macro\n 
282   */
283   function remove_from_parent()
284   {
285   }
287   //! Save our data
288   /*! 
289       Save POST data to object \n
290       This gives us the possibility to leave a tab, without losing our typed informations\n
291       \n
292       Read the POST fields for the parameters and saves their info the the class\n
293   */
294   function save_object()
295   {
296     if (isset($_POST['phoneparameters'])){
297       plugin::save_object();
298     }
299     /* read out post data, and assign it to the parameters */
300     /* And or delete */
301     foreach($_POST as $name=>$value){
303       /* Test if there is a variable begining with "del" */
304       if(preg_match("/del/",$name)){
306         /* Extract entry id to delete */
307         $nr = str_replace("del","",$name) ;
309         /* unset entry */
310         unset($this->goFonMacroParameter[$nr]);
312       }elseif(preg_match("/number/",$name)){
314         /* Set Post vars */
315         $key = $_POST[$name];
316     
317         $this->goFonMacroParameter[$key]['var']   = $_POST["var".$key];
318         $this->goFonMacroParameter[$key]['name']   = $_POST["varname".$key];
319         $this->goFonMacroParameter[$key]['type']   = $_POST["vartype".$key];
320         $this->goFonMacroParameter[$key]['default']= $_POST["default".$key];
321       }
322     }
324   }
327   //! Checks given values 
328   /*! 
329       Check values\n 
330       If a user enters an invalid value, then this function will output an error msg\n
331       (In better words :prepare the errormessages that will be put out )\n
332   */
333   function check()
334   {
335     /* Call common method to give check the hook */
336     $message= plugin::check();
338     foreach($this->goFonMacroParameter as $key=>$val){
339       if((strstr($val['default'],"!"))||(strstr($val['default'],"#"))) {
340         $message[] = sprintf(_("Parameter %s contains invalid character. '!,#' is used as delimiter!"),$val['name']);
341       }
342       switch($val['type']){
343         case 'bool'   :   $possible = array("","0","1");
344                           if(!in_array($val['default'],$possible)) {
345                             $message[] = sprintf(_("Parameter %s is invalid!"),$val['name']);
346                           };break;
347         case 'string' :
348         case 'combo'  : 
349         default : ;
351       }
352     }
353     return $message;
354   }
356   //! Save changes to openldap
357   /*!
358       Save to LDAP 
359       This function saves given attributes to the ldap
360   */
361   function save()
362   {
363     /* Post checks */
365     plugin::save();
367     $this->attrs['goFonMacroParameter']=array();
369     foreach($this->goFonMacroParameter as $key=>$fonpara){
370       $this->attrs['goFonMacroParameter'][]=$key."!".base64_encode($fonpara['name'])."!".$fonpara['type']."!".$fonpara['default'];
371     }
373     if($this->para_count != count($this->attrs['goFonMacroParameter'])){
374       msg_dialog::display(_("Information"), _("Number of parameters for this macro has changed. Please update all users using it!"), INFO_DIALOG);
375     }
377     unset($this->attrs['base']);
379     /* Write back to ldap */
380     $ldap= $this->config->get_ldap_link();
381     $ldap->cat($this->dn, array('dn'));
382     $a= $ldap->fetch();
384     if (count($a)){
385       $ldap->cd($this->dn);
386       $this->cleanup();
387       $ldap->modify ($this->attrs); 
389       $this->handle_post_events("modify");
390     } else {
391       if(count($this->attrs['goFonMacroParameter']==0)){
392         unset($this->attrs['goFonMacroParameter']);
393       }         
394       $ldap->cd($this->dn);
395       $ldap->create_missing_trees( $this->dn);
396       $ldap->cd($this->dn);
397       $ldap->add($this->attrs);
398       $this->handle_post_events("add");
399     }
400     if (!$ldap->success()){
401       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
402     }
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 ?>