f11234051cd997648294d38edb8593b8517a15a5
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 $departments = ""; // All departments within $this->curbase;
14 var $apps = array(); // All available applications
15 var $used_apps = array(); // Specifies which applications are currently assigned
17 var $option_name = array();
18 var $option_value = array();
19 var $appoption = array();
21 var $table = "";
22 var $curbase = "";
23 var $curCatDir ;
24 var $curCatDepth =0; //
26 /* attribute list for save action */
27 var $attributes= array();
28 var $objectclasses= array("gosaApplicationGroup");
30 function appgroup ($config, $dn= NULL)
31 {
32 plugin::plugin ($config, $dn);
34 /* Load member applications */
35 if (isset ($this->attrs["gosaMemberApplication"][0])){
36 for ($i= 0; $i<$this->attrs["gosaMemberApplication"]["count"]; $i++){
37 $this->gosaMemberApplication[]=
38 $this->attrs["gosaMemberApplication"][$i];
39 }
40 }
42 /* Load application options */
43 if (isset($this->attrs['gosaApplicationParameter'])){
44 for ($i= 0; $i<$this->attrs['gosaApplicationParameter']['count']; $i++){
45 $option= preg_replace('/^[^:]+:/', '',
46 $this->attrs['gosaApplicationParameter'][$i]);
47 $name= preg_replace('/:.*$/', '',
48 $this->attrs['gosaApplicationParameter'][$i]);
49 $this->appoption[$name]= $option;
50 }
51 }
53 /* Parse MemberApplication*/
54 $tmp = array();
55 $tmp2 = array();
56 $prios = array();
57 $cats = array();
59 foreach($this->gosaMemberApplication as $memberApp){
60 if(preg_match("/\|/i",$memberApp)){
62 $tmp = split("\|",$memberApp);
64 if(!empty($tmp[0])){
65 $tmp2[$tmp[1]][$tmp[2]] = array("App"=>$tmp[0]);
66 }
67 if(!empty($tmp[1])){
68 $cats [$tmp[1]] = $tmp[1];
69 }
70 $prios[$tmp[1]][$tmp[2]] = $tmp[2];
71 }else{
72 $tmp2[]['App'] = $memberApp;
73 }
74 }
76 $this->Categories = $cats;
78 $this->gosaMemberApplication = $tmp2;
79 $cats[""]="";
80 foreach($cats as $cat ){
81 if((isset($prios[$cat]))&&(count($prios[$cat]))){
82 $max = max($prios[$cat]);
83 $min = 1;//min($prios[$cat]);
84 $last = false;
85 for($i = $min ; $i < $max ; $i++){
86 if(!isset($prios[$cat][$i])){
87 if($last == false){
88 $this->gosaMemberApplication[$cat][$i]['App'] = "__SEPARATOR__".$i;
90 $last = true;
91 }
92 }else{
93 $last = false;
94 }
95 }
96 }
97 }
99 $tmp = array();
100 foreach($this->gosaMemberApplication as $key => $entries){
101 ksort ($entries);
102 foreach($entries as $entry){
103 $tmp[$key][]= $entry;
104 }
105 }
106 $this->gosaMemberApplication = $tmp;
107 $this->curbase = $this->config->current['BASE'];
108 }
110 function getOneUp($appl)
111 {
112 $cat = $this->curCatDir;
113 if(!isset($this->gosaMemberApplication[$cat][($appl-1)])) return;
114 $f = 1;
115 if($appl == 1 ){
116 $f = 0;
117 }
118 $first = array_slice($this->gosaMemberApplication[$cat],0,($appl-1));
119 $middle = array_slice($this->gosaMemberApplication[$cat],$appl-1,(($appl+1)-$f));
120 $last = array_slice($this->gosaMemberApplication[$cat],($appl+1));
121 $tmp = array();
122 foreach($first as $entry){
123 $tmp[] = $entry;
124 }
125 foreach(array_reverse($middle) as $entry){
126 $tmp[] = $entry;
127 }
128 foreach($last as $entry){
129 $tmp[] = $entry;
130 }
131 $this->gosaMemberApplication[$cat] =$tmp;
132 }
134 function getOneDown($appl){
135 $cat = $this->curCatDir;
136 if(!isset($this->gosaMemberApplication[$cat][($appl+1)])) return;
137 $f = 0;
138 if($appl == 0 ){
139 $f = 1;
140 }
141 $first = array_slice($this->gosaMemberApplication[$cat],0,($appl));
142 $middle = array_slice($this->gosaMemberApplication[$cat],$appl,(($appl+1+$f)));
143 $last = array_slice($this->gosaMemberApplication[$cat],($appl+2));
145 $tmp = array();
146 foreach($first as $entry){
147 $tmp[] = $entry;
148 }
149 foreach(array_reverse($middle) as $entry){
150 $tmp[] = $entry;
151 }
152 foreach($last as $entry){
153 $tmp[] = $entry;
154 }
155 $this->gosaMemberApplication[$cat] =$tmp;
156 }
158 function AddSeperator()
159 {
160 $cat = $this->curCatDir;
161 $cnt = count($this->gosaMemberApplication[$cat]);
162 $tmp = array("App" => "__SEPARATOR__".($cnt+1));
163 $this->gosaMemberApplication[$cat][] = $tmp;
164 }
166 function execute()
167 {
168 /* Call parent execute */
169 plugin::execute();
171 if((isset($_GET['act']))&&($_GET['act']=="depopen")){
172 $dep = base64_decode($_GET['depid']);
173 if(isset($this->config->idepartments[$dep])){
174 $this->curbase = $dep;
175 }
176 }
178 if(isset($_POST['AddSeperator'])){
179 $this->AddSeperator();
180 }
182 if((isset($_GET['act']))&&($_GET['act']=="open")){
183 $this->curCatDir = $_GET['id'];
184 }
186 /* Do we need to flip is_account state? */
187 if (isset($_POST['modify_state'])){
188 $this->is_account= !$this->is_account;
189 }
191 /* Do we represent a valid group? */
192 if (!$this->is_account && $this->parent == NULL){
193 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
194 _("This 'dn' is no appgroup.")."</b>";
195 return ($display);
196 }
198 /* Show tab dialog headers */
199 $display= "";
200 if ($this->parent != NULL){
201 if ($this->is_account){
202 $display= $this->show_header(_("Remove applications"),
203 _("This group has application features enabled. You can disable them by clicking below."));
204 } else {
205 $display.= $this->show_header(_("Create applications"),
206 _("This group has application features disabled. You can enable them by clicking below."));
207 return ($display);
208 }
209 }
212 /* Add Categorie */
213 if((isset($_POST['AddCat']))&&(isset($_POST['CatName']))&&(!empty($_POST['CatName']))){
214 if(empty($this->curCatDir)){
215 $this->Categories[$_POST['CatName']]=$_POST['CatName'];
216 }else{
217 $this->Categories[$this->curCatDir."/".$_POST['CatName']]=$_POST['CatName'];
218 }
219 }
222 $this->reload();
223 foreach($_POST as $name => $value){
224 if(preg_match("/DelApp_/",$name)){
225 $app = $value;
227 foreach($this->gosaMemberApplication as $key => $cat){
228 foreach($cat as $key2 => $app){
229 if($app['App'] == $value){
230 unset($this->gosaMemberApplication[$key][$key2]);
231 if(isset($this->used_apps[$value])){
232 unset($this->used_apps[$value]);
233 }
234 }
235 }
236 }
237 }
238 if(preg_match("/EdiApp_/",$name)){
239 $appname = $value;
240 /* We've got the appname, get parameters from ldap */
241 $ldap= $this->config->get_ldap_link();
242 $ldap->cd($this->config->current['BASE']);
243 $ldap->search("(&(objectClass=gosaApplication)(cn=$appname))");
244 if ($ldap->count() != 1){
245 print_red (_("The selected application name is not uniq. Please check your LDAP."));
246 } else {
247 $attrs= $ldap->fetch();
248 if(isset($attrs['gosaApplicationParameter'])){
249 $this->dialog= TRUE;
251 /* Fill name and value arrays */
252 for ($i= 0; $i<$attrs['gosaApplicationParameter']['count']; $i++){
253 $option= preg_replace('/^[^:]+:/', '',
254 $attrs['gosaApplicationParameter'][$i]);
255 $name= preg_replace('/:.*$/', '',
256 $attrs['gosaApplicationParameter'][$i]);
257 $this->option_name[$i]= $name;
259 /* Fill with values from application, default should be
260 loaded by the external scripts */
261 if (isset($this->appoption[$name])){
262 $this->option_value[$i]= $this->appoption[$name];
263 }
264 }
266 /* Create edit field */
267 $table= "<table summary=\"\">";
268 for ($i= 0; $i < count($this->option_name); $i++){
269 if (isset($this->option_value[$i])){
270 $value= $this->option_value[$i];
271 } else {
272 $value= "";
273 }
274 $table.="<tr><td>".$this->option_name[$i]."</td><td>".
275 "<input name=\"value$i\" size=60 maxlength=250 ".
276 "value=\"".$value."\"><br></td></tr>";
277 }
278 $table.= "</table>";
279 $this->table= $table;
280 } else {
281 print_red (_("The selected application has no options."));
282 }
283 }
284 }
285 }
286 $this->reload();
288 /* Add group with post */
289 if((isset($_GET['act']))&&($_GET['act']=="add")){
290 $this->used_apps[$_GET['id']]= $_GET['id'];
291 asort($this->used_apps);
292 $this->addApp($_GET['id']);
293 }
295 /* Add multiple */
296 if(isset($_POST['AddApps'])){
297 foreach($_POST as $name => $value){
298 if(preg_match("/AddApp_/",$name)){
299 $app = preg_replace("/AddApp_/","",$name);
300 $this->addApp($app);
301 }
302 }
303 }
306 /* Cancel edit options? */
307 if (isset($_POST['edit_options_cancel'])){
308 $this->dialog= FALSE;
309 }
311 /* Finish edit options? */
312 if (isset($_POST['edit_options_finish'])){
313 $this->dialog= FALSE;
315 /* Save informations passed by the user */
316 $this->option_value= array();
317 for ($i= 0; $i<count($this->option_name); $i++){
318 $this->appoption[$this->option_name[$i]]= $_POST["value$i"];
319 $this->is_modified= TRUE;
320 }
321 }
323 /* Prepare templating stuff */
324 $smarty= get_smarty();
325 $smarty->assign("used_apps", $this->used_apps);
326 $apps= array();
327 foreach ($this->apps as $key => $value){
328 if (!array_key_exists($key, $this->used_apps)){
329 $apps["$key"]= "$value";
330 }
331 }
333 $div = new DivSelectBox("appgroup");
335 $div->SetHeight(300);
337 /* NEW LIST MANAGMENT
338 * We also need to search for the departments
339 * So we are able to navigate like in konquerer
340 */
342 $ldap = $this->config->get_ldap_link();
343 $ldap->cd($this->curbase) ;
344 $ldap->ls("(objectClass=gosaDepartment)");
345 $departments= array();
346 $tmp = array();
347 while ($value = $ldap->fetch()){
348 $tmp[strtolower($value['dn']).$value['dn']]=$value;
349 }
350 ksort($tmp);
351 foreach($tmp as $value){
352 if($value["description"][0]!=".."){
353 $departments[$value['dn']]=convert_department_dn($value['dn'])." - [".$value["description"][0]."]";
354 }else{
355 $departments[$value['dn']]=convert_department_dn($value['dn']);
356 }
357 }
359 /* END NEW LIST MANAGMENT
360 */
362 $linkopen = "<a href='?plug=".$_GET['plug']."&act=depopen&depid=%s'>%s</a>";
363 $linkadd = "<a href='?plug=".$_GET['plug']."&act=add&id=%s'>%s</a>";
365 $base_back = preg_replace("/^[^,]+,/","",$this->curbase);
366 if((strlen($base_back)>= strlen($this->config->current['BASE']))&&($this->curbase!=$this->config->current['BASE'])){
367 $div->AddEntry(array(
368 array("string"=>sprintf($linkopen,base64_encode($base_back),".. ["._("back")."]"),
369 "attach"=>"style='border:0px;'")
370 ));
371 }
372 foreach($departments as $key => $app){
373 $div->AddEntry(array(
374 array("string"=>"<img src='images/folder.png' alt='"._("department")."'> ".sprintf($linkopen,base64_encode($key),$app),
375 "attach"=>"style='border:0px;'")
376 ));
377 }
379 foreach($apps as $key => $app){
380 $div->AddEntry(array(
381 array("string"=>sprintf("<input type='checkbox' value='1' name='AddApp_%s'>",$key).
382 "<img src='images/select_application.png' alt='"._("application")."'> ".sprintf($linkadd,$key,$app),
383 "attach"=>"style='border:0px;'")
384 ));
385 }
387 if((isset($_GET['act']))&&(($_GET['act'] == "one_up")||($_GET['act']=="one_down"))){
388 if(isset($_GET['id'])){
389 $id = $_GET['id'];
390 $act = $_GET['act'];
391 $found = -1;
392 if(isset($this->gosaMemberApplication[$this->curCatDir])){
393 foreach($this->gosaMemberApplication[$this->curCatDir] as $key => $member){
394 if($id == $member['App']){
395 $found = $key;
396 }
397 }
398 }
400 if($found != -1){
401 if($act == "one_up"){
402 $this->getOneUp($found);
403 }elseif($act == "one_down") {
404 $this->getOneDown($found);
405 }
406 }
407 }
408 }
410 $div2 = new DivSelectBox("appgroup");
411 $div2->SetHeight(300);
413 $linkopen = "<img src='images/folder.png'> <a href='?plug=".$_GET['plug']."&act=open&id=%s'>%s</a>";
414 $catremove = "<a href='?plug=".$_GET['plug']."&act=idelcat&id=%s'><input type='image' src='images/edittrash.png' ></a>";
415 $app = "<img src='images/select_application.png'> %s";
417 if(empty($this->curCatDir)){
418 $cnt =0;
419 }else{
420 $cnt = count(split("/",$this->curCatDir));
421 $tmp = split("/",$this->curCatDir);
422 $bbk = "";
423 for($i = 0 ; $i < ($cnt -1 ) ; $i++){
424 $bbk .= $tmp[$i];
425 }
426 $div2 ->AddEntry(array(array("string"=>sprintf($linkopen,$bbk,"..")),array("string"=>" ","attach"=>"style='border-right:0px;'")));
427 }
429 foreach($this->Categories as $path => $name){
430 $depth = count(split("/",$path));
431 if(($depth == ($cnt)+1)){
433 $div2 ->AddEntry(array(
434 array("string"=>sprintf($linkopen,$path,$name)),
435 array("string"=>sprintf($catremove,$path).
436 "<img src='images/empty.png' width='19px'>",
437 "attach"=>"align='right' style='width:80px;border-right:0px;'")));
438 }
439 }
441 /* Append entries */
443 $separator ="<hr height=1 size=1></hr>";
445 $upudown = "<a href='?plug=".$_GET['plug']."&act=one_up&id=%s'> <img src='images/sort_up.png' border=0></a>".
446 " <a href='?plug=".$_GET['plug']."&act=one_down&id=%s'> <img src='images/sort_down.png' border=0></a>".
447 " <input type='image' src='images/edittrash.png' name='DelApp_%s' value='%s'>";
448 $edit= " <input type='image' src='images/edit.png' name='EdiApp_%s' value='%s'>";
450 if(isset($this->gosaMemberApplication[$this->curCatDir])){
451 foreach($this->gosaMemberApplication[$this->curCatDir] as $cat => $entry){
452 if(preg_match("/__SEPARATOR__/",$entry['App'])){
453 $div2 ->AddEntry(array(array("string"=>$separator),
454 array("string"=>preg_replace("/\%s/",$entry['App'],$upudown."<img src='images/empty.png' width='19px'>"),"attach"=>"align='right' style='border-right:0px;'")));
455 }else{
456 $div2 ->AddEntry(array(array("string"=>sprintf($app,$entry['App'])),
457 array("string"=>preg_replace("/\%s/",$entry['App'],$upudown.$edit),"attach"=>"align='right' style='border-right:0px;'")));
458 }
459 }
460 }
462 $smarty->assign("UsedApps", $div2->DrawList());
463 $smarty->assign("List", $div->DrawList());
464 $smarty->assign("apps", $apps);
466 /* Show main page */
467 if ($this->dialog){
468 $smarty->assign("table", $this->table);
469 $display.= $smarty->fetch (get_template_path('application_options.tpl', TRUE));
470 } else {
471 $display.= $smarty->fetch (get_template_path('application.tpl', TRUE));
472 }
473 return ($display);
474 }
477 function remove_from_parent()
478 {
479 plugin::remove_from_parent();
481 $this->attrs["gosaMemberApplication"]= array();
483 $ldap= $this->config->get_ldap_link();
484 $ldap->cd($this->dn);
485 $ldap->modify($this->attrs);
486 show_ldap_error($ldap->get_error());
488 /* Optionally execute a command after we're done */
489 $this->handle_post_events("remove");
490 }
493 /* Save to LDAP */
494 function save()
495 {
496 plugin::save();
498 /* Copy members */
499 $this->Categories[""]="";
500 $this->attrs["gosaMemberApplication"]= array();
501 $cats = array();
502 foreach($this->Categories as $name => $cats){
503 $cats[$name] =0;
504 if(isset($this->gosaMemberApplication[$name])){
505 foreach($this->gosaMemberApplication[$name] as $entry){
506 if(!preg_match("/__SEPARATOR__/",$entry['App'])){
507 $this->attrs["gosaMemberApplication"][]= $entry['App']."|".$name."|".$cats[$name];
508 }
509 $cats[$name] = $cats[$name] + 1;
510 }
511 }
512 if(($cats[$name]==0)&&(!empty($name))){
513 $this->attrs["gosaMemberApplication"][]= "|".$name."|".$cats[$name];
514 }
515 }
517 /* Are there application parameters to be saved */
518 $this->attrs['gosaApplicationParameter']= array();
519 foreach($this->appoption as $name => $value){
520 if ($value != ""){
521 $this->attrs['gosaApplicationParameter'][]= "$name:$value";
522 }
523 }
525 /* Write back to LDAP */
526 $ldap= $this->config->get_ldap_link();
527 $ldap->cd($this->dn);
528 $ldap->modify($this->attrs);
529 show_ldap_error($ldap->get_error());
531 /* Optionally execute a command after we're done */
532 if ($this->initially_was_account == $this->is_account){
533 if ($this->is_modified){
534 $this->handle_post_events("mofify");
535 }
536 } else {
537 $this->handle_post_events("add");
538 }
540 }
542 function check()
543 {
544 $message= array();
545 return ($message);
546 }
549 function reload()
550 {
551 /* Generate applist */
552 $this->apps= array();
553 $ldap= $this->config->get_ldap_link();
554 $ldap->cd ("ou=apps,".$this->curbase);
556 $ldap->ls ("(objectClass=gosaApplication)","ou=apps,".$this->curbase);
557 while ($attrs= $ldap->fetch()){
558 if (isset($attrs["description"][0])){
559 $this->apps[$attrs["cn"][0]]=
560 $attrs["cn"][0]." (".
561 $attrs["description"][0].")";
562 } else {
563 $this->apps[$attrs["cn"][0]]=
564 $attrs["cn"][0];
565 }
566 }
567 natcasesort ($this->apps);
568 reset ($this->apps);
570 if(is_array($this->gosaMemberApplication))
571 foreach ($this->gosaMemberApplication as $cat){
572 if(is_array($cat))
573 foreach($cat as $entry){
574 $this->used_apps[$entry['App']]= $entry['App'];
575 }
576 }
577 }
580 function addApp($cn)
581 {
582 if((isset($this->gosaMemberApplication[$this->curCatDir]))&&(is_array($this->gosaMemberApplication[$this->curCatDir]))){
583 foreach($this->gosaMemberApplication[$this->curCatDir] as $entry){
584 if($entry['App'] == $cn) return;
585 }
586 }
587 $this->gosaMemberApplication[$this->curCatDir][]= array("App"=>$cn);
588 $this->used_apps[$cn]=$cn;
589 $this->is_modified= TRUE;
590 }
593 function removeApp($cn)
594 {
595 $temp= array();
596 foreach ($this->gosaMemberApplication as $value){
597 if ($value != $cn){
598 $temp[]= $value;
599 }
600 }
601 $this->gosaMemberApplication= $temp;
602 $this->is_modified= TRUE;
603 }
605 }
607 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
608 ?>