Code

6f939f96c1baf4237a09560ec0fd3231205ffa21
[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 $selectedBase       = "";
32   var $DivHeight          = "";
34   var $HideFilterPart     = false;
36   var $SaveAdditionalVars = array();  // Additional Post vars to store 
37   var $module= "";
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, $module)
77         {
78                 $this->config = $config;
79                 $this->module = $module;
80                 $this->SaveButtonString         = _("Save");
81                 $this->CloseButtonString        = _("Close");
82                 $this->filterName                       = $filterName;
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     }
92     
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   /* Return default header part. With back, home and root icons and department selection */
159   function get_default_header()
160   {
161     $enable_back = TRUE;
162     $enable_root = TRUE;
163     $enable_home = TRUE;
165     $ui = get_userinfo();
167     /* Check if selectedBase = first available base */
168     $deps = $ui->get_module_departments($this->module);
169     if($deps[0] == $this->selectedBase){
170       $enable_back = FALSE;
171       $enable_root = FALSE;
172     }
173    
174     /* Check if we are in users home  department */ 
175     if($this->selectedBase == get_base_from_people($ui->dn)){
176       $enable_home = FALSE;
177     }
179     /* Create header with selected base */
180     $listhead = "<div style='background:#F0F0F9;padding:5px;'>";
182     /* Draw root button */
183     if($enable_root){
184       $listhead .= " <input class='center' type='image' src='images/list_root.png' align='middle'
185         title='"._("Go to root department")."' name='dep_root' alt='"._("Root")."'>&nbsp;";
186     }else{
187       $listhead .= " <img src='images/list_root_gray.png' class='center' alt='"._("Root")."'>&nbsp;";
188     }
190     /* Draw back button */
191     if($enable_back){
192       $listhead .= " <input class='center' type='image' align='middle' src='images/list_back.png'
193         title='"._("Go up one department")."' alt='"._("Up")."'                name='dep_back'>&nbsp;";
194     }else{
195       $listhead .= " <img src='images/list_back_gray.png' class='center' alt='"._("Up")."'>&nbsp;";
196     }
198     /* Draw home button */
199     if($enable_home){
200       $listhead .= " <input class='center' type='image' align='middle' src='images/list_home.png'
201         title='"._("Go to users department")."' alt='"._("Home")."'            name='dep_home'>&nbsp;";
202     }else{
203       $listhead .= " <img src='images/list_home_gray.png' class='center' alt='"._("Home")."'>&nbsp;";
204     }
205    
206     /* And at least draw reload button, this button is enabled everytime */ 
207     $listhead .=  " <input class='center' type='image' src='images/list_reload.png' align='middle'
208       title='"._("Reload list")."' name='submit_department' alt='"._("Submit")."'>&nbsp;".
209       " <img   class='center' src='images/list_seperator.png' align='middle' alt='-' height='16' width='1'>&nbsp;";
211     return ($listhead);
212   }
214         /* Add a checkbox to the filter element,
215            the name specifies an existing class var to store the 'selection' */
216         function AddCheckBox($name,$value="Unset",$string="Unset",$default=false)
217         {
218                 $arr = array();
220                 if($name == SEPERATOR){
221                         $arr['name'] = SEPERATOR;
222                 }else{
223                         /* Check if there was already a variable 
224                            for this dialog which we should use instead of the default*/
225                         if(isset($_SESSION['MultiDialogFilters'][$this->filterName][$name])){
226                                 $arr['default'] = $_SESSION['MultiDialogFilters'][$this->filterName][$name];
227                                 $this->$name = $arr['default'];
228                         }else{
229                                 $arr['default'] = $default; 
230         $this->$name = $default;
231                         }
232                         $arr['name']          = $name;
233                         $arr['string']      = $string;
234                         $arr['value']         = $value;
235                         $arr['enabled']     = true;
237                 }
238                 $this->array_Checkboxes[] = $arr;
239         }
242         /* Hides or unhides the checkbox with the given name */
243         function DisableCheckBox($name,$HideShow = false)
244         {
245                 foreach($this->array_Checkboxes as $key => $chkbox){
246                         if($chkbox['name'] == $name){
247                                 $this->array_Checkboxes[$key]['enabled'] = $HideShow;
248                         }
249                 }
250         }
253         /* Returns true if the close button was pressed */
254         function isClosed()
255         {
256                 return($this->is_closed);
257         }
259         /* Enable the close button */
260         function EnableCloseButton($bool)
261         {
262                 $this->bool_DisplayCloseButton = $bool;
263         }
265         /* Enable the save button on the bottom of the list*/
266         function EnableSaveButton ($bool)
267         {
268                 $this->bool_DisplaySaveButton = $bool;
269         }
271   /* Add a list specific filter object to position 
272       1 on top of Information 
273       2 Between Information && Filter
274       3 Below the Filter Part */
275   function AddUserBoxToFilter($position)
276   {
277     return("");
278   }
280         /* Draw the list with all list elements and filters */
281         function Draw()
282         {
284                 /* Check for exeeded sizelimit */
285                 if (($message= check_sizelimit()) != ""){
286                         return($message);
287                 }
289                 $smarty = get_smarty();
290                 $smarty->assign("UserBox1",$this->AddUserBoxToFilter(1));
291                 $smarty->assign("UserBox2",$this->AddUserBoxToFilter(2));
292                 $smarty->assign("UserBox3",$this->AddUserBoxToFilter(3));
294                 $divlist = new divlist($this->string_Title);
295                 $divlist->SetSummary($this->string_Summary);
296                 $divlist->SetEntriesPerPage(0); // 0 for scrollable list 
297   
298     if($this->DivHeight != ""){
299       $divlist->SetHeight($this->DivHeight);
300     }
302                 /* set Header informations 
303                  */
304                 $header = array();
305                 foreach($this->array_Header as $head){
306                         $header[] = $head;
307                 }
308                 $divlist->SetHeader($header);
310                 /* set Entries 
311                  */
312                 $elements = array();
313                 foreach($this->array_Elements as $element){
314                         $divlist->AddEntry($element);
315                 }
317                 /* Create checkboxes fields 
318                  */
319                 $boxes = "";
320                 $boxClick = " onClick='document.mainform.submit();' ";
321                 foreach($this->array_Checkboxes as $box){
323                         if($box['name'] == SEPERATOR){
324                                 $boxes .= "</td></tr></table><table style='width:100%;border-top:1px solid #B0B0B0;'><tr><td>";
325                                 continue;
326                         }
328                         /* Skip disabled boxes */
329                         if(!$box['enabled']) continue;
331                         /* Check if box is checked */
332                         if($box['default'] == true){
333                                 $boxes .="<input type='checkbox' name=\"".$box['name']."\" value='1' title=\"".htmlentities($box['value'])."\" checked ".$boxClick.">&nbsp;".$box['string']."<br>";
334                         }else{
335                                 $boxes .="<input type='checkbox' name=\"".$box['name']."\" value='1' title=\"".htmlentities($box['value'])."\" ".$boxClick.">&nbsp;".$box['string']."<br>";
336                         }
337                 }
338                 $smarty->assign("CheckBoxes", $boxes);
340                 /* Assign regex fields 
341                  */
342                 $regexes = "";
343                 foreach($this->array_Regexes as $regex){
344                         $regexes.="<table summary=\"\" style=\"width:100%;border-top:1px solid #B0B0B0;\">
345                                 <tr>
346                                 <td>
347                                 <label for=\"".$regex['name']."\">
348                                 <img alt=\"".$regex['string']."\" src=\"".$regex['image']."\" align=middle>
349                                 </label>
350                                 </td>
351                                 <td width=\"99%\">
352                                 <input type=\"text\" style='width:99%' name=\"".$regex['name']."\" maxlength='20'
353                                 value=\"".htmlentities($regex['value'])."\" title=\"".htmlentities($regex['string'])."\"> 
354                                 </td>
355                                 </tr>
356                                 </table>";
357                 }
358                 $smarty->assign("regexes"                       , $regexes );
360     /* Hide Filter Part if Requested or empty */
361     if((empty($boxes)) && (empty($regexes)) || ($this->HideFilterPart)){
362                   $smarty->assign("Skip_Filter_Part",   true);
363     }else{ 
364                   $smarty->assign("Skip_Filter_Part",   false);
365     }
367                 /* Assign alphabet and display it 
368                  */     
369                 $smarty->assign("Display_alphabet",     $this->bool_DisplayAlpahabet);
370                 $smarty->assign("alphabet",             generate_alphabet());
371                 $smarty->assign("Header"                        , $this->string_ListHeader );
372                 $smarty->assign("Summary"                       , $this->string_Summary);
373                 $smarty->assign("Title"                         , $this->string_Title);
374                 $smarty->assign("Information"           , $this->string_Information);
376                 /* Check for exeeded sizelimit */
377                 $smarty->assign("hint"                          , print_sizelimit_warning());
378                 $smarty->assign("DivList"                       , $divlist->DrawList());
381                 if($this->is_headpage){
382                         $smarty->assign("tree_image",           get_template_path('images/tree.png'));
383                         $smarty->assign("infoimage",            get_template_path('images/info.png'));
384                         $smarty->assign("launchimage",          get_template_path('images/launch.png'));
385                         $smarty->assign("apply",                        apply_filter());
386                 }else{
387                         $smarty->assign("tree_image",           get_template_path('images/tree.png'));
388                         $smarty->assign("infoimage",            get_template_path('images/info_small.png'));
389                         $smarty->assign("launchimage",          get_template_path('images/rocket.png'));
390                         $smarty->assign("apply",                        apply_filter());
391                 }
393                 /* Button handling */
394                 $smarty->assign("SaveButtonString" ,$this->SaveButtonString);
395                 $smarty->assign("CloseButtonString",$this->CloseButtonString);
397                 $smarty->assign("Display_Close",        $this->bool_DisplayCloseButton);
398                 $smarty->assign("Display_Save" ,        $this->bool_DisplaySaveButton);
400                 $smarty->assign("filterName"    ,       $this->filterName);
401                 $smarty->assign("is_headpage"   ,       $this->is_headpage);
403                 $display = $smarty->fetch(get_template_path("MultiSelectWindow.tpl"));
404                 return($display);
405         }
407         /* Set the close var, which simulates the close button is pressed */
408         function Close()
409         {
410                 $this->is_closed = true;
411         }
413         function Save()
414         {
415                 $this->is_saved = true;
416         }
418         /* Store all checkboxes/ regexes ... 
419            Store data also into a session var, to keep the checkboxes check after reload  */
420         function save_object()
421         {
422     /* Get up to date config */
423     if(isset($this->parent->config)){
424       $this->config = $this->parent->config;
425     }
427                 /* Update current base */
428                 if($this->DepartmentsAdded){
429                         $s_action ="";
430                         foreach($_POST as $key => $value){
431                                 if(preg_match("/^dep_back.*/i",$key)){
432                                         $s_action="back";
433                                 }elseif(preg_match("/^dep_root.*/",$key)){
434                                         $s_action="root";
435                                 }elseif(preg_match("/^dep_home.*/i",$key)){
436                                         $s_action="home";
437                                 }
438                         }
440                         /* Save base selection from headpage selectbox*/
441                         if(isset($_POST['CurrentMainBase'])){
442                                 $this->selectedBase = $_POST['CurrentMainBase'];
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                           $dep_id = $this->ui->get_module_departments($this->module);
467         $this->selectedBase = $dep_id[key($dep_id)];
468       }
471       /* If Back-button is pressed, move back one step in DN */
472       if($s_action=="back"){
474         /* Get parent deprtment and check if we are allowed to step in it */
475         $base_back= preg_replace("/^[^,]+,/", "", $_SESSION['CurrentMainBase']);
476         $dep_id = $this->ui->get_module_departments($this->module);
477         if(in_array_ics($base_back,$dep_id)){
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                         }
491       $_SESSION['CurrentMainBase'] = $this->selectedBase;
492                 }
494                 if(isset($_POST['MultiSelectWindow'.$this->filterName])){
496       /* Save some additional vars */
497       foreach($this->SaveAdditionalVars as $name){
498         if(isset($_POST[$name])){
499           if(isset($this->$name)){
500             $this->$name = $_POST[$name];
501             $_SESSION['MultiDialogFilters'][$this->filterName][$name] = $_POST[$name];
502           }
503         }
504       }
506                         /* Check posts from checkboxes 
507                          */
508                         foreach($this->array_Checkboxes as $key => $box){
509                                 if(isset($_POST[$box['name']])){
510                                         $this->array_Checkboxes[$key]['default'] = true;
511                                         $this->$box['name'] = true;
512                                 }else{
513                                         $this->array_Checkboxes[$key]['default'] = false;
514                                         $this->$box['name'] = false;
515                                 }
516                                 /* Save settings in out session */
517                                 $_SESSION['MultiDialogFilters'][$this->filterName][$box['name']] = $this->$box['name'];
518                         }
520                         /* Check regex posts */
521                         foreach($this->array_Regexes as $key => $box){
522                                 $this->array_Regexes[$key]['value'] = $_POST[$box['name']];
523                                 $this->$box['name'] = $_POST[$box['name']];
524                                 $_SESSION['MultiDialogFilters'][$this->filterName][$box['name']] = $this->$box['name'];
525                         }
527                         /* call close/save if buttons are pressed */
528                         if(isset($_POST['CloseMultiSelectWindow'])){
529                                 $this->Close();
530                         }
532                         if(isset($_POST['SaveMultiSelectWindow'])){
533                                 $this->Save();
534                         }
535                 }
537                 /* check for alphabet selection
538                    Check which regexes are connected to the alphabet 
539                  */     
540                 if(isset($_GET['search'])){
541                         foreach($this->array_Regexes as $key => $box){
542                                 /* check if this regex is connected to the alphabet selection */
543                                 if(($box['connAlpha'])&&(isset($_GET['search']))){
544                                         $val =  $_GET['search']."*";
545                                         $val = preg_replace("/\*\**/","*",$val);
546                                         $this->array_Regexes[$key]['value'] = $val;
547                                         $this->$box['name'] = $val;
548           $_SESSION['MultiDialogFilters'][$this->filterName][$box['name']] =  $val;
549                                 }
550                         }
551                 }
552         }
555   /* Create header snapshot value */
556   function get_snapshot_header()
557   {
558     $str = "&nbsp;";
559     if($this->parent->snapshotEnabled()){
560       $ok = false;
561       foreach($this->parent->get_used_snapshot_bases() as $base){
562         $ok |= count($this->parent->getAllDeletedSnapshots($base)) >= 1 ;
563       }
564       if($ok){
565         $str = "<input class='center' type='image' align='middle' src='images/restore.png'
566           title='"._("Restore snapshopts of already deleted objects")."' 
567           alt='"._("Restore")."' name='RestoreDeletedSnapShot'>&nbsp;"; 
568       }else{
569         $str = "<img class='center' src='images/restore_grey.png' alt=''>&nbsp;";
570       }
572 #      $str .= "<img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;";
573     }
574     return($str);
575   }
578   function GetSnapShotActions($dn)
579   {
580     $str= "";
581     if($this->parent->snapshotEnabled()){
583       if(count($this->parent->Available_SnapsShots($dn))){
584         $str.= "<input class='center' type='image' src='images/restore.png'
585           alt='"._("Restore snapshot")."' name='RestoreSnapShotDialog_".base64_encode($dn)."' title='"._("Restore snapshot")."'>&nbsp;";
586       } else {
587         $str = "<img class='center' src='images/restore_grey.png' alt=''>&nbsp;";
588       }
590       $str.= "<input class='center' type='image' src='images/snapshot.png'
591         alt='"._("Create snapshot")."' name='CreateSnapShotDialog_".base64_encode($dn)."' title='"._("Create a new snapshot from this object")."'>&nbsp;";
592     }
594     return($str);
595   }
598         /* this function adds the sub-departments of the current tree to the list */
599         function AddDepartments($base = false,$numtabs = 3)
600         {
601                 $this->DepartmentsAdded = true;
603                 /* check for a valid base */
604                 if(!$base){
605                         if(!isset($_SESSION['CurrentMainBase'])){
606                                 $_SESSION['CurrentMainBase'] = $this->config->current['BASE'];
607                         }
608                         $base = $_SESSION['CurrentMainBase'];
609                 }
611                 /* Create ldap obj and switch into base*/
612                 $ldap = $this->config->get_ldap_link();
613                 $ldap->cd($base);
615                 /* reset current deps */
616                 $this->departments = array();
618                 /* Get all departments within this subtree */
619                 $deps= get_list("(&(|(ou=*)(description=*))(objectClass=gosaDepartment))", $this->module, $base,
620                     array("ou", "description"), GL_SIZELIMIT | GL_CONVERT);
622                 /* Edit delete link for system types
623                  */
624                 $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=dep_open&amp;dep_id=%s'>%s</a>";
626                 /* Create an array with all visible (in the list) departments */
627                 $departments = array();
628                 foreach($deps as $value){
629                         if(isset($value['description'][0])){
630                                 $this->departments[$value['dn']]= get_sub_department($value['dn'])." - [".$value["description"][0]."]";
631                         }else{
632                                 $this->departments[$value['dn']]= get_sub_department($value['dn']);
633                         }
634                 }
635                 natcasesort($this->departments);
637                 /* Add deps to this dialog object list */
638                 foreach($this->departments as $key=> $val){
640                         /* Add missing entries ... */
641                         if(!isset($this->config->departments[trim($key)])){
642                                 $this->config->departments[trim($key)]="";
643                         }
645                         /* check if this department contains sub-departments
646                            Display different image in this case
647                          */
648                         $non_empty="";
649                         $nkey= normalizePreg($key);
650                         foreach($this->config->departments as $keyd=>$vald ){
651                                 if(preg_match("/$nkey\/.*/",$keyd)){
652                                         $non_empty="full";
653                                 }
654                         }
656                         /* Add to divlist */
657                         $row = array();
658                         $row[]=$field1=array("string"=>"<img src='images/".$non_empty."folder.png' alt='department'>","attach"=>"style='text-align:center;width:20px;'");
659                         $row[]=$field2=array("string"=>sprintf($linkopen,base64_encode($key),$val), "attach" => "style=''");
661                         if($numtabs > 2){       
662                                 for($i = 2 ; $i <$numtabs;$i++){
663           if(isset( $this->array_Header[$i]['attach'])){
664             $row[] = array("string"=>"&nbsp;","attach" => $this->array_Header[$i]['attach']);
665           }else{
666             $row[] = array("string"=>"&nbsp;");
667           }
668                                 }
669                         }
671                         $this->AddElement($row);
672                 }
673         }
675 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
676 ?>