Code

Made new group application plugin release save.
[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->FAIrelease = 0;
40     $this->Releases   = $this->getReleases();
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['PARAMETER'] = array();
190             if(isset($attrs['gosaApplicationParameter'])){
191               for($p = 0 ; $p < $attrs['gosaApplicationParameter']['count'] ; $p ++){
192                 $tmp = split(":",$attrs['gosaApplicationParameter'][$p]);
193                 $data['PARAMETER'][$tmp[0]] = $tmp[1];
194               }
195             }
196           }elseif(in_array("FAIbranch",$attrs['objectClass'])){
198             $type = "RELEASE";
199             if(isset($attrs['FAIstate'][0])){
200               $data['FAIstate'] = $attrs['FAIstate'][0];
201             }else{
202               $data['FAIstate'] = "";
203             }
204           }
206           $data['DN']       = $attrs['dn'];
207           $data['NAME']     = $name;
208           $data['TYPE']     = $type;
209           $data['PRIORITY'] = $priority;
210           $data['ENTRIES']  = array();
211           $data['UNIQID']   = uniqid();
212           $data['PARENT']   = $parent_id;
213           $data['STATUS']   = "LOADED";
214           $cur[$priority]   = $data;
215           ksort($cur);
216         }
217       }
218     }
219   } 
222   function execute()
223   {
224     /* Call parent execute */
225     plugin::execute();
227     if(isset($_GET['send'])){
228       $id = $_GET['send'];
229       $all = $this->_get_all_entries();
230       if(isset($all[$id])){
231         send_binary_content($all[$id]['ICON'],$id.".jpg","image/jpeg");
232         exit;
233       }
234     }
236     if(isset($_GET['r']))
237     $this->__construct($this->config,$this->dn);
239     if(count($this->edit_entry)){
240       if($this->edit_entry['TYPE'] == "ENTRY"){
241         $smarty = get_smarty();
242         $smarty->assign("type", "ENTRY");
243         $smarty->assign("entry",$this->edit_entry);
244         $smarty->assign("paras",$this->app_parameter);
245         $display= $smarty->fetch (get_template_path('edit_entry.tpl', TRUE, dirname(__FILE__)));
246         return($display);
247       }
248       if($this->edit_entry['TYPE'] == "FOLDER"){
249         $smarty = get_smarty();
251         session::set("binarytype" , "image/jpeg");
252         session::set("binary" , $this->edit_entry['ICON']);
253   
254         $smarty->assign("rand", microtime(TRUE));
255         $smarty->assign("image_set" , strlen($this->edit_entry['ICON']) > 0); 
256         $smarty->assign("type", "FOLDER");
257         $smarty->assign("entry",$this->edit_entry);
258         $display= $smarty->fetch (get_template_path('edit_entry.tpl', TRUE, dirname(__FILE__)));
259         return($display);
260       }
261     }
263     $smarty = get_smarty();
264     $smarty->assign("plug_id" , $_GET['plug']);
266     /* Create application list */
267     $div = new divSelectBox("appgroup");
268     $div->SetHeight(300);
269     $departments = array();
270     $res = get_list("(objectClass=gosaDepartment)", "application", $this->curbase,array("description","cn","ou"),GL_SIZELIMIT);
271     foreach($res as $value){
272       $fdn = $value['dn'];
273       $fdn = preg_replace("/".normalizePreg($this->curbase)."/","",$fdn);
274       $fdn= @LDAP::fix($fdn);
275       if($value["description"][0]!=".."){
276         $departments[$value['dn']]= convert_department_dn($fdn)." - [".$value["description"][0]."]";
277       }else{
278         $departments[$value['dn']]=convert_department_dn($fdn)." ["._("Back")."]";
279       }
280     }
282     $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=depopen&amp;depid=%s'>%s</a>";
284     /* Create base back entry */
285     $base_back = preg_replace("/^[^,]+,/","",$this->curbase);
286     if((strlen($base_back)>= strlen($this->config->current['BASE']))&&($this->curbase!=$this->config->current['BASE'])){
287       $div->AddEntry(array(
288             array("string"=>sprintf($linkopen,base64_encode($base_back),".. ["._("back")."]"),
289               "attach"=>"style='border:0px;'")
290             ));
291     }
293     /* Append departments for current base */
294     foreach($departments as $key => $app){
295       $div->AddEntry(array(
296             array("string"=>"<img class='center' src='images/folder.png' alt='"._("department")."'>&nbsp;".sprintf($linkopen,
297                 base64_encode($key),$app),
298               "attach"=>"style='border:0px;'")
299             ));
300     }
302   
303     /* Add applications found on this base */
304     $used_apps = $this->_get_used_entry_name();
305     foreach($this->apps as $key => $app){
306       if(in_array($app['cn'][0],$used_apps)){
307         continue;
308       }
309       if(!preg_match("/".get_ou('applicationou').normalizePreg($this->curbase)."$/",$app['dn'])){
310         continue;
311       }
313       $name = $app['cn'][0];
314       if(isset($app['description'])){
315         $name .= "&nbsp;[".$app['description'][0]."]";
316       }
317       $div->AddEntry(array(
318             array("string"=>sprintf("<input class='center' type='checkbox' value='1' name='AddApp_%s'>",$key).
319               "<img class='center' src='images/select_application.png' alt='"._("application")."'>&nbsp;".$name,
320               "attach"=>"style='border:0px;'")
321             ));
322     }
324     
325     $smarty->assign("enableReleaseManagement",$this->enableReleaseManagement);
326     $smarty->assign("FAIrelease",$this->FAIrelease);
327     $smarty->assign("app_list",$div->DrawList());
328     $smarty->assign("releases",$this->Releases);
329     $smarty->assign("folders" , $this->_get_folder_names());
330     $entries = $this->_get_entries_for_release($this->FAIrelease);
331     $smarty->assign("entries",$entries);
332     $display= $smarty->fetch (get_template_path('app_list.tpl', TRUE, dirname(__FILE__)));
333     return($display);
334   }
336    
337   /*! \brief Returns all used folder names 
338       @return Array  All used folder names.
339    */ 
340   function _get_folder_names()
341   {
342     $data = $this->_get_entries_for_release($this->FAIrelease);
343     $ret = array("BASE" => ".");
344     foreach($data as $entry){
345       if($entry['TYPE'] == "FOLDER"){
346         $ret[$entry['UNIQID']] = $entry['NAME'];
347       }
348     }
349     return($ret);
350   }
353   /*! \brief return all used applications 
354       @return Array  All used applications.
355    */ 
356   function _get_used_entry_name()
357   {
358     $data = $this->_get_entries_for_release($this->FAIrelease);
359     $ret = array();
360     foreach($data as $entry){
361       if($entry['TYPE'] == "ENTRY"){
362         $ret[] = $entry['NAME'];
363       }
364     }
365     return($ret);
366   }
369   /*! \brief Returns all folder an entries for the selected release 
370       @return Array  Returns the complete menu structure for the given array.
371    */ 
372   function _get_entries_for_release($release,$cur = NULL)
373   {
374     $all = $this->_get_all_entries();
375     $key = $this->_get_release_key($release);
376     if(isset($all[$key]) && count($all[$key]['ENTRIES'])){
377       $res = $this->_get_all_entries(TRUE,TRUE,&$all[$key]['ENTRIES']);
378       return($res);
379     } 
380     return(array());
381   }
384   /*! \brief Save the currently edited entry */
385   function _save_entry_edit()
386   {
387     $all    = $this->_get_all_entries();
388     $entry  = $this->edit_entry;
389     $r_entry= &$all[$entry['UNIQID']];
391     if($entry['TYPE'] == "ENTRY"){
392       $r_entry['PARAMETER'] = $this->app_parameter;
393       $r_entry['STATUS'] = "EDITED";
394     }
395     if($entry['TYPE'] == "FOLDER"){
396       $r_entry['ICON']   = $this->edit_entry['ICON'];
397       $r_entry['STATUS'] = "EDITED";
398     }
399     $this->dialog = FALSE;
400     $this->edit_entry = array();
401   }
404   /*! \brief prepare the entry with the given ID, to be edited.
405    */
406   function _edit_entry_edit($id)
407   {
408     $all   = $this->_get_all_entries();
409     $entry = $all[$id];
411     $this->app_parameter = array();
412     if($entry['TYPE'] == "ENTRY"){
413       $found = FALSE;
414       foreach($this->apps as $id => $app){
416         if($app['cn'][0] == $entry['NAME']){
417           $found = TRUE;
418           break;
419         }
420       }
421       if($found){
422       
423         /* Create a list of editable parameter */
424         if(isset($app['gosaApplicationParameter'])){
425           for($i = 0 ; $i < $app['gosaApplicationParameter']['count'] ; $i++) {
426             $para = $app['gosaApplicationParameter'][$i];
427             $tmp  = split(":",$para);
428             $this->app_parameter[$tmp[0]] = $tmp[1];
429           }
430         }
432         /* Overwrite parameters with entry parameters */
433         foreach($entry['PARAMETER'] as $name => $value){
434           $this->app_parameter[$name] = $value;
435         }
436         
437         $this->dialog = TRUE;
438         $this->edit_entry = $entry;
439       }
440     }
442     if($entry['TYPE'] == "FOLDER"){
443       $this->dialog = TRUE;
444       $this->edit_entry = $entry;
445     }
446   }
449   function remove_from_parent()
450   {
451   }
455   function check()
456   {
457   }
460   /*! \brief Create missing releases, if there is a release selected \
461               that is currently not part of the menu structure \
462               then create this entry
463    */
464   function _check_missing_release($release)
465   {
466     $release_info = $this->Releases[$release];
468     $parent_id = $this->a_Structure[0]['UNIQID'];
469     $cur = &$this->a_Structure[0]['ENTRIES'];
470     for($i = 0 ; $i < count($release_info['parts']) ; $i ++){
471       $part = $release_info['parts'][$i];
472       $found = FALSE;
473       foreach($cur as $key => $name){
474         if($name['NAME'] == $part){
475           $parent_id = $cur[$key]['UNIQID'];
476           $cur = &$cur[$key]['ENTRIES'];
477           
478           $found =TRUE;
479           break;
480         }
481       }
482       if(!$found){
483         $release           =  array();
484         $release['UNIQID'] = uniqid();
485         $release['PARENT'] = $parent_id;
486         $release['NAME']   = $part;
487         $release['TYPE']   = "RELEASE";
488         $release['ENTRIES']= array();
489         $release['STATUS']   = "ADDED";
490         $release['FAIstate'] =  $release_info['FAIstate'];
491         $cur[] = $release;
492         $i --;
493       }
494     }
495   }
498   /* !\brief Handle ui POSTS, like sort up/down/delete
499    */ 
500   function save_object()
501   {
502     foreach($_POST as $name => $value){
503       if(preg_match("/del_/",$name)){
504         $id = preg_replace("/^del_/","",$name);
505         $id = preg_replace("/_(x|y)$/","",$id);
506         $this->_remove_entry_id($id);
507         break;
508       }
509       if(preg_match("/app_entry_edit/",$name)){
510         $id = preg_replace("/^app_entry_edit/","",$name);
511         $id = preg_replace("/_(x|y)$/","",$id);
512         $this->_edit_entry_edit($id);
513         break;
514       }
515       if(preg_match("/up_/",$name)){
516         $id = preg_replace("/^up_/","",$name);
517         $id = preg_replace("/_(x|y)$/","",$id);
518         $this->_move_entry_up($id);
519         break;
520       }
521       if(preg_match("/down_/",$name)){
522         $id = preg_replace("/^down_/","",$name);
523         $id = preg_replace("/_(x|y)$/","",$id);
524         $this->_move_entry_down($id);
525         break;
526       }
527       if(preg_match("/^parameter_/",$name) && 
528         count($this->edit_entry) && $this->edit_entry['TYPE'] == "ENTRY"){
529         $name = preg_replace("/^parameter_/","",$name);
530         $this->app_parameter[$name] = $value;
531       }
532     }
533     if(isset($_POST['FAIrelease'])){
534       $this->FAIrelease = $_POST['FAIrelease'];
535       $this->_check_missing_release($this->FAIrelease);
536     }
537     if(isset($_GET['act']) && $_GET['act'] == 'depopen'){
538       $this->curbase = base64_decode($_GET['depid']);
539     }
540     if(isset($_POST['add_to_folder']) && isset($_POST['folder'])){
541       $folder = $_POST['folder'];
542       foreach($_POST as $name => $value){
543         if(preg_match("/^AddApp_[0-9]*$/",$name)){
544           $this->_add_app_id($folder,preg_replace("/^AddApp_/","",$name));   
545         }
546       }
547     }
548     if(isset($_POST['add_menu_to_folder']) && isset($_POST['menu_folder'])){
549       $folder = $_POST['menu_folder'];
550       $name = $_POST['menu_folder_name'];
551       if(strlen($name) > 0 && preg_match("/[a-z ]/i",$name)){
552         $this->_add_sub_folder($folder,$name);
553       }
554     }
555     if(isset($_POST['app_entry_save'])){ 
556       $this->_save_entry_edit();
557     }
559     if(isset($_FILES['folder_image']) && isset($_POST['folder_image_upload'])){
560       if($_FILES['folder_image']['error'] == 0 && $_FILES['folder_image']['size'] > 0){
561         $this->edit_entry['ICON'] = file_get_contents($_FILES['folder_image']['tmp_name']);
562       }
563     }
565     if(isset($_POST['edit_reset_image'])){
566       $this->edit_entry['ICON'] = "";
567     }
569     if(isset($_POST['app_entry_cancel'])){
570       $this->edit_entry = array();
571       $this->dialog = FALSE;
572     }
573     $this->reload();
574   }
576  
577   /*! \brief Returns the UNIQID of the currently selected release 
578    */ 
579   function _get_release_key($release,$add_if_missing = FALSE)
580   {
581     $release_info = $this->Releases[$release];
583     if($release_info['name'] == "/"){
584       return($this->a_Structure['0']['UNIQID']);
585     }
587     $cur = &$this->a_Structure[0]['ENTRIES'];
588     $s_key = "";
589     $found = FALSE;
590     foreach($release_info['parts'] as $name){
591       foreach($cur as $key => $obj){
592         if($obj['TYPE'] == "RELEASE" && $obj['NAME'] == $name){
593           $s_key = $cur[$key]['UNIQID'];
594           $cur = &$cur[$key]['ENTRIES'];
595           $found = TRUE;
596           break;
597         }
598         $found = FALSE;
599       }
600     }
601     if($found){
602       return($s_key);  
603     }  
604     return(FALSE);
605   }
607  
608   /*! \brief Add a new folder folder to the specified folder id
609       @param  String $folder The folder id in where we want to add the new folder.
610       @param  String $name   The name of the new folder.
611    */ 
612   function _add_sub_folder($folder,$name)
613   {
614     $all = $this->_get_all_entries();
615     if($folder == "BASE"){
616       $folder = $this->_get_release_key($this->FAIrelease,TRUE);
617     }
618     
619     if(isset($all[$folder])){
620       $a_folder = array();
621       $a_folder['STATUS'] = "ADDED";
622       $a_folder['NAME']   = $name;
623       $a_folder['UNIQID'] = uniqid();
624       $a_folder['PARENT'] = $folder;      
625       $a_folder['TYPE']   = "FOLDER";
626       $all[$folder]['ENTRIES'][] = $a_folder;
627     }
628   }
631   /* !\brief Remove the given id from the menu structure.
632       @param  String  ID to of the entry we want to remove.
633       @return Boolean TRUE on success
634    */
635   function _remove_entry_id($id)
636   {
637     $all = $this->_get_all_entries();
638     if(isset($all[$id])){
639       $all[$id]['STATUS'] = "REMOVED";
640       return(TRUE);
641     }
642     return(FALSE);
643   }
645   
646   /* !\brief Remove the given id from the menu structure.
647       @param  String  ID to of the entry we want to remove.
648       @return Boolean TRUE on success
649    */
650   function _add_entry($folder_id,$entry,$pos = 0)
651   {
652     $all = $this->_get_all_entries();
654     if(isset($all[$folder_id])){
656       $folder  = &$all[$folder_id];
657       $entries = $entry['ENTRIES'];
659       $entry['UNIQID'] = uniqid();     
660       $entry['PARENT'] = $folder_id;
661       $entry['ENTRIES']= array();
662       $entry['STATUS'] = "ADDED";
663       
664       $cnt = 0; 
665       $new = array();
666       $added =FALSE;
667       foreach($folder['ENTRIES'] as $key => $obj){
668         if($pos == $cnt){
669           $new[] = $entry;
670           $added = TRUE;
671           $cnt ++;
672         }
673         $new[] = $obj;
674       }
675       if(!$added){
676         $new[] = $entry;
677       }
678       $all[$folder_id]['ENTRIES'] = $new;
679       foreach($entries as $sub){
680         $this->_add_entry($entry['UNIQID'],$sub);
681       }
682       return(TRUE);
683     }
684     return(FALSE);
685   }
687  
688   /*! \brief Add the application identified by $app_id to folder $folder_id 
689       @param  String  folder_id The UNIQID of the folder where we want to add the new folder.
690       @param  Integer app_id    The ID of the application which should be added.
691    */ 
692   function _add_app_id($folder_id,$app_id)
693   {
694     $all = $this->_get_all_entries();
695     if($folder_id == "BASE"){
696       $folder_id = $this->_get_release_key($this->FAIrelease);
697     }
698     if(isset($all[$folder_id]) && isset($this->apps[$app_id])){
700       $new = array();
701       $new['TYPE']  = "ENTRY";
702       $new['NAME']  = $this->apps[$app_id]['cn'][0];
703       $new['UNIQID']= uniqid(); 
704       $new['PARENT']= $folder_id;
705       $new['STATUS']= "ADDED";
706       $all[$folder_id]['ENTRIES'][] = $new;
707     }
708   }
711   /*! \brief move the object identified by the given ID one position up.
712       @param  String  The ID of the entry to be moved.
713       @return Boolean TRUE on success, else FALSE;
714    */
715   function _move_entry_up($id)
716   {
717     $all = $this->_get_all_entries(TRUE);
718     $parent = FALSE;
719     foreach($all as $entry){
720       if(isset($entry['UNIQID']) && $entry['UNIQID'] == $id){
721         if($parent != FALSE){
722           return($this->__switch_entries($id,"up",$parent));
723         }
724       }else{
725         if(in_array($entry['TYPE'],array("CLOSE","OPEN"))){
726           $parent = $entry['PARENT'];
727         }else{
728           $parent = $entry['UNIQID'];
729         }
730       }
731     }
732     return(FALSE);
733   }
736   /*! \brief move the object identified by the given ID one position down.
737       @param  String  The ID of the entry to be moved.
738       @return Boolean TRUE on success, else FALSE;
739    */
740   function _move_entry_down($id)
741   {
742     $all = $this->_get_all_entries(TRUE);
743     $found = FALSE;
744     foreach($all as $entry){
745       if(isset($entry['UNIQID']) && $entry['UNIQID'] == $id){
746         $found = TRUE;
747         continue;
748       }else{
749         if(in_array($entry['TYPE'],array("CLOSE","OPEN"))){
750           $parent = $entry['PARENT'];
751         }else{
752           $parent = $entry['UNIQID'];
753         }
754         if($found){
755           return($this->__switch_entries($id,"down",$parent));
756         }
757       }
758     }
759     return(FALSE);
760   }
763   /*! \brief  Return all entries linear. 
764       @param  Boolean   $add_tags  If TRUE, OPEN/CLOSE Tags will be appended.
765       @param  &Array    Start here, Pointer to an array.
766    */ 
767   function _get_all_entries($add_tags = FALSE, $skip_release = FALSE, $cur = NULL)
768   {
769     $ret = array();
770     if($cur == NULL){
771       $cur = &$this->a_Structure;
772     }
773     foreach($cur as $key => $entry){
775       if($skip_release && $entry['TYPE'] == "RELEASE"){
776         continue;
777       }    
778       if($entry['TYPE'] == "ENTRY"){
779         $found = FALSE;
780         foreach($this->apps as $app){
781           if($app['cn'][0] == $entry['NAME']){
782             $found = TRUE;
783             if(isset($app['description'][0])){
784               $entry['INFO'] = "[".$app['description'][0]."]";
785             }
786             break;
787           }
788         } 
789         if(!$found){
790           $entry['INFO'] = "<font color='red'>"._("Not available in release.")."</font>";
791         }
792       }
793       
795       $tmp = $entry;
796       if(!$add_tags){
797         $ret[$tmp['UNIQID']] = &$cur[$key];
798         if(isset($entry['ENTRIES']) && count($entry['ENTRIES'])){
799           $ret = array_merge($ret,$this->_get_all_entries($add_tags,$skip_release,&$cur[$key]['ENTRIES']));
800         }
801       }else{
802       
803         if(isset($tmp['ENTRIES'])){
804           unset($tmp['ENTRIES']);
805         }
806         if($tmp['STATUS'] != "REMOVED"){
807           $ret[] = $tmp;
808           if(isset($entry['ENTRIES']) && count($entry['ENTRIES'])){
809             $add = false;
810             foreach($entry['ENTRIES'] as $entry){
811               if($entry['STATUS'] != "REMOVED"){
812                 $add = TRUE;
813                 break;
814               }
815             }
817             if($add){
818               $ret[] = array("TYPE" => "OPEN", "PARENT" => $entry['UNIQID']);
819               $ret = array_merge($ret,$this->_get_all_entries($add_tags,$skip_release,&$cur[$key]['ENTRIES']));
820               $ret[] = array("TYPE" => "CLOSE" , "PARENT" => $entry['UNIQID']);
821             }
822           }
823         }
824       }
825     }
826     return($ret);
827   }
830   /* !\brief Switch one entry with another 
831      @param  String  from  The source ID.
832      @param  String  type  "up"/"down" type switched.
833      @param  String  to    The destination ID.
834      return  Boolean TRUE on success 
835    */
836   function __switch_entries($from,$type,$to)
837   {
838     $all = $this->_get_all_entries();
840     $o_to   = &$all[$to];
841     $o_from = &$all[$from];
843     print_a($o_to,$o_from);
844   
845     /***********************
846      * Source == Destination 
847      * Move into next parent. 
848      ************************/
849     if($to == $from){
850       $to = $o_to['PARENT'];
851       $o_to   = &$all[$to];
852     }
854     /***********************
855      * Target is container 
856      ************************/
857     if(in_array($o_to['TYPE'],array("FOLDER","RELEASE"))){
859       /***********************
860        * Move into parent folder  
861        *   + Target 
862        *   |-> Source
863        ************************/
864       if($to == $o_from['PARENT']){
866  
867         /* Check if source is a folder object 
868          */
869         $o_to_sub = &$all[$o_to['PARENT']]; 
870         if(in_array($o_to_sub['TYPE'],array("FOLDER","RELEASE"))){
872           $o_from['STATUS'] = "ADDED";
874           /* Adding new */
875           $tmp2 = array();  
876           $cnt = 0; 
877           foreach($o_to_sub['ENTRIES'] as $key => $entry){
878             $cnt ++;
879             if($entry['UNIQID'] == $to){
880               break;
881             }
882           }
883           if($type == "up"){
884             $cnt --;
885  
886          }
887          $this->_add_entry($o_to_sub['UNIQID'],$o_from,$cnt);
889           /* Removing old */
890           $tmp = array();
891           if(!$this->_remove_entry_id($from)){
892             return(FALSE);
893           }
894         } 
895       }else{
897         /***********************
898          * Target is NOT parent container
899          *  + Parent Folder
900          *  |-> Source 
901          *  + Destination
902          ************************/
904         /* Removing old */
905         $o_to   = $all[$to];
906         $o_from = $all[$from];
907         $this->_add_entry($to,$o_from);
908         $this->_remove_entry_id($from);
909       }
910     }else{
912       /***********************
913        * Source and Destination in some Folder.
914        *  + Parent folder
915        *  |-> Source
916        *  |-> Destination
917        ************************/
918       $o_to   = &$all[$to];
919       $o_from = &$all[$from];
920       $parent = &$all[$o_to['PARENT']];
922       if($o_to['PARENT'] == $o_from['PARENT']){
923         $tmp = $all[$to];
924         $all[$to]   = $o_from;
925         $all[$from] = $tmp;
926   
927         /* Ensure that the app priority is updated */
928         foreach($parent['ENTRIES'] as $key => $entry){
929           $parent['ENTRIES'][$key]['STATUS'] = "EDITED";
930         }
931       }
932     }
933   }
936   function save()
937   {
938     $ldap = $this->config->get_ldap_link();
939     $all = $this->_get_all_entries();
940     $prio = 0;
941     $Actions = array("Remove" => array(),"Edit" => array() , "Add" => array());
943     foreach($all as $entry){
944       $prio ++;
945       $cur = $entry;
946       $dn = "";
948       do{  
949         if($cur['TYPE'] == "ENTRY"){
950           $dn.= "cn=".$cur['NAME'].",";
951         }elseif($cur['TYPE'] == "FOLDER"){
952           $dn.= "cn=".$cur['NAME'].",";
953         }elseif($cur['TYPE'] == "RELEASE"){
954           $dn.= "ou=".$cur['NAME'].",";
955         }elseif($cur['TYPE'] == "BASE"){
956         }
957         if(!isset($all[$cur['PARENT']])){
958           $cur = NULL;
959         }else{
960           $cur = $all[$cur['PARENT']];
961         }
962       }while(is_array($cur));
964       $cur_dn = $dn.$this->dn;
966       $attrs = array();
967       switch($entry['TYPE']){
968         case "ENTRY"    :
969         { 
970           $attrs['objectClass'] = "gotoMenuEntry";
971           $attrs['cn']          = $entry['NAME'];
972           $attrs['gosaApplicationPriority'] = $prio;
973           $attrs['gosaApplicationParameter'] = array(); 
974      
975           foreach($entry['PARAMETER'] as $name => $value){
976             $attrs['gosaApplicationParameter'][] = $name.":".$value; 
977           }
978         }
979         break;
980         case "FOLDER"   : 
981         { 
982           $attrs['objectClass'] = "gotoSubmenuEntry";
983           $attrs['cn']          = $entry['NAME'];
984           $attrs['gosaApplicationPriority'] = $prio;
985           $attrs['gosaApplicationIcon']     = $entry['ICON'];
986         }
987         break;
988         case "RELEASE"  : 
989         { 
990           $attrs['ou']            = $entry['NAME'];
991           $attrs['objectClass']   = array();
992           $attrs['objectClass'][] = "top";
993           $attrs['objectClass'][] = "organizationalUnit";
994           $attrs['objectClass'][] = "FAIbranch";
995           if(!empty($entry['FAIstate'])){
996             $attrs['FAIstate']      = $entry['FAIstate'];
997           }
998         }
999         break;
1000       }
1001   
1002       if($entry['STATUS'] == "LOADED"){
1003         continue;
1004       }
1005       if($entry['STATUS'] == "REMOVED"){
1006         $Actions['Remove'][$cur_dn] = $cur_dn;
1007       }
1008       if($entry['STATUS'] == "EDITED"){
1009         $Actions['Edit'][$cur_dn] = $attrs;
1010       }
1011       if($entry['STATUS'] == "ADDED"){
1012         $Actions['Add'][$cur_dn] = $attrs;
1013       }
1014     }
1016     $ldap = $this->config->get_ldap_link();
1017     $ldap->cd($this->config->current['BASE']);
1018     foreach($Actions['Remove'] as $dn){
1019       $ldap->cd($dn);
1020       $ldap->cat($dn);
1021       if($ldap->count()){
1022         $ldap->rmdir_recursive($dn);
1023       }
1024     }
1025     foreach($Actions['Add'] as $dn => $data){
1026       $ldap->cd($dn);
1027       $ldap->cat($dn);
1028       if(!$ldap->count()){
1029         $ldap->add($data);
1030       }
1031     }
1032     foreach($Actions['Edit'] as $dn => $data){
1033       $ldap->cd($dn);
1034       $ldap->cat($dn);
1035       if($ldap->count()){
1036         $ldap->modify($data);
1037       }
1038     }
1039   }
1042   /* Return plugin informations for acl handling  */ 
1043   static function plInfo()
1044   {
1045     return (array(
1046           "plShortName"   => _("Applications"),
1047           "plDescription" => _("Group applications"),
1048           "plSelfModify"  => FALSE,
1049           "plDepends"     => array(),
1050           "plPriority"    => 0,
1051           "plSection"     => array("admin"),
1052           "plCategory"    => array("groups"),
1053           "plProvidedAcls"=> array(
1054             "gosaMemberApplication"     => _("Application"),
1055             "FAIrelease"                => _("Release"),
1056             "gosaApplicationParameter"  => _("Application parameter"))
1057           ));
1058   }
1061   function PrepareForCopyPaste($source)
1062   {
1063   }
1066   function multiple_save_object()
1067   {
1068     if(isset($_POST['group_apps_multi'])){
1069       $this->save_object(); 
1070       plugin::multiple_save_object();    
1071   
1072       /* Get posts */
1073       foreach(array("apps") as $attr){
1074         if(isset($_POST['use_'.$attr])) {
1075           $this->multi_boxes[] = $attr;
1076         }
1077       }
1078     }
1079   }
1080   
1082   function get_multi_edit_values()
1083   {
1084     $ret = plugin::get_multi_edit_values();
1086     if(in_array("apps",$this->multi_boxes)){
1087       $ret['gosaApplicationParameter'] = $this->gosaApplicationParameter;
1088       $ret['Categories']               = $this->Categories;
1089       $ret['gosaMemberApplication']    = $this->gosaMemberApplication;
1090       $ret['FAIrelease']               = $this->FAIrelease;
1091       $ret['appoption']                = $this->appoption;
1092     }
1093     return($ret);
1094   }
1096 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1097 ?>