Code

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