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