Code

Removed duplicated post call
[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      */
150     
151     /* Get all departments within this subtree */
152     $this->base = $_SESSION['glpi_user_filter']['base'];
153     $options  ="";
154     $base = $this->config->current['BASE'];
155     $deps= get_list("(&(|(ou=*)(description=*))(objectClass=gosaDepartment))", "users", $base,
156                     array("ou", "description"), GL_SIZELIMIT | GL_SUBSEARCH);
158     /* Load possible departments */
159     $ui= get_userinfo();
160     $tdeps= $ui->get_module_departments("users");
161     $ids = $this->config->idepartments;
162     foreach($deps as $dep){
163       if(isset($ids[$dep['dn']]) && in_array_ics($dep['dn'], $tdeps)){
164         $value = $ids[$dep['dn']];
165         if ($this->base == $dep['dn']){
166           $options.= "<option selected='selected' value='".$dep['dn']."'>$value</option>";
167         } else {
168           $options.= "<option value='".$dep['dn']."'>$value</option>";
169         }
170       }
171     }
173     $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
174       " <input class='center' type='image' src='images/list_root.png' 
175       align='middle' title='"._("Go to root department")."' name='dep_root' alt='"._("Root")."'>&nbsp;".
176       " <input class='center' type='image' align='middle' 
177       src='images/list_back.png' title='"._("Go up one department")."' alt='"._("Up")."' name='dep_back'>&nbsp;".
178       " <input class='center' type='image' align='middle' 
179       src='images/list_home.png' title='"._("Go to users department")."' alt='"._("Home")."' name='dep_home'>&nbsp;".
180       " <input class='center' type='image' src='images/list_reload.png' align='middle' title='"._("Reload list")."' name='submit_department' alt='"._("Submit")."'>&nbsp;".
181 " <img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;"._("Base")."&nbsp;".
182     " <select name='depselect' onChange='mainform.submit()' class='center'>$options</select>".
183     " <input class='center' type='image' src='images/list_submit.png' align='middle'
184         title='"._("Submit department")."' name='submit_department' alt='".           _("Submit")."'>&nbsp;".
185       "</div>";
188     $filter= $_SESSION['glpi_user_filter'];
189     $smarty->assign("usershead", $listhead);
190     $smarty->assign("users", $divlist->DrawList());
191     $smarty->assign("search_image", get_template_path('images/search.png'));
192     $smarty->assign("searchu_image", get_template_path('images/search_user.png'));
193     $smarty->assign("tree_image", get_template_path('images/tree.png'));
194     $smarty->assign("infoimage", get_template_path('images/info_small.png'));
195     $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
196     $smarty->assign("apply", apply_filter());
197     $smarty->assign("alphabet", generate_alphabet());
198     $smarty->assign("users_regex", $filter['users_regex']);
201     $display.= $smarty->fetch(get_template_path('glpiSelectUser.tpl', TRUE));
202     return($display);
203   }
205   /* Save to LDAP */
206   function save()
207   {
208     plugin::save();
210     /* Optionally execute a command after we're done */
211 #$this->handle_post_events($mode);
212   }
214   function reload()
215   {
217     $filter= $_SESSION['glpi_user_filter'];
219     $base = $filter['base'];
220     $regex= $filter['users_regex'];
221     /* NEW LIST MANAGMENT
222      * We also need to search for the departments
223      * So we are able to navigate like in konquerer
224      */
226     $res3= get_list("(&(|(ou=*)(description=*))(objectClass=gosaDepartment))","users",
227        $base, array("ou", "description"), GL_SIZELIMIT | GL_CONVERT);
229     $this->departments= array();
230     $tmp = array();
231     foreach ($res3 as $value){
232       $tmp[strtolower($value['dn']).$value['dn']]=$value;
233     }
234     ksort($tmp);
235     foreach($tmp as $value){
236       if(isset($value["description"][0])){
237         $this->departments[$value['dn']]= get_sub_department($value['dn'])." - [".$value["description"][0]."]";
238       }else{
239         $this->departments[$value['dn']]= get_sub_department($value['dn']);//$value["description"][0];
240       }
241     }
243     /* END NEW LIST MANAGMENT
244      */
245     $ou = get_people_ou();
247     $res2= get_list("(&(|(cn=$regex)(sn=$regex))(objectClass=person))", "users",
248                     $ou.$base, array("cn", "sn", "uid", "givenName", "description"),
249                     GL_SUBSEARCH | GL_SIZELIMIT);
251     $tmp = array();
253     foreach($res2 as $val){
254       $str = "";
255       if(isset($val['givenName'][0])){
256         $str .= $val['givenName'][0];
257       }
258       if(isset($val['sn'][0])){
259         $str .= $val['sn'][0];
260       }
261       if(isset($val['uid'][0])){
262         $str .= $val['uid'][0];
263       }
265       $tmp[$str] = $val;
266     }
268     ksort($tmp);
269     $this->users = array();
270     foreach($tmp as $value){
271       if (isset($value["givenName"][0]) && isset($value["sn"][0])){
272         $this->users[$value['dn']]= $value["sn"][0].", ".
273           $value["givenName"][0].
274           " [".$value["uid"][0]."]";
275       } else {
276         $this->users[$value["dn"]]= "[".$value["uid"][0]."]";
277       }
278     }
279   }
283 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
284 ?>