Code

Smart workaround for broken dn's
[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 
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                 $this->selectedBase = $_SESSION['CurrentMainBase'];
84     /* Check default values for SaveAdditionalVars */
85     foreach($this->SaveAdditionalVars as $name){
86       if(isset($_SESSION['MultiDialogFilters'][$this->filterName][$name])){
87         $this->$name = $_SESSION['MultiDialogFilters'][$this->filterName][$name];
88       }
89     }
90     
91         }
93         /* Enables the headpage mode, which changes the list look */
94         function SetHeadpageMode()
95         {
96                 $this->is_headpage = true;
97         }
99         /* Sets the List internal name (not displayed anywhere) 
100            it is used to identify every single list
101          */     
102         function SetTitle($str)
103         {
104                 $this->string_Title = $str;
105         }
107         /* Set the list header string  */
108         function SetListHeader($str)
109         {
110                 $this->string_ListHeader = $str;
111         }
113         /* This sets the list description which is the first gray bar on top of the list */
114         function SetSummary($str)
115         {
116                 $this->string_Summary = $str;
117         }
119         /* If the save button is enabled, you can change its caption with this function */      
120         function SetSaveButtonString($str)
121         {
122                 $this->SaveButtonString = $str;
123         }
125         /* If the close button is enabled, you can change its caption with this function */     
126         function SetCloseButtonString($str)
127         {
128                 $this->CloseButtonString = $str;
129         }
131         /* With this function you can change the text of the information box */
132         function SetInformation($str)
133         {
134                 $this->string_Information = $str;
135         }
137         /* Display the alphabet selection box*/
138         function EnableAplhabet($bool)
139         {
140                 $this->bool_DisplayAlpahabet = $bool;
141         }
143         /* Add additional header col */
144         function AddHeader($arr)
145         {
146                 $this->array_Header[] = $arr;
147         }
149         /* add additional List element */
150         function AddElement($arr)
151         {
152                 $this->array_Elements[] = $arr;
153         }
155         /* Add a checkbox to the filter element,
156            the name specifies an existing class var to store the 'selection' */
157         function AddCheckBox($name,$value="Unset",$string="Unset",$default=false)
158         {
159                 $arr = array();
161                 if($name == SEPERATOR){
162                         $arr['name'] = SEPERATOR;
163                 }else{
164                         /* Check if there was already a variable 
165                            for this dialog which we should use instead of the default*/
166                         if(isset($_SESSION['MultiDialogFilters'][$this->filterName][$name])){
167                                 $arr['default'] = $_SESSION['MultiDialogFilters'][$this->filterName][$name];
168                                 $this->$name = $arr['default'];
169                         }else{
170                                 $arr['default'] = $default; 
171         $this->$name = $default;
172                         }
173                         $arr['name']          = $name;
174                         $arr['string']      = $string;
175                         $arr['value']         = $value;
176                         $arr['enabled']     = true;
178                 }
179                 $this->array_Checkboxes[] = $arr;
180         }
183         /* Hides or unhides the checkbox with the given name */
184         function DisableCheckBox($name,$HideShow = false)
185         {
186                 foreach($this->array_Checkboxes as $key => $chkbox){
187                         if($chkbox['name'] == $name){
188                                 $this->array_Checkboxes[$key]['enabled'] = $HideShow;
189                         }
190                 }
191         }
194         /* Returns true if the close button was pressed */
195         function isClosed()
196         {
197                 return($this->is_closed);
198         }
200         /* Enable the close button */
201         function EnableCloseButton($bool)
202         {
203                 $this->bool_DisplayCloseButton = $bool;
204         }
206         /* Enable the save button on the bottom of the list*/
207         function EnableSaveButton ($bool)
208         {
209                 $this->bool_DisplaySaveButton = $bool;
210         }
212   /* Add a list specific filter object to position 
213       1 on top of Information 
214       2 Between Information && Filter
215       3 Below the Filter Part */
216   function AddUserBoxToFilter($position)
217   {
218     return("");
219   }
221         /* Draw the list with all list elements and filters */
222         function Draw()
223         {
225                 /* Check for exeeded sizelimit */
226                 if (($message= check_sizelimit()) != ""){
227                         return($message);
228                 }
230                 $smarty = get_smarty();
231                 $smarty->assign("UserBox1",$this->AddUserBoxToFilter(1));
232                 $smarty->assign("UserBox2",$this->AddUserBoxToFilter(2));
233                 $smarty->assign("UserBox3",$this->AddUserBoxToFilter(3));
235                 $divlist = new divlist($this->string_Title);
236                 $divlist->SetSummary($this->string_Summary);
237                 $divlist->SetEntriesPerPage(0); // 0 for scrollable list 
238   
239     if($this->DivHeight != ""){
240       $divlist->SetHeight($this->DivHeight);
241     }
243                 /* set Header informations 
244                  */
245                 $header = array();
246                 foreach($this->array_Header as $head){
247                         $header[] = $head;
248                 }
249                 $divlist->SetHeader($header);
251                 /* set Entries 
252                  */
253                 $elements = array();
254                 foreach($this->array_Elements as $element){
255                         $divlist->AddEntry($element);
256                 }
258                 /* Create checkboxes fields 
259                  */
260                 $boxes = "";
261                 $boxClick = " onClick='document.mainform.submit();' ";
262                 foreach($this->array_Checkboxes as $box){
264                         if($box['name'] == SEPERATOR){
265                                 $boxes .= "</td></tr></table><table style='width:100%;border-top:1px solid #B0B0B0;'><tr><td>";
266                                 continue;
267                         }
269                         /* Skip disabled boxes */
270                         if(!$box['enabled']) continue;
272                         /* Check if box is checked */
273                         if($box['default'] == true){
274                                 $boxes .="<input type='checkbox' name='".$box['name']."' value='1' title='".$box['value']."' checked ".$boxClick.">&nbsp;".$box['string']."<br>";
275                         }else{
276                                 $boxes .="<input type='checkbox' name='".$box['name']."' value='1' title='".$box['value']."'".$boxClick.";>&nbsp;".$box['string']."<br>";
277                         }
278                 }
279                 $smarty->assign("CheckBoxes", $boxes);
281                 /* Assign regex fields 
282                  */
283                 $regexes = "";
284                 foreach($this->array_Regexes as $regex){
285                         $regexes.="<table summary=\"\" style=\"width:100%;border-top:1px solid #B0B0B0;\">
286                                 <tr>
287                                 <td>
288                                 <label for=\"".$regex['name']."\">
289                                 <img alt=\"".$regex['string']."\" src=\"".$regex['image']."\" align=middle>
290                                 </label>
291                                 </td>
292                                 <td width=\"99%\">
293                                 <input type='text' style='width:99%' name='".$regex['name']."' maxlength='20'
294                                 value='".$regex['value']."' title=\"".$regex['string']."\"> 
295                                 </td>
296                                 </tr>
297                                 </table>";
298                 }
299                 $smarty->assign("regexes"                       , $regexes );
301     /* Hide Filter Part if Requested or empty */
302     if((empty($boxes)) && (empty($regexes)) || ($this->HideFilterPart)){
303                   $smarty->assign("Skip_Filter_Part",   true);
304     }else{ 
305                   $smarty->assign("Skip_Filter_Part",   false);
306     }
308                 /* Assign alphabet and display it 
309                  */     
310                 $smarty->assign("Display_alphabet",     $this->bool_DisplayAlpahabet);
311                 $smarty->assign("alphabet",             generate_alphabet());
312                 $smarty->assign("Header"                        , $this->string_ListHeader );
313                 $smarty->assign("Summary"                       , $this->string_Summary);
314                 $smarty->assign("Title"                         , $this->string_Title);
315                 $smarty->assign("Information"           , $this->string_Information);
317                 /* Check for exeeded sizelimit */
318                 $smarty->assign("hint"                          , print_sizelimit_warning());
319                 $smarty->assign("DivList"                       , $divlist->DrawList());
322                 if($this->is_headpage){
323                         $smarty->assign("tree_image",           get_template_path('images/tree.png'));
324                         $smarty->assign("infoimage",            get_template_path('images/info.png'));
325                         $smarty->assign("launchimage",          get_template_path('images/launch.png'));
326                         $smarty->assign("apply",                        apply_filter());
327                 }else{
328                         $smarty->assign("tree_image",           get_template_path('images/tree.png'));
329                         $smarty->assign("infoimage",            get_template_path('images/info_small.png'));
330                         $smarty->assign("launchimage",          get_template_path('images/rocket.png'));
331                         $smarty->assign("apply",                        apply_filter());
332                 }
334                 /* Button handling */
335                 $smarty->assign("SaveButtonString" ,$this->SaveButtonString);
336                 $smarty->assign("CloseButtonString",$this->CloseButtonString);
338                 $smarty->assign("Display_Close",        $this->bool_DisplayCloseButton);
339                 $smarty->assign("Display_Save" ,        $this->bool_DisplaySaveButton);
341                 $smarty->assign("filterName"    ,       $this->filterName);
342                 $smarty->assign("is_headpage"   ,       $this->is_headpage);
344                 $display = $smarty->fetch(get_template_path("MultiSelectWindow.tpl"));
345                 return($display);
346         }
348         /* Set the close var, which simulates the close button is pressed */
349         function Close()
350         {
351                 $this->is_closed = true;
352         }
354         function Save()
355         {
356                 $this->is_saved = true;
357         }
359         /* Store all checkboxes/ regexes ... 
360            Store data also into a session var, to keep the checkboxes check after reload  */
361         function save_object()
362         {
363     /* Get up to date config */
364     $this->config = $this->parent->config;
366                 /* Update current base */
367                 if($this->DepartmentsAdded){
368                         $s_action ="";
369                         foreach($_POST as $key => $value){
370                                 if(preg_match("/^dep_back.*/i",$key)){
371                                         $s_action="back";
372                                 }elseif(preg_match("/^dep_root.*/",$key)){
373                                         $s_action="root";
374                                 }elseif(preg_match("/^dep_home.*/i",$key)){
375                                         $s_action="home";
376                                 }
377                         }
379                         /* Save base selection from headpage selectbox*/
380                         if(isset($_POST['CurrentMainBase'])){
381                                 $this->selectedBase = $_POST['CurrentMainBase'];
382                         }
384                         /* Homebutton is posted */
385                         if($s_action=="home"){
386         $ui= get_userinfo();
387         $base = get_base_from_people($ui->dn);
388                                 $this->selectedBase= $base;
389                         }
391                         /* Open selected department
392                            this is posted by the parent class MultiSelectWindow */
393                         if(isset($_GET['act'])&& ($_GET['act'] == "dep_open")){
394                                 $s_entry = base64_decode($_GET['dep_id']);
395         if (!isset($this->config->departments[$s_entry])){
396           print_red(_("Error: The requested subtree has an inconsistent DN encoding, check your LDAP!"));
397         } else {
398                                 $this->selectedBase = $this->config->departments[$s_entry];
399         }
400                         }
402                         /* back to the roots ^^ */
403                         if($s_action=="root"){
404                                 $this->selectedBase=($this->config->current['BASE']);
405                         }
408                         /* If Back-button is pressed, move back one step in DN */
409                         if($s_action=="back"){
410                                 //FIXME: This is not 100% correct. We'll only display ou's, but there may be
411                                 //       a step between. You'll stumble in a "hidden" department in this case.
412                                 $base_back= preg_replace("/^[^,]+,/", "", $_SESSION['CurrentMainBase']);
414                                 /* The department array keeps non DN entries as index. We need to convert
415                                    it before checking the existance. */
416                                 $base_back= trim(convert_department_dn($base_back));
418                                 /* Check if the department exists, otherwise revert to the configure base DN */
419                                 if(isset($this->config->departments[$base_back])){
420                                         $this->selectedBase= $this->config->departments[$base_back];
421                                 }else{
422                                         $this->selectedBase= $this->config->departments['/'];
423                                 }
424                         }
425       $_SESSION['CurrentMainBase'] = $this->selectedBase;
426                 }
428                 if(isset($_POST['MultiSelectWindow'.$this->filterName])){
430       /* Save some additional vars */
431       foreach($this->SaveAdditionalVars as $name){
432         if(isset($_POST[$name])){
433           if(isset($this->$name)){
434             $this->$name = $_POST[$name];
435             $_SESSION['MultiDialogFilters'][$this->filterName][$name] = $_POST[$name];
436           }
437         }
438       }
440                         /* Check posts from checkboxes 
441                          */
442                         foreach($this->array_Checkboxes as $key => $box){
443                                 if(isset($_POST[$box['name']])){
444                                         $this->array_Checkboxes[$key]['default'] = true;
445                                         $this->$box['name'] = true;
446                                 }else{
447                                         $this->array_Checkboxes[$key]['default'] = false;
448                                         $this->$box['name'] = false;
449                                 }
450                                 /* Save settings in out session */
451                                 $_SESSION['MultiDialogFilters'][$this->filterName][$box['name']] = $this->$box['name'];
452                         }
454                         /* Check regex posts */
455                         foreach($this->array_Regexes as $key => $box){
456                                 $this->array_Regexes[$key]['value'] = $_POST[$box['name']];
457                                 $this->$box['name'] = $_POST[$box['name']];
458                                 $_SESSION['MultiDialogFilters'][$this->filterName][$box['name']] = $this->$box['name'];
459                         }
461                         /* call close/save if buttons are pressed */
462                         if(isset($_POST['CloseMultiSelectWindow'])){
463                                 $this->Close();
464                         }
466                         if(isset($_POST['SaveMultiSelectWindow'])){
467                                 $this->Save();
468                         }
469                 }
471                 /* check for alphabet selection
472                    Check which regexes are connected to the alphabet 
473                  */     
474                 if(isset($_GET['search'])){
475                         foreach($this->array_Regexes as $key => $box){
476                                 /* check if this regex is connected to the alphabet selection */
477                                 if(($box['connAlpha'])&&(isset($_GET['search']))){
478                                         $val =  $_GET['search']."*";
479                                         $val = preg_replace("/\*\**/","*",$val);
480                                         $this->array_Regexes[$key]['value'] = $val;
481                                         $this->$box['name'] = $val;
482           $_SESSION['MultiDialogFilters'][$this->filterName][$box['name']] =  $val;
483                                 }
484                         }
485                 }
486         }
489   /* Create header snapshot value */
490   function get_snapshot_header()
491   {
492     $str = "&nbsp;";
493     if($this->parent->snapshotEnabled()){
494       $ok = false;
495       foreach($this->parent->get_used_snapshot_bases() as $base){
496         $ok |= count($this->parent->getAllDeletedSnapshots($base)) >= 1 ;
497       }
498       if($ok){
499         $str = "<input class='center' type='image' align='middle' src='images/restore.png'
500           title='"._("Restore snapshopts of already deleted objects")."' 
501           alt='"._("Restore")."' name='RestoreDeletedSnapShot'>&nbsp;"; 
502       }else{
503         $str = "<img class='center' src='images/restore_grey.png'>&nbsp;";
504       }
506       $str .= "<img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;";
507     }
508     return($str);
509   }
512   function GetSnapShotActions($dn)
513   {
514     $str= "";
515     if($this->parent->snapshotEnabled()){
517       if(count($this->parent->Available_SnapsShots($dn))){
518         $str.= "<input class='center' type='image' src='images/restore.png'
519           alt='"._("Restore snapshot")."' name='RestoreSnapShotDialog_".base64_encode($dn)."' title='"._("Restore snapshot")."'>&nbsp;";
520       } else {
521         $str = "<img class='center' src='images/restore_grey.png'>&nbsp;";
522       }
524       $str.= "<input class='center' type='image' src='images/snapshot.png'
525         alt='"._("Create snapshot")."' name='CreateSnapShotDialog_".base64_encode($dn)."' title='"._("Create a new snapshot from this object")."'>&nbsp;";
526     }
528     return($str);
529   }
532         /* this function adds the sub-departments of the current tree to the list */
533         function AddDepartments($base = false,$numtabs = 3)
534         {
535                 $this->DepartmentsAdded = true;
537                 /* check for a valid base */
538                 if(!$base){
539                         if(!isset($_SESSION['CurrentMainBase'])){
540                                 $_SESSION['CurrentMainBase'] = $this->config->current['BASE'];
541                         }
542                         $base = $_SESSION['CurrentMainBase'];
543                 }
545                 /* Create ldap obj and switch into base*/
546                 $ldap = $this->config->get_ldap_link();
547                 $ldap->cd($base);
549                 /* reset current deps */
550                 $this->departments = array();
552                 /* Get all departments within this subtree */
553                 $deps= get_list("(&(|(ou=*)(description=*))(objectClass=gosaDepartment))", $this->ui->subtreeACL,
554                                 $base, array("ou", "description"), GL_SIZELIMIT | GL_CONVERT);
556                 /* Edit delete link for system types
557                  */
558                 $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=dep_open&amp;dep_id=%s'>%s</a>";
560                 /* Create an array with all visible (in the list) departments */
561                 $departments = array();
562                 foreach($deps as $value){
563                         if(isset($value['description'][0])){
564                                 $this->departments[$value['dn']]= get_sub_department($value['dn'])." - [".$value["description"][0]."]";
565                         }else{
566                                 $this->departments[$value['dn']]= get_sub_department($value['dn']);
567                         }
568                 }
569                 natcasesort($this->departments);
571                 /* Add deps to this dialog object list */
572                 foreach($this->departments as $key=> $val){
573                         /* Add missing entries ... */
574                         if(!isset($this->config->departments[trim($key)])){
575                                 $this->config->departments[trim($key)]="";
576                         }
578                         /* check if this department contains sub-departments
579                            Display different image in this case
580                          */
581                         $non_empty="";
582                         $nkey= normalizePreg($key);
583                         foreach($this->config->departments as $keyd=>$vald ){
584                                 if(preg_match("/$nkey\/.*/",$keyd)){
585                                         $non_empty="full";
586                                 }
587                         }
589                         /* Add to divlist */
590                         $row = array();
591                         $row[]=$field1=array("string"=>"<img src='images/".$non_empty."folder.png' alt='department'>","attach"=>"style='text-align:center;width:20px;'");
592                         $row[]=$field2=array("string"=>sprintf($linkopen,base64_encode($key),$val), "attach" => "style=''");
594                         if($numtabs > 2){       
595                                 for($i = 2 ; $i <$numtabs;$i++){
596           if(isset( $this->array_Header[$i]['attach'])){
597             $row[] = array("string"=>"&nbsp;","attach" => $this->array_Header[$i]['attach']);
598           }else{
599             $row[] = array("string"=>"&nbsp;");
600           }
601                                 }
602                         }
604                         $this->AddElement($row);
605                 }
606         }
608 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
609 ?>