Code

a70248b0a5693e5c9ce0ab72d8416c81f78b8323
[gosa.git] / plugins / admin / groups / class_groupApplication.inc
1 <?php
2 class appgroup extends plugin
3 {
4   /* Appgroup attributes */
5   var $gosaMemberApplication= array();
7   /* Helpers */
8   var $departments        = "";       // All departments within $this->curbase;
9   var $apps               = array();  // All available applications
10   var $used_apps          = array();  // Specifies which applications are currently assigned 
12   var $option_name          = array();
13   var $option_value         = array();
14   var $appoption            = array();
16   var $table                  = "";
17   var $curbase                = "";
18   var $curCatDir                  ;
19   var $curCatDepth              =0;         //
20   var $Categories;
22   /* attribute list for save action */
23   var $attributes               = array("gosaMemberApplication","gosaApplicationParameter");
24   var $objectclasses            = array("gosaApplicationGroup");
26   var $FAIrelease               = "/";
27   var $InitialFAIrelease           = "/";
28   var $Releases                 = array();
29   var $enableReleaseManagement  = false;
31   var $AllAppsForRelease        = array();
32   var $AllAppsForReleaseParameter = array();
34   var $CopyPasteVars            = array("Categories");
35   var $gosaApplicationParameter ;
36   var $ui                       ;
39   function appgroup ($config, $dn= NULL, $parent= NULL)
40   {
41     $this->ui = get_userinfo();  
42  
43     /* prepare group app for release management */ 
44     $tmp = search_config($config->data,"faiManagement","CLASS");
45     if(!empty($tmp)){
46       $this->enableReleaseManagement = true;
47       $this->objectclasses [] = "FAIreleaseTag";
48       $this->attributes[] =  "FAIrelease";
49     }
51     plugin::plugin ($config, $dn, $parent);
53     /* In some case of old applikations with old release tag saving, we 
54         must reassign is_account state.
55        (Or if release management wasn't activated before creating this app) */
56     if($this->enableReleaseManagement){
57   
58       /* Release management is activated && this is a valid group app account,
59           but no release class was found, so activate is_account flag  */
60       if($dn != "new" && ( in_array("gosaApplicationGroup",$this->attrs['objectClass'])) && 
61          (!in_array("FAIreleaseTag",$this->attrs['objectClass']))){
62         $this->is_account =true;
63       }
64     }
65     
67     /* Load member applications */
68     if (isset ($this->attrs["gosaMemberApplication"][0])){
69       $this->gosaMemberApplication = array();
70       for ($i= 0; $i<$this->attrs["gosaMemberApplication"]["count"]; $i++){
71         $this->gosaMemberApplication[]=
72           $this->attrs["gosaMemberApplication"][$i];
73       }
74     }
76     /* Load application options */
77     if (isset($this->attrs['gosaApplicationParameter'])){
78       for ($i= 0; $i<$this->attrs['gosaApplicationParameter']['count']; $i++){
79         $option= preg_replace('/^[^:]+:/', '',
80             $this->attrs['gosaApplicationParameter'][$i]);
81         $name= preg_replace('/:.*$/', '',
82             $this->attrs['gosaApplicationParameter'][$i]);
83         $this->appoption[$name]= $option;
84       }
85     }
87     /* Parse MemberApplication*/
88     $tmp    = array();
89     $tmp2   = array();
90     $prios  = array();
91     $cats   = array();
93     foreach($this->gosaMemberApplication as $memberApp){
94       if(preg_match("/\|/i",$memberApp)){
95     
96         $tmp = split("\|",$memberApp);
97  
98         if(!empty($tmp[0])){
99           $tmp2[$tmp[1]][$tmp[2]] = array("App"=>$tmp[0]);
100         }
101         if(!empty($tmp[1])){
102           $n = split("/",$tmp[1]);
103           $c = count($n);
104           $cats [$tmp[1]] = $n[$c-1];
105         }
106         $prios[$tmp[1]][$tmp[2]] = $tmp[2];
107       }else{
108         $tmp2[]['App'] = $memberApp;
109       }
110     }
112     $this->Categories = $cats;
114     $this->gosaMemberApplication = $tmp2;
115     $cats[""]="";
116     foreach($cats as $cat ){
117       if((isset($prios[$cat]))&&(count($prios[$cat]))){
118         $max = max($prios[$cat]);
119         $min = 1;//min($prios[$cat]);  
120         $last = false;
121         for($i = $min ; $i < $max ; $i++){
122           if(!isset($prios[$cat][$i])){
123             if($last == false){
124               $this->gosaMemberApplication[$cat][$i]['App'] = "__SEPARATOR__".$i;
125             
126               $last = true;
127             }
128           }else{
129             $last = false;
130           }
131         }
132       }
133     }
135     $tmp = array();
136     foreach($this->gosaMemberApplication as $key =>  $entries){
137       ksort ($entries);
138       foreach($entries as $entry){
139         $tmp[$key][]= $entry;
140       }
141     }
142     $this->gosaMemberApplication = $tmp;
143     $this->curbase = $this->config->current['BASE'];
144     
145     /* Get required release informations */
146     if($this->enableReleaseManagement){
147       $this->InitialFAIrelease = $this->FAIrelease;
148       $this->Releases       = $this->getReleases();
149     }
151   }
154   /* Combine new array */
155   function combineArrays($ar0,$ar1,$ar2)
156   {
157     $ret = array();
158     if(is_array($ar0))
159     foreach($ar0 as $ar => $a){
160         $ret[$ar]=$a;
161     }
162     if(is_array($ar1))
163     foreach($ar1 as $ar => $a){
164         $ret[$ar]=$a;
165     }
166     if(is_array($ar2))
167     foreach($ar2 as $ar => $a){
168         $ret[$ar]=$a;
169     }
170     return($ret);
171   }
173   function getpos($atr,$attrs)
174   {
175     $i = 0;
176     foreach($attrs as $attr => $name)    {
177       $i++;
178       if($attr == $atr){
179         return($i);
180       }
181     }
182     return(-1);
183   }
185   function diffAppsInReleases()
186   {
187     /* Only diff if it is required 
188      */
189     $ret =array();
191     /* If current release has changed */
192     if($this->FAIrelease != $this->InitialFAIrelease){
194       /* Walk through all apps which are currently displayed */
195       if(isset($this->gosaMemberApplication[$this->curCatDir])){
197         foreach($this->gosaMemberApplication[$this->curCatDir] as $entry){
199           /* If application is also available in new release, check if parameter differs */
200           if(in_array($entry['App'],$this->AllAppsForRelease)){ 
202             $old = array();   // Old application parameter
203             $new = array();   // New parameters 
205             /* There are possibly no parameters specified */
206             if(isset($this->AllAppsForReleaseParameter[$this->InitialFAIrelease][$entry['App']])){
207               $old = $this->AllAppsForReleaseParameter[$this->InitialFAIrelease][$entry['App']];
208             }
210             if(isset($this->AllAppsForReleaseParameter[$this->FAIrelease][$entry['App']])){
211               $new = $this->AllAppsForReleaseParameter[$this->FAIrelease][$entry['App']];
212             }
214             /*  Both (old & new) have no gosaApplicationParameter
215              */
216             if((!isset($old['gosaApplicationParameter'])) && (!isset($new['gosaApplicationParameter']))){
217               $ret[$entry['App']] = false; 
219               /* Both got gosaApplicationParameter
220                */
221             }elseif((isset($old['gosaApplicationParameter'])) && (isset($new['gosaApplicationParameter']))){
222               if(array_differs($old['gosaApplicationParameter'],$new['gosaApplicationParameter'])){
223                 $ret[$entry['App']]  = true;
224               }else{
225                 $ret[$entry['App']]  = false;
226               }
227             }
228             /* Initialy had gosaApplicationParameter bot in new release not 
229              */
230           }elseif((isset($old['gosaApplicationParameter'])) && (!isset($new['gosaApplicationParameter']))){
231             $ret[$entry['App']]  = true;
233             /* Old release had no gosaApplicationParameter but new got some
234              */
235           }elseif((!isset($old['gosaApplicationParameter'])) && (isset($new['gosaApplicationParameter']))){
236             $ret[$entry['App']]  = true;
237           }
238         }
239       }
240     }
241     return($ret);
242   }
244   /* TRansports the geiven Arraykey one position up*/
245   function ArrayUp($atr,$attrs)
246   {
247     $ret = $attrs;
248     $pos = $this->getpos($atr,$attrs) ;
249     $cn = count($attrs);
250     if(!(($pos == -1)||($pos == 1))){
251       $before = array_slice($attrs,0,($pos-2));
252       $mitte  = array_reverse(array_slice($attrs,($pos-2),2));
253       $unten  = array_slice($attrs,$pos);
254       $ret = array();
255       $ret = $this->combineArrays($before,$mitte,$unten);
256     }
257     return($ret);
258   }
261   /* TRansports the geiven Arraykey one position up*/
262   function ArrayDown($atr,$attrs)
263   {
264     $ret = $attrs;
265     $pos = $this->getpos($atr,$attrs) ;
266     $cn = count($attrs);
267     if(!(($pos == -1)||($pos == $cn))){
268       $before = array_slice($attrs,0,($pos-1));
269       $mitte  = array_reverse(array_slice($attrs,($pos-1),2));
270       $unten  = array_slice($attrs,($pos+1));
271       $ret = array();
272       $ret = $this->combineArrays($before,$mitte,$unten);
273     }
274     return($ret);
275   }
278   function catUp($id)
279   {
280     /* Get all cats depinding on current dir */
281     $cats = $this->GetSubdirs($this->curCatDir);
282     $newcats =$this->ArrayUp($id,$cats);
284     foreach($newcats as $cat => $name){
285       unset($this->Categories[$cat]);
286     }
287     foreach($newcats as $cat => $name){
288       $this->Categories[$cat]=$name;
289     }
290   }
293   function catDown($id)
294   {
295     /* Get all cats depinding on current dir */
296     $cats = $this->GetSubdirs($this->curCatDir);
298     $newcats =$this->ArrayDown($id,$cats);
300     foreach($newcats as $cat => $name){
301       unset($this->Categories[$cat]);
302     }
303     foreach($newcats as $cat => $name){
304       $this->Categories[$cat]=$name;
305     }
306   }
309   function getOneUp($appl)
310   { 
311     $cat  = $this->curCatDir;
312     $apps = $this->gosaMemberApplication[$cat];
314     $appsA = array();
315     foreach ($apps as $appkey => $name){
316       $appsA[$name['App']] =$name['App'];
317     }
319     $result = $this->ArrayUp($appl,$appsA);
321     $ret = array();
322     foreach($result as $app){
323       $ret[]=array("App"=>$app);
324     }
325     $this->gosaMemberApplication[$cat] = $ret;
326   }
329   function getOneDown($appl)
330   {
331     $cat  = $this->curCatDir;
332     $apps = $this->gosaMemberApplication[$cat];
334     $appsA = array();
335     foreach ($apps as $appkey => $name){
336       $appsA[$name['App']] =$name['App'];
337     }
339     $result = $this->ArrayDown($appl,$appsA);
341     $ret = array();
342     foreach($result as $app){
343       $ret[]=array("App"=>$app);
344     }
345     $this->gosaMemberApplication[$cat] = $ret;
346   } 
350   function AddSeperator($id)
351   {
352     $found  = false;
353     $cat    = "";
354     $tmp = array();
355     foreach($this->gosaMemberApplication[$this->curCatDir] as $appID => $app){  
356       $tmp[] = $app;    
357       if(($app['App'] == $id)&&(!$found)){
358         $cnt = count($this->gosaMemberApplication[$this->curCatDir]);
359         $tmp[] = array("App" => "__SEPARATOR__".($cnt+1));
360         $found = true;
361       }
362     }
363     if($found){
364       $this->gosaMemberApplication[$this->curCatDir]=$tmp;
365     }
366   }
368   function execute()
369   {
370     /* Call parent execute */
371     plugin::execute();
373     if((isset($_GET['act']))&&($_GET['act']=="depopen")){
374       $dep = base64_decode($_GET['depid']);  
375       if(isset($this->config->idepartments[$dep])){
376         $this->curbase =$dep;
377       }
378     }
380     if((isset($_GET['act']))&&($_GET['act']=="open")){
381       $this->curCatDir = base64_decode($_GET['id']);
382     }
384     /* Do we need to flip is_account state? */
385     if (isset($_POST['modify_state'])){
387       /* Onyl change account state if allowed */
388       if($this->is_account && $this->acl == "#all#"){
389         $this->is_account= !$this->is_account;
390       }elseif(!$this->is_account && chkacl($this->acl,"create") == ""){
391         $this->is_account= !$this->is_account;
392       }
393     }
395     /* Do we represent a valid group? */
396     if (!$this->is_account && $this->parent == NULL){
397       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
398         _("This 'dn' is no appgroup.")."</b>";
399       return ($display);
400     }
402     /* Show tab dialog headers */
403     $display= "";
404     if ($this->parent != NULL){
405       if ($this->is_account){
406         $display= $this->show_header(_("Remove applications"),
407             _("This group has application features enabled. You can disable them by clicking below."));
408       } else {
409         $display.= $this->show_header(_("Create applications"),
410             _("This group has application features disabled. You can enable them by clicking below."));
411         return ($display);
412       }
413     }
416     /* Add Categorie */ 
419     if((isset($_POST['AddCat']))&&(isset($_POST['CatName']))&&(!empty($_POST['CatName']))){
421       if(preg_match("/[\\\\\/]/i",$_POST['CatName'])){
422         print_red(_("Invalid character in category name."));
423       }elseif(!in_array($_POST['CatName'],$this->Categories)){ 
424         if(empty($this->curCatDir)){
425           $this->Categories[$_POST['CatName']]=$_POST['CatName'];
426         }else{
427           $this->Categories[$this->curCatDir."/".$_POST['CatName']]=$_POST['CatName'];
428         }
429       }else{
430         print_red(_("The specified category already exists."));
431       }
432     }
435     $this->reload();
437     $this->diffAppsInReleases();
438     $only_once = false;
439     $once =FALSE;
440     foreach($_POST as $name => $value){
441       
442       if((preg_match("/AddSep_/",$name))&&(!$only_once)){
443         $only_once = true;
444         $n = preg_replace("/AddSep_/","",$name);
445         $val= preg_replace("/_.*$/","",$n);
446         $this->AddSeperator($val);
447       }
449       if((preg_match("/DelApp_/",$name))&&(!$only_once)){
450         $only_once = true;
451    
453         if(preg_match("/DelApp___SEPARATOR__/",$name)) {
454           $n=  preg_replace("/DelApp___SEPARATOR__/","",$name);
455           $val= "__SEPARATOR__".preg_replace("/_.*$/","",$n);
456         }else{
457           $n = preg_replace("/DelApp_/","",$name);
458           $val= preg_replace("/_.*$/","",$n);
459         }
461         foreach($this->gosaMemberApplication as $key =>  $cat){
462           foreach($cat as $key2 => $app){
463             
464             if($app['App'] == $val){
465               unset($this->gosaMemberApplication[$key][$key2]);
466               if(isset($this->used_apps[$val])){
467                 foreach($this->getParameters($val) as $para){
468                   if(isset($this->appoption[$para])){
469                     unset($this->appoption[$para]);
470                   }
471                 }
472                 unset($this->used_apps[$val]);
473               }
474             }
475           }
476         }
477       }
479  
480       if(preg_match("/DelCat_/",$name) && !$once){
481         $once =TRUE;
482         $cat = preg_replace("/DelCat_/","",$name);
483         $cat = trim(base64_decode( preg_replace("/_.*$/","",$cat)));
485         $free_apps = array();
487         if(isset($this->gosaMemberApplication[$cat]) && is_array($this->gosaMemberApplication[$cat])){
488           foreach($this->gosaMemberApplication[$cat] as $app){
489             $free_apps[] = $app['App'];
490           }
491           unset($this->gosaMemberApplication[$cat]);
492           unset($this->Categories[$cat]);
493         }
494         foreach($this->Categories as $key => $name){
495           if(preg_match("/^".normalizePreg($cat)."\/.*/",$key)){
496             foreach($this->gosaMemberApplication[$key] as $app){
497               $free_apps[] = $app['App'];
498             }
499             unset($this->gosaMemberApplication[$key]);
500             unset($this->Categories[$key]);
501           }
502         }
503         foreach($free_apps as $app){
504           if(isset($this->used_apps[$app])){
505             unset($this->used_apps[$app]);
506           }
507         }
508       }
509       
510       if((preg_match("/EdiApp_/",$name))&&(!$only_once)){
512         $only_once = true;
513         $appname = $value;
514         $appname = preg_replace("/EdiApp_/","",$name);  
515         $appname = preg_replace("/_.*$/","",$appname);
517         /* We've got the appname, get parameters from ldap 
518          */
519         $ldap= $this->config->get_ldap_link();
521         $tmp = search_config($this->config->data,"faiManagement","CLASS");
522         if(!empty($tmp)){
523           $tmp = array_flip($this->Releases);
524           $base = $tmp[$this->FAIrelease];
526           $ldap->cd($this->config->current['BASE']);
527           $ldap->search("(&(objectClass=gosaApplication)(cn=$appname))",array("gosaApplicationParameter"));
528           $found = "";
529           while($attrs = $ldap->fetch()) {
530             if(preg_match("/cn=".$appname.",".$base."/",$attrs['dn'])){
531               $found = $attrs['dn'];
532             }
533           }
534           $ldap->cat($found, array("gosaApplicationParameter"));
535         }else{
536           $ldap->cd($this->config->current['BASE']);
537           $ldap->search("(&(objectClass=gosaApplication)(cn=$appname))",array("gosaApplicationParameter"));
538         }
541         if ($ldap->count() != 1){
542           print_red (_("The selected application name is not uniq. Please check your LDAP."));
543         } else {
544           $attrs= $ldap->fetch();
545           if(isset($attrs['gosaApplicationParameter'])){
546             $this->dialog= TRUE;
548             /* Fill name and value arrays */
549             for ($i= 0; $i<$attrs['gosaApplicationParameter']['count']; $i++){
550               $option= preg_replace('/^[^:]+:/', '',
551                   $attrs['gosaApplicationParameter'][$i]);
552               $name= preg_replace('/:.*$/', '', 
553                   $attrs['gosaApplicationParameter'][$i]);
554               $this->option_name[$i]= $name;
556               /* Fill with values from application, default should be
557                  loaded by the external scripts */
558               if (isset($this->appoption[$name])){
559                 $this->option_value[$i]= $this->appoption[$name];
560               }
561             }
563             /* Create edit field */
564             $table= "<table summary=\"\">";
565             for ($i= 0; $i < count($this->option_name); $i++){
566               if (isset($this->option_value[$i])){
567                 $value= $this->option_value[$i];
568               } else {
569                 $value= "";
570               }
571               $table.="<tr><td>".$this->option_name[$i]."</td><td>".
572                 "<input name=\"value$i\" size=60 maxlength=250 ".
573                 "value=\"".$value."\"><br></td></tr>";
574             }
575             $table.= "</table>";
576             $this->table= $table;
577           } else {
578             print_red (_("The selected application has no options."));
579           }
580         }
581       }
582     }
584     $this->reload();
586     /* Add group with post */
587     if((isset($_GET['act']))&&($_GET['act']=="add")){
588       $this->used_apps[$_GET['id']]= $_GET['id'];
589       asort($this->used_apps);
590       $this->addApp($_GET['id']);
591     }
593     /* Add multiple */
594     if(isset($_POST['AddApps'])){
595       foreach($_POST as $name => $value){
596         if(preg_match("/AddApp_/",$name)){
597           $app = preg_replace("/AddApp_/","",$name);
598           $this->addApp($app);
599         }
600       }
601     }
604     /* Cancel edit options? */
605     if (isset($_POST['edit_options_cancel'])){
606       $this->dialog= FALSE;
607     }
609     /* Finish edit options? */
610     if (isset($_POST['edit_options_finish'])){
611       $this->dialog= FALSE;
613       /* Save informations passed by the user */
614       $this->option_value= array();
615       for ($i= 0; $i<count($this->option_name); $i++){
616         $this->appoption[$this->option_name[$i]]= $_POST["value$i"];
617         $this->is_modified= TRUE;
618       }
619     }
621     /* Prepare templating stuff */
622     $smarty= get_smarty();
623     $smarty->assign("used_apps", $this->used_apps);
624     $apps= array();
625     foreach ($this->apps as $key => $value){
626       if (!array_key_exists($key, $this->used_apps)){
627         $apps["$key"]= "$value";
628       }
629     }
631     $div = new DivSelectBox("appgroup");    
633     $div->SetHeight(300);
635     /* get departments */  
636     $ui = get_userinfo();
637     $departments = array();
638     $res = get_list("(objectClass=gosaDepartment)",$ui->subtreeACL,$this->curbase,array("description","cn","ou"),GL_SIZELIMIT);
639     foreach($res as $value){
641       $fdn = $value['dn'];
642       $fdn = preg_replace("/".normalizePreg($this->curbase)."/","",$fdn);
643       $fdn= @LDAP::fix($fdn);
644       if($value["description"][0]!=".."){
645         $departments[$value['dn']]= convert_department_dn($fdn)." - [".$value["description"][0]."]";
646       }else{
647         $departments[$value['dn']]= convert_department_dn($fdn);
648       }
649     }
651     $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=depopen&amp;depid=%s'>%s</a>";
652     $linkadd  = "<a href='?plug=".$_GET['plug']."&amp;act=add&amp;id=%s'>%s</a>";
654     $base_back = preg_replace("/^[^,]+,/","",$this->curbase);
655     if((strlen($base_back)>= strlen($this->config->current['BASE']))&&($this->curbase!=$this->config->current['BASE'])){
656       $div->AddEntry(array(
657             array("string"=>sprintf($linkopen,base64_encode($base_back),".. ["._("back")."]"),
658               "attach"=>"style='border:0px;'")
659             ));
660     }
661     foreach($departments as $key => $app){
662       $div->AddEntry(array(
663             array("string"=>"<img class='center' src='images/folder.png' alt='"._("department")."'>&nbsp;".sprintf($linkopen,base64_encode($key),$app),
664               "attach"=>"style='border:0px;'")
665             ));
666     }
668     foreach($apps as $key => $app){
669       $div->AddEntry(array(
670             array("string"=>sprintf("<input class='center' type='checkbox' value='1' name='AddApp_%s'>",$key).
671               "<img class='center' src='images/select_application.png' alt='"._("application")."'>&nbsp;".sprintf($linkadd,$key,$app),
672               "attach"=>"style='border:0px;'")
673             ));
674     }
676     if((isset($_GET['act']))&&(($_GET['act'] == "cat_up")||($_GET['act']=="cat_down"))){
677       if($_GET['act']=="cat_up"){
678         $this->catUp(base64_decode($_GET['id']));
679       }
680       if($_GET['act']=="cat_down"){
681         $this->catDown(base64_decode($_GET['id']));
682       }
683     }
685     if((isset($_GET['act']))&&(($_GET['act'] == "one_up")||($_GET['act']=="one_down"))){
686       if(isset($_GET['id'])){
687         $id   = $_GET['id'];
688         $act  = $_GET['act']; 
690         if($act == "one_up"){
691           $this->getOneUp($id);
692         }elseif($act == "one_down")   { 
693           $this->getOneDown($id);
694         }
695       }
696     }
698     $div2 = new DivSelectBox("appgroup");
699     $div2->SetHeight(300);
701     $linkopen       = "<img class='center' src='images/folder.png' alt=\"\">&nbsp;<a href='?plug=".$_GET['plug']."&amp;act=open&amp;id=%s'>%s</a>";
702     $catremove      = "&nbsp;<input type='image' src='images/edittrash.png' title='"._("Delete entry")."' name='DelCat_%s' value='%s'>";
703     $app            = "<img class='center' src='images/select_application.png' alt=\"\">&nbsp;%s";
704     
705     $catupdown        = "<a href='?plug=".$_GET['plug']."&amp;act=cat_up&amp;id=%s'>
706                        <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'> 
707                        <img alt=\"\" src='images/sort_down.png' title='"._("Move down")."' border=0></a>";
709     if(empty($this->curCatDir)){
710       $cnt =0;
711     }else{
712       $cnt = count(split("/",$this->curCatDir));
713       $tmp = split("/",$this->curCatDir);
714       $bbk = "";
715       for($i = 0 ; $i < ($cnt -1 ) ; $i++){
716         $bbk .= $tmp[$i]."/";
717       }
718       $bbk = preg_replace("/\/$/","",$bbk);
719       $div2 ->AddEntry(array(array("string"=>sprintf($linkopen,base64_encode($bbk),".. ["._("Back")."]")),array("string"=>"&nbsp;","attach"=>"style='border-right:0px;'")));
720     }
722     $this->GetSubdirs($this->curCatDir);
724     foreach($this->GetSubdirs($this->curCatDir) as $path => $name){
725       $div2 ->AddEntry(array( 
726             array("string"=>sprintf($linkopen,base64_encode($path),$name)),
727             array("string"=>preg_replace("/%s/",base64_encode($path),$catupdown.$catremove),
728               "attach"=>"align='right' style='width:80px;border-right:0px;'"))); 
729     }
731     /* Append entries */
733     $separator ="<hr size=1>"; 
735     $sep = "<input type='image' src='images/back.png' title='"._("Insert seperator")."' value='%s' name='AddSep_%s'>";
736   
737     $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>".
738       "&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>".
739       "&nbsp;<input type='image' src='images/edittrash.png' title='"._("Delete entry")."' name='DelApp_%s' value='%s' alt='{t}delete{/t}' >";
740     $edit=      "&nbsp;<input type='image' src='images/edit.png' title='"._("Edit entry")."' name='EdiApp_%s' value='%s' alt='{t}edit{/t}' >";
742     $Differences = $this->diffAppsInReleases();
744     if(isset($this->gosaMemberApplication[$this->curCatDir])){
745       foreach($this->gosaMemberApplication[$this->curCatDir] as $cat => $entry){
747         
748         if(preg_match("/__SEPARATOR__/",$entry['App'])){
749           $div2 ->AddEntry(array(array("string"=>$separator),
750                 array("string"=>preg_replace("/\%s/",htmlentities($entry['App']),$upudown),"attach"=>"align='right' style='border-right:0px;'")));
751         }else{
753           $image = "";
754           /* Check if application was available within selected release
755            *  or application list if release management is not active
756            */
757           if(!in_array($entry['App'],$this->AllAppsForRelease)){
759             /*  release managent is active
760              */
761             if(!$this->enableReleaseManagement){
762               $image = "<img class='center' alt='R' src='images/select_invalid_application.png' 
763                           title='"._("This application is no longer available.")."'>&nbsp;";
764             }else{
765               $image = "<img class='center' alt='F' src='images/select_invalid_application.png' 
766                           title=\"".sprintf(_("This application is not available in any release named %s."),$this->FAIrelease)."\">&nbsp;";
767             }
768       
769           }elseif(isset($Differences[$entry['App']]) && ($Differences[$entry['App']] == true)) {
770             $entry['App'].=" <i>["._("Check parameter")."]</i>";
771               $image = "<img class='center' src='images/select_invalid_application.png' 
772                           title='"._("This application has changed parameters.")."'>&nbsp;";
773           }else{
774             $image = "<img class='center' src='images/select_application.png' alt=\"\">&nbsp;"; 
775           }
776           
777           $div2->AddEntry(array(array("string"=>sprintf($image."%s",$entry['App'])),
778                 array("string"=>preg_replace("/\%s/",htmlentities($entry['App']),$sep.$edit.$upudown),
779                       "attach"=>"align='right' style='width:100px;border-right:0px;'")));
780         }
781       }
782     }
784     $smarty->assign("UsedApps", $div2->DrawList());
785     $smarty->assign("List", $div->DrawList());
786     $smarty->assign("apps", $apps);
787    
788     $smarty->assign("enableReleaseManagement",$this->enableReleaseManagement);
789  
790     if($this->enableReleaseManagement){
791       $smarty->assign("FAIrelease",  $this->FAIrelease);
792       $smarty->assign("Releases", $this->Releases);
794       if(count($this->used_apps)){
795         $smarty->assign("ReleaseSelectAble", false);
796       }else{
797         $smarty->assign("ReleaseSelectAble", true);
798       }
799     }
800         $smarty->assign("ReleaseSelectAble", true);
802     /* Show main page */
803     if ($this->dialog){
804       $smarty->assign("table", $this->table);
805       $display.= $smarty->fetch (get_template_path('application_options.tpl', TRUE,dirname(__FILE__)));
806     } else {
807       $display.= $smarty->fetch (get_template_path('application.tpl', TRUE, dirname(__FILE__)));
808     }
809     return ($display);
810   }
813   function getReleases()
814   {
815     $dn   = $this->config->current['BASE'];
816     $ldap = $this->config->get_ldap_link();
817     $ldap->cd($dn);
818     $ldap->search("(&(objectClass=organizationalUnit)(objectClass=FAIbranch))",array("ou","dn"));
819     $ret =array();
820     while($attrs = $ldap->fetch()){
821   
822       if(preg_match("/ou=apps,/",$attrs['dn'])){
823         $bb = preg_replace("/ou=apps,.*/","",$attrs['dn']);
824         $parts = array_reverse(split("ou=",$bb));
826         $str ="";
827         foreach($parts as $part){
828           if(empty($part)) {
829             continue;
830           }
831           $str .= str_replace(",","",$part)."/";
832         }    
833         $name = preg_replace("/\/$/","",$str);
834         if(empty($name)) {
835           $name ="/";
836         }
837         $ret[$attrs['dn']] = $name;
838       }
839     }
841     /* Set releases to / id nor presenet */
842     if (!count($ret)){
843       $ret['/']= '/';
844     }
846     return($ret);
847   }
849   function save_object()
850   {
851     plugin::save_object();
852   }
853  
855   function remove_from_parent()
856   {
857     plugin::remove_from_parent();
859     $ldap= $this->config->get_ldap_link();
860     $ldap->cd($this->dn);
861     $this->cleanup();
862     
863     $ldap->modify ($this->attrs); 
865     show_ldap_error($ldap->get_error(), _("Removing application information failed"));
867     /* Optionally execute a command after we're done */
868     $this->handle_post_events("remove");
869   }
872   /* Save to LDAP */
873   function save()
874   {
875     plugin::save();
877     /* Copy members */
878     $this->Categories[""]=""; 
879     $this->attrs["gosaMemberApplication"]= array();
880     $this->attrs["gosaApplicationParameter"]= array();
881     $cat_id= 0 ;
882     foreach($this->Categories as $name => $cats){
883       $i =0;
884       if(isset($this->gosaMemberApplication[$name])){
885         foreach($this->gosaMemberApplication[$name] as $entry){
886           if(!preg_match("/__SEPARATOR__/",$entry['App'])){
887             $this->attrs["gosaMemberApplication"][]= $entry['App']."|".$name."|".$i;
888             $i ++;
889           }
890         }
891       }
892       if(($i==0)&&(!empty($name))){
893         $this->attrs["gosaMemberApplication"][]= "|".$name."|".$cat_id;
894       }
895       $cat_id++;
896     }
900     /* Are there application parameters to be saved */
901     $this->attrs['gosaApplicationParameter']= array();
902     foreach($this->appoption as $name => $value){
903       if ($value != ""){
904         $this->attrs['gosaApplicationParameter'][]= "$name:$value";
905       }
906     }
908     /* Write back to LDAP */
909     $ldap= $this->config->get_ldap_link();
910     $ldap->cd($this->dn);
911     $this->cleanup();
912     $ldap->modify ($this->attrs); 
914     show_ldap_error($ldap->get_error(), _("Saving application information failed"));
916     /* Optionally execute a command after we're done */
917     if ($this->initially_was_account == $this->is_account){
918       if ($this->is_modified){
919         $this->handle_post_events("mofify");
920       }
921     } else {
922       $this->handle_post_events("add");
923     }
925   }
927   function check()
928   {
929     /* Call common method to give check the hook */
930     $message= plugin::check();
932     return ($message);
933   }
936   function reload()
937   {
938     /* Generate applist */
939     $this->apps= array();
941     /* Special handling for release managed apps 
942      */
943     $tmp = search_config($this->config->data,"faiManagement","CLASS");
944     if(!empty($tmp) && count($this->Releases)){
945       $this->enableReleaseManagement = true;
947       $tmp = array_flip($this->Releases);
948       if(isset($tmp[$this->FAIrelease])){
949         $base =  $tmp[$this->FAIrelease];
950       }else{
951         $old_r =  $this->FAIrelease;
952         $k = key($tmp);
953         $r = $tmp[$k];
954         $this->FAIrelease = $k;
955         $base = $r; 
956         if($old_r != "/"){ 
957           print_red(sprintf(_("Can't resolve the release name '%s', setting release name to '%s'. Possibly the objects base has changed."),$old_r,$k));
958         }
959       }
961       $base = preg_replace("/ou=apps,.*$/","ou=apps,".$this->curbase,$base);
962       $app_res = get_list("(objectClass=gosaApplication)", $this->ui->subtreeACL, $base,array("cn","description"));
963     }else{
964       $app_res = get_list("(objectClass=gosaApplication)", $this->ui->subtreeACL, "ou=apps,".$this->curbase,array("cn","description"));
965     }
966     
967     foreach($app_res as $attrs){
968       if (isset($attrs["description"][0])){    
969         $this->apps[$attrs["cn"][0]]=
970           $attrs["cn"][0]." (".
971           $attrs["description"][0].")";
972       } else {
973         $this->apps[$attrs["cn"][0]]=
974           $attrs["cn"][0];
975       }
976     }
978     $ldap = $this->config->get_ldap_link();
979     $ldap->cd($this->config->current['BASE']);
980     $ldap->search("objectClass=gosaApplication",array("gosaApplicationParameter","cn"));
981     $tmp = search_config($this->config->data,"faiManagement","CLASS");
982     $this->AllAppsForRelease = array();
983     if(!empty($tmp)){
985       $tmp = array_flip($this->Releases);
987       while($attrs = $ldap->fetch()){
988         
989         $testdn = preg_replace("/ou=apps,.*$/","ou=apps",$attrs['dn']);
990         $testdn = preg_replace("/^[^,]+/","",$testdn);
991         $testdn = preg_replace("/^,/","",$testdn);
993         if($testdn == preg_replace("/ou=apps,.*$/","ou=apps",$tmp[$this->FAIrelease])){
994           $this->AllAppsForRelease[$attrs['dn']] = $attrs['cn'][0];
995           $this->AllAppsForReleaseParameter[$this->FAIrelease][$attrs['cn'][0]] = $attrs;
996         }
997       }
998     }else{
999       while($attrs = $ldap->fetch()){
1000         $this->AllAppsForRelease[$attrs['dn']] = $attrs['cn'][0];
1001       }
1002     }
1004     natcasesort ($this->apps);
1005     reset ($this->apps);
1007     if(is_array($this->gosaMemberApplication))
1008       foreach ($this->gosaMemberApplication as $cat){   
1009         if(is_array($cat))
1010           foreach($cat as $entry){
1011             $this->used_apps[$entry['App']]= $entry['App'];
1012           }
1013       }
1014   }
1017   function addApp($cn)
1018   {
1019     if((isset($this->gosaMemberApplication[$this->curCatDir]))&&(is_array($this->gosaMemberApplication[$this->curCatDir]))){
1020       foreach($this->gosaMemberApplication[$this->curCatDir] as $entry){
1021         if($entry['App'] == $cn) return;
1022       }
1023     }
1024     $this->gosaMemberApplication[$this->curCatDir][]= array("App"=>$cn);
1025     $this->used_apps[$cn]=$cn;
1026     $this->is_modified= TRUE;
1027   }
1030   function removeApp($cn)
1031   {
1032     $temp= array();
1033     foreach ($this->gosaMemberApplication as $value){
1034       if ($value != $cn){
1035         $temp[]= $value;
1036       }
1037     }
1038     $this->gosaMemberApplication= $temp;
1039     $this->is_modified= TRUE;
1040   }
1042   function getParameters($app)
1043   {
1044     $tmp = $this->getReleases();
1045     $ret = array();
1046     if(in_array($this->FAIrelease,$tmp)){
1047       $tmp2 = array_flip($tmp);
1048       $base = $tmp2[$this->FAIrelease];
1049       $ldap = $this->config->get_ldap_link();
1050       $ldap->cd($this->config->current['BASE']);
1051       $ldap->search("(&(objectClass=gosaApplication)(cn=".$app.")(gosaApplicationParameter=*))",array("gosaApplicationParameter"));
1052       if($ldap->count()){
1053         $attrs = $ldap->fetch();
1054         for($i = 0 ; $i < $attrs['gosaApplicationParameter']['count'] ; $i ++ ){
1055           $name = preg_replace("/:.*$/","",$attrs['gosaApplicationParameter'][$i]);
1056           $ret[$name] = $name;
1057         } 
1058       }
1059     }
1060     return($ret);
1061   }
1063   function GetSubdirs($dir)
1064   {
1065     $ret = array();
1066     $tmp1 = split("/",$this->curCatDir);
1067  
1068     foreach($this->Categories as $path=>$cat){
1069       $tmp2 = split("/",$path);
1070       
1071       if((empty($this->curCatDir))&&(!preg_match("/\//",$path))){
1072         $abort = false;
1073       }elseif(((count($tmp1))+1) == (count($tmp2))){
1074         $abort = false;
1075         for($i = 0 ; $i < count($tmp1) ; $i++){
1076           if($tmp1[$i] != $tmp2[$i]){
1077             $abort = true;
1078           }
1079         }
1080       }else{
1081         $abort= true;
1082       }
1083       if(!$abort){
1084         $ret[$path]=$cat;
1085       } 
1086     }
1087   return($ret);
1088   }
1090   function PrepareForCopyPaste($source)
1091   {
1092     $this->FAIrelease = $this->InitialFAIrelease = $source->FAIrelease;
1093     $this->gosaMemberApplication = $source->gosaMemberApplication;
1094     $this->appoption = $source->appoption; 
1095     $this->Categories = $source->Categories; 
1096   }
1099 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1100 ?>