Code

Made current main base set up more early
[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                 /* Set default base, to users base  */
83                 $this->selectedBase = $_SESSION['CurrentMainBase'];
85     /* Check default values for SaveAdditionalVars */
86     foreach($this->SaveAdditionalVars as $name){
87       if(isset($_SESSION['MultiDialogFilters'][$this->filterName][$name])){
88         $this->$name = $_SESSION['MultiDialogFilters'][$this->filterName][$name];
89       }
90     }
91     
92         }
94         /* Enables the headpage mode, which changes the list look */
95         function SetHeadpageMode()
96         {
97                 $this->is_headpage = true;
98         }
100         /* Sets the List internal name (not displayed anywhere) 
101            it is used to identify every single list
102          */     
103         function SetTitle($str)
104         {
105                 $this->string_Title = $str;
106         }
108         /* Set the list header string  */
109         function SetListHeader($str)
110         {
111                 $this->string_ListHeader = $str;
112         }
114         /* This sets the list description which is the first gray bar on top of the list */
115         function SetSummary($str)
116         {
117                 $this->string_Summary = $str;
118         }
120         /* If the save button is enabled, you can change its caption with this function */      
121         function SetSaveButtonString($str)
122         {
123                 $this->SaveButtonString = $str;
124         }
126         /* If the close button is enabled, you can change its caption with this function */     
127         function SetCloseButtonString($str)
128         {
129                 $this->CloseButtonString = $str;
130         }
132         /* With this function you can change the text of the information box */
133         function SetInformation($str)
134         {
135                 $this->string_Information = $str;
136         }
138         /* Display the alphabet selection box*/
139         function EnableAplhabet($bool)
140         {
141                 $this->bool_DisplayAlpahabet = $bool;
142         }
144         /* Add additional header col */
145         function AddHeader($arr)
146         {
147                 $this->array_Header[] = $arr;
148         }
150         /* add additional List element */
151         function AddElement($arr)
152         {
153                 $this->array_Elements[] = $arr;
154         }
156         /* Add a checkbox to the filter element,
157            the name specifies an existing class var to store the 'selection' */
158         function AddCheckBox($name,$value="Unset",$string="Unset",$default=false)
159         {
160                 $arr = array();
162                 if($name == SEPERATOR){
163                         $arr['name'] = SEPERATOR;
164                 }else{
165                         /* Check if there was already a variable 
166                            for this dialog which we should use instead of the default*/
167                         if(isset($_SESSION['MultiDialogFilters'][$this->filterName][$name])){
168                                 $arr['default'] = $_SESSION['MultiDialogFilters'][$this->filterName][$name];
169                                 $this->$name = $arr['default'];
170                         }else{
171                                 $arr['default'] = $default; 
172         $this->$name = $default;
173                         }
174                         $arr['name']          = $name;
175                         $arr['string']      = $string;
176                         $arr['value']         = $value;
177                         $arr['enabled']     = true;
179                 }
180                 $this->array_Checkboxes[] = $arr;
181         }
184         /* Hides or unhides the checkbox with the given name */
185         function DisableCheckBox($name,$HideShow = false)
186         {
187                 foreach($this->array_Checkboxes as $key => $chkbox){
188                         if($chkbox['name'] == $name){
189                                 $this->array_Checkboxes[$key]['enabled'] = $HideShow;
190                         }
191                 }
192         }
195         /* Returns true if the close button was pressed */
196         function isClosed()
197         {
198                 return($this->is_closed);
199         }
201         /* Enable the close button */
202         function EnableCloseButton($bool)
203         {
204                 $this->bool_DisplayCloseButton = $bool;
205         }
207         /* Enable the save button on the bottom of the list*/
208         function EnableSaveButton ($bool)
209         {
210                 $this->bool_DisplaySaveButton = $bool;
211         }
213   /* Add a list specific filter object to position 
214       1 on top of Information 
215       2 Between Information && Filter
216       3 Below the Filter Part */
217   function AddUserBoxToFilter($position)
218   {
219     return("");
220   }
222         /* Draw the list with all list elements and filters */
223         function Draw()
224         {
226                 /* Check for exeeded sizelimit */
227                 if (($message= check_sizelimit()) != ""){
228                         return($message);
229                 }
231                 $smarty = get_smarty();
232                 $smarty->assign("UserBox1",$this->AddUserBoxToFilter(1));
233                 $smarty->assign("UserBox2",$this->AddUserBoxToFilter(2));
234                 $smarty->assign("UserBox3",$this->AddUserBoxToFilter(3));
236                 $divlist = new divlist($this->string_Title);
237                 $divlist->SetSummary($this->string_Summary);
238                 $divlist->SetEntriesPerPage(0); // 0 for scrollable list 
239   
240     if($this->DivHeight != ""){
241       $divlist->SetHeight($this->DivHeight);
242     }
244                 /* set Header informations 
245                  */
246                 $header = array();
247                 foreach($this->array_Header as $head){
248                         $header[] = $head;
249                 }
250                 $divlist->SetHeader($header);
252                 /* set Entries 
253                  */
254                 $elements = array();
255                 foreach($this->array_Elements as $element){
256                         $divlist->AddEntry($element);
257                 }
259                 /* Create checkboxes fields 
260                  */
261                 $boxes = "";
262                 $boxClick = " onClick='document.mainform.submit();' ";
263                 foreach($this->array_Checkboxes as $box){
265                         if($box['name'] == SEPERATOR){
266                                 $boxes .= "</td></tr></table><table style='width:100%;border-top:1px solid #B0B0B0;'><tr><td>";
267                                 continue;
268                         }
270                         /* Skip disabled boxes */
271                         if(!$box['enabled']) continue;
273                         /* Check if box is checked */
274                         if($box['default'] == true){
275                                 $boxes .="<input type='checkbox' name='".$box['name']."' value='1' title='".$box['value']."' checked ".$boxClick.">&nbsp;".$box['string']."<br>";
276                         }else{
277                                 $boxes .="<input type='checkbox' name='".$box['name']."' value='1' title='".$box['value']."'".$boxClick.";>&nbsp;".$box['string']."<br>";
278                         }
279                 }
280                 $smarty->assign("CheckBoxes", $boxes);
282                 /* Assign regex fields 
283                  */
284                 $regexes = "";
285                 foreach($this->array_Regexes as $regex){
286                         $regexes.="<table summary=\"\" style=\"width:100%;border-top:1px solid #B0B0B0;\">
287                                 <tr>
288                                 <td>
289                                 <label for=\"".$regex['name']."\">
290                                 <img alt=\"".$regex['string']."\" src=\"".$regex['image']."\" align=middle>
291                                 </label>
292                                 </td>
293                                 <td width=\"99%\">
294                                 <input type='text' style='width:99%' name='".$regex['name']."' maxlength='20'
295                                 value='".$regex['value']."' title=\"".$regex['string']."\"> 
296                                 </td>
297                                 </tr>
298                                 </table>";
299                 }
300                 $smarty->assign("regexes"                       , $regexes );
302     /* Hide Filter Part if Requested or empty */
303     if((empty($boxes)) && (empty($regexes)) || ($this->HideFilterPart)){
304                   $smarty->assign("Skip_Filter_Part",   true);
305     }else{ 
306                   $smarty->assign("Skip_Filter_Part",   false);
307     }
309                 /* Assign alphabet and display it 
310                  */     
311                 $smarty->assign("Display_alphabet",     $this->bool_DisplayAlpahabet);
312                 $smarty->assign("alphabet",             generate_alphabet());
313                 $smarty->assign("Header"                        , $this->string_ListHeader );
314                 $smarty->assign("Summary"                       , $this->string_Summary);
315                 $smarty->assign("Title"                         , $this->string_Title);
316                 $smarty->assign("Information"           , $this->string_Information);
318                 /* Check for exeeded sizelimit */
319                 $smarty->assign("hint"                          , print_sizelimit_warning());
320                 $smarty->assign("DivList"                       , $divlist->DrawList());
323                 if($this->is_headpage){
324                         $smarty->assign("tree_image",           get_template_path('images/tree.png'));
325                         $smarty->assign("infoimage",            get_template_path('images/info.png'));
326                         $smarty->assign("launchimage",          get_template_path('images/launch.png'));
327                         $smarty->assign("apply",                        apply_filter());
328                 }else{
329                         $smarty->assign("tree_image",           get_template_path('images/tree.png'));
330                         $smarty->assign("infoimage",            get_template_path('images/info_small.png'));
331                         $smarty->assign("launchimage",          get_template_path('images/rocket.png'));
332                         $smarty->assign("apply",                        apply_filter());
333                 }
335                 /* Button handling */
336                 $smarty->assign("SaveButtonString" ,$this->SaveButtonString);
337                 $smarty->assign("CloseButtonString",$this->CloseButtonString);
339                 $smarty->assign("Display_Close",        $this->bool_DisplayCloseButton);
340                 $smarty->assign("Display_Save" ,        $this->bool_DisplaySaveButton);
342                 $smarty->assign("filterName"    ,       $this->filterName);
343                 $smarty->assign("is_headpage"   ,       $this->is_headpage);
345                 $display = $smarty->fetch(get_template_path("MultiSelectWindow.tpl"));
346                 return($display);
347         }
349         /* Set the close var, which simulates the close button is pressed */
350         function Close()
351         {
352                 $this->is_closed = true;
353         }
355         function Save()
356         {
357                 $this->is_saved = true;
358         }
360         /* Store all checkboxes/ regexes ... 
361            Store data also into a session var, to keep the checkboxes check after reload  */
362         function save_object()
363         {
364                 /* Update current base */
365                 if($this->DepartmentsAdded){
366                         $s_action ="";
367                         foreach($_POST as $key => $value){
368                                 if(preg_match("/^dep_back.*/i",$key)){
369                                         $s_action="back";
370                                 }elseif(preg_match("/^dep_root.*/",$key)){
371                                         $s_action="root";
372                                 }elseif(preg_match("/^dep_home.*/i",$key)){
373                                         $s_action="home";
374                                 }
375                         }
377                         /* Save base selection from headpage selectbox*/
378                         if(isset($_POST['CurrentMainBase'])){
379                                 $this->selectedBase = $_POST['CurrentMainBase'];
380                         }
382                         /* Homebutton is posted */
383                         if($s_action=="home"){
384         $ui= get_userinfo();
385         $base = get_base_from_people($ui->dn);
386                                 $this->selectedBase= $base;
387                         }
389                         /* Open selected department
390                            this is posted by the parent class MultiSelectWindow */
391                         if(isset($_GET['act'])&& ($_GET['act'] == "dep_open")){
392                                 $s_entry = base64_decode($_GET['dep_id']);
393         if (!isset($this->config->departments[$s_entry])){
394           print_red(_("Error: The requested subtree has an inconsistent DN encoding, check your LDAP!"));
395         } else {
396                                 $this->selectedBase = $this->config->departments[$s_entry];
397         }
398                         }
400                         /* back to the roots ^^ */
401                         if($s_action=="root"){
402                                 $this->selectedBase=($this->config->current['BASE']);
403                         }
406                         /* If Back-button is pressed, move back one step in DN */
407                         if($s_action=="back"){
408                                 //FIXME: This is not 100% correct. We'll only display ou's, but there may be
409                                 //       a step between. You'll stumble in a "hidden" department in this case.
410                                 $base_back= preg_replace("/^[^,]+,/", "", $_SESSION['CurrentMainBase']);
412                                 /* The department array keeps non DN entries as index. We need to convert
413                                    it before checking the existance. */
414                                 $base_back= trim(convert_department_dn($base_back));
416                                 /* Check if the department exists, otherwise revert to the configure base DN */
417                                 if(isset($this->config->departments[$base_back])){
418                                         $this->selectedBase= $this->config->departments[$base_back];
419                                 }else{
420                                         $this->selectedBase= $this->config->departments['/'];
421                                 }
422                         }
423       $_SESSION['CurrentMainBase'] = $this->selectedBase;
424                 }
426                 if(isset($_POST['MultiSelectWindow'.$this->filterName])){
428       /* Save some additional vars */
429       foreach($this->SaveAdditionalVars as $name){
430         if(isset($_POST[$name])){
431           if(isset($this->$name)){
432             $this->$name = $_POST[$name];
433             $_SESSION['MultiDialogFilters'][$this->filterName][$name] = $_POST[$name];
434           }
435         }
436       }
438                         /* Check posts from checkboxes 
439                          */
440                         foreach($this->array_Checkboxes as $key => $box){
441                                 if(isset($_POST[$box['name']])){
442                                         $this->array_Checkboxes[$key]['default'] = true;
443                                         $this->$box['name'] = true;
444                                 }else{
445                                         $this->array_Checkboxes[$key]['default'] = false;
446                                         $this->$box['name'] = false;
447                                 }
448                                 /* Save settings in out session */
449                                 $_SESSION['MultiDialogFilters'][$this->filterName][$box['name']] = $this->$box['name'];
450                         }
452                         /* Check regex posts */
453                         foreach($this->array_Regexes as $key => $box){
454                                 $this->array_Regexes[$key]['value'] = $_POST[$box['name']];
455                                 $this->$box['name'] = $_POST[$box['name']];
456                                 $_SESSION['MultiDialogFilters'][$this->filterName][$box['name']] = $this->$box['name'];
457                         }
459                         /* call close/save if buttons are pressed */
460                         if(isset($_POST['CloseMultiSelectWindow'])){
461                                 $this->Close();
462                         }
464                         if(isset($_POST['SaveMultiSelectWindow'])){
465                                 $this->Save();
466                         }
467                 }
469                 /* check for alphabet selection
470                    Check which regexes are connected to the alphabet 
471                  */     
472                 if(isset($_GET['search'])){
473                         foreach($this->array_Regexes as $key => $box){
474                                 /* check if this regex is connected to the alphabet selection */
475                                 if(($box['connAlpha'])&&(isset($_GET['search']))){
476                                         $val =  $_GET['search']."*";
477                                         $val = preg_replace("/\*\**/","*",$val);
478                                         $this->array_Regexes[$key]['value'] = $val;
479                                         $this->$box['name'] = $val;
480                                 }
481                         }
482                 }
484         }
486         /* this function adds the sub-departments of the current tree to the list */
487         function AddDepartments($base = false,$numtabs = 3)
488         {
489                 $this->DepartmentsAdded = true;
491                 /* check for a valid base */
492                 if(!$base){
493                         if(!isset($_SESSION['CurrentMainBase'])){
494                                 $_SESSION['CurrentMainBase'] = $this->config->current['BASE'];
495                         }
496                         $base = $_SESSION['CurrentMainBase'];
497                 }
499                 /* Create ldap obj and switch into base*/
500                 $ldap = $this->config->get_ldap_link();
501                 $ldap->cd($base);
503                 /* reset current deps */
504                 $this->departments = array();
506                 /* Get all departments within this subtree */
507                 $deps= get_list("(&(|(ou=*)(description=*))(objectClass=gosaDepartment))", $this->ui->subtreeACL,
508                                 $base, array("ou", "description"), GL_SIZELIMIT | GL_CONVERT);
510                 /* Edit delete link for system types
511                  */
512                 $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=dep_open&amp;dep_id=%s'>%s</a>";
514                 /* Create an array with all visible (in the list) departments */
515                 $departments = array();
516                 foreach($deps as $value){
517                         if(isset($value['description'][0])){
518                                 $this->departments[$value['dn']]= get_sub_department($value['dn'])." - [".$value["description"][0]."]";
519                         }else{
520                                 $this->departments[$value['dn']]= get_sub_department($value['dn']);
521                         }
522                 }
523                 natcasesort($this->departments);
525                 /* Add deps to this dialog object list */
526                 foreach($this->departments as $key=> $val){
527                         /* Add missing entries ... */
528                         if(!isset($this->config->departments[trim($key)])){
529                                 $this->config->departments[trim($key)]="";
530                         }
532                         /* check if this department contains sub-departments
533                            Display different image in this case
534                          */
535                         $non_empty="";
536                         $nkey= normalizePreg($key);
537                         foreach($this->config->departments as $keyd=>$vald ){
538                                 if(preg_match("/$nkey\/.*/",$keyd)){
539                                         $non_empty="full";
540                                 }
541                         }
543                         /* Add to divlist */
544                         $row = array();
545                         $row[]=$field1=array("string"=>"<img src='images/".$non_empty."folder.png' alt='department'>","attach"=>"style='text-align:center;width:20px;'");
546                         $row[]=$field2=array("string"=>sprintf($linkopen,base64_encode($key),$val), "attach" => "style=''");
548                         if($numtabs > 2){       
549                                 for($i = 2 ; $i <$numtabs;$i++){
550           if(isset( $this->array_Header[$i]['attach'])){
551             $row[] = array("string"=>"&nbsp;","attach" => $this->array_Header[$i]['attach']);
552           }else{
553             $row[] = array("string"=>"&nbsp;");
554           }
555                                 }
556                         }
558                         $this->AddElement($row);
559                 }
560         }
562 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
563 ?>