Code

f75d80d630417a30c931e869ddc2632ceedaff77
[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   var $curbase   = "";
22   var $curCatDir;
23   var $curCatDepth=0;
24   var $Categories=array();
27   /* attribute list for save action */
28   var $attributes= array();
29   var $objectclasses= array("gosaApplicationGroup");
31   function appgroup ($config, $dn= NULL)
32   {
33     plugin::plugin ($config, $dn);
35     /* Load member applications */
36     if (isset ($this->attrs["gosaMemberApplication"][0])){
37       for ($i= 0; $i<$this->attrs["gosaMemberApplication"]["count"]; $i++){
38         $this->gosaMemberApplication[]=
39           $this->attrs["gosaMemberApplication"][$i];
40       }
41     }
43     /* Load application options */
44     if (isset($this->attrs['gosaApplicationParameter'])){
45       for ($i= 0; $i<$this->attrs['gosaApplicationParameter']['count']; $i++){
46         $option= preg_replace('/^[^:]+:/', '',
47             $this->attrs['gosaApplicationParameter'][$i]);
48         $name= preg_replace('/:.*$/', '',
49             $this->attrs['gosaApplicationParameter'][$i]);
50         $this->appoption[$name]= $option;
51       }
52     }
54     $tmp = array();
55     $tmp2 = array();
56   
57     $prios = 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     
66         $cats [$tmp[1]] = $tmp[1];
67         $prios[$tmp[1]][$tmp[2]] = $tmp[2];
68       }else{
69         $tmp2[$memberApp."|"]['App'] = $memberApp;
70         $tmp2[$memberApp."|"]['Cat'] = "";
71         $tmp2[$memberApp."|"]['Pri'] = 1000;
72       }
73     }
75     $this->gosaMemberApplication = $tmp2;
76     foreach($cats as $cat ){
77       $max = max($prios[$cat]);
78       $min = 1;//min($prios[$cat]);  
79       for($i = $min ; $i < $max ; $i++){
80         if(!isset($prios[$cat][$i])){
81           $this->gosaMemberApplication["__SEPARATOR__".$i."|".$cat]['Pri'] = $i;
82           $this->gosaMemberApplication["__SEPARATOR__".$i."|".$cat]['Cat'] = $this->curCatDir;
83           $this->gosaMemberApplication["__SEPARATOR__".$i."|".$cat]['App'] = "__SEPARATOR__".$i;
84         }
85       }
86     }
88     /* Append at last*/
89     foreach($this->gosaMemberApplication as $key => $app){
90       if($app['Pri'] == 1000){
91         $this->gosaMemberApplication[$key]['Pri'] = $this->getNextFree(1,$app['Cat']);
92       }
93     }
94   
95   
96     $this->curbase = $this->config->current['BASE'];
97   }
99   function getOneUp($appl)
100   {
101     $prio = $this->gosaMemberApplication[$appl]['Pri'];
102     $prios = array();
103     $next = 0;
104     $key_use = false;
105     foreach($this->gosaMemberApplication as $key => $app){
106       if($app['Cat'] != $this->gosaMemberApplication[$appl]['Cat']) continue;
107       if($app['Pri']< $prio){
108         if($app['Pri'] > $next){
109           $next     = $app['Pri'];
110           $key_use  = $key;
111         }
112       }        
113     }
115     if($next != 0){
116       $this->gosaMemberApplication[$appl]['Pri']= $next;
117       $this->gosaMemberApplication[$key_use]['Pri']= $prio;
118     }
119   }
120   
121   function getOneDown($prio){
122     return($prio); 
123   }
124   
125   function getNextFree($prio,$cat)
126   {
127     $ret = $prio;
128     $prios =array();
129     foreach($this->gosaMemberApplication as $app){
130       if($cat == $app['Cat']){
131         $prios[$app['Pri']]=$app['Pri'];
132       }
133     }
134     while(isset($prios[$ret])){
135       $ret ++;
136     }
137     return($ret);
138   }
140   function AddSeperator()
141   {
142     $i = 1;
143     while(isset($this->gosaMemberApplication["__SEPARATOR__".$i."|".$this->curCatDir])){
144       $i ++;
145     }
146     $this->gosaMemberApplication["__SEPARATOR__".$i."|".$this->curCatDir]['Pri'] = $this->getNextFree(1,$this->curCatDir);
147     $this->gosaMemberApplication["__SEPARATOR__".$i."|".$this->curCatDir]['Cat'] = $this->curCatDir; 
148     $this->gosaMemberApplication["__SEPARATOR__".$i."|".$this->curCatDir]['App'] = "__SEPARATOR__".$i;
149   }
151   function execute()
152   {
153           /* Call parent execute */
154         //plugin::execute();
155     if((isset($_GET['act']))&&($_GET['act']=="depopen")){
156       $dep = base64_decode($_GET['depid']);  
157       if(isset($this->config->idepartments[$dep])){
158         $this->curbase = $dep;
159       }
160     }
162     if(isset($_POST['AddSeperator'])){
163       $this->AddSeperator();
164     }
166     if((isset($_GET['act']))&&($_GET['act']=="open")){
167       $this->curCatDir = $_GET['id'];
169       $found = false;
170       foreach($this->Categories as $key => $name ){
171         if($this->curCatDir==$name){
172           $tmp = array_flip(split("\/",$key));
174           $this->curCatDepth = ($tmp[$this->curCatDir]);
175           $found= true;
176         }
177       }
178       if(!$found){
179         $this->curCatDir = "";
180         $this->curCatDepth = 0;
181       }
182     }
184     /* Do we need to flip is_account state? */
185     if (isset($_POST['modify_state'])){
186       $this->is_account= !$this->is_account;
187     }
189     /* Do we represent a valid group? */
190     if (!$this->is_account && $this->parent == NULL){
191       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
192         _("This 'dn' is no appgroup.")."</b>";
193       return ($display);
194     }
196     /* Show tab dialog headers */
197     $display= "";
198     if ($this->parent != NULL){
199       if ($this->is_account){
200         $display= $this->show_header(_("Remove applications"),
201             _("This group has application features enabled. You can disable them by clicking below."));
202       } else {
203         $display.= $this->show_header(_("Create applications"),
204             _("This group has application features disabled. You can enable them by clicking below."));
205         return ($display);
206       }
207     }
209     $this->reload();
210     foreach($_POST as $name => $value){
211       if(preg_match("/DelApp_/",$name)){
212         $app = preg_replace("/DelApp_/","",$name); 
213         unset($this->used_apps[$app]);
214         unset($this->gosaMemberApplication[$app."|".$this->curCatDir]);
215       }
216       if(preg_match("/EdiApp_/",$name)){
217         $appname = $value;
218         /* We've got the appname, get parameters from ldap */
219         $ldap= $this->config->get_ldap_link();
220         $ldap->cd($this->config->current['BASE']);
221         $ldap->search("(&(objectClass=gosaApplication)(cn=$appname))");
222         if ($ldap->count() != 1){
223           print_red (_("The selected application name is not uniq. Please check your LDAP."));
224         } else {
225           $attrs= $ldap->fetch();
226           if(isset($attrs['gosaApplicationParameter'])){
227             $this->dialog= TRUE;
229             /* Fill name and value arrays */
230             for ($i= 0; $i<$attrs['gosaApplicationParameter']['count']; $i++){
231               $option= preg_replace('/^[^:]+:/', '',
232                   $attrs['gosaApplicationParameter'][$i]);
233               $name= preg_replace('/:.*$/', '', 
234                   $attrs['gosaApplicationParameter'][$i]);
235               $this->option_name[$i]= $name;
237               /* Fill with values from application, default should be
238                  loaded by the external scripts */
239               if (isset($this->appoption[$name])){
240                 $this->option_value[$i]= $this->appoption[$name];
241               }
242             }
244             /* Create edit field */
245             $table= "<table summary=\"\">";
246             for ($i= 0; $i < count($this->option_name); $i++){
247               if (isset($this->option_value[$i])){
248                 $value= $this->option_value[$i];
249               } else {
250                 $value= "";
251               }
252               $table.="<tr><td>".$this->option_name[$i]."</td><td>".
253                 "<input name=\"value$i\" size=60 maxlength=250 ".
254                 "value=\"".$value."\"><br></td></tr>";
255             }
256             $table.= "</table>";
257             $this->table= $table;
258           } else {
259             print_red (_("The selected application has no options."));
260           }
261         }
262       }
263     }
264     $this->reload();
266     /* Add group with post */
267     if((isset($_GET['act']))&&($_GET['act']=="add")){
268       $this->used_apps[$_GET['id']]= $_GET['id'];
269       asort($this->used_apps);
270       $this->addApp($_GET['id']);
271     }
273     /* Add multiple */
274     if(isset($_POST['AddApps'])){
275       foreach($_POST as $name => $value){
276         if(preg_match("/AddApp_/",$name)){
277           $app = preg_replace("/AddApp_/","",$name);
278           $this->addApp($app);
279         }
280       }
281     }
284     /* Cancel edit options? */
285     if (isset($_POST['edit_options_cancel'])){
286       $this->dialog= FALSE;
287     }
289     /* Finish edit options? */
290     if (isset($_POST['edit_options_finish'])){
291       $this->dialog= FALSE;
293       /* Save informations passed by the user */
294       $this->option_value= array();
295       for ($i= 0; $i<count($this->option_name); $i++){
296         $this->appoption[$this->option_name[$i]]= $_POST["value$i"];
297         $this->is_modified= TRUE;
298       }
299     }
301     /* Prepare templating stuff */
302     $smarty= get_smarty();
303     $smarty->assign("used_apps", $this->used_apps);
304     $apps= array();
305     foreach ($this->apps as $key => $value){
306       if (!array_key_exists($key, $this->used_apps)){
307         $apps["$key"]= "$value";
308       }
309     }
311     $div = new DivSelectBox("appgroup");    
313     $div->SetHeight(300);
315     /* NEW LIST MANAGMENT
316      * We also need to search for the departments
317      * So we are able to navigate like in konquerer
318      */
320     $ldap = $this->config->get_ldap_link();
321     $ldap->cd($this->curbase) ;
322     $ldap->ls("(objectClass=gosaDepartment)"); 
323     $departments= array();
324     $tmp = array();
325     while ($value = $ldap->fetch()){
326       $tmp[strtolower($value['dn']).$value['dn']]=$value;
327     }
328     ksort($tmp);
329     foreach($tmp as $value){
330       if($value["description"][0]!=".."){
331         $departments[$value['dn']]=convert_department_dn($value['dn'])." - [".$value["description"][0]."]";
332       }else{
333         $departments[$value['dn']]=convert_department_dn($value['dn']);
334       }
335     }
336     
337     /* END NEW LIST MANAGMENT
338      */
340     $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=depopen&amp;depid=%s'>%s</a>";
341     $linkadd  = "<a href='?plug=".$_GET['plug']."&amp;act=add&amp;id=%s'>%s</a>";
343     $base_back = preg_replace("/^[^,]+,/","",$this->curbase);
344     if((strlen($base_back)>= strlen($this->config->current['BASE']))&&($this->curbase!=$this->config->current['BASE'])){
345       $div->AddEntry(array(
346             array("string"=>sprintf($linkopen,base64_encode($base_back),".. ["._("back")."]"),
347                   "attach"=>"style='border:0px;'")
348             ));
349     }
350     foreach($departments as $key => $app){
351       $div->AddEntry(array(
352                             array("string"=>"<img src='images/folder.png' alt='"._("department")."'>&nbsp;".sprintf($linkopen,base64_encode($key),$app),
353                                   "attach"=>"style='border:0px;'")
354                           ));
355     }
356     
357     foreach($apps as $key => $app){
358       $div->AddEntry(array(
359                             array("string"=>sprintf("<input type='checkbox' value='1' name='AddApp_%s'>",$key).
360                                   "<img src='images/select_application.png' alt='"._("application")."'>&nbsp;".sprintf($linkadd,$key,$app),
361                                   "attach"=>"style='border:0px;'")
362                           ));
363     }
365     if((isset($_GET['act']))&&(($_GET['act'] == "one_up")||($_GET['act']=="one_down"))){
366       if(isset($_GET['id'])){
367         $id   = $_GET['id'];
368         $act  = $_GET['act']; 
369         $found = false;
370         foreach($this->gosaMemberApplication as $key =>  $member){
371           if($id == $member['App']){
372             $found = $key;
373           }
374         }
376         if($found != false){
377           if($act == "one_up"){
378             $this->getOneUp($found);
379           }elseif($act == "one_down")   { 
380             $this->getOneDown($found);
381           }
382         }
383       }
384     }
386     $div2 = new DivSelectBox("appgroup");
387     $div2->SetHeight(300);
389     $menu           = $this->CreateCatMenu();
390     $str_noprio     = " %s ";
391     $linkopen       = "<img src='images/folder.png'>            &nbsp;<a href='?plug=".$_GET['plug']."&amp;act=open&amp;id=%s'>%s</a>";
392     $app   = "<img src='images/select_application.png'>&nbsp;%s".$str_noprio;
394         /* append back entry */
395     if($menu["__BACK__"] != false){
396         $div2 ->AddEntry(array(
397                     array("string"=>sprintf($linkopen,$menu["__BACK__"],".. [ "._("back")." ]")),
398                     array("string"=>"&nbsp;","attach"=>"style='border-right:0px;'")
399                     ));
400     }
402     foreach($menu['__CATEGORY__'] as $path => $name){
403       $div2 ->AddEntry(array(array("string"=>sprintf($linkopen,$path,$name)),array("string"=>"&nbsp;","attach"=>"style='border-right:0px;'"))); 
404     }
406       /* Append entries */
408     $separator ="<hr height=1  size=1></hr>"; 
409  
410     $upudown = "<a href='?plug=".$_GET['plug']."&amp;act=one_up&id=%s'>   <img src='images/sort_up.png' border=0></a>".
411           "&nbsp;<a href='?plug=".$_GET['plug']."&amp;act=one_down&id=%s'> <img src='images/sort_down.png' border=0></a>".
412           "&nbsp;<input type='image' src='images/edittrash.png' name='DelApp_%s' value='%s'>";
413     $edit=      "&nbsp;<input type='image' src='images/edit.png' name='EdiApp_%s' value='%s'>";
414  
415     foreach($menu["__ENTRY__"] as $path => $entry){
417       if(preg_match("/__SEPARATOR__/",$path)){
418         $div2 ->AddEntry(array(array("string"=>$separator),
419                          array("string"=>preg_replace("/\%s/",$entry['name'],$upudown),"attach"=>"style='border-right:0px;'")));
421       }else{
422         $div2 ->AddEntry(array(array("string"=>sprintf($app,$entry['name'],$entry['prio'])),
423                          array("string"=>preg_replace("/\%s/",$entry['name'],$upudown.$edit),"attach"=>"style='border-right:0px;'")));
424       }
425     }
427     $smarty->assign("UsedApps", $div2->DrawList());
428     $smarty->assign("List", $div->DrawList());
429     $smarty->assign("apps", $apps);
431     /* Show main page */
432     if ($this->dialog){
433       $smarty->assign("table", $this->table);
434       $display.= $smarty->fetch (get_template_path('application_options.tpl', TRUE));
435     } else {
436       $display.= $smarty->fetch (get_template_path('application.tpl', TRUE));
437     }
438     return ($display);
439   }
442   function remove_from_parent()
443   {
444     plugin::remove_from_parent();
446     $this->attrs["gosaMemberApplication"]= array();
448     $ldap= $this->config->get_ldap_link();
449     $ldap->cd($this->dn);
450     $ldap->modify($this->attrs);
451     show_ldap_error($ldap->get_error());
453     /* Optionally execute a command after we're done */
454     $this->handle_post_events("remove");
455   }
458   /* Save data to object */
459 #  function save_object()
460 #  {
461 #    plugin::save_object();
462 #  }
465   /* Save to LDAP */
466   function save()
467   {
468     plugin::save();
470     /* Copy members */
472     print_a($this->attrs["gosaMemberApplication"]);
473     $this->attrs["gosaMemberApplication"]= array();
474     foreach ($this->gosaMemberApplication as $val){
475       if(!preg_match("/__SEPARATOR__/i",$val['App'])){
476         $this->attrs["gosaMemberApplication"][]= $val['App']."|".$val['Cat']."|".$val['Pri'];
477       }
478     }
480     /* Are there application parameters to be saved */
481     $this->attrs['gosaApplicationParameter']= array();
482     foreach($this->appoption as $name => $value){
483       if ($value != ""){
484         $this->attrs['gosaApplicationParameter'][]= "$name:$value";
485       }
486     }
488     /* Write back to LDAP */
489     $ldap= $this->config->get_ldap_link();
490     $ldap->cd($this->dn);
491     $ldap->modify($this->attrs);
492     show_ldap_error($ldap->get_error());
494     /* Optionally execute a command after we're done */
495     if ($this->initially_was_account == $this->is_account){
496       if ($this->is_modified){
497         $this->handle_post_events("mofify");
498       }
499     } else {
500       $this->handle_post_events("add");
501     }
503   }
505   function check()
506   {
507     $message= array();
508     return ($message);
509   }
512   function reload()
513   {
514     /* Generate applist */
515     $this->apps= array();
516     $ldap= $this->config->get_ldap_link();
517     $ldap->cd ("ou=apps,".$this->curbase);
519     $ldap->ls ("(objectClass=gosaApplication)","ou=apps,".$this->curbase);
520     while ($attrs= $ldap->fetch()){
521       if (isset($attrs["description"][0])){    
522         $this->apps[$attrs["cn"][0]]=
523           $attrs["cn"][0]." (".
524           $attrs["description"][0].")";
525       } else {
526         $this->apps[$attrs["cn"][0]]=
527           $attrs["cn"][0];
528       }
529     }
530     natcasesort ($this->apps);
531     reset ($this->apps);
533     foreach ($this->gosaMemberApplication as $value){
534       $this->used_apps[$value['App']]= $value['App'];
535     }
536   }
547   function CreateCatMenu()
548   {
549     /* The current category */
550     $na = $this->curCatDir;
552     /* the current folder depth */
553     $nd = $this->curCatDepth;
555     /* init array */
556     $return = array();
558     $return["__CATEGORY__"] = array();  // Categories
559     $return["__ENTRY__"]    = array();  // Entries in this category
560     $return["__BACK__"]     = false;    // The back entry
562     $tmp = new xmlParse();
564     $this->Categories= array();
565     if(!isset($this->config->data['MAIN']['KDE_APPLICATIONS_MENU']))    {
566         print_red(_("There is no value for 'KDE_APPLICATIONS_MENU' specified in your gosa.conf."));
567     }else{
568         $path = $this->config->data['MAIN']['KDE_APPLICATIONS_MENU'];
569         if(file_exists($path)){
570             if(is_readable($path)){
571                 $tmp->parseMenu($path);
572                 $this->Categories = $tmp->GetData();
573             }else{
574                 print_red(sprintf(_("The specified file '%s' for 'KDE_APPLICATIONS_MENU' in your gosa.conf is not accessable."),$path));
575             }
576         }else{
577             print_red(sprintf(_("The specified file '%s' for 'KDE_APPLICATIONS_MENU' in your gosa.conf is not accessable."),$path));
578         }
579     }
582     /* Create new categories to return */
583     foreach($this->Categories as $key => $val){
585         /* Split categories by | to get name and priority */
586         $nk = split("\/",$key);
588         /* Check if this category must be shown */
589         if((isset($nk[$nd]))&&(is_array($nk))&&($nk[$nd] == $na)){
591             /* Add this to the list, if theres is a sub category */
592             if(isset($nk[($nd+1)])){
593                 $return["__CATEGORY__"][$nk[($nd+1)]] = $nk[($nd+1)];
594             }
596             /* Create back entry */
597             if($nd !=0 ){
598                 $return['__BACK__'] = $nk[($nd-1)];
599             }else{
600                 $return['__BACK__'] = "..";
601             }
603         }elseif($na==""){
604             $return["__CATEGORY__"][$nk[0]] = $nk[0];
605         }
609     }
611     /* If back entry isn't set, set it to false (none) or to ..(base)*/
612     if($return["__BACK__"] == false){
613         $return['__BACK__'] = "..";
614         if($nd == 0 ){
615             $return['__BACK__'] = false;
616         }
617     }
619     foreach($this->gosaMemberApplication as $app){
620       if($app['Cat'] == $this->curCatDir){
621        $return["__ENTRY__"][$app['Pri']][$app['App']]  =  array("name"=>$app['App'],"prio" => $app['Pri']) ;
622       }
623     }    
626     ksort($return["__ENTRY__"]);
627  /* recreate array index */
628     $tmp = array();
629     foreach($return["__ENTRY__"] as $prio => $entries){
630         ksort($entries);
631         foreach($entries as $entry){
632             $tmp[$entry['name']] = $entry;
633         }
634     }
636     /* Assign sorted entries */
637     $return['__ENTRY__'] = ($tmp);
639     /* Return this all ..*/
640     return($return);
641   }
643   function addApp($cn)
644   {
645     $this->gosaMemberApplication[$cn."|".$this->curCatDir]= array("App"=>$cn,"Cat"=>$this->curCatDir,"Pri"=>$this->getNextFree(1,$this->curCatDir));
646     $this->used_apps[$cn]=$cn;
647     $this->is_modified= TRUE;
648   }
650   function removeApp($cn)
651   {
652     $temp= array();
653     foreach ($this->gosaMemberApplication as $value){
654       if ($value != $cn){
655         $temp[]= $value;
656       }
657     }
658     $this->gosaMemberApplication= $temp;
659     $this->is_modified= TRUE;
660   }
664 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
665 ?>