Code

* Created "old" branch and moved stuff
[gosa.git] / branches / old / gosa-plugins / glpi / admin / systems / services / glpi / class_glpiSelectUser.inc
1 <?php
3 class glpiSelectUser extends plugin
4 {
5   /* attribute list for save action */
6   var $ignore_account= TRUE;
7   var $attributes= array();
8   var $objectclasses= array("whatever");
10   var $users = array();
11   var $departments = array();
13   var $ui;
15   var $PostVarName      = "";
17   function glpiSelectUser (&$config, $dn= NULL,$postvar)
18   {
19     plugin::plugin ($config, $dn);
21     $this->PostVarName = $postvar;
23     if(!session::is_set('glpi_user_filter')){
24       $tmp['users_regex'] = "*";
25       $tmp['base']       = $this->config->current['BASE'];
26       session::set('glpi_user_filter',$tmp);
27     }
29     $this->ui = get_userinfo();  
30   }
32   function execute()
33   {
34     /* Call parent execute */
35     plugin::execute();
37     if(isset($_POST['depselect'])){
38       session::set('glpi_user_filter', array('base' => $_POST['depselect']));
39     }
41     $filter  = session::get('glpi_user_filter');
43     /* Filter settings, remove double* */
44     if(isset($_GET['search'])){
45       $filter['users_regex'] = preg_replace("/\*\**/","*",$_GET['search']."*");
46     }elseif(isset($_POST['regex'])){
47       $filter['users_regex'] = preg_replace("/\*\**/","*",$_POST['regex']);
48     }
49     if(empty($filter['users_regex'])) {
50       $filter = "*";
51     }
54     $s_action="";
55     /* Test Posts */
56     foreach($_POST as $key => $val){
57       // Post for delete
58       if(preg_match("/dep_back.*/i",$key)){
59         $s_action="back";
60       }elseif(preg_match("/user_new.*/",$key)){
61         $s_action="new";
62       }elseif(preg_match("/dep_home.*/i",$key)){
63         $s_action="home";
64       }
65     }
67     /* Homebutton is posted */
68     if($s_action=="home"){
69       $filter['base']=(preg_replace("/^[^,]+,/","",$this->ui->dn));
70       $filter['base']=(preg_replace("/^[^,]+,/","",$filter['base']));
71     }
73     if($s_action=="root"){
74       $filter['base']=($this->config->current['BASE']);
75     }
78     /* If Backbutton is Posted */
79     if($s_action=="back"){
80       $base_back          = preg_replace("/^[^,]+,/","",$filter['base']);
81       $base_back          = convert_department_dn($base_back);
83       if(isset($this->config->departments[trim($base_back)])){
84         $filter['base']= $this->config->departments[trim($base_back)];
85       }else{
86         $filter['base']= $this->config->departments["/"];
87       }
88     }
90     if((isset($_GET['act']))&&($_GET['act']=="dep_open")){
91       $entry = base64_decode($_GET['dep_id']);
92       $filter['base']= ($this->config->departments[$entry]); 
93     }    
95     session::set('glpi_user_filter',$filter);
97     $this->reload();
99     /* Fill templating stuff */
100     $smarty= get_smarty();
101     $display= "";
103     $divlist = new divlist("glpi users");
104     $divlist->SetPluginMode();
105     $divlist->SetEntriesPerPage(0);
106     $divlist->SetHeader(array(
107           array("string" => "&nbsp;", "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']."&amp;act=dep_open&amp;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" => "&nbsp;", "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']."&amp;act=".$this->PostVarName."&amp;id=%s'>%s</a>";
132     
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 title='"._("Use")."' border=0 src='plugins/glpi/images/crossref.png' alt='"._("use")."'>"), 
137         "attach" => "style='width:60px;border-right:0px;text-align:right;'");
138       $divlist->AddEntry(array($field1,$field2,$field3));
139       
140     }
142      /* Prepare departments,
143        which are shown in the listbox on top of the listbox
144      */
146     
147     /* Get all departments within this subtree */
148     $glpi_user_filter = session::get('glpi_user_filter');
149     $this->base = $glpi_user_filter['base'];
150     $options  ="";
151     $base = $this->config->current['BASE'];
152     $deps= get_list("(&(|(ou=*)(description=*))(objectClass=gosaDepartment))", "users", $base,
153                     array("ou", "description"), GL_SIZELIMIT | GL_SUBSEARCH);
155     /* Load possible departments */
156     $ui= get_userinfo();
157     $tdeps= $ui->get_module_departments("users");
158     $ids = $this->config->idepartments;
159     foreach($deps as $dep){
160       if(isset($ids[$dep['dn']]) && in_array_ics($dep['dn'], $tdeps)){
161         $value = $ids[$dep['dn']];
162         if ($this->base == $dep['dn']){
163           $options.= "<option selected='selected' value='".$dep['dn']."'>$value</option>";
164         } else {
165           $options.= "<option value='".$dep['dn']."'>$value</option>";
166         }
167       }
168     }
170     $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
171       " <input class='center' type='image' src='images/lists/root.png' 
172       align='middle' title='"._("Go to root department")."' name='dep_root' alt='"._("Root")."'>&nbsp;".
173       " <input class='center' type='image' align='middle' 
174       src='images/lists/back.png' title='"._("Go up one department")."' alt='"._("Up")."' name='dep_back'>&nbsp;".
175       " <input class='center' type='image' align='middle' 
176       src='images/lists/home.png' title='"._("Go to users department")."' alt='"._("Home")."' name='dep_home'>&nbsp;".
177       " <input class='center' type='image' src='images/lists/reload.png' align='middle' title='"._("Reload list")."' name='submit_department' alt='"._("Submit")."'>&nbsp;".
178 " <img class='center' src='images/lists/seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;"._("Base")."&nbsp;".
179     " <select name='depselect' onChange='mainform.submit()' class='center'>$options</select>".
180     " <input class='center' type='image' src='images/lists/submit.png' align='middle'
181         title='"._("Submit department")."' name='submit_department' alt='".           _("Submit")."'>&nbsp;".
182       "</div>";
185     $filter = session::get('glpi_user_filter');
186     $smarty->assign("usershead", $listhead);
187     $smarty->assign("users", $divlist->DrawList());
188     $smarty->assign("search_image", get_template_path('images/lists/search.png'));
189     $smarty->assign("searchu_image", get_template_path('images/lists/search-user.png'));
190     $smarty->assign("tree_image", get_template_path('images/lists/search-subtree.png'));
191     $smarty->assign("infoimage", get_template_path('images/info_small.png'));
192     $smarty->assign("launchimage", get_template_path('images/lists/action.png'));
193     $smarty->assign("apply", apply_filter());
194     $smarty->assign("alphabet", generate_alphabet());
195     $smarty->assign("users_regex", $filter['users_regex']);
198     $display.= $smarty->fetch(get_template_path('glpiSelectUser.tpl',TRUE,dirname(__FILE__)));
199     return($display);
200   }
202   /* Save to LDAP */
203   function save()
204   {
205     plugin::save();
207     /* Optionally execute a command after we're done */
208 #$this->handle_post_events($mode);
209   }
211   function reload()
212   {
214     $filter = session::get('glpi_user_filter');
216     $base = $filter['base'];
217     $regex= $filter['users_regex'];
218     /* NEW LIST MANAGMENT
219      * We also need to search for the departments
220      * So we are able to navigate like in konquerer
221      */
223     $res3= get_list("(&(|(ou=*)(description=*))(objectClass=gosaDepartment))","users",
224        $base, array("ou", "description"), GL_SIZELIMIT | GL_CONVERT);
226     $this->departments= array();
227     $tmp = array();
228     foreach ($res3 as $value){
229       $tmp[strtolower($value['dn']).$value['dn']]=$value;
230     }
231     ksort($tmp);
232     foreach($tmp as $value){
233       if(isset($value["description"][0])){
234         $this->departments[$value['dn']]= get_sub_department($value['dn'])." - [".$value["description"][0]."]";
235       }else{
236         $this->departments[$value['dn']]= get_sub_department($value['dn']);//$value["description"][0];
237       }
238     }
240     /* END NEW LIST MANAGMENT
241      */
242     $ou = get_people_ou();
244     $res2= get_list("(&(|(cn=$regex)(sn=$regex))(objectClass=person))", "users",
245                     $ou.$base, array("cn", "sn", "uid", "givenName", "description"),
246                     GL_SUBSEARCH | GL_SIZELIMIT);
248     $tmp = array();
250     foreach($res2 as $val){
251       $str = "";
252       if(isset($val['givenName'][0])){
253         $str .= $val['givenName'][0];
254       }
255       if(isset($val['sn'][0])){
256         $str .= $val['sn'][0];
257       }
258       if(isset($val['uid'][0])){
259         $str .= $val['uid'][0];
260       }
262       $tmp[$str] = $val;
263     }
265     ksort($tmp);
266     $this->users = array();
267     foreach($tmp as $value){
268       if (isset($value["givenName"][0]) && isset($value["sn"][0])){
269         $this->users[$value['dn']]= $value["sn"][0].", ".
270           $value["givenName"][0].
271           " [".$value["uid"][0]."]";
272       } else {
273         $this->users[$value["dn"]]= "[".$value["uid"][0]."]";
274       }
275     }
276   }
280 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
281 ?>