59cf060f5ce8cd00a67bcc77a5ac738f71d91a14
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 }
229 }
230 }
231 }
232 if(preg_match("/EdiApp_/",$name)){
233 $appname = $value;
234 /* We've got the appname, get parameters from ldap */
235 $ldap= $this->config->get_ldap_link();
236 $ldap->cd($this->config->current['BASE']);
237 $ldap->search("(&(objectClass=gosaApplication)(cn=$appname))");
238 if ($ldap->count() != 1){
239 print_red (_("The selected application name is not uniq. Please check your LDAP."));
240 } else {
241 $attrs= $ldap->fetch();
242 if(isset($attrs['gosaApplicationParameter'])){
243 $this->dialog= TRUE;
245 /* Fill name and value arrays */
246 for ($i= 0; $i<$attrs['gosaApplicationParameter']['count']; $i++){
247 $option= preg_replace('/^[^:]+:/', '',
248 $attrs['gosaApplicationParameter'][$i]);
249 $name= preg_replace('/:.*$/', '',
250 $attrs['gosaApplicationParameter'][$i]);
251 $this->option_name[$i]= $name;
253 /* Fill with values from application, default should be
254 loaded by the external scripts */
255 if (isset($this->appoption[$name])){
256 $this->option_value[$i]= $this->appoption[$name];
257 }
258 }
260 /* Create edit field */
261 $table= "<table summary=\"\">";
262 for ($i= 0; $i < count($this->option_name); $i++){
263 if (isset($this->option_value[$i])){
264 $value= $this->option_value[$i];
265 } else {
266 $value= "";
267 }
268 $table.="<tr><td>".$this->option_name[$i]."</td><td>".
269 "<input name=\"value$i\" size=60 maxlength=250 ".
270 "value=\"".$value."\"><br></td></tr>";
271 }
272 $table.= "</table>";
273 $this->table= $table;
274 } else {
275 print_red (_("The selected application has no options."));
276 }
277 }
278 }
279 }
280 $this->reload();
282 /* Add group with post */
283 if((isset($_GET['act']))&&($_GET['act']=="add")){
284 $this->used_apps[$_GET['id']]= $_GET['id'];
285 asort($this->used_apps);
286 $this->addApp($_GET['id']);
287 }
289 /* Add multiple */
290 if(isset($_POST['AddApps'])){
291 foreach($_POST as $name => $value){
292 if(preg_match("/AddApp_/",$name)){
293 $app = preg_replace("/AddApp_/","",$name);
294 $this->addApp($app);
295 }
296 }
297 }
300 /* Cancel edit options? */
301 if (isset($_POST['edit_options_cancel'])){
302 $this->dialog= FALSE;
303 }
305 /* Finish edit options? */
306 if (isset($_POST['edit_options_finish'])){
307 $this->dialog= FALSE;
309 /* Save informations passed by the user */
310 $this->option_value= array();
311 for ($i= 0; $i<count($this->option_name); $i++){
312 $this->appoption[$this->option_name[$i]]= $_POST["value$i"];
313 $this->is_modified= TRUE;
314 }
315 }
317 /* Prepare templating stuff */
318 $smarty= get_smarty();
319 $smarty->assign("used_apps", $this->used_apps);
320 $apps= array();
321 foreach ($this->apps as $key => $value){
322 if (!array_key_exists($key, $this->used_apps)){
323 $apps["$key"]= "$value";
324 }
325 }
327 $div = new DivSelectBox("appgroup");
329 $div->SetHeight(300);
331 /* NEW LIST MANAGMENT
332 * We also need to search for the departments
333 * So we are able to navigate like in konquerer
334 */
336 $ldap = $this->config->get_ldap_link();
337 $ldap->cd($this->curbase) ;
338 $ldap->ls("(objectClass=gosaDepartment)");
339 $departments= array();
340 $tmp = array();
341 while ($value = $ldap->fetch()){
342 $tmp[strtolower($value['dn']).$value['dn']]=$value;
343 }
344 ksort($tmp);
345 foreach($tmp as $value){
346 if($value["description"][0]!=".."){
347 $departments[$value['dn']]=convert_department_dn($value['dn'])." - [".$value["description"][0]."]";
348 }else{
349 $departments[$value['dn']]=convert_department_dn($value['dn']);
350 }
351 }
353 /* END NEW LIST MANAGMENT
354 */
356 $linkopen = "<a href='?plug=".$_GET['plug']."&act=depopen&depid=%s'>%s</a>";
357 $linkadd = "<a href='?plug=".$_GET['plug']."&act=add&id=%s'>%s</a>";
359 $base_back = preg_replace("/^[^,]+,/","",$this->curbase);
360 if((strlen($base_back)>= strlen($this->config->current['BASE']))&&($this->curbase!=$this->config->current['BASE'])){
361 $div->AddEntry(array(
362 array("string"=>sprintf($linkopen,base64_encode($base_back),".. ["._("back")."]"),
363 "attach"=>"style='border:0px;'")
364 ));
365 }
366 foreach($departments as $key => $app){
367 $div->AddEntry(array(
368 array("string"=>"<img src='images/folder.png' alt='"._("department")."'> ".sprintf($linkopen,base64_encode($key),$app),
369 "attach"=>"style='border:0px;'")
370 ));
371 }
373 foreach($apps as $key => $app){
374 $div->AddEntry(array(
375 array("string"=>sprintf("<input type='checkbox' value='1' name='AddApp_%s'>",$key).
376 "<img src='images/select_application.png' alt='"._("application")."'> ".sprintf($linkadd,$key,$app),
377 "attach"=>"style='border:0px;'")
378 ));
379 }
381 if((isset($_GET['act']))&&(($_GET['act'] == "one_up")||($_GET['act']=="one_down"))){
382 if(isset($_GET['id'])){
383 $id = $_GET['id'];
384 $act = $_GET['act'];
385 $found = -1;
386 foreach($this->gosaMemberApplication[$this->curCatDir] as $key => $member){
387 if($id == $member['App']){
388 $found = $key;
389 }
390 }
392 if($found != -1){
393 if($act == "one_up"){
394 $this->getOneUp($found);
395 }elseif($act == "one_down") {
396 $this->getOneDown($found);
397 }
398 }
399 }
400 }
402 $div2 = new DivSelectBox("appgroup");
403 $div2->SetHeight(300);
405 $menu = $this->CreateCatMenu();
406 $str_noprio = " %s ";
407 $linkopen = "<img src='images/folder.png'> <a href='?plug=".$_GET['plug']."&act=open&id=%s'>%s</a>";
408 $app = "<img src='images/select_application.png'> %s";
410 /* append back entry */
411 if($menu["__BACK__"] != false){
412 $div2 ->AddEntry(array(
413 array("string"=>sprintf($linkopen,$menu["__BACK__"],".. [ "._("back")." ]")),
414 array("string"=>" ","attach"=>"style='border-right:0px;'")
415 ));
416 }
418 foreach($menu['__CATEGORY__'] as $path => $name){
419 $div2 ->AddEntry(array(array("string"=>sprintf($linkopen,$path,$name)),array("string"=>" ","attach"=>"style='border-right:0px;'")));
420 }
422 /* Append entries */
424 $separator ="<hr height=1 size=1></hr>";
426 $upudown = "<a href='?plug=".$_GET['plug']."&act=one_up&id=%s'> <img src='images/sort_up.png' border=0></a>".
427 " <a href='?plug=".$_GET['plug']."&act=one_down&id=%s'> <img src='images/sort_down.png' border=0></a>".
428 " <input type='image' src='images/edittrash.png' name='DelApp_%s' value='%s'>";
429 $edit= " <input type='image' src='images/edit.png' name='EdiApp_%s' value='%s'>";
431 foreach($menu["__ENTRY__"] as $path => $entry){
433 if(preg_match("/__SEPARATOR__/",$path)){
434 $div2 ->AddEntry(array(array("string"=>$separator),
435 array("string"=>preg_replace("/\%s/",$entry['name'],$upudown),"attach"=>"style='border-right:0px;'")));
437 }else{
438 $div2 ->AddEntry(array(array("string"=>sprintf($app,$entry['name'])),
439 array("string"=>preg_replace("/\%s/",$entry['name'],$upudown.$edit),"attach"=>"style='border-right:0px;'")));
440 }
441 }
443 $smarty->assign("UsedApps", $div2->DrawList());
444 $smarty->assign("List", $div->DrawList());
445 $smarty->assign("apps", $apps);
447 /* Show main page */
448 if ($this->dialog){
449 $smarty->assign("table", $this->table);
450 $display.= $smarty->fetch (get_template_path('application_options.tpl', TRUE));
451 } else {
452 $display.= $smarty->fetch (get_template_path('application.tpl', TRUE));
453 }
454 return ($display);
455 }
458 function remove_from_parent()
459 {
460 plugin::remove_from_parent();
462 $this->attrs["gosaMemberApplication"]= array();
464 $ldap= $this->config->get_ldap_link();
465 $ldap->cd($this->dn);
466 $ldap->modify($this->attrs);
467 show_ldap_error($ldap->get_error());
469 /* Optionally execute a command after we're done */
470 $this->handle_post_events("remove");
471 }
474 /* Save data to object */
475 # function save_object()
476 # {
477 # plugin::save_object();
478 # }
481 /* Save to LDAP */
482 function save()
483 {
484 plugin::save();
486 /* Copy members */
488 $cats = array(""=>0);
489 foreach($this->gosaMemberApplication as $mem => $entries){
490 $cats[$mem]= 0;
491 }
493 $this->attrs["gosaMemberApplication"]= array();
494 foreach ($this->gosaMemberApplication as $cat){
495 foreach($cat as $val){
497 $cats[$val['Cat']] ++;
498 if(!preg_match("/__SEPARATOR__/i",$val['App'])){
499 $this->attrs["gosaMemberApplication"][]= $val['App']."|".$val['Cat']."|".$cats[$val['Cat']];
500 }
501 }
502 }
504 /* Are there application parameters to be saved */
505 $this->attrs['gosaApplicationParameter']= array();
506 foreach($this->appoption as $name => $value){
507 if ($value != ""){
508 $this->attrs['gosaApplicationParameter'][]= "$name:$value";
509 }
510 }
512 /* Write back to LDAP */
513 $ldap= $this->config->get_ldap_link();
514 $ldap->cd($this->dn);
515 $ldap->modify($this->attrs);
516 show_ldap_error($ldap->get_error());
518 /* Optionally execute a command after we're done */
519 if ($this->initially_was_account == $this->is_account){
520 if ($this->is_modified){
521 $this->handle_post_events("mofify");
522 }
523 } else {
524 $this->handle_post_events("add");
525 }
527 }
529 function check()
530 {
531 $message= array();
532 return ($message);
533 }
536 function reload()
537 {
538 /* Generate applist */
539 $this->apps= array();
540 $ldap= $this->config->get_ldap_link();
541 $ldap->cd ("ou=apps,".$this->curbase);
543 $ldap->ls ("(objectClass=gosaApplication)","ou=apps,".$this->curbase);
544 while ($attrs= $ldap->fetch()){
545 if (isset($attrs["description"][0])){
546 $this->apps[$attrs["cn"][0]]=
547 $attrs["cn"][0]." (".
548 $attrs["description"][0].")";
549 } else {
550 $this->apps[$attrs["cn"][0]]=
551 $attrs["cn"][0];
552 }
553 }
554 natcasesort ($this->apps);
555 reset ($this->apps);
557 if(is_array($this->gosaMemberApplication))
558 foreach ($this->gosaMemberApplication as $cat){
559 if(is_array($cat))
560 foreach($cat as $entry){
561 $this->used_apps[$entry['App']]= $entry['App'];
562 }
563 }
564 }
567 function CreateCatMenu()
568 {
569 /* The current category */
570 $na = $this->curCatDir;
572 /* the current folder depth */
573 $nd = $this->curCatDepth;
575 /* init array */
576 $return = array();
578 $return["__CATEGORY__"] = array(); // Categories
579 $return["__ENTRY__"] = array(); // Entries in this category
580 $return["__BACK__"] = false; // The back entry
582 $tmp = new xmlParse();
584 $this->Categories= array();
585 if(!isset($this->config->data['MAIN']['KDE_APPLICATIONS_MENU'])) {
586 print_red(_("There is no value for 'KDE_APPLICATIONS_MENU' specified in your gosa.conf."));
587 }else{
588 $path = $this->config->data['MAIN']['KDE_APPLICATIONS_MENU'];
589 if(file_exists($path)){
590 if(is_readable($path)){
591 $tmp->parseMenu($path);
592 $this->Categories = $tmp->GetData();
593 }else{
594 print_red(sprintf(_("The specified file '%s' for 'KDE_APPLICATIONS_MENU' in your gosa.conf is not accessable."),$path));
595 }
596 }else{
597 print_red(sprintf(_("The specified file '%s' for 'KDE_APPLICATIONS_MENU' in your gosa.conf is not accessable."),$path));
598 }
599 }
602 /* Create new categories to return */
603 foreach($this->Categories as $key => $val){
605 /* Split categories by | to get name and priority */
606 $nk = split("\/",$key);
608 /* Check if this category must be shown */
609 if((isset($nk[$nd]))&&(is_array($nk))&&($nk[$nd] == $na)){
611 /* Add this to the list, if theres is a sub category */
612 if(isset($nk[($nd+1)])){
613 $return["__CATEGORY__"][$nk[($nd+1)]] = $nk[($nd+1)];
614 }
616 /* Create back entry */
617 if($nd !=0 ){
618 $return['__BACK__'] = $nk[($nd-1)];
619 }else{
620 $return['__BACK__'] = "..";
621 }
623 }elseif($na==""){
624 $return["__CATEGORY__"][$nk[0]] = $nk[0];
625 }
629 }
631 /* If back entry isn't set, set it to false (none) or to ..(base)*/
632 if($return["__BACK__"] == false){
633 $return['__BACK__'] = "..";
634 if($nd == 0 ){
635 $return['__BACK__'] = false;
636 }
637 }
638 if((isset($this->gosaMemberApplication[$this->curCatDir]))&&(is_array($this->gosaMemberApplication[$this->curCatDir]))){
639 foreach($this->gosaMemberApplication[$this->curCatDir] as $app){
640 if($app['Cat'] == $this->curCatDir){
641 $return["__ENTRY__"][$app['App']] = array("name"=>$app['App']) ;
642 }
643 }
644 }
645 /* Return this all ..*/
646 return($return);
647 }
649 function addApp($cn)
650 {
652 if((isset($this->gosaMemberApplication[$this->curCatDir]))&&(is_array($this->gosaMemberApplication[$this->curCatDir]))){
653 foreach($this->gosaMemberApplication[$this->curCatDir] as $entry){
654 if($entry['App'] == $cn) return;
655 }
656 }
657 $this->gosaMemberApplication[$this->curCatDir][]= array("App"=>$cn,"Cat"=>$this->curCatDir);
658 $this->used_apps[$cn]=$cn;
659 $this->is_modified= TRUE;
660 }
662 function removeApp($cn)
663 {
664 $temp= array();
665 foreach ($this->gosaMemberApplication as $value){
666 if ($value != $cn){
667 $temp[]= $value;
668 }
669 }
670 $this->gosaMemberApplication= $temp;
671 $this->is_modified= TRUE;
672 }
674 }
676 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
677 ?>