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 $filter = $_SESSION['glpi_user_filter'];
44 /* Filter settings, remove double* */
45 if(isset($_GET['search'])){
46 $filter['users_regex'] = preg_replace("/\*\**/","*",$_GET['search']."*");
47 }elseif(isset($_POST['regex'])){
48 $filter['users_regex'] = preg_replace("/\*\**/","*",$_POST['regex']);
49 }
50 if(empty($filter['users_regex'])) {
51 $filter = "*";
52 }
55 $s_action="";
56 /* Test Posts */
57 foreach($_POST as $key => $val){
58 // Post for delete
59 if(preg_match("/dep_back.*/i",$key)){
60 $s_action="back";
61 }elseif(preg_match("/user_new.*/",$key)){
62 $s_action="new";
63 }elseif(preg_match("/dep_home.*/i",$key)){
64 $s_action="home";
65 }
66 }
68 /* Homebutton is posted */
69 if($s_action=="home"){
70 $filter['base']=(preg_replace("/^[^,]+,/","",$this->ui->dn));
71 $filter['base']=(preg_replace("/^[^,]+,/","",$filter['base']));
72 }
74 if($s_action=="root"){
75 $filter['base']=($this->config->current['BASE']);
76 }
79 /* If Backbutton is Posted */
80 if($s_action=="back"){
81 $base_back = preg_replace("/^[^,]+,/","",$filter['base']);
82 $base_back = convert_department_dn($base_back);
84 if(isset($this->config->departments[trim($base_back)])){
85 $filter['base']= $this->config->departments[trim($base_back)];
86 }else{
87 $filter['base']= $this->config->departments["/"];
88 }
89 }
91 if((isset($_GET['act']))&&($_GET['act']=="dep_open")){
92 $entry = base64_decode($_GET['dep_id']);
93 $filter['base']= ($this->config->departments[$entry]);
94 }
96 $_SESSION['glpi_user_filter'] = $filter;
98 $this->reload();
100 /* Fill templating stuff */
101 $smarty= get_smarty();
102 $display= "";
104 $divlist = new divlist("glpi users");
105 $divlist->SetEntriesPerPage(0);
106 $divlist->SetHeader(array(
107 array("string" => " ", "attach" => "style='text-align:center;width:20px;'"),
108 array("string" => _("Users")." / "._("Departments"), "attach" => "style=''"),
109 array("string" => _("Use"), "attach" => "style='width:60px;border-right:0px;text-align:right;'" )));
110 $linkopen = "<a href='?plug=".$_GET['plug']."&act=dep_open&dep_id=%s'>%s</a>";
111 foreach($this->departments as $key=> $val){
113 if(!isset($this->config->departments[trim($key)])){
114 $this->config->departments[trim($key)]="";
115 }
117 $non_empty="";
118 $keys= str_replace("/","\/",$key);
119 foreach($this->config->departments as $keyd=>$vald ){
120 if(preg_match("/".$keys."\/.*/",$keyd)){
121 $non_empty="full";
122 }
123 }
125 $field1 = array("string" => "<img src='images/".$non_empty."folder.png' alt='department'>", "attach" => "style='text-align:center;width:20px;'");
126 $field2 = array("string" => sprintf($linkopen,base64_encode($key),$val), "attach" => "style=''");
127 $field3 = array("string" => " ", "attach" => "style='width:60px;border-right:0px;text-align:right;'");
128 $divlist->AddEntry(array($field1,$field2,$field3));
129 }
131 $useruse = "<a href='?plug=".$_GET['plug']."&act=".$this->PostVarName."&id=%s'>%s</a>";
133 foreach($this->users as $key=>$user){
134 $field1 = array("string" => "<img src='images/select_user.png' alt='user'>", "attach" => "style='text-align:center;width:20px;'");
135 $field2 = array("string" => sprintf($useruse,base64_encode($key),$user), "attach" => "style=''");
136 $field3 = array("string" => sprintf($useruse,base64_encode($key),"<img border=0 src='images/crossref.png' alt='"._("use")."'>"),
137 "attach" => "style='width:60px;border-right:0px;text-align:right;'");
138 $divlist->AddEntry(array($field1,$field2,$field3));
140 }
142 $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
143 " <input class='center' type='image' align='middle'
144 src='images/list_back.png' title='"._("Go up one department")."' alt='"._("Up")."' name='dep_back'> ".
145 " <input class='center' type='image' src='images/list_root.png'
146 align='middle' title='"._("Go to root department")."' name='dep_root' alt='"._("Root")."'> ".
147 " <input class='center' type='image' align='middle'
148 src='images/list_home.png' title='"._("Go to users department")."' alt='"._("Home")."' name='dep_home'> ".
149 "</div>";
152 $filter= $_SESSION['glpi_user_filter'];
153 $smarty->assign("usershead", $listhead);
154 $smarty->assign("users", $divlist->DrawList());
155 $smarty->assign("search_image", get_template_path('images/search.png'));
156 $smarty->assign("searchu_image", get_template_path('images/search_user.png'));
157 $smarty->assign("tree_image", get_template_path('images/tree.png'));
158 $smarty->assign("infoimage", get_template_path('images/info.png'));
159 $smarty->assign("launchimage", get_template_path('images/launch.png'));
160 $smarty->assign("apply", apply_filter());
161 $smarty->assign("alphabet", generate_alphabet());
162 $smarty->assign("users_regex", $filter['users_regex']);
165 $display.= $smarty->fetch(get_template_path('glpiSelectUser.tpl', TRUE));
166 return($display);
167 }
169 /* Save to LDAP */
170 function save()
171 {
172 plugin::save();
174 /* Optionally execute a command after we're done */
175 #$this->handle_post_events($mode);
176 }
178 function reload()
179 {
181 $filter= $_SESSION['glpi_user_filter'];
183 $base = $filter['base'];
184 $regex= $filter['users_regex'];
185 /* NEW LIST MANAGMENT
186 * We also need to search for the departments
187 * So we are able to navigate like in konquerer
188 */
190 $res3 = get_list2($this->ui->subtreeACL, "(&(|(ou=*)(description=*))(objectClass=gosaDepartment))",
191 TRUE, $base, array("ou", "description"), TRUE);
193 $this->departments= array();
194 $tmp = array();
195 foreach ($res3 as $value){
196 $tmp[strtolower($value['dn']).$value['dn']]=$value;
197 }
198 ksort($tmp);
199 foreach($tmp as $value){
200 if(isset($value["description"][0])){
201 $this->departments[$value['dn']]=convert_department_dn2($value['dn'])." - [".$value["description"][0]."]";
202 }else{
203 $this->departments[$value['dn']]=convert_department_dn2($value['dn']);//$value["description"][0];
204 }
205 }
207 /* END NEW LIST MANAGMENT
208 */
209 $ou = get_people_ou();
210 $res2 = get_list($this->ui->subtreeACL, "(&(|(cn=$regex)(sn=$regex))(objectClass=person))",
211 TRUE,$ou.$base, array("cn","sn","uid","givenName", "description"), TRUE);
213 $tmp = array();
215 foreach($res2 as $val){
216 $str = "";
217 if(isset($val['givenName'][0])){
218 $str .= $val['givenName'][0];
219 }
220 if(isset($val['sn'][0])){
221 $str .= $val['sn'][0];
222 }
223 if(isset($val['uid'][0])){
224 $str .= $val['uid'][0];
225 }
227 $tmp[$str] = $val;
228 }
230 ksort($tmp);
231 $this->users = array();
232 foreach($tmp as $value){
233 if (isset($value["givenName"][0]) && isset($value["sn"][0])){
234 $this->users[$value['dn']]= $value["sn"][0].", ".
235 $value["givenName"][0].
236 " [".$value["uid"][0]."]";
237 } else {
238 $this->users[$value["dn"]]= "[".$value["uid"][0]."]";
239 }
240 }
241 }
243 }
245 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
246 ?>