Code

some changes for new group->applications.
[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 = "/";
19   var $apps = array();
21   public function __construct(&$config, $dn= NULL, $parent= NULL)
22   {
23     plugin::plugin($config,$dn,$parent);
24     $this->dn = $dn; 
25     $this->_load_menu_structure();
26     $this->a_Structure_on_load = $this->a_Structure;
28     /* Check if we have relase mangement enabled and prepare group application for release management */
29     $tmp = $config->search("faiManagement", "CLASS",array('menu','tabs'));
30     if(!empty($tmp)){
31       $this->enableReleaseManagement = true;
32     }
34     /* Get required release informations */
35     if($this->enableReleaseManagement){
36       $this->Releases   = $this->getReleases();
37       $this->FAIrelease = "/";
38     }
39     $this->curbase = $this->config->current['BASE'];
40     $this->reload();
41   }
44   function reload()
45   {
46     $ldap = $this->config->get_ldap_link();
47     $ldap->cd($this->curbase);
48     $ldap->search("ou=apps",array("dn"));
49     $app_deps = array();
50     while($attrs = $ldap->fetch()){
51       $app_deps[] = $attrs['dn'];
52     }
54     foreach($app_deps as $dep){
55     #  $list = get_list("(objectClass=gosaApplication)","application",$dep,array("*"));
56     }
58   }
61   function getReleases()
62   {
63     /* Only display those releases that we are able to read */
64     $dn     = $this->config->current['BASE'];
65     $filter = "(&(objectClass=organizationalUnit)(objectClass=FAIbranch))";
66     $res    = get_list($filter,"application", $dn, array("ou"), GL_SUBSEARCH);
68     $ret =array();
69     foreach($res as $attrs){
70       if(preg_match("/".get_ou('applicationou')."/",$attrs['dn'])){
71         $bb     = preg_replace("/".get_ou('applicationou').".*/","",$attrs['dn']);
72         $parts  = array_reverse(split("ou=",$bb));
74         $str ="";
75         foreach($parts as $part){
76           if(empty($part)) {
77             continue;
78           }
79           $str .= str_replace(",","",$part)."/";
80         }
81         $name = preg_replace("/\/$/","",$str);
82         if(empty($name)) {
83           $name ="/";
84         }
85         $ret[$attrs['dn']] = $name;
86       }
87     }
88     natcasesort($ret);
89     return($ret);
90   }
93   function getReleases2()
94   {
95     $ret = array();
96     $ret['/'] = array("UNIQID" => 0, "NAME" => "/");
97     $tmp = $this->_get_all_entries();
98     foreach($tmp as $entry){
99       if($entry['TYPE'] == "RELEASE"){
100         unset($entry['ENTRIES']);
101         $ret[] = $entry;
102       }  
103     }
104     return($ret);
105   }
108   function _load_menu_structure()
109   {
110     $this->a_Structure  = array();
111     $ldap = $this->config->get_ldap_link();
112     $ldap->cd($this->dn);
113     $ldap->search("(|(objectClass=gotoSubmenuEntry)(objectClass=FAIbranch)(objectClass=gotoMenuEntry))",array("*"));
114     $parent_id = 0;
115     while($attrs = $ldap->fetch()){
116       $cur = &$this->a_Structure;
117       $sub_dn       = preg_replace("/,".normalizePreg($this->dn)."$/","",$attrs['dn']);
118       $sub_dn_array = split("\,",$sub_dn);
119       for($i = (count($sub_dn_array)-1) ; $i >= 0 ; $i--){
120         $name = preg_replace("/^[^=]*+=/","",$sub_dn_array[$i]);
121         if($i != 0){
122           foreach($cur as $key => $entry){
123             if($entry['NAME'] == $name){
124               $cur = &$cur[$key]['ENTRIES'];
125               $parent_id = $entry['UNIQID'];
126             }
127           }
128         }else{
130           $priority = -1;
131           if(isset($attrs['gosaApplicationPriority'])){
132             $priority= $attrs['gosaApplicationPriority'][0];
133           }
134           while(isset($cur[$priority])){
135             $priority ++;
136           }
138           if(in_array("gotoSubmenuEntry",$attrs['objectClass'])){
139             $type = "FOLDER";
140           }elseif(in_array("gotoMenuEntry",$attrs['objectClass'])){
141             $type = "ENTRY";
142           }elseif(in_array("FAIbranch",$attrs['objectClass'])){
143             $type = "RELEASE";
144           }
146           $data = array();
147           $data['DN']       = $attrs['dn'];
148           $data['NAME']     = $name;
149           $data['TYPE']     = $type;
150           $data['PRIORITY'] = $priority;
151           $data['ENTRIES']  = array();
152           $data['UNIQID']   = uniqid();
153           $data['PARENT']   = $parent_id;
154           $cur[$priority]   = $data;
155           ksort($cur);
156         }
157       }
158     }
159   } 
162   function execute()
163   {
164     /* Call parent execute */
165     plugin::execute();
167     if(isset($_GET['r']))
168     $this->__construct($this->config,$this->dn);
170     $smarty = get_smarty();
173     /* Create application list */
174     $div = new divSelectBox("appgroup");
175     $div->SetHeight(300);
176     $departments = array();
177     $res = get_list("(objectClass=gosaDepartment)", "application", $this->curbase,array("description","cn","ou"),GL_SIZELIMIT);
178     foreach($res as $value){
179       $fdn = $value['dn'];
180       $fdn = preg_replace("/".normalizePreg($this->curbase)."/","",$fdn);
181       $fdn= @LDAP::fix($fdn);
182       if($value["description"][0]!=".."){
183         $departments[$value['dn']]= convert_department_dn($fdn)." - [".$value["description"][0]."]";
184       }else{
185         $departments[$value['dn']]=convert_department_dn($fdn)." ["._("Back")."]";
186       }
187     }
189     /* Create 'open' and 'add' links */
190     if($this->acl_is_writeable("gosaMemberApplication",$this->no_release_acls)){
191       $linkadd  = "<a href='?plug=".$_GET['plug']."&amp;act=add&amp;id=%s'>%s</a>";
192     }else{
193       $linkadd = "%s";
194     }
195     $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=depopen&amp;depid=%s'>%s</a>";
197     /* Create base back entry */
198     $base_back = preg_replace("/^[^,]+,/","",$this->curbase);
199     if((strlen($base_back)>= strlen($this->config->current['BASE']))&&($this->curbase!=$this->config->current['BASE'])){
200       $div->AddEntry(array(
201             array("string"=>sprintf($linkopen,base64_encode($base_back),".. ["._("back")."]"),
202               "attach"=>"style='border:0px;'")
203             ));
204     }
206     /* Append departments for current base */
207     foreach($departments as $key => $app){
208       $div->AddEntry(array(
209             array("string"=>"<img class='center' src='images/folder.png' alt='"._("department")."'>&nbsp;".sprintf($linkopen,
210                 base64_encode($key),$app),
211               "attach"=>"style='border:0px;'")
212             ));
213     }
215     /* Add applications found on this base */
216     foreach($apps as $key => $app){
217       $div->AddEntry(array(
218             array("string"=>sprintf("<input class='center' type='checkbox' value='1' name='AddApp_%s'>",$key).
219               "<img class='center' src='images/select_application.png' alt='"._("application")."'>&nbsp;".sprintf($linkadd,$key,$app),
220               "attach"=>"style='border:0px;'")
221             ));
222     }
224     
225     $entries = $this->_get_all_entries();
226     $smarty->assign("FAIrelease",$this->FAIrelease);
227     $smarty->assign("app_list",$div->DrawList());
228     $smarty->assign("releases",$this->Releases);
230     if($this->FAIrelease == "/"){
231       $tmp = $entries;
232     }else{
233       $tmp = $entries[$this->FAIrelease];
234     }
236     $smarty->assign("entries",$this->_get_all_entries(TRUE,$tmp['ENTRIES']));
237     $display= $smarty->fetch (get_template_path('app_list.tpl', TRUE, dirname(__FILE__)));
238     return($display);
239   }
241   
242   function _edit_entry_edit($id)
243   {
244   }
247   function remove_from_parent()
248   {
249   }
252   function save()
253   {
254   }
257   function check()
258   {
259   }
263   /* !\brief Handle ui POSTS, like sort up/down/delete
264    */ 
265   function save_object()
266   {
267     foreach($_POST as $name => $value){
268       if(preg_match("/del_/",$name)){
269         $id = preg_replace("/^del_/","",$name);
270         $id = preg_replace("/_(x|y)$/","",$id);
271         $this->_remove_entry_id($id);
272         break;
273       }
274       if(preg_match("/edit_/",$name)){
275         $id = preg_replace("/^edit_/","",$name);
276         $id = preg_replace("/_(x|y)$/","",$id);
277         $this->_edit_entry_edit($id);
278         break;
279       }
280       if(preg_match("/up_/",$name)){
281         $id = preg_replace("/^up_/","",$name);
282         $id = preg_replace("/_(x|y)$/","",$id);
283         $this->_move_entry_up($id);
284         break;
285       }
286       if(preg_match("/down_/",$name)){
287         $id = preg_replace("/^down_/","",$name);
288         $id = preg_replace("/_(x|y)$/","",$id);
289         $this->_move_entry_down($id);
290         break;
291       }
292     }
293     if(isset($_POST['FAIrelease'])){
294       $this->FAIrelease = $_POST['FAIrelease'];
295     }
296     if(isset($_GET['act']) && $_GET['act'] == 'depopen'){
297       $this->curbase = base64_decode($_GET['depid']);
298     }
299     $this->reload();
300   }
303   /* !\brief Remove the given id from the menu structure.
304       @param  String  ID to of the entry we want to remove.
305       @return Boolean TRUE on success
306    */
307   function _remove_entry_id($id,$start = NULL)
308   {
309     if($start == NULL){
310       $start = &$this->a_Structure;
311     }
312     foreach($start as $name => $entry){
313       if($entry['UNIQID'] == $id){
314         unset($start[$name]);
315         return(TRUE);
316       }
317       if(isset($entry['ENTRIES']) && count($start[$name]['ENTRIES'])){
318         if($this->_remove_entry_id($id,&$start[$name]['ENTRIES'])){
319           return(TRUE);
320         }
321       }
322     }
323     return(FALSE);
324   }
327   /*! \brief move the object identified by the given ID one position up.
328       @param  String  The ID of the entry to be moved.
329       @return Boolean TRUE on success, else FALSE;
330    */
331   function _move_entry_up($id)
332   {
333     $all = $this->_get_all_entries(TRUE);
334     $parent = FALSE;
335     foreach($all as $entry){
336       if(isset($entry['UNIQID']) && $entry['UNIQID'] == $id){
337         if($parent != FALSE){
338           return($this->__switch_entries($id,"up",$parent));
339         }
340       }else{
341         if(in_array($entry['TYPE'],array("CLOSE","OPEN"))){
342           $parent = $entry['PARENT'];
343         }else{
344           $parent = $entry['UNIQID'];
345         }
346       }
347     }
348     return(FALSE);
349   }
352   /*! \brief move the object identified by the given ID one position down.
353       @param  String  The ID of the entry to be moved.
354       @return Boolean TRUE on success, else FALSE;
355    */
356   function _move_entry_down($id)
357   {
358     $all = $this->_get_all_entries(TRUE);
359     $found = FALSE;
360     foreach($all as $entry){
361       if(isset($entry['UNIQID']) && $entry['UNIQID'] == $id){
362         $found = TRUE;
363         continue;
364       }else{
365         if(in_array($entry['TYPE'],array("CLOSE","OPEN"))){
366           $parent = $entry['PARENT'];
367         }else{
368           $parent = $entry['UNIQID'];
369         }
370         if($found){
371           return($this->__switch_entries($id,"down",$parent));
372         }
373       }
374     }
375     return(FALSE);
376   }
379   /*! \brief  Return all entries linear. 
380       @param  Boolean   $add_tags  If TRUE, OPEN/CLOSE Tags will be appended.
381       @param  &Array    Start here, Pointer to an array.
382    */ 
383   function _get_all_entries($add_tags = FALSE,$cur = NULL)
384   {
385     $ret = array();
386     if($cur == NULL){
387       $cur = &$this->a_Structure;
388     }
389     foreach($cur as $key => $entry){
391       $tmp = $entry;
392       if(!$add_tags){
393         $ret[$tmp['UNIQID']] = &$cur[$key];
394         if(isset($entry['ENTRIES']) && count($entry['ENTRIES'])){
395           $ret = array_merge($ret,$this->_get_all_entries($add_tags,&$cur[$key]['ENTRIES']));
396         }
397       }else{
398       if(isset($tmp['ENTRIES'])){
399         unset($tmp['ENTRIES']);
400       }
401         $ret[] = $tmp;
402         if(isset($entry['ENTRIES']) && count($entry['ENTRIES'])){
403           $ret[] = array("TYPE" => "OPEN", "PARENT" => $entry['UNIQID']);
404           $ret = array_merge($ret,$this->_get_all_entries($add_tags,&$cur[$key]['ENTRIES']));
405           $ret[] = array("TYPE" => "CLOSE" , "PARENT" => $entry['UNIQID']);
406         }
407       }
408     }
409     return($ret);
410   }
413   /* !\brief Switch one entry with another 
414      @param  String  from  The source ID.
415      @param  String  type  "up"/"down" type switched.
416      @param  String  to    The destination ID.
417      return  Boolean TRUE on success 
418    */
419   function __switch_entries($from,$type,$to)
420   {
421     $all = $this->_get_all_entries();
423     $o_to   = &$all[$to];
424     $o_from = &$all[$from];
426     /***********************
427      * Source == Destination 
428      * Move into next parent. 
429      ************************/
430     if($to == $from){
431       $to = $o_to['PARENT'];
432       $o_to   = &$all[$to];
433     }
435     /***********************
436      * Target is container 
437      ************************/
438     if(in_array($o_to['TYPE'],array("FOLDER","RELEASE"))){
440       /***********************
441        * Move into parent folder  
442        *   + Target 
443        *   |-> Source
444        ************************/
445       if($to == $o_from['PARENT']){
446  
447         /* Check if source is a folder object 
448          */
449         $o_to_sub = &$all[$o_to['PARENT']]; 
450         if(in_array($o_to_sub['TYPE'],array("FOLDER","RELEASE"))){
452           /* Removing old */
453           $tmp = array();
454           foreach($o_to['ENTRIES'] as $key => $entry){
455             if($entry['UNIQID'] == $from){
456               if(!$this->_remove_entry_id($from)){
457                 return(FALSE);
458               }
459             }
460           }
462           /* Adding new */
463           $tmp2 = array();
464           foreach($o_to_sub['ENTRIES'] as $key => $entry){
466             if($type == "up"){
467               if($entry['UNIQID'] == $to){
468                 $o_from['PARENT'] = $o_to_sub['UNIQID'];
469                 $tmp2[] = &$o_from;
470               }
471               $tmp2[] = &$o_to_sub['ENTRIES'][$key];
472             }else{
474               $tmp2[] = &$o_to_sub['ENTRIES'][$key];
475               if($entry['UNIQID'] == $to){
476                 $o_from['PARENT'] = $o_to_sub['UNIQID'];
477                 $tmp2[] = &$o_from;
478               }
479             }
480           }
481           $all[$o_to_sub['UNIQID']]['ENTRIES'] = $tmp2;
482         } 
483       }else{
485         /***********************
486          * Target is NOT parent container
487          *  + Parent Folder
488          *  |-> Source 
489          *  + Destination
490          ************************/
492         /* Removing old */
493         $o_to   = &$all[$to];
494         $o_from = &$all[$from];
495         $this->_remove_entry_id($from);
496         $o_from['PARENT']     = $to;
497         $o_to['ENTRIES'][]    = $o_from;
499       }
500     }else{
502       /***********************
503        * Source and Destination in some Folder.
504        *  + Parent folder
505        *  |-> Source
506        *  |-> Destination
507        ************************/
508       $o_to   = &$all[$to];
509       $o_from = &$all[$from];
511       if($o_to['PARENT'] == $o_from['PARENT']){
512         $tmp = $all[$to];
513         $all[$to]   = $o_from;
514         $all[$from] = $tmp;
515       }
516     }
517   }
520   /* Return plugin informations for acl handling  */ 
521   static function plInfo()
522   {
523     return (array(
524           "plShortName"   => _("Applications"),
525           "plDescription" => _("Group applications"),
526           "plSelfModify"  => FALSE,
527           "plDepends"     => array(),
528           "plPriority"    => 0,
529           "plSection"     => array("admin"),
530           "plCategory"    => array("groups"),
531           "plProvidedAcls"=> array(
532             "gosaMemberApplication"     => _("Application"),
533             "FAIrelease"                => _("Release"),
534             "gosaApplicationParameter"  => _("Application parameter"))
535           ));
536   }
539   function PrepareForCopyPaste($source)
540   {
541   }
544   function multiple_save_object()
545   {
546     if(isset($_POST['group_apps_multi'])){
547       $this->save_object(); 
548       plugin::multiple_save_object();    
549   
550       /* Get posts */
551       foreach(array("apps") as $attr){
552         if(isset($_POST['use_'.$attr])) {
553           $this->multi_boxes[] = $attr;
554         }
555       }
556     }
557   }
558   
560   function get_multi_edit_values()
561   {
562     $ret = plugin::get_multi_edit_values();
564     if(in_array("apps",$this->multi_boxes)){
565       $ret['gosaApplicationParameter'] = $this->gosaApplicationParameter;
566       $ret['Categories']               = $this->Categories;
567       $ret['gosaMemberApplication']    = $this->gosaMemberApplication;
568       $ret['FAIrelease']               = $this->FAIrelease;
569       $ret['appoption']                = $this->appoption;
570     }
571     return($ret);
572   }
574 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
575 ?>