Code

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