Code

Added RDN support for users
[gosa.git] / plugins / admin / systems / class_glpiSelectUser.inc
1 <?php
3 class glpiSelectUser extends plugin
4 {
5   /* attribute list for save action */
6   var $ignore_account= TRUE;
7   var $attributes= array();
8   var $objectclasses= array("whatever");
10   var $users = array();
11   var $departments = array();
13   var $ui;
15   var $PostVarName      = "";
17   function glpiSelectUser ($config, $dn= NULL,$postvar)
18   {
19     plugin::plugin ($config, $dn);
21     $this->PostVarName = $postvar;
23     if(!isset($_SESSION['glpi_user_filter'])){
24       $tmp['users_regex'] = "*";
25       $tmp['base']       = $this->config->current['BASE'];
26       $_SESSION['glpi_user_filter'] = $tmp;
27     }
29     $this->ui = get_userinfo();  
30   }
32   function execute()
33   {
34     /* Call parent execute */
35     plugin::execute();
37     if(isset($_POST['depselect'])){
38       $_SESSION['glpi_user_filter']['base'] = $_POST['depselect'];
39     }
41     $filter = $_SESSION['glpi_user_filter'];
43     /* Filter settings, remove double* */
44     if(isset($_GET['search'])){
45       $filter['users_regex'] = preg_replace("/\*\**/","*",$_GET['search']."*");
46     }elseif(isset($_POST['regex'])){
47       $filter['users_regex'] = preg_replace("/\*\**/","*",$_POST['regex']);
48     }
49     if(empty($filter['users_regex'])) {
50       $filter = "*";
51     }
54     $s_action="";
55     /* Test Posts */
56     foreach($_POST as $key => $val){
57       // Post for delete
58       if(preg_match("/dep_back.*/i",$key)){
59         $s_action="back";
60       }elseif(preg_match("/user_new.*/",$key)){
61         $s_action="new";
62       }elseif(preg_match("/dep_home.*/i",$key)){
63         $s_action="home";
64       }
65     }
67     /* Homebutton is posted */
68     if($s_action=="home"){
69       $filter['base']=(preg_replace("/^[^,]+,/","",$this->ui->dn));
70       $filter['base']=(preg_replace("/^[^,]+,/","",$filter['base']));
71     }
73     if($s_action=="root"){
74       $filter['base']=($this->config->current['BASE']);
75     }
78     /* If Backbutton is Posted */
79     if($s_action=="back"){
80       $base_back          = preg_replace("/^[^,]+,/","",$filter['base']);
81       $base_back          = convert_department_dn($base_back);
83       if(isset($this->config->departments[trim($base_back)])){
84         $filter['base']= $this->config->departments[trim($base_back)];
85       }else{
86         $filter['base']= $this->config->departments["/"];
87       }
88     }
90     if((isset($_GET['act']))&&($_GET['act']=="dep_open")){
91       $entry = base64_decode($_GET['dep_id']);
92       $filter['base']= ($this->config->departments[$entry]); 
93     }    
95     $_SESSION['glpi_user_filter'] = $filter;
97     $this->reload();
99     /* Fill templating stuff */
100     $smarty= get_smarty();
101     $display= "";
103     $divlist = new divlist("glpi users");
104     $divlist->SetEntriesPerPage(0);
105     $divlist->SetHeader(array(
106           array("string" => "&nbsp;", "attach" => "style='text-align:center;width:20px;'"),
107           array("string" => _("Users")." / "._("Departments"), "attach" => "style=''"),
108           array("string" => _("Use"), "attach" => "style='width:60px;border-right:0px;text-align:right;'" )));
109     $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=dep_open&amp;dep_id=%s'>%s</a>";
110     foreach($this->departments as $key=> $val){
112       if(!isset($this->config->departments[trim($key)])){
113         $this->config->departments[trim($key)]="";
114       }
116       $non_empty="";
117       $keys= str_replace("/","\/",$key);
118       foreach($this->config->departments as $keyd=>$vald ){
119         if(preg_match("/".$keys."\/.*/",$keyd)){
120           $non_empty="full";
121         }
122       }
124       $field1 = array("string" => "<img src='images/".$non_empty."folder.png' alt='department'>", "attach" => "style='text-align:center;width:20px;'");
125       $field2 = array("string" => sprintf($linkopen,base64_encode($key),$val), "attach" => "style=''");
126       $field3 = array("string" => "&nbsp;", "attach" => "style='width:60px;border-right:0px;text-align:right;'");
127       $divlist->AddEntry(array($field1,$field2,$field3));
128     }
130     $useruse = "<a href='?plug=".$_GET['plug']."&amp;act=".$this->PostVarName."&amp;id=%s'>%s</a>";
131     
132     foreach($this->users as $key=>$user){
133       $field1 = array("string" => "<img src='images/select_user.png' alt='user' >", "attach" => "style='text-align:center;width:20px;'");
134       $field2 = array("string" => sprintf($useruse,base64_encode($key),$user), "attach" => "style=''");
135       $field3 = array("string" => sprintf($useruse,base64_encode($key),"<img title='"._("Use")."' border=0 src='images/crossref.png' alt='"._("use")."'>"), 
136         "attach" => "style='width:60px;border-right:0px;text-align:right;'");
137       $divlist->AddEntry(array($field1,$field2,$field3));
138       
139     }
141      /* Prepare departments,
142        which are shown in the listbox on top of the listbox
143      */
144     $options= "";
145     foreach ($this->config->idepartments as $key => $value){
146       if ($_SESSION['glpi_user_filter']['base'] == $key){
147         $options.= "<option selected='selected' value='$key'>$value</option>";
148       } else {
149         $options.= "<option value='$key'>$value</option>";
150       }
151     }
153     $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
154       " <input class='center' type='image' src='images/list_root.png' 
155       align='middle' title='"._("Go to root department")."' name='dep_root' alt='"._("Root")."'>&nbsp;".
156       " <input class='center' type='image' align='middle' 
157       src='images/list_back.png' title='"._("Go up one department")."' alt='"._("Up")."' name='dep_back'>&nbsp;".
158       " <input class='center' type='image' align='middle' 
159       src='images/list_home.png' title='"._("Go to users department")."' alt='"._("Home")."' name='dep_home'>&nbsp;".
160       " <input class='center' type='image' src='images/list_reload.png' align='middle' title='"._("Reload list")."' name='submit_department' alt='"._("Submit")."'>&nbsp;".
161 " <img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;"._("Base")."&nbsp;".
162     " <select name='depselect' onChange='mainform.submit()' class='center'>$options</select>".
163     " <input class='center' type='image' src='images/list_submit.png' align='middle'
164         title='"._("Submit department")."' name='submit_department' alt='".           _("Submit")."'>&nbsp;".
165       "</div>";
168     $filter= $_SESSION['glpi_user_filter'];
169     $smarty->assign("usershead", $listhead);
170     $smarty->assign("users", $divlist->DrawList());
171     $smarty->assign("search_image", get_template_path('images/search.png'));
172     $smarty->assign("searchu_image", get_template_path('images/search_user.png'));
173     $smarty->assign("tree_image", get_template_path('images/tree.png'));
174     $smarty->assign("infoimage", get_template_path('images/info_small.png'));
175     $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
176     $smarty->assign("apply", apply_filter());
177     $smarty->assign("alphabet", generate_alphabet());
178     $smarty->assign("users_regex", $filter['users_regex']);
181     $display.= $smarty->fetch(get_template_path('glpiSelectUser.tpl', TRUE));
182     return($display);
183   }
185   /* Save to LDAP */
186   function save()
187   {
188     plugin::save();
190     /* Optionally execute a command after we're done */
191 #$this->handle_post_events($mode);
192   }
194   function reload()
195   {
197     $filter= $_SESSION['glpi_user_filter'];
199     $base = $filter['base'];
200     $regex= $filter['users_regex'];
201     /* NEW LIST MANAGMENT
202      * We also need to search for the departments
203      * So we are able to navigate like in konquerer
204      */
206     $res3= get_list("(&(|(ou=*)(description=*))(objectClass=gosaDepartment))",
207                     $this->ui->subtreeACL, $base, array("ou", "description"), GL_SIZELIMIT | GL_CONVERT);
209     $this->departments= array();
210     $tmp = array();
211     foreach ($res3 as $value){
212       $tmp[strtolower($value['dn']).$value['dn']]=$value;
213     }
214     ksort($tmp);
215     foreach($tmp as $value){
216       if(isset($value["description"][0])){
217         $this->departments[$value['dn']]= get_sub_department($value['dn'])." - [".$value["description"][0]."]";
218       }else{
219         $this->departments[$value['dn']]= get_sub_department($value['dn']);//$value["description"][0];
220       }
221     }
223     /* END NEW LIST MANAGMENT
224      */
225     $ou = get_people_ou();
226     $res2= get_list("(&(|(cn=$regex)(sn=$regex))(objectClass=person))", $this->ui->subtreeACL,
227                     $ou.$base, array("cn", "sn", "uid", "givenName", "description"),
228                     GL_SUBSEARCH | GL_SIZELIMIT);
230     $tmp = array();
232     foreach($res2 as $val){
233       $str = "";
234       if(isset($val['givenName'][0])){
235         $str .= $val['givenName'][0];
236       }
237       if(isset($val['sn'][0])){
238         $str .= $val['sn'][0];
239       }
240       if(isset($val['uid'][0])){
241         $str .= $val['uid'][0];
242       }
244       $tmp[$str] = $val;
245     }
247     ksort($tmp);
248     $this->users = array();
249     foreach($tmp as $value){
250       if (isset($value["givenName"][0]) && isset($value["sn"][0])){
251         $this->users[$value['dn']]= $value["sn"][0].", ".
252           $value["givenName"][0].
253           " [".$value["uid"][0]."]";
254       } else {
255         $this->users[$value["dn"]]= "[".$value["uid"][0]."]";
256       }
257     }
258   }
262 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
263 ?>