Code

Updated to regex support
[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);
170     if(!count($deps) || $deps[0] == $this->selectedBase){
171       $enable_back = FALSE;
172       $enable_root = FALSE;
173     }
174    
175     /* Check if we are in users home  department */ 
176     if(!count($deps) ||$this->selectedBase == get_base_from_people($ui->dn)){
177       $enable_home = FALSE;
178     }
180     /* Create header with selected base */
181     $listhead = "<div style='background:#F0F0F9;padding:5px;'>";
183     /* Draw root button */
184     if($enable_root){
185       $listhead .= " <input class='center' type='image' src='images/list_root.png' align='middle'
186         title='"._("Go to root department")."' name='dep_root' alt='"._("Root")."'>&nbsp;";
187     }else{
188       $listhead .= " <img src='images/list_root_gray.png' class='center' alt='"._("Root")."'>&nbsp;";
189     }
191     /* Draw back button */
192     if($enable_back){
193       $listhead .= " <input class='center' type='image' align='middle' src='images/list_back.png'
194         title='"._("Go up one department")."' alt='"._("Up")."'                name='dep_back'>&nbsp;";
195     }else{
196       $listhead .= " <img src='images/list_back_gray.png' class='center' alt='"._("Up")."'>&nbsp;";
197     }
199     /* Draw home button */
200     if($enable_home){
201       $listhead .= " <input class='center' type='image' align='middle' src='images/list_home.png'
202         title='"._("Go to users department")."' alt='"._("Home")."'            name='dep_home'>&nbsp;";
203     }else{
204       $listhead .= " <img src='images/list_home_gray.png' class='center' alt='"._("Home")."'>&nbsp;";
205     }
206    
207     /* And at least draw reload button, this button is enabled everytime */ 
208     $listhead .=  " <input class='center' type='image' src='images/list_reload.png' align='middle'
209       title='"._("Reload list")."' name='submit_department' alt='"._("Submit")."'>&nbsp;".
210       " <img   class='center' src='images/list_seperator.png' align='middle' alt='-' height='16' width='1'>&nbsp;";
212     return ($listhead);
213   }
215         /* Add a checkbox to the filter element,
216            the name specifies an existing class var to store the 'selection' */
217         function AddCheckBox($name,$value="Unset",$string="Unset",$default=false)
218         {
219                 $arr = array();
221                 if($name == SEPERATOR){
222                         $arr['name'] = SEPERATOR;
223                 }else{
224                         /* Check if there was already a variable 
225                            for this dialog which we should use instead of the default*/
226                         if(isset($_SESSION['MultiDialogFilters'][$this->filterName][$name])){
227                                 $arr['default'] = $_SESSION['MultiDialogFilters'][$this->filterName][$name];
228                                 $this->$name = $arr['default'];
229                         }else{
230                                 $arr['default'] = $default; 
231         $this->$name = $default;
232                         }
233                         $arr['name']          = $name;
234                         $arr['string']      = $string;
235                         $arr['value']         = $value;
236                         $arr['enabled']     = true;
238                 }
239                 $this->array_Checkboxes[] = $arr;
240         }
243         /* Hides or unhides the checkbox with the given name */
244         function DisableCheckBox($name,$HideShow = false)
245         {
246                 foreach($this->array_Checkboxes as $key => $chkbox){
247                         if($chkbox['name'] == $name){
248                                 $this->array_Checkboxes[$key]['enabled'] = $HideShow;
249                         }
250                 }
251         }
254         /* Returns true if the close button was pressed */
255         function isClosed()
256         {
257                 return($this->is_closed);
258         }
260         /* Enable the close button */
261         function EnableCloseButton($bool)
262         {
263                 $this->bool_DisplayCloseButton = $bool;
264         }
266         /* Enable the save button on the bottom of the list*/
267         function EnableSaveButton ($bool)
268         {
269                 $this->bool_DisplaySaveButton = $bool;
270         }
272   /* Add a list specific filter object to position 
273       1 on top of Information 
274       2 Between Information && Filter
275       3 Below the Filter Part */
276   function AddUserBoxToFilter($position)
277   {
278     return("");
279   }
281         /* Draw the list with all list elements and filters */
282         function Draw()
283         {
285                 /* Check for exeeded sizelimit */
286                 if (($message= check_sizelimit()) != ""){
287                         return($message);
288                 }
290                 $smarty = get_smarty();
291                 $smarty->assign("UserBox1",$this->AddUserBoxToFilter(1));
292                 $smarty->assign("UserBox2",$this->AddUserBoxToFilter(2));
293                 $smarty->assign("UserBox3",$this->AddUserBoxToFilter(3));
295                 $divlist = new divlist($this->string_Title);
296                 $divlist->SetSummary($this->string_Summary);
297                 $divlist->SetEntriesPerPage(0); // 0 for scrollable list 
298   
299     if($this->DivHeight != ""){
300       $divlist->SetHeight($this->DivHeight);
301     }
303                 /* set Header informations 
304                  */
305                 $header = array();
306                 foreach($this->array_Header as $head){
307                         $header[] = $head;
308                 }
309                 $divlist->SetHeader($header);
311                 /* set Entries 
312                  */
313                 $elements = array();
314                 foreach($this->array_Elements as $element){
315                         $divlist->AddEntry($element);
316                 }
318                 /* Create checkboxes fields 
319                  */
320                 $boxes = "";
321                 $boxClick = " onClick='document.mainform.submit();' ";
322                 foreach($this->array_Checkboxes as $box){
324                         if($box['name'] == SEPERATOR){
325                                 $boxes .= "</td></tr></table><table style='width:100%;border-top:1px solid #B0B0B0;'><tr><td>";
326                                 continue;
327                         }
329                         /* Skip disabled boxes */
330                         if(!$box['enabled']) continue;
332                         /* Check if box is checked */
333                         if($box['default'] == true){
334                                 $boxes .="<input type='checkbox' name=\"".$box['name']."\" value='1' title=\"".htmlentities($box['value'])."\" checked ".$boxClick.">&nbsp;".$box['string']."<br>";
335                         }else{
336                                 $boxes .="<input type='checkbox' name=\"".$box['name']."\" value='1' title=\"".htmlentities($box['value'])."\" ".$boxClick.">&nbsp;".$box['string']."<br>";
337                         }
338                 }
339                 $smarty->assign("CheckBoxes", $boxes);
341                 /* Assign regex fields 
342                  */
343                 $regexes = "";
344                 foreach($this->array_Regexes as $regex){
345                         $regexes.="<table summary=\"\" style=\"width:100%;border-top:1px solid #B0B0B0;\">
346                                 <tr>
347                                 <td>
348                                 <label for=\"".$regex['name']."\">
349                                 <img alt=\"".$regex['string']."\" src=\"".$regex['image']."\" align=middle>
350                                 </label>
351                                 </td>
352                                 <td width=\"99%\">
353                                 <input type=\"text\" style='width:99%' name=\"".$regex['name']."\" maxlength='20'
354                                 value=\"".htmlentities($regex['value'])."\" title=\"".htmlentities($regex['string'])."\"> 
355                                 </td>
356                                 </tr>
357                                 </table>";
358                 }
359                 $smarty->assign("regexes"                       , $regexes );
361     /* Hide Filter Part if Requested or empty */
362     if((empty($boxes)) && (empty($regexes)) || ($this->HideFilterPart)){
363                   $smarty->assign("Skip_Filter_Part",   true);
364     }else{ 
365                   $smarty->assign("Skip_Filter_Part",   false);
366     }
368                 /* Assign alphabet and display it 
369                  */     
370                 $smarty->assign("Display_alphabet",     $this->bool_DisplayAlpahabet);
371                 $smarty->assign("alphabet",             generate_alphabet());
372                 $smarty->assign("Header"                        , $this->string_ListHeader );
373                 $smarty->assign("Summary"                       , $this->string_Summary);
374                 $smarty->assign("Title"                         , $this->string_Title);
375                 $smarty->assign("Information"           , $this->string_Information);
377                 /* Check for exeeded sizelimit */
378                 $smarty->assign("hint"                          , print_sizelimit_warning());
379                 $smarty->assign("DivList"                       , $divlist->DrawList());
382                 if($this->is_headpage){
383                         $smarty->assign("tree_image",           get_template_path('images/tree.png'));
384                         $smarty->assign("infoimage",            get_template_path('images/info.png'));
385                         $smarty->assign("launchimage",          get_template_path('images/launch.png'));
386                         $smarty->assign("apply",                        apply_filter());
387                 }else{
388                         $smarty->assign("tree_image",           get_template_path('images/tree.png'));
389                         $smarty->assign("infoimage",            get_template_path('images/info_small.png'));
390                         $smarty->assign("launchimage",          get_template_path('images/rocket.png'));
391                         $smarty->assign("apply",                        apply_filter());
392                 }
394                 /* Button handling */
395                 $smarty->assign("SaveButtonString" ,$this->SaveButtonString);
396                 $smarty->assign("CloseButtonString",$this->CloseButtonString);
398                 $smarty->assign("Display_Close",        $this->bool_DisplayCloseButton);
399                 $smarty->assign("Display_Save" ,        $this->bool_DisplaySaveButton);
401                 $smarty->assign("filterName"    ,       $this->filterName);
402                 $smarty->assign("is_headpage"   ,       $this->is_headpage);
404                 $display = $smarty->fetch(get_template_path("MultiSelectWindow.tpl"));
405                 return($display);
406         }
408         /* Set the close var, which simulates the close button is pressed */
409         function Close()
410         {
411                 $this->is_closed = true;
412         }
414         function Save()
415         {
416                 $this->is_saved = true;
417         }
419         /* Store all checkboxes/ regexes ... 
420            Store data also into a session var, to keep the checkboxes check after reload  */
421         function save_object()
422         {
423     /* Get up to date config */
424     if(isset($this->parent->config)){
425       $this->config = $this->parent->config;
426     }
428                 /* Update current base */
429                 if($this->DepartmentsAdded){
430                         $s_action ="";
431                         foreach($_POST as $key => $value){
432                                 if(preg_match("/^dep_back.*/i",$key)){
433                                         $s_action="back";
434                                 }elseif(preg_match("/^dep_root.*/",$key)){
435                                         $s_action="root";
436                                 }elseif(preg_match("/^dep_home.*/i",$key)){
437                                         $s_action="home";
438                                 }
439                         }
441                         /* Save base selection from headpage selectbox*/
442                         if(isset($_POST['CurrentMainBase'])){
443                                 $this->selectedBase = $_POST['CurrentMainBase'];
444                         }
446                         /* Homebutton is posted */
447                         if($s_action=="home"){
448         $ui= get_userinfo();
449         $base = get_base_from_people($ui->dn);
450                                 $this->selectedBase= $base;
451                         }
453                         /* Open selected department
454                            this is posted by the parent class MultiSelectWindow */
455                         if(isset($_GET['act'])&& ($_GET['act'] == "dep_open")){
456                                 $s_entry = base64_decode($_GET['dep_id']);
457         if (!isset($this->config->departments[$s_entry])){
458           print_red(_("Error: The requested subtree has an inconsistent DN encoding, check your LDAP!"));
459         } else {
460                                 $this->selectedBase = $this->config->departments[$s_entry];
461         }
462                         }
464                         /* back to the roots ^^ */
465                         if($s_action=="root"){
466                                 $this->selectedBase=($this->config->current['BASE']);
467                           $dep_id = $this->ui->get_module_departments($this->module);
468         $this->selectedBase = $dep_id[key($dep_id)];
469       }
472       /* If Back-button is pressed, move back one step in DN */
473       if($s_action=="back"){
475         /* Get parent deprtment and check if we are allowed to step in it */
476         $base_back= preg_replace("/^[^,]+,/", "", $_SESSION['CurrentMainBase']);
477         $dep_id = $this->ui->get_module_departments($this->module);
478         if(in_array_ics($base_back,$dep_id)){
480           /* The department array keeps non DN entries as index. We need to convert
481              it before checking the existance. */
482           $base_back= trim(convert_department_dn($base_back));
484           /* Check if the department exists, otherwise revert to the configure base DN */
485           if(isset($this->config->departments[$base_back])){
486             $this->selectedBase= $this->config->departments[$base_back];
487           }else{
488             $this->selectedBase= $this->config->departments['/'];
489           }
490         }
491                         }
492       $_SESSION['CurrentMainBase'] = $this->selectedBase;
493                 }
495                 if(isset($_POST['MultiSelectWindow'.$this->filterName])){
497       /* Save some additional vars */
498       foreach($this->SaveAdditionalVars as $name){
499         if(isset($_POST[$name])){
500           if(isset($this->$name)){
501             $this->$name = $_POST[$name];
502             $_SESSION['MultiDialogFilters'][$this->filterName][$name] = $_POST[$name];
503           }
504         }
505       }
507                         /* Check posts from checkboxes 
508                          */
509                         foreach($this->array_Checkboxes as $key => $box){
510                                 if(isset($_POST[$box['name']])){
511                                         $this->array_Checkboxes[$key]['default'] = true;
512                                         $this->$box['name'] = true;
513                                 }else{
514                                         $this->array_Checkboxes[$key]['default'] = false;
515                                         $this->$box['name'] = false;
516                                 }
517                                 /* Save settings in out session */
518                                 $_SESSION['MultiDialogFilters'][$this->filterName][$box['name']] = $this->$box['name'];
519                         }
521                         /* Check regex posts */
522                         foreach($this->array_Regexes as $key => $box){
523                                 $this->array_Regexes[$key]['value'] = $_POST[$box['name']];
524                                 $this->$box['name'] = $_POST[$box['name']];
525                                 $_SESSION['MultiDialogFilters'][$this->filterName][$box['name']] = $this->$box['name'];
526                         }
528                         /* call close/save if buttons are pressed */
529                         if(isset($_POST['CloseMultiSelectWindow'])){
530                                 $this->Close();
531                         }
533                         if(isset($_POST['SaveMultiSelectWindow'])){
534                                 $this->Save();
535                         }
536                 }
538                 /* check for alphabet selection
539                    Check which regexes are connected to the alphabet 
540                  */     
541                 if(isset($_GET['search'])){
542                         foreach($this->array_Regexes as $key => $box){
543                                 /* check if this regex is connected to the alphabet selection */
544                                 if(($box['connAlpha'])&&(isset($_GET['search']))){
545                                         $val =  $_GET['search']."*";
546                                         $val = preg_replace("/\*\**/","*",$val);
547                                         $this->array_Regexes[$key]['value'] = $val;
548                                         $this->$box['name'] = $val;
549           $_SESSION['MultiDialogFilters'][$this->filterName][$box['name']] =  $val;
550                                 }
551                         }
552                 }
553         }
556   /* Create header snapshot value */
557   function get_snapshot_header()
558   {
559     $str = "&nbsp;";
560     if($this->parent->snapshotEnabled()){
561       $ok = false;
562       foreach($this->parent->get_used_snapshot_bases() as $base){
563         $ok |= count($this->parent->getAllDeletedSnapshots($base)) >= 1 ;
564       }
565       if($ok){
566         $str = "<input class='center' type='image' align='middle' src='images/restore.png'
567           title='"._("Restore snapshopts of already deleted objects")."' 
568           alt='"._("Restore")."' name='RestoreDeletedSnapShot'>&nbsp;"; 
569       }else{
570         $str = "<img class='center' src='images/restore_grey.png' alt=''>&nbsp;";
571       }
573 #      $str .= "<img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;";
574     }
575     return($str);
576   }
579   function GetSnapShotActions($dn)
580   {
581     $str= "";
582     if($this->parent->snapshotEnabled()){
584       if(count($this->parent->Available_SnapsShots($dn))){
585         $str.= "<input class='center' type='image' src='images/restore.png'
586           alt='"._("Restore snapshot")."' name='RestoreSnapShotDialog_".base64_encode($dn)."' title='"._("Restore snapshot")."'>&nbsp;";
587       } else {
588         $str = "<img class='center' src='images/restore_grey.png' alt=''>&nbsp;";
589       }
591       $str.= "<input class='center' type='image' src='images/snapshot.png'
592         alt='"._("Create snapshot")."' name='CreateSnapShotDialog_".base64_encode($dn)."' title='"._("Create a new snapshot from this object")."'>&nbsp;";
593     }
595     return($str);
596   }
599         /* this function adds the sub-departments of the current tree to the list */
600         function AddDepartments($base = false,$numtabs = 3)
601         {
602                 $this->DepartmentsAdded = true;
604                 /* check for a valid base */
605                 if(!$base){
606                         if(!isset($_SESSION['CurrentMainBase'])){
607                                 $_SESSION['CurrentMainBase'] = $this->config->current['BASE'];
608                         }
609                         $base = $_SESSION['CurrentMainBase'];
610                 }
612                 /* Create ldap obj and switch into base*/
613                 $ldap = $this->config->get_ldap_link();
614                 $ldap->cd($base);
616                 /* reset current deps */
617                 $this->departments = array();
619                 /* Get all departments within this subtree */
620                 $deps= get_list("(&(|(ou=*)(description=*))(objectClass=gosaDepartment))", $this->module, $base,
621                     array("ou", "description"), GL_SIZELIMIT | GL_CONVERT);
623                 /* Edit delete link for system types
624                  */
625                 $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=dep_open&amp;dep_id=%s'>%s</a>";
627                 /* Create an array with all visible (in the list) departments */
628                 $departments = array();
629                 foreach($deps as $value){
630                         if(isset($value['description'][0])){
631                                 $this->departments[$value['dn']]= get_sub_department($value['dn'])." - [".$value["description"][0]."]";
632                         }else{
633                                 $this->departments[$value['dn']]= get_sub_department($value['dn']);
634                         }
635                 }
636                 natcasesort($this->departments);
638                 /* Add deps to this dialog object list */
639                 foreach($this->departments as $key=> $val){
641                         /* Add missing entries ... */
642                         if(!isset($this->config->departments[trim($key)])){
643                                 $this->config->departments[trim($key)]="";
644                         }
646                         /* check if this department contains sub-departments
647                            Display different image in this case
648                          */
649                         $non_empty="";
650                         $nkey= normalizePreg($key);
651                         foreach($this->config->departments as $keyd=>$vald ){
652                                 if(preg_match("/$nkey\/.*/",$keyd)){
653                                         $non_empty="full";
654                                 }
655                         }
657                         /* Add to divlist */
658                         $row = array();
659                         $row[]=$field1=array("string"=>"<img src='images/".$non_empty."folder.png' alt='department'>","attach"=>"style='text-align:center;width:20px;'");
660                         $row[]=$field2=array("string"=>sprintf($linkopen,base64_encode($key),$val), "attach" => "style=''");
662                         if($numtabs > 2){       
663                                 for($i = 2 ; $i <$numtabs;$i++){
664           if(isset( $this->array_Header[$i]['attach'])){
665             $row[] = array("string"=>"&nbsp;","attach" => $this->array_Header[$i]['attach']);
666           }else{
667             $row[] = array("string"=>"&nbsp;");
668           }
669                                 }
670                         }
672                         $this->AddElement($row);
673                 }
674         }
676 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
677 ?>