Code

5b42adcde0524569323894690f099e03c798d4dc
[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     $cats = array();
61     foreach($this->gosaMemberApplication as $memberApp){
62       if(preg_match("/\|/i",$memberApp)){
63         $tmp = split("\|",$memberApp);
64         $tmp2[$tmp[0]."|".$tmp[1]]['App'] = $tmp[0];
65         $tmp2[$tmp[0]."|".$tmp[1]]['Cat'] = $tmp[1];
66         $tmp2[$tmp[0]."|".$tmp[1]]['Pri'] = $tmp[2];
67     
68         $cats [$tmp[1]] = $tmp[1];
69         $prios[$tmp[1]][$tmp[2]] = $tmp[2];
70       }else{
71         $tmp2[$memberApp."|"]['App'] = $memberApp;
72         $tmp2[$memberApp."|"]['Cat'] = "";
73         $tmp2[$memberApp."|"]['Pri'] = 1000;
74       }
75     }
77     $this->gosaMemberApplication = $tmp2;
78     foreach($cats as $cat ){
79       $max = max($prios[$cat]);
80       $min = 1;//min($prios[$cat]);  
81       $last = false;
82       for($i = $min ; $i < $max ; $i++){
83         if(!isset($prios[$cat][$i])){
84           if($last == false){
85             $this->gosaMemberApplication["__SEPARATOR__".$i."|".$cat]['Pri'] = $i;
86             $this->gosaMemberApplication["__SEPARATOR__".$i."|".$cat]['Cat'] = $this->curCatDir;
87             $this->gosaMemberApplication["__SEPARATOR__".$i."|".$cat]['App'] = "__SEPARATOR__".$i;
88           
89             $last = true;
90           }
91         }else{
92           $last = false;
93         }
94       }
95     }
97     /* Append at last*/
98     foreach($this->gosaMemberApplication as $key => $app){
99       if($app['Pri'] == 1000){
100         $this->gosaMemberApplication[$key]['Pri'] = $this->getNextFree(1,$app['Cat']);
101       }
102     }
103   
104   
105     $this->curbase = $this->config->current['BASE'];
106   }
108   function getOneUp($appl)
109   {
110     $prio = $this->gosaMemberApplication[$appl]['Pri'];
111     $prios = array();
112     $next = 0;
113     $key_use = false;
114     foreach($this->gosaMemberApplication as $key => $app){
115       if($app['Cat'] != $this->gosaMemberApplication[$appl]['Cat']) continue;
116       if($app['Pri']< $prio){
117         if($app['Pri'] > $next){
118           $next     = $app['Pri'];
119           $key_use  = $key;
120         }
121       }        
122     }
124     if($next != 0){
125       $this->gosaMemberApplication[$appl]['Pri']= $next;
126       $this->gosaMemberApplication[$key_use]['Pri']= $prio;
127     }
128   }
129   
130   function getOneDown($appl){
131     $prio = $this->gosaMemberApplication[$appl]['Pri'];
132     $prios = array();
133     $next = 10000;
134     $key_use = false;
135     foreach($this->gosaMemberApplication as $key => $app){
136       if($app['Cat'] != $this->gosaMemberApplication[$appl]['Cat']) continue;
138       
140       if($app['Pri']> $prio){
141         if($app['Pri'] < $next){
142           $next     = $app['Pri'];
143           $key_use  = $key;
144         }
145       }
146     }
148     if($next != 0){
149       $this->gosaMemberApplication[$appl]['Pri']= $next;
150       $this->gosaMemberApplication[$key_use]['Pri']= $prio;
151     }
152   }
153   
154   function getNextFree($prio,$cat)
155   {
156     $ret = $prio;
157     $prios =array();
158     foreach($this->gosaMemberApplication as $app){
159       if($cat == $app['Cat']){
160         $prios[$app['Pri']]=$app['Pri'];
161       }
162     }
163     while(isset($prios[$ret])){
164       $ret ++;
165     }
166     return($ret);
167   }
169   function AddSeperator()
170   {
171     $i = 1;
172     while(isset($this->gosaMemberApplication["__SEPARATOR__".$i."|".$this->curCatDir])){
173       $i ++;
174     }
175     $this->gosaMemberApplication["__SEPARATOR__".$i."|".$this->curCatDir]['Pri'] = $this->getNextFree(1,$this->curCatDir);
176     $this->gosaMemberApplication["__SEPARATOR__".$i."|".$this->curCatDir]['Cat'] = $this->curCatDir; 
177     $this->gosaMemberApplication["__SEPARATOR__".$i."|".$this->curCatDir]['App'] = "__SEPARATOR__".$i;
178   }
180   function execute()
181   {
182           /* Call parent execute */
183         //plugin::execute();
184     if((isset($_GET['act']))&&($_GET['act']=="depopen")){
185       $dep = base64_decode($_GET['depid']);  
186       if(isset($this->config->idepartments[$dep])){
187         $this->curbase = $dep;
188       }
189     }
191     if(isset($_POST['AddSeperator'])){
192       $this->AddSeperator();
193     }
195     if((isset($_GET['act']))&&($_GET['act']=="open")){
196       $this->curCatDir = $_GET['id'];
198       $found = false;
199       foreach($this->Categories as $key => $name ){
200         if($this->curCatDir==$name){
201           $tmp = array_flip(split("\/",$key));
203           $this->curCatDepth = ($tmp[$this->curCatDir]);
204           $found= true;
205         }
206       }
207       if(!$found){
208         $this->curCatDir = "";
209         $this->curCatDepth = 0;
210       }
211     }
213     /* Do we need to flip is_account state? */
214     if (isset($_POST['modify_state'])){
215       $this->is_account= !$this->is_account;
216     }
218     /* Do we represent a valid group? */
219     if (!$this->is_account && $this->parent == NULL){
220       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
221         _("This 'dn' is no appgroup.")."</b>";
222       return ($display);
223     }
225     /* Show tab dialog headers */
226     $display= "";
227     if ($this->parent != NULL){
228       if ($this->is_account){
229         $display= $this->show_header(_("Remove applications"),
230             _("This group has application features enabled. You can disable them by clicking below."));
231       } else {
232         $display.= $this->show_header(_("Create applications"),
233             _("This group has application features disabled. You can enable them by clicking below."));
234         return ($display);
235       }
236     }
238     $this->reload();
239     foreach($_POST as $name => $value){
240       if(preg_match("/DelApp_/",$name)){
241         $app = $value; 
242         unset($this->used_apps[$app]);
243         unset($this->gosaMemberApplication[$app."|".$this->curCatDir]);
244       }
245       if(preg_match("/EdiApp_/",$name)){
246         $appname = $value;
247         /* We've got the appname, get parameters from ldap */
248         $ldap= $this->config->get_ldap_link();
249         $ldap->cd($this->config->current['BASE']);
250         $ldap->search("(&(objectClass=gosaApplication)(cn=$appname))");
251         if ($ldap->count() != 1){
252           print_red (_("The selected application name is not uniq. Please check your LDAP."));
253         } else {
254           $attrs= $ldap->fetch();
255           if(isset($attrs['gosaApplicationParameter'])){
256             $this->dialog= TRUE;
258             /* Fill name and value arrays */
259             for ($i= 0; $i<$attrs['gosaApplicationParameter']['count']; $i++){
260               $option= preg_replace('/^[^:]+:/', '',
261                   $attrs['gosaApplicationParameter'][$i]);
262               $name= preg_replace('/:.*$/', '', 
263                   $attrs['gosaApplicationParameter'][$i]);
264               $this->option_name[$i]= $name;
266               /* Fill with values from application, default should be
267                  loaded by the external scripts */
268               if (isset($this->appoption[$name])){
269                 $this->option_value[$i]= $this->appoption[$name];
270               }
271             }
273             /* Create edit field */
274             $table= "<table summary=\"\">";
275             for ($i= 0; $i < count($this->option_name); $i++){
276               if (isset($this->option_value[$i])){
277                 $value= $this->option_value[$i];
278               } else {
279                 $value= "";
280               }
281               $table.="<tr><td>".$this->option_name[$i]."</td><td>".
282                 "<input name=\"value$i\" size=60 maxlength=250 ".
283                 "value=\"".$value."\"><br></td></tr>";
284             }
285             $table.= "</table>";
286             $this->table= $table;
287           } else {
288             print_red (_("The selected application has no options."));
289           }
290         }
291       }
292     }
293     $this->reload();
295     /* Add group with post */
296     if((isset($_GET['act']))&&($_GET['act']=="add")){
297       $this->used_apps[$_GET['id']]= $_GET['id'];
298       asort($this->used_apps);
299       $this->addApp($_GET['id']);
300     }
302     /* Add multiple */
303     if(isset($_POST['AddApps'])){
304       foreach($_POST as $name => $value){
305         if(preg_match("/AddApp_/",$name)){
306           $app = preg_replace("/AddApp_/","",$name);
307           $this->addApp($app);
308         }
309       }
310     }
313     /* Cancel edit options? */
314     if (isset($_POST['edit_options_cancel'])){
315       $this->dialog= FALSE;
316     }
318     /* Finish edit options? */
319     if (isset($_POST['edit_options_finish'])){
320       $this->dialog= FALSE;
322       /* Save informations passed by the user */
323       $this->option_value= array();
324       for ($i= 0; $i<count($this->option_name); $i++){
325         $this->appoption[$this->option_name[$i]]= $_POST["value$i"];
326         $this->is_modified= TRUE;
327       }
328     }
330     /* Prepare templating stuff */
331     $smarty= get_smarty();
332     $smarty->assign("used_apps", $this->used_apps);
333     $apps= array();
334     foreach ($this->apps as $key => $value){
335       if (!array_key_exists($key, $this->used_apps)){
336         $apps["$key"]= "$value";
337       }
338     }
340     $div = new DivSelectBox("appgroup");    
342     $div->SetHeight(300);
344     /* NEW LIST MANAGMENT
345      * We also need to search for the departments
346      * So we are able to navigate like in konquerer
347      */
349     $ldap = $this->config->get_ldap_link();
350     $ldap->cd($this->curbase) ;
351     $ldap->ls("(objectClass=gosaDepartment)"); 
352     $departments= array();
353     $tmp = array();
354     while ($value = $ldap->fetch()){
355       $tmp[strtolower($value['dn']).$value['dn']]=$value;
356     }
357     ksort($tmp);
358     foreach($tmp as $value){
359       if($value["description"][0]!=".."){
360         $departments[$value['dn']]=convert_department_dn($value['dn'])." - [".$value["description"][0]."]";
361       }else{
362         $departments[$value['dn']]=convert_department_dn($value['dn']);
363       }
364     }
365     
366     /* END NEW LIST MANAGMENT
367      */
369     $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=depopen&amp;depid=%s'>%s</a>";
370     $linkadd  = "<a href='?plug=".$_GET['plug']."&amp;act=add&amp;id=%s'>%s</a>";
372     $base_back = preg_replace("/^[^,]+,/","",$this->curbase);
373     if((strlen($base_back)>= strlen($this->config->current['BASE']))&&($this->curbase!=$this->config->current['BASE'])){
374       $div->AddEntry(array(
375             array("string"=>sprintf($linkopen,base64_encode($base_back),".. ["._("back")."]"),
376                   "attach"=>"style='border:0px;'")
377             ));
378     }
379     foreach($departments as $key => $app){
380       $div->AddEntry(array(
381                             array("string"=>"<img src='images/folder.png' alt='"._("department")."'>&nbsp;".sprintf($linkopen,base64_encode($key),$app),
382                                   "attach"=>"style='border:0px;'")
383                           ));
384     }
385     
386     foreach($apps as $key => $app){
387       $div->AddEntry(array(
388                             array("string"=>sprintf("<input type='checkbox' value='1' name='AddApp_%s'>",$key).
389                                   "<img src='images/select_application.png' alt='"._("application")."'>&nbsp;".sprintf($linkadd,$key,$app),
390                                   "attach"=>"style='border:0px;'")
391                           ));
392     }
394     if((isset($_GET['act']))&&(($_GET['act'] == "one_up")||($_GET['act']=="one_down"))){
395       if(isset($_GET['id'])){
396         $id   = $_GET['id'];
397         $act  = $_GET['act']; 
398         $found = false;
399         foreach($this->gosaMemberApplication as $key =>  $member){
400           if($id == $member['App']){
401             $found = $key;
402           }
403         }
405         if($found != false){
406           if($act == "one_up"){
407             $this->getOneUp($found);
408           }elseif($act == "one_down")   { 
409             $this->getOneDown($found);
410           }
411         }
412       }
413     }
415     $div2 = new DivSelectBox("appgroup");
416     $div2->SetHeight(300);
418     $menu           = $this->CreateCatMenu();
419     $str_noprio     = " %s ";
420     $linkopen       = "<img src='images/folder.png'>            &nbsp;<a href='?plug=".$_GET['plug']."&amp;act=open&amp;id=%s'>%s</a>";
421     $app   = "<img src='images/select_application.png'>&nbsp;%s".$str_noprio;
423         /* append back entry */
424     if($menu["__BACK__"] != false){
425         $div2 ->AddEntry(array(
426                     array("string"=>sprintf($linkopen,$menu["__BACK__"],".. [ "._("back")." ]")),
427                     array("string"=>"&nbsp;","attach"=>"style='border-right:0px;'")
428                     ));
429     }
431     foreach($menu['__CATEGORY__'] as $path => $name){
432       $div2 ->AddEntry(array(array("string"=>sprintf($linkopen,$path,$name)),array("string"=>"&nbsp;","attach"=>"style='border-right:0px;'"))); 
433     }
435       /* Append entries */
437     $separator ="<hr height=1  size=1></hr>"; 
438  
439     $upudown = "<a href='?plug=".$_GET['plug']."&amp;act=one_up&id=%s'>   <img src='images/sort_up.png' border=0></a>".
440           "&nbsp;<a href='?plug=".$_GET['plug']."&amp;act=one_down&id=%s'> <img src='images/sort_down.png' border=0></a>".
441           "&nbsp;<input type='image' src='images/edittrash.png' name='DelApp_%s' value='%s'>";
442     $edit=      "&nbsp;<input type='image' src='images/edit.png' name='EdiApp_%s' value='%s'>";
443  
444     foreach($menu["__ENTRY__"] as $path => $entry){
446       if(preg_match("/__SEPARATOR__/",$path)){
447         $div2 ->AddEntry(array(array("string"=>$separator),
448                          array("string"=>preg_replace("/\%s/",$entry['name'],$upudown),"attach"=>"style='border-right:0px;'")));
450       }else{
451         $div2 ->AddEntry(array(array("string"=>sprintf($app,$entry['name'],$entry['prio'])),
452                          array("string"=>preg_replace("/\%s/",$entry['name'],$upudown.$edit),"attach"=>"style='border-right:0px;'")));
453       }
454     }
456     $smarty->assign("UsedApps", $div2->DrawList());
457     $smarty->assign("List", $div->DrawList());
458     $smarty->assign("apps", $apps);
460     /* Show main page */
461     if ($this->dialog){
462       $smarty->assign("table", $this->table);
463       $display.= $smarty->fetch (get_template_path('application_options.tpl', TRUE));
464     } else {
465       $display.= $smarty->fetch (get_template_path('application.tpl', TRUE));
466     }
467     return ($display);
468   }
471   function remove_from_parent()
472   {
473     plugin::remove_from_parent();
475     $this->attrs["gosaMemberApplication"]= array();
477     $ldap= $this->config->get_ldap_link();
478     $ldap->cd($this->dn);
479     $ldap->modify($this->attrs);
480     show_ldap_error($ldap->get_error());
482     /* Optionally execute a command after we're done */
483     $this->handle_post_events("remove");
484   }
487   /* Save data to object */
488 #  function save_object()
489 #  {
490 #    plugin::save_object();
491 #  }
494   /* Save to LDAP */
495   function save()
496   {
497     plugin::save();
499     /* Copy members */
500     $this->attrs["gosaMemberApplication"]= array();
501     foreach ($this->gosaMemberApplication as $val){
502       if(!preg_match("/__SEPARATOR__/i",$val['App'])){
503         $this->attrs["gosaMemberApplication"][]= $val['App']."|".$val['Cat']."|".$val['Pri'];
504       }
505     }
507     /* Are there application parameters to be saved */
508     $this->attrs['gosaApplicationParameter']= array();
509     foreach($this->appoption as $name => $value){
510       if ($value != ""){
511         $this->attrs['gosaApplicationParameter'][]= "$name:$value";
512       }
513     }
515     /* Write back to LDAP */
516     $ldap= $this->config->get_ldap_link();
517     $ldap->cd($this->dn);
518     $ldap->modify($this->attrs);
519     show_ldap_error($ldap->get_error());
521     /* Optionally execute a command after we're done */
522     if ($this->initially_was_account == $this->is_account){
523       if ($this->is_modified){
524         $this->handle_post_events("mofify");
525       }
526     } else {
527       $this->handle_post_events("add");
528     }
530   }
532   function check()
533   {
534     $message= array();
535     return ($message);
536   }
539   function reload()
540   {
541     /* Generate applist */
542     $this->apps= array();
543     $ldap= $this->config->get_ldap_link();
544     $ldap->cd ("ou=apps,".$this->curbase);
546     $ldap->ls ("(objectClass=gosaApplication)","ou=apps,".$this->curbase);
547     while ($attrs= $ldap->fetch()){
548       if (isset($attrs["description"][0])){    
549         $this->apps[$attrs["cn"][0]]=
550           $attrs["cn"][0]." (".
551           $attrs["description"][0].")";
552       } else {
553         $this->apps[$attrs["cn"][0]]=
554           $attrs["cn"][0];
555       }
556     }
557     natcasesort ($this->apps);
558     reset ($this->apps);
560     foreach ($this->gosaMemberApplication as $value){
561       $this->used_apps[$value['App']]= $value['App'];
562     }
563   }
574   function CreateCatMenu()
575   {
576     /* The current category */
577     $na = $this->curCatDir;
579     /* the current folder depth */
580     $nd = $this->curCatDepth;
582     /* init array */
583     $return = array();
585     $return["__CATEGORY__"] = array();  // Categories
586     $return["__ENTRY__"]    = array();  // Entries in this category
587     $return["__BACK__"]     = false;    // The back entry
589     $tmp = new xmlParse();
591     $this->Categories= array();
592     if(!isset($this->config->data['MAIN']['KDE_APPLICATIONS_MENU']))    {
593         print_red(_("There is no value for 'KDE_APPLICATIONS_MENU' specified in your gosa.conf."));
594     }else{
595         $path = $this->config->data['MAIN']['KDE_APPLICATIONS_MENU'];
596         if(file_exists($path)){
597             if(is_readable($path)){
598                 $tmp->parseMenu($path);
599                 $this->Categories = $tmp->GetData();
600             }else{
601                 print_red(sprintf(_("The specified file '%s' for 'KDE_APPLICATIONS_MENU' in your gosa.conf is not accessable."),$path));
602             }
603         }else{
604             print_red(sprintf(_("The specified file '%s' for 'KDE_APPLICATIONS_MENU' in your gosa.conf is not accessable."),$path));
605         }
606     }
609     /* Create new categories to return */
610     foreach($this->Categories as $key => $val){
612         /* Split categories by | to get name and priority */
613         $nk = split("\/",$key);
615         /* Check if this category must be shown */
616         if((isset($nk[$nd]))&&(is_array($nk))&&($nk[$nd] == $na)){
618             /* Add this to the list, if theres is a sub category */
619             if(isset($nk[($nd+1)])){
620                 $return["__CATEGORY__"][$nk[($nd+1)]] = $nk[($nd+1)];
621             }
623             /* Create back entry */
624             if($nd !=0 ){
625                 $return['__BACK__'] = $nk[($nd-1)];
626             }else{
627                 $return['__BACK__'] = "..";
628             }
630         }elseif($na==""){
631             $return["__CATEGORY__"][$nk[0]] = $nk[0];
632         }
636     }
638     /* If back entry isn't set, set it to false (none) or to ..(base)*/
639     if($return["__BACK__"] == false){
640         $return['__BACK__'] = "..";
641         if($nd == 0 ){
642             $return['__BACK__'] = false;
643         }
644     }
646     foreach($this->gosaMemberApplication as $app){
647       if($app['Cat'] == $this->curCatDir){
648        $return["__ENTRY__"][$app['Pri']][$app['App']]  =  array("name"=>$app['App'],"prio" => $app['Pri']) ;
649       }
650     }    
653     ksort($return["__ENTRY__"]);
654  /* recreate array index */
655     $tmp = array();
656     foreach($return["__ENTRY__"] as $prio => $entries){
657         ksort($entries);
658         foreach($entries as $entry){
659             $tmp[$entry['name']] = $entry;
660         }
661     }
663     /* Assign sorted entries */
664     $return['__ENTRY__'] = ($tmp);
666     /* Return this all ..*/
667     return($return);
668   }
670   function addApp($cn)
671   {
672     $this->gosaMemberApplication[$cn."|".$this->curCatDir]= array("App"=>$cn,"Cat"=>$this->curCatDir,"Pri"=>$this->getNextFree(1,$this->curCatDir));
673     $this->used_apps[$cn]=$cn;
674     $this->is_modified= TRUE;
675   }
677   function removeApp($cn)
678   {
679     $temp= array();
680     foreach ($this->gosaMemberApplication as $value){
681       if ($value != $cn){
682         $temp[]= $value;
683       }
684     }
685     $this->gosaMemberApplication= $temp;
686     $this->is_modified= TRUE;
687   }
691 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
692 ?>