Code

f16e89cc1e31a17b13a19b2a8ad725c15a638b5d
[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 $departments        = "";       // All departments within $this->curbase;
14   var $apps               = array();  // All available applications
15   var $used_apps          = array();  // Specifies which applications are currently assigned 
17   var $option_name          = array();
18   var $option_value         = array();
19   var $appoption            = array();
21   var $table                  = "";
22   var $curbase                = "";
23   var $curCatDir                  ;
24   var $curCatDepth              =0;         //
26   /* attribute list for save action */
27   var $attributes               = array("gosaMemberApplication","gosaApplicationParameter");
28   var $objectclasses            = array("gosaApplicationGroup");
30   var $FAIrelease               = "/";
31   var $InitialFAIrelease           = "/";
32   var $Releases                 = array();
33   var $enableReleaseManagement  = false;
35   var $AllAppsForRelease        = array();
36   var $AllAppsForReleaseParameter = array();
38   var $CopyPasteVars            = array("Categories");
39   var $gosaApplicationParameter ;
42   function appgroup ($config, $dn= NULL)
43   {
44    
45     /* prepare group app for release management */ 
46     $tmp = search_config($config->data,"faiManagement","CLASS");
47     if(!empty($tmp)){
48       $this->enableReleaseManagement = true;
49       $this->objectclasses [] = "FAIreleaseTag";
50       $this->attributes[] =  "FAIrelease";
51     }
53     plugin::plugin ($config, $dn);
55     /* In some case of old applikations with old release tag saving, we 
56         must reassign is_account state.
57        (Or if release management wasn't activated before creating this app) */
58     if($this->enableReleaseManagement){
59   
60       /* Release management is activated && this is a valid group app account,
61           but no release class was found, so activate is_account flag  */
62       if($dn != "new" && ( in_array("gosaApplicationGroup",$this->attrs['objectClass'])) && 
63          (!in_array("FAIreleaseTag",$this->attrs['objectClass']))){
64         $this->is_account =true;
65       }
66     }
67     
69     /* Load member applications */
70     if (isset ($this->attrs["gosaMemberApplication"][0])){
71       $this->gosaMemberApplication = array();
72       for ($i= 0; $i<$this->attrs["gosaMemberApplication"]["count"]; $i++){
73         $this->gosaMemberApplication[]=
74           $this->attrs["gosaMemberApplication"][$i];
75       }
76     }
78     /* Load application options */
79     if (isset($this->attrs['gosaApplicationParameter'])){
80       for ($i= 0; $i<$this->attrs['gosaApplicationParameter']['count']; $i++){
81         $option= preg_replace('/^[^:]+:/', '',
82             $this->attrs['gosaApplicationParameter'][$i]);
83         $name= preg_replace('/:.*$/', '',
84             $this->attrs['gosaApplicationParameter'][$i]);
85         $this->appoption[$name]= $option;
86       }
87     }
89     /* Parse MemberApplication*/
90     $tmp    = array();
91     $tmp2   = array();
92     $prios  = array();
93     $cats   = array();
95     foreach($this->gosaMemberApplication as $memberApp){
96       if(preg_match("/\|/i",$memberApp)){
97     
98         $tmp = split("\|",$memberApp);
99  
100         if(!empty($tmp[0])){
101           $tmp2[$tmp[1]][$tmp[2]] = array("App"=>$tmp[0]);
102         }
103         if(!empty($tmp[1])){
104           $n = split("/",$tmp[1]);
105           $c = count($n);
106           $cats [$tmp[1]] = $n[$c-1];
107         }
108         $prios[$tmp[1]][$tmp[2]] = $tmp[2];
109       }else{
110         $tmp2[]['App'] = $memberApp;
111       }
112     }
114     $this->Categories = $cats;
116     $this->gosaMemberApplication = $tmp2;
117     $cats[""]="";
118     foreach($cats as $cat ){
119       if((isset($prios[$cat]))&&(count($prios[$cat]))){
120         $max = max($prios[$cat]);
121         $min = 1;//min($prios[$cat]);  
122         $last = false;
123         for($i = $min ; $i < $max ; $i++){
124           if(!isset($prios[$cat][$i])){
125             if($last == false){
126               $this->gosaMemberApplication[$cat][$i]['App'] = "__SEPARATOR__".$i;
127             
128               $last = true;
129             }
130           }else{
131             $last = false;
132           }
133         }
134       }
135     }
137     $tmp = array();
138     foreach($this->gosaMemberApplication as $key =>  $entries){
139       ksort ($entries);
140       foreach($entries as $entry){
141         $tmp[$key][]= $entry;
142       }
143     }
144     $this->gosaMemberApplication = $tmp;
145     $this->curbase = $this->config->current['BASE'];
146     
147     /* Get required release informations */
148     if($this->enableReleaseManagement){
149       $this->InitialFAIrelease = $this->FAIrelease;
150       $this->Releases       = $this->getReleases();
151     }
152   }
155   /* Combine new array */
156   function combineArrays($ar0,$ar1,$ar2)
157   {
158     $ret = array();
159     if(is_array($ar0))
160     foreach($ar0 as $ar => $a){
161         $ret[$ar]=$a;
162     }
163     if(is_array($ar1))
164     foreach($ar1 as $ar => $a){
165         $ret[$ar]=$a;
166     }
167     if(is_array($ar2))
168     foreach($ar2 as $ar => $a){
169         $ret[$ar]=$a;
170     }
171     return($ret);
172   }
174   function getpos($atr,$attrs)
175   {
176     $i = 0;
177     foreach($attrs as $attr => $name)    {
178       $i++;
179       if($attr == $atr){
180         return($i);
181       }
182     }
183     return(-1);
184   }
186   function diffAppsInReleases()
187   {
188     /* Only diff if it is required 
189      */
190     $ret =array();
192     /* If current release has changed */
193     if($this->FAIrelease != $this->InitialFAIrelease){
195       /* Walk through all apps which are currently displayed */
196       if(isset($this->gosaMemberApplication[$this->curCatDir])){
198         foreach($this->gosaMemberApplication[$this->curCatDir] as $entry){
200           /* If application is also available in new release, check if parameter differs */
201           if(in_array($entry['App'],$this->AllAppsForRelease)){ 
203             $old = array();   // Old application parameter
204             $new = array();   // New parameters 
206             /* There are possibly no parameters specified */
207             if(isset($this->AllAppsForReleaseParameter[$this->InitialFAIrelease][$entry['App']])){
208               $old = $this->AllAppsForReleaseParameter[$this->InitialFAIrelease][$entry['App']];
209             }
211             if(isset($this->AllAppsForReleaseParameter[$this->FAIrelease][$entry['App']])){
212               $new = $this->AllAppsForReleaseParameter[$this->FAIrelease][$entry['App']];
213             }
215             /*  Both (old & new) have no gosaApplicationParameter
216              */
217             if((!isset($old['gosaApplicationParameter'])) && (!isset($new['gosaApplicationParameter']))){
218               $ret[$entry['App']] = false; 
220               /* Both got gosaApplicationParameter
221                */
222             }elseif((isset($old['gosaApplicationParameter'])) && (isset($new['gosaApplicationParameter']))){
223               if(array_differs($old['gosaApplicationParameter'],$new['gosaApplicationParameter'])){
224                 $ret[$entry['App']]  = true;
225               }else{
226                 $ret[$entry['App']]  = false;
227               }
228             }
229             /* Initialy had gosaApplicationParameter bot in new release not 
230              */
231           }elseif((isset($old['gosaApplicationParameter'])) && (!isset($new['gosaApplicationParameter']))){
232             $ret[$entry['App']]  = true;
234             /* Old release had no gosaApplicationParameter but new got some
235              */
236           }elseif((!isset($old['gosaApplicationParameter'])) && (isset($new['gosaApplicationParameter']))){
237             $ret[$entry['App']]  = true;
238           }
239         }
240       }
241     }
242     return($ret);
243   }
245   /* TRansports the geiven Arraykey one position up*/
246   function ArrayUp($atr,$attrs)
247   {
248     $ret = $attrs;
249     $pos = $this->getpos($atr,$attrs) ;
250     $cn = count($attrs);
251     if(!(($pos == -1)||($pos == 1))){
252       $before = array_slice($attrs,0,($pos-2));
253       $mitte  = array_reverse(array_slice($attrs,($pos-2),2));
254       $unten  = array_slice($attrs,$pos);
255       $ret = array();
256       $ret = $this->combineArrays($before,$mitte,$unten);
257     }
258     return($ret);
259   }
262   /* TRansports the geiven Arraykey one position up*/
263   function ArrayDown($atr,$attrs)
264   {
265     $ret = $attrs;
266     $pos = $this->getpos($atr,$attrs) ;
267     $cn = count($attrs);
268     if(!(($pos == -1)||($pos == $cn))){
269       $before = array_slice($attrs,0,($pos-1));
270       $mitte  = array_reverse(array_slice($attrs,($pos-1),2));
271       $unten  = array_slice($attrs,($pos+1));
272       $ret = array();
273       $ret = $this->combineArrays($before,$mitte,$unten);
274     }
275     return($ret);
276   }
279   function catUp($id)
280   {
281     /* Get all cats depinding on current dir */
282     $cats = $this->GetSubdirs($this->curCatDir);
283     $newcats =$this->ArrayUp($id,$cats);
285     foreach($newcats as $cat => $name){
286       unset($this->Categories[$cat]);
287     }
288     foreach($newcats as $cat => $name){
289       $this->Categories[$cat]=$name;
290     }
291   }
294   function catDown($id)
295   {
296     /* Get all cats depinding on current dir */
297     $cats = $this->GetSubdirs($this->curCatDir);
299     $newcats =$this->ArrayDown($id,$cats);
301     foreach($newcats as $cat => $name){
302       unset($this->Categories[$cat]);
303     }
304     foreach($newcats as $cat => $name){
305       $this->Categories[$cat]=$name;
306     }
307   }
310   function getOneUp($appl)
311   { 
312     $cat  = $this->curCatDir;
313     $apps = $this->gosaMemberApplication[$cat];
315     $appsA = array();
316     foreach ($apps as $appkey => $name){
317       $appsA[$name['App']] =$name['App'];
318     }
320     $result = $this->ArrayUp($appl,$appsA);
322     $ret = array();
323     foreach($result as $app){
324       $ret[]=array("App"=>$app);
325     }
326     $this->gosaMemberApplication[$cat] = $ret;
327   }
330   function getOneDown($appl)
331   {
332     $cat  = $this->curCatDir;
333     $apps = $this->gosaMemberApplication[$cat];
335     $appsA = array();
336     foreach ($apps as $appkey => $name){
337       $appsA[$name['App']] =$name['App'];
338     }
340     $result = $this->ArrayDown($appl,$appsA);
342     $ret = array();
343     foreach($result as $app){
344       $ret[]=array("App"=>$app);
345     }
346     $this->gosaMemberApplication[$cat] = $ret;
347   } 
351   function AddSeperator($id)
352   {
353     $found  = false;
354     $cat    = "";
355     $tmp = array();
356     foreach($this->gosaMemberApplication[$this->curCatDir] as $appID => $app){  
357       $tmp[] = $app;    
358       if(($app['App'] == $id)&&(!$found)){
359         $cnt = count($this->gosaMemberApplication[$this->curCatDir]);
360         $tmp[] = array("App" => "__SEPARATOR__".($cnt+1));
361         $found = true;
362       }
363     }
364     if($found){
365       $this->gosaMemberApplication[$this->curCatDir]=$tmp;
366     }
367   }
369   function execute()
370   {
371     /* Call parent execute */
372     plugin::execute();
374     if((isset($_GET['act']))&&($_GET['act']=="depopen")){
375       $dep = base64_decode($_GET['depid']);  
376       if(isset($this->config->idepartments[$dep])){
377         $this->curbase =$dep;
378       }
379     }
381     if((isset($_GET['act']))&&($_GET['act']=="open")){
382       $this->curCatDir = base64_decode($_GET['id']);
383     }
385     /* Do we need to flip is_account state? */
386     if (isset($_POST['modify_state'])){
387       $this->is_account= !$this->is_account;
388     }
390     /* Do we represent a valid group? */
391     if (!$this->is_account && $this->parent == NULL){
392       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
393         _("This 'dn' is no appgroup.")."</b>";
394       return ($display);
395     }
397     /* Show tab dialog headers */
398     $display= "";
399     if ($this->parent != NULL){
400       if ($this->is_account){
401         $display= $this->show_disable_header(_("Remove applications"),
402             _("This group has application features enabled. You can disable them by clicking below."));
403       } else {
404         $display.= $this->show_enable_header(_("Create applications"),
405             _("This group has application features disabled. You can enable them by clicking below."));
406         return ($display);
407       }
408     }
411     /* Add Categorie */ 
414     if((isset($_POST['AddCat']))&&(isset($_POST['CatName']))&&(!empty($_POST['CatName']))){
416       if(preg_match("/[\\\\\/]/i",$_POST['CatName'])){
417         print_red(_("Invalid character in category name."));
418       }elseif(!in_array($_POST['CatName'],$this->Categories)){ 
419         if(empty($this->curCatDir)){
420           $this->Categories[$_POST['CatName']]=$_POST['CatName'];
421         }else{
422           $this->Categories[$this->curCatDir."/".$_POST['CatName']]=$_POST['CatName'];
423         }
424       }else{
425         print_red(_("The specified category already exists."));
426       }
427     }
430     $this->reload();
432     $this->diffAppsInReleases();
433     $only_once = false;
434     foreach($_POST as $name => $value){
435       
436       if((preg_match("/AddSep_/",$name))&&(!$only_once)){
437         $only_once = true;
438         $n = preg_replace("/AddSep_/","",$name);
439         $val= preg_replace("/_.*$/","",$n);
440         $this->AddSeperator($val);
441       }
443       if((preg_match("/DelApp_/",$name))&&(!$only_once)){
444         $only_once = true;
445    
447         if(preg_match("/DelApp___SEPARATOR__/",$name)) {
448           $n=  preg_replace("/DelApp___SEPARATOR__/","",$name);
449           $val= "__SEPARATOR__".preg_replace("/_.*$/","",$n);
450         }else{
451           $n = preg_replace("/DelApp_/","",$name);
452           $val= preg_replace("/_.*$/","",$n);
453         }
455         foreach($this->gosaMemberApplication as $key =>  $cat){
456           foreach($cat as $key2 => $app){
457             
458             if($app['App'] == $val){
459               unset($this->gosaMemberApplication[$key][$key2]);
460               if(isset($this->used_apps[$val])){
461                 foreach($this->getParameters($val) as $para){
462                   if(isset($this->appoption[$para])){
463                     unset($this->appoption[$para]);
464                   }
465                 }
466                 unset($this->used_apps[$val]);
467               }
468             }
469           }
470         }
471       }
472   
473       if(preg_match("/DelCat_/",$name)){
474         $n = preg_replace("/DelCat_/","",$name);
475         $app = base64_decode( preg_replace("/_.*$/","",$n));
476         foreach($this->Categories as $key =>  $cat){
477           if($cat == $app){
478             foreach($this->Categories as $p => $n){
479               if(preg_match("/^".$key."\/.*/",$p)){
480                 unset($this->Categories[$p]);    
481               }
482             }
483             unset($this->Categories[$key]);
484           }
485         }
486       }
487       
488       if((preg_match("/EdiApp_/",$name))&&(!$only_once)){
490         $only_once = true;
491         $appname = $value;
492         $appname = preg_replace("/EdiApp_/","",$name);  
493         $appname = preg_replace("/_.*$/","",$appname);
495         /* We've got the appname, get parameters from ldap 
496          */
497         $ldap= $this->config->get_ldap_link();
499         $tmp = search_config($this->config->data,"faiManagement","CLASS");
500         if(!empty($tmp)){
501           $tmp = array_flip($this->Releases);
502           $base = $tmp[$this->FAIrelease];
504           $ldap->cd($this->config->current['BASE']);
505           $ldap->search("(&(objectClass=gosaApplication)(cn=$appname))",array("gosaApplicationParameter"));
506           $found = "";
507           while($attrs = $ldap->fetch()) {
508             if(preg_match("/cn=".$appname.",".$base."/",$attrs['dn'])){
509               $found = $attrs['dn'];
510             }
511           }
512           $ldap->cat($found, array("gosaApplicationParameter"));
513         }else{
514           $ldap->cd($this->config->current['BASE']);
515           $ldap->search("(&(objectClass=gosaApplication)(cn=$appname))",array("gosaApplicationParameter"));
516         }
519         if ($ldap->count() != 1){
520           print_red (_("The selected application name is not uniq. Please check your LDAP."));
521         } else {
522           $attrs= $ldap->fetch();
523           if(isset($attrs['gosaApplicationParameter'])){
524             $this->dialog= TRUE;
526             /* Fill name and value arrays */
527             for ($i= 0; $i<$attrs['gosaApplicationParameter']['count']; $i++){
528               $option= preg_replace('/^[^:]+:/', '',
529                   $attrs['gosaApplicationParameter'][$i]);
530               $name= preg_replace('/:.*$/', '', 
531                   $attrs['gosaApplicationParameter'][$i]);
532               $this->option_name[$i]= $name;
534               /* Fill with values from application, default should be
535                  loaded by the external scripts */
536               if (isset($this->appoption[$name])){
537                 $this->option_value[$i]= $this->appoption[$name];
538               }
539             }
541             /* Create edit field */
542             $table= "<table summary=\"\">";
543             for ($i= 0; $i < count($this->option_name); $i++){
544               if (isset($this->option_value[$i])){
545                 $value= $this->option_value[$i];
546               } else {
547                 $value= "";
548               }
549               $table.="<tr><td>".$this->option_name[$i]."</td><td>".
550                 "<input name=\"value$i\" size=60 maxlength=250 ".
551                 "value=\"".$value."\"><br></td></tr>";
552             }
553             $table.= "</table>";
554             $this->table= $table;
555           } else {
556             print_red (_("The selected application has no options."));
557           }
558         }
559       }
560     }
562     $this->reload();
564     /* Add group with post */
565     if((isset($_GET['act']))&&($_GET['act']=="add")){
566       $this->used_apps[$_GET['id']]= $_GET['id'];
567       asort($this->used_apps);
568       $this->addApp($_GET['id']);
569     }
571     /* Add multiple */
572     if(isset($_POST['AddApps'])){
573       foreach($_POST as $name => $value){
574         if(preg_match("/AddApp_/",$name)){
575           $app = preg_replace("/AddApp_/","",$name);
576           $this->addApp($app);
577         }
578       }
579     }
582     /* Cancel edit options? */
583     if (isset($_POST['edit_options_cancel'])){
584       $this->dialog= FALSE;
585     }
587     /* Finish edit options? */
588     if (isset($_POST['edit_options_finish'])){
589       $this->dialog= FALSE;
591       /* Save informations passed by the user */
592       $this->option_value= array();
593       for ($i= 0; $i<count($this->option_name); $i++){
594         $this->appoption[$this->option_name[$i]]= $_POST["value$i"];
595         $this->is_modified= TRUE;
596       }
597     }
599     /* Prepare templating stuff */
600     $smarty= get_smarty();
601     $smarty->assign("used_apps", $this->used_apps);
602     $apps= array();
603     foreach ($this->apps as $key => $value){
604       if (!array_key_exists($key, $this->used_apps)){
605         $apps["$key"]= "$value";
606       }
607     }
609     $div = new DivSelectBox("appgroup");    
611     $div->SetHeight(300);
613     /* get departments */  
614     $ui = get_userinfo();
615     $departments = array();
616     $res = get_list("(objectClass=gosaDepartment)", "department", $this->curbase,array("description","cn","ou"),GL_SIZELIMIT);
617     foreach($res as $value){
618       $fdn= @LDAP::fix($value['dn']);
619       if($value["description"][0]!=".."){
620         $departments[$value['dn']]= convert_department_dn($fdn)." - [".$value["description"][0]."]";
621       }else{
622         $departments[$value['dn']]=convert_department_dn($fdn);
623       }
624     }
626     $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=depopen&amp;depid=%s'>%s</a>";
627     $linkadd  = "<a href='?plug=".$_GET['plug']."&amp;act=add&amp;id=%s'>%s</a>";
629     $base_back = preg_replace("/^[^,]+,/","",$this->curbase);
630     if((strlen($base_back)>= strlen($this->config->current['BASE']))&&($this->curbase!=$this->config->current['BASE'])){
631       $div->AddEntry(array(
632             array("string"=>sprintf($linkopen,base64_encode($base_back),".. ["._("back")."]"),
633               "attach"=>"style='border:0px;'")
634             ));
635     }
636     foreach($departments as $key => $app){
637       $div->AddEntry(array(
638             array("string"=>"<img class='center' src='images/folder.png' alt='"._("department")."'>&nbsp;".sprintf($linkopen,base64_encode($key),$app),
639               "attach"=>"style='border:0px;'")
640             ));
641     }
643     foreach($apps as $key => $app){
644       $div->AddEntry(array(
645             array("string"=>sprintf("<input class='center' type='checkbox' value='1' name='AddApp_%s'>",$key).
646               "<img class='center' src='images/select_application.png' alt='"._("application")."'>&nbsp;".sprintf($linkadd,$key,$app),
647               "attach"=>"style='border:0px;'")
648             ));
649     }
651     if((isset($_GET['act']))&&(($_GET['act'] == "cat_up")||($_GET['act']=="cat_down"))){
652       if($_GET['act']=="cat_up"){
653         $this->catUp(base64_decode($_GET['id']));
654       }
655       if($_GET['act']=="cat_down"){
656         $this->catDown(base64_decode($_GET['id']));
657       }
658     }
660     if((isset($_GET['act']))&&(($_GET['act'] == "one_up")||($_GET['act']=="one_down"))){
661       if(isset($_GET['id'])){
662         $id   = $_GET['id'];
663         $act  = $_GET['act']; 
665         if($act == "one_up"){
666           $this->getOneUp($id);
667         }elseif($act == "one_down")   { 
668           $this->getOneDown($id);
669         }
670       }
671     }
673     $div2 = new DivSelectBox("appgroup");
674     $div2->SetHeight(300);
676     $linkopen       = "<img class='center' src='images/folder.png' alt=\"\">&nbsp;<a href='?plug=".$_GET['plug']."&amp;act=open&amp;id=%s'>%s</a>";
677     $catremove      = "&nbsp;<input type='image' src='images/edittrash.png' title='"._("Delete entry")."' name='DelCat_%s' value='%s'>";
678     $app            = "<img class='center' src='images/select_application.png' alt=\"\">&nbsp;%s";
679     
680     $catupdown        = "<a href='?plug=".$_GET['plug']."&amp;act=cat_up&amp;id=%s'>
681                        <img align='top' alt=\"\" src='images/sort_up.png' border=0 title='"._("Move up")."'></a>&nbsp;<a href='?plug=".$_GET['plug']."&amp;act=cat_down&amp;id=%s'> 
682                        <img alt=\"\" src='images/sort_down.png' title='"._("Move down")."' border=0></a>";
684     if(empty($this->curCatDir)){
685       $cnt =0;
686     }else{
687       $cnt = count(split("/",$this->curCatDir));
688       $tmp = split("/",$this->curCatDir);
689       $bbk = "";
690       for($i = 0 ; $i < ($cnt -1 ) ; $i++){
691         $bbk .= $tmp[$i];
692       }
693       $div2 ->AddEntry(array(array("string"=>sprintf($linkopen,base64_encode($bbk),"..")),array("string"=>"&nbsp;","attach"=>"style='border-right:0px;'")));
694     }
696     $this->GetSubdirs($this->curCatDir);
698     foreach($this->GetSubdirs($this->curCatDir) as $path => $name){
699       $div2 ->AddEntry(array( 
700             array("string"=>sprintf($linkopen,base64_encode($path),$name)),
701             array("string"=>preg_replace("/%s/",base64_encode($path),$catupdown.$catremove),
702               "attach"=>"align='right' style='width:80px;border-right:0px;'"))); 
703     }
705     /* Append entries */
707     $separator ="<hr size=1>"; 
709     $sep = "<input type='image' src='images/back.png' title='"._("Insert seperator")."' value='%s' name='AddSep_%s'>";
710   
711     $upudown ="<a href='?plug=".$_GET['plug']."&amp;act=one_up&amp;id=%s'>   <img alt='{t}sort{/t}' align='top' src='images/sort_up.png' title='"._("Move up")."' border=0></a>".
712       "&nbsp;<a href='?plug=".$_GET['plug']."&amp;act=one_down&amp;id=%s'> <img alt='{t}sort{/t}' src='images/sort_down.png' title='"._("Move down")."' border=0></a>".
713       "&nbsp;<input type='image' src='images/edittrash.png' title='"._("Delete entry")."' name='DelApp_%s' value='%s' alt='{t}delete{/t}' >";
714     $edit=      "&nbsp;<input type='image' src='images/edit.png' title='"._("Edit entry")."' name='EdiApp_%s' value='%s' alt='{t}edit{/t}' >";
716     $Differences = $this->diffAppsInReleases();
718     if(isset($this->gosaMemberApplication[$this->curCatDir])){
719       foreach($this->gosaMemberApplication[$this->curCatDir] as $cat => $entry){
721         
722         if(preg_match("/__SEPARATOR__/",$entry['App'])){
723           $div2 ->AddEntry(array(array("string"=>$separator),
724                 array("string"=>preg_replace("/\%s/",htmlentities($entry['App']),$upudown),"attach"=>"align='right' style='border-right:0px;'")));
725         }else{
727           $image = "";
728           /* Check if application was available within selected release
729            *  or application list if release management is not active
730            */
731           if(!in_array($entry['App'],$this->AllAppsForRelease)){
733             /*  release managent is active
734              */
735             if(!$this->enableReleaseManagement){
736               $image = "<img class='center' src='images/select_invalid_application.png' 
737                           title='"._("This application is no longer available.")."'>&nbsp;";
738             }else{
739               $image = "<img class='center' src='images/select_invalid_application.png' 
740                           title='".sprintf(_("This application is not available in any release named %s."),$this->FAIrelease)."'>&nbsp;";
741             }
742       
743           }elseif(isset($Differences[$entry['App']]) && ($Differences[$entry['App']] == true)) {
744             $entry['App'].=" <i>["._("Check parameter")."]</i>";
745               $image = "<img class='center' src='images/select_invalid_application.png' 
746                           title='"._("This application has changed parameters.")."'>&nbsp;";
747           }else{
748             $image = "<img class='center' src='images/select_application.png' alt=\"\">&nbsp;"; 
749           }
750           
751           $div2->AddEntry(array(array("string"=>sprintf($image."%s",$entry['App'])),
752                 array("string"=>preg_replace("/\%s/",htmlentities($entry['App']),$sep.$edit.$upudown),
753                       "attach"=>"align='right' style='width:100px;border-right:0px;'")));
754         }
755       }
756     }
758     $smarty->assign("UsedApps", $div2->DrawList());
759     $smarty->assign("List", $div->DrawList());
760     $smarty->assign("apps", $apps);
761    
762     $smarty->assign("enableReleaseManagement",$this->enableReleaseManagement);
763  
764     if($this->enableReleaseManagement){
765       $smarty->assign("FAIrelease",  $this->FAIrelease);
766       $smarty->assign("Releases", $this->Releases);
768       if(count($this->used_apps)){
769         $smarty->assign("ReleaseSelectAble", false);
770       }else{
771         $smarty->assign("ReleaseSelectAble", true);
772       }
773     }
774         $smarty->assign("ReleaseSelectAble", true);
776     /* Show main page */
777     if ($this->dialog){
778       $smarty->assign("table", $this->table);
779       $display.= $smarty->fetch (get_template_path('application_options.tpl', TRUE));
780     } else {
781       $display.= $smarty->fetch (get_template_path('application.tpl', TRUE));
782     }
783     return ($display);
784   }
787   function getReleases()
788   {
789     $dn   = $this->config->current['BASE'];
790     $ldap = $this->config->get_ldap_link();
791     $ldap->cd($dn);
792     $ldap->search("(&(objectClass=organizationalUnit)(objectClass=FAIbranch))",array("ou"));
793     $ret =array();
794     while($attrs = $ldap->fetch()){
795       if(preg_match("/ou=apps,/",$attrs['dn'])){
796         $bb = preg_replace("/ou=apps,.*/","",$attrs['dn']);
797         $parts = array_reverse(split("ou=",$bb));
799         $str ="";
800         foreach($parts as $part){
801           if(empty($part)) {
802             continue;
803           }
804           $str .= str_replace(",","",$part)."/";
805         }    
806         $name = preg_replace("/\/$/","",$str);
807         if(empty($name)) {
808           $name ="/";
809         }
810         $ret[$attrs['dn']] = $name;
811       }
812     }
813     return($ret);
814   }
816   function save_object()
817   {
818     plugin::save_object();
819   }
820  
822   function remove_from_parent()
823   {
824     plugin::remove_from_parent();
826     $ldap= $this->config->get_ldap_link();
827     $ldap->cd($this->dn);
828     $this->cleanup();
829     
830     $ldap->modify ($this->attrs); 
832     show_ldap_error($ldap->get_error(), sprintf(_("Removing of groups/applications with dn '%s' failed."),$this->dn));
834     /* Optionally execute a command after we're done */
835     $this->handle_post_events("remove");
836   }
839   /* Save to LDAP */
840   function save()
841   {
842     plugin::save();
844     /* Copy members */
845     $this->Categories[""]=""; 
846     $this->attrs["gosaMemberApplication"]= array();
847     $this->attrs["gosaApplicationParameter"]= array();
848     foreach($this->Categories as $name => $cats){
849       $i =0;
850       if(isset($this->gosaMemberApplication[$name])){
851         foreach($this->gosaMemberApplication[$name] as $entry){
852           if(!preg_match("/__SEPARATOR__/",$entry['App'])){
853             $this->attrs["gosaMemberApplication"][]= $entry['App']."|".$name."|".$i;
854           }
855           $i ++;
856         }
857       }
858       if(($i==0)&&(!empty($name))){
859         $this->attrs["gosaMemberApplication"][]= "|".$name."|".$i;
860       }
861     }
863     /* Are there application parameters to be saved */
864     $this->attrs['gosaApplicationParameter']= array();
865     foreach($this->appoption as $name => $value){
866       if ($value != ""){
867         $this->attrs['gosaApplicationParameter'][]= "$name:$value";
868       }
869     }
871     /* Write back to LDAP */
872     $ldap= $this->config->get_ldap_link();
873     $ldap->cd($this->dn);
874     $this->cleanup();
875     $ldap->modify ($this->attrs); 
877     show_ldap_error($ldap->get_error(), sprintf(_("Saving of groups/applications with dn '%s' failed."),$this->dn));
879     /* Optionally execute a command after we're done */
880     if ($this->initially_was_account == $this->is_account){
881       if ($this->is_modified){
882         $this->handle_post_events("mofify");
883       }
884     } else {
885       $this->handle_post_events("add");
886     }
888   }
890   function check()
891   {
892     /* Call common method to give check the hook */
893     $message= plugin::check();
895     return ($message);
896   }
899   function reload()
900   {
901     /* Generate applist */
902     $this->apps= array();
904     /* Special handling for release managed apps 
905      */
906     $tmp = search_config($this->config->data,"faiManagement","CLASS");
907     if(!empty($tmp)){
908       $this->enableReleaseManagement = true;
910       $tmp = array_flip($this->Releases);
911       if(isset($tmp[$this->FAIrelease])){
912         $base =  $tmp[$this->FAIrelease];
913       }else{
914         $k = key($tmp);
915         $r = $tmp[$k];
916         $this->FAIrelease = $k;
917         $base = $r; 
918     
919         print_red(sprintf(_("Can't resolve the release name '%s', setting release name to '%s'. Possibly the objects base has changed."),$this->FAIrelease,$k));
920       }
922       $base = preg_replace("/ou=apps,.*$/","ou=apps,".$this->curbase,$base);
923     
924       $ldap   = $this->config->get_ldap_link();
925       $ldap->cd ($base);
926       $ldap->ls ("(objectClass=gosaApplication)",$base);
927     }else{
928       $ldap   = $this->config->get_ldap_link();
929       $ldap->cd ("ou=apps,".$this->curbase);
930       $ldap->ls ("(objectClass=gosaApplication)","ou=apps,".$this->curbase);
931     }
932     
933     while ($attrs= $ldap->fetch()){
934       if (isset($attrs["description"][0])){    
935         $this->apps[$attrs["cn"][0]]=
936           $attrs["cn"][0]." (".
937           $attrs["description"][0].")";
938       } else {
939         $this->apps[$attrs["cn"][0]]=
940           $attrs["cn"][0];
941       }
942     }
944     $ldap->cd($this->config->current['BASE']);
945     $ldap->search("objectClass=gosaApplication",array("gosaApplicationParameter","cn"));
946     $tmp = search_config($this->config->data,"faiManagement","CLASS");
947     $this->AllAppsForRelease = array();
948     if(!empty($tmp)){
950       $tmp = array_flip($this->Releases);
952       while($attrs = $ldap->fetch()){
953         
954         $testdn = preg_replace("/ou=apps,.*$/","ou=apps",$attrs['dn']);
955         $testdn = preg_replace("/^[^,]+/","",$testdn);
956         $testdn = preg_replace("/^,/","",$testdn);
958         if($testdn == preg_replace("/ou=apps,.*$/","ou=apps",$tmp[$this->FAIrelease])){
959           $this->AllAppsForRelease[$attrs['dn']] = $attrs['cn'][0];
960           $this->AllAppsForReleaseParameter[$this->FAIrelease][$attrs['cn'][0]] = $attrs;
961         }
962       }
963     }else{
964       while($attrs = $ldap->fetch()){
965         $this->AllAppsForRelease[$attrs['dn']] = $attrs['cn'][0];
966       }
967     }
969     natcasesort ($this->apps);
970     reset ($this->apps);
972     if(is_array($this->gosaMemberApplication))
973       foreach ($this->gosaMemberApplication as $cat){   
974         if(is_array($cat))
975           foreach($cat as $entry){
976             $this->used_apps[$entry['App']]= $entry['App'];
977           }
978       }
979   }
982   function addApp($cn)
983   {
984     if((isset($this->gosaMemberApplication[$this->curCatDir]))&&(is_array($this->gosaMemberApplication[$this->curCatDir]))){
985       foreach($this->gosaMemberApplication[$this->curCatDir] as $entry){
986         if($entry['App'] == $cn) return;
987       }
988     }
989     $this->gosaMemberApplication[$this->curCatDir][]= array("App"=>$cn);
990     $this->used_apps[$cn]=$cn;
991     $this->is_modified= TRUE;
992   }
995   function removeApp($cn)
996   {
997     $temp= array();
998     foreach ($this->gosaMemberApplication as $value){
999       if ($value != $cn){
1000         $temp[]= $value;
1001       }
1002     }
1003     $this->gosaMemberApplication= $temp;
1004     $this->is_modified= TRUE;
1005   }
1007   function getParameters($app)
1008   {
1009     $tmp = $this->getReleases();
1010     $ret = array();
1011     if(in_array($this->FAIrelease,$tmp)){
1012       $tmp2 = array_flip($tmp);
1013       $base = $tmp2[$this->FAIrelease];
1014       $ldap = $this->config->get_ldap_link();
1015       $ldap->cd($this->config->current['BASE']);
1016       $ldap->search("(&(objectClass=gosaApplication)(cn=".$app.")(gosaApplicationParameter=*))",array("gosaApplicationParameter"));
1017       if($ldap->count()){
1018         $attrs = $ldap->fetch();
1019         for($i = 0 ; $i < $attrs['gosaApplicationParameter']['count'] ; $i ++ ){
1020           $name = preg_replace("/:.*$/","",$attrs['gosaApplicationParameter'][$i]);
1021           $ret[$name] = $name;
1022         } 
1023       }
1024     }
1025     return($ret);
1026   }
1028   function GetSubdirs($dir)
1029   {
1030     $ret = array();
1031     $tmp1 = split("/",$this->curCatDir);
1032  
1033     foreach($this->Categories as $path=>$cat){
1034       $tmp2 = split("/",$path);
1035       
1036       if((empty($this->curCatDir))&&(!preg_match("/\//",$path))){
1037         $abort = false;
1038       }elseif(((count($tmp1))+1) == (count($tmp2))){
1039         $abort = false;
1040         for($i = 0 ; $i < count($tmp1) ; $i++){
1041           if($tmp1[$i] != $tmp2[$i]){
1042             $abort = true;
1043           }
1044         }
1045       }else{
1046         $abort= true;
1047       }
1048       if(!$abort){
1049         $ret[$path]=$cat;
1050       } 
1051     }
1052     return($ret);
1053   }
1056   /* Return plugin informations for acl handling  */ 
1057   function plInfo()
1058   {
1059      function plInfo()
1060   {
1061     return (array(
1062           "plShortName"   => _("Applications"),
1063           "plDescription" => _("Group applications"),
1064           "plSelfModify"  => FALSE,
1065           "plDepends"     => array(),
1066           "plPriority"    => 0,
1067           "plSection"     => array("administration"),
1068           "plCategory"    => array("groups"),
1069           "plProvidedAcls"=> array(
1070             "gosaMemberApplication"     => _("Application"),
1071             "gosaApplicationParameter"  => _("Application parameter"))
1072           ));
1073   }
1077   }
1079 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1080 ?>