Code

a73092b23cc91a059a78f6d21eb2886e9c5eb255
[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   = "";
25   var $curCatDir;
26   var $curCatDepth=0;
27   var $Categories=array();
30   /* attribute list for save action */
31   var $attributes= array();
32   var $objectclasses= array("gosaApplicationGroup");
34   function appgroup ($config, $dn= NULL)
35   {
36     plugin::plugin ($config, $dn);
38     /* Load member applications */
39     if (isset ($this->attrs["gosaMemberApplication"][0])){
40       for ($i= 0; $i<$this->attrs["gosaMemberApplication"]["count"]; $i++){
41         $this->gosaMemberApplication[]=
42           $this->attrs["gosaMemberApplication"][$i];
43       }
44     }
46     /* Load application options */
47     if (isset($this->attrs['gosaApplicationParameter'])){
48       for ($i= 0; $i<$this->attrs['gosaApplicationParameter']['count']; $i++){
49         $option= preg_replace('/^[^:]+:/', '',
50             $this->attrs['gosaApplicationParameter'][$i]);
51         $name= preg_replace('/:.*$/', '',
52             $this->attrs['gosaApplicationParameter'][$i]);
53         $this->appoption[$name]= $option;
54       }
55     }
57     $tmp = array();
58     $tmp2 = array();
59     foreach($this->gosaMemberApplication as $memberApp){
60       if(preg_match("/\|/i",$memberApp)){
61         $tmp = split("\|",$memberApp);
62         $tmp2[$tmp[0]."|".$tmp[1]]['App'] = $tmp[0];
63         $tmp2[$tmp[0]."|".$tmp[1]]['Cat'] = $tmp[1];
64         $tmp2[$tmp[0]."|".$tmp[1]]['Pri'] = $tmp[2];
65       }else{
66         $tmp2[$memberApp."|"]['App'] = $memberApp;
67         $tmp2[$memberApp."|"]['Cat'] = "";
68         $tmp2[$memberApp."|"]['Pri'] = 100;
69       }
70     }
71     $this->gosaMemberApplication = $tmp2;
72   
73     $this->curbase = $this->config->current['BASE'];
74   }
76   function execute()
77   {
78           /* Call parent execute */
79         plugin::execute();
80     if((isset($_GET['act']))&&($_GET['act']=="depopen")){
81       $dep = base64_decode($_GET['depid']);  
82       if(isset($this->config->idepartments[$dep])){
83         $this->curbase = $dep;
84       }
85     }
88     if((isset($_GET['act']))&&($_GET['act']=="open")){
89       $this->curCatDir = $_GET['id'];
91       $found = false;
92       foreach($this->Categories as $key => $name ){
93         if($this->curCatDir==$name){
94           $tmp = array_flip(split("\/",$key));
96           $this->curCatDepth = ($tmp[$this->curCatDir]);
97           $found= true;
98         }
99       }
100       if(!$found){
101         $this->curCatDir = "";
102         $this->curCatDepth = 0;
103       }
104     }
106     /* Do we need to flip is_account state? */
107     if (isset($_POST['modify_state'])){
108       $this->is_account= !$this->is_account;
109     }
111     /* Do we represent a valid group? */
112     if (!$this->is_account && $this->parent == NULL){
113       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
114         _("This 'dn' is no appgroup.")."</b>";
115       return ($display);
116     }
118     /* Show tab dialog headers */
119     $display= "";
120     if ($this->parent != NULL){
121       if ($this->is_account){
122         $display= $this->show_header(_("Remove applications"),
123             _("This group has application features enabled. You can disable them by clicking below."));
124       } else {
125         $display.= $this->show_header(_("Create applications"),
126             _("This group has application features disabled. You can enable them by clicking below."));
127         return ($display);
128       }
129     }
131     $this->reload();
132     foreach($_POST as $name => $value){
133       if(preg_match("/DelApp_/",$name)){
134         $app = preg_replace("/DelApp_/","",$name); 
135         unset($this->used_apps[$app]);
136         unset($this->gosaMemberApplication[$app."|".$this->curCatDir]);
137       }
138       if(preg_match("/EdiApp_/",$name)){
139         $appname = $value;
140         /* We've got the appname, get parameters from ldap */
141         $ldap= $this->config->get_ldap_link();
142         $ldap->cd($this->config->current['BASE']);
143         $ldap->search("(&(objectClass=gosaApplication)(cn=$appname))");
144         if ($ldap->count() != 1){
145           print_red (_("The selected application name is not uniq. Please check your LDAP."));
146         } else {
147           $attrs= $ldap->fetch();
148           if(isset($attrs['gosaApplicationParameter'])){
149             $this->dialog= TRUE;
151             /* Fill name and value arrays */
152             for ($i= 0; $i<$attrs['gosaApplicationParameter']['count']; $i++){
153               $option= preg_replace('/^[^:]+:/', '',
154                   $attrs['gosaApplicationParameter'][$i]);
155               $name= preg_replace('/:.*$/', '', 
156                   $attrs['gosaApplicationParameter'][$i]);
157               $this->option_name[$i]= $name;
159               /* Fill with values from application, default should be
160                  loaded by the external scripts */
161               if (isset($this->appoption[$name])){
162                 $this->option_value[$i]= $this->appoption[$name];
163               }
164             }
166             /* Create edit field */
167             $table= "<table summary=\"\">";
168             for ($i= 0; $i < count($this->option_name); $i++){
169               if (isset($this->option_value[$i])){
170                 $value= $this->option_value[$i];
171               } else {
172                 $value= "";
173               }
174               $table.="<tr><td>".$this->option_name[$i]."</td><td>".
175                 "<input name=\"value$i\" size=60 maxlength=250 ".
176                 "value=\"".$value."\"><br></td></tr>";
177             }
178             $table.= "</table>";
179             $this->table= $table;
180           } else {
181             print_red (_("The selected application has no options."));
182           }
183         }
184       }
185     }
186     $this->reload();
188     /* Add group with post */
189     if((isset($_GET['act']))&&($_GET['act']=="add")){
190       $this->used_apps[$_GET['id']]= $_GET['id'];
191       asort($this->used_apps);
192       $this->addApp($_GET['id']);
193     }
195     /* Add multiple */
196     if(isset($_POST['AddApps'])){
197       foreach($_POST as $name => $value){
198         if(preg_match("/AddApp_/",$name)){
199           $app = preg_replace("/AddApp_/","",$name);
200           $this->addApp($app);
201         }
202       }
203     }
206     /* Cancel edit options? */
207     if (isset($_POST['edit_options_cancel'])){
208       $this->dialog= FALSE;
209     }
211     /* Finish edit options? */
212     if (isset($_POST['edit_options_finish'])){
213       $this->dialog= FALSE;
215       /* Save informations passed by the user */
216       $this->option_value= array();
217       for ($i= 0; $i<count($this->option_name); $i++){
218         $this->appoption[$this->option_name[$i]]= $_POST["value$i"];
219         $this->is_modified= TRUE;
220       }
221     }
223     /* Prepare templating stuff */
224     $smarty= get_smarty();
225     $smarty->assign("used_apps", $this->used_apps);
226     $apps= array();
227     foreach ($this->apps as $key => $value){
228       if (!array_key_exists($key, $this->used_apps)){
229         $apps["$key"]= "$value";
230       }
231     }
233     $div = new DivSelectBox("appgroup");    
235     $div->SetHeight(300);
237     /* NEW LIST MANAGMENT
238      * We also need to search for the departments
239      * So we are able to navigate like in konquerer
240      */
242     $ldap = $this->config->get_ldap_link();
243     $ldap->cd($this->curbase) ;
244     $ldap->ls("(objectClass=gosaDepartment)"); 
245     $departments= array();
246     $tmp = array();
247     while ($value = $ldap->fetch()){
248       $tmp[strtolower($value['dn']).$value['dn']]=$value;
249     }
250     ksort($tmp);
251     foreach($tmp as $value){
252       if($value["description"][0]!=".."){
253         $departments[$value['dn']]=convert_department_dn($value['dn'])." - [".$value["description"][0]."]";
254       }else{
255         $departments[$value['dn']]=convert_department_dn($value['dn']);
256       }
257     }
258     
259     /* END NEW LIST MANAGMENT
260      */
262     $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=depopen&amp;depid=%s'>%s</a>";
263     $linkadd  = "<a href='?plug=".$_GET['plug']."&amp;act=add&amp;id=%s'>%s</a>";
265     $base_back = preg_replace("/^[^,]+,/","",$this->curbase);
266     if((strlen($base_back)>= strlen($this->config->current['BASE']))&&($this->curbase!=$this->config->current['BASE'])){
267       $div->AddEntry(array(
268             array("string"=>sprintf($linkopen,base64_encode($base_back),".. ["._("back")."]"),
269                   "attach"=>"style='border:0px;'")
270             ));
271     }
272     foreach($departments as $key => $app){
273       $div->AddEntry(array(
274                             array("string"=>"<img src='images/folder.png' alt='"._("department")."'>&nbsp;".sprintf($linkopen,base64_encode($key),$app),
275                                   "attach"=>"style='border:0px;'")
276                           ));
277     }
278     
279     foreach($apps as $key => $app){
280       $div->AddEntry(array(
281                             array("string"=>sprintf("<input type='checkbox' value='1' name='AddApp_%s'>",$key).
282                                   "<img src='images/select_application.png' alt='"._("application")."'>&nbsp;".sprintf($linkadd,$key,$app),
283                                   "attach"=>"style='border:0px;'")
284                           ));
285     }
287     if((isset($_GET['act']))&&(($_GET['act'] == "one_up")||($_GET['act']=="one_down"))){
288       if(isset($_GET['id'])){
289         $id   = $_GET['id'];
290         $act  = $_GET['act']; 
291         $found = false;
292         foreach($this->gosaMemberApplication as $key =>  $member){
293           if($id == $member['App']){
294             $found = $key;
295           }
296         }
298         if($found != false){
299           if($act == "one_up"){
300             $this->gosaMemberApplication[$found]['Pri'] = $this->gosaMemberApplication[$found]['Pri'] -1;
301           }elseif($act == "one_down")   { 
302             $this->gosaMemberApplication[$found]['Pri'] = $this->gosaMemberApplication[$found]['Pri'] +1;
303           }
304         }
305       }
306     }
308     $div2 = new DivSelectBox("appgroup");
309     $div2->SetHeight(300);
311     $menu           = $this->CreateCatMenu();
312     $str_noprio     = "&nbsp; [ "._("Priority: %s")." ] ";
313     $linkopen       = "<img src='images/folder.png'>            &nbsp;<a href='?plug=".$_GET['plug']."&amp;act=open&amp;id=%s'>%s</a>";
314     $app   = "<img src='images/select_application.png'>&nbsp;%s".$str_noprio;
316         /* append back entry */
317     if($menu["__BACK__"] != false){
318         $div2 ->AddEntry(array(
319                     array("string"=>sprintf($linkopen,$menu["__BACK__"],".. [ "._("back")." ]")),
320                     array("string"=>"&nbsp;","attach"=>"style='border-right:0px;'")
321                     ));
322     }
324     foreach($menu['__CATEGORY__'] as $path => $name){
325       $div2 ->AddEntry(array(array("string"=>sprintf($linkopen,$path,$name)),array("string"=>"&nbsp;","attach"=>"style='border-right:0px;'"))); 
326     }
328       /* Append entries */
329   
330     $upudown = "<a href='?plug=".$_GET['plug']."&amp;act=one_up&id=%s'>   <img src='images/sort_up.png' border=0></a>".
331           "&nbsp;<a href='?plug=".$_GET['plug']."&amp;act=one_down&id=%s'> <img src='images/sort_down.png' border=0></a>".
332           "&nbsp;<input type='image' src='images/edittrash.png' name='DelApp_%s' value='%s'>".
333           "&nbsp;<input type='image' src='images/edit.png' name='EdiApp_%s' value='%s'>";
334  
335     foreach($menu["__ENTRY__"] as $path => $entry){
336       $div2 ->AddEntry(array(array("string"=>sprintf($app,$entry['name'],$entry['prio'])),
337                              array("string"=>sprintf($upudown,$entry['name'],$entry['name'],$entry['name'],$entry['name'],$entry['name'],$entry['name']),"attach"=>"style='border-right:0px;'")));
338     }
340     $smarty->assign("UsedApps", $div2->DrawList());
341     $smarty->assign("List", $div->DrawList());
342     $smarty->assign("apps", $apps);
344     /* Show main page */
345     if ($this->dialog){
346       $smarty->assign("table", $this->table);
347       $display.= $smarty->fetch (get_template_path('application_options.tpl', TRUE));
348     } else {
349       $display.= $smarty->fetch (get_template_path('application.tpl', TRUE));
350     }
351     return ($display);
352   }
355   function remove_from_parent()
356   {
357     plugin::remove_from_parent();
359     $this->attrs["gosaMemberApplication"]= array();
361     $ldap= $this->config->get_ldap_link();
362     $ldap->cd($this->dn);
363     $ldap->modify($this->attrs);
364     show_ldap_error($ldap->get_error());
366     /* Optionally execute a command after we're done */
367     $this->handle_post_events("remove");
368   }
371   /* Save data to object */
372 #  function save_object()
373 #  {
374 #    plugin::save_object();
375 #  }
378   /* Save to LDAP */
379   function save()
380   {
381     plugin::save();
383     /* Copy members */
384     $this->attrs["gosaMemberApplication"]= array();
385     foreach ($this->gosaMemberApplication as $val){
386       $this->attrs["gosaMemberApplication"][]= $val['App']."|".$val['Cat']."|".$val['Pri'];
387     }
389     /* Are there application parameters to be saved */
390     $this->attrs['gosaApplicationParameter']= array();
391     foreach($this->appoption as $name => $value){
392       if ($value != ""){
393         $this->attrs['gosaApplicationParameter'][]= "$name:$value";
394       }
395     }
397     /* Write back to LDAP */
398     $ldap= $this->config->get_ldap_link();
399     $ldap->cd($this->dn);
400     $ldap->modify($this->attrs);
401     show_ldap_error($ldap->get_error());
403     /* Optionally execute a command after we're done */
404     if ($this->initially_was_account == $this->is_account){
405       if ($this->is_modified){
406         $this->handle_post_events("mofify");
407       }
408     } else {
409       $this->handle_post_events("add");
410     }
412   }
414   function check()
415   {
416     $message= array();
417     return ($message);
418   }
421   function reload()
422   {
423     /* Generate applist */
424     $this->apps= array();
425     $ldap= $this->config->get_ldap_link();
426     $ldap->cd ("ou=apps,".$this->curbase);
428     $ldap->ls ("(objectClass=gosaApplication)","ou=apps,".$this->curbase);
429     while ($attrs= $ldap->fetch()){
430       if (isset($attrs["description"][0])){    
431         $this->apps[$attrs["cn"][0]]=
432           $attrs["cn"][0]." (".
433           $attrs["description"][0].")";
434       } else {
435         $this->apps[$attrs["cn"][0]]=
436           $attrs["cn"][0];
437       }
438     }
439     natcasesort ($this->apps);
440     reset ($this->apps);
442     foreach ($this->gosaMemberApplication as $value){
443       $this->used_apps[$value['App']]= $value['App'];
444     }
445   }
456   function CreateCatMenu()
457   {
458     /* The current category */
459     $na = $this->curCatDir;
461     /* the current folder depth */
462     $nd = $this->curCatDepth;
464     /* init array */
465     $return = array();
467     $return["__CATEGORY__"] = array();  // Categories
468     $return["__ENTRY__"]    = array();  // Entries in this category
469     $return["__BACK__"]     = false;    // The back entry
471     $tmp = new xmlParse();
473     $this->Categories= array();
474     if(!isset($this->config->data['MAIN']['KDE_APPLICATIONS_MENU']))    {
475         print_red(_("There is no value for 'KDE_APPLICATIONS_MENU' specified in your gosa.conf."));
476     }else{
477         $path = $this->config->data['MAIN']['KDE_APPLICATIONS_MENU'];
478         if(file_exists($path)){
479             if(is_readable($path)){
480                 $tmp->parseMenu($path);
481                 $this->Categories = $tmp->GetData();
482             }else{
483                 print_red(sprintf(_("The specified file '%s' for 'KDE_APPLICATIONS_MENU' in your gosa.conf is not accessable."),$path));
484             }
485         }else{
486             print_red(sprintf(_("The specified file '%s' for 'KDE_APPLICATIONS_MENU' in your gosa.conf is not accessable."),$path));
487         }
488     }
491     /* Create new categories to return */
492     foreach($this->Categories as $key => $val){
494         /* Split categories by | to get name and priority */
495         $nk = split("\/",$key);
497         /* Check if this category must be shown */
498         if((isset($nk[$nd]))&&(is_array($nk))&&($nk[$nd] == $na)){
500             /* Add this to the list, if theres is a sub category */
501             if(isset($nk[($nd+1)])){
502                 $return["__CATEGORY__"][$nk[($nd+1)]] = $nk[($nd+1)];
503             }
505             /* Create back entry */
506             if($nd !=0 ){
507                 $return['__BACK__'] = $nk[($nd-1)];
508             }else{
509                 $return['__BACK__'] = "..";
510             }
512         }elseif($na==""){
513             $return["__CATEGORY__"][$nk[0]] = $nk[0];
514         }
518     }
520     /* If back entry isn't set, set it to false (none) or to ..(base)*/
521     if($return["__BACK__"] == false){
522         $return['__BACK__'] = "..";
523         if($nd == 0 ){
524             $return['__BACK__'] = false;
525         }
526     }
528     foreach($this->gosaMemberApplication as $app){
529       if($app['Cat'] == $this->curCatDir){
530        $return["__ENTRY__"][$app['Pri']][$app['App']]  =  array("name"=>$app['App'],"prio" => $app['Pri']) ;
531       }
532     }    
535     ksort($return["__ENTRY__"]);
536  /* recreate array index */
537     $tmp = array();
538     foreach($return["__ENTRY__"] as $prio => $entries){
539         ksort($entries);
540         foreach($entries as $entry){
541             $tmp[$entry['name']] = $entry;
542         }
543     }
545     /* Assign sorted entries */
546     $return['__ENTRY__'] = ($tmp);
548     /* Return this all ..*/
549     return($return);
550   }
552   function addApp($cn)
553   {
554     $this->gosaMemberApplication[$cn."|".$this->curCatDir]= array("App"=>$cn,"Cat"=>$this->curCatDir,"Pri"=>"100");
555     $this->used_apps[$cn]=$cn;
556     $this->is_modified= TRUE;
557   }
559   function removeApp($cn)
560   {
561     $temp= array();
562     foreach ($this->gosaMemberApplication as $value){
563       if ($value != $cn){
564         $temp[]= $value;
565       }
566     }
567     $this->gosaMemberApplication= $temp;
568     $this->is_modified= TRUE;
569   }
573 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
574 ?>