Code

Fixed Translation Strings.
[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'])){
220             $type = "ENTRY";
221             $data['INFO'] = "";
222             $data['PARAMETER'] = array();
223             if(isset($attrs['gosaApplicationParameter'])){
224               for($p = 0 ; $p < $attrs['gosaApplicationParameter']['count'] ; $p ++){
225                 if(preg_match("/:/",$attrs['gosaApplicationParameter'][$p])){
226                   $tmp = split(":",$attrs['gosaApplicationParameter'][$p]);
227                   $data['PARAMETER'][$tmp[0]] = $tmp[1];
228                 }elseif($attrs['gosaApplicationParameter'][$p] == "*separator*"){
229                   $type = "SEPERATOR";
230                   $data['PARAMETER'] = array();
231                   break;
232                 }
233               }
234             }
235           }elseif(in_array("FAIbranch",$attrs['objectClass'])){
237             $type = "RELEASE";
238             if(isset($attrs['FAIstate'][0])){
239               $data['FAIstate'] = $attrs['FAIstate'][0];
240             }else{
241               $data['FAIstate'] = "";
242             }
243           }
245           $data['LDAP_ATTRS'] = $attrs;
246           $data['DN']       = $attrs['dn'];
247           $data['NAME']     = $name;
248           $data['TYPE']     = $type;
249           $data['PRIORITY'] = $priority;
250           $data['ENTRIES']  = array();
251           $data['UNIQID']   = uniqid();
252           $data['PARENT']   = $parent_id;
253           $data['STATUS']   = "LOADED";
254           $cur[$priority]   = $data;
255           ksort($cur);
256         }
257       }
258     }
259   } 
262   function execute()
263   {
264     /* Call parent execute */
265     plugin::execute();
267     if(isset($_GET['r'])) $this->__construct($this->config,$this->dn);
269     if (isset($_POST['modify_state'])){
270       $this->is_account = !$this->is_account;
271     }
273     /* Do we represent a valid account? */
274     if (!$this->is_account){
275       $display= $this->show_enable_header(_("Add application extension"),
276           _("Application extension disabled. You can enable it by clicking below."));
277       return ($display);
278     }
280     $display =  $this->show_enable_header(_("Remove application extension"),
281           _("Application extension enabled. You can disable it by clicking below."));
284     if(isset($_GET['send'])){
285       $id = $_GET['send'];
286       $all = $this->_get_all_entries();
287       if(isset($all[$id])){
288         send_binary_content($all[$id]['ICON'],$id.".jpg","image/jpeg");
289         exit;
290       }
291     }
293     if(isset($_GET['r']))
294     $this->__construct($this->config,$this->dn);
296     if(count($this->edit_entry)){
297       if($this->edit_entry['TYPE'] == "ENTRY"){
298         $smarty = get_smarty();
299         $smarty->assign("type", "ENTRY");
300         $smarty->assign("entry",$this->edit_entry);
301         $smarty->assign("paras",$this->app_parameter);
302         $display= $smarty->fetch (get_template_path('edit_entry.tpl', TRUE, dirname(__FILE__)));
303         return($display);
304       }
305       if($this->edit_entry['TYPE'] == "FOLDER"){
306         $smarty = get_smarty();
308         session::set("binarytype" , "image/jpeg");
309         session::set("binary" , $this->edit_entry['ICON']);
310   
311         $smarty->assign("rand", microtime(TRUE));
312         $smarty->assign("image_set" , strlen($this->edit_entry['ICON']) > 0); 
313         $smarty->assign("type", "FOLDER");
314         $smarty->assign("entry",$this->edit_entry);
315         $display= $smarty->fetch (get_template_path('edit_entry.tpl', TRUE, dirname(__FILE__)));
316         return($display);
317       }
318     }
320     $smarty = get_smarty();
321     $smarty->assign("plug_id" , $_GET['plug']);
323     /* Create application list */
324     $div = new divSelectBox("appgroup");
325     $div->SetHeight(300);
326     $departments = array();
327     $res = get_list("(objectClass=gosaDepartment)", "application", $this->curbase,array("description","cn","ou"),GL_SIZELIMIT);
328     foreach($res as $value){
329       $fdn = $value['dn'];
330       $fdn = preg_replace("/".normalizePreg($this->curbase)."/","",$fdn);
331       $fdn= @LDAP::fix($fdn);
332       if($value["description"][0]!=".."){
333         $departments[$value['dn']]= convert_department_dn($fdn)." - [".$value["description"][0]."]";
334       }else{
335         $departments[$value['dn']]=convert_department_dn($fdn)." ["._("Back")."]";
336       }
337     }
339     $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=depopen&amp;depid=%s'>%s</a>";
341     /* Create base back entry */
342     $base_back = preg_replace("/^[^,]+,/","",$this->curbase);
343     if((strlen($base_back)>= strlen($this->config->current['BASE']))&&($this->curbase!=$this->config->current['BASE'])){
344       $div->AddEntry(array(
345             array("string"=>sprintf($linkopen,base64_encode($base_back),".. ["._("back")."]"),
346               "attach"=>"style='border:0px;'")
347             ));
348     }
350     /* Append departments for current base */
351     foreach($departments as $key => $app){
352       $div->AddEntry(array(
353             array("string"=>"<img class='center' src='images/folder.png' alt='"._("department")."'>&nbsp;".sprintf($linkopen,
354                 base64_encode($key),$app),
355               "attach"=>"style='border:0px;'")
356             ));
357     }
359   
360     /* Add applications found on this base */
361     $used_apps = $this->_get_used_entry_name();
362     foreach($this->apps as $key => $app){
363       if(in_array($app['cn'][0],$used_apps)){
364         continue;
365       }
366       if(!preg_match("/".get_ou('applicationou').normalizePreg($this->curbase)."$/",$app['dn'])){
367         continue;
368       }
370       $name = $app['cn'][0];
371       if(isset($app['description'])){
372         $name .= "&nbsp;[".$app['description'][0]."]";
373       }
374       $div->AddEntry(array(
375             array("string"=>sprintf("<input class='center' type='checkbox' value='1' name='AddApp_%s'>",$key).
376               "<img class='center' src='images/select_application.png' alt='"._("application")."'>&nbsp;".$name,
377               "attach"=>"style='border:0px;'")
378             ));
379     }
381     
382     $smarty->assign("enableReleaseManagement",$this->enableReleaseManagement);
383     $smarty->assign("FAIrelease",$this->FAIrelease);
384     $smarty->assign("app_list",$div->DrawList());
385     $smarty->assign("i",0);
386     $smarty->assign("releases",$this->Releases);
387     $smarty->assign("folders" , $this->_get_folder_names());
388     $entries = $this->_get_entries_for_release($this->FAIrelease);
389     $smarty->assign("entries",$entries);
390     $display.= $smarty->fetch (get_template_path('app_list.tpl', TRUE, dirname(__FILE__)));
391     return($display);
392   }
394    
395   /*! \brief Returns all used folder names 
396       @return Array  All used folder names.
397    */ 
398   function _get_folder_names()
399   {
400     $data = $this->_get_entries_for_release($this->FAIrelease);
401     $all    = $this->_get_all_entries();
402     $ret = array("BASE" => ".");
403     foreach($data as $entry){
405       if($entry['TYPE'] == "FOLDER"){
406         $str = $entry['NAME'];
407         $parent = $entry['PARENT'];
408         $i = 10;
409         while(isset($all[$parent]) && $i){  
410           $i --;
411           $parent_o = $all[$parent];
412           $str      = $parent_o['NAME']."/".$str;
413           $parent   = $all[$parent_o['UNIQID']]['PARENT'];
414         }        
415         $ret[$entry['UNIQID']] = $str;
416       }
417     }
418     return($ret);
419   }
422   /*! \brief return all used applications 
423       @return Array  All used applications.
424    */ 
425   function _get_used_entry_name()
426   {
427     $data = $this->_get_entries_for_release($this->FAIrelease);
428     $ret = array();
429     foreach($data as $entry){
430       if($entry['TYPE'] == "ENTRY"){
431         $ret[] = $entry['NAME'];
432       }
433     }
434     return($ret);
435   }
438   /*! \brief Returns all folder an entries for the selected release 
439       @return Array  Returns the complete menu structure for the given array.
440    */ 
441   function _get_entries_for_release($release,$cur = NULL)
442   {
443     $all = $this->_get_all_entries();
444     $key = $this->_get_release_key($release);
445     if(isset($all[$key]) && count($all[$key]['ENTRIES'])){
446       $res = $this->_get_all_entries(TRUE,TRUE,$all[$key]['ENTRIES']);
447       return($res);
448     } 
449     return(array());
450   }
453   /*! \brief Save the currently edited entry */
454   function _save_entry_edit()
455   {
456     $all    = $this->_get_all_entries();
457     $entry  = $this->edit_entry;
458     $r_entry= &$all[$entry['UNIQID']];
460     if($entry['TYPE'] == "ENTRY"){
461       $r_entry['PARAMETER'] = $this->app_parameter;
462       $r_entry['STATUS'] = "EDITED";
463     }
464     if($entry['TYPE'] == "FOLDER"){
465       $r_entry['ICON']   = $this->edit_entry['ICON'];
466       $r_entry['STATUS'] = "EDITED";
467     }
468     $this->dialog = FALSE;
469     $this->edit_entry = array();
470   }
473   /*! \brief prepare the entry with the given ID, to be edited.
474    */
475   function _edit_entry_edit($id)
476   {
477     $all   = $this->_get_all_entries();
478     $entry = $all[$id];
480     $this->app_parameter = array();
481     if($entry['TYPE'] == "ENTRY"){
482       $found = FALSE;
483       foreach($this->apps as $id => $app){
485         if($app['cn'][0] == $entry['NAME']){
486           $found = TRUE;
487           break;
488         }
489       }
490       if($found){
491       
492         /* Create a list of editable parameter */
493         if(isset($app['gosaApplicationParameter'])){
494           for($i = 0 ; $i < $app['gosaApplicationParameter']['count'] ; $i++) {
495             $para = $app['gosaApplicationParameter'][$i];
496             $tmp  = split(":",$para);
497             $this->app_parameter[$tmp[0]] = $tmp[1];
498           }
499         }
501         /* Overwrite parameters with entry parameters */
502         foreach($entry['PARAMETER'] as $name => $value){
503           $this->app_parameter[$name] = $value;
504         }
505         
506         $this->dialog = TRUE;
507         $this->edit_entry = $entry;
508       }
509     }
511     if($entry['TYPE'] == "FOLDER"){
512       $this->dialog = TRUE;
513       $this->edit_entry = $entry;
514     }
515   }
518   function remove_from_parent()
519   {
520     $ldap = $this->config->get_ldap_link();
521     $ldap->cd($this->dn);
522     $ldap->ls("(|(objectClass=gotoSubmenuEntry)(objectClass=FAIbranch)(objectClass=gotoMenuEntry))",$this->dn,array("*"));
523     $a_remove = array();
524     while($attrs = $ldap->fetch()){
525       $a_remove[] = $attrs['dn'];
526     }
527     foreach($a_remove as $remove){
528       $ldap->rmdir_recursive($remove);
529       if (!$ldap->success()){
530         msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
531       }
532     }
533     $this->_load_menu_structure();
534   }
537   function check()
538   {
539   }
542   /*! \brief Create missing releases, if there is a release selected \
543               that is currently not part of the menu structure \
544               then create this entry
545    */
546   function _check_missing_release($release)
547   {
548     $release_info = $this->Releases[$release];
550     $parent_id = $this->a_Structure[0]['UNIQID'];
551     $cur = &$this->a_Structure[0]['ENTRIES'];
552     for($i = 0 ; $i < count($release_info['parts']) ; $i ++){
553       $part = $release_info['parts'][$i];
554       $found = FALSE;
555       foreach($cur as $key => $name){
556         if($name['NAME'] == $part){
557           $parent_id = $cur[$key]['UNIQID'];
558           $cur = &$cur[$key]['ENTRIES'];
559           
560           $found =TRUE;
561           break;
562         }
563       }
564       if(!$found){
565         $release           =  array();
566         $release['UNIQID'] = uniqid();
567         $release['PARENT'] = $parent_id;
568         $release['NAME']   = $part;
569         $release['TYPE']   = "RELEASE";
570         $release['ENTRIES']= array();
571         $release['STATUS']   = "ADDED";
572         $release['FAIstate'] =  $release_info['FAIstate'];
573         $cur[] = $release;
574         $i --;
575       }
576     }
577   }
581   function _move_entry($id,$dir)
582   {
583     $all   = $this->_get_all_entries();
584     if($dir == "down"){
585       $to = $this->_get_next($id);
586     } 
587     if($dir == "up"){
588       $to = $this->_get_last($id);
589     }
591     if(!$to){
592       return;
593     }
595     $o_to   = $all[$to];
596     $o_from = $all[$id];
598     if($o_to['PARENT'] == $o_from['UNIQID'] && $dir == "down"){
599       $to    = $this->_get_next($to,$o_from['PARENT']); 
600       $o_to  = $all[$to]; 
601     }
602  
603  
604     /* Target is ENTRY && same BASE, just switch */
605     if($o_to['PARENT'] == $o_from['PARENT'] ){
606       $parent = $all[$o_to['PARENT']];
607       $pos = 0;
608       foreach($parent['ENTRIES'] as $entry){
609         $pos ++;
610         if($entry['UNIQID'] == $to){
611           break;
612         }
613       }
614       if($dir == "up" && $pos > 0){
615         $pos --;
616       }
617       $this->_add_entry($parent['UNIQID'],$o_from,$pos);
618       $this->_remove_entry_id($id);
619       return(TRUE);
620     }
621     return(FALSE);
622   }
625  
626   function _get_last($id)
627   {
628     $all_l = array_reverse($this->_get_entries_for_release($this->FAIrelease));
629     for($i = 0 ; $i < count($all_l) ; $i ++){
630       if(isset($all_l[$i]['UNIQID']) && $all_l[$i]['UNIQID'] == $id){
631         $i++;
632         break;
633       }
634     }
635     while(isset($all_l[$i]) && !in_array($all_l[$i]['TYPE'],array("ENTRY","FOLDER","CLOSE","OPEN")) && $i < count($all_l)){
636       $i++;
637     }
639     if(!isset($all_l[$i])){
640       return(FALSE);
641     }
643     if(in_array($all_l[$i]['TYPE'],array("CLOSE","OPEN"))){
644       return($all_l[$i]['PARENT']);
645     }     
647     return($all_l[$i]['UNIQID']);
648   }
650  
651   function _get_next($id,$parent = 0)
652   {
653     $all_l = $this->_get_entries_for_release($this->FAIrelease);
654     for($i = 0 ; $i < count($all_l) ; $i ++){
655       if(isset($all_l[$i]['UNIQID']) && $all_l[$i]['UNIQID'] == $id){
656         $i++;
657         break;
658       }
659     }
660     if($parent != 0){
661       while(isset($all_l[$i]) && $all_l[$i]['PARENT'] != $parent){
662         $i++;
663       }
664     }else{
665       while(isset($all_l[$i]) && !in_array($all_l[$i]['TYPE'],array("ENTRY","FOLDER")) && $i < count($all_l)){
666         $i++;
667       }
668     }
669     if(!isset($all_l[$i])){
670       return(FALSE);
671     }
672     if(in_array($all_l[$i]['TYPE'],array("CLOSE","OPEN"))){
673       return($all_l[$i]['PARENT']);
674     }
675     return($all_l[$i]['UNIQID']);
676   }
681   /* !\brief Handle ui POSTS, like sort up/down/delete
682    */ 
683   function save_object()
684   {
685     foreach($_POST as $name => $value){
686       if(preg_match("/del_/",$name)){
687         $id = preg_replace("/^del_/","",$name);
688         $id = preg_replace("/_(x|y)$/","",$id);
689         $this->_remove_entry_id($id);
690         break;
691       }
692       if(preg_match("/app_entry_edit/",$name)){
693         $id = preg_replace("/^app_entry_edit/","",$name);
694         $id = preg_replace("/_(x|y)$/","",$id);
695         $this->_edit_entry_edit($id);
696         break;
697       }
698       if(preg_match("/up_/",$name)){
699         $id = preg_replace("/^up_/","",$name);
700         $id = preg_replace("/_(x|y)$/","",$id);
701         $this->_move_entry($id,"up");
702         break;
703       }
704       if(preg_match("/down_/",$name)){
705         $id = preg_replace("/^down_/","",$name);
706         $id = preg_replace("/_(x|y)$/","",$id);
707         $this->_move_entry($id,"down");
708         break;
709       }
710       if(preg_match("/^parameter_/",$name) && 
711         count($this->edit_entry) && $this->edit_entry['TYPE'] == "ENTRY"){
712         $name = preg_replace("/^parameter_/","",$name);
713         $this->app_parameter[$name] = $value;
714       }
715     }
716     if(isset($_POST['FAIrelease'])){
717       $this->FAIrelease = $_POST['FAIrelease'];
718       $this->_check_missing_release($this->FAIrelease);
719     }
720     if(isset($_GET['act']) && $_GET['act'] == 'depopen'){
721       $this->curbase = base64_decode($_GET['depid']);
722     }
723     if(isset($_POST['add_to_folder']) && isset($_POST['folder'])){
724       $folder = $_POST['folder'];
725       foreach($_POST as $name => $value){
726         if(preg_match("/^AddApp_[0-9]*$/",$name)){
727           $this->_add_app_id($folder,preg_replace("/^AddApp_/","",$name));   
728         }
729       }
730     }
732     /* Add seperator */
733     if(isset($_POST['add_seperator']) && isset($_POST['menu_folder'])){
734       $folder = $_POST['menu_folder'];
735       $this->_add_seperator($folder);
736     }
738     if(isset($_POST['add_menu_to_folder']) && isset($_POST['menu_folder'])){
739       $folder = $_POST['menu_folder'];
740       $name = $_POST['menu_folder_name'];
741       if(strlen($name) > 0 && preg_match("/[a-z ]/i",$name)){
742         $this->_add_sub_folder($folder,$name);
743       }
744     }
745     if(isset($_POST['app_entry_save'])){ 
746       $this->_save_entry_edit();
747     }
749     if(isset($_FILES['folder_image']) && isset($_POST['folder_image_upload'])){
750       if($_FILES['folder_image']['error'] == 0 && $_FILES['folder_image']['size'] > 0){
751         $this->edit_entry['ICON'] = file_get_contents($_FILES['folder_image']['tmp_name']);
752       }
753     }
755     if(isset($_POST['edit_reset_image'])){
756       $this->edit_entry['ICON'] = "";
757     }
759     if(isset($_POST['app_entry_cancel'])){
760       $this->edit_entry = array();
761       $this->dialog = FALSE;
762     }
763     $this->reload();
764   }
766  
767   /*! \brief Returns the UNIQID of the currently selected release 
768    */ 
769   function _get_release_key($release,$add_if_missing = FALSE)
770   {
771     $release_info = $this->Releases[$release];
773     if($release_info['name'] == "/"){
774       return($this->a_Structure['0']['UNIQID']);
775     }
777     $cur = &$this->a_Structure[0]['ENTRIES'];
778     $s_key = "";
779     $found = FALSE;
780     foreach($release_info['parts'] as $name){
781       foreach($cur as $key => $obj){
782         if($obj['TYPE'] == "RELEASE" && $obj['NAME'] == $name){
783           $s_key = $cur[$key]['UNIQID'];
784           $cur = &$cur[$key]['ENTRIES'];
785           $found = TRUE;
786           break;
787         }
788         $found = FALSE;
789       }
790     }
791     if($found){
792       return($s_key);  
793     }  
794     return(FALSE);
795   }
797  
798   /*! \brief Add a new folder folder to the specified folder id
799       @param  String $folder The folder id in where we want to add the new folder.
800       @param  String $name   The name of the new folder.
801    */ 
802   function _add_sub_folder($folder,$name)
803   {
804     $all = $this->_get_all_entries();
805     if($folder == "BASE"){
806       $folder = $this->_get_release_key($this->FAIrelease,TRUE);
807     }
808     
809     if(isset($all[$folder])){
810       $a_folder = array();
811       $a_folder['STATUS'] = "ADDED";
812       $a_folder['NAME']   = $name;
813       $a_folder['UNIQID'] = uniqid();
814       $a_folder['ENTRIES']= array();
815       $a_folder['PARENT'] = $folder;      
816       $a_folder['TYPE']   = "FOLDER";
817       $a_folder['ICON']   = "";
818       $all[$folder]['ENTRIES'][] = $a_folder;
819     }
820   }
823   /* !\brief Remove the given id from the menu structure.
824       @param  String  ID to of the entry we want to remove.
825       @return Boolean TRUE on success
826    */
827   function _remove_entry_id($id)
828   {
829     $all = $this->_get_all_entries();
830     if(isset($all[$id])){
831       $all[$id]['STATUS'] = "REMOVED";
832       $all[$id]['ENTRIES'] = array();
833       return(TRUE);
834     }
835     return(FALSE);
836   }
838   
839   /* !\brief Remove the given id from the menu structure.
840       @param  String  ID to of the entry we want to remove.
841       @return Boolean TRUE on success
842    */
843   function _add_entry($folder_id,$entry,$pos = 0)
844   {
845     $all = $this->_get_all_entries();
847     /* Do not add removed */
848     if($entry['STATUS'] == "REMOVED"){
849       return;
850     }
852     if(isset($all[$folder_id])){
854       if(!isset($entry['ENTRIES'])){
855         $entries = array();
856       }else{
857         $entries = $entry['ENTRIES'];
858       }
860       $folder  = &$all[$folder_id];
862       $entry['UNIQID'] = uniqid();     
863       $entry['PARENT'] = $folder_id;
864       $entry['ENTRIES']= array();
865       $entry['STATUS'] = "ADDED";
866       
867       $cnt = 0; 
868       $new = array();
869       $added =FALSE;
870       foreach($folder['ENTRIES'] as $key => $obj){
871         if($obj['STATUS'] == "LOADED"){
872           $obj['STATUS'] = "EDITED";
873         }
874         if($pos == $cnt){
875           $new[] = $entry;
876           $added = TRUE;
877         }
878         $cnt ++;
879         $new[] = $obj;
880       }
881       if(!$added){
882         $new[] = $entry;
883       }
884   
885       $all[$folder_id]['ENTRIES'] = $new;
886       foreach($entries as $sub){
887         $this->_add_entry($entry['UNIQID'],$sub,-1);
888       }
889       return(TRUE);
890     }
891     return(FALSE);
892   }
894  
895   /*! \brief Add the application identified by $app_id to folder $folder_id 
896       @param  String  folder_id The UNIQID of the folder where we want to add the new folder.
897       @param  Integer app_id    The ID of the application which should be added.
898    */ 
899   function _add_app_id($folder_id,$app_id)
900   {
901     $all = $this->_get_all_entries();
902     if($folder_id == "BASE"){
903       $folder_id = $this->_get_release_key($this->FAIrelease);
904     }
905     if(isset($all[$folder_id]) && isset($this->apps[$app_id])){
907       $new = array();
908       $new['TYPE']  = "ENTRY";
909       $new['NAME']  = $this->apps[$app_id]['cn'][0];
910       $new['UNIQID']= uniqid(); 
911       $new['PARENT']= $folder_id;
912       $new['PARAMETER']= array();
913       if(isset($this->apps[$app_id]['description'][0])){
914         $new['INFO']  = $this->apps[$app_id]['description'][0];
915       }else{
916         $new['INFO']  = "";
917       }
918       $new['STATUS']= "ADDED";
919       $all[$folder_id]['ENTRIES'][] = $new;
920     }
921   }
924   /*! \brief Add the application identified by $app_id to folder $folder_id 
925       @param  String  folder_id The UNIQID of the folder where we want to add the new folder.
926       @param  Integer app_id    The ID of the application which should be added.
927    */ 
928   function _add_seperator($folder_id)
929   {
930     $all = $this->_get_all_entries();
931     if($folder_id == "BASE"){
932       $folder_id = $this->_get_release_key($this->FAIrelease);
933     }
935     if(isset($all[$folder_id])){
936       $new = array();
937       $new['TYPE']  = "SEPERATOR";
938       $new['NAME']  = "SEPERATOR";
939       $new['UNIQID']= uniqid(); 
940       $new['PARENT']= $folder_id;
941       $new['PARAMETER']= array();
942       $new['STATUS']= "ADDED";
943       $all[$folder_id]['ENTRIES'][] = $new;
944     }
945   }
948   /*! \brief  Return all entries linear. 
949       @param  Boolean   $add_tags  If TRUE, OPEN/CLOSE Tags will be appended.
950       @param  &Array    Start here, Pointer to an array.
951    */ 
952   function _get_all_entries($add_tags = FALSE, $skip_release = FALSE, &$cur = NULL)
953   {
954     $ret = array();
955     if($cur == NULL){
956       $cur = &$this->a_Structure;
957     }
958     foreach($cur as $key => $entry){
960       if($skip_release && $entry['TYPE'] == "RELEASE"){
961         continue;
962       }    
963       if($entry['TYPE'] == "ENTRY"){
964         $found = FALSE;
965         foreach($this->apps as $app){
966           if($app['cn'][0] == $entry['NAME']){
967             $found = TRUE;
968             if(isset($app['description'][0])){
969               $entry['INFO'] = "[".$app['description'][0]."]";
970             }
971             break;
972           }
973         } 
974         if(!$found){
975           $entry['INFO'] = "<font color='red'>"._("Not available in release.")."</font>";
976         }
977       }
978       
980       $tmp = $entry;
981       if(!$add_tags){
982         $ret[$tmp['UNIQID']] = &$cur[$key];
983         if(isset($entry['ENTRIES']) && count($entry['ENTRIES'])){
984           $ret = array_merge($ret,$this->_get_all_entries($add_tags,$skip_release,$cur[$key]['ENTRIES']));
985         }
986       }else{
987       
988         if(isset($tmp['ENTRIES'])){
989           unset($tmp['ENTRIES']);
990         }
991         if($tmp['STATUS'] != "REMOVED"){
992           $ret[] = $tmp;
993           if(isset($entry['ENTRIES']) && count($entry['ENTRIES'])){
994             $add = false;
995             foreach($entry['ENTRIES'] as $entry){
996               if($entry['STATUS'] != "REMOVED"){
997                 $add = TRUE;
998                 break;
999               }
1000             }
1002             if($add){
1003               $ret[] = array("TYPE" => "OPEN", "PARENT" => $entry['PARENT']);
1004               $ret = array_merge($ret,$this->_get_all_entries($add_tags,$skip_release,$cur[$key]['ENTRIES']));
1005               $ret[] = array("TYPE" => "CLOSE" , "PARENT" => $entry['PARENT']);
1006             }
1007           }
1008         }
1009       }
1010     }
1011     return($ret);
1012   }
1015   function save()
1016   {
1017     $ldap = $this->config->get_ldap_link();
1018     $all = $this->_get_all_entries();
1019     $prio = 0;
1020     $Actions = array("Remove" => array(),"Edit" => array() , "Add" => array());
1022     $sep_id = 0;
1023     foreach($all as $entry){
1024       $prio ++;
1025       $cur = $entry;
1026       $dn = "";
1028       do{  
1029         if($cur['TYPE'] == "SEPERATOR"){
1030           $sep_id ++;
1031           $dn.= "cn=seperator_".$sep_id.",";
1032         }elseif($cur['TYPE'] == "ENTRY"){
1033           $dn.= "cn=".$cur['NAME'].",";
1034         }elseif($cur['TYPE'] == "FOLDER"){
1035           $dn.= "cn=".$cur['NAME'].",";
1036         }elseif($cur['TYPE'] == "RELEASE"){
1037           $dn.= "ou=".$cur['NAME'].",";
1038         }elseif($cur['TYPE'] == "BASE"){
1039         }
1040         if(!isset($all[$cur['PARENT']])){
1041           $cur = NULL;
1042         }else{
1043           $cur = $all[$cur['PARENT']];
1044         }
1045       }while(is_array($cur));
1047       $cur_dn = $dn.$this->dn;
1049       $attrs = array();
1051       switch($entry['TYPE']){
1052         case "SEPERATOR"    :
1053         { 
1054           $attrs['objectClass'] = array("gotoMenuEntry");
1055           $attrs['cn']          = "seperator_".$sep_id;
1056           $attrs['gosaApplicationPriority'] = $prio;
1057           $attrs['gosaApplicationParameter'] = "*separator*";
1058         }
1059         break;
1060         case "ENTRY"    :
1061         { 
1062           $attrs['objectClass'] = array("gotoMenuEntry");
1063           $attrs['cn']          = $entry['NAME'];
1064           $attrs['gosaApplicationPriority'] = $prio;
1065           $attrs['gosaApplicationParameter'] = array(); 
1066           
1067           
1068           foreach($entry['PARAMETER'] as $name => $value){
1069             $attrs['gosaApplicationParameter'][] = $name.":".$value; 
1070           }
1071           if($entry['STATUS'] == "ADDED" && !count($attrs['gosaApplicationParameter'])){
1072             unset($attrs['gosaApplicationParameter']);
1073           } 
1074         }
1075         break;
1076         case "FOLDER"   : 
1077         { 
1078           $attrs['objectClass'] = array("gotoSubmenuEntry");
1079           $attrs['cn']          = $entry['NAME'];
1080           $attrs['gosaApplicationPriority'] = $prio;
1081           if($entry['STATUS'] != "ADDED"){
1082             $attrs['gosaApplicationIcon'] = array();
1083           }
1084           
1085           if(!empty($entry['ICON'])){
1086             $attrs['gosaApplicationIcon']     = $entry['ICON'];
1087           }
1088         }
1089         break;
1090         case "RELEASE"  : 
1091         {
1092           $attrs['ou']            = $entry['NAME'];
1093           $attrs['objectClass']   = array();
1094           $attrs['objectClass'][] = "top";
1095           $attrs['objectClass'][] = "organizationalUnit";
1096           $attrs['objectClass'][] = "FAIbranch";
1097           if(!empty($entry['FAIstate'])){
1098             $attrs['FAIstate']      = $entry['FAIstate'];
1099           }
1100         }
1101         break;
1102       }
1103  
1104       /* Append missing ObjectClasses,  ...  Tagging */
1105       if(isset($entry['LDAP_ATTRS'])){
1106         for($i = 0 ; $i < $entry['LDAP_ATTRS']['objectClass']['count']; $i ++){
1107           $oc = $entry['LDAP_ATTRS']['objectClass'][$i];
1108           if(!in_array($oc,$attrs['objectClass'])){
1109             $attrs['objectClass'][] = $oc;
1110           }
1111         }
1112       }
1113  
1114       if($entry['STATUS'] == "LOADED"){
1115         continue;
1116       }
1117       if($entry['STATUS'] == "REMOVED"){
1118         $Actions['Remove'][$entry['DN']] = $entry['DN'];
1119       }
1120       if($entry['STATUS'] == "EDITED"){
1121         $Actions['Edit'][$cur_dn] = $attrs;
1122       }
1123       if($entry['STATUS'] == "ADDED"){
1124         $Actions['Add'][$cur_dn] = $attrs;
1125       }
1126     }
1128     $ldap = $this->config->get_ldap_link();
1129     $ldap->cd($this->config->current['BASE']);
1130     foreach($Actions['Remove'] as $dn){
1131       $ldap->cd($dn);
1132       $ldap->cat($dn);
1133       if($ldap->count()){
1134         $ldap->rmdir_recursive($dn);
1135         if (!$ldap->success()){
1136           msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $dn, LDAP_DEL, get_class()));
1137         }
1138       }
1139     }
1140     foreach($Actions['Add'] as $dn => $data){
1141       $ldap->cd($dn);
1142       $ldap->cat($dn);
1143       if(!$ldap->count()){
1144         $ldap->add($data);
1145         if (!$ldap->success()){
1146           msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $dn, LDAP_DEL, get_class()));
1147         }
1148       }
1149     }
1150     foreach($Actions['Edit'] as $dn => $data){
1151       $ldap->cd($dn);
1152       $ldap->cat($dn);
1153       if($ldap->count()){
1154         $ldap->modify($data);
1155         if (!$ldap->success()){
1156           msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $dn, LDAP_DEL, get_class()));
1157         }
1158       }
1159     }
1161     $this->_load_menu_structure();
1162   }
1165   /* Return plugin informations for acl handling  */ 
1166   static function plInfo()
1167   {
1168     return (array(
1169           "plShortName"   => _("Applications"),
1170           "plDescription" => _("Group applications"),
1171           "plSelfModify"  => FALSE,
1172           "plDepends"     => array(),
1173           "plPriority"    => 0,
1174           "plSection"     => array("admin"),
1175           "plCategory"    => array("groups"),
1176           "plProvidedAcls"=> array(
1177             "gosaMemberApplication"     => _("Application"),
1178             "FAIrelease"                => _("Release"),
1179             "gosaApplicationParameter"  => _("Application parameter"))
1180           ));
1181   }
1184   function PrepareForCopyPaste($source)
1185   {
1186   }
1189   function multiple_save_object()
1190   {
1191     if(isset($_POST['group_apps_multi'])){
1192       $this->save_object(); 
1193       plugin::multiple_save_object();    
1194   
1195       /* Get posts */
1196       foreach(array("apps") as $attr){
1197         if(isset($_POST['use_'.$attr])) {
1198           $this->multi_boxes[] = $attr;
1199         }
1200       }
1201     }
1202   }
1203   
1205   function get_multi_edit_values()
1206   {
1207     $ret = plugin::get_multi_edit_values();
1209     if(in_array("apps",$this->multi_boxes)){
1210       $ret['gosaApplicationParameter'] = $this->gosaApplicationParameter;
1211       $ret['Categories']               = $this->Categories;
1212       $ret['gosaMemberApplication']    = $this->gosaMemberApplication;
1213       $ret['FAIrelease']               = $this->FAIrelease;
1214       $ret['appoption']                = $this->appoption;
1215     }
1216     return($ret);
1217   }
1219 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1220 ?>