Code

nothing important
[gosa.git] / plugins / gofon / macro / class_gofonMacroParameters.inc
1 <?php
3 class macroParameter extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary= "Handling of GOsa's application object";
7   var $cli_description= "Some longer text\nfor help";
8   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10   /* application attributes */
11   var $base= "";
12   var $goFonMacroParameter =array();
13   var $type_shortcut= array("string" => array("selected", "", ""),
14       "combo"  => array("", "selected", ""),
15       "bool"   => array("", "", "selected"));
17   /* attribute list for save action */
18   var $attributes= array("base","goFonMacroParameter");
19   var $objectclasses= array("top", "goFonMacro");
21   /*!
22     Konstructor
23    */
24   function macroParameter ($config, $dn= NULL)
25   {
26     plugin::plugin ($config, $dn);
28     $tmp = array();  // temporary Var 
29     $tmp2 = array(); // temporary Var ...
30     $tmp3 = "";
31     $ldap= $config->get_ldap_link();
33     $this->dn = $dn;
35     /* This is always an account */
36     $this->is_account= TRUE;
38     /* Edit or new one ?*/
39     if ($this->dn == "new"){
40       $ui= get_userinfo();
41       $this->base= dn2base($ui->dn);
42     } else {
43       $this->base= dn2base($this->dn);
44     }
46     /* initialising macro parameter */
47     unset($this->attrs['goFonMacroParameter']['count']);
49     /* Set Parameters, or a new array if ther are no parameters */
50     if(isset($this->attrs['goFonMacroParameter'])){
51       $this->goFonMacroParameter = $this->attrs['goFonMacroParameter'];
52     }else{
53       $this->goFonMacroParameter =array();
54     }
56     /* Create an array for parameters if not given yet */
57     if(!is_array($this->goFonMacroParameter)){
58       $tmp3 = $this->goFonMacroParameter;
59       $this->goFonMacroParameter =array();      
60       if(!empty($tmp3)) {
61         $this->goFonMacroParameter[]  = $tmp3;
62       }
63     }
65     /* Load parametersettings*/
66     foreach($this->goFonMacroParameter as $para){
67       $tmp = split("!",$para);
68       $num = $tmp[0];
69       $tmp2[$num]['name']        = $tmp[1];
70       $tmp2[$num]['type']        = $tmp[2];
71       $tmp2[$num]['default']     = $tmp[3];
72       $tmp2[$num]['var']         = "var".$num;
73     }
75     /* Assign this array */
76     $this->goFonMacroParameter = $tmp2;
77   }
79   /*!
80     Check given parameters from content and from object and compare them
81    */
82   function check_paras($content,$goFonMacroParameter)
83   { 
84     /* Check contents for parameters */
85     preg_match_all("/[$]\{ARG[0-9]*\}/",$content,$res,PREG_OFFSET_CAPTURE);
87     $new = array();
89     /* Detect parameters with positions */
90     foreach($res[0] as $val){
91       $num = preg_replace("/[^0-9]/","",$val[0]); 
92       $new[$num]['val'] = $val[0];
93       $new[$num]['num'] = $num;
94     }
96     /* Compare content parameter and macro parameter */
97     foreach($goFonMacroParameter as $gokey => $goval){
98       foreach($new as $nkey => $nval){
99         if($gokey == $nval['num']){
100           /* sign this as OK */
101           $goFonMacroParameter[$gokey]['check']= true;
102         }
103       }
104     }
106     /* Now check if there is new parameter in the content, which is not assigned yet */
107     foreach($new as $key => $val){
108       /* Assign std values */
109       $goFonMacroParameter[$key]['var']="var".$key;
110       $goFonMacroParameter[$key]['check']= true;
112       /* If this is a new Parameter, name it ${ARG#} by default*/
113       if((!isset($goFonMacroParameter[$key]['name']))||(empty($goFonMacroParameter[$key]['name']))){
114         $goFonMacroParameter[$key]['name']="\${ARG".$key."}";
115       }
116     }  
118     foreach($goFonMacroParameter as $key => $val){
119       /* All attributes with check == false, are unneeded so mark them with ['check']= false */
120       if(!isset($goFonMacroParameter[$key]['check'])){
121         $goFonMacroParameter[$key]['check']= false;
122       }
123       /* Ah no default given assign ="" to prevent unsigned index  */
124       if(!isset($goFonMacroParameter[$key]['default'])){
125         $goFonMacroParameter[$key]['default'] = "";
126       }
127     }
129     /* Sort output for better reading */
130     asort($goFonMacroParameter);
131     return($goFonMacroParameter);
133   }
136   function execute()
137   {
138     /* Variables */
139     $vars       = "";
140     $tmp        = array();
141     $number = 0; 
143     $content = $_SESSION['macroManagment']->macrotabs->by_object['macro']->goFonMacroContent;
145     if(strstr($content,"ARG")){
146       $vorpos = strpos($content,"ARG");
147       $rest   = substr($content,$vorpos, strlen($content));
148     }    
150     /* Do we represent a valid group? */
151     if (!$this->is_account && $this->parent == NULL){
152       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
153         _("This 'dn' is no phone macro.")."</b>";
154       return ($display);
155     }
157     /* Fill templating stuff */
158     $smarty= get_smarty();
160     /* Assign all vars to Smarty */
161     foreach($this->attributes as $ar){
162       $smarty->assign($ar, $this->$ar);
163     }
165     /* Add an empty Parameter */
166     if(isset($_POST['addvar'])){
167       if(!is_array($this->goFonMacroParameter)){
168         $vars = $this->goFonMacroParameter;
169         $this->goFonMacroParameter = array();
170         $this->goFonMacroParameter[]= $vars;
171       }
172       $number= count($this->goFonMacroParameter);
173       $number++;
174       $this->goFonMacroParameter[]=array("var"=>"var","name"=>"new","type"=>"string","default"=>"test");
175     }
177     /*generate Table which shows als parameters */
178     $FonParas = $this->check_paras($content,$this->goFonMacroParameter); 
180     /* Sort by Parameterid, and keep keys */    
181     ksort($FonParas);
183     foreach($FonParas as $key=>$para)   {
185       /* Select correct item of combobox */
186       if(isset($para['type'])){
187         list($sel1, $sel2, $sel3)= $this->type_shortcut[$para['type']];
188       }else{
189         list($sel1, $sel2, $sel3)= array("", "", "");
190       }
192       /* Assemble output table */
193       $vars .="<tr>
194         <input name=\"number".$key."\" value='".$key."' type='hidden'>
195         <td><input name='var".$key."' type='hidden'   value='".$para['var']."'>".$para['var']."</td>
196         <td><input size=\"45\" name='varname".$key."'  value='".$para['name']."'></td>
197         <td>
198         <select name='vartype".$key."'>
199         <option name='vartype".$key."' value='string' ".$sel1.">"._("String")."</option>
200         <option name='vartype".$key."' value='combo'   ".$sel2.">"._("Combobox")."</option>
201         <option name='vartype".$key."' value='bool'   ".$sel3.">"._("Bool")."</option>
202         </select>
203         </td>
204         <td><input size=\"45\" name='default".$key."'   value='".$para['default']."'></td>
205         <td>&nbsp;";
206       if($para['check']==false) {
207         $vars.="<input name='del".$key."' value='"._("Delete unused")."' type='submit'>";
208       }
210       $vars.=" </td></tr>";
211     }
213     /* Checkboxes */
214     $smarty->assign("base_select", $this->base);
215     $smarty->assign("vars", $vars);
217     /* Show main page */
218     return($smarty->fetch (get_template_path('parameter.tpl', TRUE)));
219   }
222   function remove_from_parent()
223   {
224   }
227   /* Save data to object */
228   function save_object()
229   {
230     if (isset($_POST['phoneparameters'])){
231       plugin::save_object();
232     }
233     /* read out post data, and assign it to the parameters */
234     /* And or delete */
235     foreach($_POST as $name=>$value){
237       /* Test if there is a variable begining with "del" */
238       if(preg_match("/del/",$name)){
240         /* Extract entry id to delete */
241         $nr = str_replace("del","",$name) ;
243         /* unset entry */
244         unset($this->goFonMacroParameter[$nr]);
246       }elseif(preg_match("/number/",$name)){
248         /* Set Post vars */
249         $key = $_POST[$name];
251         $this->goFonMacroParameter[$key]['var']   = $_POST["var".$key];
252         $this->goFonMacroParameter[$key]['name']   = $_POST["varname".$key];
253         $this->goFonMacroParameter[$key]['type']   = $_POST["vartype".$key];
254         $this->goFonMacroParameter[$key]['default']= $_POST["default".$key];
255       }
256     }
258   }
261   /* Check values */
262   function check()
263   {
264     $message = array();
266     foreach($this->attributes as $attr){
267       if(chkacl($this->acl,$attr)){
268         $str =  sprintf( _("Insufficient permissions, can't change attribute '%s' in goFonMacro."),$attr) ;
269         return(array($str));
270       }
271     }
274     foreach($this->goFonMacroParameter as $key=>$val){
275       if((strstr($val['default'],"!"))||(strstr($val['default'],"#"))) {
276         $message[] = sprintf(_("The parameter %s contains invalid char. '!,#' is used as delimiter"),$val['name']);
277       }
278       switch($val['type']){
279         case 'bool'   :   $possible = array("","0","1");
280                           if(!in_array($val['default'],$possible)) {
281                             $message[] = sprintf(_("The parameter %s has incorrect value for type bool."),$val['name']);
282                           };break;
283         case 'string' :
284         case 'combo'  : 
285         default : ;
287       }
288     }
289     return $message;
290   }
293   /* Save to LDAP */
294   function save()
295   {
296     /* Post checks */
298     plugin::save();
300     $this->attrs['goFonMacroParameter']=array();
302     foreach($this->goFonMacroParameter as $key=>$fonpara){
303       $this->attrs['goFonMacroParameter'][]=$key."!".$fonpara['name']."!".$fonpara['type']."!".$fonpara['default'];
304     }
306     unset($this->attrs['base']);
308     /* Write back to ldap */
309     $ldap= $this->config->get_ldap_link();
310     $ldap->cat($this->dn);
311     $a= $ldap->fetch();
313     if (count($a)){
314       $ldap->cd($this->dn);
315       $ldap->modify($this->attrs);
316       $this->handle_post_events("modify");
317     } else {
318       if(count($this->attrs['goFonMacroParameter']==0)){
319         unset($this->attrs['goFonMacroParameter']);
320       }         
321       $ldap->cd($this->dn);
322       $ldap->create_missing_trees( $this->dn);
323       $ldap->cd($this->dn);
324       $ldap->add($this->attrs);
325       $this->handle_post_events("add");
326     }
327     show_ldap_error($ldap->get_error());
328   }
331 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
332 ?>