Code

Removed show_ldap_error() calls
[gosa.git] / gosa-plugins / goto / admin / groups / apps / class_groupApplication.inc
1 <?php
2 class appgroup extends plugin
3 {
4   var $config;
5   var $curbase;
7   /* Contains the menu structure in an array.
8    */
9   var $a_Structure= array();
10   var $a_Structure_on_load = array();
11   var $Releases;
12   var $FAIrelease = 0;
13   var $apps = array();
14   var $_cache = array();
16   var $app_parameter = array();
17   var $edit_entry    = array();
18   var $enableReleaseManagement = FALSE;
19   
21   public function __construct(&$config, $dn= NULL, $parent= NULL)
22   {
23     plugin::plugin($config,$dn,$parent);
24     $this->dn = $dn; 
25     $this->_load_menu_structure();
26     $this->a_Structure_on_load = $this->a_Structure;
28     /* Check if we have relase mangement enabled and prepare group application for release management */
29     $tmp = $config->search("faiManagement", "CLASS",array('menu','tabs'));
30     if(!empty($tmp)){
31       $this->enableReleaseManagement = true;
32     }
34     $this->Releases   = $this->getReleases();
35     $this->FAIrelease = "/";
36     $this->curbase = $this->config->current['BASE'];
37     $this->reload();
38   
39     $this->is_account = FALSE;
40     if(count($this->_get_all_entries()) > 1){
41       $this->is_account= TRUE;
42     }   
43     $this->initially_was_account = $this->is_account;
44   }
47   /*! \brief Reload the list of applications for the currently selected release 
48    */
49   function reload()
50   {
51     $ret = array();
52     $release_info = $this->Releases[$this->FAIrelease];
54     if(!isset($this->_cache['ReleaseApps'][$release_info['suffix']])){
56       $ldap = $this->config->get_ldap_link();
57       $ldap->cd($this->config->current['BASE']);
58       $ldap->search("ou=apps",array("dn"));
59       $app_deps = array();
60       while($attrs = $ldap->fetch()){
61         $app_deps[] = $attrs['dn'];
62       }
64       foreach($app_deps as $dep){
65         $ldap->cd($dep);
66         $ldap->search("objectClass=FAIbranch",array("dn"));
67         while($attrs = $ldap->fetch()){
68           $app_deps[] = $attrs['dn'];
69         }
70       }
72       foreach($app_deps as $dep){
73         if(preg_match("/^".normalizePreg($release_info['suffix'])."/",$dep)){
74           $ret = array_merge($ret,get_list("(objectClass=gosaApplication)","application",$dep,array("*"),GL_NONE));
75         }
76       }
78       $tmp = array();
79       foreach($ret as $key => $app){
80         $tmp[$key] = $app['cn'][0];
81       }
82       natcasesort($tmp);
83       $res = array();
84       foreach($tmp as $key => $app){
85         $res[] = $ret[$key];
86       }
88       $this->_cache['ReleaseApps'][$release_info['suffix']] = $res;
89     } 
90     $this->apps = $this->_cache['ReleaseApps'][$release_info['suffix']];
91   }
93   
94   
95   
96   /*! \brief generate a list of available releases
97       @return return an array with all available releases.
98     */
99   function getReleases()
100   {
101     $ret =array("/" => array("name" => "/", "found" => TRUE , "parts" => array(),"suffix" => get_ou('applicationou')));
102     if($this->enableReleaseManagement){
104       /* Only display those releases that we are able to read */
105       $dn     = $this->config->current['BASE'];
106       $filter = "(&(objectClass=organizationalUnit)(objectClass=FAIbranch))";
107       $res    = get_sub_list($filter,array("application","fai"), array(get_ou("applicationou"),get_ou("faiou")),$dn, array("ou","FAIstate"), GL_SUBSEARCH);
110       foreach($res as $attrs){
111         if(preg_match("/".get_ou('applicationou')."/",$attrs['dn'])){
112           $bb     = preg_replace("/".get_ou('applicationou').".*/","",$attrs['dn']);
113           $parts  = array_reverse(split("ou=",$bb));
115           $str ="";
116           foreach($parts as $key => $part){
117             if(empty($part)) {
118               unset($parts[$key]);
119               continue;
120             }
121             $part = str_replace(",","",$part);
122             $str .= $part."/";
123             $parts[$key] = $part;
124           }
125           $name = preg_replace("/\/$/","",$str);
126           if(empty($name)) {
127             $name ="/";
128           }
129           $FAIstate = "";
130           if(isset($attrs['FAIstate'])){
131             $FAIstate = $attrs['FAIstate'][0];
132           }
134           $all = $this->_get_all_entries();
135           $found = FALSE;
136           foreach($all as $entry){
137             if(isset($entry['DN']) && preg_match("/^".normalizePreg($bb)."/",$entry['DN'])){
138               $found =TRUE;
139               break;
140             }
141           }
143           $cnt = 0;
144           $ret[$name] = array("name"     => $name, 
145               "found"    => $found,
146               "FAIstate" => $FAIstate,
147               "dn"       => $attrs['dn'], 
148               "parts"    => $parts,"suffix" => $bb.get_ou('applicationou'));
149         }
150       }
151     }
152     ksort($ret);
153     return($ret);
154   }
157   /*! \brief Load the menu structure from ldap and create a multi dimensional array
158    */
159   function _load_menu_structure()
160   {
161     $this->a_Structure  = array();
162     $ldap = $this->config->get_ldap_link();
163     $ldap->cd($this->dn);
164     $ldap->search("(|(objectClass=gotoSubmenuEntry)(objectClass=FAIbranch)(objectClass=gotoMenuEntry))",array("*"));
166     $base =  array();
167     $base['UNIQID'] = uniqid();
168     $base['PARENT'] = 0; 
169     $base['NAME']   = "";
170     $base['TYPE']   = "BASE";
171     $base['ENTRIES']= array();
172     $base['STATUS'] = "LOADED";
173     
174     $this->a_Structure[0] = $base;
176     while($attrs = $ldap->fetch()){
177       $cur = &$this->a_Structure[0]['ENTRIES'];
178       $parent_id    = $base['UNIQID'];
179       $sub_dn       = preg_replace("/,".normalizePreg($this->dn)."$/","",$attrs['dn']);
180       $sub_dn_array = split("\,",$sub_dn);
182       $found = true;
183       for($i = (count($sub_dn_array)-1) ; $i >= 0 ; $i--){
184         $name = preg_replace("/^[^=]*+=/","",$sub_dn_array[$i]);
185         
186         if($i > 0){
187           $found =FALSE;
188           foreach($cur as $key => $entry){
189             if($entry['NAME'] == $name){
190               $cur = &$cur[$key]['ENTRIES'];
191               $parent_id = $entry['UNIQID'];
192               $found =true;
193               break;
194             }
195           }
196         }else{
197           
198           if(!$found){
199             break;
200           }
201           $priority = 1;
202           if(isset($attrs['gosaApplicationPriority'])){
203             $priority= $attrs['gosaApplicationPriority'][0];
204           }
205           while(isset($cur[$priority])){
206             $priority ++;
207           }
209           $data = array();
210           if(in_array("gotoSubmenuEntry",$attrs['objectClass'])){
211             $type = "FOLDER";
213             $data['ICON'] = "";
214             if(isset($attrs['gosaApplicationIcon'])){
215               $data['ICON'] = $ldap->get_attribute($attrs['dn'],"gosaApplicationIcon");
216             }
218           }elseif(in_array("gotoMenuEntry",$attrs['objectClass'])){
219             $type = "ENTRY";
220             $data['INFO'] = "";
221             $data['PARAMETER'] = array();
222             if(isset($attrs['gosaApplicationParameter'])){
223               for($p = 0 ; $p < $attrs['gosaApplicationParameter']['count'] ; $p ++){
224                 $tmp = split(":",$attrs['gosaApplicationParameter'][$p]);
225                 $data['PARAMETER'][$tmp[0]] = $tmp[1];
226               }
227             }
228           }elseif(in_array("FAIbranch",$attrs['objectClass'])){
230             $type = "RELEASE";
231             if(isset($attrs['FAIstate'][0])){
232               $data['FAIstate'] = $attrs['FAIstate'][0];
233             }else{
234               $data['FAIstate'] = "";
235             }
236           }
238           $data['DN']       = $attrs['dn'];
239           $data['NAME']     = $name;
240           $data['TYPE']     = $type;
241           $data['PRIORITY'] = $priority;
242           $data['ENTRIES']  = array();
243           $data['UNIQID']   = uniqid();
244           $data['PARENT']   = $parent_id;
245           $data['STATUS']   = "LOADED";
246           $cur[$priority]   = $data;
247           ksort($cur);
248         }
249       }
250     }
251   } 
254   function execute()
255   {
256     /* Call parent execute */
257     plugin::execute();
259     if (isset($_POST['modify_state'])){
260       $this->is_account = !$this->is_account;
261     }
263     /* Do we represent a valid account? */
264     if (!$this->is_account){
265       $display= $this->show_enable_header(_("Add application extension"),
266           _("Application extension disabled. You can enable it by clicking below."));
267       return ($display);
268     }
270     $display =  $this->show_enable_header(_("Remove application extension"),
271           _("Application extension enabled. You can disable it by clicking below."));
274     if(isset($_GET['send'])){
275       $id = $_GET['send'];
276       $all = $this->_get_all_entries();
277       if(isset($all[$id])){
278         send_binary_content($all[$id]['ICON'],$id.".jpg","image/jpeg");
279         exit;
280       }
281     }
283     if(isset($_GET['r']))
284     $this->__construct($this->config,$this->dn);
286     if(count($this->edit_entry)){
287       if($this->edit_entry['TYPE'] == "ENTRY"){
288         $smarty = get_smarty();
289         $smarty->assign("type", "ENTRY");
290         $smarty->assign("entry",$this->edit_entry);
291         $smarty->assign("paras",$this->app_parameter);
292         $display= $smarty->fetch (get_template_path('edit_entry.tpl', TRUE, dirname(__FILE__)));
293         return($display);
294       }
295       if($this->edit_entry['TYPE'] == "FOLDER"){
296         $smarty = get_smarty();
298         session::set("binarytype" , "image/jpeg");
299         session::set("binary" , $this->edit_entry['ICON']);
300   
301         $smarty->assign("rand", microtime(TRUE));
302         $smarty->assign("image_set" , strlen($this->edit_entry['ICON']) > 0); 
303         $smarty->assign("type", "FOLDER");
304         $smarty->assign("entry",$this->edit_entry);
305         $display= $smarty->fetch (get_template_path('edit_entry.tpl', TRUE, dirname(__FILE__)));
306         return($display);
307       }
308     }
310     $smarty = get_smarty();
311     $smarty->assign("plug_id" , $_GET['plug']);
313     /* Create application list */
314     $div = new divSelectBox("appgroup");
315     $div->SetHeight(300);
316     $departments = array();
317     $res = get_list("(objectClass=gosaDepartment)", "application", $this->curbase,array("description","cn","ou"),GL_SIZELIMIT);
318     foreach($res as $value){
319       $fdn = $value['dn'];
320       $fdn = preg_replace("/".normalizePreg($this->curbase)."/","",$fdn);
321       $fdn= @LDAP::fix($fdn);
322       if($value["description"][0]!=".."){
323         $departments[$value['dn']]= convert_department_dn($fdn)." - [".$value["description"][0]."]";
324       }else{
325         $departments[$value['dn']]=convert_department_dn($fdn)." ["._("Back")."]";
326       }
327     }
329     $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=depopen&amp;depid=%s'>%s</a>";
331     /* Create base back entry */
332     $base_back = preg_replace("/^[^,]+,/","",$this->curbase);
333     if((strlen($base_back)>= strlen($this->config->current['BASE']))&&($this->curbase!=$this->config->current['BASE'])){
334       $div->AddEntry(array(
335             array("string"=>sprintf($linkopen,base64_encode($base_back),".. ["._("back")."]"),
336               "attach"=>"style='border:0px;'")
337             ));
338     }
340     /* Append departments for current base */
341     foreach($departments as $key => $app){
342       $div->AddEntry(array(
343             array("string"=>"<img class='center' src='images/folder.png' alt='"._("department")."'>&nbsp;".sprintf($linkopen,
344                 base64_encode($key),$app),
345               "attach"=>"style='border:0px;'")
346             ));
347     }
349   
350     /* Add applications found on this base */
351     $used_apps = $this->_get_used_entry_name();
352     foreach($this->apps as $key => $app){
353       if(in_array($app['cn'][0],$used_apps)){
354         continue;
355       }
356       if(!preg_match("/".get_ou('applicationou').normalizePreg($this->curbase)."$/",$app['dn'])){
357         continue;
358       }
360       $name = $app['cn'][0];
361       if(isset($app['description'])){
362         $name .= "&nbsp;[".$app['description'][0]."]";
363       }
364       $div->AddEntry(array(
365             array("string"=>sprintf("<input class='center' type='checkbox' value='1' name='AddApp_%s'>",$key).
366               "<img class='center' src='images/select_application.png' alt='"._("application")."'>&nbsp;".$name,
367               "attach"=>"style='border:0px;'")
368             ));
369     }
371     
372     $smarty->assign("enableReleaseManagement",$this->enableReleaseManagement);
373     $smarty->assign("FAIrelease",$this->FAIrelease);
374     $smarty->assign("app_list",$div->DrawList());
375     $smarty->assign("i",0);
376     $smarty->assign("releases",$this->Releases);
377     $smarty->assign("folders" , $this->_get_folder_names());
378     $entries = $this->_get_entries_for_release($this->FAIrelease);
379     $smarty->assign("entries",$entries);
380     $display.= $smarty->fetch (get_template_path('app_list.tpl', TRUE, dirname(__FILE__)));
381     return($display);
382   }
384    
385   /*! \brief Returns all used folder names 
386       @return Array  All used folder names.
387    */ 
388   function _get_folder_names()
389   {
390     $data = $this->_get_entries_for_release($this->FAIrelease);
391     $all    = $this->_get_all_entries();
392     $ret = array("BASE" => ".");
393     foreach($data as $entry){
395       if($entry['TYPE'] == "FOLDER"){
396         $str = $entry['NAME'];
397         $parent = $entry['PARENT'];
398         $i = 10;
399         while(isset($all[$parent]) && $i){  
400           $i --;
401           $parent_o = $all[$parent];
402           $str      = $parent_o['NAME']."/".$str;
403           $parent   = $all[$parent_o['UNIQID']]['PARENT'];
404         }        
405         $ret[$entry['UNIQID']] = $str;
406       }
407     }
408     return($ret);
409   }
412   /*! \brief return all used applications 
413       @return Array  All used applications.
414    */ 
415   function _get_used_entry_name()
416   {
417     $data = $this->_get_entries_for_release($this->FAIrelease);
418     $ret = array();
419     foreach($data as $entry){
420       if($entry['TYPE'] == "ENTRY"){
421         $ret[] = $entry['NAME'];
422       }
423     }
424     return($ret);
425   }
428   /*! \brief Returns all folder an entries for the selected release 
429       @return Array  Returns the complete menu structure for the given array.
430    */ 
431   function _get_entries_for_release($release,$cur = NULL)
432   {
433     $all = $this->_get_all_entries();
434     $key = $this->_get_release_key($release);
435     if(isset($all[$key]) && count($all[$key]['ENTRIES'])){
436       $res = $this->_get_all_entries(TRUE,TRUE,$all[$key]['ENTRIES']);
437       return($res);
438     } 
439     return(array());
440   }
443   /*! \brief Save the currently edited entry */
444   function _save_entry_edit()
445   {
446     $all    = $this->_get_all_entries();
447     $entry  = $this->edit_entry;
448     $r_entry= &$all[$entry['UNIQID']];
450     if($entry['TYPE'] == "ENTRY"){
451       $r_entry['PARAMETER'] = $this->app_parameter;
452       $r_entry['STATUS'] = "EDITED";
453     }
454     if($entry['TYPE'] == "FOLDER"){
455       $r_entry['ICON']   = $this->edit_entry['ICON'];
456       $r_entry['STATUS'] = "EDITED";
457     }
458     $this->dialog = FALSE;
459     $this->edit_entry = array();
460   }
463   /*! \brief prepare the entry with the given ID, to be edited.
464    */
465   function _edit_entry_edit($id)
466   {
467     $all   = $this->_get_all_entries();
468     $entry = $all[$id];
470     $this->app_parameter = array();
471     if($entry['TYPE'] == "ENTRY"){
472       $found = FALSE;
473       foreach($this->apps as $id => $app){
475         if($app['cn'][0] == $entry['NAME']){
476           $found = TRUE;
477           break;
478         }
479       }
480       if($found){
481       
482         /* Create a list of editable parameter */
483         if(isset($app['gosaApplicationParameter'])){
484           for($i = 0 ; $i < $app['gosaApplicationParameter']['count'] ; $i++) {
485             $para = $app['gosaApplicationParameter'][$i];
486             $tmp  = split(":",$para);
487             $this->app_parameter[$tmp[0]] = $tmp[1];
488           }
489         }
491         /* Overwrite parameters with entry parameters */
492         foreach($entry['PARAMETER'] as $name => $value){
493           $this->app_parameter[$name] = $value;
494         }
495         
496         $this->dialog = TRUE;
497         $this->edit_entry = $entry;
498       }
499     }
501     if($entry['TYPE'] == "FOLDER"){
502       $this->dialog = TRUE;
503       $this->edit_entry = $entry;
504     }
505   }
508   function remove_from_parent()
509   {
510     $ldap = $this->config->get_ldap_link();
511     $ldap->cd($this->dn);
512     $ldap->ls("(|(objectClass=gotoSubmenuEntry)(objectClass=FAIbranch)(objectClass=gotoMenuEntry))",$this->dn,array("*"));
513     $a_remove = array();
514     while($attrs = $ldap->fetch()){
515       $a_remove[] = $attrs['dn'];
516     }
517     foreach($a_remove as $remove){
518       $ldap->rmdir_recursive($remove);
519       if (!$ldap->success()){
520         msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
521       }
522     }
523     $this->_load_menu_structure();
524   }
527   function check()
528   {
529   }
532   /*! \brief Create missing releases, if there is a release selected \
533               that is currently not part of the menu structure \
534               then create this entry
535    */
536   function _check_missing_release($release)
537   {
538     $release_info = $this->Releases[$release];
540     $parent_id = $this->a_Structure[0]['UNIQID'];
541     $cur = &$this->a_Structure[0]['ENTRIES'];
542     for($i = 0 ; $i < count($release_info['parts']) ; $i ++){
543       $part = $release_info['parts'][$i];
544       $found = FALSE;
545       foreach($cur as $key => $name){
546         if($name['NAME'] == $part){
547           $parent_id = $cur[$key]['UNIQID'];
548           $cur = &$cur[$key]['ENTRIES'];
549           
550           $found =TRUE;
551           break;
552         }
553       }
554       if(!$found){
555         $release           =  array();
556         $release['UNIQID'] = uniqid();
557         $release['PARENT'] = $parent_id;
558         $release['NAME']   = $part;
559         $release['TYPE']   = "RELEASE";
560         $release['ENTRIES']= array();
561         $release['STATUS']   = "ADDED";
562         $release['FAIstate'] =  $release_info['FAIstate'];
563         $cur[] = $release;
564         $i --;
565       }
566     }
567   }
571   function _move_entry($id,$dir)
572   {
573     $all   = $this->_get_all_entries();
574     if($dir == "down"){
575       $to = $this->_get_next($id);
576     } 
577     if($dir == "up"){
578       $to = $this->_get_last($id);
579     }
581     if(!$to){
582       return;
583     }
585     $o_to   = $all[$to];
586     $o_from = $all[$id];
588     if($o_to['PARENT'] == $o_from['UNIQID'] && $dir == "down"){
589       $to    = $this->_get_next($to,$o_from['PARENT']); 
590       $o_to  = $all[$to]; 
591     }
592  
593  
594     /* Target is ENTRY && same BASE, just switch */
595     if($o_to['PARENT'] == $o_from['PARENT'] ){
596       $parent = $all[$o_to['PARENT']];
597       $pos = 0;
598       foreach($parent['ENTRIES'] as $entry){
599         $pos ++;
600         if($entry['UNIQID'] == $to){
601           break;
602         }
603       }
604       if($dir == "up" && $pos > 0){
605         $pos --;
606       }
607       $this->_add_entry($parent['UNIQID'],$o_from,$pos);
608       $this->_remove_entry_id($id);
609       return(TRUE);
610     }
611     return(FALSE);
612   }
615  
616   function _get_last($id)
617   {
618     $all_l = array_reverse($this->_get_entries_for_release($this->FAIrelease));
619     for($i = 0 ; $i < count($all_l) ; $i ++){
620       if(isset($all_l[$i]['UNIQID']) && $all_l[$i]['UNIQID'] == $id){
621         $i++;
622         break;
623       }
624     }
625     while(isset($all_l[$i]) && !in_array($all_l[$i]['TYPE'],array("ENTRY","FOLDER","CLOSE","OPEN")) && $i < count($all_l)){
626       $i++;
627     }
629     if(!isset($all_l[$i])){
630       return(FALSE);
631     }
633     if(in_array($all_l[$i]['TYPE'],array("CLOSE","OPEN"))){
634       return($all_l[$i]['PARENT']);
635     }     
637     return($all_l[$i]['UNIQID']);
638   }
640  
641   function _get_next($id,$parent = 0)
642   {
643     $all_l = $this->_get_entries_for_release($this->FAIrelease);
644     for($i = 0 ; $i < count($all_l) ; $i ++){
645       if(isset($all_l[$i]['UNIQID']) && $all_l[$i]['UNIQID'] == $id){
646         $i++;
647         break;
648       }
649     }
650     if($parent != 0){
651       while(isset($all_l[$i]) && $all_l[$i]['PARENT'] != $parent){
652         $i++;
653       }
654     }else{
655       while(isset($all_l[$i]) && !in_array($all_l[$i]['TYPE'],array("ENTRY","FOLDER")) && $i < count($all_l)){
656         $i++;
657       }
658     }
659     if(!isset($all_l[$i])){
660       return(FALSE);
661     }
662     if(in_array($all_l[$i]['TYPE'],array("CLOSE","OPEN"))){
663       return($all_l[$i]['PARENT']);
664     }
665     return($all_l[$i]['UNIQID']);
666   }
671   /* !\brief Handle ui POSTS, like sort up/down/delete
672    */ 
673   function save_object()
674   {
675     foreach($_POST as $name => $value){
676       if(preg_match("/del_/",$name)){
677         $id = preg_replace("/^del_/","",$name);
678         $id = preg_replace("/_(x|y)$/","",$id);
679         $this->_remove_entry_id($id);
680         break;
681       }
682       if(preg_match("/app_entry_edit/",$name)){
683         $id = preg_replace("/^app_entry_edit/","",$name);
684         $id = preg_replace("/_(x|y)$/","",$id);
685         $this->_edit_entry_edit($id);
686         break;
687       }
688       if(preg_match("/up_/",$name)){
689         $id = preg_replace("/^up_/","",$name);
690         $id = preg_replace("/_(x|y)$/","",$id);
691         $this->_move_entry($id,"up");
692         break;
693       }
694       if(preg_match("/down_/",$name)){
695         $id = preg_replace("/^down_/","",$name);
696         $id = preg_replace("/_(x|y)$/","",$id);
697         $this->_move_entry($id,"down");
698         break;
699       }
700       if(preg_match("/^parameter_/",$name) && 
701         count($this->edit_entry) && $this->edit_entry['TYPE'] == "ENTRY"){
702         $name = preg_replace("/^parameter_/","",$name);
703         $this->app_parameter[$name] = $value;
704       }
705     }
706     if(isset($_POST['FAIrelease'])){
707       $this->FAIrelease = $_POST['FAIrelease'];
708       $this->_check_missing_release($this->FAIrelease);
709     }
710     if(isset($_GET['act']) && $_GET['act'] == 'depopen'){
711       $this->curbase = base64_decode($_GET['depid']);
712     }
713     if(isset($_POST['add_to_folder']) && isset($_POST['folder'])){
714       $folder = $_POST['folder'];
715       foreach($_POST as $name => $value){
716         if(preg_match("/^AddApp_[0-9]*$/",$name)){
717           $this->_add_app_id($folder,preg_replace("/^AddApp_/","",$name));   
718         }
719       }
720     }
721     if(isset($_POST['add_menu_to_folder']) && isset($_POST['menu_folder'])){
722       $folder = $_POST['menu_folder'];
723       $name = $_POST['menu_folder_name'];
724       if(strlen($name) > 0 && preg_match("/[a-z ]/i",$name)){
725         $this->_add_sub_folder($folder,$name);
726       }
727     }
728     if(isset($_POST['app_entry_save'])){ 
729       $this->_save_entry_edit();
730     }
732     if(isset($_FILES['folder_image']) && isset($_POST['folder_image_upload'])){
733       if($_FILES['folder_image']['error'] == 0 && $_FILES['folder_image']['size'] > 0){
734         $this->edit_entry['ICON'] = file_get_contents($_FILES['folder_image']['tmp_name']);
735       }
736     }
738     if(isset($_POST['edit_reset_image'])){
739       $this->edit_entry['ICON'] = "";
740     }
742     if(isset($_POST['app_entry_cancel'])){
743       $this->edit_entry = array();
744       $this->dialog = FALSE;
745     }
746     $this->reload();
747   }
749  
750   /*! \brief Returns the UNIQID of the currently selected release 
751    */ 
752   function _get_release_key($release,$add_if_missing = FALSE)
753   {
754     $release_info = $this->Releases[$release];
756     if($release_info['name'] == "/"){
757       return($this->a_Structure['0']['UNIQID']);
758     }
760     $cur = &$this->a_Structure[0]['ENTRIES'];
761     $s_key = "";
762     $found = FALSE;
763     foreach($release_info['parts'] as $name){
764       foreach($cur as $key => $obj){
765         if($obj['TYPE'] == "RELEASE" && $obj['NAME'] == $name){
766           $s_key = $cur[$key]['UNIQID'];
767           $cur = &$cur[$key]['ENTRIES'];
768           $found = TRUE;
769           break;
770         }
771         $found = FALSE;
772       }
773     }
774     if($found){
775       return($s_key);  
776     }  
777     return(FALSE);
778   }
780  
781   /*! \brief Add a new folder folder to the specified folder id
782       @param  String $folder The folder id in where we want to add the new folder.
783       @param  String $name   The name of the new folder.
784    */ 
785   function _add_sub_folder($folder,$name)
786   {
787     $all = $this->_get_all_entries();
788     if($folder == "BASE"){
789       $folder = $this->_get_release_key($this->FAIrelease,TRUE);
790     }
791     
792     if(isset($all[$folder])){
793       $a_folder = array();
794       $a_folder['STATUS'] = "ADDED";
795       $a_folder['NAME']   = $name;
796       $a_folder['UNIQID'] = uniqid();
797       $a_folder['ENTRIES']= array();
798       $a_folder['PARENT'] = $folder;      
799       $a_folder['TYPE']   = "FOLDER";
800       $a_folder['ICON']   = "";
801       $all[$folder]['ENTRIES'][] = $a_folder;
802     }
803   }
806   /* !\brief Remove the given id from the menu structure.
807       @param  String  ID to of the entry we want to remove.
808       @return Boolean TRUE on success
809    */
810   function _remove_entry_id($id)
811   {
812     $all = $this->_get_all_entries();
813     if(isset($all[$id])){
814       $all[$id]['STATUS'] = "REMOVED";
815       $all[$id]['ENTRIES'] = array();
816       return(TRUE);
817     }
818     return(FALSE);
819   }
821   
822   /* !\brief Remove the given id from the menu structure.
823       @param  String  ID to of the entry we want to remove.
824       @return Boolean TRUE on success
825    */
826   function _add_entry($folder_id,$entry,$pos = 0)
827   {
828     $all = $this->_get_all_entries();
830     /* Do not add removed */
831     if($entry['STATUS'] == "REMOVED"){
832       return;
833     }
835     if(isset($all[$folder_id])){
837       if(!isset($entry['ENTRIES'])){
838         $entries = array();
839       }else{
840         $entries = $entry['ENTRIES'];
841       }
843       $folder  = &$all[$folder_id];
845       $entry['UNIQID'] = uniqid();     
846       $entry['PARENT'] = $folder_id;
847       $entry['ENTRIES']= array();
848       $entry['STATUS'] = "ADDED";
849       
850       $cnt = 0; 
851       $new = array();
852       $added =FALSE;
853       foreach($folder['ENTRIES'] as $key => $obj){
854         if($obj['STATUS'] == "LOADED"){
855           $obj['STATUS'] = "EDITED";
856         }
857         if($pos == $cnt){
858           $new[] = $entry;
859           $added = TRUE;
860         }
861         $cnt ++;
862         $new[] = $obj;
863       }
864       if(!$added){
865         $new[] = $entry;
866       }
867   
868       $all[$folder_id]['ENTRIES'] = $new;
869       foreach($entries as $sub){
870         $this->_add_entry($entry['UNIQID'],$sub,-1);
871       }
872       return(TRUE);
873     }
874     return(FALSE);
875   }
877  
878   /*! \brief Add the application identified by $app_id to folder $folder_id 
879       @param  String  folder_id The UNIQID of the folder where we want to add the new folder.
880       @param  Integer app_id    The ID of the application which should be added.
881    */ 
882   function _add_app_id($folder_id,$app_id)
883   {
884     $all = $this->_get_all_entries();
885     if($folder_id == "BASE"){
886       $folder_id = $this->_get_release_key($this->FAIrelease);
887     }
888     if(isset($all[$folder_id]) && isset($this->apps[$app_id])){
890       $new = array();
891       $new['TYPE']  = "ENTRY";
892       $new['NAME']  = $this->apps[$app_id]['cn'][0];
893       $new['UNIQID']= uniqid(); 
894       $new['PARENT']= $folder_id;
895       $new['PARAMETER']= array();
896       if(isset($this->apps[$app_id]['description'][0])){
897         $new['INFO']  = $this->apps[$app_id]['description'][0];
898       }else{
899         $new['INFO']  = "";
900       }
901       $new['STATUS']= "ADDED";
902       $all[$folder_id]['ENTRIES'][] = $new;
903     }
904   }
907   /*! \brief  Return all entries linear. 
908       @param  Boolean   $add_tags  If TRUE, OPEN/CLOSE Tags will be appended.
909       @param  &Array    Start here, Pointer to an array.
910    */ 
911   function _get_all_entries($add_tags = FALSE, $skip_release = FALSE, &$cur = NULL)
912   {
913     $ret = array();
914     if($cur == NULL){
915       $cur = &$this->a_Structure;
916     }
917     foreach($cur as $key => $entry){
919       if($skip_release && $entry['TYPE'] == "RELEASE"){
920         continue;
921       }    
922       if($entry['TYPE'] == "ENTRY"){
923         $found = FALSE;
924         foreach($this->apps as $app){
925           if($app['cn'][0] == $entry['NAME']){
926             $found = TRUE;
927             if(isset($app['description'][0])){
928               $entry['INFO'] = "[".$app['description'][0]."]";
929             }
930             break;
931           }
932         } 
933         if(!$found){
934           $entry['INFO'] = "<font color='red'>"._("Not available in release.")."</font>";
935         }
936       }
937       
939       $tmp = $entry;
940       if(!$add_tags){
941         $ret[$tmp['UNIQID']] = &$cur[$key];
942         if(isset($entry['ENTRIES']) && count($entry['ENTRIES'])){
943           $ret = array_merge($ret,$this->_get_all_entries($add_tags,$skip_release,$cur[$key]['ENTRIES']));
944         }
945       }else{
946       
947         if(isset($tmp['ENTRIES'])){
948           unset($tmp['ENTRIES']);
949         }
950         if($tmp['STATUS'] != "REMOVED"){
951           $ret[] = $tmp;
952           if(isset($entry['ENTRIES']) && count($entry['ENTRIES'])){
953             $add = false;
954             foreach($entry['ENTRIES'] as $entry){
955               if($entry['STATUS'] != "REMOVED"){
956                 $add = TRUE;
957                 break;
958               }
959             }
961             if($add){
962               $ret[] = array("TYPE" => "OPEN", "PARENT" => $entry['PARENT']);
963               $ret = array_merge($ret,$this->_get_all_entries($add_tags,$skip_release,$cur[$key]['ENTRIES']));
964               $ret[] = array("TYPE" => "CLOSE" , "PARENT" => $entry['PARENT']);
965             }
966           }
967         }
968       }
969     }
970     return($ret);
971   }
974   function save()
975   {
976     $ldap = $this->config->get_ldap_link();
977     $all = $this->_get_all_entries();
978     $prio = 0;
979     $Actions = array("Remove" => array(),"Edit" => array() , "Add" => array());
981     foreach($all as $entry){
982       $prio ++;
983       $cur = $entry;
984       $dn = "";
986       do{  
987         if($cur['TYPE'] == "ENTRY"){
988           $dn.= "cn=".$cur['NAME'].",";
989         }elseif($cur['TYPE'] == "FOLDER"){
990           $dn.= "cn=".$cur['NAME'].",";
991         }elseif($cur['TYPE'] == "RELEASE"){
992           $dn.= "ou=".$cur['NAME'].",";
993         }elseif($cur['TYPE'] == "BASE"){
994         }
995         if(!isset($all[$cur['PARENT']])){
996           $cur = NULL;
997         }else{
998           $cur = $all[$cur['PARENT']];
999         }
1000       }while(is_array($cur));
1002       $cur_dn = $dn.$this->dn;
1004       $attrs = array();
1005       switch($entry['TYPE']){
1006         case "ENTRY"    :
1007         { 
1008           $attrs['objectClass'] = "gotoMenuEntry";
1009           $attrs['cn']          = $entry['NAME'];
1010           $attrs['gosaApplicationPriority'] = $prio;
1011           $attrs['gosaApplicationParameter'] = array(); 
1012           
1013           
1014           foreach($entry['PARAMETER'] as $name => $value){
1015             $attrs['gosaApplicationParameter'][] = $name.":".$value; 
1016           }
1017           if($entry['STATUS'] == "ADDED" && !count($attrs['gosaApplicationParameter'])){
1018             unset($attrs['gosaApplicationParameter']);
1019           } 
1020         }
1021         break;
1022         case "FOLDER"   : 
1023         { 
1024           $attrs['objectClass'] = "gotoSubmenuEntry";
1025           $attrs['cn']          = $entry['NAME'];
1026           $attrs['gosaApplicationPriority'] = $prio;
1027           if($entry['STATUS'] != "ADDED"){
1028             $attrs['gosaApplicationIcon'] = array();
1029           }
1030           
1031           if(!empty($entry['ICON'])){
1032             $attrs['gosaApplicationIcon']     = $entry['ICON'];
1033           }
1034         }
1035         break;
1036         case "RELEASE"  : 
1037         { 
1038           $attrs['ou']            = $entry['NAME'];
1039           $attrs['objectClass']   = array();
1040           $attrs['objectClass'][] = "top";
1041           $attrs['objectClass'][] = "organizationalUnit";
1042           $attrs['objectClass'][] = "FAIbranch";
1043           if(!empty($entry['FAIstate'])){
1044             $attrs['FAIstate']      = $entry['FAIstate'];
1045           }
1046         }
1047         break;
1048       }
1049   
1050       if($entry['STATUS'] == "LOADED"){
1051         continue;
1052       }
1053       if($entry['STATUS'] == "REMOVED"){
1054         $Actions['Remove'][$cur_dn] = $cur_dn;
1055       }
1056       if($entry['STATUS'] == "EDITED"){
1057         $Actions['Edit'][$cur_dn] = $attrs;
1058       }
1059       if($entry['STATUS'] == "ADDED"){
1060         $Actions['Add'][$cur_dn] = $attrs;
1061       }
1062     }
1064     $ldap = $this->config->get_ldap_link();
1065     $ldap->cd($this->config->current['BASE']);
1066     foreach($Actions['Remove'] as $dn){
1067       $ldap->cd($dn);
1068       $ldap->cat($dn);
1069       if($ldap->count()){
1070         $ldap->rmdir_recursive($dn);
1071         if (!$ldap->success()){
1072           msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $dn, LDAP_DEL, get_class()));
1073         }
1074       }
1075     }
1076     foreach($Actions['Add'] as $dn => $data){
1077       $ldap->cd($dn);
1078       $ldap->cat($dn);
1079       if(!$ldap->count()){
1080         $ldap->add($data);
1081         if (!$ldap->success()){
1082           msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $dn, LDAP_DEL, get_class()));
1083         }
1084       }
1085     }
1086     foreach($Actions['Edit'] as $dn => $data){
1087       $ldap->cd($dn);
1088       $ldap->cat($dn);
1089       if($ldap->count()){
1090         $ldap->modify($data);
1091         if (!$ldap->success()){
1092           msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $dn, LDAP_DEL, get_class()));
1093         }
1094       }
1095     }
1096     $this->_load_menu_structure();
1097   }
1100   /* Return plugin informations for acl handling  */ 
1101   static function plInfo()
1102   {
1103     return (array(
1104           "plShortName"   => _("Applications"),
1105           "plDescription" => _("Group applications"),
1106           "plSelfModify"  => FALSE,
1107           "plDepends"     => array(),
1108           "plPriority"    => 0,
1109           "plSection"     => array("admin"),
1110           "plCategory"    => array("groups"),
1111           "plProvidedAcls"=> array(
1112             "gosaMemberApplication"     => _("Application"),
1113             "FAIrelease"                => _("Release"),
1114             "gosaApplicationParameter"  => _("Application parameter"))
1115           ));
1116   }
1119   function PrepareForCopyPaste($source)
1120   {
1121   }
1124   function multiple_save_object()
1125   {
1126     if(isset($_POST['group_apps_multi'])){
1127       $this->save_object(); 
1128       plugin::multiple_save_object();    
1129   
1130       /* Get posts */
1131       foreach(array("apps") as $attr){
1132         if(isset($_POST['use_'.$attr])) {
1133           $this->multi_boxes[] = $attr;
1134         }
1135       }
1136     }
1137   }
1138   
1140   function get_multi_edit_values()
1141   {
1142     $ret = plugin::get_multi_edit_values();
1144     if(in_array("apps",$this->multi_boxes)){
1145       $ret['gosaApplicationParameter'] = $this->gosaApplicationParameter;
1146       $ret['Categories']               = $this->Categories;
1147       $ret['gosaMemberApplication']    = $this->gosaMemberApplication;
1148       $ret['FAIrelease']               = $this->FAIrelease;
1149       $ret['appoption']                = $this->appoption;
1150     }
1151     return($ret);
1152   }
1154 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1155 ?>