Code

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