Code

some checks
[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;
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='combo'   ".$sel2.">combo</option>
151         <option name='vartype".$key."' value='bool'   ".$sel2.">bool</option>
152         </select>
153         </td>
154         <td><input name='default".$key."'   value='".$para['default']."'></td>
155         <td><input name='del".$key."' value='delete' type='submit'></td>
156         </tr>";
159     }
161     /* Display add renew button */
162     $vars .="<tr>       
163       <td colspan=5>
164       <input name='addvar' value="._("Add")." type='submit'>
165       <input name='actvar' value="._("Refresh")." type='submit'>
166       </td>
167       </tr>";
168     /* Checkboxes */
169     $smarty->assign("base_select", $this->base);
170     $smarty->assign("vars", $vars);
172     if($this->goFonMacroVisible){
173       $smarty->assign("goFonMacroVisibleChecked"," checked ");
174     }else{
175       $smarty->assign("goFonMacroVisibleChecked","");
176     }
178     if(isset($_POST['goFonMacroVisible'])) {
179       $this->goFonMacroVisible= 1 ;
180       $smarty->assign("goFonMacroVisibleChecked"," checked ");
181     }else  {
182       if(isset($_POST['cn'])){
183         $this->goFonMacroVisible= 0 ;
184         $smarty->assign("goFonMacroVisibleChecked","");
185       }
186     }
189     /* Show main page */
190     return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
191   }
193   function remove_from_parent()
194   {
195     $ldap= $this->config->get_ldap_link();
196     $ldap->rmDir($this->dn);
198     /* Optionally execute a command after we're done */
199     $this->handle_post_events("remove");
201     /* Delete references to object groups */
202     $ldap->cd ($this->config->current['BASE']);
203     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
204     while ($ldap->fetch()){
205       $og= new ogroup($this->config, $ldap->getDN());
206       unset($og->member[$this->dn]);
207       $og->save ();
208     }
210   }
213   /* Save data to object */
214   function save_object()
215   {
216     if (isset($_POST['cn'])){
217       plugin::save_object();
219     }
220   }
223   /* Check values */
224   function check()
225   {
226     $message = array();
227     
228     foreach($this->attributes as $attr){
229       if(chkacl($this->acl,$attr)){
230         $str =  sprintf( _("Insufficient permissions, can't change attribute '%s' in goFonMacro"),$attr) ;
231         return(array($str));
232         }
233       }
235     if(($this->cn != strtolower($this->cn))||(empty($this->cn))){
236       $message[] = "The attribute 'cn' must be lowercase and a least one char.";
237     }
238     return $message;
239   }
242   /* Save to LDAP */
243   function save()
244   {
245     /* Post checks */
246     $this->execute();
248     plugin::save();
250     $this->attrs['goFonMacroParameter']=array();
252     foreach($this->goFonMacroParameter as $key=>$fonpara){
253       $this->attrs['goFonMacroParameter'][]=$key."!".$fonpara['name']."!".$fonpara['type']."!".$fonpara['default'];
254     }
256     unset($this->attrs['base']);
258     /* Write back to ldap */
259     $ldap= $this->config->get_ldap_link();
260     $ldap->cat($this->dn);
261     $a= $ldap->fetch();
263     if (count($a)){
264       $ldap->cd($this->dn);
265       $ldap->modify($this->attrs);
266       $this->handle_post_events("modify");
267     } else {
268       if(count($this->attrs['goFonMacroParameter']==0)){
269         unset($this->attrs['goFonMacroParameter']);
270       }         
272       $ldap->cd($this->dn);
273       $ldap->create_missing_trees( $this->dn);
274       $ldap->cd($this->dn);
275       $ldap->add($this->attrs);
276       $this->handle_post_events("add");
277     }
278     show_ldap_error($ldap->get_error());
279   }
282 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
283 ?>