Code

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