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();
25 public function __construct(&$config, $dn= NULL, $parent= NULL)
26 {
27 plugin::plugin($config,$dn,$parent);
28 $this->dn = $dn;
29 $this->_load_menu_structure();
30 $this->a_Structure_on_load = $this->a_Structure;
32 /* Check if we have relase mangement enabled and prepare group application for release management */
33 $tmp = $config->search("faiManagement", "CLASS",array('menu','tabs'));
34 if(!empty($tmp)){
35 $this->enableReleaseManagement = true;
36 }
38 /* Get required release informations */
39 if($this->enableReleaseManagement){
40 $this->Releases = $this->getReleases();
41 $this->FAIrelease = 0;
42 }
43 $this->curbase = $this->config->current['BASE'];
44 $this->reload();
45 }
48 /*! \brief Reload the list of applications for the currently selected release
49 */
50 function reload()
51 {
52 $ret = array();
53 $release_info = $this->Releases[$this->FAIrelease];
55 if(!isset($this->_cache['ReleaseApps'][$release_info['suffix']])){
57 $ldap = $this->config->get_ldap_link();
58 $ldap->cd($this->config->current['BASE']);
59 $ldap->search("ou=apps",array("dn"));
60 $app_deps = array();
61 while($attrs = $ldap->fetch()){
62 $app_deps[] = $attrs['dn'];
63 }
65 foreach($app_deps as $dep){
66 $ldap->cd($dep);
67 $ldap->search("objectClass=FAIbranch",array("dn"));
68 while($attrs = $ldap->fetch()){
69 $app_deps[] = $attrs['dn'];
70 }
71 }
73 foreach($app_deps as $dep){
74 if(preg_match("/^".normalizePreg($release_info['suffix'])."/",$dep)){
75 $ret = array_merge($ret,get_list("(objectClass=gosaApplication)","application",$dep,array("*"),GL_NONE));
76 }
77 }
78 $this->_cache['ReleaseApps'][$release_info['suffix']] = $ret;
79 }
80 $this->apps = $this->_cache['ReleaseApps'][$release_info['suffix']];
81 }
86 /*! \brief generate a list of available releases
87 @return return an array with all available releases.
88 */
89 function getReleases()
90 {
91 /* Only display those releases that we are able to read */
92 $dn = $this->config->current['BASE'];
93 $filter = "(&(objectClass=organizationalUnit)(objectClass=FAIbranch))";
94 $res = get_list($filter,"application", $dn, array("ou","FAIstate"), GL_SUBSEARCH);
96 $ret =array(array("name" => "/" , "parts" => array(),"suffix" => get_ou('applicationou')));
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 ksort($ret);
128 return($ret);
129 }
132 /*! \brief Load the menu structure from ldap and create a multi dimensional array
133 */
134 function _load_menu_structure()
135 {
136 $this->a_Structure = array();
137 $ldap = $this->config->get_ldap_link();
138 $ldap->cd($this->dn);
139 $ldap->search("(|(objectClass=gotoSubmenuEntry)(objectClass=FAIbranch)(objectClass=gotoMenuEntry))",array("*"));
141 $base = array();
142 $base['UNIQID'] = uniqid();
143 $base['PARENT'] = 0;
144 $base['NAME'] = "";
145 $base['TYPE'] = "BASE";
146 $base['ENTRIES']= array();
147 $base['STATUS'] = "LOADED";
149 $this->a_Structure[0] = $base;
151 while($attrs = $ldap->fetch()){
152 $cur = &$this->a_Structure[0]['ENTRIES'];
153 $parent_id = $base['UNIQID'];
154 $sub_dn = preg_replace("/,".normalizePreg($this->dn)."$/","",$attrs['dn']);
155 $sub_dn_array = split("\,",$sub_dn);
158 for($i = (count($sub_dn_array)-1) ; $i >= 0 ; $i--){
159 $name = preg_replace("/^[^=]*+=/","",$sub_dn_array[$i]);
160 if($i > 0){
161 foreach($cur as $key => $entry){
162 if($entry['NAME'] == $name){
163 $cur = &$cur[$key]['ENTRIES'];
164 $parent_id = $entry['UNIQID'];
165 }
166 }
167 }else{
169 $priority = 1;
170 if(isset($attrs['gosaApplicationPriority'])){
171 $priority= $attrs['gosaApplicationPriority'][0];
172 }
173 while(isset($cur[$priority])){
174 $priority ++;
175 }
177 $data = array();
178 if(in_array("gotoSubmenuEntry",$attrs['objectClass'])){
179 $type = "FOLDER";
181 $data['ICON'] = "";
182 if(isset($attrs['gosaApplicationIcon'])){
183 $data['ICON'] = $ldap->get_attribute($attrs['dn'],"gosaApplicationIcon");
184 }
186 }elseif(in_array("gotoMenuEntry",$attrs['objectClass'])){
187 $type = "ENTRY";
188 $data['PARAMETER'] = array();
189 if(isset($attrs['gosaApplicationParameter'])){
190 for($p = 0 ; $p < $attrs['gosaApplicationParameter']['count'] ; $p ++){
191 $tmp = split(":",$attrs['gosaApplicationParameter'][$p]);
192 $data['PARAMETER'][$tmp[0]] = $tmp[1];
193 }
194 }
195 }elseif(in_array("FAIbranch",$attrs['objectClass'])){
196 $type = "RELEASE";
197 if(isset($attrs['FAIstate'][0])){
198 $data['FAIstate'] = $attrs['FAIstate'][0];
199 }else{
200 $data['FAIstate'] = "";
201 }
202 }
204 $data['DN'] = $attrs['dn'];
205 $data['NAME'] = $name;
206 $data['TYPE'] = $type;
207 $data['PRIORITY'] = $priority;
208 $data['ENTRIES'] = array();
209 $data['UNIQID'] = uniqid();
210 $data['PARENT'] = $parent_id;
211 $data['STATUS'] = "LOADED";
212 $cur[$priority] = $data;
213 ksort($cur);
214 }
215 }
216 }
217 }
220 function execute()
221 {
222 /* Call parent execute */
223 plugin::execute();
225 if(isset($_GET['send'])){
226 $id = $_GET['send'];
227 $all = $this->_get_all_entries();
228 if(isset($all[$id])){
229 send_binary_content($all[$id]['ICON'],$id.".jpg","image/jpeg");
230 exit;
231 }
232 }
234 if(isset($_GET['r']))
235 $this->__construct($this->config,$this->dn);
237 if(count($this->edit_entry)){
238 if($this->edit_entry['TYPE'] == "ENTRY"){
239 $smarty = get_smarty();
240 $smarty->assign("type", "ENTRY");
241 $smarty->assign("entry",$this->edit_entry);
242 $smarty->assign("paras",$this->app_parameter);
243 $display= $smarty->fetch (get_template_path('edit_entry.tpl', TRUE, dirname(__FILE__)));
244 return($display);
245 }
246 if($this->edit_entry['TYPE'] == "FOLDER"){
247 $smarty = get_smarty();
249 session::set("binarytype" , "image/jpeg");
250 session::set("binary" , $this->edit_entry['ICON']);
252 $smarty->assign("rand", microtime(TRUE));
253 $smarty->assign("image_set" , strlen($this->edit_entry['ICON']) > 0);
254 $smarty->assign("type", "FOLDER");
255 $smarty->assign("entry",$this->edit_entry);
256 $display= $smarty->fetch (get_template_path('edit_entry.tpl', TRUE, dirname(__FILE__)));
257 return($display);
258 }
259 }
261 $smarty = get_smarty();
262 $smarty->assign("plug_id" , $_GET['plug']);
264 /* Create application list */
265 $div = new divSelectBox("appgroup");
266 $div->SetHeight(300);
267 $departments = array();
268 $res = get_list("(objectClass=gosaDepartment)", "application", $this->curbase,array("description","cn","ou"),GL_SIZELIMIT);
269 foreach($res as $value){
270 $fdn = $value['dn'];
271 $fdn = preg_replace("/".normalizePreg($this->curbase)."/","",$fdn);
272 $fdn= @LDAP::fix($fdn);
273 if($value["description"][0]!=".."){
274 $departments[$value['dn']]= convert_department_dn($fdn)." - [".$value["description"][0]."]";
275 }else{
276 $departments[$value['dn']]=convert_department_dn($fdn)." ["._("Back")."]";
277 }
278 }
280 $linkopen = "<a href='?plug=".$_GET['plug']."&act=depopen&depid=%s'>%s</a>";
282 /* Create base back entry */
283 $base_back = preg_replace("/^[^,]+,/","",$this->curbase);
284 if((strlen($base_back)>= strlen($this->config->current['BASE']))&&($this->curbase!=$this->config->current['BASE'])){
285 $div->AddEntry(array(
286 array("string"=>sprintf($linkopen,base64_encode($base_back),".. ["._("back")."]"),
287 "attach"=>"style='border:0px;'")
288 ));
289 }
291 /* Append departments for current base */
292 foreach($departments as $key => $app){
293 $div->AddEntry(array(
294 array("string"=>"<img class='center' src='images/folder.png' alt='"._("department")."'> ".sprintf($linkopen,
295 base64_encode($key),$app),
296 "attach"=>"style='border:0px;'")
297 ));
298 }
301 /* Add applications found on this base */
302 $used_apps = $this->_get_used_entry_name();
303 foreach($this->apps as $key => $app){
304 if(in_array($app['cn'][0],$used_apps)){
305 continue;
306 }
307 if(!preg_match("/".get_ou('applicationou').normalizePreg($this->curbase)."$/",$app['dn'])){
308 continue;
309 }
311 $name = $app['cn'][0];
312 if(isset($app['description'])){
313 $name .= " [".$app['description'][0]."]";
314 }
315 $div->AddEntry(array(
316 array("string"=>sprintf("<input class='center' type='checkbox' value='1' name='AddApp_%s'>",$key).
317 "<img class='center' src='images/select_application.png' alt='"._("application")."'> ".$name,
318 "attach"=>"style='border:0px;'")
319 ));
320 }
323 $smarty->assign("FAIrelease",$this->FAIrelease);
324 $smarty->assign("app_list",$div->DrawList());
325 $smarty->assign("releases",$this->Releases);
326 $smarty->assign("folders" , $this->_get_folder_names());
327 $entries = $this->_get_entries_for_release($this->FAIrelease);
328 $smarty->assign("entries",$entries);
329 $display= $smarty->fetch (get_template_path('app_list.tpl', TRUE, dirname(__FILE__)));
330 return($display);
331 }
334 /*! \brief Returns all used folder names
335 @return Array All used folder names.
336 */
337 function _get_folder_names()
338 {
339 $data = $this->_get_entries_for_release($this->FAIrelease);
340 $ret = array("BASE" => ".");
341 foreach($data as $entry){
342 if($entry['TYPE'] == "FOLDER"){
343 $ret[$entry['UNIQID']] = $entry['NAME'];
344 }
345 }
346 return($ret);
347 }
350 /*! \brief return all used applications
351 @return Array All used applications.
352 */
353 function _get_used_entry_name()
354 {
355 $data = $this->_get_entries_for_release($this->FAIrelease);
356 $ret = array();
357 foreach($data as $entry){
358 if($entry['TYPE'] == "ENTRY"){
359 $ret[] = $entry['NAME'];
360 }
361 }
362 return($ret);
363 }
366 /*! \brief Returns all folder an entries for the selected release
367 @return Array Returns the complete menu structure for the given array.
368 */
369 function _get_entries_for_release($release,$cur = NULL)
370 {
371 $all = $this->_get_all_entries();
372 $key = $this->_get_release_key($release);
373 if(isset($all[$key]) && count($all[$key]['ENTRIES'])){
374 $res = $this->_get_all_entries(TRUE,TRUE,&$all[$key]['ENTRIES']);
375 return($res);
376 }
377 return(array());
378 }
381 /*! \brief Save the currently edited entry */
382 function _save_entry_edit()
383 {
384 $all = $this->_get_all_entries();
385 $entry = $this->edit_entry;
386 $r_entry= &$all[$entry['UNIQID']];
388 if($entry['TYPE'] == "ENTRY"){
389 $r_entry['PARAMETER'] = $this->app_parameter;
390 $r_entry['STATUS'] = "EDITED";
391 }
392 if($entry['TYPE'] == "FOLDER"){
393 $r_entry['ICON'] = $this->edit_entry['ICON'];
394 $r_entry['STATUS'] = "EDITED";
395 }
396 $this->dialog = FALSE;
397 $this->edit_entry = array();
398 }
401 /*! \brief prepare the entry with the given ID, to be edited.
402 */
403 function _edit_entry_edit($id)
404 {
405 $all = $this->_get_all_entries();
406 $entry = $all[$id];
408 $this->app_parameter = array();
409 if($entry['TYPE'] == "ENTRY"){
410 $found = FALSE;
411 foreach($this->apps as $id => $app){
413 if($app['cn'][0] == $entry['NAME']){
414 $found = TRUE;
415 break;
416 }
417 }
418 if($found){
420 /* Create a list of editable parameter */
421 if(isset($app['gosaApplicationParameter'])){
422 for($i = 0 ; $i < $app['gosaApplicationParameter']['count'] ; $i++) {
423 $para = $app['gosaApplicationParameter'][$i];
424 $tmp = split(":",$para);
425 $this->app_parameter[$tmp[0]] = $tmp[1];
426 }
427 }
429 /* Overwrite parameters with entry parameters */
430 foreach($entry['PARAMETER'] as $name => $value){
431 $this->app_parameter[$name] = $value;
432 }
434 $this->dialog = TRUE;
435 $this->edit_entry = $entry;
436 }
437 }
439 if($entry['TYPE'] == "FOLDER"){
440 $this->dialog = TRUE;
441 $this->edit_entry = $entry;
442 }
443 }
446 function remove_from_parent()
447 {
448 }
452 function check()
453 {
454 }
457 /*! \brief Create missing releases, if there is a release selected \
458 that is currently not part of the menu structure \
459 then create this entry
460 */
461 function _check_missing_release($release)
462 {
463 $release_info = $this->Releases[$release];
465 $parent_id = $this->a_Structure[0]['UNIQID'];
466 $cur = &$this->a_Structure[0]['ENTRIES'];
467 for($i = 0 ; $i < count($release_info['parts']) ; $i ++){
468 $part = $release_info['parts'][$i];
469 $found = FALSE;
470 foreach($cur as $key => $name){
471 if($name['NAME'] == $part){
472 $parent_id = $cur[$key]['UNIQID'];
473 $cur = &$cur[$key]['ENTRIES'];
475 $found =TRUE;
476 break;
477 }
478 }
479 if(!$found){
480 $release = array();
481 $release['UNIQID'] = uniqid();
482 $release['PARENT'] = $parent_id;
483 $release['NAME'] = $part;
484 $release['TYPE'] = "RELEASE";
485 $release['ENTRIES']= array();
486 $release['STATUS'] = "ADDED";
487 $release['FAIstate'] = $release_info['FAIstate'];
488 $cur[] = $release;
489 $i --;
490 }
491 }
492 }
495 /* !\brief Handle ui POSTS, like sort up/down/delete
496 */
497 function save_object()
498 {
499 foreach($_POST as $name => $value){
500 if(preg_match("/del_/",$name)){
501 $id = preg_replace("/^del_/","",$name);
502 $id = preg_replace("/_(x|y)$/","",$id);
503 $this->_remove_entry_id($id);
504 break;
505 }
506 if(preg_match("/app_entry_edit/",$name)){
507 $id = preg_replace("/^app_entry_edit/","",$name);
508 $id = preg_replace("/_(x|y)$/","",$id);
509 $this->_edit_entry_edit($id);
510 break;
511 }
512 if(preg_match("/up_/",$name)){
513 $id = preg_replace("/^up_/","",$name);
514 $id = preg_replace("/_(x|y)$/","",$id);
515 $this->_move_entry_up($id);
516 break;
517 }
518 if(preg_match("/down_/",$name)){
519 $id = preg_replace("/^down_/","",$name);
520 $id = preg_replace("/_(x|y)$/","",$id);
521 $this->_move_entry_down($id);
522 break;
523 }
524 if(preg_match("/^parameter_/",$name) &&
525 count($this->edit_entry) && $this->edit_entry['TYPE'] == "ENTRY"){
526 $name = preg_replace("/^parameter_/","",$name);
527 $this->app_parameter[$name] = $value;
528 }
529 }
530 if(isset($_POST['FAIrelease'])){
531 $this->FAIrelease = $_POST['FAIrelease'];
532 $this->_check_missing_release($this->FAIrelease);
533 }
534 if(isset($_GET['act']) && $_GET['act'] == 'depopen'){
535 $this->curbase = base64_decode($_GET['depid']);
536 }
537 if(isset($_POST['add_to_folder']) && isset($_POST['folder'])){
538 $folder = $_POST['folder'];
539 foreach($_POST as $name => $value){
540 if(preg_match("/^AddApp_[0-9]*$/",$name)){
541 $this->_add_app_id($folder,preg_replace("/^AddApp_/","",$name));
542 }
543 }
544 }
545 if(isset($_POST['add_menu_to_folder']) && isset($_POST['menu_folder'])){
546 $folder = $_POST['menu_folder'];
547 $name = $_POST['menu_folder_name'];
548 if(strlen($name) > 0 && preg_match("/[a-z ]/i",$name)){
549 $this->_add_sub_folder($folder,$name);
550 }
551 }
552 if(isset($_POST['app_entry_save'])){
553 $this->_save_entry_edit();
554 }
556 if(isset($_FILES['folder_image']) && isset($_POST['folder_image_upload'])){
557 if($_FILES['folder_image']['error'] == 0 && $_FILES['folder_image']['size'] > 0){
558 $this->edit_entry['ICON'] = file_get_contents($_FILES['folder_image']['tmp_name']);
559 }
560 }
562 if(isset($_POST['edit_reset_image'])){
563 $this->edit_entry['ICON'] = "";
564 }
566 if(isset($_POST['app_entry_cancel'])){
567 $this->edit_entry = array();
568 $this->dialog = FALSE;
569 }
570 $this->reload();
571 }
574 /*! \brief Returns the UNIQID of the currently selected release
575 */
576 function _get_release_key($release,$add_if_missing = FALSE)
577 {
578 $release_info = $this->Releases[$release];
580 if($release_info['name'] == "/"){
581 return($this->a_Structure['0']['UNIQID']);
582 }
584 $cur = &$this->a_Structure[0]['ENTRIES'];
585 $s_key = "";
586 $found = FALSE;
587 foreach($release_info['parts'] as $name){
588 foreach($cur as $key => $obj){
589 if($obj['TYPE'] == "RELEASE" && $obj['NAME'] == $name){
590 $s_key = $cur[$key]['UNIQID'];
591 $cur = &$cur[$key]['ENTRIES'];
592 $found = TRUE;
593 break;
594 }
595 $found = FALSE;
596 }
597 }
598 if($found){
599 return($s_key);
600 }
601 return(FALSE);
602 }
605 /*! \brief Add a new folder folder to the specified folder id
606 @param String $folder The folder id in where we want to add the new folder.
607 @param String $name The name of the new folder.
608 */
609 function _add_sub_folder($folder,$name)
610 {
611 $all = $this->_get_all_entries();
612 if($folder == "BASE"){
613 $folder = $this->_get_release_key($this->FAIrelease,TRUE);
614 }
616 if(isset($all[$folder])){
617 $a_folder = array();
618 $a_folder['STATUS'] = "ADDED";
619 $a_folder['NAME'] = $name;
620 $a_folder['UNIQID'] = uniqid();
621 $a_folder['PARENT'] = $folder;
622 $a_folder['TYPE'] = "FOLDER";
623 $all[$folder]['ENTRIES'][] = $a_folder;
624 }
625 }
628 /* !\brief Remove the given id from the menu structure.
629 @param String ID to of the entry we want to remove.
630 @return Boolean TRUE on success
631 */
632 function _remove_entry_id($id)
633 {
634 $all = $this->_get_all_entries();
635 if(isset($all[$id])){
636 $all[$id]['STATUS'] = "REMOVED";
637 return(TRUE);
638 }
639 return(FALSE);
640 }
643 /* !\brief Remove the given id from the menu structure.
644 @param String ID to of the entry we want to remove.
645 @return Boolean TRUE on success
646 */
647 function _add_entry($folder_id,$entry,$pos = 0)
648 {
649 $all = $this->_get_all_entries();
651 if(isset($all[$folder_id])){
653 $folder = &$all[$folder_id];
654 $entries = $entry['ENTRIES'];
656 $entry['UNIQID'] = uniqid();
657 $entry['PARENT'] = $folder_id;
658 $entry['ENTRIES']= array();
659 $entry['STATUS'] = "ADDED";
661 $cnt = 0;
662 $new = array();
663 $added =FALSE;
664 foreach($folder['ENTRIES'] as $key => $obj){
665 if($pos == $cnt){
666 $new[] = $entry;
667 $added = TRUE;
668 $cnt ++;
669 }
670 $new[] = $obj;
671 }
672 if(!$added){
673 $new[] = $entry;
674 }
675 $all[$folder_id]['ENTRIES'] = $new;
676 foreach($entries as $sub){
677 $this->_add_entry($entry['UNIQID'],$sub);
678 }
679 return(TRUE);
680 }
681 return(FALSE);
682 }
685 /*! \brief Add the application identified by $app_id to folder $folder_id
686 @param String folder_id The UNIQID of the folder where we want to add the new folder.
687 @param Integer app_id The ID of the application which should be added.
688 */
689 function _add_app_id($folder_id,$app_id)
690 {
691 $all = $this->_get_all_entries();
692 if($folder_id == "BASE"){
693 $folder_id = $this->_get_release_key($this->FAIrelease);
694 }
695 if(isset($all[$folder_id]) && isset($this->apps[$app_id])){
697 $new = array();
698 $new['TYPE'] = "ENTRY";
699 $new['NAME'] = $this->apps[$app_id]['cn'][0];
700 $new['UNIQID']= uniqid();
701 $new['PARENT']= $folder_id;
702 $new['STATUS']= "ADDED";
703 $all[$folder_id]['ENTRIES'][] = $new;
704 }
705 }
708 /*! \brief move the object identified by the given ID one position up.
709 @param String The ID of the entry to be moved.
710 @return Boolean TRUE on success, else FALSE;
711 */
712 function _move_entry_up($id)
713 {
714 $all = $this->_get_all_entries(TRUE);
715 $parent = FALSE;
716 foreach($all as $entry){
717 if(isset($entry['UNIQID']) && $entry['UNIQID'] == $id){
718 if($parent != FALSE){
719 return($this->__switch_entries($id,"up",$parent));
720 }
721 }else{
722 if(in_array($entry['TYPE'],array("CLOSE","OPEN"))){
723 $parent = $entry['PARENT'];
724 }else{
725 $parent = $entry['UNIQID'];
726 }
727 }
728 }
729 return(FALSE);
730 }
733 /*! \brief move the object identified by the given ID one position down.
734 @param String The ID of the entry to be moved.
735 @return Boolean TRUE on success, else FALSE;
736 */
737 function _move_entry_down($id)
738 {
739 $all = $this->_get_all_entries(TRUE);
740 $found = FALSE;
741 foreach($all as $entry){
742 if(isset($entry['UNIQID']) && $entry['UNIQID'] == $id){
743 $found = TRUE;
744 continue;
745 }else{
746 if(in_array($entry['TYPE'],array("CLOSE","OPEN"))){
747 $parent = $entry['PARENT'];
748 }else{
749 $parent = $entry['UNIQID'];
750 }
751 if($found){
752 return($this->__switch_entries($id,"down",$parent));
753 }
754 }
755 }
756 return(FALSE);
757 }
760 /*! \brief Return all entries linear.
761 @param Boolean $add_tags If TRUE, OPEN/CLOSE Tags will be appended.
762 @param &Array Start here, Pointer to an array.
763 */
764 function _get_all_entries($add_tags = FALSE, $skip_release = FALSE, $cur = NULL)
765 {
766 $ret = array();
767 if($cur == NULL){
768 $cur = &$this->a_Structure;
769 }
770 foreach($cur as $key => $entry){
772 if($skip_release && $entry['TYPE'] == "RELEASE"){
773 continue;
774 }
775 if($entry['TYPE'] == "ENTRY"){
776 $found = FALSE;
777 foreach($this->apps as $app){
778 if($app['cn'][0] == $entry['NAME']){
779 $found = TRUE;
780 if(isset($app['description'][0])){
781 $entry['INFO'] = "[".$app['description'][0]."]";
782 }
783 break;
784 }
785 }
786 if(!$found){
787 $entry['INFO'] = "<font color='red'>"._("Not available in release.")."</font>";
788 }
789 }
792 $tmp = $entry;
793 if(!$add_tags){
794 $ret[$tmp['UNIQID']] = &$cur[$key];
795 if(isset($entry['ENTRIES']) && count($entry['ENTRIES'])){
796 $ret = array_merge($ret,$this->_get_all_entries($add_tags,$skip_release,&$cur[$key]['ENTRIES']));
797 }
798 }else{
800 if(isset($tmp['ENTRIES'])){
801 unset($tmp['ENTRIES']);
802 }
803 if($tmp['STATUS'] != "REMOVED"){
804 $ret[] = $tmp;
805 if(isset($entry['ENTRIES']) && count($entry['ENTRIES'])){
806 $add = false;
807 foreach($entry['ENTRIES'] as $entry){
808 if($entry['STATUS'] != "REMOVED"){
809 $add = TRUE;
810 break;
811 }
812 }
814 if($add){
815 $ret[] = array("TYPE" => "OPEN", "PARENT" => $entry['UNIQID']);
816 $ret = array_merge($ret,$this->_get_all_entries($add_tags,$skip_release,&$cur[$key]['ENTRIES']));
817 $ret[] = array("TYPE" => "CLOSE" , "PARENT" => $entry['UNIQID']);
818 }
819 }
820 }
821 }
822 }
823 return($ret);
824 }
827 /* !\brief Switch one entry with another
828 @param String from The source ID.
829 @param String type "up"/"down" type switched.
830 @param String to The destination ID.
831 return Boolean TRUE on success
832 */
833 function __switch_entries($from,$type,$to)
834 {
835 $all = $this->_get_all_entries();
837 $o_to = &$all[$to];
838 $o_from = &$all[$from];
840 print_a($o_to,$o_from);
842 /***********************
843 * Source == Destination
844 * Move into next parent.
845 ************************/
846 if($to == $from){
847 $to = $o_to['PARENT'];
848 $o_to = &$all[$to];
849 }
851 /***********************
852 * Target is container
853 ************************/
854 if(in_array($o_to['TYPE'],array("FOLDER","RELEASE"))){
856 /***********************
857 * Move into parent folder
858 * + Target
859 * |-> Source
860 ************************/
861 if($to == $o_from['PARENT']){
864 /* Check if source is a folder object
865 */
866 $o_to_sub = &$all[$o_to['PARENT']];
867 if(in_array($o_to_sub['TYPE'],array("FOLDER","RELEASE"))){
869 $o_from['STATUS'] = "ADDED";
871 /* Adding new */
872 $tmp2 = array();
873 $cnt = 0;
874 foreach($o_to_sub['ENTRIES'] as $key => $entry){
875 $cnt ++;
876 if($entry['UNIQID'] == $to){
877 break;
878 }
879 }
880 if($type == "up"){
881 $cnt --;
883 }
884 $this->_add_entry($o_to_sub['UNIQID'],$o_from,$cnt);
886 /* Removing old */
887 $tmp = array();
888 if(!$this->_remove_entry_id($from)){
889 return(FALSE);
890 }
891 }
892 }else{
894 /***********************
895 * Target is NOT parent container
896 * + Parent Folder
897 * |-> Source
898 * + Destination
899 ************************/
901 /* Removing old */
902 $o_to = $all[$to];
903 $o_from = $all[$from];
904 $this->_add_entry($to,$o_from);
905 $this->_remove_entry_id($from);
906 }
907 }else{
909 /***********************
910 * Source and Destination in some Folder.
911 * + Parent folder
912 * |-> Source
913 * |-> Destination
914 ************************/
915 $o_to = &$all[$to];
916 $o_from = &$all[$from];
917 $parent = &$all[$o_to['PARENT']];
919 if($o_to['PARENT'] == $o_from['PARENT']){
920 $tmp = $all[$to];
921 $all[$to] = $o_from;
922 $all[$from] = $tmp;
924 /* Ensure that the app priority is updated */
925 foreach($parent['ENTRIES'] as $key => $entry){
926 $parent['ENTRIES'][$key]['STATUS'] = "EDITED";
927 }
928 }
929 }
930 }
933 function save()
934 {
935 $ldap = $this->config->get_ldap_link();
936 $all = $this->_get_all_entries();
937 $prio = 0;
938 $Actions = array("Remove" => array(),"Edit" => array() , "Add" => array());
940 foreach($all as $entry){
941 $prio ++;
942 $cur = $entry;
943 $dn = "";
945 do{
946 if($cur['TYPE'] == "ENTRY"){
947 $dn.= "cn=".$cur['NAME'].",";
948 }elseif($cur['TYPE'] == "FOLDER"){
949 $dn.= "cn=".$cur['NAME'].",";
950 }elseif($cur['TYPE'] == "RELEASE"){
951 $dn.= "ou=".$cur['NAME'].",";
952 }elseif($cur['TYPE'] == "BASE"){
953 }
954 if(!isset($all[$cur['PARENT']])){
955 $cur = NULL;
956 }else{
957 $cur = $all[$cur['PARENT']];
958 }
959 }while(is_array($cur));
961 $cur_dn = $dn.$this->dn;
963 $attrs = array();
964 switch($entry['TYPE']){
965 case "ENTRY" :
966 {
967 $attrs['objectClass'] = "gotoMenuEntry";
968 $attrs['cn'] = $entry['NAME'];
969 $attrs['gosaApplicationPriority'] = $prio;
970 $attrs['gosaApplicationParameter'] = array();
972 foreach($entry['PARAMETER'] as $name => $value){
973 $attrs['gosaApplicationParameter'][] = $name.":".$value;
974 }
975 }
976 break;
977 case "FOLDER" :
978 {
979 $attrs['objectClass'] = "gotoSubmenuEntry";
980 $attrs['cn'] = $entry['NAME'];
981 $attrs['gosaApplicationPriority'] = $prio;
982 $attrs['gosaApplicationIcon'] = $entry['ICON'];
983 }
984 break;
985 case "RELEASE" :
986 {
987 $attrs['ou'] = $entry['NAME'];
988 $attrs['objectClass'] = array();
989 $attrs['objectClass'][] = "top";
990 $attrs['objectClass'][] = "organizationalUnit";
991 $attrs['objectClass'][] = "FAIbranch";
992 if(!empty($entry['FAIstate'])){
993 $attrs['FAIstate'] = $entry['FAIstate'];
994 }
995 }
996 break;
997 }
999 if($entry['STATUS'] == "LOADED"){
1000 continue;
1001 }
1002 if($entry['STATUS'] == "REMOVED"){
1003 $Actions['Remove'][$cur_dn] = $cur_dn;
1004 }
1005 if($entry['STATUS'] == "EDITED"){
1006 $Actions['Edit'][$cur_dn] = $attrs;
1007 }
1008 if($entry['STATUS'] == "ADDED"){
1009 $Actions['Add'][$cur_dn] = $attrs;
1010 }
1011 }
1013 $ldap = $this->config->get_ldap_link();
1014 $ldap->cd($this->config->current['BASE']);
1015 foreach($Actions['Remove'] as $dn){
1016 $ldap->cd($dn);
1017 $ldap->cat($dn);
1018 if($ldap->count()){
1019 $ldap->rmdir_recursive($dn);
1020 }
1021 }
1022 foreach($Actions['Add'] as $dn => $data){
1023 $ldap->cd($dn);
1024 $ldap->cat($dn);
1025 if(!$ldap->count()){
1026 $ldap->add($data);
1027 }
1028 }
1029 foreach($Actions['Edit'] as $dn => $data){
1030 $ldap->cd($dn);
1031 $ldap->cat($dn);
1032 if($ldap->count()){
1033 $ldap->modify($data);
1034 }
1035 }
1036 }
1039 /* Return plugin informations for acl handling */
1040 static function plInfo()
1041 {
1042 return (array(
1043 "plShortName" => _("Applications"),
1044 "plDescription" => _("Group applications"),
1045 "plSelfModify" => FALSE,
1046 "plDepends" => array(),
1047 "plPriority" => 0,
1048 "plSection" => array("admin"),
1049 "plCategory" => array("groups"),
1050 "plProvidedAcls"=> array(
1051 "gosaMemberApplication" => _("Application"),
1052 "FAIrelease" => _("Release"),
1053 "gosaApplicationParameter" => _("Application parameter"))
1054 ));
1055 }
1058 function PrepareForCopyPaste($source)
1059 {
1060 }
1063 function multiple_save_object()
1064 {
1065 if(isset($_POST['group_apps_multi'])){
1066 $this->save_object();
1067 plugin::multiple_save_object();
1069 /* Get posts */
1070 foreach(array("apps") as $attr){
1071 if(isset($_POST['use_'.$attr])) {
1072 $this->multi_boxes[] = $attr;
1073 }
1074 }
1075 }
1076 }
1079 function get_multi_edit_values()
1080 {
1081 $ret = plugin::get_multi_edit_values();
1083 if(in_array("apps",$this->multi_boxes)){
1084 $ret['gosaApplicationParameter'] = $this->gosaApplicationParameter;
1085 $ret['Categories'] = $this->Categories;
1086 $ret['gosaMemberApplication'] = $this->gosaMemberApplication;
1087 $ret['FAIrelease'] = $this->FAIrelease;
1088 $ret['appoption'] = $this->appoption;
1089 }
1090 return($ret);
1091 }
1092 }
1093 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1094 ?>