af56827a189ab05a8ab7c63f42605bd6963c71af
1 <?php
2 class appgroup extends plugin
3 {
4 /* CLI vars */
5 var $cli_summary= "Manage application groups";
6 var $cli_description= "Some longer text\nfor help";
7 var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
9 /* Appgroup attributes */
10 var $gosaMemberApplication= array();
12 /* Helpers */
13 var $department= "";
14 var $apps= array();
15 var $used_apps= array();
16 var $opt_edit= FALSE;
17 var $option_name= array();
18 var $option_value= array();
19 var $appoption= array();
20 var $table= "";
21 var $curbase = "";
22 var $curCatDir;
23 var $curCatDepth=0;
24 var $Categories=array();
27 /* attribute list for save action */
28 var $attributes= array();
29 var $objectclasses= array("gosaApplicationGroup");
31 function appgroup ($config, $dn= NULL)
32 {
33 plugin::plugin ($config, $dn);
35 /* Load member applications */
36 if (isset ($this->attrs["gosaMemberApplication"][0])){
37 for ($i= 0; $i<$this->attrs["gosaMemberApplication"]["count"]; $i++){
38 $this->gosaMemberApplication[]=
39 $this->attrs["gosaMemberApplication"][$i];
40 }
41 }
43 /* Load application options */
44 if (isset($this->attrs['gosaApplicationParameter'])){
45 for ($i= 0; $i<$this->attrs['gosaApplicationParameter']['count']; $i++){
46 $option= preg_replace('/^[^:]+:/', '',
47 $this->attrs['gosaApplicationParameter'][$i]);
48 $name= preg_replace('/:.*$/', '',
49 $this->attrs['gosaApplicationParameter'][$i]);
50 $this->appoption[$name]= $option;
51 }
52 }
54 $tmp = array();
55 $tmp2 = array();
57 $prios = array();
59 $cats = array();
61 foreach($this->gosaMemberApplication as $memberApp){
62 if(preg_match("/\|/i",$memberApp)){
63 $tmp = split("\|",$memberApp);
64 $tmp2[$tmp[1]][$tmp[2]] = array("App"=>$tmp[0],"Cat"=>$tmp[1]);
65 $cats [$tmp[1]] = $tmp[1];
66 $prios[$tmp[1]][$tmp[2]] = $tmp[2];
67 }else{
68 $tmp2[]['App'] = $memberApp;
69 $tmp2[]['Cat'] = "";
70 }
71 }
73 $this->gosaMemberApplication = $tmp2;
74 foreach($cats as $cat ){
75 $max = max($prios[$cat]);
76 $min = 1;//min($prios[$cat]);
77 $last = false;
78 for($i = $min ; $i < $max ; $i++){
79 if(!isset($prios[$cat][$i])){
80 if($last == false){
81 $this->gosaMemberApplication[$cat][$i]['Cat'] = $cat;
82 $this->gosaMemberApplication[$cat][$i]['App'] = "__SEPARATOR__".$i;
84 $last = true;
85 }
86 }else{
87 $last = false;
88 }
89 }
90 }
92 $tmp = array();
93 foreach($this->gosaMemberApplication as $key => $entries){
94 ksort ($entries);
95 foreach($entries as $entry){
96 $tmp[$key][]= $entry;
97 }
98 }
99 $this->gosaMemberApplication = $tmp;
101 $this->curbase = $this->config->current['BASE'];
102 }
104 function getOneUp($appl)
105 {
106 $cat = $this->curCatDir;
107 if(!isset($this->gosaMemberApplication[$cat][($appl-1)])) return;
108 $f = 1;
109 if($appl == 1 ){
110 $f = 0;
111 }
112 $first = array_slice($this->gosaMemberApplication[$cat],0,($appl-1));
113 $middle = array_slice($this->gosaMemberApplication[$cat],$appl-1,(($appl+1)-$f));
114 $last = array_slice($this->gosaMemberApplication[$cat],($appl+1));
115 $tmp = array();
116 foreach($first as $entry){
117 $tmp[] = $entry;
118 }
119 foreach(array_reverse($middle) as $entry){
120 $tmp[] = $entry;
121 }
122 foreach($last as $entry){
123 $tmp[] = $entry;
124 }
125 $this->gosaMemberApplication[$cat] =$tmp;
126 }
128 function getOneDown($appl){
129 $cat = $this->curCatDir;
130 if(!isset($this->gosaMemberApplication[$cat][($appl+1)])) return;
131 $f = 0;
132 if($appl == 0 ){
133 $f = 1;
134 }
135 $first = array_slice($this->gosaMemberApplication[$cat],0,($appl));
136 $middle = array_slice($this->gosaMemberApplication[$cat],$appl,(($appl+1+$f)));
137 $last = array_slice($this->gosaMemberApplication[$cat],($appl+2));
139 $tmp = array();
140 foreach($first as $entry){
141 $tmp[] = $entry;
142 }
143 foreach(array_reverse($middle) as $entry){
144 $tmp[] = $entry;
145 }
146 foreach($last as $entry){
147 $tmp[] = $entry;
148 }
149 $this->gosaMemberApplication[$cat] =$tmp;
150 }
152 function AddSeperator()
153 {
154 $cat = $this->curCatDir;
155 $cnt = count($this->gosaMemberApplication[$cat]);
156 $tmp = array("Cat" => $cat , "App" => "__SEPARATOR__".($cnt+1));
157 $this->gosaMemberApplication[$cat][] = $tmp;
158 }
160 function execute()
161 {
162 /* Call parent execute */
163 plugin::execute();
165 if((isset($_GET['act']))&&($_GET['act']=="depopen")){
166 $dep = base64_decode($_GET['depid']);
167 if(isset($this->config->idepartments[$dep])){
168 $this->curbase = $dep;
169 }
170 }
172 if(isset($_POST['AddSeperator'])){
173 $this->AddSeperator();
174 }
176 if((isset($_GET['act']))&&($_GET['act']=="open")){
177 $this->curCatDir = $_GET['id'];
179 $found = false;
180 foreach($this->Categories as $key => $name ){
181 if($this->curCatDir==$name){
182 $tmp = array_flip(split("\/",$key));
184 $this->curCatDepth = ($tmp[$this->curCatDir]);
185 $found= true;
186 }
187 }
188 if(!$found){
189 $this->curCatDir = "";
190 $this->curCatDepth = 0;
191 }
192 }
194 /* Do we need to flip is_account state? */
195 if (isset($_POST['modify_state'])){
196 $this->is_account= !$this->is_account;
197 }
199 /* Do we represent a valid group? */
200 if (!$this->is_account && $this->parent == NULL){
201 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
202 _("This 'dn' is no appgroup.")."</b>";
203 return ($display);
204 }
206 /* Show tab dialog headers */
207 $display= "";
208 if ($this->parent != NULL){
209 if ($this->is_account){
210 $display= $this->show_header(_("Remove applications"),
211 _("This group has application features enabled. You can disable them by clicking below."));
212 } else {
213 $display.= $this->show_header(_("Create applications"),
214 _("This group has application features disabled. You can enable them by clicking below."));
215 return ($display);
216 }
217 }
219 $this->reload();
220 foreach($_POST as $name => $value){
221 if(preg_match("/DelApp_/",$name)){
222 $app = $value;
224 foreach($this->gosaMemberApplication as $key => $cat){
225 foreach($cat as $key2 => $app){
226 if($app['App'] == $value){
227 unset($this->gosaMemberApplication[$key][$key2]);
228 if(isset($this->used_apps[$value])){
229 unset($this->used_apps[$value]);
230 }
231 }
232 }
233 }
234 }
235 if(preg_match("/EdiApp_/",$name)){
236 $appname = $value;
237 /* We've got the appname, get parameters from ldap */
238 $ldap= $this->config->get_ldap_link();
239 $ldap->cd($this->config->current['BASE']);
240 $ldap->search("(&(objectClass=gosaApplication)(cn=$appname))");
241 if ($ldap->count() != 1){
242 print_red (_("The selected application name is not uniq. Please check your LDAP."));
243 } else {
244 $attrs= $ldap->fetch();
245 if(isset($attrs['gosaApplicationParameter'])){
246 $this->dialog= TRUE;
248 /* Fill name and value arrays */
249 for ($i= 0; $i<$attrs['gosaApplicationParameter']['count']; $i++){
250 $option= preg_replace('/^[^:]+:/', '',
251 $attrs['gosaApplicationParameter'][$i]);
252 $name= preg_replace('/:.*$/', '',
253 $attrs['gosaApplicationParameter'][$i]);
254 $this->option_name[$i]= $name;
256 /* Fill with values from application, default should be
257 loaded by the external scripts */
258 if (isset($this->appoption[$name])){
259 $this->option_value[$i]= $this->appoption[$name];
260 }
261 }
263 /* Create edit field */
264 $table= "<table summary=\"\">";
265 for ($i= 0; $i < count($this->option_name); $i++){
266 if (isset($this->option_value[$i])){
267 $value= $this->option_value[$i];
268 } else {
269 $value= "";
270 }
271 $table.="<tr><td>".$this->option_name[$i]."</td><td>".
272 "<input name=\"value$i\" size=60 maxlength=250 ".
273 "value=\"".$value."\"><br></td></tr>";
274 }
275 $table.= "</table>";
276 $this->table= $table;
277 } else {
278 print_red (_("The selected application has no options."));
279 }
280 }
281 }
282 }
283 $this->reload();
285 /* Add group with post */
286 if((isset($_GET['act']))&&($_GET['act']=="add")){
287 $this->used_apps[$_GET['id']]= $_GET['id'];
288 asort($this->used_apps);
289 $this->addApp($_GET['id']);
290 }
292 /* Add multiple */
293 if(isset($_POST['AddApps'])){
294 foreach($_POST as $name => $value){
295 if(preg_match("/AddApp_/",$name)){
296 $app = preg_replace("/AddApp_/","",$name);
297 $this->addApp($app);
298 }
299 }
300 }
303 /* Cancel edit options? */
304 if (isset($_POST['edit_options_cancel'])){
305 $this->dialog= FALSE;
306 }
308 /* Finish edit options? */
309 if (isset($_POST['edit_options_finish'])){
310 $this->dialog= FALSE;
312 /* Save informations passed by the user */
313 $this->option_value= array();
314 for ($i= 0; $i<count($this->option_name); $i++){
315 $this->appoption[$this->option_name[$i]]= $_POST["value$i"];
316 $this->is_modified= TRUE;
317 }
318 }
320 /* Prepare templating stuff */
321 $smarty= get_smarty();
322 $smarty->assign("used_apps", $this->used_apps);
323 $apps= array();
324 foreach ($this->apps as $key => $value){
325 if (!array_key_exists($key, $this->used_apps)){
326 $apps["$key"]= "$value";
327 }
328 }
330 $div = new DivSelectBox("appgroup");
332 $div->SetHeight(300);
334 /* NEW LIST MANAGMENT
335 * We also need to search for the departments
336 * So we are able to navigate like in konquerer
337 */
339 $ldap = $this->config->get_ldap_link();
340 $ldap->cd($this->curbase) ;
341 $ldap->ls("(objectClass=gosaDepartment)");
342 $departments= array();
343 $tmp = array();
344 while ($value = $ldap->fetch()){
345 $tmp[strtolower($value['dn']).$value['dn']]=$value;
346 }
347 ksort($tmp);
348 foreach($tmp as $value){
349 if($value["description"][0]!=".."){
350 $departments[$value['dn']]=convert_department_dn($value['dn'])." - [".$value["description"][0]."]";
351 }else{
352 $departments[$value['dn']]=convert_department_dn($value['dn']);
353 }
354 }
356 /* END NEW LIST MANAGMENT
357 */
359 $linkopen = "<a href='?plug=".$_GET['plug']."&act=depopen&depid=%s'>%s</a>";
360 $linkadd = "<a href='?plug=".$_GET['plug']."&act=add&id=%s'>%s</a>";
362 $base_back = preg_replace("/^[^,]+,/","",$this->curbase);
363 if((strlen($base_back)>= strlen($this->config->current['BASE']))&&($this->curbase!=$this->config->current['BASE'])){
364 $div->AddEntry(array(
365 array("string"=>sprintf($linkopen,base64_encode($base_back),".. ["._("back")."]"),
366 "attach"=>"style='border:0px;'")
367 ));
368 }
369 foreach($departments as $key => $app){
370 $div->AddEntry(array(
371 array("string"=>"<img src='images/folder.png' alt='"._("department")."'> ".sprintf($linkopen,base64_encode($key),$app),
372 "attach"=>"style='border:0px;'")
373 ));
374 }
376 foreach($apps as $key => $app){
377 $div->AddEntry(array(
378 array("string"=>sprintf("<input type='checkbox' value='1' name='AddApp_%s'>",$key).
379 "<img src='images/select_application.png' alt='"._("application")."'> ".sprintf($linkadd,$key,$app),
380 "attach"=>"style='border:0px;'")
381 ));
382 }
384 if((isset($_GET['act']))&&(($_GET['act'] == "one_up")||($_GET['act']=="one_down"))){
385 if(isset($_GET['id'])){
386 $id = $_GET['id'];
387 $act = $_GET['act'];
388 $found = -1;
389 foreach($this->gosaMemberApplication[$this->curCatDir] as $key => $member){
390 if($id == $member['App']){
391 $found = $key;
392 }
393 }
395 if($found != -1){
396 if($act == "one_up"){
397 $this->getOneUp($found);
398 }elseif($act == "one_down") {
399 $this->getOneDown($found);
400 }
401 }
402 }
403 }
405 $div2 = new DivSelectBox("appgroup");
406 $div2->SetHeight(300);
408 $menu = $this->CreateCatMenu();
409 $str_noprio = " %s ";
410 $linkopen = "<img src='images/folder.png'> <a href='?plug=".$_GET['plug']."&act=open&id=%s'>%s</a>";
411 $app = "<img src='images/select_application.png'> %s";
413 /* append back entry */
414 if($menu["__BACK__"] != false){
415 $div2 ->AddEntry(array(
416 array("string"=>sprintf($linkopen,$menu["__BACK__"],".. [ "._("back")." ]")),
417 array("string"=>" ","attach"=>"style='border-right:0px;'")
418 ));
419 }
421 foreach($menu['__CATEGORY__'] as $path => $name){
422 $div2 ->AddEntry(array(array("string"=>sprintf($linkopen,$path,$name)),array("string"=>" ","attach"=>"style='border-right:0px;'")));
423 }
425 /* Append entries */
427 $separator ="<hr height=1 size=1></hr>";
429 $upudown = "<a href='?plug=".$_GET['plug']."&act=one_up&id=%s'> <img src='images/sort_up.png' border=0></a>".
430 " <a href='?plug=".$_GET['plug']."&act=one_down&id=%s'> <img src='images/sort_down.png' border=0></a>".
431 " <input type='image' src='images/edittrash.png' name='DelApp_%s' value='%s'>";
432 $edit= " <input type='image' src='images/edit.png' name='EdiApp_%s' value='%s'>";
434 foreach($menu["__ENTRY__"] as $path => $entry){
436 if(preg_match("/__SEPARATOR__/",$path)){
437 $div2 ->AddEntry(array(array("string"=>$separator),
438 array("string"=>preg_replace("/\%s/",$entry['name'],$upudown),"attach"=>"style='border-right:0px;'")));
440 }else{
441 $div2 ->AddEntry(array(array("string"=>sprintf($app,$entry['name'])),
442 array("string"=>preg_replace("/\%s/",$entry['name'],$upudown.$edit),"attach"=>"style='border-right:0px;'")));
443 }
444 }
446 $smarty->assign("UsedApps", $div2->DrawList());
447 $smarty->assign("List", $div->DrawList());
448 $smarty->assign("apps", $apps);
450 /* Show main page */
451 if ($this->dialog){
452 $smarty->assign("table", $this->table);
453 $display.= $smarty->fetch (get_template_path('application_options.tpl', TRUE));
454 } else {
455 $display.= $smarty->fetch (get_template_path('application.tpl', TRUE));
456 }
457 return ($display);
458 }
461 function remove_from_parent()
462 {
463 plugin::remove_from_parent();
465 $this->attrs["gosaMemberApplication"]= array();
467 $ldap= $this->config->get_ldap_link();
468 $ldap->cd($this->dn);
469 $ldap->modify($this->attrs);
470 show_ldap_error($ldap->get_error());
472 /* Optionally execute a command after we're done */
473 $this->handle_post_events("remove");
474 }
477 /* Save data to object */
478 # function save_object()
479 # {
480 # plugin::save_object();
481 # }
484 /* Save to LDAP */
485 function save()
486 {
487 plugin::save();
489 /* Copy members */
491 $cats = array(""=>0);
492 foreach($this->gosaMemberApplication as $mem => $entries){
493 $cats[$mem]= 0;
494 }
496 $this->attrs["gosaMemberApplication"]= array();
497 $lastSEP = false;
498 foreach ($this->gosaMemberApplication as $cat){
499 foreach($cat as $val){
500 $cats[$val['Cat']] ++;
501 if(!preg_match("/__SEPARATOR__/i",$val['App'])){
502 $this->attrs["gosaMemberApplication"][]= $val['App']."|".$val['Cat']."|".$cats[$val['Cat']];
503 $lastSEP = false;
504 }else{
505 if($lastSEP){
506 $cats[$val['Cat']] --;
507 }
508 $lastSEP = true;
509 }
510 }
511 }
513 /* Are there application parameters to be saved */
514 $this->attrs['gosaApplicationParameter']= array();
515 foreach($this->appoption as $name => $value){
516 if ($value != ""){
517 $this->attrs['gosaApplicationParameter'][]= "$name:$value";
518 }
519 }
521 /* Write back to LDAP */
522 $ldap= $this->config->get_ldap_link();
523 $ldap->cd($this->dn);
524 $ldap->modify($this->attrs);
525 show_ldap_error($ldap->get_error());
527 /* Optionally execute a command after we're done */
528 if ($this->initially_was_account == $this->is_account){
529 if ($this->is_modified){
530 $this->handle_post_events("mofify");
531 }
532 } else {
533 $this->handle_post_events("add");
534 }
536 }
538 function check()
539 {
540 $message= array();
541 return ($message);
542 }
545 function reload()
546 {
547 /* Generate applist */
548 $this->apps= array();
549 $ldap= $this->config->get_ldap_link();
550 $ldap->cd ("ou=apps,".$this->curbase);
552 $ldap->ls ("(objectClass=gosaApplication)","ou=apps,".$this->curbase);
553 while ($attrs= $ldap->fetch()){
554 if (isset($attrs["description"][0])){
555 $this->apps[$attrs["cn"][0]]=
556 $attrs["cn"][0]." (".
557 $attrs["description"][0].")";
558 } else {
559 $this->apps[$attrs["cn"][0]]=
560 $attrs["cn"][0];
561 }
562 }
563 natcasesort ($this->apps);
564 reset ($this->apps);
566 if(is_array($this->gosaMemberApplication))
567 foreach ($this->gosaMemberApplication as $cat){
568 if(is_array($cat))
569 foreach($cat as $entry){
570 $this->used_apps[$entry['App']]= $entry['App'];
571 }
572 }
573 }
576 function CreateCatMenu()
577 {
578 /* The current category */
579 $na = $this->curCatDir;
581 /* the current folder depth */
582 $nd = $this->curCatDepth;
584 /* init array */
585 $return = array();
587 $return["__CATEGORY__"] = array(); // Categories
588 $return["__ENTRY__"] = array(); // Entries in this category
589 $return["__BACK__"] = false; // The back entry
591 $tmp = new xmlParse();
593 $this->Categories= array();
594 if(!isset($this->config->data['MAIN']['KDE_APPLICATIONS_MENU'])) {
595 print_red(_("There is no value for 'KDE_APPLICATIONS_MENU' specified in your gosa.conf."));
596 }else{
597 $path = $this->config->data['MAIN']['KDE_APPLICATIONS_MENU'];
598 if(file_exists($path)){
599 if(is_readable($path)){
600 $tmp->parseMenu($path);
601 $this->Categories = $tmp->GetData();
602 }else{
603 print_red(sprintf(_("The specified file '%s' for 'KDE_APPLICATIONS_MENU' in your gosa.conf is not accessable."),$path));
604 }
605 }else{
606 print_red(sprintf(_("The specified file '%s' for 'KDE_APPLICATIONS_MENU' in your gosa.conf is not accessable."),$path));
607 }
608 }
611 /* Create new categories to return */
612 foreach($this->Categories as $key => $val){
614 /* Split categories by | to get name and priority */
615 $nk = split("\/",$key);
617 /* Check if this category must be shown */
618 if((isset($nk[$nd]))&&(is_array($nk))&&($nk[$nd] == $na)){
620 /* Add this to the list, if theres is a sub category */
621 if(isset($nk[($nd+1)])){
622 $return["__CATEGORY__"][$nk[($nd+1)]] = $nk[($nd+1)];
623 }
625 /* Create back entry */
626 if($nd !=0 ){
627 $return['__BACK__'] = $nk[($nd-1)];
628 }else{
629 $return['__BACK__'] = "..";
630 }
632 }elseif($na==""){
633 $return["__CATEGORY__"][$nk[0]] = $nk[0];
634 }
638 }
640 /* If back entry isn't set, set it to false (none) or to ..(base)*/
641 if($return["__BACK__"] == false){
642 $return['__BACK__'] = "..";
643 if($nd == 0 ){
644 $return['__BACK__'] = false;
645 }
646 }
647 if((isset($this->gosaMemberApplication[$this->curCatDir]))&&(is_array($this->gosaMemberApplication[$this->curCatDir]))){
648 foreach($this->gosaMemberApplication[$this->curCatDir] as $app){
649 if($app['Cat'] == $this->curCatDir){
650 $return["__ENTRY__"][$app['App']] = array("name"=>$app['App']) ;
651 }
652 }
653 }
654 /* Return this all ..*/
655 return($return);
656 }
658 function addApp($cn)
659 {
661 if((isset($this->gosaMemberApplication[$this->curCatDir]))&&(is_array($this->gosaMemberApplication[$this->curCatDir]))){
662 foreach($this->gosaMemberApplication[$this->curCatDir] as $entry){
663 if($entry['App'] == $cn) return;
664 }
665 }
666 $this->gosaMemberApplication[$this->curCatDir][]= array("App"=>$cn,"Cat"=>$this->curCatDir);
667 $this->used_apps[$cn]=$cn;
668 $this->is_modified= TRUE;
669 }
671 function removeApp($cn)
672 {
673 $temp= array();
674 foreach ($this->gosaMemberApplication as $value){
675 if ($value != $cn){
676 $temp[]= $value;
677 }
678 }
679 $this->gosaMemberApplication= $temp;
680 $this->is_modified= TRUE;
681 }
683 }
685 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
686 ?>