Code

fixed style
[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 $department= "";
14   var $apps= array();
15   var $used_apps= array();
16   var $opt_edit= FALSE;
17   var $option_name= array();
18   var $option_value= array();
19   var $appoption= array();
20   var $table= "";
21   var $curbase   = "";
22   var $curCatDir;
23   var $curCatDepth=0;
24   var $Categories=array();
27   /* attribute list for save action */
28   var $attributes= array();
29   var $objectclasses= array("gosaApplicationGroup");
31   function appgroup ($config, $dn= NULL)
32   {
33     plugin::plugin ($config, $dn);
35     /* Load member applications */
36     if (isset ($this->attrs["gosaMemberApplication"][0])){
37       for ($i= 0; $i<$this->attrs["gosaMemberApplication"]["count"]; $i++){
38         $this->gosaMemberApplication[]=
39           $this->attrs["gosaMemberApplication"][$i];
40       }
41     }
43     /* Load application options */
44     if (isset($this->attrs['gosaApplicationParameter'])){
45       for ($i= 0; $i<$this->attrs['gosaApplicationParameter']['count']; $i++){
46         $option= preg_replace('/^[^:]+:/', '',
47             $this->attrs['gosaApplicationParameter'][$i]);
48         $name= preg_replace('/:.*$/', '',
49             $this->attrs['gosaApplicationParameter'][$i]);
50         $this->appoption[$name]= $option;
51       }
52     }
54     $tmp = array();
55     $tmp2 = array();
56     foreach($this->gosaMemberApplication as $memberApp){
57       if(preg_match("/\|/i",$memberApp)){
58         $tmp = split("\|",$memberApp);
59         $tmp2[$tmp[0]."|".$tmp[1]]['App'] = $tmp[0];
60         $tmp2[$tmp[0]."|".$tmp[1]]['Cat'] = $tmp[1];
61         $tmp2[$tmp[0]."|".$tmp[1]]['Pri'] = $tmp[2];
62       }else{
63         $tmp2[$memberApp."|"]['App'] = $memberApp;
64         $tmp2[$memberApp."|"]['Cat'] = "";
65         $tmp2[$memberApp."|"]['Pri'] = 100;
66       }
67     }
68     $this->gosaMemberApplication = $tmp2;
69   
70     $this->curbase = $this->config->current['BASE'];
71   }
73   function getOneUp($appl)
74   {
75     $prio = $this->gosaMemberApplication[$appl]['Pri'];
76     $prios = array();
77     $next = 0;
78     $key_use = false;
79     foreach($this->gosaMemberApplication as $key => $app){
80       if($app['Cat'] != $this->gosaMemberApplication[$appl]['Cat']) continue;
81       if($app['Pri']< $prio){
82         if($app['Pri'] > $next){
83           $next     = $app['Pri'];
84           $key_use  = $key;
85         }
86       }        
87     }
89     if($next != 0){
90       $this->gosaMemberApplication[$appl]['Pri']= $next;
91       $this->gosaMemberApplication[$key_use]['Pri']= $prio;
92     }
93   }
94   
95   function getOneDown($prio){
96     return($prio); 
97   }
98   
99   function getNextFree($prio,$cat)
100   {
101     $ret = $prio;
102     $prios =array();
103     foreach($this->gosaMemberApplication as $app){
104       if($cat == $app['Cat']){
105         $prios[$app['Pri']]=$app['Pri'];
106       }
107     }
108     while(isset($prios[$ret])){
109       $ret ++;
110     }
111     return($ret);
112   }
115   function execute()
116   {
117           /* Call parent execute */
118         plugin::execute();
119     if((isset($_GET['act']))&&($_GET['act']=="depopen")){
120       $dep = base64_decode($_GET['depid']);  
121       if(isset($this->config->idepartments[$dep])){
122         $this->curbase = $dep;
123       }
124     }
127     if((isset($_GET['act']))&&($_GET['act']=="open")){
128       $this->curCatDir = $_GET['id'];
130       $found = false;
131       foreach($this->Categories as $key => $name ){
132         if($this->curCatDir==$name){
133           $tmp = array_flip(split("\/",$key));
135           $this->curCatDepth = ($tmp[$this->curCatDir]);
136           $found= true;
137         }
138       }
139       if(!$found){
140         $this->curCatDir = "";
141         $this->curCatDepth = 0;
142       }
143     }
145     /* Do we need to flip is_account state? */
146     if (isset($_POST['modify_state'])){
147       $this->is_account= !$this->is_account;
148     }
150     /* Do we represent a valid group? */
151     if (!$this->is_account && $this->parent == NULL){
152       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
153         _("This 'dn' is no appgroup.")."</b>";
154       return ($display);
155     }
157     /* Show tab dialog headers */
158     $display= "";
159     if ($this->parent != NULL){
160       if ($this->is_account){
161         $display= $this->show_header(_("Remove applications"),
162             _("This group has application features enabled. You can disable them by clicking below."));
163       } else {
164         $display.= $this->show_header(_("Create applications"),
165             _("This group has application features disabled. You can enable them by clicking below."));
166         return ($display);
167       }
168     }
170     $this->reload();
171     foreach($_POST as $name => $value){
172       if(preg_match("/DelApp_/",$name)){
173         $app = preg_replace("/DelApp_/","",$name); 
174         unset($this->used_apps[$app]);
175         unset($this->gosaMemberApplication[$app."|".$this->curCatDir]);
176       }
177       if(preg_match("/EdiApp_/",$name)){
178         $appname = $value;
179         /* We've got the appname, get parameters from ldap */
180         $ldap= $this->config->get_ldap_link();
181         $ldap->cd($this->config->current['BASE']);
182         $ldap->search("(&(objectClass=gosaApplication)(cn=$appname))");
183         if ($ldap->count() != 1){
184           print_red (_("The selected application name is not uniq. Please check your LDAP."));
185         } else {
186           $attrs= $ldap->fetch();
187           if(isset($attrs['gosaApplicationParameter'])){
188             $this->dialog= TRUE;
190             /* Fill name and value arrays */
191             for ($i= 0; $i<$attrs['gosaApplicationParameter']['count']; $i++){
192               $option= preg_replace('/^[^:]+:/', '',
193                   $attrs['gosaApplicationParameter'][$i]);
194               $name= preg_replace('/:.*$/', '', 
195                   $attrs['gosaApplicationParameter'][$i]);
196               $this->option_name[$i]= $name;
198               /* Fill with values from application, default should be
199                  loaded by the external scripts */
200               if (isset($this->appoption[$name])){
201                 $this->option_value[$i]= $this->appoption[$name];
202               }
203             }
205             /* Create edit field */
206             $table= "<table summary=\"\">";
207             for ($i= 0; $i < count($this->option_name); $i++){
208               if (isset($this->option_value[$i])){
209                 $value= $this->option_value[$i];
210               } else {
211                 $value= "";
212               }
213               $table.="<tr><td>".$this->option_name[$i]."</td><td>".
214                 "<input name=\"value$i\" size=60 maxlength=250 ".
215                 "value=\"".$value."\"><br></td></tr>";
216             }
217             $table.= "</table>";
218             $this->table= $table;
219           } else {
220             print_red (_("The selected application has no options."));
221           }
222         }
223       }
224     }
225     $this->reload();
227     /* Add group with post */
228     if((isset($_GET['act']))&&($_GET['act']=="add")){
229       $this->used_apps[$_GET['id']]= $_GET['id'];
230       asort($this->used_apps);
231       $this->addApp($_GET['id']);
232     }
234     /* Add multiple */
235     if(isset($_POST['AddApps'])){
236       foreach($_POST as $name => $value){
237         if(preg_match("/AddApp_/",$name)){
238           $app = preg_replace("/AddApp_/","",$name);
239           $this->addApp($app);
240         }
241       }
242     }
245     /* Cancel edit options? */
246     if (isset($_POST['edit_options_cancel'])){
247       $this->dialog= FALSE;
248     }
250     /* Finish edit options? */
251     if (isset($_POST['edit_options_finish'])){
252       $this->dialog= FALSE;
254       /* Save informations passed by the user */
255       $this->option_value= array();
256       for ($i= 0; $i<count($this->option_name); $i++){
257         $this->appoption[$this->option_name[$i]]= $_POST["value$i"];
258         $this->is_modified= TRUE;
259       }
260     }
262     /* Prepare templating stuff */
263     $smarty= get_smarty();
264     $smarty->assign("used_apps", $this->used_apps);
265     $apps= array();
266     foreach ($this->apps as $key => $value){
267       if (!array_key_exists($key, $this->used_apps)){
268         $apps["$key"]= "$value";
269       }
270     }
272     $div = new DivSelectBox("appgroup");    
274     $div->SetHeight(300);
276     /* NEW LIST MANAGMENT
277      * We also need to search for the departments
278      * So we are able to navigate like in konquerer
279      */
281     $ldap = $this->config->get_ldap_link();
282     $ldap->cd($this->curbase) ;
283     $ldap->ls("(objectClass=gosaDepartment)"); 
284     $departments= array();
285     $tmp = array();
286     while ($value = $ldap->fetch()){
287       $tmp[strtolower($value['dn']).$value['dn']]=$value;
288     }
289     ksort($tmp);
290     foreach($tmp as $value){
291       if($value["description"][0]!=".."){
292         $departments[$value['dn']]=convert_department_dn($value['dn'])." - [".$value["description"][0]."]";
293       }else{
294         $departments[$value['dn']]=convert_department_dn($value['dn']);
295       }
296     }
297     
298     /* END NEW LIST MANAGMENT
299      */
301     $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=depopen&amp;depid=%s'>%s</a>";
302     $linkadd  = "<a href='?plug=".$_GET['plug']."&amp;act=add&amp;id=%s'>%s</a>";
304     $base_back = preg_replace("/^[^,]+,/","",$this->curbase);
305     if((strlen($base_back)>= strlen($this->config->current['BASE']))&&($this->curbase!=$this->config->current['BASE'])){
306       $div->AddEntry(array(
307             array("string"=>sprintf($linkopen,base64_encode($base_back),".. ["._("back")."]"),
308                   "attach"=>"style='border:0px;'")
309             ));
310     }
311     foreach($departments as $key => $app){
312       $div->AddEntry(array(
313                             array("string"=>"<img src='images/folder.png' alt='"._("department")."'>&nbsp;".sprintf($linkopen,base64_encode($key),$app),
314                                   "attach"=>"style='border:0px;'")
315                           ));
316     }
317     
318     foreach($apps as $key => $app){
319       $div->AddEntry(array(
320                             array("string"=>sprintf("<input type='checkbox' value='1' name='AddApp_%s'>",$key).
321                                   "<img src='images/select_application.png' alt='"._("application")."'>&nbsp;".sprintf($linkadd,$key,$app),
322                                   "attach"=>"style='border:0px;'")
323                           ));
324     }
326     if((isset($_GET['act']))&&(($_GET['act'] == "one_up")||($_GET['act']=="one_down"))){
327       if(isset($_GET['id'])){
328         $id   = $_GET['id'];
329         $act  = $_GET['act']; 
330         $found = false;
331         foreach($this->gosaMemberApplication as $key =>  $member){
332           if($id == $member['App']){
333             $found = $key;
334           }
335         }
337         if($found != false){
338           if($act == "one_up"){
339             $this->getOneUp($found);
340           }elseif($act == "one_down")   { 
341             $this->getOneDown($found);
342           }
343         }
344       }
345     }
347     $div2 = new DivSelectBox("appgroup");
348     $div2->SetHeight(300);
350     $menu           = $this->CreateCatMenu();
351     $str_noprio     = " %s ";
352     $linkopen       = "<img src='images/folder.png'>            &nbsp;<a href='?plug=".$_GET['plug']."&amp;act=open&amp;id=%s'>%s</a>";
353     $app   = "<img src='images/select_application.png'>&nbsp;%s".$str_noprio;
355         /* append back entry */
356     if($menu["__BACK__"] != false){
357         $div2 ->AddEntry(array(
358                     array("string"=>sprintf($linkopen,$menu["__BACK__"],".. [ "._("back")." ]")),
359                     array("string"=>"&nbsp;","attach"=>"style='border-right:0px;'")
360                     ));
361     }
363     foreach($menu['__CATEGORY__'] as $path => $name){
364       $div2 ->AddEntry(array(array("string"=>sprintf($linkopen,$path,$name)),array("string"=>"&nbsp;","attach"=>"style='border-right:0px;'"))); 
365     }
367       /* Append entries */
368   
369     $upudown = "<a href='?plug=".$_GET['plug']."&amp;act=one_up&id=%s'>   <img src='images/sort_up.png' border=0></a>".
370           "&nbsp;<a href='?plug=".$_GET['plug']."&amp;act=one_down&id=%s'> <img src='images/sort_down.png' border=0></a>".
371           "&nbsp;<input type='image' src='images/edittrash.png' name='DelApp_%s' value='%s'>".
372           "&nbsp;<input type='image' src='images/edit.png' name='EdiApp_%s' value='%s'>";
373  
374     foreach($menu["__ENTRY__"] as $path => $entry){
375       $div2 ->AddEntry(array(array("string"=>sprintf($app,$entry['name'],$entry['prio'])),
376                              array("string"=>sprintf($upudown,$entry['name'],$entry['name'],$entry['name'],$entry['name'],$entry['name'],$entry['name']),"attach"=>"style='border-right:0px;'")));
377     }
379     $smarty->assign("UsedApps", $div2->DrawList());
380     $smarty->assign("List", $div->DrawList());
381     $smarty->assign("apps", $apps);
383     /* Show main page */
384     if ($this->dialog){
385       $smarty->assign("table", $this->table);
386       $display.= $smarty->fetch (get_template_path('application_options.tpl', TRUE));
387     } else {
388       $display.= $smarty->fetch (get_template_path('application.tpl', TRUE));
389     }
390     return ($display);
391   }
394   function remove_from_parent()
395   {
396     plugin::remove_from_parent();
398     $this->attrs["gosaMemberApplication"]= array();
400     $ldap= $this->config->get_ldap_link();
401     $ldap->cd($this->dn);
402     $ldap->modify($this->attrs);
403     show_ldap_error($ldap->get_error());
405     /* Optionally execute a command after we're done */
406     $this->handle_post_events("remove");
407   }
410   /* Save data to object */
411 #  function save_object()
412 #  {
413 #    plugin::save_object();
414 #  }
417   /* Save to LDAP */
418   function save()
419   {
420     plugin::save();
422     /* Copy members */
423     $this->attrs["gosaMemberApplication"]= array();
424     foreach ($this->gosaMemberApplication as $val){
425       $this->attrs["gosaMemberApplication"][]= $val['App']."|".$val['Cat']."|".$val['Pri'];
426     }
428     /* Are there application parameters to be saved */
429     $this->attrs['gosaApplicationParameter']= array();
430     foreach($this->appoption as $name => $value){
431       if ($value != ""){
432         $this->attrs['gosaApplicationParameter'][]= "$name:$value";
433       }
434     }
436     /* Write back to LDAP */
437     $ldap= $this->config->get_ldap_link();
438     $ldap->cd($this->dn);
439     $ldap->modify($this->attrs);
440     show_ldap_error($ldap->get_error());
442     /* Optionally execute a command after we're done */
443     if ($this->initially_was_account == $this->is_account){
444       if ($this->is_modified){
445         $this->handle_post_events("mofify");
446       }
447     } else {
448       $this->handle_post_events("add");
449     }
451   }
453   function check()
454   {
455     $message= array();
456     return ($message);
457   }
460   function reload()
461   {
462     /* Generate applist */
463     $this->apps= array();
464     $ldap= $this->config->get_ldap_link();
465     $ldap->cd ("ou=apps,".$this->curbase);
467     $ldap->ls ("(objectClass=gosaApplication)","ou=apps,".$this->curbase);
468     while ($attrs= $ldap->fetch()){
469       if (isset($attrs["description"][0])){    
470         $this->apps[$attrs["cn"][0]]=
471           $attrs["cn"][0]." (".
472           $attrs["description"][0].")";
473       } else {
474         $this->apps[$attrs["cn"][0]]=
475           $attrs["cn"][0];
476       }
477     }
478     natcasesort ($this->apps);
479     reset ($this->apps);
481     foreach ($this->gosaMemberApplication as $value){
482       $this->used_apps[$value['App']]= $value['App'];
483     }
484   }
495   function CreateCatMenu()
496   {
497     /* The current category */
498     $na = $this->curCatDir;
500     /* the current folder depth */
501     $nd = $this->curCatDepth;
503     /* init array */
504     $return = array();
506     $return["__CATEGORY__"] = array();  // Categories
507     $return["__ENTRY__"]    = array();  // Entries in this category
508     $return["__BACK__"]     = false;    // The back entry
510     $tmp = new xmlParse();
512     $this->Categories= array();
513     if(!isset($this->config->data['MAIN']['KDE_APPLICATIONS_MENU']))    {
514         print_red(_("There is no value for 'KDE_APPLICATIONS_MENU' specified in your gosa.conf."));
515     }else{
516         $path = $this->config->data['MAIN']['KDE_APPLICATIONS_MENU'];
517         if(file_exists($path)){
518             if(is_readable($path)){
519                 $tmp->parseMenu($path);
520                 $this->Categories = $tmp->GetData();
521             }else{
522                 print_red(sprintf(_("The specified file '%s' for 'KDE_APPLICATIONS_MENU' in your gosa.conf is not accessable."),$path));
523             }
524         }else{
525             print_red(sprintf(_("The specified file '%s' for 'KDE_APPLICATIONS_MENU' in your gosa.conf is not accessable."),$path));
526         }
527     }
530     /* Create new categories to return */
531     foreach($this->Categories as $key => $val){
533         /* Split categories by | to get name and priority */
534         $nk = split("\/",$key);
536         /* Check if this category must be shown */
537         if((isset($nk[$nd]))&&(is_array($nk))&&($nk[$nd] == $na)){
539             /* Add this to the list, if theres is a sub category */
540             if(isset($nk[($nd+1)])){
541                 $return["__CATEGORY__"][$nk[($nd+1)]] = $nk[($nd+1)];
542             }
544             /* Create back entry */
545             if($nd !=0 ){
546                 $return['__BACK__'] = $nk[($nd-1)];
547             }else{
548                 $return['__BACK__'] = "..";
549             }
551         }elseif($na==""){
552             $return["__CATEGORY__"][$nk[0]] = $nk[0];
553         }
557     }
559     /* If back entry isn't set, set it to false (none) or to ..(base)*/
560     if($return["__BACK__"] == false){
561         $return['__BACK__'] = "..";
562         if($nd == 0 ){
563             $return['__BACK__'] = false;
564         }
565     }
567     foreach($this->gosaMemberApplication as $app){
568       if($app['Cat'] == $this->curCatDir){
569        $return["__ENTRY__"][$app['Pri']][$app['App']]  =  array("name"=>$app['App'],"prio" => $app['Pri']) ;
570       }
571     }    
574     ksort($return["__ENTRY__"]);
575  /* recreate array index */
576     $tmp = array();
577     foreach($return["__ENTRY__"] as $prio => $entries){
578         ksort($entries);
579         foreach($entries as $entry){
580             $tmp[$entry['name']] = $entry;
581         }
582     }
584     /* Assign sorted entries */
585     $return['__ENTRY__'] = ($tmp);
587     /* Return this all ..*/
588     return($return);
589   }
591   function addApp($cn)
592   {
593     $this->gosaMemberApplication[$cn."|".$this->curCatDir]= array("App"=>$cn,"Cat"=>$this->curCatDir,"Pri"=>$this->getNextFree(1,$this->curCatDir));
594     $this->used_apps[$cn]=$cn;
595     $this->is_modified= TRUE;
596   }
598   function removeApp($cn)
599   {
600     $temp= array();
601     foreach ($this->gosaMemberApplication as $value){
602       if ($value != $cn){
603         $temp[]= $value;
604       }
605     }
606     $this->gosaMemberApplication= $temp;
607     $this->is_modified= TRUE;
608   }
612 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
613 ?>