Code

3f1f089c83cec7f40ab4073c42062cb52a41ebf3
[gosa.git] / include / class_MultiSelectWindow.inc
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 
38   var $module= "";
40         function ClearElementsList()
41         {
42                 $this->array_Elements = array();
43         }
45   function HideFilterPart($bool = true)
46   {
47     $this->HideFilterPart = $bool;
48   }
50   function SetHeight($height)
51   { 
52     $this->DivHeight=$height;
53   }
55         /* Adds a regex input field to the current dialog */
56         function AddRegex($name,$string,$value,$conn,$image="images/search.png")
57         {
58                 $arr = array();
60                 /* Check if the given input field name was already used 
61                    for this type of dialog */   
62                 if(isset($_SESSION['MultiDialogFilters'][$this->filterName][$name])){
63                         $arr['value']   = $_SESSION['MultiDialogFilters'][$this->filterName][$name];
64                         $this->$name = $arr['value'];
65                 }else{
66                         $arr['value']   = $value;       
67                 }
69                 $arr['name']            = $name;
70                 $arr['string']          = $string;
71                 $arr['image']           = $image;       
72                 $arr['connAlpha']       = $conn;                // Connect with alphabet select 
73                 $this->array_Regexes[] = $arr;
74         }
76         /* Contrucktion */
77         function MultiSelectWindow($config, $filterName, $module)
78         {
79                 $this->config = $config;
80                 $this->module = $module;
81                 $this->SaveButtonString         = _("Save");
82                 $this->CloseButtonString        = _("Close");
83                 $this->filterName                       = $filterName;
85                 $this->selectedBase = $_SESSION['CurrentMainBase'];
87     /* Check default values for SaveAdditionalVars */
88     foreach($this->SaveAdditionalVars as $name){
89       if(isset($_SESSION['MultiDialogFilters'][$this->filterName][$name])){
90         $this->$name = $_SESSION['MultiDialogFilters'][$this->filterName][$name];
91       }
92     }
93     
94         }
96         /* Enables the headpage mode, which changes the list look */
97         function SetHeadpageMode()
98         {
99                 $this->is_headpage = true;
100         }
102         /* Sets the List internal name (not displayed anywhere) 
103            it is used to identify every single list
104          */     
105         function SetTitle($str)
106         {
107                 $this->string_Title = $str;
108         }
110         /* Set the list header string  */
111         function SetListHeader($str)
112         {
113                 $this->string_ListHeader = $str;
114         }
116         /* This sets the list description which is the first gray bar on top of the list */
117         function SetSummary($str)
118         {
119                 $this->string_Summary = $str;
120         }
122         /* If the save button is enabled, you can change its caption with this function */      
123         function SetSaveButtonString($str)
124         {
125                 $this->SaveButtonString = $str;
126         }
128         /* If the close button is enabled, you can change its caption with this function */     
129         function SetCloseButtonString($str)
130         {
131                 $this->CloseButtonString = $str;
132         }
134         /* With this function you can change the text of the information box */
135         function SetInformation($str)
136         {
137                 $this->string_Information = $str;
138         }
140         /* Display the alphabet selection box*/
141         function EnableAplhabet($bool)
142         {
143                 $this->bool_DisplayAlpahabet = $bool;
144         }
146         /* Add additional header col */
147         function AddHeader($arr)
148         {
149                 $this->array_Header[] = $arr;
150         }
152         /* add additional List element */
153         function AddElement($arr)
154         {
155                 $this->array_Elements[] = $arr;
156         }
159   /* Return default header part. With back, home and root icons and department selection */
160   function get_default_header()
161   {
162     $enable_back = TRUE;
163     $enable_root = TRUE;
164     $enable_home = TRUE;
166     $ui = get_userinfo();
168     /* Check if selectedBase = first available base */
169     $deps = $ui->get_module_departments($this->module);
171     if(!count($deps) || $deps[0] == $this->selectedBase){
172       $enable_back = FALSE;
173       $enable_root = FALSE;
174     }
175    
176     /* Check if we are in users home  department */ 
177     if(!count($deps) ||$this->selectedBase == get_base_from_people($ui->dn)){
178       $enable_home = FALSE;
179     }
181     /* Create header with selected base */
182     $listhead = "<div style='background:white;padding:5px;'>";
184     /* Draw root button */
185     if($enable_root){
186       $listhead .= " <input class='center' type='image' src='images/list_root.png' align='middle'
187         title='"._("Go to root department")."' name='dep_root' alt='"._("Root")."'>&nbsp;";
188     }else{
189       $listhead .= " <img src='images/list_root_gray.png' class='center' alt='"._("Root")."'>&nbsp;";
190     }
192     /* Draw back button */
193     if($enable_back){
194       $listhead .= " <input class='center' type='image' align='middle' src='images/list_back.png'
195         title='"._("Go up one department")."' alt='"._("Up")."'                name='dep_back'>&nbsp;";
196     }else{
197       $listhead .= " <img src='images/list_back_gray.png' class='center' alt='"._("Up")."'>&nbsp;";
198     }
200     /* Draw home button */
201     if($enable_home){
202       $listhead .= " <input class='center' type='image' align='middle' src='images/list_home.png'
203         title='"._("Go to users department")."' alt='"._("Home")."'            name='dep_home'>&nbsp;";
204     }else{
205       $listhead .= " <img src='images/list_home_gray.png' class='center' alt='"._("Home")."'>&nbsp;";
206     }
207    
208     /* And at least draw reload button, this button is enabled everytime */ 
209     $listhead .=  " <input class='center' type='image' src='images/list_reload.png' align='middle'
210       title='"._("Reload list")."' name='submit_department' alt='"._("Submit")."'>&nbsp;".
211       " <img   class='center' src='images/list_seperator.png' align='middle' alt='-' height='16' width='1'>&nbsp;";
213     return ($listhead);
214   }
216         /* Add a checkbox to the filter element,
217            the name specifies an existing class var to store the 'selection' */
218         function AddCheckBox($name,$value="Unset",$string="Unset",$default=false)
219         {
220                 $arr = array();
222                 if($name == SEPERATOR){
223                         $arr['name'] = SEPERATOR;
224                 }else{
225                         /* Check if there was already a variable 
226                            for this dialog which we should use instead of the default*/
227                         if(isset($_SESSION['MultiDialogFilters'][$this->filterName][$name])){
228                                 $arr['default'] = $_SESSION['MultiDialogFilters'][$this->filterName][$name];
229                                 $this->$name = $arr['default'];
230                         }else{
231                                 $arr['default'] = $default; 
232         $this->$name = $default;
233                         }
234                         $arr['name']          = $name;
235                         $arr['string']      = $string;
236                         $arr['value']         = $value;
237                         $arr['enabled']     = true;
239                 }
240                 $this->array_Checkboxes[] = $arr;
241         }
244         /* Hides or unhides the checkbox with the given name */
245         function DisableCheckBox($name,$HideShow = false)
246         {
247                 foreach($this->array_Checkboxes as $key => $chkbox){
248                         if($chkbox['name'] == $name){
249                                 $this->array_Checkboxes[$key]['enabled'] = $HideShow;
250                         }
251                 }
252         }
255         /* Returns true if the close button was pressed */
256         function isClosed()
257         {
258                 return($this->is_closed);
259         }
261         /* Enable the close button */
262         function EnableCloseButton($bool)
263         {
264                 $this->bool_DisplayCloseButton = $bool;
265         }
267         /* Enable the save button on the bottom of the list*/
268         function EnableSaveButton ($bool)
269         {
270                 $this->bool_DisplaySaveButton = $bool;
271         }
273   /* Add a list specific filter object to position 
274       1 on top of Information 
275       2 Between Information && Filter
276       3 Below the Filter Part */
277   function AddUserBoxToFilter($position)
278   {
279     return("");
280   }
282         /* Draw the list with all list elements and filters */
283         function Draw()
284         {
286                 /* Check for exeeded sizelimit */
287                 if (($message= check_sizelimit()) != ""){
288                         return($message);
289                 }
291                 $smarty = get_smarty();
292                 $smarty->assign("UserBox1",$this->AddUserBoxToFilter(1));
293                 $smarty->assign("UserBox2",$this->AddUserBoxToFilter(2));
294                 $smarty->assign("UserBox3",$this->AddUserBoxToFilter(3));
296                 $divlist = new divlist($this->string_Title);
297                 $divlist->SetSummary($this->string_Summary);
298                 $divlist->SetEntriesPerPage(0); // 0 for scrollable list
300     /* Display list footer with summary of all listed entries */
301     if(isset($this->config->data['MAIN']['LIST_SUMMARY']) && preg_match("/true/i",$this->config->data['MAIN']['LIST_SUMMARY'])){
302       $divlist->SetFooter($this->get_List_Bottom_Info());
303     }
304   
305     if($this->DivHeight != ""){
306       $divlist->SetHeight($this->DivHeight);
307     }
309                 /* set Header informations 
310                  */
311                 $header = array();
312                 foreach($this->array_Header as $head){
313                         $header[] = $head;
314                 }
315                 $divlist->SetHeader($header);
317                 /* set Entries 
318                  */
319                 $elements = array();
320                 foreach($this->array_Elements as $element){
321                         $divlist->AddEntry($element);
322                 }
324                 /* Create checkboxes fields 
325                  */
326                 $boxes = "";
327                 $boxClick = " onClick='document.mainform.submit();' ";
328                 foreach($this->array_Checkboxes as $box){
330                         if($box['name'] == SEPERATOR){
331                                 $boxes .= "</td></tr></table><table style='width:100%;border-top:1px solid #B0B0B0;'><tr><td>";
332                                 continue;
333                         }
335                         /* Skip disabled boxes */
336                         if(!$box['enabled']) continue;
338                         /* Check if box is checked */
339                         if($box['default'] == true){
340                                 $boxes .="<input type='checkbox' name=\"".$box['name']."\" value='1' title=\"".htmlentities($box['value'])."\" checked ".$boxClick.">&nbsp;".$box['string']."<br>";
341                         }else{
342                                 $boxes .="<input type='checkbox' name=\"".$box['name']."\" value='1' title=\"".htmlentities($box['value'])."\" ".$boxClick.">&nbsp;".$box['string']."<br>";
343                         }
344                 }
345                 $smarty->assign("CheckBoxes", $boxes);
347                 /* Assign regex fields 
348                  */
349                 $regexes = "";
350                 foreach($this->array_Regexes as $regex){
351                         $regexes.="<table summary=\"\" style=\"width:100%;border-top:1px solid #B0B0B0;\">
352                                 <tr>
353                                 <td>
354                                 <label for=\"".$regex['name']."\">
355                                 <img alt=\"".$regex['string']."\" src=\"".$regex['image']."\" align=middle>
356                                 </label>
357                                 </td>
358                                 <td width=\"99%\">
359                                 <input id=\"".$regex['name']."\" type=\"text\" style='width:99%' name=\"".$regex['name']."\" maxlength='20'
360                                 value=\"".htmlentities($regex['value'])."\" title=\"".htmlentities($regex['string'])."\"> 
361                                 </td>
362                                 </tr>
363                                 </table>";
364                 }
365                 $smarty->assign("regexes"                       , $regexes );
367     /* Hide Filter Part if Requested or empty */
368     if((empty($boxes)) && (empty($regexes)) || ($this->HideFilterPart)){
369                   $smarty->assign("Skip_Filter_Part",   true);
370     }else{ 
371                   $smarty->assign("Skip_Filter_Part",   false);
372     }
374                 /* Assign alphabet and display it 
375                  */     
376                 $smarty->assign("Display_alphabet",     $this->bool_DisplayAlpahabet);
377                 $smarty->assign("alphabet",             generate_alphabet());
378                 $smarty->assign("Header"                        , $this->string_ListHeader );
379                 $smarty->assign("Summary"                       , $this->string_Summary);
380                 $smarty->assign("Title"                         , $this->string_Title);
381                 $smarty->assign("Information"           , $this->string_Information);
383                 /* Check for exeeded sizelimit */
384                 $smarty->assign("hint"                          , print_sizelimit_warning());
385                 $smarty->assign("DivList"                       , $divlist->DrawList());
388                 if($this->is_headpage){
389                         $smarty->assign("tree_image",           get_template_path('images/tree.png'));
390                         $smarty->assign("infoimage",            get_template_path('images/info.png'));
391                         $smarty->assign("launchimage",          get_template_path('images/launch.png'));
392                         $smarty->assign("apply",                        apply_filter());
393                 }else{
394                         $smarty->assign("tree_image",           get_template_path('images/tree.png'));
395                         $smarty->assign("infoimage",            get_template_path('images/info_small.png'));
396                         $smarty->assign("launchimage",          get_template_path('images/rocket.png'));
397                         $smarty->assign("apply",                        apply_filter());
398                 }
400                 /* Button handling */
401                 $smarty->assign("SaveButtonString" ,$this->SaveButtonString);
402                 $smarty->assign("CloseButtonString",$this->CloseButtonString);
404                 $smarty->assign("Display_Close",        $this->bool_DisplayCloseButton);
405                 $smarty->assign("Display_Save" ,        $this->bool_DisplaySaveButton);
407                 $smarty->assign("filterName"    ,       $this->filterName);
408                 $smarty->assign("is_headpage"   ,       $this->is_headpage);
410                 $display = $smarty->fetch(get_template_path("MultiSelectWindow.tpl"));
411                 return($display);
412         }
414         /* Set the close var, which simulates the close button is pressed */
415         function Close()
416         {
417                 $this->is_closed = true;
418         }
420         function Save()
421         {
422                 $this->is_saved = true;
423         }
425         /* Store all checkboxes/ regexes ... 
426            Store data also into a session var, to keep the checkboxes check after reload  */
427         function save_object()
428         {
429     /* Get up to date config */
430     if(isset($this->parent->config)){
431       $this->config = $this->parent->config;
432     }
434     /* Update current base */
435     $s_action ="";
436     foreach($_POST as $key => $value){
437       if(preg_match("/^dep_back.*/i",$key)){
438         $s_action="back";
439       }elseif(preg_match("/^dep_root.*/",$key)){
440         $s_action="root";
441       }elseif(preg_match("/^dep_home.*/i",$key)){
442         $s_action="home";
443       }
444     }
446     /* Save base selection from headpage selectbox*/
447     if(isset($_POST['CurrentMainBase'])){
448       $this->selectedBase = $_POST['CurrentMainBase'];
449     }
451     /* Homebutton is posted */
452     if($s_action=="home"){
453       $ui= get_userinfo();
454       $base = get_base_from_people($ui->dn);
455       $this->selectedBase= $base;
456     }
458     /* Open selected department
459        this is posted by the parent class MultiSelectWindow */
460     if(isset($_GET['act'])&& ($_GET['act'] == "dep_open")){
461       $s_entry = base64_decode($_GET['dep_id']);
462       if (!isset($this->config->departments[$s_entry])){
463         print_red(_("Error: The requested subtree has an inconsistent DN encoding, check your LDAP!"));
464       } else {
465         $this->selectedBase = $this->config->departments[$s_entry];
466       }
467     }
469     /* back to the roots ^^ */
470     if($s_action=="root"){
471       $this->selectedBase=($this->config->current['BASE']);
472       $dep_id = $this->ui->get_module_departments($this->module);
473       $this->selectedBase = $dep_id[key($dep_id)];
474     }
477     /* If Back-button is pressed, move back one step in DN */
478     if($s_action=="back"){
480       /* Get parent deprtment and check if we are allowed to step in it */
481       $base_back= preg_replace("/^[^,]+,/", "", $_SESSION['CurrentMainBase']);
482       $dep_id = $this->ui->get_module_departments($this->module);
483       if(in_array_ics($base_back,$dep_id)){
485         /* The department array keeps non DN entries as index. We need to convert
486            it before checking the existance. */
487         $base_back= trim(convert_department_dn($base_back));
489         /* Check if the department exists, otherwise revert to the configure base DN */
490         if(isset($this->config->departments[$base_back])){
491           $this->selectedBase= $this->config->departments[$base_back];
492         }else{
493           $this->selectedBase= $this->config->departments['/'];
494         }
495       }
496     }
497     $_SESSION['CurrentMainBase'] = $this->selectedBase;
499                 if(isset($_POST['MultiSelectWindow'.$this->filterName])){
501       /* Save some additional vars */
502       foreach($this->SaveAdditionalVars as $name){
503         if(isset($_POST[$name])){
504           if(isset($this->$name)){
505             $this->$name = $_POST[$name];
506             $_SESSION['MultiDialogFilters'][$this->filterName][$name] = $_POST[$name];
507           }
508         }
509       }
511                         /* Check posts from checkboxes 
512                          */
513                         foreach($this->array_Checkboxes as $key => $box){
514                                 if(isset($_POST[$box['name']])){
515                                         $this->array_Checkboxes[$key]['default'] = true;
516                                         $this->$box['name'] = true;
517                                 }else{
518                                         $this->array_Checkboxes[$key]['default'] = false;
519                                         $this->$box['name'] = false;
520                                 }
521                                 /* Save settings in out session */
522                                 $_SESSION['MultiDialogFilters'][$this->filterName][$box['name']] = $this->$box['name'];
523                         }
525                         /* Check regex posts */
526                         foreach($this->array_Regexes as $key => $box){
527                                 $this->array_Regexes[$key]['value'] = $_POST[$box['name']];
528                                 $this->$box['name'] = $_POST[$box['name']];
529                                 $_SESSION['MultiDialogFilters'][$this->filterName][$box['name']] = $this->$box['name'];
530                         }
532                         /* call close/save if buttons are pressed */
533                         if(isset($_POST['CloseMultiSelectWindow'])){
534                                 $this->Close();
535                         }
537                         if(isset($_POST['SaveMultiSelectWindow'])){
538                                 $this->Save();
539                         }
540                 }
542                 /* check for alphabet selection
543                    Check which regexes are connected to the alphabet 
544                  */     
545                 if(isset($_GET['search'])){
546                         foreach($this->array_Regexes as $key => $box){
547                                 /* check if this regex is connected to the alphabet selection */
548                                 if(($box['connAlpha'])&&(isset($_GET['search']))){
549                                         $val =  $_GET['search']."*";
550                                         $val = preg_replace("/\*\**/","*",$val);
551                                         $this->array_Regexes[$key]['value'] = $val;
552                                         $this->$box['name'] = $val;
553           $_SESSION['MultiDialogFilters'][$this->filterName][$box['name']] =  $val;
554                                 }
555                         }
556                 }
557         }
560   /* Create header snapshot value */
561   function get_snapshot_header()
562   {
563     $str = "&nbsp;";
564     if($this->parent->snapshotEnabled()){
565       $ok = false;
566       foreach($this->parent->get_used_snapshot_bases() as $base){
567         $ok |= count($this->parent->getAllDeletedSnapshots($base)) >= 1 ;
568       }
569       if($ok){
570         $str = "<input class='center' type='image' align='middle' src='images/restore.png'
571           title='"._("Restore snapshopts of already deleted objects")."' 
572           alt='"._("Restore")."' name='RestoreDeletedSnapShot'>&nbsp;"; 
573       }else{
574         $str = "<img class='center' src='images/restore_grey.png' alt=''>&nbsp;";
575       }
577 #      $str .= "<img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;";
578     }
579     return($str);
580   }
583   function GetSnapShotActions($dn)
584   {
585     $str= "";
586     if($this->parent->snapshotEnabled()){
588       if(count($this->parent->Available_SnapsShots($dn))){
589         $str.= "<input class='center' type='image' src='images/restore.png'
590           alt='"._("Restore snapshot")."' name='RestoreSnapShotDialog_".base64_encode($dn)."' title='"._("Restore snapshot")."'>&nbsp;";
591       } else {
592         $str = "<img class='center' src='images/restore_grey.png' alt=''>&nbsp;";
593       }
595       $str.= "<input class='center' type='image' src='images/snapshot.png'
596         alt='"._("Create snapshot")."' name='CreateSnapShotDialog_".base64_encode($dn)."' title='"._("Create a new snapshot from this object")."'>&nbsp;";
597     }
599     return($str);
600   }
603         /* this function adds the sub-departments of the current tree to the list */
604         function AddDepartments($base = false,$numtabs = 3,$empty_tabs_in_front = 0)
605         {
606                 $this->DepartmentsAdded = true;
607     $this->Added_Departments = array();
609                 /* check for a valid base */
610                 if(!$base){
611                         if(!isset($_SESSION['CurrentMainBase'])){
612                                 $_SESSION['CurrentMainBase'] = $this->config->current['BASE'];
613                         }
614                         $base = $_SESSION['CurrentMainBase'];
615                 }
617                 /* Create ldap obj and switch into base*/
618                 $ldap = $this->config->get_ldap_link();
619                 $ldap->cd($base);
621                 /* reset current deps */
622                 $this->departments = array();
624                 /* Get all departments within this subtree */
625                 $deps= get_list("(&(|(ou=*)(description=*))(objectClass=gosaDepartment))", $this->module, $base,
626                     array("ou", "description"), GL_SIZELIMIT | GL_CONVERT);
628                 /* Edit delete link for system types
629                  */
630                 $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=dep_open&amp;dep_id=%s'>%s</a>";
632                 /* Create an array with all visible (in the list) departments */
633                 $departments = array();
634                 foreach($deps as $value){
635                         if(isset($value['description'][0])){
636                                 $this->departments[$value['dn']]= get_sub_department($value['dn'])." - [".$value["description"][0]."]";
637                         }else{
638                                 $this->departments[$value['dn']]= get_sub_department($value['dn']);
639                         }
640                 }
641                 natcasesort($this->departments);
644     if($empty_tabs_in_front){
645 #      $numtabs -= $empty_tabs_in_front;
646     }
647   
648                 /* Add deps to this dialog object list */
649                 foreach($this->departments as $key=> $val){
651                         /* Add missing entries ... */
652                         if(!isset($this->config->departments[trim($key)])){
653                                 $this->config->departments[trim($key)]="";
654                         }
656                         /* check if this department contains sub-departments
657                            Display different image in this case
658                          */
659                         $non_empty="";
660                         $nkey= normalizePreg($key);
661                         foreach($this->config->departments as $keyd=>$vald ){
662                                 if(preg_match('/$nkey\/.*/',$keyd)){
663                                         $non_empty="full";
664                                 }
665                         }
667                         /* Add to divlist */
668                         $row = array();
670       if($empty_tabs_in_front){
671         for($i = 0; $i < $empty_tabs_in_front ; $i ++){
672           $row[] = array("string"=>"&nbsp;", "attach" => "style='text-align:center;width:20px;'");
673         }
674       }
676                         $row[]=$field1=array("string"=>"<img src='images/".$non_empty."folder.png' alt='department'>","attach"=>"style='text-align:center;width:20px;'");
677                         $row[]=$field2=array("string"=>sprintf($linkopen,base64_encode($key),$val), "attach" => "style=''");
679                         if($numtabs > 2){       
680                                 for($i = 2 ; $i < $numtabs;$i++){
681           if(isset( $this->array_Header[$i + $empty_tabs_in_front]['attach'])){
682             $row[] = array("string"=>"&nbsp;","attach" => $this->array_Header[$i + $empty_tabs_in_front]['attach']);
683           }else{
684             $row[] = array("string"=>"&nbsp;");
685           }
686                                 }
687                         }
688                         $this->AddElement($row);
689       $this->Added_Departments[] = $row;
690                 }
691         }
693   function set_List_Bottom_Info($str)
694   {
695     $this->List_Bottom_Info = $str;
696   }
698   function get_List_Bottom_Info()
699   {
700     return($this->List_Bottom_Info); 
701   }
703 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
704 ?>