Code

ad21edb52d877ea5b2e8fda65d35af68276831f9
[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();
28   var $objectclasses= array("gosaApplicationGroup");
30   function appgroup ($config, $dn= NULL)
31   {
32     plugin::plugin ($config, $dn);
34     /* Load member applications */
35     if (isset ($this->attrs["gosaMemberApplication"][0])){
36       for ($i= 0; $i<$this->attrs["gosaMemberApplication"]["count"]; $i++){
37         $this->gosaMemberApplication[]=
38           $this->attrs["gosaMemberApplication"][$i];
39       }
40     }
42     /* Load application options */
43     if (isset($this->attrs['gosaApplicationParameter'])){
44       for ($i= 0; $i<$this->attrs['gosaApplicationParameter']['count']; $i++){
45         $option= preg_replace('/^[^:]+:/', '',
46             $this->attrs['gosaApplicationParameter'][$i]);
47         $name= preg_replace('/:.*$/', '',
48             $this->attrs['gosaApplicationParameter'][$i]);
49         $this->appoption[$name]= $option;
50       }
51     }
53     /* Parse MemberApplication*/
54     $tmp = array();
55     $tmp2 = array();
56     $prios = array();
57     $cats = array();
59     foreach($this->gosaMemberApplication as $memberApp){
60       if(preg_match("/\|/i",$memberApp)){
61     
62         $tmp = split("\|",$memberApp);
63  
64         if(!empty($tmp[0])){
65           $tmp2[$tmp[1]][$tmp[2]] = array("App"=>$tmp[0]);
66         }
67         if(!empty($tmp[1])){
68           $cats [$tmp[1]] = $tmp[1];
69         }
70         $prios[$tmp[1]][$tmp[2]] = $tmp[2];
71       }else{
72         $tmp2[]['App'] = $memberApp;
73       }
74     }
76     $this->Categories = $cats;
78     $this->gosaMemberApplication = $tmp2;
79     $cats[""]="";
80     foreach($cats as $cat ){
81       if((isset($prios[$cat]))&&(count($prios[$cat]))){
82         $max = max($prios[$cat]);
83         $min = 1;//min($prios[$cat]);  
84         $last = false;
85         for($i = $min ; $i < $max ; $i++){
86           if(!isset($prios[$cat][$i])){
87             if($last == false){
88               $this->gosaMemberApplication[$cat][$i]['App'] = "__SEPARATOR__".$i;
89             
90               $last = true;
91             }
92           }else{
93             $last = false;
94           }
95         }
96       }
97     }
99     $tmp = array();
100     foreach($this->gosaMemberApplication as $key =>  $entries){
101       ksort ($entries);
102       foreach($entries as $entry){
103         $tmp[$key][]= $entry;
104       }
105     }
106     $this->gosaMemberApplication = $tmp;
107     $this->curbase = $this->config->current['BASE'];
108   }
110   function getOneUp($appl)
111   {
112     $cat = $this->curCatDir;
113     if(!isset($this->gosaMemberApplication[$cat][($appl-1)])) return;
114     $f = 1;
115     if($appl == 1 ){
116       $f = 0;
117     }
118     $first  = array_slice($this->gosaMemberApplication[$cat],0,($appl-1));
119     $middle = array_slice($this->gosaMemberApplication[$cat],$appl-1,(($appl+1)-$f));
120     $last   = array_slice($this->gosaMemberApplication[$cat],($appl+1));
121     $tmp = array();
122     foreach($first as $entry){
123       $tmp[] = $entry;
124     }
125     foreach(array_reverse($middle) as $entry){
126       $tmp[] = $entry;
127     }
128     foreach($last as $entry){
129       $tmp[] = $entry;
130     }
131     $this->gosaMemberApplication[$cat] =$tmp;
132   }
133   
134   function getOneDown($appl){
135     $cat = $this->curCatDir;
136     if(!isset($this->gosaMemberApplication[$cat][($appl+1)])) return;
137     $f = 0;
138     if($appl == 0 ){
139       $f = 1;
140     }
141     $first  = array_slice($this->gosaMemberApplication[$cat],0,($appl));
142     $middle = array_slice($this->gosaMemberApplication[$cat],$appl,(($appl+1+$f)));
143     $last   = array_slice($this->gosaMemberApplication[$cat],($appl+2));
144   
145     $tmp = array();
146     foreach($first as $entry){
147       $tmp[] = $entry;
148     }
149     foreach(array_reverse($middle) as $entry){
150       $tmp[] = $entry;
151     }
152     foreach($last as $entry){
153       $tmp[] = $entry;
154     }
155     $this->gosaMemberApplication[$cat] =$tmp;
156   }
157   
158   function AddSeperator()
159   {
160     $cat = $this->curCatDir;
161     $cnt = count($this->gosaMemberApplication[$cat]);
162     $tmp = array("App" => "__SEPARATOR__".($cnt+1));
163     $this->gosaMemberApplication[$cat][] = $tmp;
164   }
166   function execute()
167   {
168           /* Call parent execute */
169         plugin::execute();
171     if((isset($_GET['act']))&&($_GET['act']=="depopen")){
172       $dep = base64_decode($_GET['depid']);  
173       if(isset($this->config->idepartments[$dep])){
174         $this->curbase = $dep;
175       }
176     }
178     if(isset($_POST['AddSeperator'])){
179       $this->AddSeperator();
180     }
182     if((isset($_GET['act']))&&($_GET['act']=="open")){
183       $this->curCatDir = $_GET['id'];
184     }
186     /* Do we need to flip is_account state? */
187     if (isset($_POST['modify_state'])){
188       $this->is_account= !$this->is_account;
189     }
191     /* Do we represent a valid group? */
192     if (!$this->is_account && $this->parent == NULL){
193       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
194         _("This 'dn' is no appgroup.")."</b>";
195       return ($display);
196     }
198     /* Show tab dialog headers */
199     $display= "";
200     if ($this->parent != NULL){
201       if ($this->is_account){
202         $display= $this->show_header(_("Remove applications"),
203             _("This group has application features enabled. You can disable them by clicking below."));
204       } else {
205         $display.= $this->show_header(_("Create applications"),
206             _("This group has application features disabled. You can enable them by clicking below."));
207         return ($display);
208       }
209     }
212     /* Add Categorie */ 
213   
214     
215     if((isset($_POST['AddCat']))&&(isset($_POST['CatName']))&&(!empty($_POST['CatName']))){
216       
217       if(preg_match("/[^0-9a-z,\.-;:_#\+\- ]/i",$_POST['CatName'])){
218         print_red(_("Invalid character in category name."));
219       }elseif(!in_array($_POST['CatName'],$this->Categories)){ 
220         if(empty($this->curCatDir)){
221           $this->Categories[$_POST['CatName']]=$_POST['CatName'];
222         }else{
223           $this->Categories[$this->curCatDir."/".$_POST['CatName']]=$_POST['CatName'];
224         }
225       }else{
226         print_red(_("The specified category already exists."));
227       }
228     }
231     $this->reload();
232     foreach($_POST as $name => $value){
233       if(preg_match("/DelApp_/",$name)){
234         $app = $value; 
236         foreach($this->gosaMemberApplication as $key =>  $cat){
237           foreach($cat as $key2 => $app){
238             if($app['App'] == $value){
239               unset($this->gosaMemberApplication[$key][$key2]);
240               if(isset($this->used_apps[$value])){
241                 unset($this->used_apps[$value]);
242               }
243             }
244           }
245         }
246       }
247       if(preg_match("/EdiApp_/",$name)){
248         $appname = $value;
249         /* We've got the appname, get parameters from ldap */
250         $ldap= $this->config->get_ldap_link();
251         $ldap->cd($this->config->current['BASE']);
252         $ldap->search("(&(objectClass=gosaApplication)(cn=$appname))");
253         if ($ldap->count() != 1){
254           print_red (_("The selected application name is not uniq. Please check your LDAP."));
255         } else {
256           $attrs= $ldap->fetch();
257           if(isset($attrs['gosaApplicationParameter'])){
258             $this->dialog= TRUE;
260             /* Fill name and value arrays */
261             for ($i= 0; $i<$attrs['gosaApplicationParameter']['count']; $i++){
262               $option= preg_replace('/^[^:]+:/', '',
263                   $attrs['gosaApplicationParameter'][$i]);
264               $name= preg_replace('/:.*$/', '', 
265                   $attrs['gosaApplicationParameter'][$i]);
266               $this->option_name[$i]= $name;
268               /* Fill with values from application, default should be
269                  loaded by the external scripts */
270               if (isset($this->appoption[$name])){
271                 $this->option_value[$i]= $this->appoption[$name];
272               }
273             }
275             /* Create edit field */
276             $table= "<table summary=\"\">";
277             for ($i= 0; $i < count($this->option_name); $i++){
278               if (isset($this->option_value[$i])){
279                 $value= $this->option_value[$i];
280               } else {
281                 $value= "";
282               }
283               $table.="<tr><td>".$this->option_name[$i]."</td><td>".
284                 "<input name=\"value$i\" size=60 maxlength=250 ".
285                 "value=\"".$value."\"><br></td></tr>";
286             }
287             $table.= "</table>";
288             $this->table= $table;
289           } else {
290             print_red (_("The selected application has no options."));
291           }
292         }
293       }
294     }
295     $this->reload();
297     /* Add group with post */
298     if((isset($_GET['act']))&&($_GET['act']=="add")){
299       $this->used_apps[$_GET['id']]= $_GET['id'];
300       asort($this->used_apps);
301       $this->addApp($_GET['id']);
302     }
304     /* Add multiple */
305     if(isset($_POST['AddApps'])){
306       foreach($_POST as $name => $value){
307         if(preg_match("/AddApp_/",$name)){
308           $app = preg_replace("/AddApp_/","",$name);
309           $this->addApp($app);
310         }
311       }
312     }
315     /* Cancel edit options? */
316     if (isset($_POST['edit_options_cancel'])){
317       $this->dialog= FALSE;
318     }
320     /* Finish edit options? */
321     if (isset($_POST['edit_options_finish'])){
322       $this->dialog= FALSE;
324       /* Save informations passed by the user */
325       $this->option_value= array();
326       for ($i= 0; $i<count($this->option_name); $i++){
327         $this->appoption[$this->option_name[$i]]= $_POST["value$i"];
328         $this->is_modified= TRUE;
329       }
330     }
332     /* Prepare templating stuff */
333     $smarty= get_smarty();
334     $smarty->assign("used_apps", $this->used_apps);
335     $apps= array();
336     foreach ($this->apps as $key => $value){
337       if (!array_key_exists($key, $this->used_apps)){
338         $apps["$key"]= "$value";
339       }
340     }
342     $div = new DivSelectBox("appgroup");    
344     $div->SetHeight(300);
346     /* NEW LIST MANAGMENT
347      * We also need to search for the departments
348      * So we are able to navigate like in konquerer
349      */
351     $ldap = $this->config->get_ldap_link();
352     $ldap->cd($this->curbase) ;
353     $ldap->ls("(objectClass=gosaDepartment)"); 
354     $departments= array();
355     $tmp = array();
356     while ($value = $ldap->fetch()){
357       $tmp[strtolower($value['dn']).$value['dn']]=$value;
358     }
359     ksort($tmp);
360     foreach($tmp as $value){
361       if($value["description"][0]!=".."){
362         $departments[$value['dn']]=convert_department_dn($value['dn'])." - [".$value["description"][0]."]";
363       }else{
364         $departments[$value['dn']]=convert_department_dn($value['dn']);
365       }
366     }
367     
368     /* END NEW LIST MANAGMENT
369      */
371     $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=depopen&amp;depid=%s'>%s</a>";
372     $linkadd  = "<a href='?plug=".$_GET['plug']."&amp;act=add&amp;id=%s'>%s</a>";
374     $base_back = preg_replace("/^[^,]+,/","",$this->curbase);
375     if((strlen($base_back)>= strlen($this->config->current['BASE']))&&($this->curbase!=$this->config->current['BASE'])){
376       $div->AddEntry(array(
377             array("string"=>sprintf($linkopen,base64_encode($base_back),".. ["._("back")."]"),
378                   "attach"=>"style='border:0px;'")
379             ));
380     }
381     foreach($departments as $key => $app){
382       $div->AddEntry(array(
383                             array("string"=>"<img src='images/folder.png' alt='"._("department")."'>&nbsp;".sprintf($linkopen,base64_encode($key),$app),
384                                   "attach"=>"style='border:0px;'")
385                           ));
386     }
387     
388     foreach($apps as $key => $app){
389       $div->AddEntry(array(
390                             array("string"=>sprintf("<input type='checkbox' value='1' name='AddApp_%s'>",$key).
391                                   "<img src='images/select_application.png' alt='"._("application")."'>&nbsp;".sprintf($linkadd,$key,$app),
392                                   "attach"=>"style='border:0px;'")
393                           ));
394     }
396     if((isset($_GET['act']))&&(($_GET['act'] == "one_up")||($_GET['act']=="one_down"))){
397       if(isset($_GET['id'])){
398         $id   = $_GET['id'];
399         $act  = $_GET['act']; 
400         $found = -1;
401         if(isset($this->gosaMemberApplication[$this->curCatDir])){
402           foreach($this->gosaMemberApplication[$this->curCatDir] as $key =>  $member){
403             if($id == $member['App']){
404               $found = $key;
405             }
406           }
407         }
409         if($found != -1){
410           if($act == "one_up"){
411             $this->getOneUp($found);
412           }elseif($act == "one_down")   { 
413             $this->getOneDown($found);
414           }
415         }
416       }
417     }
419     $div2 = new DivSelectBox("appgroup");
420     $div2->SetHeight(300);
422     $linkopen       = "<img src='images/folder.png'>&nbsp;<a href='?plug=".$_GET['plug']."&amp;act=open&amp;id=%s'>%s</a>";
423     $catremove      = "<a href='?plug=".$_GET['plug']."&amp;act=idelcat&amp;id=%s'><input type='image' src='images/edittrash.png' ></a>";
424     $app            = "<img src='images/select_application.png'>&nbsp;%s";
426     if(empty($this->curCatDir)){
427       $cnt =0;
428     }else{
429       $cnt = count(split("/",$this->curCatDir));
430       $tmp = split("/",$this->curCatDir);
431       $bbk = "";
432       for($i = 0 ; $i < ($cnt -1 ) ; $i++){
433         $bbk .= $tmp[$i];
434       }
435       $div2 ->AddEntry(array(array("string"=>sprintf($linkopen,$bbk,"..")),array("string"=>"&nbsp;","attach"=>"style='border-right:0px;'")));
436     }
437     
438     foreach($this->Categories as $path => $name){
439       $depth = count(split("/",$path));
440       if(($depth == ($cnt)+1)){
441         
442         $div2 ->AddEntry(array( 
443                               array("string"=>sprintf($linkopen,$path,$name)),
444                               array("string"=>sprintf($catremove,$path).
445                                 "<img src='images/empty.png' width='19px'>",
446                                     "attach"=>"align='right' style='width:80px;border-right:0px;'"))); 
447       }
448     }
450       /* Append entries */
452     $separator ="<hr height=1  size=1></hr>"; 
453  
454     $upudown = "<a href='?plug=".$_GET['plug']."&amp;act=one_up&id=%s'>   <img src='images/sort_up.png' border=0></a>".
455           "&nbsp;<a href='?plug=".$_GET['plug']."&amp;act=one_down&id=%s'> <img src='images/sort_down.png' border=0></a>".
456           "&nbsp;<input type='image' src='images/edittrash.png' name='DelApp_%s' value='%s'>";
457     $edit=      "&nbsp;<input type='image' src='images/edit.png' name='EdiApp_%s' value='%s'>";
459     if(isset($this->gosaMemberApplication[$this->curCatDir])){
460       foreach($this->gosaMemberApplication[$this->curCatDir] as $cat => $entry){
461         if(preg_match("/__SEPARATOR__/",$entry['App'])){
462           $div2 ->AddEntry(array(array("string"=>$separator),
463                 array("string"=>preg_replace("/\%s/",$entry['App'],$upudown."<img src='images/empty.png' width='19px'>"),"attach"=>"align='right' style='border-right:0px;'")));
464         }else{
465           $div2 ->AddEntry(array(array("string"=>sprintf($app,$entry['App'])),
466                 array("string"=>preg_replace("/\%s/",$entry['App'],$upudown.$edit),"attach"=>"align='right' style='border-right:0px;'")));
467         }
468       }
469     }
470   
471     $smarty->assign("UsedApps", $div2->DrawList());
472     $smarty->assign("List", $div->DrawList());
473     $smarty->assign("apps", $apps);
475     /* Show main page */
476     if ($this->dialog){
477       $smarty->assign("table", $this->table);
478       $display.= $smarty->fetch (get_template_path('application_options.tpl', TRUE));
479     } else {
480       $display.= $smarty->fetch (get_template_path('application.tpl', TRUE));
481     }
482     return ($display);
483   }
486   function remove_from_parent()
487   {
488     plugin::remove_from_parent();
490     $this->attrs["gosaMemberApplication"]= array();
492     $ldap= $this->config->get_ldap_link();
493     $ldap->cd($this->dn);
494     $ldap->modify($this->attrs);
495     show_ldap_error($ldap->get_error());
497     /* Optionally execute a command after we're done */
498     $this->handle_post_events("remove");
499   }
502   /* Save to LDAP */
503   function save()
504   {
505     plugin::save();
507     /* Copy members */
508     $this->Categories[""]=""; 
509     $this->attrs["gosaMemberApplication"]= array();
510     $cats = array();
511     foreach($this->Categories as $name => $cats){
512       $cats[$name] =0;
513       if(isset($this->gosaMemberApplication[$name])){
514         foreach($this->gosaMemberApplication[$name] as $entry){
515           if(!preg_match("/__SEPARATOR__/",$entry['App'])){
516             $this->attrs["gosaMemberApplication"][]= $entry['App']."|".$name."|".$cats[$name];
517           }
518           $cats[$name] = $cats[$name] + 1;
519         }
520       }
521       if(($cats[$name]==0)&&(!empty($name))){
522         $this->attrs["gosaMemberApplication"][]= "|".$name."|".$cats[$name];
523       }
524     }
526     /* Are there application parameters to be saved */
527     $this->attrs['gosaApplicationParameter']= array();
528     foreach($this->appoption as $name => $value){
529       if ($value != ""){
530         $this->attrs['gosaApplicationParameter'][]= "$name:$value";
531       }
532     }
534     /* Write back to LDAP */
535     $ldap= $this->config->get_ldap_link();
536     $ldap->cd($this->dn);
537     $ldap->modify($this->attrs);
538     show_ldap_error($ldap->get_error());
540     /* Optionally execute a command after we're done */
541     if ($this->initially_was_account == $this->is_account){
542       if ($this->is_modified){
543         $this->handle_post_events("mofify");
544       }
545     } else {
546       $this->handle_post_events("add");
547     }
549   }
551   function check()
552   {
553     $message= array();
554     return ($message);
555   }
558   function reload()
559   {
560     /* Generate applist */
561     $this->apps= array();
562     $ldap= $this->config->get_ldap_link();
563     $ldap->cd ("ou=apps,".$this->curbase);
565     $ldap->ls ("(objectClass=gosaApplication)","ou=apps,".$this->curbase);
566     while ($attrs= $ldap->fetch()){
567       if (isset($attrs["description"][0])){    
568         $this->apps[$attrs["cn"][0]]=
569           $attrs["cn"][0]." (".
570           $attrs["description"][0].")";
571       } else {
572         $this->apps[$attrs["cn"][0]]=
573           $attrs["cn"][0];
574       }
575     }
576     natcasesort ($this->apps);
577     reset ($this->apps);
579     if(is_array($this->gosaMemberApplication))
580     foreach ($this->gosaMemberApplication as $cat){   
581       if(is_array($cat))
582       foreach($cat as $entry){
583         $this->used_apps[$entry['App']]= $entry['App'];
584       }
585     }
586   }
589   function addApp($cn)
590   {
591     if((isset($this->gosaMemberApplication[$this->curCatDir]))&&(is_array($this->gosaMemberApplication[$this->curCatDir]))){
592       foreach($this->gosaMemberApplication[$this->curCatDir] as $entry){
593         if($entry['App'] == $cn) return;
594       }
595     }
596     $this->gosaMemberApplication[$this->curCatDir][]= array("App"=>$cn);
597     $this->used_apps[$cn]=$cn;
598     $this->is_modified= TRUE;
599   }
602   function removeApp($cn)
603   {
604     $temp= array();
605     foreach ($this->gosaMemberApplication as $value){
606       if ($value != $cn){
607         $temp[]= $value;
608       }
609     }
610     $this->gosaMemberApplication= $temp;
611     $this->is_modified= TRUE;
612   }
616 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
617 ?>