Code

Updated trunk, introduced gosa-core
[gosa.git] / plugins / admin / systems / services / glpi / 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->SetPluginMode();
110     $divlist->SetEntriesPerPage(0);
111     $divlist->SetHeader(array(
112           array("string" => "&nbsp;", "attach" => "style='text-align:center;width:20px;'"),
113           array("string" => _("Users")." / "._("Departments"), "attach" => "style=''"),
114           array("string" => _("Use"), "attach" => "style='width:60px;border-right:0px;text-align:right;'" )));
115     $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=dep_open&amp;dep_id=%s'>%s</a>";
116     foreach($this->departments as $key=> $val){
118       if(!isset($this->config->departments[trim($key)])){
119         $this->config->departments[trim($key)]="";
120       }
122       $non_empty="";
123       $keys= str_replace("/","\/",$key);
124       foreach($this->config->departments as $keyd=>$vald ){
125         if(preg_match("/".$keys."\/.*/",$keyd)){
126           $non_empty="full";
127         }
128       }
130       $field1 = array("string" => "<img src='images/".$non_empty."folder.png' alt='department'>", "attach" => "style='text-align:center;width:20px;'");
131       $field2 = array("string" => sprintf($linkopen,base64_encode($key),$val), "attach" => "style=''");
132       $field3 = array("string" => "&nbsp;", "attach" => "style='width:60px;border-right:0px;text-align:right;'");
133       $divlist->AddEntry(array($field1,$field2,$field3));
134     }
136     $useruse = "<a href='?plug=".$_GET['plug']."&amp;act=".$this->PostVarName."&amp;id=%s'>%s</a>";
137     
138     foreach($this->users as $key=>$user){
139       $field1 = array("string" => "<img src='images/select_user.png' alt='user' >", "attach" => "style='text-align:center;width:20px;'");
140       $field2 = array("string" => sprintf($useruse,base64_encode($key),$user), "attach" => "style=''");
141       $field3 = array("string" => sprintf($useruse,base64_encode($key),"<img title='"._("Use")."' border=0 src='images/crossref.png' alt='"._("use")."'>"), 
142         "attach" => "style='width:60px;border-right:0px;text-align:right;'");
143       $divlist->AddEntry(array($field1,$field2,$field3));
144       
145     }
147      /* Prepare departments,
148        which are shown in the listbox on top of the listbox
149      */
151     
152     /* Get all departments within this subtree */
153     $this->base = $_SESSION['glpi_user_filter']['base'];
154     $options  ="";
155     $base = $this->config->current['BASE'];
156     $deps= get_list("(&(|(ou=*)(description=*))(objectClass=gosaDepartment))", "users", $base,
157                     array("ou", "description"), GL_SIZELIMIT | GL_SUBSEARCH);
159     /* Load possible departments */
160     $ui= get_userinfo();
161     $tdeps= $ui->get_module_departments("users");
162     $ids = $this->config->idepartments;
163     foreach($deps as $dep){
164       if(isset($ids[$dep['dn']]) && in_array_ics($dep['dn'], $tdeps)){
165         $value = $ids[$dep['dn']];
166         if ($this->base == $dep['dn']){
167           $options.= "<option selected='selected' value='".$dep['dn']."'>$value</option>";
168         } else {
169           $options.= "<option value='".$dep['dn']."'>$value</option>";
170         }
171       }
172     }
174     $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
175       " <input class='center' type='image' src='images/list_root.png' 
176       align='middle' title='"._("Go to root department")."' name='dep_root' alt='"._("Root")."'>&nbsp;".
177       " <input class='center' type='image' align='middle' 
178       src='images/list_back.png' title='"._("Go up one department")."' alt='"._("Up")."' name='dep_back'>&nbsp;".
179       " <input class='center' type='image' align='middle' 
180       src='images/list_home.png' title='"._("Go to users department")."' alt='"._("Home")."' name='dep_home'>&nbsp;".
181       " <input class='center' type='image' src='images/list_reload.png' align='middle' title='"._("Reload list")."' name='submit_department' alt='"._("Submit")."'>&nbsp;".
182 " <img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;"._("Base")."&nbsp;".
183     " <select name='depselect' onChange='mainform.submit()' class='center'>$options</select>".
184     " <input class='center' type='image' src='images/list_submit.png' align='middle'
185         title='"._("Submit department")."' name='submit_department' alt='".           _("Submit")."'>&nbsp;".
186       "</div>";
189     $filter= $_SESSION['glpi_user_filter'];
190     $smarty->assign("usershead", $listhead);
191     $smarty->assign("users", $divlist->DrawList());
192     $smarty->assign("search_image", get_template_path('images/search.png'));
193     $smarty->assign("searchu_image", get_template_path('images/search_user.png'));
194     $smarty->assign("tree_image", get_template_path('images/tree.png'));
195     $smarty->assign("infoimage", get_template_path('images/info_small.png'));
196     $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
197     $smarty->assign("apply", apply_filter());
198     $smarty->assign("alphabet", generate_alphabet());
199     $smarty->assign("users_regex", $filter['users_regex']);
202     $display.= $smarty->fetch(get_template_path('glpiSelectUser.tpl', TRUE));
203     return($display);
204   }
206   /* Save to LDAP */
207   function save()
208   {
209     plugin::save();
211     /* Optionally execute a command after we're done */
212 #$this->handle_post_events($mode);
213   }
215   function reload()
216   {
218     $filter= $_SESSION['glpi_user_filter'];
220     $base = $filter['base'];
221     $regex= $filter['users_regex'];
222     /* NEW LIST MANAGMENT
223      * We also need to search for the departments
224      * So we are able to navigate like in konquerer
225      */
227     $res3= get_list("(&(|(ou=*)(description=*))(objectClass=gosaDepartment))","users",
228        $base, array("ou", "description"), GL_SIZELIMIT | GL_CONVERT);
230     $this->departments= array();
231     $tmp = array();
232     foreach ($res3 as $value){
233       $tmp[strtolower($value['dn']).$value['dn']]=$value;
234     }
235     ksort($tmp);
236     foreach($tmp as $value){
237       if(isset($value["description"][0])){
238         $this->departments[$value['dn']]= get_sub_department($value['dn'])." - [".$value["description"][0]."]";
239       }else{
240         $this->departments[$value['dn']]= get_sub_department($value['dn']);//$value["description"][0];
241       }
242     }
244     /* END NEW LIST MANAGMENT
245      */
246     $ou = get_people_ou();
248     $res2= get_list("(&(|(cn=$regex)(sn=$regex))(objectClass=person))", "users",
249                     $ou.$base, array("cn", "sn", "uid", "givenName", "description"),
250                     GL_SUBSEARCH | GL_SIZELIMIT);
252     $tmp = array();
254     foreach($res2 as $val){
255       $str = "";
256       if(isset($val['givenName'][0])){
257         $str .= $val['givenName'][0];
258       }
259       if(isset($val['sn'][0])){
260         $str .= $val['sn'][0];
261       }
262       if(isset($val['uid'][0])){
263         $str .= $val['uid'][0];
264       }
266       $tmp[$str] = $val;
267     }
269     ksort($tmp);
270     $this->users = array();
271     foreach($tmp as $value){
272       if (isset($value["givenName"][0]) && isset($value["sn"][0])){
273         $this->users[$value['dn']]= $value["sn"][0].", ".
274           $value["givenName"][0].
275           " [".$value["uid"][0]."]";
276       } else {
277         $this->users[$value["dn"]]= "[".$value["uid"][0]."]";
278       }
279     }
280   }
284 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
285 ?>