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" => " ", "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']."&act=dep_open&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" => " ", "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']."&act=".$this->PostVarName."&id=%s'>%s</a>";
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));
144 }
146 /* Prepare departments,
147 which are shown in the listbox on top of the listbox
148 */
149 $options= "";
150 foreach ($this->config->idepartments as $key => $value){
151 if ($_SESSION['glpi_user_filter']['base'] == $key){
152 $options.= "<option selected='selected' value='$key'>$value</option>";
153 } else {
154 $options.= "<option value='$key'>$value</option>";
155 }
156 }
158 $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
159 " <input class='center' type='image' src='images/list_root.png'
160 align='middle' title='"._("Go to root department")."' name='dep_root' alt='"._("Root")."'> ".
161 " <input class='center' type='image' align='middle'
162 src='images/list_back.png' title='"._("Go up one department")."' alt='"._("Up")."' name='dep_back'> ".
163 " <input class='center' type='image' align='middle'
164 src='images/list_home.png' title='"._("Go to users department")."' alt='"._("Home")."' name='dep_home'> ".
165 " <input class='center' type='image' src='images/list_reload.png' align='middle' title='"._("Reload list")."' name='submit_department' alt='"._("Submit")."'> ".
166 " <img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> "._("Base")." ".
167 " <select name='depselect' onChange='mainform.submit()' class='center'>$options</select>".
168 " <input class='center' type='image' src='images/list_submit.png' align='middle'
169 title='"._("Submit department")."' name='submit_department' alt='". _("Submit")."'> ".
170 "</div>";
173 $filter= $_SESSION['glpi_user_filter'];
174 $smarty->assign("usershead", $listhead);
175 $smarty->assign("users", $divlist->DrawList());
176 $smarty->assign("search_image", get_template_path('images/search.png'));
177 $smarty->assign("searchu_image", get_template_path('images/search_user.png'));
178 $smarty->assign("tree_image", get_template_path('images/tree.png'));
179 $smarty->assign("infoimage", get_template_path('images/info_small.png'));
180 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
181 $smarty->assign("apply", apply_filter());
182 $smarty->assign("alphabet", generate_alphabet());
183 $smarty->assign("users_regex", $filter['users_regex']);
186 $display.= $smarty->fetch(get_template_path('glpiSelectUser.tpl', TRUE));
187 return($display);
188 }
190 /* Save to LDAP */
191 function save()
192 {
193 plugin::save();
195 /* Optionally execute a command after we're done */
196 #$this->handle_post_events($mode);
197 }
199 function reload()
200 {
202 $filter= $_SESSION['glpi_user_filter'];
204 $base = $filter['base'];
205 $regex= $filter['users_regex'];
206 /* NEW LIST MANAGMENT
207 * We also need to search for the departments
208 * So we are able to navigate like in konquerer
209 */
211 $res3= get_list("(&(|(ou=*)(description=*))(objectClass=gosaDepartment))",
212 "glpi", $base, array("ou", "description"), GL_SIZELIMIT | GL_CONVERT);
214 $this->departments= array();
215 $tmp = array();
216 foreach ($res3 as $value){
217 $tmp[strtolower($value['dn']).$value['dn']]=$value;
218 }
219 ksort($tmp);
220 foreach($tmp as $value){
221 if(isset($value["description"][0])){
222 $this->departments[$value['dn']]= get_sub_department($value['dn'])." - [".$value["description"][0]."]";
223 }else{
224 $this->departments[$value['dn']]= get_sub_department($value['dn']);//$value["description"][0];
225 }
226 }
228 /* END NEW LIST MANAGMENT
229 */
230 $ou = get_people_ou();
231 $res2= get_list("(&(|(cn=$regex)(sn=$regex))(objectClass=person))", "glpi",
232 $ou.$base, array("cn", "sn", "uid", "givenName", "description"),
233 GL_SUBSEARCH | GL_SIZELIMIT);
235 $tmp = array();
237 foreach($res2 as $val){
238 $str = "";
239 if(isset($val['givenName'][0])){
240 $str .= $val['givenName'][0];
241 }
242 if(isset($val['sn'][0])){
243 $str .= $val['sn'][0];
244 }
245 if(isset($val['uid'][0])){
246 $str .= $val['uid'][0];
247 }
249 $tmp[$str] = $val;
250 }
252 ksort($tmp);
253 $this->users = array();
254 foreach($tmp as $value){
255 if (isset($value["givenName"][0]) && isset($value["sn"][0])){
256 $this->users[$value['dn']]= $value["sn"][0].", ".
257 $value["givenName"][0].
258 " [".$value["uid"][0]."]";
259 } else {
260 $this->users[$value["dn"]]= "[".$value["uid"][0]."]";
261 }
262 }
263 }
265 }
267 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
268 ?>