Code

4e35980d2b9046bf37282c0a466fc11e6c6a0bd4
[gosa.git] / plugins / admin / groups / class_groupApplication.inc
1 <?php
2 class appgroup extends plugin
3 {
4   /* CLI vars */
5   var $cli_summary= "Manage application groups";
6   var $cli_description= "Some longer text\nfor help";
7   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
9   /* Appgroup attributes */
10   var $gosaMemberApplication= array();
12   /* Helpers */
13   var $department= "";
14   var $apps= array();
15   var $used_apps= array();
16   var $opt_edit= FALSE;
17   var $option_name= array();
18   var $option_value= array();
19   var $appoption= array();
20   var $table= "";
21   
22   var $curbase   = "";
24   /* attribute list for save action */
25   var $attributes= array();
26   var $objectclasses= array("gosaApplicationGroup");
28   function appgroup ($config, $dn= NULL)
29   {
30     plugin::plugin ($config, $dn);
32     /* Load member applications */
33     if (isset ($this->attrs["gosaMemberApplication"][0])){
34       for ($i= 0; $i<$this->attrs["gosaMemberApplication"]["count"]; $i++){
35         $this->gosaMemberApplication[]=
36           $this->attrs["gosaMemberApplication"][$i];
37       }
38     }
40     /* Load application options */
41     if (isset($this->attrs['gosaApplicationParameter'])){
42       for ($i= 0; $i<$this->attrs['gosaApplicationParameter']['count']; $i++){
43         $option= preg_replace('/^[^:]+:/', '',
44             $this->attrs['gosaApplicationParameter'][$i]);
45         $name= preg_replace('/:.*$/', '',
46             $this->attrs['gosaApplicationParameter'][$i]);
47         $this->appoption[$name]= $option;
48       }
49     }
50     $this->curbase = $this->config->current['BASE'];
51   }
53   function execute()
54   {
55           /* Call parent execute */
56         plugin::execute();
57     if((isset($_GET['act']))&&($_GET['act']=="depopen")){
58       $dep = base64_decode($_GET['depid']);  
59       if(isset($this->config->idepartments[$dep])){
60         $this->curbase = $dep;
61       }
62     }
64     /* Do we need to flip is_account state? */
65     if (isset($_POST['modify_state'])){
66       $this->is_account= !$this->is_account;
67     }
69     /* Do we represent a valid group? */
70     if (!$this->is_account && $this->parent == NULL){
71       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
72         _("This 'dn' is no appgroup.")."</b>";
73       return ($display);
74     }
76     /* Show tab dialog headers */
77     $display= "";
78     if ($this->parent != NULL){
79       if ($this->is_account){
80         $display= $this->show_header(_("Remove applications"),
81             _("This group has application features enabled. You can disable them by clicking below."));
82       } else {
83         $display.= $this->show_header(_("Create applications"),
84             _("This group has application features disabled. You can enable them by clicking below."));
85         return ($display);
86       }
87     }
89     /* Check sorting variable */
90     $this->reload();
92     /* Delete app from group */
93     if (isset($_POST['del_app']) && isset ($_POST['used_apps'])){
94       foreach ($_POST['used_apps'] as $value){
95         unset ($this->used_apps["$value"]);
96         $this->removeApp($value);
97       }
98     }
100     /* Add app to group */
101     if (isset($_POST['add_app']) && isset($_POST['apps'])){
102       foreach ($_POST['apps'] as $value){
103         $this->used_apps["$value"]= $this->apps[$value];
104         asort($this->used_apps);
105         $this->addApp($value);
106       }
107     }
109     /* Add group with post */
110     if((isset($_GET['act']))&&($_GET['act']=="add")){
111       $this->used_apps[$_GET['id']]= $_GET['id'];
112       asort($this->used_apps);
113       $this->addApp($_GET['id']);
114     }
116     /* Edit application options? */
117     if (isset($_POST['edit_options']) && isset($_POST['used_apps'])){
118       $appname= $_POST['used_apps'][0];
120       /* We've got the appname, get parameters from ldap */
121       $ldap= $this->config->get_ldap_link();
122       $ldap->cd($this->config->current['BASE']);
123       $ldap->search("(&(objectClass=gosaApplication)(cn=$appname))");
124       if ($ldap->count() != 1){
125         print_red (_("The selected application name is not uniq. Please check your LDAP."));
126       } else {
127         $attrs= $ldap->fetch();
128         if(isset($attrs['gosaApplicationParameter'])){
129           $this->dialog= TRUE;
131           /* Fill name and value arrays */
132           for ($i= 0; $i<$attrs['gosaApplicationParameter']['count']; $i++){
133             $option= preg_replace('/^[^:]+:/', '',
134                 $attrs['gosaApplicationParameter'][$i]);
135             $name= preg_replace('/:.*$/', '', 
136                 $attrs['gosaApplicationParameter'][$i]);
137             $this->option_name[$i]= $name;
139             /* Fill with values from application, default should be
140                loaded by the external scripts */
141             if (isset($this->appoption[$name])){
142               $this->option_value[$i]= $this->appoption[$name];
143             }
144           }
146           /* Create edit field */
147           $table= "<table summary=\"\">";
148           for ($i= 0; $i < count($this->option_name); $i++){
149             if (isset($this->option_value[$i])){
150               $value= $this->option_value[$i];
151             } else {
152               $value= "";
153             }
154             $table.="<tr><td>".$this->option_name[$i]."</td><td>".
155               "<input name=\"value$i\" size=60 maxlength=250 ".
156               "value=\"".$value."\"><br></td></tr>";
157           }
158           $table.= "</table>";
159           $this->table= $table;
160         } else {
161           print_red (_("The selected application has no options."));
162         }
163       }
164     }
166     /* Cancel edit options? */
167     if (isset($_POST['edit_options_cancel'])){
168       $this->dialog= FALSE;
169     }
171     /* Finish edit options? */
172     if (isset($_POST['edit_options_finish'])){
173       $this->dialog= FALSE;
175       /* Save informations passed by the user */
176       $this->option_value= array();
177       for ($i= 0; $i<count($this->option_name); $i++){
178         $this->appoption[$this->option_name[$i]]= $_POST["value$i"];
179         $this->is_modified= TRUE;
180       }
181     }
183     /* Prepare templating stuff */
184     $smarty= get_smarty();
185     $smarty->assign("used_apps", $this->used_apps);
186     $apps= array();
187     foreach ($this->apps as $key => $value){
188       if (!array_key_exists($key, $this->used_apps)){
189         $apps["$key"]= "$value";
190       }
191     }
193     $div = new DivSelectBox("appgroup");    
195     /* NEW LIST MANAGMENT
196      * We also need to search for the departments
197      * So we are able to navigate like in konquerer
198      */
200     $ldap = $this->config->get_ldap_link();
201     $ldap->cd($this->curbase) ;
202     $ldap->ls("(objectClass=gosaDepartment)"); 
203     $departments= array();
204     $tmp = array();
205     while ($value = $ldap->fetch()){
206       $tmp[strtolower($value['dn']).$value['dn']]=$value;
207     }
208     ksort($tmp);
209     foreach($tmp as $value){
210       if($value["description"][0]!=".."){
211         $departments[$value['dn']]=convert_department_dn($value['dn'])." - [".$value["description"][0]."]";
212       }else{
213         $departments[$value['dn']]=convert_department_dn($value['dn']);
214       }
215     }
216     
217     /* END NEW LIST MANAGMENT
218      */
220     $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=depopen&amp;depid=%s'>%s</a>";
221     $linkadd  = "<a href='?plug=".$_GET['plug']."&amp;act=add&amp;id=%s'>%s</a>";
223     $base_back = preg_replace("/^[^,]+,/","",$this->curbase);
224     if((strlen($base_back)>= strlen($this->config->current['BASE']))&&($this->curbase!=$this->config->current['BASE'])){
225       $div->AddEntry(array(
226             array("string"=>sprintf($linkopen,base64_encode($base_back),".. ["._("back")."]"),
227                   "attach"=>"style='border:0px;'")
228             ));
229     }
230     foreach($departments as $key => $app){
231       $div->AddEntry(array(
232                             array("string"=>"<img src='images/folder.png' alt='"._("department")."'>&nbsp;".sprintf($linkopen,base64_encode($key),$app),
233                                   "attach"=>"style='border:0px;'")
234                           ));
235     }
236     
237     foreach($apps as $key => $app){
238       $div->AddEntry(array(
239                             array("string"=>"<img src='images/select_application.png' alt='"._("application")."'>&nbsp;".sprintf($linkadd,$key,$app),
240                                   "attach"=>"style='border:0px;'")
241                           ));
242     }
244     $smarty->assign("List", $div->DrawList());
245     $smarty->assign("apps", $apps);
247     /* Show main page */
248     if ($this->dialog){
249       $smarty->assign("table", $this->table);
250       $display.= $smarty->fetch (get_template_path('application_options.tpl', TRUE));
251     } else {
252       $display.= $smarty->fetch (get_template_path('application.tpl', TRUE));
253     }
255     return ($display);
256   }
259   function remove_from_parent()
260   {
261     plugin::remove_from_parent();
263     $this->attrs["gosaMemberApplication"]= array();
265     $ldap= $this->config->get_ldap_link();
266     $ldap->cd($this->dn);
267     $ldap->modify($this->attrs);
268     show_ldap_error($ldap->get_error());
270     /* Optionally execute a command after we're done */
271     $this->handle_post_events("remove");
272   }
275   /* Save data to object */
276 #  function save_object()
277 #  {
278 #    plugin::save_object();
279 #  }
282   /* Save to LDAP */
283   function save()
284   {
285     plugin::save();
287     /* Copy members */
288     $this->attrs["gosaMemberApplication"]= array();
289     foreach ($this->gosaMemberApplication as $val){
290       $this->attrs["gosaMemberApplication"][]= stripslashes($val);
291     }
293     /* Are there application parameters to be saved */
294     $this->attrs['gosaApplicationParameter']= array();
295     foreach($this->appoption as $name => $value){
296       if ($value != ""){
297         $this->attrs['gosaApplicationParameter'][]= "$name:$value";
298       }
299     }
301     /* Write back to LDAP */
302     $ldap= $this->config->get_ldap_link();
303     $ldap->cd($this->dn);
304     $ldap->modify($this->attrs);
305     show_ldap_error($ldap->get_error());
307     /* Optionally execute a command after we're done */
308     if ($this->initially_was_account == $this->is_account){
309       if ($this->is_modified){
310         $this->handle_post_events("mofify");
311       }
312     } else {
313       $this->handle_post_events("add");
314     }
316   }
318   function check()
319   {
320     $message= array();
321     return ($message);
322   }
325   function reload()
326   {
327     /* Generate applist */
328     $this->apps= array();
329     $ldap= $this->config->get_ldap_link();
330     $ldap->cd ("ou=apps,".$this->curbase);
332     $ldap->ls ("(objectClass=gosaApplication)","ou=apps,".$this->curbase);
333     while ($attrs= $ldap->fetch()){
334       if (isset($attrs["description"][0])){    
335         $this->apps[$attrs["cn"][0]]=
336           $attrs["cn"][0]." (".
337           $attrs["description"][0].")";
338       } else {
339         $this->apps[$attrs["cn"][0]]=
340           $attrs["cn"][0];
341       }
342     }
343     natcasesort ($this->apps);
344     reset ($this->apps);
346     foreach ($this->gosaMemberApplication as $value){
347       $this->used_apps[$value]= $value;
348     }
349   }
351   function addApp($cn)
352   {
353     $this->gosaMemberApplication[]= $cn;
354     $this->gosaMemberApplication= array_unique($this->gosaMemberApplication);
355     $this->is_modified= TRUE;
356   }
359   function removeApp($cn)
360   {
361     $temp= array();
362     foreach ($this->gosaMemberApplication as $value){
363       if ($value != $cn){
364         $temp[]= $value;
365       }
366     }
367     $this->gosaMemberApplication= $temp;
368     $this->is_modified= TRUE;
369   }
373 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
374 ?>