Code

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