Code

Renamed class
[gosa.git] / plugins / gofon / macro / class_gofonMacro.inc
1 <?php
2 class macro 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 $cn= "";
11         var $description= "";
12         var $base= "";
13         var $displayName = "";
14         var $goFonMacroParameter =array();
15         var $goFonMacroContent;
16         var $goFonMacroVisible=0;
17         
18         /* attribute list for save action */
19         var $attributes= array("cn","base", "description","displayName","goFonMacroParameter","goFonMacroContent","goFonMacroVisible");
20         var $objectclasses= array("top", "goFonMacro");
22         function macro ($config, $dn= NULL)
23         {
24                 plugin::plugin ($config, $dn);
26                 $tmp = array();  // temporary Var 
27                 $tmp2 = array(); // temporary Var ...
28                 $tmp3 = "";
29                 $ldap= $config->get_ldap_link();
31                 $this->dn = $dn;
33                 /* This is always an account */
34                 $this->is_account= TRUE;
36                 /* Edit or new one ?*/
37                 if ($this->dn == "new"){
38                         $ui= get_userinfo();
39                         $this->base= dn2base($ui->dn);
40                 } else {
41                         $this->base= dn2base($this->dn);
42                 }
44                 /* initialising macro parameter */
45                 unset($this->attrs['goFonMacroParameter']['count']);
47                 /* Set Parameters, or a new array if ther are no parameters */
48                 if(isset($this->attrs['goFonMacroParameter'])){
49                         $this->goFonMacroParameter = $this->attrs['goFonMacroParameter'];
50                 }else{
51                         $this->goFonMacroParameter =array();
52                 }
54                 /* Create an array for parameters if not given yet */
55                 if(!is_array($this->goFonMacroParameter)){
56                         $tmp3 = $this->goFonMacroParameter;
57                         $this->goFonMacroParameter =array();    
58                         if(!empty($tmp3)) {
59                                 $this->goFonMacroParameter[] = $tmp3;
60                         }
61                 }
63                 /* Create a better sorted array */
64                 foreach($this->goFonMacroParameter as $para)    {
65                         $tmp = split("!",$para);
66                         $tmp2[$tmp[0]]['name']          = $tmp[1];  
67                         $tmp2[$tmp[0]]['type']          = $tmp[2];  
68                         $tmp2[$tmp[0]]['default']       = $tmp[3];  
69                 }
71                 /* Assign this array */
72                 $this->goFonMacroParameter = $tmp2;     
74         }
76         function execute()
77         {
78                 /* Variables */
79                 $vars   = "";
80                 $tmp    = array();
81                 $number = 0; 
83                 /* Do we represent a valid group? */
84                 if (!$this->is_account && $this->parent == NULL){
85                         $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
86                                 _("This 'dn' is no application.")."</b>";
87                         return ($display);
88                 }
90                 /* Fill templating stuff */
91                 $smarty= get_smarty();
92                 $smarty->assign("bases", $this->config->idepartments);
94                 /* Assign all vars to Smarty */
95                 foreach($this->attributes as $ar){
96                         $smarty->assign($ar, $this->$ar);
97                 }
99                 /* Add an empty Parameter */
100                 if(isset($_POST['addvar'])){
101                         if(!is_array($this->goFonMacroParameter)){
102                                 $vars = $this->goFonMacroParameter;
103                                 $this->goFonMacroParameter = array();
104                                 $this->goFonMacroParameter[]= $vars;
105                         }
106                         $number= count($this->goFonMacroParameter);
107                         $number++;
108                         $this->goFonMacroParameter[]=array("name"=>"new","type"=>"string","default"=>"test");
109                 }
111                 /* read out post data, and assign it to the parameters */
112                 /* And or delete */
113                 foreach($_POST as $name=>$value){       
115                         /* Test if there is a variable begining with "del" */
116                         if(preg_match("/del/",$name)){
118                                 /* Extract entry id to delete */
119                                 $nr = str_replace("del","",$name)       ;
121                                 /* unset entry */                       
122                                 unset($this->goFonMacroParameter[$nr]);
124                         }elseif(preg_match("/number/",$name)){
125                                 /* Set Post vars */
126                                 $key = $_POST[$name];
127                                 $this->goFonMacroParameter[$key]['name']                =$_POST["varname".$key];                
128                                 $this->goFonMacroParameter[$key]['type']                =$_POST["vartype".$key];                
129                                 $this->goFonMacroParameter[$key]['default']             =$_POST["default".$key];                
130                         }
131                 } 
133                 /*generate Table which shows als parameters */
134                 foreach($this->goFonMacroParameter as $key=>$para)      {
136                         if($para['type']=="string") {
137                                 $sel1 = "selected";
138                                 $sel2 = "";
139                         }else{
140                                 $sel1 = "";
141                                 $sel2 = "selected";
142                         }
144                         $vars .="<tr>
145                                 <td>".$key."<input name=\"number".$key."\" value='".$key."' type='hidden'> </td>
146                                 <td><input name='varname".$key."'   value='".$para['name']."'></td>
147                                 <td>
148                                 <select name='vartype".$key."'>
149                                 <option name='vartype".$key."' value='string' ".$sel1.">string</option>
150                                 <option name='vartype".$key."' value='bool'   ".$sel2.">bool</option>
151                                 </select>
152                                 </td>
153                                 <td><input name='default".$key."'   value='".$para['default']."'></td>
154                                 <td><input name='del".$key."' value='delete' type='submit'></td>
155                                 </tr>";
158                 }
160                 /* Display add renew button */
161                 $vars .="<tr>   
162                         <td colspan=5>
163                         <input name='addvar' value="._("Add")." type='submit'>
164                         <input name='actvar' value="._("Refresh")." type='submit'>
165                         </td>
166                         </tr>";
167                 /* Checkboxes */
168                 $smarty->assign("base_select", $this->base);
169                 $smarty->assign("vars", $vars);
171                 if($this->goFonMacroVisible) {
172                         $smarty->assign("goFonMacroVisibleChecked"," checked ");
173                 }else  {
174                         $smarty->assign("goFonMacroVisibleChecked","");
175                 }
178                 /* Show main page */
179                 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
180         }
182         function remove_from_parent()
183         {
184                 $ldap= $this->config->get_ldap_link();
185                 $ldap->rmDir($this->dn);
187                 /* Optionally execute a command after we're done */
188                 $this->handle_post_events("remove");
190                 /* Delete references to object groups */
191                 $ldap->cd ($this->config->current['BASE']);
192                 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
193                 while ($ldap->fetch()){
194                         $og= new ogroup($this->config, $ldap->getDN());
195                         unset($og->member[$this->dn]);
196                         $og->save ();
197                 }
199         }
202         /* Save data to object */
203         function save_object()
204         {
205                 if (isset($_POST['cn'])){
206                         plugin::save_object();
208                 }
209         }
212         /* Check values */
213         function check()
214         {
215                 $message=array();
216                 return $message;
217         }
220         /* Save to LDAP */
221         function save()
222         {
223                 /* Post checks */
224                 $this->execute();
226                 plugin::save();
228                 $this->attrs['goFonMacroParameter']=array();
230                 foreach($this->goFonMacroParameter as $key=>$fonpara){
231                         $this->attrs['goFonMacroParameter'][]=$key."!".$fonpara['name']."!".$fonpara['type']."!".$fonpara['default'];
232                 }
234                 unset($this->attrs['base']);
236                 /* Write back to ldap */
237                 $ldap= $this->config->get_ldap_link();
238                 $ldap->cat($this->dn);
239                 $a= $ldap->fetch();
241                 if (count($a)){
242                         $ldap->cd($this->dn);
243                         $ldap->modify($this->attrs);
244                         $this->handle_post_events("modify");
245                 } else {
246                         $ldap->cd($this->config->current['BASE']);
247                         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
248                         $ldap->cd($this->dn);
249                         $ldap->add($this->attrs);
250                         $this->handle_post_events("add");
251                 }
252                 show_ldap_error($ldap->get_error());
253         }
257 ?>