Code

Added some features to multiSelectDialog
[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("");
26         var $is_closed                          = false;
27         var $is_saved                           = false;
29         var $is_headpage                        = false;        // if true the design changes
31         var $filterName                         = "Liste";
33         function ClearElementsList()
34         {
35                 $this->array_Elements = array();
36                 #FIXME  print_sizelimit_warning() ? still missing
37         }
40         /* Adds a regex input field to the current dialog */
41         function AddRegex($name,$string,$value,$conn,$image="images/search.png")
42         {
43                 $arr = array();
44         
45                 /* Check if the given input field name was already used 
46             for this type of dialog */  
47                 if(isset($_SESSION['MultiDialogFilters'][$this->filterName][$name])){
48                         $arr['value']   = $_SESSION['MultiDialogFilters'][$this->filterName][$name];
49                         $this->$name = $arr['value'];
50                 }else{
51                         $arr['value']   = $value;       
52                 }
54                 $arr['name']            = $name;
55                 $arr['string']          = $string;
56                 $arr['image']           = $image;       
57                 $arr['connAlpha']       = $conn;                // Connect with alphabet select 
58                 $this->array_Regexes[] = $arr;
59         }
61         /* Contrucktion */
62         function MultiSelectWindow($config,$filterName)
63         {
64                 $this->config = $config;
65                 $this->SaveButtonString         = _("Save");
66                 $this->CloseButtonString        = _("Close");
67                 $this->filterName                       = $filterName;
68         }
70         /* Enables the headpage mode, which changes the list look */
71         function SetHeadpageMode()
72         {
73                 $this->is_headpage = true;
74         }
76         /* Sets the List internal name (not displayed anywhere) 
77        it is used to identify every single list
78      */ 
79         function SetTitle($str)
80         {
81                 $this->string_Title = $str;
82         }
84         /* Set the list header string  */
85         function SetListHeader($str)
86         {
87                 $this->string_ListHeader = $str;
88         }
90         /* This sets the list description which is the first gray bar on top of the list */
91         function SetSummary($str)
92         {
93                 $this->string_Summary = $str;
94         }
96         /* If the save button is enabled, you can change its caption with this function */      
97         function SetSaveButtonString($str)
98         {
99                 $this->SaveButtonString = $str;
100         }
102         /* If the close button is enabled, you can change its caption with this function */     
103         function SetCloseButtonString($str)
104         {
105                 $this->CloseButtonString = $str;
106         }
108         /* With this function you can change the text of the information box */
109         function SetInformation($str)
110         {
111                 $this->string_Information = $str;
112         }
114         /* Display the alphabet selection box*/
115         function EnableAplhabet($bool)
116         {
117                 $this->bool_DisplayAlpahabet = $bool;
118         }
120         /* Add additional header col */
121         function AddHeader($arr)
122         {
123                 $this->array_Header[] = $arr;
124         }
126         /* add additional List element */
127         function AddElement($arr)
128         {
129                 $this->array_Elements[] = $arr;
130         }
132         /* Add a checkbox to the filter element,
133         the name specifies an existing class var to store the 'selection' */
134         function AddCheckBox($name,$value="Unset",$string="Unset",$default=false)
135         {
136                 $arr = array();
137         
138                 if($name == SEPERATOR){
139                         $arr['name'] = SEPERATOR;
140                 }else{
141                         /* Check if there was already a variable 
142                            for this dialog which we should use instead of the default*/
143                         if(isset($_SESSION['MultiDialogFilters'][$this->filterName][$name])){
144                                 $arr['default'] = $_SESSION['MultiDialogFilters'][$this->filterName][$name];
145                                 $this->$name = $arr['default'];
146                         }else{
147                                 $arr['default'] = $default;
148                         }
149                         $arr['name']    = $name;
150                         $arr['string']  = $string;
151                         $arr['value']   = $value;
152                         $arr['enabled'] = true;
153                 }
154                 $this->array_Checkboxes[] = $arr;
155         }
157         /* Hides or unhides the checkbox with the given name */
158         function DisableCheckBox($name,$HideShow = false)
159         {
160                 foreach($this->array_Checkboxes as $key => $chkbox){
161                         if($chkbox['name'] == $name){
162                                 $this->array_Checkboxes[$key]['enabled'] = $HideShow;
163                         }
164                 }
165         }
167         
169         /* Returns true if the close button was pressed */
170         function isClosed()
171         {
172                 return($this->is_closed);
173         }
175         /* Enable the close button */
176         function EnableCloseButton($bool)
177         {
178                 $this->bool_DisplayCloseButton = $bool;
179         }
181         /* Enable the save button on the bottom of the list*/
182         function EnableSaveButton ($bool)
183         {
184                 $this->bool_DisplaySaveButton = $bool;
185         }
187         /* Draw the list with all list elements and filters */
188         function Draw()
189         {
190                 $smarty = get_smarty();
192                 $divlist = new divlist($this->string_Title);
193                 $divlist->SetSummary($this->string_Summary);
194                 $divlist->SetEntriesPerPage(0); // 0 for scrollable list 
196                 /* set Header informations 
197                  */
198                 $header = array();
199                 foreach($this->array_Header as $head){
200                         $header[] = $head;
201                 }
202                 $divlist->SetHeader($header);
204                 /* set Entries 
205                  */
206                 $elements = array();
207                 foreach($this->array_Elements as $element){
208                         $divlist->AddEntry($element);
209                 }
211                 /* Create checkboxes fields 
212                  */
213                 $boxes = "";
214                 $boxClick = " onClick='document.mainform.submit();' ";
215                 foreach($this->array_Checkboxes as $box){
217                         if($box['name'] == SEPERATOR){
218                                 $boxes .= "</td></tr></table><table style='width:100%;border-top:1px solid #B0B0B0;'><tr><td>";
219                                 continue;
220                         }
222                         /* Skip disabled boxes */
223                         if(!$box['enabled']) continue;
225                         /* Check if box is checked */
226                         if($box['default'] == true){
227                                 $boxes .="<input type='checkbox' name='".$box['name']."' value='1' title='".$box['value']."' checked ".$boxClick.">&nbsp;".$box['string']."<br>";
228                         }else{
229                                 $boxes .="<input type='checkbox' name='".$box['name']."' value='1' title='".$box['value']."'".$boxClick.";>&nbsp;".$box['string']."<br>";
230                         }
231                 }
232                 $smarty->assign("CheckBoxes", $boxes);
234                 /* Assign regex fields 
235                  */
236                 $regexes = "";
237                 foreach($this->array_Regexes as $regex){
238                         $regexes.="<table summary=\"\" style=\"width:100%;border-top:1px solid #B0B0B0;\">
239                                 <tr>
240                                 <td>
241                                 <label for=\"".$regex['name']."\">
242                                 <img alt=\"".$regex['string']."\" src=\"".$regex['image']."\" align=middle>
243                                 </label>
244                                 </td>
245                                 <td width=\"99%\">
246                                 <input type='text' style='width:99%' name='".$regex['name']."' maxlength='20'
247                                 value='".$regex['value']."' title=\"".$regex['string']."\"> 
248                                 </td>
249                                 </tr>
250                                 </table>";
251                 }
252                 $smarty->assign("regexes"                       , $regexes );
253                 
254                 /* Assign alphabet and display it 
255          */     
256                 $smarty->assign("Display_alphabet",     $this->bool_DisplayAlpahabet);
257                 $smarty->assign("alphabet",             generate_alphabet());
258                 $smarty->assign("Header"                        , $this->string_ListHeader );
259                 $smarty->assign("Summary"                       , $this->string_Summary);
260                 $smarty->assign("Title"                         , $this->string_Title);
261                 $smarty->assign("Information"           , $this->string_Information);
262                 
263                 /* Check for exeeded sizelimit */
264                 $smarty->assign("hint"                          , print_sizelimit_warning());
265                 $smarty->assign("DivList"                       , $divlist->DrawList());
268                 if($this->is_headpage){
269                         $smarty->assign("tree_image",           get_template_path('images/tree.png'));
270                         $smarty->assign("infoimage",            get_template_path('images/info.png'));
271                         $smarty->assign("launchimage",          get_template_path('images/launch.png'));
272                         $smarty->assign("apply",                        apply_filter());
273                 }else{
274                         $smarty->assign("tree_image",           get_template_path('images/tree.png'));
275                         $smarty->assign("infoimage",            get_template_path('images/info_small.png'));
276                         $smarty->assign("launchimage",          get_template_path('images/rocket.png'));
277                         $smarty->assign("apply",                        apply_filter());
278                 }
280                 /* Button handling */
281                 $smarty->assign("SaveButtonString" ,$this->SaveButtonString);
282                 $smarty->assign("CloseButtonString",$this->CloseButtonString);
283         
284                 $smarty->assign("Display_Close",        $this->bool_DisplayCloseButton);
285                 $smarty->assign("Display_Save" ,        $this->bool_DisplaySaveButton);
287                 $smarty->assign("filterName"    ,       $this->filterName);
288                 $smarty->assign("is_headpage"   ,       $this->is_headpage);
289                         
290                 $display = $smarty->fetch(get_template_path("MultiSelectWindow.tpl"));
291                 return($display);
292         }
294         /* Set the close var, which simulates the close button is pressed */
295         function Close()
296         {
297                 $this->is_closed = true;
298         }
300         function Save()
301         {
302                 $this->is_saved = true;
303         }
305         /* Store all checkboxes/ regexes ... 
306         Store data also into a session var, to keep the checkboxes check after reload  */
307         function save_object()
308         {
309                 if(isset($_POST['MultiSelectWindow'.$this->filterName])){
311                         /* Check posts from checkboxes 
312              */
313                         foreach($this->array_Checkboxes as $key => $box){
314                                 if(isset($_POST[$box['name']])){
315                                         $this->array_Checkboxes[$key]['default'] = true;
316                                         $this->$box['name'] = true;
317                                 }else{
318                                         $this->array_Checkboxes[$key]['default'] = false;
319                                         $this->$box['name'] = false;
320                                 }
321                                 /* Save settings in out session */
322                                 $_SESSION['MultiDialogFilters'][$this->filterName][$box['name']] = $this->$box['name'];
323                         }
325                         /* Check regex posts */
326                         foreach($this->array_Regexes as $key => $box){
327                                 $this->array_Regexes[$key]['value'] = $_POST[$box['name']];
328                                 $this->$box['name'] = $_POST[$box['name']];
329                                 $_SESSION['MultiDialogFilters'][$this->filterName][$box['name']] = $this->$box['name'];
330                         }
332                         /* call close/save if buttons are pressed */
333                         if(isset($_POST['CloseMultiSelectWindow'])){
334                                 $this->Close();
335                         }
336                         
337                         if(isset($_POST['SaveMultiSelectWindow'])){
338                                 $this->Save();
339                         }
340                 }
342                 /* check for alphabet selection
343            Check which regexes are connected to the alphabet 
344          */     
345                 if(isset($_GET['search'])){
346                         foreach($this->array_Regexes as $key => $box){
347                                 /* check if this regex is connected to the alphabet selection */
348                                 if(($box['connAlpha'])&&(isset($_GET['search']))){
349                                         $val =  $_GET['search']."*";
350                                         $val = preg_replace("/\*\**/","*",$val);
351                                         $this->array_Regexes[$key]['value'] = $val;
352                                         $this->$box['name'] = $val;
353                                 }
354                         }
355                 }
357         }
359         /* this function adds the sub-departments of the current tree to the list */
360         function AddDepartments($base = false,$numtabs = 3)
361         {
362                 /* check for a valid base */
363                 if(!$base){
364                         if(!isset($_SESSION['CurrentMainBase'])){
365                                 $_SESSION['CurrentMainBase'] = $this->config->current['BASE'];
366                         }
367                         $base = $_SESSION['CurrentMainBase'];
368                 }
370                 /* Create ldap obj and switch into base*/
371                 $ldap = $this->config->get_ldap_link();
372                 $ldap->cd($base);
374                 /* reset current deps */
375                 $this->departments = array();
377                 /* Get all departments within this subtree */
378                 $deps= get_list("(&(|(ou=*)(description=*))(objectClass=gosaDepartment))", $this->ui->subtreeACL,
379                                 $base, array("ou", "description"), GL_SIZELIMIT | GL_CONVERT);
381                 /* Edit delete link for system types
382                  */
383                 $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=dep_open&amp;dep_id=%s'>%s</a>";
385                 /* Create an array with all visible (in the list) departments */
386                 $departments = array();
387                 foreach($deps as $value){
388                         if(isset($value['description'][0])){
389                                 $this->departments[$value['dn']]= get_sub_department($value['dn'])." - [".$value["description"][0]."]";
390                         }else{
391                                 $this->departments[$value['dn']]= get_sub_department($value['dn']);
392                         }
393                 }
394                 natcasesort($this->departments);
396                 /* Add deps to this dialog object list */
397                 foreach($this->departments as $key=> $val){
398                         /* Add missing entries ... */
399                         if(!isset($this->config->departments[trim($key)])){
400                                 $this->config->departments[trim($key)]="";
401                         }
403                         /* check if this department contains sub-departments
404                            Display different image in this case
405                          */
406                         $non_empty="";
407                         $nkey= normalizePreg($key);
408                         foreach($this->config->departments as $keyd=>$vald ){
409                                 if(preg_match("/$nkey\/.*/",$keyd)){
410                                         $non_empty="full";
411                                 }
412                         }
414                         
415                         /* Add to divlist */
416                         $row = array();
417                         $row[] = $field1 = array("string" => "<img src='images/".$non_empty."folder.png' alt='department'>", "attach" => "style='text-align:center;width:20px;'");
418                         $row[] = $field2 = array("string" => sprintf($linkopen,base64_encode($key),$val), "attach" => "style=''");
420                         if($numtabs > 2){       
421                                 for($i = 2 ; $i <$numtabs;$i++){
422                                         if($i ==($numtabs-1)){
423                                                 $row[] = array("string"=>"&nbsp;","attach" => "style='width:60px;border-right:0px;text-align:right;'");
424                                         }else{
425                                                 $row[] = array("string"=>"&nbsp;");
426                                         }
427                                 }
428                         }
430                         $this->AddElement($row);
431                 }
432         }
435 ?>