Code

Updated Todo
[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($prio)
74   {
75     
76     
77   }
78   
79   function getOneDown(){
81   
82   }
83   
85   function execute()
86   {
87           /* Call parent execute */
88         plugin::execute();
89     if((isset($_GET['act']))&&($_GET['act']=="depopen")){
90       $dep = base64_decode($_GET['depid']);  
91       if(isset($this->config->idepartments[$dep])){
92         $this->curbase = $dep;
93       }
94     }
97     if((isset($_GET['act']))&&($_GET['act']=="open")){
98       $this->curCatDir = $_GET['id'];
100       $found = false;
101       foreach($this->Categories as $key => $name ){
102         if($this->curCatDir==$name){
103           $tmp = array_flip(split("\/",$key));
105           $this->curCatDepth = ($tmp[$this->curCatDir]);
106           $found= true;
107         }
108       }
109       if(!$found){
110         $this->curCatDir = "";
111         $this->curCatDepth = 0;
112       }
113     }
115     /* Do we need to flip is_account state? */
116     if (isset($_POST['modify_state'])){
117       $this->is_account= !$this->is_account;
118     }
120     /* Do we represent a valid group? */
121     if (!$this->is_account && $this->parent == NULL){
122       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
123         _("This 'dn' is no appgroup.")."</b>";
124       return ($display);
125     }
127     /* Show tab dialog headers */
128     $display= "";
129     if ($this->parent != NULL){
130       if ($this->is_account){
131         $display= $this->show_header(_("Remove applications"),
132             _("This group has application features enabled. You can disable them by clicking below."));
133       } else {
134         $display.= $this->show_header(_("Create applications"),
135             _("This group has application features disabled. You can enable them by clicking below."));
136         return ($display);
137       }
138     }
140     $this->reload();
141     foreach($_POST as $name => $value){
142       if(preg_match("/DelApp_/",$name)){
143         $app = preg_replace("/DelApp_/","",$name); 
144         unset($this->used_apps[$app]);
145         unset($this->gosaMemberApplication[$app."|".$this->curCatDir]);
146       }
147       if(preg_match("/EdiApp_/",$name)){
148         $appname = $value;
149         /* We've got the appname, get parameters from ldap */
150         $ldap= $this->config->get_ldap_link();
151         $ldap->cd($this->config->current['BASE']);
152         $ldap->search("(&(objectClass=gosaApplication)(cn=$appname))");
153         if ($ldap->count() != 1){
154           print_red (_("The selected application name is not uniq. Please check your LDAP."));
155         } else {
156           $attrs= $ldap->fetch();
157           if(isset($attrs['gosaApplicationParameter'])){
158             $this->dialog= TRUE;
160             /* Fill name and value arrays */
161             for ($i= 0; $i<$attrs['gosaApplicationParameter']['count']; $i++){
162               $option= preg_replace('/^[^:]+:/', '',
163                   $attrs['gosaApplicationParameter'][$i]);
164               $name= preg_replace('/:.*$/', '', 
165                   $attrs['gosaApplicationParameter'][$i]);
166               $this->option_name[$i]= $name;
168               /* Fill with values from application, default should be
169                  loaded by the external scripts */
170               if (isset($this->appoption[$name])){
171                 $this->option_value[$i]= $this->appoption[$name];
172               }
173             }
175             /* Create edit field */
176             $table= "<table summary=\"\">";
177             for ($i= 0; $i < count($this->option_name); $i++){
178               if (isset($this->option_value[$i])){
179                 $value= $this->option_value[$i];
180               } else {
181                 $value= "";
182               }
183               $table.="<tr><td>".$this->option_name[$i]."</td><td>".
184                 "<input name=\"value$i\" size=60 maxlength=250 ".
185                 "value=\"".$value."\"><br></td></tr>";
186             }
187             $table.= "</table>";
188             $this->table= $table;
189           } else {
190             print_red (_("The selected application has no options."));
191           }
192         }
193       }
194     }
195     $this->reload();
197     /* Add group with post */
198     if((isset($_GET['act']))&&($_GET['act']=="add")){
199       $this->used_apps[$_GET['id']]= $_GET['id'];
200       asort($this->used_apps);
201       $this->addApp($_GET['id']);
202     }
204     /* Add multiple */
205     if(isset($_POST['AddApps'])){
206       foreach($_POST as $name => $value){
207         if(preg_match("/AddApp_/",$name)){
208           $app = preg_replace("/AddApp_/","",$name);
209           $this->addApp($app);
210         }
211       }
212     }
215     /* Cancel edit options? */
216     if (isset($_POST['edit_options_cancel'])){
217       $this->dialog= FALSE;
218     }
220     /* Finish edit options? */
221     if (isset($_POST['edit_options_finish'])){
222       $this->dialog= FALSE;
224       /* Save informations passed by the user */
225       $this->option_value= array();
226       for ($i= 0; $i<count($this->option_name); $i++){
227         $this->appoption[$this->option_name[$i]]= $_POST["value$i"];
228         $this->is_modified= TRUE;
229       }
230     }
232     /* Prepare templating stuff */
233     $smarty= get_smarty();
234     $smarty->assign("used_apps", $this->used_apps);
235     $apps= array();
236     foreach ($this->apps as $key => $value){
237       if (!array_key_exists($key, $this->used_apps)){
238         $apps["$key"]= "$value";
239       }
240     }
242     $div = new DivSelectBox("appgroup");    
244     $div->SetHeight(300);
246     /* NEW LIST MANAGMENT
247      * We also need to search for the departments
248      * So we are able to navigate like in konquerer
249      */
251     $ldap = $this->config->get_ldap_link();
252     $ldap->cd($this->curbase) ;
253     $ldap->ls("(objectClass=gosaDepartment)"); 
254     $departments= array();
255     $tmp = array();
256     while ($value = $ldap->fetch()){
257       $tmp[strtolower($value['dn']).$value['dn']]=$value;
258     }
259     ksort($tmp);
260     foreach($tmp as $value){
261       if($value["description"][0]!=".."){
262         $departments[$value['dn']]=convert_department_dn($value['dn'])." - [".$value["description"][0]."]";
263       }else{
264         $departments[$value['dn']]=convert_department_dn($value['dn']);
265       }
266     }
267     
268     /* END NEW LIST MANAGMENT
269      */
271     $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=depopen&amp;depid=%s'>%s</a>";
272     $linkadd  = "<a href='?plug=".$_GET['plug']."&amp;act=add&amp;id=%s'>%s</a>";
274     $base_back = preg_replace("/^[^,]+,/","",$this->curbase);
275     if((strlen($base_back)>= strlen($this->config->current['BASE']))&&($this->curbase!=$this->config->current['BASE'])){
276       $div->AddEntry(array(
277             array("string"=>sprintf($linkopen,base64_encode($base_back),".. ["._("back")."]"),
278                   "attach"=>"style='border:0px;'")
279             ));
280     }
281     foreach($departments as $key => $app){
282       $div->AddEntry(array(
283                             array("string"=>"<img src='images/folder.png' alt='"._("department")."'>&nbsp;".sprintf($linkopen,base64_encode($key),$app),
284                                   "attach"=>"style='border:0px;'")
285                           ));
286     }
287     
288     foreach($apps as $key => $app){
289       $div->AddEntry(array(
290                             array("string"=>sprintf("<input type='checkbox' value='1' name='AddApp_%s'>",$key).
291                                   "<img src='images/select_application.png' alt='"._("application")."'>&nbsp;".sprintf($linkadd,$key,$app),
292                                   "attach"=>"style='border:0px;'")
293                           ));
294     }
296     if((isset($_GET['act']))&&(($_GET['act'] == "one_up")||($_GET['act']=="one_down"))){
297       if(isset($_GET['id'])){
298         $id   = $_GET['id'];
299         $act  = $_GET['act']; 
300         $found = false;
301         foreach($this->gosaMemberApplication as $key =>  $member){
302           if($id == $member['App']){
303             $found = $key;
304           }
305         }
307         if($found != false){
308           if($act == "one_up"){
309             $this->gosaMemberApplication[$found]['Pri'] = $this->gosaMemberApplication[$found]['Pri'] -1;
310           }elseif($act == "one_down")   { 
311             $this->gosaMemberApplication[$found]['Pri'] = $this->gosaMemberApplication[$found]['Pri'] +1;
312           }
313         }
314       }
315     }
317     $div2 = new DivSelectBox("appgroup");
318     $div2->SetHeight(300);
320     $menu           = $this->CreateCatMenu();
321     $str_noprio     = "&nbsp; [ "._("Priority: %s")." ] ";
322     $linkopen       = "<img src='images/folder.png'>            &nbsp;<a href='?plug=".$_GET['plug']."&amp;act=open&amp;id=%s'>%s</a>";
323     $app   = "<img src='images/select_application.png'>&nbsp;%s".$str_noprio;
325         /* append back entry */
326     if($menu["__BACK__"] != false){
327         $div2 ->AddEntry(array(
328                     array("string"=>sprintf($linkopen,$menu["__BACK__"],".. [ "._("back")." ]")),
329                     array("string"=>"&nbsp;","attach"=>"style='border-right:0px;'")
330                     ));
331     }
333     foreach($menu['__CATEGORY__'] as $path => $name){
334       $div2 ->AddEntry(array(array("string"=>sprintf($linkopen,$path,$name)),array("string"=>"&nbsp;","attach"=>"style='border-right:0px;'"))); 
335     }
337       /* Append entries */
338   
339     $upudown = "<a href='?plug=".$_GET['plug']."&amp;act=one_up&id=%s'>   <img src='images/sort_up.png' border=0></a>".
340           "&nbsp;<a href='?plug=".$_GET['plug']."&amp;act=one_down&id=%s'> <img src='images/sort_down.png' border=0></a>".
341           "&nbsp;<input type='image' src='images/edittrash.png' name='DelApp_%s' value='%s'>".
342           "&nbsp;<input type='image' src='images/edit.png' name='EdiApp_%s' value='%s'>";
343  
344     foreach($menu["__ENTRY__"] as $path => $entry){
345       $div2 ->AddEntry(array(array("string"=>sprintf($app,$entry['name'],$entry['prio'])),
346                              array("string"=>sprintf($upudown,$entry['name'],$entry['name'],$entry['name'],$entry['name'],$entry['name'],$entry['name']),"attach"=>"style='border-right:0px;'")));
347     }
349     $smarty->assign("UsedApps", $div2->DrawList());
350     $smarty->assign("List", $div->DrawList());
351     $smarty->assign("apps", $apps);
353     /* Show main page */
354     if ($this->dialog){
355       $smarty->assign("table", $this->table);
356       $display.= $smarty->fetch (get_template_path('application_options.tpl', TRUE));
357     } else {
358       $display.= $smarty->fetch (get_template_path('application.tpl', TRUE));
359     }
360     return ($display);
361   }
364   function remove_from_parent()
365   {
366     plugin::remove_from_parent();
368     $this->attrs["gosaMemberApplication"]= array();
370     $ldap= $this->config->get_ldap_link();
371     $ldap->cd($this->dn);
372     $ldap->modify($this->attrs);
373     show_ldap_error($ldap->get_error());
375     /* Optionally execute a command after we're done */
376     $this->handle_post_events("remove");
377   }
380   /* Save data to object */
381 #  function save_object()
382 #  {
383 #    plugin::save_object();
384 #  }
387   /* Save to LDAP */
388   function save()
389   {
390     plugin::save();
392     /* Copy members */
393     $this->attrs["gosaMemberApplication"]= array();
394     foreach ($this->gosaMemberApplication as $val){
395       $this->attrs["gosaMemberApplication"][]= $val['App']."|".$val['Cat']."|".$val['Pri'];
396     }
398     /* Are there application parameters to be saved */
399     $this->attrs['gosaApplicationParameter']= array();
400     foreach($this->appoption as $name => $value){
401       if ($value != ""){
402         $this->attrs['gosaApplicationParameter'][]= "$name:$value";
403       }
404     }
406     /* Write back to LDAP */
407     $ldap= $this->config->get_ldap_link();
408     $ldap->cd($this->dn);
409     $ldap->modify($this->attrs);
410     show_ldap_error($ldap->get_error());
412     /* Optionally execute a command after we're done */
413     if ($this->initially_was_account == $this->is_account){
414       if ($this->is_modified){
415         $this->handle_post_events("mofify");
416       }
417     } else {
418       $this->handle_post_events("add");
419     }
421   }
423   function check()
424   {
425     $message= array();
426     return ($message);
427   }
430   function reload()
431   {
432     /* Generate applist */
433     $this->apps= array();
434     $ldap= $this->config->get_ldap_link();
435     $ldap->cd ("ou=apps,".$this->curbase);
437     $ldap->ls ("(objectClass=gosaApplication)","ou=apps,".$this->curbase);
438     while ($attrs= $ldap->fetch()){
439       if (isset($attrs["description"][0])){    
440         $this->apps[$attrs["cn"][0]]=
441           $attrs["cn"][0]." (".
442           $attrs["description"][0].")";
443       } else {
444         $this->apps[$attrs["cn"][0]]=
445           $attrs["cn"][0];
446       }
447     }
448     natcasesort ($this->apps);
449     reset ($this->apps);
451     foreach ($this->gosaMemberApplication as $value){
452       $this->used_apps[$value['App']]= $value['App'];
453     }
454   }
465   function CreateCatMenu()
466   {
467     /* The current category */
468     $na = $this->curCatDir;
470     /* the current folder depth */
471     $nd = $this->curCatDepth;
473     /* init array */
474     $return = array();
476     $return["__CATEGORY__"] = array();  // Categories
477     $return["__ENTRY__"]    = array();  // Entries in this category
478     $return["__BACK__"]     = false;    // The back entry
480     $tmp = new xmlParse();
482     $this->Categories= array();
483     if(!isset($this->config->data['MAIN']['KDE_APPLICATIONS_MENU']))    {
484         print_red(_("There is no value for 'KDE_APPLICATIONS_MENU' specified in your gosa.conf."));
485     }else{
486         $path = $this->config->data['MAIN']['KDE_APPLICATIONS_MENU'];
487         if(file_exists($path)){
488             if(is_readable($path)){
489                 $tmp->parseMenu($path);
490                 $this->Categories = $tmp->GetData();
491             }else{
492                 print_red(sprintf(_("The specified file '%s' for 'KDE_APPLICATIONS_MENU' in your gosa.conf is not accessable."),$path));
493             }
494         }else{
495             print_red(sprintf(_("The specified file '%s' for 'KDE_APPLICATIONS_MENU' in your gosa.conf is not accessable."),$path));
496         }
497     }
500     /* Create new categories to return */
501     foreach($this->Categories as $key => $val){
503         /* Split categories by | to get name and priority */
504         $nk = split("\/",$key);
506         /* Check if this category must be shown */
507         if((isset($nk[$nd]))&&(is_array($nk))&&($nk[$nd] == $na)){
509             /* Add this to the list, if theres is a sub category */
510             if(isset($nk[($nd+1)])){
511                 $return["__CATEGORY__"][$nk[($nd+1)]] = $nk[($nd+1)];
512             }
514             /* Create back entry */
515             if($nd !=0 ){
516                 $return['__BACK__'] = $nk[($nd-1)];
517             }else{
518                 $return['__BACK__'] = "..";
519             }
521         }elseif($na==""){
522             $return["__CATEGORY__"][$nk[0]] = $nk[0];
523         }
527     }
529     /* If back entry isn't set, set it to false (none) or to ..(base)*/
530     if($return["__BACK__"] == false){
531         $return['__BACK__'] = "..";
532         if($nd == 0 ){
533             $return['__BACK__'] = false;
534         }
535     }
537     foreach($this->gosaMemberApplication as $app){
538       if($app['Cat'] == $this->curCatDir){
539        $return["__ENTRY__"][$app['Pri']][$app['App']]  =  array("name"=>$app['App'],"prio" => $app['Pri']) ;
540       }
541     }    
544     ksort($return["__ENTRY__"]);
545  /* recreate array index */
546     $tmp = array();
547     foreach($return["__ENTRY__"] as $prio => $entries){
548         ksort($entries);
549         foreach($entries as $entry){
550             $tmp[$entry['name']] = $entry;
551         }
552     }
554     /* Assign sorted entries */
555     $return['__ENTRY__'] = ($tmp);
557     /* Return this all ..*/
558     return($return);
559   }
561   function addApp($cn)
562   {
563     $this->gosaMemberApplication[$cn."|".$this->curCatDir]= array("App"=>$cn,"Cat"=>$this->curCatDir,"Pri"=>"100");
564     $this->used_apps[$cn]=$cn;
565     $this->is_modified= TRUE;
566   }
568   function removeApp($cn)
569   {
570     $temp= array();
571     foreach ($this->gosaMemberApplication as $value){
572       if ($value != $cn){
573         $temp[]= $value;
574       }
575     }
576     $this->gosaMemberApplication= $temp;
577     $this->is_modified= TRUE;
578   }
582 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
583 ?>