Code

cd62e289eb2d87b8c735234444e93e158e043234
[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($appl){
122     $prio = $this->gosaMemberApplication[$appl]['Pri'];
123     $prios = array();
124     $next = 10000;
125     $key_use = false;
126     foreach($this->gosaMemberApplication as $key => $app){
127       if($app['Cat'] != $this->gosaMemberApplication[$appl]['Cat']) continue;
129       
131       if($app['Pri']> $prio){
132         if($app['Pri'] < $next){
133           $next     = $app['Pri'];
134           $key_use  = $key;
135         }
136       }
137     }
139     if($next != 0){
140       $this->gosaMemberApplication[$appl]['Pri']= $next;
141       $this->gosaMemberApplication[$key_use]['Pri']= $prio;
142     }
143   }
144   
145   function getNextFree($prio,$cat)
146   {
147     $ret = $prio;
148     $prios =array();
149     foreach($this->gosaMemberApplication as $app){
150       if($cat == $app['Cat']){
151         $prios[$app['Pri']]=$app['Pri'];
152       }
153     }
154     while(isset($prios[$ret])){
155       $ret ++;
156     }
157     return($ret);
158   }
160   function AddSeperator()
161   {
162     $i = 1;
163     while(isset($this->gosaMemberApplication["__SEPARATOR__".$i."|".$this->curCatDir])){
164       $i ++;
165     }
166     $this->gosaMemberApplication["__SEPARATOR__".$i."|".$this->curCatDir]['Pri'] = $this->getNextFree(1,$this->curCatDir);
167     $this->gosaMemberApplication["__SEPARATOR__".$i."|".$this->curCatDir]['Cat'] = $this->curCatDir; 
168     $this->gosaMemberApplication["__SEPARATOR__".$i."|".$this->curCatDir]['App'] = "__SEPARATOR__".$i;
169   }
171   function execute()
172   {
173           /* Call parent execute */
174         //plugin::execute();
175     if((isset($_GET['act']))&&($_GET['act']=="depopen")){
176       $dep = base64_decode($_GET['depid']);  
177       if(isset($this->config->idepartments[$dep])){
178         $this->curbase = $dep;
179       }
180     }
182     if(isset($_POST['AddSeperator'])){
183       $this->AddSeperator();
184     }
186     if((isset($_GET['act']))&&($_GET['act']=="open")){
187       $this->curCatDir = $_GET['id'];
189       $found = false;
190       foreach($this->Categories as $key => $name ){
191         if($this->curCatDir==$name){
192           $tmp = array_flip(split("\/",$key));
194           $this->curCatDepth = ($tmp[$this->curCatDir]);
195           $found= true;
196         }
197       }
198       if(!$found){
199         $this->curCatDir = "";
200         $this->curCatDepth = 0;
201       }
202     }
204     /* Do we need to flip is_account state? */
205     if (isset($_POST['modify_state'])){
206       $this->is_account= !$this->is_account;
207     }
209     /* Do we represent a valid group? */
210     if (!$this->is_account && $this->parent == NULL){
211       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
212         _("This 'dn' is no appgroup.")."</b>";
213       return ($display);
214     }
216     /* Show tab dialog headers */
217     $display= "";
218     if ($this->parent != NULL){
219       if ($this->is_account){
220         $display= $this->show_header(_("Remove applications"),
221             _("This group has application features enabled. You can disable them by clicking below."));
222       } else {
223         $display.= $this->show_header(_("Create applications"),
224             _("This group has application features disabled. You can enable them by clicking below."));
225         return ($display);
226       }
227     }
229     $this->reload();
230     foreach($_POST as $name => $value){
231       if(preg_match("/DelApp_/",$name)){
232         $app = preg_replace("/DelApp_/","",$name); 
233         unset($this->used_apps[$app]);
234         unset($this->gosaMemberApplication[$app."|".$this->curCatDir]);
235       }
236       if(preg_match("/EdiApp_/",$name)){
237         $appname = $value;
238         /* We've got the appname, get parameters from ldap */
239         $ldap= $this->config->get_ldap_link();
240         $ldap->cd($this->config->current['BASE']);
241         $ldap->search("(&(objectClass=gosaApplication)(cn=$appname))");
242         if ($ldap->count() != 1){
243           print_red (_("The selected application name is not uniq. Please check your LDAP."));
244         } else {
245           $attrs= $ldap->fetch();
246           if(isset($attrs['gosaApplicationParameter'])){
247             $this->dialog= TRUE;
249             /* Fill name and value arrays */
250             for ($i= 0; $i<$attrs['gosaApplicationParameter']['count']; $i++){
251               $option= preg_replace('/^[^:]+:/', '',
252                   $attrs['gosaApplicationParameter'][$i]);
253               $name= preg_replace('/:.*$/', '', 
254                   $attrs['gosaApplicationParameter'][$i]);
255               $this->option_name[$i]= $name;
257               /* Fill with values from application, default should be
258                  loaded by the external scripts */
259               if (isset($this->appoption[$name])){
260                 $this->option_value[$i]= $this->appoption[$name];
261               }
262             }
264             /* Create edit field */
265             $table= "<table summary=\"\">";
266             for ($i= 0; $i < count($this->option_name); $i++){
267               if (isset($this->option_value[$i])){
268                 $value= $this->option_value[$i];
269               } else {
270                 $value= "";
271               }
272               $table.="<tr><td>".$this->option_name[$i]."</td><td>".
273                 "<input name=\"value$i\" size=60 maxlength=250 ".
274                 "value=\"".$value."\"><br></td></tr>";
275             }
276             $table.= "</table>";
277             $this->table= $table;
278           } else {
279             print_red (_("The selected application has no options."));
280           }
281         }
282       }
283     }
284     $this->reload();
286     /* Add group with post */
287     if((isset($_GET['act']))&&($_GET['act']=="add")){
288       $this->used_apps[$_GET['id']]= $_GET['id'];
289       asort($this->used_apps);
290       $this->addApp($_GET['id']);
291     }
293     /* Add multiple */
294     if(isset($_POST['AddApps'])){
295       foreach($_POST as $name => $value){
296         if(preg_match("/AddApp_/",$name)){
297           $app = preg_replace("/AddApp_/","",$name);
298           $this->addApp($app);
299         }
300       }
301     }
304     /* Cancel edit options? */
305     if (isset($_POST['edit_options_cancel'])){
306       $this->dialog= FALSE;
307     }
309     /* Finish edit options? */
310     if (isset($_POST['edit_options_finish'])){
311       $this->dialog= FALSE;
313       /* Save informations passed by the user */
314       $this->option_value= array();
315       for ($i= 0; $i<count($this->option_name); $i++){
316         $this->appoption[$this->option_name[$i]]= $_POST["value$i"];
317         $this->is_modified= TRUE;
318       }
319     }
321     /* Prepare templating stuff */
322     $smarty= get_smarty();
323     $smarty->assign("used_apps", $this->used_apps);
324     $apps= array();
325     foreach ($this->apps as $key => $value){
326       if (!array_key_exists($key, $this->used_apps)){
327         $apps["$key"]= "$value";
328       }
329     }
331     $div = new DivSelectBox("appgroup");    
333     $div->SetHeight(300);
335     /* NEW LIST MANAGMENT
336      * We also need to search for the departments
337      * So we are able to navigate like in konquerer
338      */
340     $ldap = $this->config->get_ldap_link();
341     $ldap->cd($this->curbase) ;
342     $ldap->ls("(objectClass=gosaDepartment)"); 
343     $departments= array();
344     $tmp = array();
345     while ($value = $ldap->fetch()){
346       $tmp[strtolower($value['dn']).$value['dn']]=$value;
347     }
348     ksort($tmp);
349     foreach($tmp as $value){
350       if($value["description"][0]!=".."){
351         $departments[$value['dn']]=convert_department_dn($value['dn'])." - [".$value["description"][0]."]";
352       }else{
353         $departments[$value['dn']]=convert_department_dn($value['dn']);
354       }
355     }
356     
357     /* END NEW LIST MANAGMENT
358      */
360     $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=depopen&amp;depid=%s'>%s</a>";
361     $linkadd  = "<a href='?plug=".$_GET['plug']."&amp;act=add&amp;id=%s'>%s</a>";
363     $base_back = preg_replace("/^[^,]+,/","",$this->curbase);
364     if((strlen($base_back)>= strlen($this->config->current['BASE']))&&($this->curbase!=$this->config->current['BASE'])){
365       $div->AddEntry(array(
366             array("string"=>sprintf($linkopen,base64_encode($base_back),".. ["._("back")."]"),
367                   "attach"=>"style='border:0px;'")
368             ));
369     }
370     foreach($departments as $key => $app){
371       $div->AddEntry(array(
372                             array("string"=>"<img src='images/folder.png' alt='"._("department")."'>&nbsp;".sprintf($linkopen,base64_encode($key),$app),
373                                   "attach"=>"style='border:0px;'")
374                           ));
375     }
376     
377     foreach($apps as $key => $app){
378       $div->AddEntry(array(
379                             array("string"=>sprintf("<input type='checkbox' value='1' name='AddApp_%s'>",$key).
380                                   "<img src='images/select_application.png' alt='"._("application")."'>&nbsp;".sprintf($linkadd,$key,$app),
381                                   "attach"=>"style='border:0px;'")
382                           ));
383     }
385     if((isset($_GET['act']))&&(($_GET['act'] == "one_up")||($_GET['act']=="one_down"))){
386       if(isset($_GET['id'])){
387         $id   = $_GET['id'];
388         $act  = $_GET['act']; 
389         $found = false;
390         foreach($this->gosaMemberApplication as $key =>  $member){
391           if($id == $member['App']){
392             $found = $key;
393           }
394         }
396         if($found != false){
397           if($act == "one_up"){
398             $this->getOneUp($found);
399           }elseif($act == "one_down")   { 
400             $this->getOneDown($found);
401           }
402         }
403       }
404     }
406     $div2 = new DivSelectBox("appgroup");
407     $div2->SetHeight(300);
409     $menu           = $this->CreateCatMenu();
410     $str_noprio     = " %s ";
411     $linkopen       = "<img src='images/folder.png'>            &nbsp;<a href='?plug=".$_GET['plug']."&amp;act=open&amp;id=%s'>%s</a>";
412     $app   = "<img src='images/select_application.png'>&nbsp;%s".$str_noprio;
414         /* append back entry */
415     if($menu["__BACK__"] != false){
416         $div2 ->AddEntry(array(
417                     array("string"=>sprintf($linkopen,$menu["__BACK__"],".. [ "._("back")." ]")),
418                     array("string"=>"&nbsp;","attach"=>"style='border-right:0px;'")
419                     ));
420     }
422     foreach($menu['__CATEGORY__'] as $path => $name){
423       $div2 ->AddEntry(array(array("string"=>sprintf($linkopen,$path,$name)),array("string"=>"&nbsp;","attach"=>"style='border-right:0px;'"))); 
424     }
426       /* Append entries */
428     $separator ="<hr height=1  size=1></hr>"; 
429  
430     $upudown = "<a href='?plug=".$_GET['plug']."&amp;act=one_up&id=%s'>   <img src='images/sort_up.png' border=0></a>".
431           "&nbsp;<a href='?plug=".$_GET['plug']."&amp;act=one_down&id=%s'> <img src='images/sort_down.png' border=0></a>".
432           "&nbsp;<input type='image' src='images/edittrash.png' name='DelApp_%s' value='%s'>";
433     $edit=      "&nbsp;<input type='image' src='images/edit.png' name='EdiApp_%s' value='%s'>";
434  
435     foreach($menu["__ENTRY__"] as $path => $entry){
437       if(preg_match("/__SEPARATOR__/",$path)){
438         $div2 ->AddEntry(array(array("string"=>$separator),
439                          array("string"=>preg_replace("/\%s/",$entry['name'],$upudown),"attach"=>"style='border-right:0px;'")));
441       }else{
442         $div2 ->AddEntry(array(array("string"=>sprintf($app,$entry['name'],$entry['prio'])),
443                          array("string"=>preg_replace("/\%s/",$entry['name'],$upudown.$edit),"attach"=>"style='border-right:0px;'")));
444       }
445     }
447     $smarty->assign("UsedApps", $div2->DrawList());
448     $smarty->assign("List", $div->DrawList());
449     $smarty->assign("apps", $apps);
451     /* Show main page */
452     if ($this->dialog){
453       $smarty->assign("table", $this->table);
454       $display.= $smarty->fetch (get_template_path('application_options.tpl', TRUE));
455     } else {
456       $display.= $smarty->fetch (get_template_path('application.tpl', TRUE));
457     }
458     return ($display);
459   }
462   function remove_from_parent()
463   {
464     plugin::remove_from_parent();
466     $this->attrs["gosaMemberApplication"]= array();
468     $ldap= $this->config->get_ldap_link();
469     $ldap->cd($this->dn);
470     $ldap->modify($this->attrs);
471     show_ldap_error($ldap->get_error());
473     /* Optionally execute a command after we're done */
474     $this->handle_post_events("remove");
475   }
478   /* Save data to object */
479 #  function save_object()
480 #  {
481 #    plugin::save_object();
482 #  }
485   /* Save to LDAP */
486   function save()
487   {
488     plugin::save();
490     /* Copy members */
491     $this->attrs["gosaMemberApplication"]= array();
492     foreach ($this->gosaMemberApplication as $val){
493       if(!preg_match("/__SEPARATOR__/i",$val['App'])){
494         $this->attrs["gosaMemberApplication"][]= $val['App']."|".$val['Cat']."|".$val['Pri'];
495       }
496     }
498     /* Are there application parameters to be saved */
499     $this->attrs['gosaApplicationParameter']= array();
500     foreach($this->appoption as $name => $value){
501       if ($value != ""){
502         $this->attrs['gosaApplicationParameter'][]= "$name:$value";
503       }
504     }
506     /* Write back to LDAP */
507     $ldap= $this->config->get_ldap_link();
508     $ldap->cd($this->dn);
509     $ldap->modify($this->attrs);
510     show_ldap_error($ldap->get_error());
512     /* Optionally execute a command after we're done */
513     if ($this->initially_was_account == $this->is_account){
514       if ($this->is_modified){
515         $this->handle_post_events("mofify");
516       }
517     } else {
518       $this->handle_post_events("add");
519     }
521   }
523   function check()
524   {
525     $message= array();
526     return ($message);
527   }
530   function reload()
531   {
532     /* Generate applist */
533     $this->apps= array();
534     $ldap= $this->config->get_ldap_link();
535     $ldap->cd ("ou=apps,".$this->curbase);
537     $ldap->ls ("(objectClass=gosaApplication)","ou=apps,".$this->curbase);
538     while ($attrs= $ldap->fetch()){
539       if (isset($attrs["description"][0])){    
540         $this->apps[$attrs["cn"][0]]=
541           $attrs["cn"][0]." (".
542           $attrs["description"][0].")";
543       } else {
544         $this->apps[$attrs["cn"][0]]=
545           $attrs["cn"][0];
546       }
547     }
548     natcasesort ($this->apps);
549     reset ($this->apps);
551     foreach ($this->gosaMemberApplication as $value){
552       $this->used_apps[$value['App']]= $value['App'];
553     }
554   }
565   function CreateCatMenu()
566   {
567     /* The current category */
568     $na = $this->curCatDir;
570     /* the current folder depth */
571     $nd = $this->curCatDepth;
573     /* init array */
574     $return = array();
576     $return["__CATEGORY__"] = array();  // Categories
577     $return["__ENTRY__"]    = array();  // Entries in this category
578     $return["__BACK__"]     = false;    // The back entry
580     $tmp = new xmlParse();
582     $this->Categories= array();
583     if(!isset($this->config->data['MAIN']['KDE_APPLICATIONS_MENU']))    {
584         print_red(_("There is no value for 'KDE_APPLICATIONS_MENU' specified in your gosa.conf."));
585     }else{
586         $path = $this->config->data['MAIN']['KDE_APPLICATIONS_MENU'];
587         if(file_exists($path)){
588             if(is_readable($path)){
589                 $tmp->parseMenu($path);
590                 $this->Categories = $tmp->GetData();
591             }else{
592                 print_red(sprintf(_("The specified file '%s' for 'KDE_APPLICATIONS_MENU' in your gosa.conf is not accessable."),$path));
593             }
594         }else{
595             print_red(sprintf(_("The specified file '%s' for 'KDE_APPLICATIONS_MENU' in your gosa.conf is not accessable."),$path));
596         }
597     }
600     /* Create new categories to return */
601     foreach($this->Categories as $key => $val){
603         /* Split categories by | to get name and priority */
604         $nk = split("\/",$key);
606         /* Check if this category must be shown */
607         if((isset($nk[$nd]))&&(is_array($nk))&&($nk[$nd] == $na)){
609             /* Add this to the list, if theres is a sub category */
610             if(isset($nk[($nd+1)])){
611                 $return["__CATEGORY__"][$nk[($nd+1)]] = $nk[($nd+1)];
612             }
614             /* Create back entry */
615             if($nd !=0 ){
616                 $return['__BACK__'] = $nk[($nd-1)];
617             }else{
618                 $return['__BACK__'] = "..";
619             }
621         }elseif($na==""){
622             $return["__CATEGORY__"][$nk[0]] = $nk[0];
623         }
627     }
629     /* If back entry isn't set, set it to false (none) or to ..(base)*/
630     if($return["__BACK__"] == false){
631         $return['__BACK__'] = "..";
632         if($nd == 0 ){
633             $return['__BACK__'] = false;
634         }
635     }
637     foreach($this->gosaMemberApplication as $app){
638       if($app['Cat'] == $this->curCatDir){
639        $return["__ENTRY__"][$app['Pri']][$app['App']]  =  array("name"=>$app['App'],"prio" => $app['Pri']) ;
640       }
641     }    
644     ksort($return["__ENTRY__"]);
645  /* recreate array index */
646     $tmp = array();
647     foreach($return["__ENTRY__"] as $prio => $entries){
648         ksort($entries);
649         foreach($entries as $entry){
650             $tmp[$entry['name']] = $entry;
651         }
652     }
654     /* Assign sorted entries */
655     $return['__ENTRY__'] = ($tmp);
657     /* Return this all ..*/
658     return($return);
659   }
661   function addApp($cn)
662   {
663     $this->gosaMemberApplication[$cn."|".$this->curCatDir]= array("App"=>$cn,"Cat"=>$this->curCatDir,"Pri"=>$this->getNextFree(1,$this->curCatDir));
664     $this->used_apps[$cn]=$cn;
665     $this->is_modified= TRUE;
666   }
668   function removeApp($cn)
669   {
670     $temp= array();
671     foreach ($this->gosaMemberApplication as $value){
672       if ($value != $cn){
673         $temp[]= $value;
674       }
675     }
676     $this->gosaMemberApplication= $temp;
677     $this->is_modified= TRUE;
678   }
682 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
683 ?>