Code

Fixed default release
[gosa.git] / gosa-plugins / goto / admin / groups / apps / class_groupApplication2.inc
1 <?php
2 class appgroup2 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   }
499   /* !\brief Handle ui POSTS, like sort up/down/delete
500    */ 
501   function save_object()
502   {
503     foreach($_POST as $name => $value){
504       if(preg_match("/del_/",$name)){
505         $id = preg_replace("/^del_/","",$name);
506         $id = preg_replace("/_(x|y)$/","",$id);
507         $this->_remove_entry_id($id);
508         break;
509       }
510       if(preg_match("/app_entry_edit/",$name)){
511         $id = preg_replace("/^app_entry_edit/","",$name);
512         $id = preg_replace("/_(x|y)$/","",$id);
513         $this->_edit_entry_edit($id);
514         break;
515       }
516       if(preg_match("/up_/",$name)){
517         $id = preg_replace("/^up_/","",$name);
518         $id = preg_replace("/_(x|y)$/","",$id);
519         $this->_move_entry_up($id);
520         break;
521       }
522       if(preg_match("/down_/",$name)){
523         $id = preg_replace("/^down_/","",$name);
524         $id = preg_replace("/_(x|y)$/","",$id);
525         $this->_move_entry_down($id);
526         break;
527       }
528       if(preg_match("/^parameter_/",$name) && 
529         count($this->edit_entry) && $this->edit_entry['TYPE'] == "ENTRY"){
530         $name = preg_replace("/^parameter_/","",$name);
531         $this->app_parameter[$name] = $value;
532       }
533     }
534     if(isset($_POST['FAIrelease'])){
535       $this->FAIrelease = $_POST['FAIrelease'];
536       $this->_check_missing_release($this->FAIrelease);
537     }
538     if(isset($_GET['act']) && $_GET['act'] == 'depopen'){
539       $this->curbase = base64_decode($_GET['depid']);
540     }
541     if(isset($_POST['add_to_folder']) && isset($_POST['folder'])){
542       $folder = $_POST['folder'];
543       foreach($_POST as $name => $value){
544         if(preg_match("/^AddApp_[0-9]*$/",$name)){
545           $this->_add_app_id($folder,preg_replace("/^AddApp_/","",$name));   
546         }
547       }
548     }
549     if(isset($_POST['add_menu_to_folder']) && isset($_POST['menu_folder'])){
550       $folder = $_POST['menu_folder'];
551       $name = $_POST['menu_folder_name'];
552       if(strlen($name) > 0 && preg_match("/[a-z ]/i",$name)){
553         $this->_add_sub_folder($folder,$name);
554       }
555     }
556     if(isset($_POST['app_entry_save'])){ 
557       $this->_save_entry_edit();
558     }
560     if(isset($_FILES['folder_image']) && isset($_POST['folder_image_upload'])){
561       if($_FILES['folder_image']['error'] == 0 && $_FILES['folder_image']['size'] > 0){
562         $this->edit_entry['ICON'] = file_get_contents($_FILES['folder_image']['tmp_name']);
563       }
564     }
566     if(isset($_POST['edit_reset_image'])){
567       $this->edit_entry['ICON'] = "";
568     }
570     if(isset($_POST['app_entry_cancel'])){
571       $this->edit_entry = array();
572       $this->dialog = FALSE;
573     }
574     $this->reload();
575   }
577  
578   /*! \brief Returns the UNIQID of the currently selected release 
579    */ 
580   function _get_release_key($release,$add_if_missing = FALSE)
581   {
582     $release_info = $this->Releases[$release];
584     if($release_info['name'] == "/"){
585       return($this->a_Structure['0']['UNIQID']);
586     }
588     $cur = &$this->a_Structure[0]['ENTRIES'];
589     $s_key = "";
590     $found = FALSE;
591     foreach($release_info['parts'] as $name){
592       foreach($cur as $key => $obj){
593         if($obj['TYPE'] == "RELEASE" && $obj['NAME'] == $name){
594           $s_key = $cur[$key]['UNIQID'];
595           $cur = &$cur[$key]['ENTRIES'];
596           $found = TRUE;
597           break;
598         }
599         $found = FALSE;
600       }
601     }
602     if($found){
603       return($s_key);  
604     }  
605     return(FALSE);
606   }
608  
609   /*! \brief Add a new folder folder to the specified folder id
610       @param  String $folder The folder id in where we want to add the new folder.
611       @param  String $name   The name of the new folder.
612    */ 
613   function _add_sub_folder($folder,$name)
614   {
615     $all = $this->_get_all_entries();
616     if($folder == "BASE"){
617       $folder = $this->_get_release_key($this->FAIrelease,TRUE);
618     }
619     
620     if(isset($all[$folder])){
621       $a_folder = array();
622       $a_folder['STATUS'] = "ADDED";
623       $a_folder['NAME']   = $name;
624       $a_folder['UNIQID'] = uniqid();
625       $a_folder['PARENT'] = $folder;      
626       $a_folder['TYPE']   = "FOLDER";
627       $a_folder['ICON']   = "";
628       $all[$folder]['ENTRIES'][] = $a_folder;
629     }
630   }
633   /* !\brief Remove the given id from the menu structure.
634       @param  String  ID to of the entry we want to remove.
635       @return Boolean TRUE on success
636    */
637   function _remove_entry_id($id)
638   {
639     $all = $this->_get_all_entries();
640     if(isset($all[$id])){
641       $all[$id]['STATUS'] = "REMOVED";
642       return(TRUE);
643     }
644     return(FALSE);
645   }
647   
648   /* !\brief Remove the given id from the menu structure.
649       @param  String  ID to of the entry we want to remove.
650       @return Boolean TRUE on success
651    */
652   function _add_entry($folder_id,$entry,$pos = 0)
653   {
654     $all = $this->_get_all_entries();
656     if(isset($all[$folder_id])){
658       $folder  = &$all[$folder_id];
659       $entries = $entry['ENTRIES'];
661       $entry['UNIQID'] = uniqid();     
662       $entry['PARENT'] = $folder_id;
663       $entry['ENTRIES']= array();
664       $entry['STATUS'] = "ADDED";
665       
666       $cnt = 0; 
667       $new = array();
668       $added =FALSE;
669       foreach($folder['ENTRIES'] as $key => $obj){
670         if($pos == $cnt){
671           $new[] = $entry;
672           $added = TRUE;
673           $cnt ++;
674         }
675         $new[] = $obj;
676       }
677       if(!$added){
678         $new[] = $entry;
679       }
680       $all[$folder_id]['ENTRIES'] = $new;
681       foreach($entries as $sub){
682         $this->_add_entry($entry['UNIQID'],$sub);
683       }
684       return(TRUE);
685     }
686     return(FALSE);
687   }
689  
690   /*! \brief Add the application identified by $app_id to folder $folder_id 
691       @param  String  folder_id The UNIQID of the folder where we want to add the new folder.
692       @param  Integer app_id    The ID of the application which should be added.
693    */ 
694   function _add_app_id($folder_id,$app_id)
695   {
696     $all = $this->_get_all_entries();
697     if($folder_id == "BASE"){
698       $folder_id = $this->_get_release_key($this->FAIrelease);
699     }
700     if(isset($all[$folder_id]) && isset($this->apps[$app_id])){
702       $new = array();
703       $new['TYPE']  = "ENTRY";
704       $new['NAME']  = $this->apps[$app_id]['cn'][0];
705       $new['UNIQID']= uniqid(); 
706       $new['PARENT']= $folder_id;
707       $new['PARAMETER']= array();
708       if(isset($this->apps[$app_id]['description'][0])){
709         $new['INFO']  = $this->apps[$app_id]['description'][0];
710       }else{
711         $new['INFO']  = "";
712       }
713       $new['STATUS']= "ADDED";
714       $all[$folder_id]['ENTRIES'][] = $new;
715     }
716   }
719   /*! \brief move the object identified by the given ID one position up.
720       @param  String  The ID of the entry to be moved.
721       @return Boolean TRUE on success, else FALSE;
722    */
723   function _move_entry_up($id)
724   {
725     $all = $this->_get_all_entries(TRUE);
726     $parent = FALSE;
727     foreach($all as $entry){
728       if(isset($entry['UNIQID']) && $entry['UNIQID'] == $id){
729         if($parent != FALSE){
730           return($this->__switch_entries($id,"up",$parent));
731         }
732       }else{
733         if(in_array($entry['TYPE'],array("CLOSE","OPEN"))){
734           $parent = $entry['PARENT'];
735         }else{
736           $parent = $entry['UNIQID'];
737         }
738       }
739     }
740     return(FALSE);
741   }
744   /*! \brief move the object identified by the given ID one position down.
745       @param  String  The ID of the entry to be moved.
746       @return Boolean TRUE on success, else FALSE;
747    */
748   function _move_entry_down($id)
749   {
750     $all = $this->_get_all_entries(TRUE);
751     $found = FALSE;
752     foreach($all as $entry){
753       if(isset($entry['UNIQID']) && $entry['UNIQID'] == $id){
754         $found = TRUE;
755         continue;
756       }else{
757         if(in_array($entry['TYPE'],array("CLOSE","OPEN"))){
758           $parent = $entry['PARENT'];
759         }else{
760           $parent = $entry['UNIQID'];
761         }
762         if($found){
763           return($this->__switch_entries($id,"down",$parent));
764         }
765       }
766     }
767     return(FALSE);
768   }
771   /*! \brief  Return all entries linear. 
772       @param  Boolean   $add_tags  If TRUE, OPEN/CLOSE Tags will be appended.
773       @param  &Array    Start here, Pointer to an array.
774    */ 
775   function _get_all_entries($add_tags = FALSE, $skip_release = FALSE, $cur = NULL)
776   {
777     $ret = array();
778     if($cur == NULL){
779       $cur = &$this->a_Structure;
780     }
781     foreach($cur as $key => $entry){
783       if($skip_release && $entry['TYPE'] == "RELEASE"){
784         continue;
785       }    
786       if($entry['TYPE'] == "ENTRY"){
787         $found = FALSE;
788         foreach($this->apps as $app){
789           if($app['cn'][0] == $entry['NAME']){
790             $found = TRUE;
791             if(isset($app['description'][0])){
792               $entry['INFO'] = "[".$app['description'][0]."]";
793             }
794             break;
795           }
796         } 
797         if(!$found){
798           $entry['INFO'] = "<font color='red'>"._("Not available in release.")."</font>";
799         }
800       }
801       
803       $tmp = $entry;
804       if(!$add_tags){
805         $ret[$tmp['UNIQID']] = &$cur[$key];
806         if(isset($entry['ENTRIES']) && count($entry['ENTRIES'])){
807           $ret = array_merge($ret,$this->_get_all_entries($add_tags,$skip_release,&$cur[$key]['ENTRIES']));
808         }
809       }else{
810       
811         if(isset($tmp['ENTRIES'])){
812           unset($tmp['ENTRIES']);
813         }
814         if($tmp['STATUS'] != "REMOVED"){
815           $ret[] = $tmp;
816           if(isset($entry['ENTRIES']) && count($entry['ENTRIES'])){
817             $add = false;
818             foreach($entry['ENTRIES'] as $entry){
819               if($entry['STATUS'] != "REMOVED"){
820                 $add = TRUE;
821                 break;
822               }
823             }
825             if($add){
826               $ret[] = array("TYPE" => "OPEN", "PARENT" => $entry['UNIQID']);
827               $ret = array_merge($ret,$this->_get_all_entries($add_tags,$skip_release,&$cur[$key]['ENTRIES']));
828               $ret[] = array("TYPE" => "CLOSE" , "PARENT" => $entry['UNIQID']);
829             }
830           }
831         }
832       }
833     }
834     return($ret);
835   }
838   /* !\brief Switch one entry with another 
839      @param  String  from  The source ID.
840      @param  String  type  "up"/"down" type switched.
841      @param  String  to    The destination ID.
842      return  Boolean TRUE on success 
843    */
844   function __switch_entries($from,$type,$to)
845   {
846     $all = $this->_get_all_entries();
848     $o_to   = &$all[$to];
849     $o_from = &$all[$from];
851     print_a($o_to,$o_from);
852   
853     /***********************
854      * Source == Destination 
855      * Move into next parent. 
856      ************************/
857     if($to == $from){
858       $to = $o_to['PARENT'];
859       $o_to   = &$all[$to];
860     }
862     /***********************
863      * Target is container 
864      ************************/
865     if(in_array($o_to['TYPE'],array("FOLDER","RELEASE"))){
867       /***********************
868        * Move into parent folder  
869        *   + Target 
870        *   |-> Source
871        ************************/
872       if($to == $o_from['PARENT']){
874  
875         /* Check if source is a folder object 
876          */
877         $o_to_sub = &$all[$o_to['PARENT']]; 
878         if(in_array($o_to_sub['TYPE'],array("FOLDER","RELEASE"))){
880           $o_from['STATUS'] = "ADDED";
882           /* Adding new */
883           $tmp2 = array();  
884           $cnt = 0; 
885           foreach($o_to_sub['ENTRIES'] as $key => $entry){
886             $cnt ++;
887             if($entry['UNIQID'] == $to){
888               break;
889             }
890           }
891           if($type == "up"){
892             $cnt --;
893  
894          }
895          $this->_add_entry($o_to_sub['UNIQID'],$o_from,$cnt);
897           /* Removing old */
898           $tmp = array();
899           if(!$this->_remove_entry_id($from)){
900             return(FALSE);
901           }
902         } 
903       }else{
905         /***********************
906          * Target is NOT parent container
907          *  + Parent Folder
908          *  |-> Source 
909          *  + Destination
910          ************************/
912         /* Removing old */
913         $o_to   = $all[$to];
914         $o_from = $all[$from];
915         $this->_add_entry($to,$o_from);
916         $this->_remove_entry_id($from);
917       }
918     }else{
920       /***********************
921        * Source and Destination in some Folder.
922        *  + Parent folder
923        *  |-> Source
924        *  |-> Destination
925        ************************/
926       $o_to   = &$all[$to];
927       $o_from = &$all[$from];
928       $parent = &$all[$o_to['PARENT']];
930       if($o_to['PARENT'] == $o_from['PARENT']){
931         $tmp = $all[$to];
932         $all[$to]   = $o_from;
933         $all[$from] = $tmp;
934   
935         /* Ensure that the app priority is updated */
936         foreach($parent['ENTRIES'] as $key => $entry){
937           $parent['ENTRIES'][$key]['STATUS'] = "EDITED";
938         }
939       }
940     }
941   }
944   function save()
945   {
946     $ldap = $this->config->get_ldap_link();
947     $all = $this->_get_all_entries();
948     $prio = 0;
949     $Actions = array("Remove" => array(),"Edit" => array() , "Add" => array());
951     foreach($all as $entry){
952       $prio ++;
953       $cur = $entry;
954       $dn = "";
956       do{  
957         if($cur['TYPE'] == "ENTRY"){
958           $dn.= "cn=".$cur['NAME'].",";
959         }elseif($cur['TYPE'] == "FOLDER"){
960           $dn.= "cn=".$cur['NAME'].",";
961         }elseif($cur['TYPE'] == "RELEASE"){
962           $dn.= "ou=".$cur['NAME'].",";
963         }elseif($cur['TYPE'] == "BASE"){
964         }
965         if(!isset($all[$cur['PARENT']])){
966           $cur = NULL;
967         }else{
968           $cur = $all[$cur['PARENT']];
969         }
970       }while(is_array($cur));
972       $cur_dn = $dn.$this->dn;
974       $attrs = array();
975       switch($entry['TYPE']){
976         case "ENTRY"    :
977         { 
978           $attrs['objectClass'] = "gotoMenuEntry";
979           $attrs['cn']          = $entry['NAME'];
980           $attrs['gosaApplicationPriority'] = $prio;
981           $attrs['gosaApplicationParameter'] = array(); 
982           
983           
984           foreach($entry['PARAMETER'] as $name => $value){
985             $attrs['gosaApplicationParameter'][] = $name.":".$value; 
986           }
987           if($entry['STATUS'] == "ADDED" && !count($attrs['gosaApplicationParameter'])){
988             unset($attrs['gosaApplicationParameter']);
989           } 
990         }
991         break;
992         case "FOLDER"   : 
993         { 
994           $attrs['objectClass'] = "gotoSubmenuEntry";
995           $attrs['cn']          = $entry['NAME'];
996           $attrs['gosaApplicationPriority'] = $prio;
997           if($entry['STATUS'] != "ADDED"){
998             $attrs['gosaApplicationIcon'] = array();
999           }
1000           
1001           if(!empty($entry['ICON'])){
1002             $attrs['gosaApplicationIcon']     = $entry['ICON'];
1003           }
1004         }
1005         break;
1006         case "RELEASE"  : 
1007         { 
1008           $attrs['ou']            = $entry['NAME'];
1009           $attrs['objectClass']   = array();
1010           $attrs['objectClass'][] = "top";
1011           $attrs['objectClass'][] = "organizationalUnit";
1012           $attrs['objectClass'][] = "FAIbranch";
1013           if(!empty($entry['FAIstate'])){
1014             $attrs['FAIstate']      = $entry['FAIstate'];
1015           }
1016         }
1017         break;
1018       }
1019   
1020       if($entry['STATUS'] == "LOADED"){
1021         continue;
1022       }
1023       if($entry['STATUS'] == "REMOVED"){
1024         $Actions['Remove'][$cur_dn] = $cur_dn;
1025       }
1026       if($entry['STATUS'] == "EDITED"){
1027         $Actions['Edit'][$cur_dn] = $attrs;
1028       }
1029       if($entry['STATUS'] == "ADDED"){
1030         $Actions['Add'][$cur_dn] = $attrs;
1031       }
1032     }
1034     $ldap = $this->config->get_ldap_link();
1035     $ldap->cd($this->config->current['BASE']);
1036     foreach($Actions['Remove'] as $dn){
1037       $ldap->cd($dn);
1038       $ldap->cat($dn);
1039       if($ldap->count()){
1040         $ldap->rmdir_recursive($dn);
1041         show_ldap_error($ldap->get_error(), _("Could not save group application settings."));
1042       }
1043     }
1044     foreach($Actions['Add'] as $dn => $data){
1045       $ldap->cd($dn);
1046       $ldap->cat($dn);
1047       if(!$ldap->count()){
1048         $ldap->add($data);
1049         show_ldap_error($ldap->get_error(), _("Could not save group application settings."));
1050       }
1051     }
1052     foreach($Actions['Edit'] as $dn => $data){
1053       $ldap->cd($dn);
1054       $ldap->cat($dn);
1055       if($ldap->count()){
1056         $ldap->modify($data);
1057         show_ldap_error($ldap->get_error(), _("Could not save group application settings."));
1058       }
1059     }
1060     $this->_load_menu_structure();
1061   }
1064   /* Return plugin informations for acl handling  */ 
1065   static function plInfo()
1066   {
1067     return (array(
1068           "plShortName"   => _("Applications"),
1069           "plDescription" => _("Group applications"),
1070           "plSelfModify"  => FALSE,
1071           "plDepends"     => array(),
1072           "plPriority"    => 0,
1073           "plSection"     => array("admin"),
1074           "plCategory"    => array("groups"),
1075           "plProvidedAcls"=> array(
1076             "gosaMemberApplication"     => _("Application"),
1077             "FAIrelease"                => _("Release"),
1078             "gosaApplicationParameter"  => _("Application parameter"))
1079           ));
1080   }
1083   function PrepareForCopyPaste($source)
1084   {
1085   }
1088   function multiple_save_object()
1089   {
1090     if(isset($_POST['group_apps_multi'])){
1091       $this->save_object(); 
1092       plugin::multiple_save_object();    
1093   
1094       /* Get posts */
1095       foreach(array("apps") as $attr){
1096         if(isset($_POST['use_'.$attr])) {
1097           $this->multi_boxes[] = $attr;
1098         }
1099       }
1100     }
1101   }
1102   
1104   function get_multi_edit_values()
1105   {
1106     $ret = plugin::get_multi_edit_values();
1108     if(in_array("apps",$this->multi_boxes)){
1109       $ret['gosaApplicationParameter'] = $this->gosaApplicationParameter;
1110       $ret['Categories']               = $this->Categories;
1111       $ret['gosaMemberApplication']    = $this->gosaMemberApplication;
1112       $ret['FAIrelease']               = $this->FAIrelease;
1113       $ret['appoption']                = $this->appoption;
1114     }
1115     return($ret);
1116   }
1118 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1119 ?>