Code

Added several comments.
[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 ;
40   var $ui                       ;
43   function appgroup ($config, $dn= NULL, $parent= NULL)
44   {
45     $this->ui = get_userinfo();  
46  
47     /* prepare group app for release management */ 
48     $tmp = search_config($config->data,"faiManagement","CLASS");
49     if(!empty($tmp)){
50       $this->enableReleaseManagement = true;
51       $this->objectclasses [] = "FAIreleaseTag";
52       $this->attributes[] =  "FAIrelease";
53     }
55     plugin::plugin ($config, $dn, $parent);
57     /* In some case of old applikations with old release tag saving, we 
58         must reassign is_account state.
59        (Or if release management wasn't activated before creating this app) */
60     if($this->enableReleaseManagement){
61   
62       /* Release management is activated && this is a valid group app account,
63           but no release class was found, so activate is_account flag  */
64       if($dn != "new" && ( in_array("gosaApplicationGroup",$this->attrs['objectClass'])) && 
65          (!in_array("FAIreleaseTag",$this->attrs['objectClass']))){
66         $this->is_account =true;
67       }
68     }
69     
71     /* Load member applications */
72     if (isset ($this->attrs["gosaMemberApplication"][0])){
73       $this->gosaMemberApplication = array();
74       for ($i= 0; $i<$this->attrs["gosaMemberApplication"]["count"]; $i++){
75         $this->gosaMemberApplication[]=
76           $this->attrs["gosaMemberApplication"][$i];
77       }
78     }
80     /* Load application options */
81     if (isset($this->attrs['gosaApplicationParameter'])){
82       for ($i= 0; $i<$this->attrs['gosaApplicationParameter']['count']; $i++){
83         $option= preg_replace('/^[^:]+:/', '',
84             $this->attrs['gosaApplicationParameter'][$i]);
85         $name= preg_replace('/:.*$/', '',
86             $this->attrs['gosaApplicationParameter'][$i]);
87         $this->appoption[$name]= $option;
88       }
89     }
91     /* Parse MemberApplication*/
92     $tmp    = array();
93     $tmp2   = array();
94     $prios  = array();
95     $cats   = array();
97     foreach($this->gosaMemberApplication as $memberApp){
98       if(preg_match("/\|/i",$memberApp)){
99     
100         $tmp = split("\|",$memberApp);
101  
102         if(!empty($tmp[0])){
103           $tmp2[$tmp[1]][$tmp[2]] = array("App"=>$tmp[0]);
104         }
105         if(!empty($tmp[1])){
106           $n = split("/",$tmp[1]);
107           $c = count($n);
108           $cats [$tmp[1]] = $n[$c-1];
109         }
110         $prios[$tmp[1]][$tmp[2]] = $tmp[2];
111       }else{
112         $tmp2[]['App'] = $memberApp;
113       }
114     }
116     $this->Categories = $cats;
118     $this->gosaMemberApplication = $tmp2;
119     $cats[""]="";
120     foreach($cats as $cat ){
121       if((isset($prios[$cat]))&&(count($prios[$cat]))){
122         $max = max($prios[$cat]);
123         $min = 1;//min($prios[$cat]);  
124         $last = false;
125         for($i = $min ; $i < $max ; $i++){
126           if(!isset($prios[$cat][$i])){
127             if($last == false){
128               $this->gosaMemberApplication[$cat][$i]['App'] = "__SEPARATOR__".$i;
129             
130               $last = true;
131             }
132           }else{
133             $last = false;
134           }
135         }
136       }
137     }
139     $tmp = array();
140     foreach($this->gosaMemberApplication as $key =>  $entries){
141       ksort ($entries);
142       foreach($entries as $entry){
143         $tmp[$key][]= $entry;
144       }
145     }
146     $this->gosaMemberApplication = $tmp;
147     $this->curbase = $this->config->current['BASE'];
148     
149     /* Get required release informations */
150     if($this->enableReleaseManagement){
151       $this->InitialFAIrelease = $this->FAIrelease;
152       $this->Releases       = $this->getReleases();
153     }
154   }
157   /* Combine new array */
158   function combineArrays($ar0,$ar1,$ar2)
159   {
160     $ret = array();
161     if(is_array($ar0))
162     foreach($ar0 as $ar => $a){
163         $ret[$ar]=$a;
164     }
165     if(is_array($ar1))
166     foreach($ar1 as $ar => $a){
167         $ret[$ar]=$a;
168     }
169     if(is_array($ar2))
170     foreach($ar2 as $ar => $a){
171         $ret[$ar]=$a;
172     }
173     return($ret);
174   }
176   function getpos($atr,$attrs)
177   {
178     $i = 0;
179     foreach($attrs as $attr => $name)    {
180       $i++;
181       if($attr == $atr){
182         return($i);
183       }
184     }
185     return(-1);
186   }
188   function diffAppsInReleases()
189   {
190     /* Only diff if it is required 
191      */
192     $ret =array();
194     /* If current release has changed */
195     if($this->FAIrelease != $this->InitialFAIrelease){
197       /* Walk through all apps which are currently displayed */
198       if(isset($this->gosaMemberApplication[$this->curCatDir])){
200         foreach($this->gosaMemberApplication[$this->curCatDir] as $entry){
202           /* If application is also available in new release, check if parameter differs */
203           if(in_array($entry['App'],$this->AllAppsForRelease)){ 
205             $old = array();   // Old application parameter
206             $new = array();   // New parameters 
208             /* There are possibly no parameters specified */
209             if(isset($this->AllAppsForReleaseParameter[$this->InitialFAIrelease][$entry['App']])){
210               $old = $this->AllAppsForReleaseParameter[$this->InitialFAIrelease][$entry['App']];
211             }
213             if(isset($this->AllAppsForReleaseParameter[$this->FAIrelease][$entry['App']])){
214               $new = $this->AllAppsForReleaseParameter[$this->FAIrelease][$entry['App']];
215             }
217             /*  Both (old & new) have no gosaApplicationParameter
218              */
219             if((!isset($old['gosaApplicationParameter'])) && (!isset($new['gosaApplicationParameter']))){
220               $ret[$entry['App']] = false; 
222               /* Both got gosaApplicationParameter
223                */
224             }elseif((isset($old['gosaApplicationParameter'])) && (isset($new['gosaApplicationParameter']))){
225               if(array_differs($old['gosaApplicationParameter'],$new['gosaApplicationParameter'])){
226                 $ret[$entry['App']]  = true;
227               }else{
228                 $ret[$entry['App']]  = false;
229               }
230             }
231             /* Initialy had gosaApplicationParameter bot in new release not 
232              */
233           }elseif((isset($old['gosaApplicationParameter'])) && (!isset($new['gosaApplicationParameter']))){
234             $ret[$entry['App']]  = true;
236             /* Old release had no gosaApplicationParameter but new got some
237              */
238           }elseif((!isset($old['gosaApplicationParameter'])) && (isset($new['gosaApplicationParameter']))){
239             $ret[$entry['App']]  = true;
240           }
241         }
242       }
243     }
244     return($ret);
245   }
247   /* TRansports the geiven Arraykey one position up*/
248   function ArrayUp($atr,$attrs)
249   {
250     $ret = $attrs;
251     $pos = $this->getpos($atr,$attrs) ;
252     $cn = count($attrs);
253     if(!(($pos == -1)||($pos == 1))){
254       $before = array_slice($attrs,0,($pos-2));
255       $mitte  = array_reverse(array_slice($attrs,($pos-2),2));
256       $unten  = array_slice($attrs,$pos);
257       $ret = array();
258       $ret = $this->combineArrays($before,$mitte,$unten);
259     }
260     return($ret);
261   }
264   /* TRansports the geiven Arraykey one position up*/
265   function ArrayDown($atr,$attrs)
266   {
267     $ret = $attrs;
268     $pos = $this->getpos($atr,$attrs) ;
269     $cn = count($attrs);
270     if(!(($pos == -1)||($pos == $cn))){
271       $before = array_slice($attrs,0,($pos-1));
272       $mitte  = array_reverse(array_slice($attrs,($pos-1),2));
273       $unten  = array_slice($attrs,($pos+1));
274       $ret = array();
275       $ret = $this->combineArrays($before,$mitte,$unten);
276     }
277     return($ret);
278   }
281   function catUp($id)
282   {
283     /* Get all cats depinding on current dir */
284     $cats = $this->GetSubdirs($this->curCatDir);
285     $newcats =$this->ArrayUp($id,$cats);
287     foreach($newcats as $cat => $name){
288       unset($this->Categories[$cat]);
289     }
290     foreach($newcats as $cat => $name){
291       $this->Categories[$cat]=$name;
292     }
293   }
296   function catDown($id)
297   {
298     /* Get all cats depinding on current dir */
299     $cats = $this->GetSubdirs($this->curCatDir);
301     $newcats =$this->ArrayDown($id,$cats);
303     foreach($newcats as $cat => $name){
304       unset($this->Categories[$cat]);
305     }
306     foreach($newcats as $cat => $name){
307       $this->Categories[$cat]=$name;
308     }
309   }
312   function getOneUp($appl)
313   { 
314     $cat  = $this->curCatDir;
315     $apps = $this->gosaMemberApplication[$cat];
317     $appsA = array();
318     foreach ($apps as $appkey => $name){
319       $appsA[$name['App']] =$name['App'];
320     }
322     $result = $this->ArrayUp($appl,$appsA);
324     $ret = array();
325     foreach($result as $app){
326       $ret[]=array("App"=>$app);
327     }
328     $this->gosaMemberApplication[$cat] = $ret;
329   }
332   function getOneDown($appl)
333   {
334     $cat  = $this->curCatDir;
335     $apps = $this->gosaMemberApplication[$cat];
337     $appsA = array();
338     foreach ($apps as $appkey => $name){
339       $appsA[$name['App']] =$name['App'];
340     }
342     $result = $this->ArrayDown($appl,$appsA);
344     $ret = array();
345     foreach($result as $app){
346       $ret[]=array("App"=>$app);
347     }
348     $this->gosaMemberApplication[$cat] = $ret;
349   } 
353   function AddSeperator($id)
354   {
355     $found  = false;
356     $cat    = "";
357     $tmp = array();
358     foreach($this->gosaMemberApplication[$this->curCatDir] as $appID => $app){  
359       $tmp[] = $app;    
360       if(($app['App'] == $id)&&(!$found)){
361         $cnt = count($this->gosaMemberApplication[$this->curCatDir]);
362         $tmp[] = array("App" => "__SEPARATOR__".($cnt+1));
363         $found = true;
364       }
365     }
366     if($found){
367       $this->gosaMemberApplication[$this->curCatDir]=$tmp;
368     }
369   }
371   function execute()
372   {
373     /* Call parent execute */
374     plugin::execute();
376     if((isset($_GET['act']))&&($_GET['act']=="depopen")){
377       $dep = base64_decode($_GET['depid']);  
378       if(isset($this->config->idepartments[$dep])){
379         $this->curbase =$dep;
380       }
381     }
383     if((isset($_GET['act']))&&($_GET['act']=="open")){
384       $this->curCatDir = base64_decode($_GET['id']);
385     }
387     /* Do we need to flip is_account state? */
388     if (isset($_POST['modify_state'])){
389       $this->is_account= !$this->is_account;
390     }
392     /* Do we represent a valid group? */
393     if (!$this->is_account && $this->parent == NULL){
394       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
395         _("This 'dn' is no appgroup.")."</b>";
396       return ($display);
397     }
399     /* Show tab dialog headers */
400     $display= "";
401     if ($this->parent != NULL){
402       if ($this->is_account){
403         $display= $this->show_header(_("Remove applications"),
404             _("This group has application features enabled. You can disable them by clicking below."));
405       } else {
406         $display.= $this->show_header(_("Create applications"),
407             _("This group has application features disabled. You can enable them by clicking below."));
408         return ($display);
409       }
410     }
413     /* Add Categorie */ 
416     if((isset($_POST['AddCat']))&&(isset($_POST['CatName']))&&(!empty($_POST['CatName']))){
418       if(preg_match("/[\\\\\/]/i",$_POST['CatName'])){
419         print_red(_("Invalid character in category name."));
420       }elseif(!in_array($_POST['CatName'],$this->Categories)){ 
421         if(empty($this->curCatDir)){
422           $this->Categories[$_POST['CatName']]=$_POST['CatName'];
423         }else{
424           $this->Categories[$this->curCatDir."/".$_POST['CatName']]=$_POST['CatName'];
425         }
426       }else{
427         print_red(_("The specified category already exists."));
428       }
429     }
432     $this->reload();
434     $this->diffAppsInReleases();
435     $only_once = false;
436     foreach($_POST as $name => $value){
437       
438       if((preg_match("/AddSep_/",$name))&&(!$only_once)){
439         $only_once = true;
440         $n = preg_replace("/AddSep_/","",$name);
441         $val= preg_replace("/_.*$/","",$n);
442         $this->AddSeperator($val);
443       }
445       if((preg_match("/DelApp_/",$name))&&(!$only_once)){
446         $only_once = true;
447    
449         if(preg_match("/DelApp___SEPARATOR__/",$name)) {
450           $n=  preg_replace("/DelApp___SEPARATOR__/","",$name);
451           $val= "__SEPARATOR__".preg_replace("/_.*$/","",$n);
452         }else{
453           $n = preg_replace("/DelApp_/","",$name);
454           $val= preg_replace("/_.*$/","",$n);
455         }
457         foreach($this->gosaMemberApplication as $key =>  $cat){
458           foreach($cat as $key2 => $app){
459             
460             if($app['App'] == $val){
461               unset($this->gosaMemberApplication[$key][$key2]);
462               if(isset($this->used_apps[$val])){
463                 foreach($this->getParameters($val) as $para){
464                   if(isset($this->appoption[$para])){
465                     unset($this->appoption[$para]);
466                   }
467                 }
468                 unset($this->used_apps[$val]);
469               }
470             }
471           }
472         }
473       }
474   
475       if(preg_match("/DelCat_/",$name)){
476         $n = preg_replace("/DelCat_/","",$name);
477         $app = base64_decode( preg_replace("/_.*$/","",$n));
478         foreach($this->Categories as $key =>  $cat){
479           if($cat == $app){
480             foreach($this->Categories as $p => $n){
481               if(preg_match("/^".$key."\/.*/",$p)){
482                 unset($this->Categories[$p]);    
483               }
484             }
485             unset($this->Categories[$key]);
486           }
487         }
488       }
489       
490       if((preg_match("/EdiApp_/",$name))&&(!$only_once)){
492         $only_once = true;
493         $appname = $value;
494         $appname = preg_replace("/EdiApp_/","",$name);  
495         $appname = preg_replace("/_.*$/","",$appname);
497         /* We've got the appname, get parameters from ldap 
498          */
499         $ldap= $this->config->get_ldap_link();
501         $tmp = search_config($this->config->data,"faiManagement","CLASS");
502         if(!empty($tmp)){
503           $tmp = array_flip($this->Releases);
504           $base = $tmp[$this->FAIrelease];
506           $ldap->cd($this->config->current['BASE']);
507           $ldap->search("(&(objectClass=gosaApplication)(cn=$appname))",array("gosaApplicationParameter"));
508           $found = "";
509           while($attrs = $ldap->fetch()) {
510             if(preg_match("/cn=".$appname.",".$base."/",$attrs['dn'])){
511               $found = $attrs['dn'];
512             }
513           }
514           $ldap->cat($found, array("gosaApplicationParameter"));
515         }else{
516           $ldap->cd($this->config->current['BASE']);
517           $ldap->search("(&(objectClass=gosaApplication)(cn=$appname))",array("gosaApplicationParameter"));
518         }
521         if ($ldap->count() != 1){
522           print_red (_("The selected application name is not uniq. Please check your LDAP."));
523         } else {
524           $attrs= $ldap->fetch();
525           if(isset($attrs['gosaApplicationParameter'])){
526             $this->dialog= TRUE;
528             /* Fill name and value arrays */
529             for ($i= 0; $i<$attrs['gosaApplicationParameter']['count']; $i++){
530               $option= preg_replace('/^[^:]+:/', '',
531                   $attrs['gosaApplicationParameter'][$i]);
532               $name= preg_replace('/:.*$/', '', 
533                   $attrs['gosaApplicationParameter'][$i]);
534               $this->option_name[$i]= $name;
536               /* Fill with values from application, default should be
537                  loaded by the external scripts */
538               if (isset($this->appoption[$name])){
539                 $this->option_value[$i]= $this->appoption[$name];
540               }
541             }
543             /* Create edit field */
544             $table= "<table summary=\"\">";
545             for ($i= 0; $i < count($this->option_name); $i++){
546               if (isset($this->option_value[$i])){
547                 $value= $this->option_value[$i];
548               } else {
549                 $value= "";
550               }
551               $table.="<tr><td>".$this->option_name[$i]."</td><td>".
552                 "<input name=\"value$i\" size=60 maxlength=250 ".
553                 "value=\"".$value."\"><br></td></tr>";
554             }
555             $table.= "</table>";
556             $this->table= $table;
557           } else {
558             print_red (_("The selected application has no options."));
559           }
560         }
561       }
562     }
564     $this->reload();
566     /* Add group with post */
567     if((isset($_GET['act']))&&($_GET['act']=="add")){
568       $this->used_apps[$_GET['id']]= $_GET['id'];
569       asort($this->used_apps);
570       $this->addApp($_GET['id']);
571     }
573     /* Add multiple */
574     if(isset($_POST['AddApps'])){
575       foreach($_POST as $name => $value){
576         if(preg_match("/AddApp_/",$name)){
577           $app = preg_replace("/AddApp_/","",$name);
578           $this->addApp($app);
579         }
580       }
581     }
584     /* Cancel edit options? */
585     if (isset($_POST['edit_options_cancel'])){
586       $this->dialog= FALSE;
587     }
589     /* Finish edit options? */
590     if (isset($_POST['edit_options_finish'])){
591       $this->dialog= FALSE;
593       /* Save informations passed by the user */
594       $this->option_value= array();
595       for ($i= 0; $i<count($this->option_name); $i++){
596         $this->appoption[$this->option_name[$i]]= $_POST["value$i"];
597         $this->is_modified= TRUE;
598       }
599     }
601     /* Prepare templating stuff */
602     $smarty= get_smarty();
603     $smarty->assign("used_apps", $this->used_apps);
604     $apps= array();
605     foreach ($this->apps as $key => $value){
606       if (!array_key_exists($key, $this->used_apps)){
607         $apps["$key"]= "$value";
608       }
609     }
611     $div = new DivSelectBox("appgroup");    
613     $div->SetHeight(300);
615     /* get departments */  
616     $ui = get_userinfo();
617     $departments = array();
618     $res = get_list("(objectClass=gosaDepartment)",$ui->subtreeACL,$this->curbase,array("description","cn","ou"),GL_SIZELIMIT);
619     foreach($res as $value){
621       $fdn = $value['dn'];
622       $fdn = preg_replace("/".normalizePreg($this->curbase)."/","",$fdn);
623       $fdn= @LDAP::fix($fdn);
624       if($value["description"][0]!=".."){
625         $departments[$value['dn']]= convert_department_dn($fdn)." - [".$value["description"][0]."]";
626       }else{
627         $departments[$value['dn']]= convert_department_dn($fdn);
628       }
629     }
631     $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=depopen&amp;depid=%s'>%s</a>";
632     $linkadd  = "<a href='?plug=".$_GET['plug']."&amp;act=add&amp;id=%s'>%s</a>";
634     $base_back = preg_replace("/^[^,]+,/","",$this->curbase);
635     if((strlen($base_back)>= strlen($this->config->current['BASE']))&&($this->curbase!=$this->config->current['BASE'])){
636       $div->AddEntry(array(
637             array("string"=>sprintf($linkopen,base64_encode($base_back),".. ["._("back")."]"),
638               "attach"=>"style='border:0px;'")
639             ));
640     }
641     foreach($departments as $key => $app){
642       $div->AddEntry(array(
643             array("string"=>"<img class='center' src='images/folder.png' alt='"._("department")."'>&nbsp;".sprintf($linkopen,base64_encode($key),$app),
644               "attach"=>"style='border:0px;'")
645             ));
646     }
648     foreach($apps as $key => $app){
649       $div->AddEntry(array(
650             array("string"=>sprintf("<input class='center' type='checkbox' value='1' name='AddApp_%s'>",$key).
651               "<img class='center' src='images/select_application.png' alt='"._("application")."'>&nbsp;".sprintf($linkadd,$key,$app),
652               "attach"=>"style='border:0px;'")
653             ));
654     }
656     if((isset($_GET['act']))&&(($_GET['act'] == "cat_up")||($_GET['act']=="cat_down"))){
657       if($_GET['act']=="cat_up"){
658         $this->catUp(base64_decode($_GET['id']));
659       }
660       if($_GET['act']=="cat_down"){
661         $this->catDown(base64_decode($_GET['id']));
662       }
663     }
665     if((isset($_GET['act']))&&(($_GET['act'] == "one_up")||($_GET['act']=="one_down"))){
666       if(isset($_GET['id'])){
667         $id   = $_GET['id'];
668         $act  = $_GET['act']; 
670         if($act == "one_up"){
671           $this->getOneUp($id);
672         }elseif($act == "one_down")   { 
673           $this->getOneDown($id);
674         }
675       }
676     }
678     $div2 = new DivSelectBox("appgroup");
679     $div2->SetHeight(300);
681     $linkopen       = "<img class='center' src='images/folder.png' alt=\"\">&nbsp;<a href='?plug=".$_GET['plug']."&amp;act=open&amp;id=%s'>%s</a>";
682     $catremove      = "&nbsp;<input type='image' src='images/edittrash.png' title='"._("Delete entry")."' name='DelCat_%s' value='%s'>";
683     $app            = "<img class='center' src='images/select_application.png' alt=\"\">&nbsp;%s";
684     
685     $catupdown        = "<a href='?plug=".$_GET['plug']."&amp;act=cat_up&amp;id=%s'>
686                        <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'> 
687                        <img alt=\"\" src='images/sort_down.png' title='"._("Move down")."' border=0></a>";
689     if(empty($this->curCatDir)){
690       $cnt =0;
691     }else{
692       $cnt = count(split("/",$this->curCatDir));
693       $tmp = split("/",$this->curCatDir);
694       $bbk = "";
695       for($i = 0 ; $i < ($cnt -1 ) ; $i++){
696         $bbk .= $tmp[$i];
697       }
698       $div2 ->AddEntry(array(array("string"=>sprintf($linkopen,base64_encode($bbk),"..")),array("string"=>"&nbsp;","attach"=>"style='border-right:0px;'")));
699     }
701     $this->GetSubdirs($this->curCatDir);
703     foreach($this->GetSubdirs($this->curCatDir) as $path => $name){
704       $div2 ->AddEntry(array( 
705             array("string"=>sprintf($linkopen,base64_encode($path),$name)),
706             array("string"=>preg_replace("/%s/",base64_encode($path),$catupdown.$catremove),
707               "attach"=>"align='right' style='width:80px;border-right:0px;'"))); 
708     }
710     /* Append entries */
712     $separator ="<hr size=1>"; 
714     $sep = "<input type='image' src='images/back.png' title='"._("Insert seperator")."' value='%s' name='AddSep_%s'>";
715   
716     $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>".
717       "&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>".
718       "&nbsp;<input type='image' src='images/edittrash.png' title='"._("Delete entry")."' name='DelApp_%s' value='%s' alt='{t}delete{/t}' >";
719     $edit=      "&nbsp;<input type='image' src='images/edit.png' title='"._("Edit entry")."' name='EdiApp_%s' value='%s' alt='{t}edit{/t}' >";
721     $Differences = $this->diffAppsInReleases();
723     if(isset($this->gosaMemberApplication[$this->curCatDir])){
724       foreach($this->gosaMemberApplication[$this->curCatDir] as $cat => $entry){
726         
727         if(preg_match("/__SEPARATOR__/",$entry['App'])){
728           $div2 ->AddEntry(array(array("string"=>$separator),
729                 array("string"=>preg_replace("/\%s/",htmlentities($entry['App']),$upudown),"attach"=>"align='right' style='border-right:0px;'")));
730         }else{
732           $image = "";
733           /* Check if application was available within selected release
734            *  or application list if release management is not active
735            */
736           if(!in_array($entry['App'],$this->AllAppsForRelease)){
738             /*  release managent is active
739              */
740             if(!$this->enableReleaseManagement){
741               $image = "<img class='center' alt='R' src='images/select_invalid_application.png' 
742                           title='"._("This application is no longer available.")."'>&nbsp;";
743             }else{
744               $image = "<img class='center' alt='F' src='images/select_invalid_application.png' 
745                           title=\"".sprintf(_("This application is not available in any release named %s."),$this->FAIrelease)."\">&nbsp;";
746             }
747       
748           }elseif(isset($Differences[$entry['App']]) && ($Differences[$entry['App']] == true)) {
749             $entry['App'].=" <i>["._("Check parameter")."]</i>";
750               $image = "<img class='center' src='images/select_invalid_application.png' 
751                           title='"._("This application has changed parameters.")."'>&nbsp;";
752           }else{
753             $image = "<img class='center' src='images/select_application.png' alt=\"\">&nbsp;"; 
754           }
755           
756           $div2->AddEntry(array(array("string"=>sprintf($image."%s",$entry['App'])),
757                 array("string"=>preg_replace("/\%s/",htmlentities($entry['App']),$sep.$edit.$upudown),
758                       "attach"=>"align='right' style='width:100px;border-right:0px;'")));
759         }
760       }
761     }
763     $smarty->assign("UsedApps", $div2->DrawList());
764     $smarty->assign("List", $div->DrawList());
765     $smarty->assign("apps", $apps);
766    
767     $smarty->assign("enableReleaseManagement",$this->enableReleaseManagement);
768  
769     if($this->enableReleaseManagement){
770       $smarty->assign("FAIrelease",  $this->FAIrelease);
771       $smarty->assign("Releases", $this->Releases);
773       if(count($this->used_apps)){
774         $smarty->assign("ReleaseSelectAble", false);
775       }else{
776         $smarty->assign("ReleaseSelectAble", true);
777       }
778     }
779         $smarty->assign("ReleaseSelectAble", true);
781     /* Show main page */
782     if ($this->dialog){
783       $smarty->assign("table", $this->table);
784       $display.= $smarty->fetch (get_template_path('application_options.tpl', TRUE));
785     } else {
786       $display.= $smarty->fetch (get_template_path('application.tpl', TRUE));
787     }
788     return ($display);
789   }
792   function getReleases()
793   {
794     $dn   = $this->config->current['BASE'];
795     $ldap = $this->config->get_ldap_link();
796     $ldap->cd($dn);
797     $ldap->search("(&(objectClass=organizationalUnit)(objectClass=FAIbranch))",array("ou"));
798     $ret =array();
799     while($attrs = $ldap->fetch()){
800       if(preg_match("/ou=apps,/",$attrs['dn'])){
801         $bb = preg_replace("/ou=apps,.*/","",$attrs['dn']);
802         $parts = array_reverse(split("ou=",$bb));
804         $str ="";
805         foreach($parts as $part){
806           if(empty($part)) {
807             continue;
808           }
809           $str .= str_replace(",","",$part)."/";
810         }    
811         $name = preg_replace("/\/$/","",$str);
812         if(empty($name)) {
813           $name ="/";
814         }
815         $ret[$attrs['dn']] = $name;
816       }
817     }
819     /* Set releases to / id nor presenet */
820     if (!count($ret)){
821       $ret['/']= '/';
822     }
824     return($ret);
825   }
827   function save_object()
828   {
829     plugin::save_object();
830   }
831  
833   function remove_from_parent()
834   {
835     plugin::remove_from_parent();
837     $ldap= $this->config->get_ldap_link();
838     $ldap->cd($this->dn);
839     $this->cleanup();
840     
841     $ldap->modify ($this->attrs); 
843     show_ldap_error($ldap->get_error(), _("Removing application information failed"));
845     /* Optionally execute a command after we're done */
846     $this->handle_post_events("remove");
847   }
850   /* Save to LDAP */
851   function save()
852   {
853     plugin::save();
855     /* Copy members */
856     $this->Categories[""]=""; 
857     $this->attrs["gosaMemberApplication"]= array();
858     $this->attrs["gosaApplicationParameter"]= array();
859     foreach($this->Categories as $name => $cats){
860       $i =0;
861       if(isset($this->gosaMemberApplication[$name])){
862         foreach($this->gosaMemberApplication[$name] as $entry){
863           if(!preg_match("/__SEPARATOR__/",$entry['App'])){
864             $this->attrs["gosaMemberApplication"][]= $entry['App']."|".$name."|".$i;
865           }
866           $i ++;
867         }
868       }
869       if(($i==0)&&(!empty($name))){
870         $this->attrs["gosaMemberApplication"][]= "|".$name."|".$i;
871       }
872     }
874     /* Are there application parameters to be saved */
875     $this->attrs['gosaApplicationParameter']= array();
876     foreach($this->appoption as $name => $value){
877       if ($value != ""){
878         $this->attrs['gosaApplicationParameter'][]= "$name:$value";
879       }
880     }
882     /* Write back to LDAP */
883     $ldap= $this->config->get_ldap_link();
884     $ldap->cd($this->dn);
885     $this->cleanup();
886     $ldap->modify ($this->attrs); 
888     show_ldap_error($ldap->get_error(), _("Saving application information failed"));
890     /* Optionally execute a command after we're done */
891     if ($this->initially_was_account == $this->is_account){
892       if ($this->is_modified){
893         $this->handle_post_events("mofify");
894       }
895     } else {
896       $this->handle_post_events("add");
897     }
899   }
901   function check()
902   {
903     /* Call common method to give check the hook */
904     $message= plugin::check();
906     return ($message);
907   }
910   function reload()
911   {
912     /* Generate applist */
913     $this->apps= array();
915     /* Special handling for release managed apps 
916      */
917     $tmp = search_config($this->config->data,"faiManagement","CLASS");
918     if(!empty($tmp) && count($this->Releases)){
919       $this->enableReleaseManagement = true;
921       $tmp = array_flip($this->Releases);
922       if(isset($tmp[$this->FAIrelease])){
923         $base =  $tmp[$this->FAIrelease];
924       }else{
925         $old_r =  $this->FAIrelease;
926         $k = key($tmp);
927         $r = $tmp[$k];
928         $this->FAIrelease = $k;
929         $base = $r; 
930     
931         print_red(sprintf(_("Can't resolve the release name '%s', setting release name to '%s'. Possibly the objects base has changed."),$old_r,$k));
932       }
934       $base = preg_replace("/ou=apps,.*$/","ou=apps,".$this->curbase,$base);
935       $app_res = get_list("(objectClass=gosaApplication)", $this->ui->subtreeACL, $base,array("cn","description"));
936     }else{
937       $app_res = get_list("(objectClass=gosaApplication)", $this->ui->subtreeACL, "ou=apps,".$this->curbase,array("cn","description"));
938     }
939     
940     foreach($app_res as $attrs){
941       if (isset($attrs["description"][0])){    
942         $this->apps[$attrs["cn"][0]]=
943           $attrs["cn"][0]." (".
944           $attrs["description"][0].")";
945       } else {
946         $this->apps[$attrs["cn"][0]]=
947           $attrs["cn"][0];
948       }
949     }
951     $ldap = $this->config->get_ldap_link();
952     $ldap->cd($this->config->current['BASE']);
953     $ldap->search("objectClass=gosaApplication",array("gosaApplicationParameter","cn"));
954     $tmp = search_config($this->config->data,"faiManagement","CLASS");
955     $this->AllAppsForRelease = array();
956     if(!empty($tmp)){
958       $tmp = array_flip($this->Releases);
960       while($attrs = $ldap->fetch()){
961         
962         $testdn = preg_replace("/ou=apps,.*$/","ou=apps",$attrs['dn']);
963         $testdn = preg_replace("/^[^,]+/","",$testdn);
964         $testdn = preg_replace("/^,/","",$testdn);
966         if($testdn == preg_replace("/ou=apps,.*$/","ou=apps",$tmp[$this->FAIrelease])){
967           $this->AllAppsForRelease[$attrs['dn']] = $attrs['cn'][0];
968           $this->AllAppsForReleaseParameter[$this->FAIrelease][$attrs['cn'][0]] = $attrs;
969         }
970       }
971     }else{
972       while($attrs = $ldap->fetch()){
973         $this->AllAppsForRelease[$attrs['dn']] = $attrs['cn'][0];
974       }
975     }
977     natcasesort ($this->apps);
978     reset ($this->apps);
980     if(is_array($this->gosaMemberApplication))
981       foreach ($this->gosaMemberApplication as $cat){   
982         if(is_array($cat))
983           foreach($cat as $entry){
984             $this->used_apps[$entry['App']]= $entry['App'];
985           }
986       }
987   }
990   function addApp($cn)
991   {
992     if((isset($this->gosaMemberApplication[$this->curCatDir]))&&(is_array($this->gosaMemberApplication[$this->curCatDir]))){
993       foreach($this->gosaMemberApplication[$this->curCatDir] as $entry){
994         if($entry['App'] == $cn) return;
995       }
996     }
997     $this->gosaMemberApplication[$this->curCatDir][]= array("App"=>$cn);
998     $this->used_apps[$cn]=$cn;
999     $this->is_modified= TRUE;
1000   }
1003   function removeApp($cn)
1004   {
1005     $temp= array();
1006     foreach ($this->gosaMemberApplication as $value){
1007       if ($value != $cn){
1008         $temp[]= $value;
1009       }
1010     }
1011     $this->gosaMemberApplication= $temp;
1012     $this->is_modified= TRUE;
1013   }
1015   function getParameters($app)
1016   {
1017     $tmp = $this->getReleases();
1018     $ret = array();
1019     if(in_array($this->FAIrelease,$tmp)){
1020       $tmp2 = array_flip($tmp);
1021       $base = $tmp2[$this->FAIrelease];
1022       $ldap = $this->config->get_ldap_link();
1023       $ldap->cd($this->config->current['BASE']);
1024       $ldap->search("(&(objectClass=gosaApplication)(cn=".$app.")(gosaApplicationParameter=*))",array("gosaApplicationParameter"));
1025       if($ldap->count()){
1026         $attrs = $ldap->fetch();
1027         for($i = 0 ; $i < $attrs['gosaApplicationParameter']['count'] ; $i ++ ){
1028           $name = preg_replace("/:.*$/","",$attrs['gosaApplicationParameter'][$i]);
1029           $ret[$name] = $name;
1030         } 
1031       }
1032     }
1033     return($ret);
1034   }
1036   function GetSubdirs($dir)
1037   {
1038     $ret = array();
1039     $tmp1 = split("/",$this->curCatDir);
1040  
1041     foreach($this->Categories as $path=>$cat){
1042       $tmp2 = split("/",$path);
1043       
1044       if((empty($this->curCatDir))&&(!preg_match("/\//",$path))){
1045         $abort = false;
1046       }elseif(((count($tmp1))+1) == (count($tmp2))){
1047         $abort = false;
1048         for($i = 0 ; $i < count($tmp1) ; $i++){
1049           if($tmp1[$i] != $tmp2[$i]){
1050             $abort = true;
1051           }
1052         }
1053       }else{
1054         $abort= true;
1055       }
1056       if(!$abort){
1057         $ret[$path]=$cat;
1058       } 
1059     }
1060   return($ret);
1061   }
1064 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1065 ?>