Code

Layout improvements for generic view
[gosa.git] / plugins / gofon / macro / class_gofonMacroParameters.inc
1 <?php
2 class macroParameter extends plugin
3 {
4   /* CLI vars */
5   var $cli_summary= "Handling of GOsa's application object";
6   var $cli_description= "Some longer text\nfor help";
7   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
9   /* application attributes */
10   var $base= "";
11   var $goFonMacroParameter =array();
13   /* attribute list for save action */
14   var $attributes= array("base","goFonMacroParameter");
15   var $objectclasses= array("top", "goFonMacro");
17   function macroParameter ($config, $dn= NULL)
18   {
19     plugin::plugin ($config, $dn);
21     $tmp = array();  // temporary Var 
22     $tmp2 = array(); // temporary Var ...
23     $tmp3 = "";
24     $ldap= $config->get_ldap_link();
26     $this->dn = $dn;
28     /* This is always an account */
29     $this->is_account= TRUE;
31     /* Edit or new one ?*/
32     if ($this->dn == "new"){
33       $ui= get_userinfo();
34       $this->base= dn2base($ui->dn);
35     } else {
36       $this->base= dn2base($this->dn);
37     }
39     /* initialising macro parameter */
40     unset($this->attrs['goFonMacroParameter']['count']);
42     /* Set Parameters, or a new array if ther are no parameters */
43     if(isset($this->attrs['goFonMacroParameter'])){
44       $this->goFonMacroParameter = $this->attrs['goFonMacroParameter'];
45     }else{
46       $this->goFonMacroParameter =array();
47     }
49     /* Create an array for parameters if not given yet */
50     if(!is_array($this->goFonMacroParameter)){
51       $tmp3 = $this->goFonMacroParameter;
52       $this->goFonMacroParameter =array();      
53       if(!empty($tmp3)) {
54         $this->goFonMacroParameter[]  = $tmp3;
55       }
56     }
57     
58     foreach($this->goFonMacroParameter as $para){
60       $tmp = split("!",$para);
61       $num = preg_replace("/[^0-9]/","",$tmp[1]);
62       $tmp2[$num]['name']        = $tmp[1];
63       $tmp2[$num]['type']        = $tmp[2];
64       $tmp2[$num]['default']     = $tmp[3];
65     }
66   
67     /* Assign this array */
68     $this->goFonMacroParameter = $tmp2;
69   }
71 function check_paras($content,$goFonMacroParameter)
72   {
73   preg_match_all("/[$]\{ARG[0-9]*\}/",$content,$res,PREG_OFFSET_CAPTURE);
75   $anz = count($res[0]);
76   $new = array();
78   foreach($res[0] as $val){
79     $num = preg_replace("/[^0-9]/","",$val[0]); 
80     $new[$num]['val'] = $val[0];
81     $new[$num]['num'] = $num;
82     }
83   
84   foreach($goFonMacroParameter as $gokey => $goval){
85     foreach($new as $nkey => $nval){
86       if($gokey == $nval['num']){
87         $goFonMacroParameter[$gokey]['check']= true;
88         }
89       }
90     }
91   foreach($new as $key => $val){
92     $goFonMacroParameter[$key]['check']= true;
93     if((!isset($goFonMacroParameter[$key]['name']))||(empty($goFonMacroParameter[$key]['name']))){
94       $goFonMacroParameter[$key]['name']="\${ARG".$key."}";
95     }
96   }  
98   foreach($goFonMacroParameter as $key => $val){
99     if(!isset($goFonMacroParameter[$key]['check'])){
100       $goFonMacroParameter[$key]['check']= false;
101       }
102     if(!isset($goFonMacroParameter[$key]['default'])){
103       $goFonMacroParameter[$key]['default'] = "";
104       }
105     }
106   asort($goFonMacroParameter);
107   return($goFonMacroParameter);
109   }
118   function execute()
119   {
120     /* Variables */
121     $vars       = "";
122     $tmp        = array();
123     $number = 0; 
125     $content = $_SESSION['macroManagment']->macrotabs->by_object['macro']->goFonMacroContent;
127     if(strstr($content,"ARG")){
128        $vorpos = strpos($content,"ARG");
129        $rest   = substr($content,$vorpos, strlen($content));
130     }    
132     /* Do we represent a valid group? */
133     if (!$this->is_account && $this->parent == NULL){
134       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
135         _("This 'dn' is no application.")."</b>";
136       return ($display);
137     }
139     /* Fill templating stuff */
140     $smarty= get_smarty();
142     /* Assign all vars to Smarty */
143     foreach($this->attributes as $ar){
144       $smarty->assign($ar, $this->$ar);
145     }
147     /* Add an empty Parameter */
148     if(isset($_POST['addvar'])){
149       if(!is_array($this->goFonMacroParameter)){
150         $vars = $this->goFonMacroParameter;
151         $this->goFonMacroParameter = array();
152         $this->goFonMacroParameter[]= $vars;
153       }
154       $number= count($this->goFonMacroParameter);
155       $number++;
156       $this->goFonMacroParameter[]=array("name"=>"new","type"=>"string","default"=>"test");
157     }
159     /* read out post data, and assign it to the parameters */
160     /* And or delete */
161     foreach($_POST as $name=>$value){   
163       /* Test if there is a variable begining with "del" */
164       if(preg_match("/del/",$name)){
166         /* Extract entry id to delete */
167         $nr = str_replace("del","",$name)       ;
169         /* unset entry */                       
170         unset($this->goFonMacroParameter[$nr]);
172       }elseif(preg_match("/number/",$name)){
173         /* Set Post vars */
174         $key = $_POST[$name];
175         $this->goFonMacroParameter[$key]['name']                =$_POST["varname".$key];                
176         $this->goFonMacroParameter[$key]['type']                =$_POST["vartype".$key];                
177         $this->goFonMacroParameter[$key]['default']     =$_POST["default".$key];                
178       }
179     } 
181     /*generate Table which shows als parameters */
182    
183      $FonParas = $this->check_paras($content,$this->goFonMacroParameter); 
184     
185     foreach($FonParas as $key=>$para)   {
187     if(isset($para['type'])){
188       if($para['type']=="string") {
189         $sel1 = "selected";
190         $sel2 = "";
191         $sel3 = "";
192       }elseif($para['type']=="combo"){
193         $sel1 = "";
194         $sel2 = "selected";
195         $sel3 = "" ;
196       }else{
197         $sel1 = "";
198         $sel3 = "selected";
199         $sel2 = "" ;
200       }
201     }else{
202         $sel1 = "";
203         $sel3 = "";
204         $sel2 = "" ;
205     }
207       $vars .="<tr>
208         <td><input name=\"number".$key."\" value='".$key."' type='hidden'> </td>
209         <td><input name='varname".$key."' type='hidden'   value='".$para['name']."'>".$para['name']."</td>
210         <td>
211         <select name='vartype".$key."'>
212         <option name='vartype".$key."' value='string' ".$sel1.">string</option>
213         <option name='vartype".$key."' value='combo'   ".$sel2.">combo</option>
214         <option name='vartype".$key."' value='bool'   ".$sel3.">bool</option>
215         </select>
216         </td>
217         <td><input name='default".$key."'   value='".$para['default']."'></td>
218         <td>";
219       if($para['check']==true) {
220         $vars.=_("Ok");
221         $vars.=" <td></td>";
222       }else{
223         $vars.="<b>"._("Unused")."</b>";
224         $vars.=" <td><input name='del".$key."' value='delete' type='submit'></td>";
225       }
226         
227       $vars.=" </tr>";
230     }
232     /* Checkboxes */
233     $smarty->assign("base_select", $this->base);
234     $smarty->assign("vars", $vars);
237     /* Show main page */
238     return($smarty->fetch (get_template_path('parameter.tpl', TRUE)));
239   }
241   function remove_from_parent()
242   {
243     /*
244     $ldap= $this->config->get_ldap_link();
245     $ldap->rmDir($this->dn);
247     /* Optionally execute a command after we're done * /
248     $this->handle_post_events("remove");
250     /* Delete references to object groups * /
251     $ldap->cd ($this->config->current['BASE']);
252     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
253     while ($ldap->fetch()){
254         $og= new ogroup($this->config, $ldap->getDN());
255         unset($og->member[$this->dn]);
256         $og->save ();
257     }
258   */
259   }
262   /* Save data to object */
263   function save_object()
264   {
265     if (isset($_POST['cn'])){
266       plugin::save_object();
267     }
268   }
271   /* Check values */
272   function check()
273   {
274     $message = array();
275     
276     foreach($this->attributes as $attr){
277       if(chkacl($this->acl,$attr)){
278         $str =  sprintf( _("Insufficient permissions, can't change attribute '%s' in goFonMacro"),$attr) ;
279         return(array($str));
280         }
281       }
282     return $message;
283   }
286   /* Save to LDAP */
287   function save()
288   {
289     /* Post checks */
290     $this->execute();
292     plugin::save();
294     $this->attrs['goFonMacroParameter']=array();
295   
296     foreach($this->goFonMacroParameter as $key=>$fonpara){
297       $this->attrs['goFonMacroParameter'][]=$key."!".$fonpara['name']."!".$fonpara['type']."!".$fonpara['default'];
298     }
300     unset($this->attrs['base']);
302     /* Write back to ldap */
303     $ldap= $this->config->get_ldap_link();
304     $ldap->cat($this->dn);
305     $a= $ldap->fetch();
307     if (count($a)){
308       $ldap->cd($this->dn);
309       $ldap->modify($this->attrs);
310       $this->handle_post_events("modify");
311     } else {
312       if(count($this->attrs['goFonMacroParameter']==0)){
313         unset($this->attrs['goFonMacroParameter']);
314       }         
315       $ldap->cd($this->dn);
316       $ldap->create_missing_trees( $this->dn);
317       $ldap->cd($this->dn);
318       $ldap->add($this->attrs);
319       $this->handle_post_events("add");
320     }
321     show_ldap_error($ldap->get_error());
322   }
325 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
326 ?>