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
303 /* Display list footer with summary of all listed entries */
304 if(isset($this->config->data['MAIN']['LIST_SUMMARY']) && preg_match("/true/i",$this->config->data['MAIN']['LIST_SUMMARY'])){
305 $divlist->SetFooter($this->get_List_Bottom_Info());
306 }
308 if($this->DivHeight != ""){
309 $divlist->SetHeight($this->DivHeight);
310 }
312 /* set Header informations
313 */
314 $header = array();
315 foreach($this->array_Header as $head){
316 $header[] = $head;
317 }
318 $divlist->SetHeader($header);
320 /* set Entries
321 */
322 $elements = array();
323 foreach($this->array_Elements as $element){
324 $divlist->AddEntry($element);
325 }
327 /* Create checkboxes fields
328 */
329 $boxes = "";
330 $boxClick = " onClick='document.mainform.submit();' ";
331 foreach($this->array_Checkboxes as $box){
333 if($box['name'] == SEPERATOR){
334 $boxes .= "</td></tr></table><table style='width:100%;border-top:1px solid #B0B0B0;'><tr><td>";
335 continue;
336 }
338 /* Skip disabled boxes */
339 if(!$box['enabled']) continue;
341 /* Check if box is checked */
342 if($box['default'] == true){
343 $boxes .="<input type='checkbox' name='".$box['name']."' value='1' title='".$box['value']."' checked ".$boxClick."> ".$box['string']."<br>";
344 }else{
345 $boxes .="<input type='checkbox' name='".$box['name']."' value='1' title='".$box['value']."'".$boxClick."> ".$box['string']."<br>";
346 }
347 }
348 $smarty->assign("CheckBoxes", $boxes);
350 /* Assign regex fields
351 */
352 $regexes = "";
353 foreach($this->array_Regexes as $regex){
354 $regexes.="<table summary=\"\" style=\"width:100%;border-top:1px solid #B0B0B0;\">
355 <tr>
356 <td>
357 <label for=\"".$regex['name']."\">
358 <img alt=\"".$regex['string']."\" src=\"".$regex['image']."\" align=middle>
359 </label>
360 </td>
361 <td width=\"99%\">
362 <input id='".$regex['name']."' type='text' style='width:99%' name='".$regex['name']."' maxlength='20'
363 value='".$regex['value']."' title=\"".$regex['string']."\">
364 </td>
365 </tr>
366 </table>";
367 }
368 $smarty->assign("regexes" , $regexes );
370 /* Hide Filter Part if Requested or empty */
371 if((empty($boxes)) && (empty($regexes)) || ($this->HideFilterPart)){
372 $smarty->assign("Skip_Filter_Part", true);
373 }else{
374 $smarty->assign("Skip_Filter_Part", false);
375 }
377 /* Assign alphabet and display it
378 */
379 $smarty->assign("Display_alphabet", $this->bool_DisplayAlpahabet);
380 $smarty->assign("alphabet", generate_alphabet());
381 $smarty->assign("Header" , $this->string_ListHeader );
382 $smarty->assign("Summary" , $this->string_Summary);
383 $smarty->assign("Title" , $this->string_Title);
384 $smarty->assign("Information" , $this->string_Information);
386 /* Check for exeeded sizelimit */
387 $smarty->assign("hint" , print_sizelimit_warning());
388 $smarty->assign("DivList" , $divlist->DrawList());
391 if($this->is_headpage){
392 $smarty->assign("tree_image", get_template_path('images/tree.png'));
393 $smarty->assign("infoimage", get_template_path('images/info.png'));
394 $smarty->assign("launchimage", get_template_path('images/launch.png'));
395 $smarty->assign("apply", apply_filter());
396 }else{
397 $smarty->assign("tree_image", get_template_path('images/tree.png'));
398 $smarty->assign("infoimage", get_template_path('images/info_small.png'));
399 $smarty->assign("launchimage", get_template_path('images/rocket.png'));
400 $smarty->assign("apply", apply_filter());
401 }
403 /* Button handling */
404 $smarty->assign("SaveButtonString" ,$this->SaveButtonString);
405 $smarty->assign("CloseButtonString",$this->CloseButtonString);
407 $smarty->assign("Display_Close", $this->bool_DisplayCloseButton);
408 $smarty->assign("Display_Save" , $this->bool_DisplaySaveButton);
410 $smarty->assign("filterName" , $this->filterName);
411 $smarty->assign("is_headpage" , $this->is_headpage);
413 $display = $smarty->fetch(get_template_path("MultiSelectWindow.tpl"));
414 return($display);
415 }
417 /* Set the close var, which simulates the close button is pressed */
418 function Close()
419 {
420 $this->is_closed = true;
421 }
423 function Save()
424 {
425 $this->is_saved = true;
426 }
428 /* Store all checkboxes/ regexes ...
429 Store data also into a session var, to keep the checkboxes check after reload */
430 function save_object()
431 {
432 /* Save base selection from headpage selectbox*/
433 if(isset($_POST['CurrentMainBase'])){
434 $this->selectedBase = $_POST['CurrentMainBase'];
435 }
437 /* Update current base */
438 $s_action ="";
439 foreach($_POST as $key => $value){
440 if(preg_match("/^dep_back.*/i",$key)){
441 $s_action="back";
442 }elseif(preg_match("/^dep_root.*/",$key)){
443 $s_action="root";
444 }elseif(preg_match("/^dep_home.*/i",$key)){
445 $s_action="home";
446 }
447 }
449 /* Homebutton is posted */
450 if($s_action=="home"){
451 $ui= get_userinfo();
452 $base = get_base_from_people($ui->dn);
453 $this->selectedBase= $base;
454 }
456 /* Open selected department
457 this is posted by the parent class MultiSelectWindow */
458 if(isset($_GET['act'])&& ($_GET['act'] == "dep_open")){
459 $s_entry = base64_decode($_GET['dep_id']);
460 if (!isset($this->config->departments[$s_entry])){
461 print_red(_("Error: The requested subtree has an inconsistent DN encoding, check your LDAP!"));
462 } else {
463 $this->selectedBase = $this->config->departments[$s_entry];
464 }
465 }
467 /* back to the roots ^^ */
468 if($s_action=="root"){
469 $this->selectedBase=($this->config->current['BASE']);
470 }
473 /* If Back-button is pressed, move back one step in DN */
474 if($s_action=="back"){
475 //FIXME: This is not 100% correct. We'll only display ou's, but there may be
476 // a step between. You'll stumble in a "hidden" department in this case.
477 $base_back= preg_replace("/^[^,]+,/", "", $_SESSION['CurrentMainBase']);
479 /* The department array keeps non DN entries as index. We need to convert
480 it before checking the existance. */
481 $base_back= trim(convert_department_dn($base_back));
483 /* Check if the department exists, otherwise revert to the configure base DN */
484 if(isset($this->config->departments[$base_back])){
485 $this->selectedBase= $this->config->departments[$base_back];
486 }else{
487 $this->selectedBase= $this->config->departments['/'];
488 }
489 }
490 $_SESSION['CurrentMainBase'] = $this->selectedBase;
492 if(isset($_POST['MultiSelectWindow'.$this->filterName])){
494 /* Save some additional vars */
495 foreach($this->SaveAdditionalVars as $name){
496 if(isset($_POST[$name])){
497 if(isset($this->$name)){
498 $this->$name = $_POST[$name];
499 $_SESSION['MultiDialogFilters'][$this->filterName][$name] = $_POST[$name];
500 }
501 }
502 }
504 /* Check posts from checkboxes
505 */
506 foreach($this->array_Checkboxes as $key => $box){
507 if(isset($_POST[$box['name']])){
508 $this->array_Checkboxes[$key]['default'] = true;
509 $this->$box['name'] = true;
510 }else{
511 $this->array_Checkboxes[$key]['default'] = false;
512 $this->$box['name'] = false;
513 }
514 /* Save settings in out session */
515 $_SESSION['MultiDialogFilters'][$this->filterName][$box['name']] = $this->$box['name'];
516 }
518 /* Check regex posts */
519 foreach($this->array_Regexes as $key => $box){
520 $this->array_Regexes[$key]['value'] = $_POST[$box['name']];
521 $this->$box['name'] = $_POST[$box['name']];
522 $_SESSION['MultiDialogFilters'][$this->filterName][$box['name']] = $this->$box['name'];
523 }
525 /* call close/save if buttons are pressed */
526 if(isset($_POST['CloseMultiSelectWindow'])){
527 $this->Close();
528 }
530 if(isset($_POST['SaveMultiSelectWindow'])){
531 $this->Save();
532 }
533 }
535 /* check for alphabet selection
536 Check which regexes are connected to the alphabet
537 */
538 if(isset($_GET['search'])){
539 foreach($this->array_Regexes as $key => $box){
540 /* check if this regex is connected to the alphabet selection */
541 if(($box['connAlpha'])&&(isset($_GET['search']))){
542 $val = $_GET['search']."*";
543 $val = preg_replace("/\*\**/","*",$val);
544 $this->array_Regexes[$key]['value'] = $val;
545 $this->$box['name'] = $val;
546 $_SESSION['MultiDialogFilters'][$this->filterName][$box['name']] = $val;
547 }
548 }
549 }
551 }
553 /* this function adds the sub-departments of the current tree to the list */
554 function AddDepartments($base = false,$numtabs = 3)
555 {
556 $this->DepartmentsAdded = true;
557 $this->Added_Departments = array();
559 /* check for a valid base */
560 if(!$base){
561 if(!isset($_SESSION['CurrentMainBase'])){
562 $_SESSION['CurrentMainBase'] = $this->config->current['BASE'];
563 }
564 $base = $_SESSION['CurrentMainBase'];
565 }
567 /* Create ldap obj and switch into base*/
568 $ldap = $this->config->get_ldap_link();
569 $ldap->cd($base);
571 /* reset current deps */
572 $this->departments = array();
574 /* Get all departments within this subtree */
575 $deps= get_list("(&(|(ou=*)(description=*))(objectClass=gosaDepartment))", $this->ui->subtreeACL,
576 $base, array("ou", "description"), GL_SIZELIMIT | GL_CONVERT);
578 /* Edit delete link for system types
579 */
580 $linkopen = "<a href='?plug=".$_GET['plug']."&act=dep_open&dep_id=%s'>%s</a>";
582 /* Create an array with all visible (in the list) departments */
583 $departments = array();
584 foreach($deps as $value){
585 if(isset($value['description'][0])){
586 $this->departments[$value['dn']]= get_sub_department($value['dn'])." - [".$value["description"][0]."]";
587 }else{
588 $this->departments[$value['dn']]= get_sub_department($value['dn']);
589 }
590 }
591 natcasesort($this->departments);
593 /* Add deps to this dialog object list */
594 foreach($this->departments as $key=> $val){
595 /* Add missing entries ... */
596 if(!isset($this->config->departments[trim($key)])){
597 $this->config->departments[trim($key)]="";
598 }
600 /* check if this department contains sub-departments
601 Display different image in this case
602 */
603 $non_empty="";
604 $nkey= normalizePreg($key);
605 foreach($this->config->departments as $keyd=>$vald ){
606 if(preg_match("/$nkey\/.*/",$keyd)){
607 $non_empty="full";
608 }
609 }
611 /* Add to divlist */
612 $row = array();
613 $row[]=$field1=array("string"=>"<img src='images/".$non_empty."folder.png' alt='department'>","attach"=>"style='text-align:center;width:20px;'");
614 $row[]=$field2=array("string"=>sprintf($linkopen,base64_encode($key),$val), "attach" => "style=''");
616 if($numtabs > 2){
617 for($i = 2 ; $i <$numtabs;$i++){
618 if(isset( $this->array_Header[$i]['attach'])){
619 $row[] = array("string"=>" ","attach" => $this->array_Header[$i]['attach']);
620 }else{
621 $row[] = array("string"=>" ");
622 }
623 }
624 }
626 $this->AddElement($row);
627 $this->Added_Departments[] = $row;
628 }
629 }
631 function set_List_Bottom_Info($str)
632 {
633 $this->List_Bottom_Info = $str;
634 }
636 function get_List_Bottom_Info()
637 {
638 return($this->List_Bottom_Info);
639 }
640 }
641 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
642 ?>