Code

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