Code

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