Code

Fixed save method
[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     if(isset($this->parent->config)){
365       $this->config = $this->parent->config;
366     }
368                 /* Update current base */
369                 if($this->DepartmentsAdded){
370                         $s_action ="";
371                         foreach($_POST as $key => $value){
372                                 if(preg_match("/^dep_back.*/i",$key)){
373                                         $s_action="back";
374                                 }elseif(preg_match("/^dep_root.*/",$key)){
375                                         $s_action="root";
376                                 }elseif(preg_match("/^dep_home.*/i",$key)){
377                                         $s_action="home";
378                                 }
379                         }
381                         /* Save base selection from headpage selectbox*/
382                         if(isset($_POST['CurrentMainBase'])){
383                                 $this->selectedBase = $_POST['CurrentMainBase'];
384                         }
386                         /* Homebutton is posted */
387                         if($s_action=="home"){
388         $ui= get_userinfo();
389         $base = get_base_from_people($ui->dn);
390                                 $this->selectedBase= $base;
391                         }
393                         /* Open selected department
394                            this is posted by the parent class MultiSelectWindow */
395                         if(isset($_GET['act'])&& ($_GET['act'] == "dep_open")){
396                                 $s_entry = base64_decode($_GET['dep_id']);
397         if (!isset($this->config->departments[$s_entry])){
398           print_red(_("Error: The requested subtree has an inconsistent DN encoding, check your LDAP!"));
399         } else {
400                                 $this->selectedBase = $this->config->departments[$s_entry];
401         }
402                         }
404                         /* back to the roots ^^ */
405                         if($s_action=="root"){
406                                 $this->selectedBase=($this->config->current['BASE']);
407                         }
410                         /* If Back-button is pressed, move back one step in DN */
411                         if($s_action=="back"){
412                                 //FIXME: This is not 100% correct. We'll only display ou's, but there may be
413                                 //       a step between. You'll stumble in a "hidden" department in this case.
414                                 $base_back= preg_replace("/^[^,]+,/", "", $_SESSION['CurrentMainBase']);
416                                 /* The department array keeps non DN entries as index. We need to convert
417                                    it before checking the existance. */
418                                 $base_back= trim(convert_department_dn($base_back));
420                                 /* Check if the department exists, otherwise revert to the configure base DN */
421                                 if(isset($this->config->departments[$base_back])){
422                                         $this->selectedBase= $this->config->departments[$base_back];
423                                 }else{
424                                         $this->selectedBase= $this->config->departments['/'];
425                                 }
426                         }
427       $_SESSION['CurrentMainBase'] = $this->selectedBase;
428                 }
430                 if(isset($_POST['MultiSelectWindow'.$this->filterName])){
432       /* Save some additional vars */
433       foreach($this->SaveAdditionalVars as $name){
434         if(isset($_POST[$name])){
435           if(isset($this->$name)){
436             $this->$name = $_POST[$name];
437             $_SESSION['MultiDialogFilters'][$this->filterName][$name] = $_POST[$name];
438           }
439         }
440       }
442                         /* Check posts from checkboxes 
443                          */
444                         foreach($this->array_Checkboxes as $key => $box){
445                                 if(isset($_POST[$box['name']])){
446                                         $this->array_Checkboxes[$key]['default'] = true;
447                                         $this->$box['name'] = true;
448                                 }else{
449                                         $this->array_Checkboxes[$key]['default'] = false;
450                                         $this->$box['name'] = false;
451                                 }
452                                 /* Save settings in out session */
453                                 $_SESSION['MultiDialogFilters'][$this->filterName][$box['name']] = $this->$box['name'];
454                         }
456                         /* Check regex posts */
457                         foreach($this->array_Regexes as $key => $box){
458                                 $this->array_Regexes[$key]['value'] = $_POST[$box['name']];
459                                 $this->$box['name'] = $_POST[$box['name']];
460                                 $_SESSION['MultiDialogFilters'][$this->filterName][$box['name']] = $this->$box['name'];
461                         }
463                         /* call close/save if buttons are pressed */
464                         if(isset($_POST['CloseMultiSelectWindow'])){
465                                 $this->Close();
466                         }
468                         if(isset($_POST['SaveMultiSelectWindow'])){
469                                 $this->Save();
470                         }
471                 }
473                 /* check for alphabet selection
474                    Check which regexes are connected to the alphabet 
475                  */     
476                 if(isset($_GET['search'])){
477                         foreach($this->array_Regexes as $key => $box){
478                                 /* check if this regex is connected to the alphabet selection */
479                                 if(($box['connAlpha'])&&(isset($_GET['search']))){
480                                         $val =  $_GET['search']."*";
481                                         $val = preg_replace("/\*\**/","*",$val);
482                                         $this->array_Regexes[$key]['value'] = $val;
483                                         $this->$box['name'] = $val;
484           $_SESSION['MultiDialogFilters'][$this->filterName][$box['name']] =  $val;
485                                 }
486                         }
487                 }
488         }
491   /* Create header snapshot value */
492   function get_snapshot_header()
493   {
494     $str = "&nbsp;";
495     if($this->parent->snapshotEnabled()){
496       $ok = false;
497       foreach($this->parent->get_used_snapshot_bases() as $base){
498         $ok |= count($this->parent->getAllDeletedSnapshots($base)) >= 1 ;
499       }
500       if($ok){
501         $str = "<input class='center' type='image' align='middle' src='images/restore.png'
502           title='"._("Restore snapshopts of already deleted objects")."' 
503           alt='"._("Restore")."' name='RestoreDeletedSnapShot'>&nbsp;"; 
504       }else{
505         $str = "<img class='center' src='images/restore_grey.png'>&nbsp;";
506       }
508       $str .= "<img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;";
509     }
510     return($str);
511   }
514   function GetSnapShotActions($dn)
515   {
516     $str= "";
517     if($this->parent->snapshotEnabled()){
519       if(count($this->parent->Available_SnapsShots($dn))){
520         $str.= "<input class='center' type='image' src='images/restore.png'
521           alt='"._("Restore snapshot")."' name='RestoreSnapShotDialog_".base64_encode($dn)."' title='"._("Restore snapshot")."'>&nbsp;";
522       } else {
523         $str = "<img class='center' src='images/restore_grey.png'>&nbsp;";
524       }
526       $str.= "<input class='center' type='image' src='images/snapshot.png'
527         alt='"._("Create snapshot")."' name='CreateSnapShotDialog_".base64_encode($dn)."' title='"._("Create a new snapshot from this object")."'>&nbsp;";
528     }
530     return($str);
531   }
534         /* this function adds the sub-departments of the current tree to the list */
535         function AddDepartments($base = false,$numtabs = 3)
536         {
537                 $this->DepartmentsAdded = true;
539                 /* check for a valid base */
540                 if(!$base){
541                         if(!isset($_SESSION['CurrentMainBase'])){
542                                 $_SESSION['CurrentMainBase'] = $this->config->current['BASE'];
543                         }
544                         $base = $_SESSION['CurrentMainBase'];
545                 }
547                 /* Create ldap obj and switch into base*/
548                 $ldap = $this->config->get_ldap_link();
549                 $ldap->cd($base);
551                 /* reset current deps */
552                 $this->departments = array();
554                 /* Get all departments within this subtree */
555                 $deps= get_list("(&(|(ou=*)(description=*))(objectClass=gosaDepartment))", $this->ui->subtreeACL,
556                                 $base, array("ou", "description"), GL_SIZELIMIT | GL_CONVERT);
558                 /* Edit delete link for system types
559                  */
560                 $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=dep_open&amp;dep_id=%s'>%s</a>";
562                 /* Create an array with all visible (in the list) departments */
563                 $departments = array();
564                 foreach($deps as $value){
565                         if(isset($value['description'][0])){
566                                 $this->departments[$value['dn']]= get_sub_department($value['dn'])." - [".$value["description"][0]."]";
567                         }else{
568                                 $this->departments[$value['dn']]= get_sub_department($value['dn']);
569                         }
570                 }
571                 natcasesort($this->departments);
573                 /* Add deps to this dialog object list */
574                 foreach($this->departments as $key=> $val){
575                         /* Add missing entries ... */
576                         if(!isset($this->config->departments[trim($key)])){
577                                 $this->config->departments[trim($key)]="";
578                         }
580                         /* check if this department contains sub-departments
581                            Display different image in this case
582                          */
583                         $non_empty="";
584                         $nkey= normalizePreg($key);
585                         foreach($this->config->departments as $keyd=>$vald ){
586                                 if(preg_match("/$nkey\/.*/",$keyd)){
587                                         $non_empty="full";
588                                 }
589                         }
591                         /* Add to divlist */
592                         $row = array();
593                         $row[]=$field1=array("string"=>"<img src='images/".$non_empty."folder.png' alt='department'>","attach"=>"style='text-align:center;width:20px;'");
594                         $row[]=$field2=array("string"=>sprintf($linkopen,base64_encode($key),$val), "attach" => "style=''");
596                         if($numtabs > 2){       
597                                 for($i = 2 ; $i <$numtabs;$i++){
598           if(isset( $this->array_Header[$i]['attach'])){
599             $row[] = array("string"=>"&nbsp;","attach" => $this->array_Header[$i]['attach']);
600           }else{
601             $row[] = array("string"=>"&nbsp;");
602           }
603                                 }
604                         }
606                         $this->AddElement($row);
607                 }
608         }
610 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
611 ?>