Code

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