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 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 /* Update current base */
427 if($this->DepartmentsAdded){
428 $s_action ="";
429 foreach($_POST as $key => $value){
430 if(preg_match("/^dep_back.*/i",$key)){
431 $s_action="back";
432 }elseif(preg_match("/^dep_root.*/",$key)){
433 $s_action="root";
434 }elseif(preg_match("/^dep_home.*/i",$key)){
435 $s_action="home";
436 }
437 }
439 /* Save base selection from headpage selectbox*/
440 if(isset($_POST['CurrentMainBase'])){
441 $this->selectedBase = $_POST['CurrentMainBase'];
442 }
444 /* Homebutton is posted */
445 if($s_action=="home"){
446 $ui= get_userinfo();
447 $base = get_base_from_people($ui->dn);
448 $this->selectedBase= $base;
449 }
451 /* Open selected department
452 this is posted by the parent class MultiSelectWindow */
453 if(isset($_GET['act'])&& ($_GET['act'] == "dep_open")){
454 $s_entry = base64_decode($_GET['dep_id']);
455 if (!isset($this->config->departments[$s_entry])){
456 print_red(_("Error: The requested subtree has an inconsistent DN encoding, check your LDAP!"));
457 } else {
458 $this->selectedBase = $this->config->departments[$s_entry];
459 }
460 }
462 /* back to the roots ^^ */
463 if($s_action=="root"){
464 $this->selectedBase=($this->config->current['BASE']);
465 }
468 /* If Back-button is pressed, move back one step in DN */
469 if($s_action=="back"){
470 //FIXME: This is not 100% correct. We'll only display ou's, but there may be
471 // a step between. You'll stumble in a "hidden" department in this case.
472 $base_back= preg_replace("/^[^,]+,/", "", $_SESSION['CurrentMainBase']);
474 /* The department array keeps non DN entries as index. We need to convert
475 it before checking the existance. */
476 $base_back= trim(convert_department_dn($base_back));
478 /* Check if the department exists, otherwise revert to the configure base DN */
479 if(isset($this->config->departments[$base_back])){
480 $this->selectedBase= $this->config->departments[$base_back];
481 }else{
482 $this->selectedBase= $this->config->departments['/'];
483 }
484 }
485 $_SESSION['CurrentMainBase'] = $this->selectedBase;
486 }
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;
554 /* check for a valid base */
555 if(!$base){
556 if(!isset($_SESSION['CurrentMainBase'])){
557 $_SESSION['CurrentMainBase'] = $this->config->current['BASE'];
558 }
559 $base = $_SESSION['CurrentMainBase'];
560 }
562 /* Create ldap obj and switch into base*/
563 $ldap = $this->config->get_ldap_link();
564 $ldap->cd($base);
566 /* reset current deps */
567 $this->departments = array();
569 /* Get all departments within this subtree */
570 $deps= get_list("(&(|(ou=*)(description=*))(objectClass=gosaDepartment))", $this->ui->subtreeACL,
571 $base, array("ou", "description"), GL_SIZELIMIT | GL_CONVERT);
573 /* Edit delete link for system types
574 */
575 $linkopen = "<a href='?plug=".$_GET['plug']."&act=dep_open&dep_id=%s'>%s</a>";
577 /* Create an array with all visible (in the list) departments */
578 $departments = array();
579 foreach($deps as $value){
580 if(isset($value['description'][0])){
581 $this->departments[$value['dn']]= get_sub_department($value['dn'])." - [".$value["description"][0]."]";
582 }else{
583 $this->departments[$value['dn']]= get_sub_department($value['dn']);
584 }
585 }
586 natcasesort($this->departments);
588 /* Add deps to this dialog object list */
589 foreach($this->departments as $key=> $val){
590 /* Add missing entries ... */
591 if(!isset($this->config->departments[trim($key)])){
592 $this->config->departments[trim($key)]="";
593 }
595 /* check if this department contains sub-departments
596 Display different image in this case
597 */
598 $non_empty="";
599 $nkey= normalizePreg($key);
600 foreach($this->config->departments as $keyd=>$vald ){
601 if(preg_match("/$nkey\/.*/",$keyd)){
602 $non_empty="full";
603 }
604 }
606 /* Add to divlist */
607 $row = array();
608 $row[]=$field1=array("string"=>"<img src='images/".$non_empty."folder.png' alt='department'>","attach"=>"style='text-align:center;width:20px;'");
609 $row[]=$field2=array("string"=>sprintf($linkopen,base64_encode($key),$val), "attach" => "style=''");
611 if($numtabs > 2){
612 for($i = 2 ; $i <$numtabs;$i++){
613 if(isset( $this->array_Header[$i]['attach'])){
614 $row[] = array("string"=>" ","attach" => $this->array_Header[$i]['attach']);
615 }else{
616 $row[] = array("string"=>" ");
617 }
618 }
619 }
621 $this->AddElement($row);
622 }
623 }
624 }
625 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
626 ?>