Code

Fixed printf , parameter was missing,
[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         var $DepartmentsAdded           = false;
34         var $selectedBase               = "";
37         function ClearElementsList()
38         {
39                 $this->array_Elements = array();
40         }
43         /* Adds a regex input field to the current dialog */
44         function AddRegex($name,$string,$value,$conn,$image="images/search.png")
45         {
46                 $arr = array();
48                 /* Check if the given input field name was already used 
49                    for this type of dialog */   
50                 if(isset($_SESSION['MultiDialogFilters'][$this->filterName][$name])){
51                         $arr['value']   = $_SESSION['MultiDialogFilters'][$this->filterName][$name];
52                         $this->$name = $arr['value'];
53                 }else{
54                         $arr['value']   = $value;       
55                 }
57                 $arr['name']            = $name;
58                 $arr['string']          = $string;
59                 $arr['image']           = $image;       
60                 $arr['connAlpha']       = $conn;                // Connect with alphabet select 
61                 $this->array_Regexes[] = $arr;
62         }
64         /* Contrucktion */
65         function MultiSelectWindow($config,$filterName)
66         {
67                 $this->config = $config;
68                 $this->SaveButtonString         = _("Save");
69                 $this->CloseButtonString        = _("Close");
70                 $this->filterName                       = $filterName;
72                 /* Set default base */
73                 if(!isset($_SESSION['CurrentMainBase'])){
74                         $_SESSION['CurrentMainBase'] = $this->config->current['BASE'];
75                 }
76                 $this->selectedBase = $_SESSION['CurrentMainBase'];
77         }
79         /* Enables the headpage mode, which changes the list look */
80         function SetHeadpageMode()
81         {
82                 $this->is_headpage = true;
83         }
85         /* Sets the List internal name (not displayed anywhere) 
86            it is used to identify every single list
87          */     
88         function SetTitle($str)
89         {
90                 $this->string_Title = $str;
91         }
93         /* Set the list header string  */
94         function SetListHeader($str)
95         {
96                 $this->string_ListHeader = $str;
97         }
99         /* This sets the list description which is the first gray bar on top of the list */
100         function SetSummary($str)
101         {
102                 $this->string_Summary = $str;
103         }
105         /* If the save button is enabled, you can change its caption with this function */      
106         function SetSaveButtonString($str)
107         {
108                 $this->SaveButtonString = $str;
109         }
111         /* If the close button is enabled, you can change its caption with this function */     
112         function SetCloseButtonString($str)
113         {
114                 $this->CloseButtonString = $str;
115         }
117         /* With this function you can change the text of the information box */
118         function SetInformation($str)
119         {
120                 $this->string_Information = $str;
121         }
123         /* Display the alphabet selection box*/
124         function EnableAplhabet($bool)
125         {
126                 $this->bool_DisplayAlpahabet = $bool;
127         }
129         /* Add additional header col */
130         function AddHeader($arr)
131         {
132                 $this->array_Header[] = $arr;
133         }
135         /* add additional List element */
136         function AddElement($arr)
137         {
138                 $this->array_Elements[] = $arr;
139         }
141         /* Add a checkbox to the filter element,
142            the name specifies an existing class var to store the 'selection' */
143         function AddCheckBox($name,$value="Unset",$string="Unset",$default=false)
144         {
145                 $arr = array();
147                 if($name == SEPERATOR){
148                         $arr['name'] = SEPERATOR;
149                 }else{
150                         /* Check if there was already a variable 
151                            for this dialog which we should use instead of the default*/
152                         if(isset($_SESSION['MultiDialogFilters'][$this->filterName][$name])){
153                                 $arr['default'] = $_SESSION['MultiDialogFilters'][$this->filterName][$name];
154                                 $this->$name = $arr['default'];
155                         }else{
156                                 $arr['default'] = $default;
157                         }
158                         $arr['name']          = $name;
159                         $arr['string']      = $string;
160                         $arr['value']         = $value;
161                         $arr['enabled']     = true;
163       $this->$name = $default;
164                 }
165                 $this->array_Checkboxes[] = $arr;
166         }
168         /* Hides or unhides the checkbox with the given name */
169         function DisableCheckBox($name,$HideShow = false)
170         {
171                 foreach($this->array_Checkboxes as $key => $chkbox){
172                         if($chkbox['name'] == $name){
173                                 $this->array_Checkboxes[$key]['enabled'] = $HideShow;
174                         }
175                 }
176         }
180         /* Returns true if the close button was pressed */
181         function isClosed()
182         {
183                 return($this->is_closed);
184         }
186         /* Enable the close button */
187         function EnableCloseButton($bool)
188         {
189                 $this->bool_DisplayCloseButton = $bool;
190         }
192         /* Enable the save button on the bottom of the list*/
193         function EnableSaveButton ($bool)
194         {
195                 $this->bool_DisplaySaveButton = $bool;
196         }
198         /* Draw the list with all list elements and filters */
199         function Draw()
200         {
202                 /* Check for exeeded sizelimit */
203                 if (($message= check_sizelimit()) != ""){
204                         return($message);
205                 }
207                 $smarty = get_smarty();
209                 $divlist = new divlist($this->string_Title);
210                 $divlist->SetSummary($this->string_Summary);
211                 $divlist->SetEntriesPerPage(0); // 0 for scrollable list 
213                 /* set Header informations 
214                  */
215                 $header = array();
216                 foreach($this->array_Header as $head){
217                         $header[] = $head;
218                 }
219                 $divlist->SetHeader($header);
221                 /* set Entries 
222                  */
223                 $elements = array();
224                 foreach($this->array_Elements as $element){
225                         $divlist->AddEntry($element);
226                 }
228                 /* Create checkboxes fields 
229                  */
230                 $boxes = "";
231                 $boxClick = " onClick='document.mainform.submit();' ";
232                 foreach($this->array_Checkboxes as $box){
234                         if($box['name'] == SEPERATOR){
235                                 $boxes .= "</td></tr></table><table style='width:100%;border-top:1px solid #B0B0B0;'><tr><td>";
236                                 continue;
237                         }
239                         /* Skip disabled boxes */
240                         if(!$box['enabled']) continue;
242                         /* Check if box is checked */
243                         if($box['default'] == true){
244                                 $boxes .="<input type='checkbox' name='".$box['name']."' value='1' title='".$box['value']."' checked ".$boxClick.">&nbsp;".$box['string']."<br>";
245                         }else{
246                                 $boxes .="<input type='checkbox' name='".$box['name']."' value='1' title='".$box['value']."'".$boxClick.";>&nbsp;".$box['string']."<br>";
247                         }
248                 }
249                 $smarty->assign("CheckBoxes", $boxes);
251                 /* Assign regex fields 
252                  */
253                 $regexes = "";
254                 foreach($this->array_Regexes as $regex){
255                         $regexes.="<table summary=\"\" style=\"width:100%;border-top:1px solid #B0B0B0;\">
256                                 <tr>
257                                 <td>
258                                 <label for=\"".$regex['name']."\">
259                                 <img alt=\"".$regex['string']."\" src=\"".$regex['image']."\" align=middle>
260                                 </label>
261                                 </td>
262                                 <td width=\"99%\">
263                                 <input type='text' style='width:99%' name='".$regex['name']."' maxlength='20'
264                                 value='".$regex['value']."' title=\"".$regex['string']."\"> 
265                                 </td>
266                                 </tr>
267                                 </table>";
268                 }
269                 $smarty->assign("regexes"                       , $regexes );
271                 /* Assign alphabet and display it 
272                  */     
273                 $smarty->assign("Display_alphabet",     $this->bool_DisplayAlpahabet);
274                 $smarty->assign("alphabet",             generate_alphabet());
275                 $smarty->assign("Header"                        , $this->string_ListHeader );
276                 $smarty->assign("Summary"                       , $this->string_Summary);
277                 $smarty->assign("Title"                         , $this->string_Title);
278                 $smarty->assign("Information"           , $this->string_Information);
280                 /* Check for exeeded sizelimit */
281                 $smarty->assign("hint"                          , print_sizelimit_warning());
282                 $smarty->assign("DivList"                       , $divlist->DrawList());
285                 if($this->is_headpage){
286                         $smarty->assign("tree_image",           get_template_path('images/tree.png'));
287                         $smarty->assign("infoimage",            get_template_path('images/info.png'));
288                         $smarty->assign("launchimage",          get_template_path('images/launch.png'));
289                         $smarty->assign("apply",                        apply_filter());
290                 }else{
291                         $smarty->assign("tree_image",           get_template_path('images/tree.png'));
292                         $smarty->assign("infoimage",            get_template_path('images/info_small.png'));
293                         $smarty->assign("launchimage",          get_template_path('images/rocket.png'));
294                         $smarty->assign("apply",                        apply_filter());
295                 }
297                 /* Button handling */
298                 $smarty->assign("SaveButtonString" ,$this->SaveButtonString);
299                 $smarty->assign("CloseButtonString",$this->CloseButtonString);
301                 $smarty->assign("Display_Close",        $this->bool_DisplayCloseButton);
302                 $smarty->assign("Display_Save" ,        $this->bool_DisplaySaveButton);
304                 $smarty->assign("filterName"    ,       $this->filterName);
305                 $smarty->assign("is_headpage"   ,       $this->is_headpage);
307                 $display = $smarty->fetch(get_template_path("MultiSelectWindow.tpl"));
308                 return($display);
309         }
311         /* Set the close var, which simulates the close button is pressed */
312         function Close()
313         {
314                 $this->is_closed = true;
315         }
317         function Save()
318         {
319                 $this->is_saved = true;
320         }
322         /* Store all checkboxes/ regexes ... 
323            Store data also into a session var, to keep the checkboxes check after reload  */
324         function save_object()
325         {
326                 /* Update current base */
327                 if($this->DepartmentsAdded){
328                         $s_action ="";
329                         foreach($_POST as $key => $value){
330                                 if(preg_match("/^dep_back.*/i",$key)){
331                                         $s_action="back";
332                                 }elseif(preg_match("/^dep_root.*/",$key)){
333                                         $s_action="root";
334                                 }elseif(preg_match("/^dep_home.*/i",$key)){
335                                         $s_action="home";
336                                 }
337                         }
339                         /* Save base selection from headpage selectbox*/
340                         if(isset($_POST['CurrentMainBase'])){
341                                 $this->selectedBase = $_POST['CurrentMainBase'];
342                         }
344                         /* Homebutton is posted */
345                         if($s_action=="home"){
346                                 $ui= get_userinfo();
347                                 $this->selectedBase=(preg_replace("/^[^,]+,/","",$ui->dn));
348                                 $this->selectedBase=(preg_replace("/^[^,]+,/","",$this->selectedBase));
349                         }
351                         /* Open selected department
352                            this is posted by the parent class MultiSelectWindow */
353                         if(isset($_GET['act'])&& ($_GET['act'] == "dep_open")){
354                                 $s_entry = base64_decode($_GET['dep_id']);
355                                 $this->selectedBase = $this->config->departments[trim($s_entry)];
356                         }
358                         /* back to the roots ^^ */
359                         if($s_action=="root"){
360                                 $this->selectedBase=($this->config->current['BASE']);
361                         }
364                         /* If Back-button is pressed, move back one step in DN */
365                         if($s_action=="back"){
366                                 //FIXME: This is not 100% correct. We'll only display ou's, but there may be
367                                 //       a step between. You'll stumble in a "hidden" department in this case.
368                                 $base_back= preg_replace("/^[^,]+,/", "", $_SESSION['CurrentMainBase']);
370                                 /* The department array keeps non DN entries as index. We need to convert
371                                    it before checking the existance. */
372                                 $base_back= trim(convert_department_dn($base_back));
374                                 /* Check if the department exists, otherwise revert to the configure base DN */
375                                 if(isset($this->config->departments[$base_back])){
376                                         $this->selectedBase= $this->config->departments[$base_back];
377                                 }else{
378                                         $this->selectedBase= $this->config->departments['/'];
379                                 }
380                         }
381                 }
383                 if(isset($_POST['MultiSelectWindow'.$this->filterName])){
385                         /* Check posts from checkboxes 
386                          */
387                         foreach($this->array_Checkboxes as $key => $box){
388                                 if(isset($_POST[$box['name']])){
389                                         $this->array_Checkboxes[$key]['default'] = true;
390                                         $this->$box['name'] = true;
391                                 }else{
392                                         $this->array_Checkboxes[$key]['default'] = false;
393                                         $this->$box['name'] = false;
394                                 }
395                                 /* Save settings in out session */
396                                 $_SESSION['MultiDialogFilters'][$this->filterName][$box['name']] = $this->$box['name'];
397                         }
399                         /* Check regex posts */
400                         foreach($this->array_Regexes as $key => $box){
401                                 $this->array_Regexes[$key]['value'] = $_POST[$box['name']];
402                                 $this->$box['name'] = $_POST[$box['name']];
403                                 $_SESSION['MultiDialogFilters'][$this->filterName][$box['name']] = $this->$box['name'];
404                         }
406                         /* call close/save if buttons are pressed */
407                         if(isset($_POST['CloseMultiSelectWindow'])){
408                                 $this->Close();
409                         }
411                         if(isset($_POST['SaveMultiSelectWindow'])){
412                                 $this->Save();
413                         }
414                 }
416                 /* check for alphabet selection
417                    Check which regexes are connected to the alphabet 
418                  */     
419                 if(isset($_GET['search'])){
420                         foreach($this->array_Regexes as $key => $box){
421                                 /* check if this regex is connected to the alphabet selection */
422                                 if(($box['connAlpha'])&&(isset($_GET['search']))){
423                                         $val =  $_GET['search']."*";
424                                         $val = preg_replace("/\*\**/","*",$val);
425                                         $this->array_Regexes[$key]['value'] = $val;
426                                         $this->$box['name'] = $val;
427                                 }
428                         }
429                 }
431         }
433         /* this function adds the sub-departments of the current tree to the list */
434         function AddDepartments($base = false,$numtabs = 3)
435         {
436                 $this->DepartmentsAdded = true;
438                 /* check for a valid base */
439                 if(!$base){
440                         if(!isset($_SESSION['CurrentMainBase'])){
441                                 $_SESSION['CurrentMainBase'] = $this->config->current['BASE'];
442                         }
443                         $base = $_SESSION['CurrentMainBase'];
444                 }
446                 /* Create ldap obj and switch into base*/
447                 $ldap = $this->config->get_ldap_link();
448                 $ldap->cd($base);
450                 /* reset current deps */
451                 $this->departments = array();
453                 /* Get all departments within this subtree */
454                 $deps= get_list("(&(|(ou=*)(description=*))(objectClass=gosaDepartment))", $this->ui->subtreeACL,
455                                 $base, array("ou", "description"), GL_SIZELIMIT | GL_CONVERT);
457                 /* Edit delete link for system types
458                  */
459                 $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=dep_open&amp;dep_id=%s'>%s</a>";
461                 /* Create an array with all visible (in the list) departments */
462                 $departments = array();
463                 foreach($deps as $value){
464                         if(isset($value['description'][0])){
465                                 $this->departments[$value['dn']]= get_sub_department($value['dn'])." - [".$value["description"][0]."]";
466                         }else{
467                                 $this->departments[$value['dn']]= get_sub_department($value['dn']);
468                         }
469                 }
470                 natcasesort($this->departments);
472                 /* Add deps to this dialog object list */
473                 foreach($this->departments as $key=> $val){
474                         /* Add missing entries ... */
475                         if(!isset($this->config->departments[trim($key)])){
476                                 $this->config->departments[trim($key)]="";
477                         }
479                         /* check if this department contains sub-departments
480                            Display different image in this case
481                          */
482                         $non_empty="";
483                         $nkey= normalizePreg($key);
484                         foreach($this->config->departments as $keyd=>$vald ){
485                                 if(preg_match("/$nkey\/.*/",$keyd)){
486                                         $non_empty="full";
487                                 }
488                         }
491                         /* Add to divlist */
492                         $row = array();
493                         $row[] = $field1 = array("string" => "<img src='images/".$non_empty."folder.png' alt='department'>", "attach" => "style='text-align:center;width:20px;'");
494                         $row[] = $field2 = array("string" => sprintf($linkopen,base64_encode($key),$val), "attach" => "style=''");
496                         if($numtabs > 2){       
497                                 for($i = 2 ; $i <$numtabs;$i++){
498                                         if($i ==($numtabs-1)){
499                                                 $row[] = array("string"=>"&nbsp;","attach" => "style='width:60px;border-right:0px;text-align:right;'");
500                                         }else{
501                                                 $row[] = array("string"=>"&nbsp;");
502                                         }
503                                 }
504                         }
506                         $this->AddElement($row);
507                 }
508         }
510 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
511 ?>