1 <?php
3 define ("SEPERATOR", 1002);
5 class MultiSelectWindow{
7 var $bool_DisplayAlpahabet = false;
8 var $bool_DisplayCloseButton= true;
9 var $bool_DisplaySaveButton = true;
11 var $SaveButtonString = "";
12 var $CloseButtonString = "";
14 var $string_Title = "";
15 var $string_ListHeader = "";
16 var $string_Summary = "";
17 var $string_Information = "";
19 var $array_Header = array();
20 var $array_Elements = array();
21 var $array_Checkboxes = array();
22 var $array_Regexes = array();
24 var $config = array("");
25 var $is_closed = false;
26 var $is_saved = false;
27 var $is_headpage = false; // if true the design changes
28 var $filterName = "Liste";
29 var $DepartmentsAdded = false;
30 var $Added_Departments = array();
31 var $selectedBase = "";
33 var $DivHeight = "";
35 var $HideFilterPart = false;
36 var $List_Bottom_Info = "";
37 var $SaveAdditionalVars = array(); // Additional Post vars to store
39 function ClearElementsList()
40 {
41 $this->array_Elements = array();
42 }
44 function HideFilterPart($bool = true)
45 {
46 $this->HideFilterPart = $bool;
47 }
49 function SetHeight($height)
50 {
51 $this->DivHeight=$height;
52 }
54 /* Adds a regex input field to the current dialog */
55 function AddRegex($name,$string,$value,$conn,$image="images/search.png")
56 {
57 $arr = array();
59 /* Check if the given input field name was already used
60 for this type of dialog */
61 if(isset($_SESSION['MultiDialogFilters'][$this->filterName][$name])){
62 $arr['value'] = $_SESSION['MultiDialogFilters'][$this->filterName][$name];
63 $this->$name = $arr['value'];
64 }else{
65 $arr['value'] = $value;
66 }
68 $arr['name'] = $name;
69 $arr['string'] = $string;
70 $arr['image'] = $image;
71 $arr['connAlpha'] = $conn; // Connect with alphabet select
72 $this->array_Regexes[] = $arr;
73 }
75 /* Contrucktion */
76 function MultiSelectWindow($config,$filterName)
77 {
78 $this->config = $config;
79 $this->SaveButtonString = _("Save");
80 $this->CloseButtonString = _("Close");
81 $this->filterName = $filterName;
83 /* Set default base, to users base */
84 $this->selectedBase = $_SESSION['CurrentMainBase'];
86 /* Check default values for SaveAdditionalVars */
87 foreach($this->SaveAdditionalVars as $name){
88 if(isset($_SESSION['MultiDialogFilters'][$this->filterName][$name])){
89 $this->$name = $_SESSION['MultiDialogFilters'][$this->filterName][$name];
90 }
91 }
93 }
95 /* Enables the headpage mode, which changes the list look */
96 function SetHeadpageMode()
97 {
98 $this->is_headpage = true;
99 }
101 /* Sets the List internal name (not displayed anywhere)
102 it is used to identify every single list
103 */
104 function SetTitle($str)
105 {
106 $this->string_Title = $str;
107 }
109 /* Set the list header string */
110 function SetListHeader($str)
111 {
112 $this->string_ListHeader = $str;
113 }
115 /* This sets the list description which is the first gray bar on top of the list */
116 function SetSummary($str)
117 {
118 $this->string_Summary = $str;
119 }
121 /* If the save button is enabled, you can change its caption with this function */
122 function SetSaveButtonString($str)
123 {
124 $this->SaveButtonString = $str;
125 }
127 /* If the close button is enabled, you can change its caption with this function */
128 function SetCloseButtonString($str)
129 {
130 $this->CloseButtonString = $str;
131 }
133 /* With this function you can change the text of the information box */
134 function SetInformation($str)
135 {
136 $this->string_Information = $str;
137 }
139 /* Display the alphabet selection box*/
140 function EnableAplhabet($bool)
141 {
142 $this->bool_DisplayAlpahabet = $bool;
143 }
145 /* Add additional header col */
146 function AddHeader($arr)
147 {
148 $this->array_Header[] = $arr;
149 }
151 /* add additional List element */
152 function AddElement($arr)
153 {
154 $this->array_Elements[] = $arr;
155 }
158 function get_default_header($navigation= TRUE)
159 {
160 $enable_back = $navigation;
161 $enable_root = $navigation;
162 $enable_home = $navigation;
164 $ui = get_userinfo();
166 /* Check if selectedBase = first available base */
167 $deps = array();
168 foreach($this->config->departments as $dep){
169 $deps[] = $dep;
170 }
173 if(!count($deps) || $deps[0] == $this->selectedBase){
174 $enable_back = FALSE;
175 $enable_root = FALSE;
176 }
178 /* Check if we are in users home department */
179 if(!count($deps) ||$this->selectedBase == get_base_from_people($ui->dn)){
180 $enable_home = FALSE;
181 }
183 /* Create header with selected base */
184 $listhead = "<div style='background:#F0F0F9;padding:5px;'>";
186 /* Draw root button */
187 if($enable_root){
188 $listhead .= " <input class='center' type='image' src='images/list_root.png' align='middle'
189 title='"._("Go to root department")."' name='dep_root' alt='"._("Root")."'> ";
190 }else{
191 $listhead .= " <img src='images/list_root_gray.png' class='center' alt='"._("Root")."'> ";
192 }
194 /* Draw back button */
195 if($enable_back){
196 $listhead .= " <input class='center' type='image' align='middle' src='images/list_back.png'
197 title='"._("Go up one department")."' alt='"._("Up")."' name='dep_back'> ";
198 }else{
199 $listhead .= " <img src='images/list_back_gray.png' class='center' alt='"._("Up")."'> ";
200 }
202 /* Draw home button */
203 if($enable_home){
204 $listhead .= " <input class='center' type='image' align='middle' src='images/list_home.png'
205 title='"._("Go to users department")."' alt='"._("Home")."' name='dep_home'> ";
206 }else{
207 $listhead .= " <img src='images/list_home_gray.png' class='center' alt='"._("Home")."'> ";
208 }
210 /* And at least draw reload button, this button is enabled everytime */
211 $listhead .= " <input class='center' type='image' src='images/list_reload.png' align='middle'
212 title='"._("Reload list")."' name='submit_department' alt='"._("Submit")."'> ".
213 " <img class='center' src='images/list_seperator.png' align='middle' alt='-' height='16' width='1'> ";
215 return ($listhead);
216 }
219 /* Add a checkbox to the filter element,
220 the name specifies an existing class var to store the 'selection' */
221 function AddCheckBox($name,$value="Unset",$string="Unset",$default=false)
222 {
223 $arr = array();
225 if($name == SEPERATOR){
226 $arr['name'] = SEPERATOR;
227 }else{
228 /* Check if there was already a variable
229 for this dialog which we should use instead of the default*/
230 if(isset($_SESSION['MultiDialogFilters'][$this->filterName][$name])){
231 $arr['default'] = $_SESSION['MultiDialogFilters'][$this->filterName][$name];
232 $this->$name = $arr['default'];
233 }else{
234 $arr['default'] = $default;
235 $this->$name = $default;
236 }
237 $arr['name'] = $name;
238 $arr['string'] = $string;
239 $arr['value'] = $value;
240 $arr['enabled'] = true;
242 }
243 $this->array_Checkboxes[] = $arr;
244 }
247 /* Hides or unhides the checkbox with the given name */
248 function DisableCheckBox($name,$HideShow = false)
249 {
250 foreach($this->array_Checkboxes as $key => $chkbox){
251 if($chkbox['name'] == $name){
252 $this->array_Checkboxes[$key]['enabled'] = $HideShow;
253 }
254 }
255 }
258 /* Returns true if the close button was pressed */
259 function isClosed()
260 {
261 return($this->is_closed);
262 }
264 /* Enable the close button */
265 function EnableCloseButton($bool)
266 {
267 $this->bool_DisplayCloseButton = $bool;
268 }
270 /* Enable the save button on the bottom of the list*/
271 function EnableSaveButton ($bool)
272 {
273 $this->bool_DisplaySaveButton = $bool;
274 }
276 /* Add a list specific filter object to position
277 1 on top of Information
278 2 Between Information && Filter
279 3 Below the Filter Part */
280 function AddUserBoxToFilter($position)
281 {
282 return("");
283 }
285 /* Draw the list with all list elements and filters */
286 function Draw()
287 {
289 /* Check for exeeded sizelimit */
290 if (($message= check_sizelimit()) != ""){
291 return($message);
292 }
294 $smarty = get_smarty();
295 $smarty->assign("UserBox1",$this->AddUserBoxToFilter(1));
296 $smarty->assign("UserBox2",$this->AddUserBoxToFilter(2));
297 $smarty->assign("UserBox3",$this->AddUserBoxToFilter(3));
299 $divlist = new divlist($this->string_Title);
300 $divlist->SetSummary($this->string_Summary);
301 $divlist->SetEntriesPerPage(0); // 0 for scrollable list
302 $divlist->SetFooter($this->get_List_Bottom_Info());
304 if($this->DivHeight != ""){
305 $divlist->SetHeight($this->DivHeight);
306 }
308 /* set Header informations
309 */
310 $header = array();
311 foreach($this->array_Header as $head){
312 $header[] = $head;
313 }
314 $divlist->SetHeader($header);
316 /* set Entries
317 */
318 $elements = array();
319 foreach($this->array_Elements as $element){
320 $divlist->AddEntry($element);
321 }
323 /* Create checkboxes fields
324 */
325 $boxes = "";
326 $boxClick = " onClick='document.mainform.submit();' ";
327 foreach($this->array_Checkboxes as $box){
329 if($box['name'] == SEPERATOR){
330 $boxes .= "</td></tr></table><table style='width:100%;border-top:1px solid #B0B0B0;'><tr><td>";
331 continue;
332 }
334 /* Skip disabled boxes */
335 if(!$box['enabled']) continue;
337 /* Check if box is checked */
338 if($box['default'] == true){
339 $boxes .="<input type='checkbox' name='".$box['name']."' value='1' title='".$box['value']."' checked ".$boxClick."> ".$box['string']."<br>";
340 }else{
341 $boxes .="<input type='checkbox' name='".$box['name']."' value='1' title='".$box['value']."'".$boxClick."> ".$box['string']."<br>";
342 }
343 }
344 $smarty->assign("CheckBoxes", $boxes);
346 /* Assign regex fields
347 */
348 $regexes = "";
349 foreach($this->array_Regexes as $regex){
350 $regexes.="<table summary=\"\" style=\"width:100%;border-top:1px solid #B0B0B0;\">
351 <tr>
352 <td>
353 <label for=\"".$regex['name']."\">
354 <img alt=\"".$regex['string']."\" src=\"".$regex['image']."\" align=middle>
355 </label>
356 </td>
357 <td width=\"99%\">
358 <input id='".$regex['name']."' type='text' style='width:99%' name='".$regex['name']."' maxlength='20'
359 value='".$regex['value']."' title=\"".$regex['string']."\">
360 </td>
361 </tr>
362 </table>";
363 }
364 $smarty->assign("regexes" , $regexes );
366 /* Hide Filter Part if Requested or empty */
367 if((empty($boxes)) && (empty($regexes)) || ($this->HideFilterPart)){
368 $smarty->assign("Skip_Filter_Part", true);
369 }else{
370 $smarty->assign("Skip_Filter_Part", false);
371 }
373 /* Assign alphabet and display it
374 */
375 $smarty->assign("Display_alphabet", $this->bool_DisplayAlpahabet);
376 $smarty->assign("alphabet", generate_alphabet());
377 $smarty->assign("Header" , $this->string_ListHeader );
378 $smarty->assign("Summary" , $this->string_Summary);
379 $smarty->assign("Title" , $this->string_Title);
380 $smarty->assign("Information" , $this->string_Information);
382 /* Check for exeeded sizelimit */
383 $smarty->assign("hint" , print_sizelimit_warning());
384 $smarty->assign("DivList" , $divlist->DrawList());
387 if($this->is_headpage){
388 $smarty->assign("tree_image", get_template_path('images/tree.png'));
389 $smarty->assign("infoimage", get_template_path('images/info.png'));
390 $smarty->assign("launchimage", get_template_path('images/launch.png'));
391 $smarty->assign("apply", apply_filter());
392 }else{
393 $smarty->assign("tree_image", get_template_path('images/tree.png'));
394 $smarty->assign("infoimage", get_template_path('images/info_small.png'));
395 $smarty->assign("launchimage", get_template_path('images/rocket.png'));
396 $smarty->assign("apply", apply_filter());
397 }
399 /* Button handling */
400 $smarty->assign("SaveButtonString" ,$this->SaveButtonString);
401 $smarty->assign("CloseButtonString",$this->CloseButtonString);
403 $smarty->assign("Display_Close", $this->bool_DisplayCloseButton);
404 $smarty->assign("Display_Save" , $this->bool_DisplaySaveButton);
406 $smarty->assign("filterName" , $this->filterName);
407 $smarty->assign("is_headpage" , $this->is_headpage);
409 $display = $smarty->fetch(get_template_path("MultiSelectWindow.tpl"));
410 return($display);
411 }
413 /* Set the close var, which simulates the close button is pressed */
414 function Close()
415 {
416 $this->is_closed = true;
417 }
419 function Save()
420 {
421 $this->is_saved = true;
422 }
424 /* Store all checkboxes/ regexes ...
425 Store data also into a session var, to keep the checkboxes check after reload */
426 function save_object()
427 {
428 /* Save base selection from headpage selectbox*/
429 if(isset($_POST['CurrentMainBase'])){
430 $this->selectedBase = $_POST['CurrentMainBase'];
431 }
433 /* Update current base */
434 $s_action ="";
435 foreach($_POST as $key => $value){
436 if(preg_match("/^dep_back.*/i",$key)){
437 $s_action="back";
438 }elseif(preg_match("/^dep_root.*/",$key)){
439 $s_action="root";
440 }elseif(preg_match("/^dep_home.*/i",$key)){
441 $s_action="home";
442 }
443 }
445 /* Homebutton is posted */
446 if($s_action=="home"){
447 $ui= get_userinfo();
448 $base = get_base_from_people($ui->dn);
449 $this->selectedBase= $base;
450 }
452 /* Open selected department
453 this is posted by the parent class MultiSelectWindow */
454 if(isset($_GET['act'])&& ($_GET['act'] == "dep_open")){
455 $s_entry = base64_decode($_GET['dep_id']);
456 if (!isset($this->config->departments[$s_entry])){
457 print_red(_("Error: The requested subtree has an inconsistent DN encoding, check your LDAP!"));
458 } else {
459 $this->selectedBase = $this->config->departments[$s_entry];
460 }
461 }
463 /* back to the roots ^^ */
464 if($s_action=="root"){
465 $this->selectedBase=($this->config->current['BASE']);
466 }
469 /* If Back-button is pressed, move back one step in DN */
470 if($s_action=="back"){
471 //FIXME: This is not 100% correct. We'll only display ou's, but there may be
472 // a step between. You'll stumble in a "hidden" department in this case.
473 $base_back= preg_replace("/^[^,]+,/", "", $_SESSION['CurrentMainBase']);
475 /* The department array keeps non DN entries as index. We need to convert
476 it before checking the existance. */
477 $base_back= trim(convert_department_dn($base_back));
479 /* Check if the department exists, otherwise revert to the configure base DN */
480 if(isset($this->config->departments[$base_back])){
481 $this->selectedBase= $this->config->departments[$base_back];
482 }else{
483 $this->selectedBase= $this->config->departments['/'];
484 }
485 }
486 $_SESSION['CurrentMainBase'] = $this->selectedBase;
488 if(isset($_POST['MultiSelectWindow'.$this->filterName])){
490 /* Save some additional vars */
491 foreach($this->SaveAdditionalVars as $name){
492 if(isset($_POST[$name])){
493 if(isset($this->$name)){
494 $this->$name = $_POST[$name];
495 $_SESSION['MultiDialogFilters'][$this->filterName][$name] = $_POST[$name];
496 }
497 }
498 }
500 /* Check posts from checkboxes
501 */
502 foreach($this->array_Checkboxes as $key => $box){
503 if(isset($_POST[$box['name']])){
504 $this->array_Checkboxes[$key]['default'] = true;
505 $this->$box['name'] = true;
506 }else{
507 $this->array_Checkboxes[$key]['default'] = false;
508 $this->$box['name'] = false;
509 }
510 /* Save settings in out session */
511 $_SESSION['MultiDialogFilters'][$this->filterName][$box['name']] = $this->$box['name'];
512 }
514 /* Check regex posts */
515 foreach($this->array_Regexes as $key => $box){
516 $this->array_Regexes[$key]['value'] = $_POST[$box['name']];
517 $this->$box['name'] = $_POST[$box['name']];
518 $_SESSION['MultiDialogFilters'][$this->filterName][$box['name']] = $this->$box['name'];
519 }
521 /* call close/save if buttons are pressed */
522 if(isset($_POST['CloseMultiSelectWindow'])){
523 $this->Close();
524 }
526 if(isset($_POST['SaveMultiSelectWindow'])){
527 $this->Save();
528 }
529 }
531 /* check for alphabet selection
532 Check which regexes are connected to the alphabet
533 */
534 if(isset($_GET['search'])){
535 foreach($this->array_Regexes as $key => $box){
536 /* check if this regex is connected to the alphabet selection */
537 if(($box['connAlpha'])&&(isset($_GET['search']))){
538 $val = $_GET['search']."*";
539 $val = preg_replace("/\*\**/","*",$val);
540 $this->array_Regexes[$key]['value'] = $val;
541 $this->$box['name'] = $val;
542 $_SESSION['MultiDialogFilters'][$this->filterName][$box['name']] = $val;
543 }
544 }
545 }
547 }
549 /* this function adds the sub-departments of the current tree to the list */
550 function AddDepartments($base = false,$numtabs = 3)
551 {
552 $this->DepartmentsAdded = true;
553 $this->Added_Departments = array();
555 /* check for a valid base */
556 if(!$base){
557 if(!isset($_SESSION['CurrentMainBase'])){
558 $_SESSION['CurrentMainBase'] = $this->config->current['BASE'];
559 }
560 $base = $_SESSION['CurrentMainBase'];
561 }
563 /* Create ldap obj and switch into base*/
564 $ldap = $this->config->get_ldap_link();
565 $ldap->cd($base);
567 /* reset current deps */
568 $this->departments = array();
570 /* Get all departments within this subtree */
571 $deps= get_list("(&(|(ou=*)(description=*))(objectClass=gosaDepartment))", $this->ui->subtreeACL,
572 $base, array("ou", "description"), GL_SIZELIMIT | GL_CONVERT);
574 /* Edit delete link for system types
575 */
576 $linkopen = "<a href='?plug=".$_GET['plug']."&act=dep_open&dep_id=%s'>%s</a>";
578 /* Create an array with all visible (in the list) departments */
579 $departments = array();
580 foreach($deps as $value){
581 if(isset($value['description'][0])){
582 $this->departments[$value['dn']]= get_sub_department($value['dn'])." - [".$value["description"][0]."]";
583 }else{
584 $this->departments[$value['dn']]= get_sub_department($value['dn']);
585 }
586 }
587 natcasesort($this->departments);
589 /* Add deps to this dialog object list */
590 foreach($this->departments as $key=> $val){
591 /* Add missing entries ... */
592 if(!isset($this->config->departments[trim($key)])){
593 $this->config->departments[trim($key)]="";
594 }
596 /* check if this department contains sub-departments
597 Display different image in this case
598 */
599 $non_empty="";
600 $nkey= normalizePreg($key);
601 foreach($this->config->departments as $keyd=>$vald ){
602 if(preg_match("/$nkey\/.*/",$keyd)){
603 $non_empty="full";
604 }
605 }
607 /* Add to divlist */
608 $row = array();
609 $row[]=$field1=array("string"=>"<img src='images/".$non_empty."folder.png' alt='department'>","attach"=>"style='text-align:center;width:20px;'");
610 $row[]=$field2=array("string"=>sprintf($linkopen,base64_encode($key),$val), "attach" => "style=''");
612 if($numtabs > 2){
613 for($i = 2 ; $i <$numtabs;$i++){
614 if(isset( $this->array_Header[$i]['attach'])){
615 $row[] = array("string"=>" ","attach" => $this->array_Header[$i]['attach']);
616 }else{
617 $row[] = array("string"=>" ");
618 }
619 }
620 }
622 $this->AddElement($row);
623 $this->Added_Departments[] = $row;
624 }
625 }
627 function set_List_Bottom_Info($str)
628 {
629 $this->List_Bottom_Info = $str;
630 }
632 function get_List_Bottom_Info()
633 {
634 return($this->List_Bottom_Info);
635 }
636 }
637 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
638 ?>