Code

bca4a45344e643c671c232a31608533c155e4af3
[gosa.git] / plugins / admin / departments / class_departmentManagement.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2003  Cajus Pollmeier
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 require "tabs_department.inc";
22 class departmentManagement extends plugin
23 {
24         /* Definitions */
25         var $plHeadline= "Departments";
26         var $plDescription= "This does something";
28         /* CLI vars */
29         var $cli_summary= "Handling of LDAP subtrees";
30         var $cli_description= "Some longer text\nfor help";
31         var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
33         /* Headpage attributes */
34         var $last_dep_sorting= "invalid";
35         var $departments= array();
36         var $deptabs= NULL;
38         /* attribute list for save action */
39         var $attributes= array();
40         var $objectclasses= array();
42         function departmentManagement ($config, $ui)
43         {
44                 $this->ui= $ui;
45                 $this->dn= "";
46                 $this->config= $config;
48                 /* Get global filter config */
49                 if (!is_global("depfilter")){
50                         $base= get_base_from_people($ui->dn);
51                         $depfilter= array("depselect" => $base,
52                                         "regex" => "*");
53                         register_global("depfilter", $depfilter);
54                 }
55         }
57         function execute()
58         {
59         /* Call parent execute */
60         plugin::execute();
62         $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^dep_edit_.*/","/^dep_del_.*/");
64                 /* Reload departments */
65                 $this->config->departments      = get_departments();
66                 $smarty                                         = get_smarty();
67                 $display                                        = "";
68                 $depfilter                                      = get_global("depfilter");
69                 $s_action                                       = "";  // Will contain an action, like del or edit
70                 $s_entry                                        = "";  // The entry name for edit delete -...
72                 $this->config->make_idepartments();
74                 if (isset($_POST['regex'])){
75                         $depfilter['regex']= $_POST['regex'];
76                 }
78                 // Check Post action
79                 foreach($_POST as $key => $val){
80                         // Post for delete
81                         if(preg_match("/dep_del.*/",$key)){
82                                 $s_action = "del";
83                                 $s_entry  = preg_replace("/dep_".$s_action."_/i","",$key);
84                                 $s_entry  = preg_replace("/_.*$/","",$s_entry);
85                                 $s_entry  = base64_decode($s_entry);
86                         // Post for edit
87                         }elseif(preg_match("/dep_edit_.*/",$key)){
88                                 $s_action="edit";
89                                 $s_entry  = preg_replace("/dep_".$s_action."_/i","",$key);
90                                 $s_entry  = preg_replace("/_.*$/","",$s_entry);
91                                 $s_entry  = base64_decode($s_entry);
92                                 // Post for new
93                         }elseif(preg_match("/dep_new.*/",$key)){
94                                 $s_action="new";
95                         }elseif(preg_match("/dep_back.*/i",$key)){
96                                 $s_action="back";
97                         }elseif(preg_match("/dep_home.*/i",$key)){
98                                 $s_action="home";
99                         }elseif(preg_match("/dep_root.*/i",$key)){
100                                 $s_action="root";
101                         }
102                 
103                 }
105                 if(isset($_POST['depselect']) && $_POST['depselect']){
106                         $depfilter['depselect']= $_POST['depselect'];
107                 }
108                 
109                 if($s_action=="root"){
110                         $depfilter['depselect']=($this->config->current['BASE']);
111                 }
113                 if($s_action=="home"){
114                         $depfilter['depselect']=(preg_replace("/^[^,]+,/","",$this->ui->dn));
115                         $depfilter['depselect']=(preg_replace("/^[^,]+,/","",$depfilter['depselect']));
116                 }
118                 if($s_action=="back"){
119                         $base_back                      = preg_replace("/^[^,]+,/","",$depfilter['depselect']);
120                         $base_back          = convert_department_dn($base_back);
121             
122                         if(isset($this->config->departments[trim($base_back)])){
123                                 $depfilter['depselect']= $this->config->departments[trim($base_back)];
124                         }else{
125                                 $depfilter['depselect']= $this->config->departments["/"];
126                         }
127                 }
129                 if(isset($_GET['act'])&&($_GET['act']=="dep_open")){
130                         $s_action="open";
131                         $s_entry = base64_decode($_GET['dep_id']);
132                         $depfilter['depselect']= $this->config->departments[trim($s_entry)];
133                 }
135                 if (isset($_GET['search'])){
136                         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
137                         if ($s == "**"){
138                                 $s= "*";
139                         }
140                         $depfilter['regex']= $s;
141                 }
142                 register_global("depfilter", $depfilter);
144                 /* New Entry if Posted action (s_action) == new
145                  */
146                 if ($s_action=="new"){
147                         $this->dn= "new";
148                         $this->deptabs= new deptabs($this->config,
149                                         $this->config->data['TABS']['DEPTABS'], $this->dn);
150                 }
152                 /* Edit Entry if Posted action (s_action) == edit 
153                  * The entry which will be edited is defined in $s_entry
154                  */
155                 if (( $s_action=="edit") && (!isset($this->deptabs->config))){
156                         $this->dn= $this->config->departments[trim($s_entry)];
158                         if (($user= get_lock($this->dn)) != ""){
159                                 return(gen_locked_message ($user, $this->dn));
160                         }
162                         /* Lock the current entry, so everyone will get the  above dialog */
163                         add_lock ($this->dn, $this->ui->dn);
165                         /* Set up the users ACL's for this 'dn' */
166                         $this->acl= get_permissions ($this->dn, $this->ui->subtreeACL);
168                         /* Register deptabs to trigger edit dialog */
169                         $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn);
170                         $this->deptabs->set_acl($this->acl);
171                         $_SESSION['objectinfo']= $this->dn;
172                 }
174                 /* Delete Entry if Posted action (s_action) == del 
175          * The entry which will be deleted is defined in $s_entry
176                  */
177                 if ($s_action =="del"){
178                         $this->dn= $this->config->departments[trim($s_entry)];
180                         /* Check locking */
181                         if (($user= get_lock($this->dn)) != ""){
182                                 $_SESSION['dn']= $this->dn;
183                                 return(gen_locked_message($user, $this->dn));
184                         } else {
185                                 add_lock ($this->dn, $this->ui->dn);
186                                 $smarty->assign("info", sprintf(_("You're about to delete the whole LDAP subtree placed under '%s'."), LDAP::fix($this->dn)));
187                                 $display.= $smarty->fetch (get_template_path('remove.tpl', TRUE));
188                                 return ($display);
189                         }
190                 }
192                 /* If department deletion is accepted ...
193                  * Finally delete department 
194                  */
195                 if (isset($_POST['delete_department_confirm'])){
196                         $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
197                         $acl= get_module_permission($acl, "department", $this->dn);
198                         if (chkacl($acl, "delete") == ""){
199                                 $this->remove_from_parent();
200                                 gosa_log ("Department object'".$this->dn."' has been removed");
201                                 $this->reload ();
202                         } else {
203                                 print_red (_("You have no permission to remove this department."));
204                         }
205                 }
207                 /* Edit Complete ...
208                  * Finish request 
209                  */
210                 if((isset($_GET['PerformRecMove'])) &&( $this->deptabs->by_object['department']->am_i_moved())){
211                         $this->deptabs->save(true);
212                         $this->deptabs->by_object['department']->recursive_move("","",true);
213                         $this->reload ();
214                         /* Group has been saved successfully, remove lock from
215                            LDAP. */
216                         if ($this->dn != "new"){
217                                 del_lock ($this->dn);
218                         }
220                         unset ($this->deptabs);
221                         $this->deptabs= NULL;
222                         unset ($_SESSION['objectinfo']);
223                         exit();
224                 }
226                 if ((isset($_POST['edit_finish'])) && (isset($this->deptabs->config) ) ){
228                         /* Check tabs, will feed message array */
229                         $message= $this->deptabs->check();
231                         /* Save, or display error message? */
232                         if (count($message) == 0){
234                                 /* Save user data to ldap */
235                                 $disp = $this->deptabs->save();
236                                 if(!$this->deptabs->by_object['department']->am_i_moved()){
237                                         gosa_log ("Department object'".$this->dn."' has been saved");
239                                         /* Group has been saved successfully, remove lock from
240                                            LDAP. */
241                                         if ($this->dn != "new"){
242                                                 del_lock ($this->dn);
243                                         }
245                                         /* There's no page reload so we have to read new users at
246                                            this point. */
247                                         $this->reload ();
248                                         unset ($this->deptabs);
249                                         $this->deptabs= NULL;
250                                         unset ($_SESSION['objectinfo']);
251                                 }else{
252                                         return($disp);
253                                 }
254                         } else {
255                                 /* Ok. There seem to be errors regarding to the tab data,
256                                    show message and continue as usual. */
257                                 show_errors($message);
258                         }
259                 }
262                 /* User cancelt edit oder delete
263          * Cancel dialog 
264          */
265                 if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) ||
266                                 isset($_POST['delete_department_confirm'])){
268                         del_lock ($this->dn);
269                         unset($this->depdabs);
270                         $this->deptabs= NULL;
271                         unset ($_SESSION['objectinfo']);
272                 }
274                 /* Headpage or normal plugin screen? */
275                 if ($this->deptabs == NULL){
276                         /* Check sorting variable */
277                         $this->reload();
279                         /* Check for exeeded sizelimit */
280                         if (($message= check_sizelimit()) != ""){
281                                 return($message);
282                         }
284                         /* Prepare departments */
285                         $options= "";
286                         foreach ($this->config->idepartments as $key => $value){
287                                 if ($depfilter['depselect'] == $key){
288                                         $options.= "<option selected value='$key'>$value</option>";
289                                 } else {
290                                         $options.= "<option value='$key'>$value</option>";
291                                 }
292                         }
294                         /* Generate list head */
295                         $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
296                                                 " <input type='image' class='center' src='images/list_root.png' align='middle' title='"._("Go to root department")."' name='dep_root' alt='"._("Root")."'>&nbsp;".
297                                                 " <input type='image' class='center' src='images/list_up.png' align='middle' title='"._("Go up one department")."' name='dep_back' alt='"._("Up")."'>&nbsp;".
298                                                 " <input type='image' class='center' src='images/list_home.png' align='middle' title='"._("Go to users home department")."' name='dep_home' alt='"._("Home")."'>&nbsp;".
299                                                 " <input class='center' type='image' src='images/list_reload.png' align='middle' title='"._("Reload list")."' name='submit_department' alt='"._("Submit")."'>&nbsp;".
300                                                 " <img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;".
301                                                 " <input class='center' type='image' src='images/list_new_department.png' align='middle' alt='"._("Create new department")."' name='dep_new'>&nbsp;".
302                                                 " <img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;".
303                                                 _("Base")."&nbsp;<select name='depselect' onChange='mainform.submit()' class='center'>$options</select>".
304                                                 " <input class='center' type='image' src='images/list_submit.png' align='middle' title='"._("Submit department")."' name='submit_department' alt='"._("Submit")."'>&nbsp;".
305                                                 "</div>";
307                         /* Show main page */
308                         $divlist = new divlist("departmenttabs");
309                         $divlist->SetSummary(_("This table displays all departments, in the selected tree."));
310                         $divlist->SetEntriesPerPage(0);
311                         $actions= "<input class='center' type='image' src='images/edit.png'              alt='"._("edit")."'    name='dep_edit_%KEY%' title='"._("Edit this entry")."'>";
312                         $actions.= "<input class='center' type='image' src='images/edittrash.png' alt='"._("delete")."'         name='dep_del_%KEY%' title='"._("Delete this entry")."'>";
314                         $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=dep_open&amp;dep_id=%s'>%s</a>";
316                         $divlist->SetHeader(array(
317                                                 array("string" => "&nbsp;", "attach" => "style='text-align:center;width:20px;'"),
318                                                 array("string"=>_("Department name"), "attach" => "style=''"),
319                                                 array("string" =>_("Actions"), "attach" => "style='width:60px;border-right:0px;text-align:right;'")));
321                         foreach($this->departments as $key => $val) {
322                                 if(!isset($this->config->departments[trim($key)])){
323                                         $this->config->departments[trim($key)]="";
324                                 }
325                                 
326                                 $non_empty="";  
327                                 $keys= str_replace("/","\/",$key);
328                                 foreach($this->config->departments as $keyd=>$vald ){
329                                         if(preg_match("/".$keys."\/.*/",$keyd)){
330                                                 $non_empty="full";
331                                         }       
332                                 }
333         
334                                 $title = $this->config->departments[$key];
335                                 $field0 = array("string" => "<img src='images/".$non_empty."folder.png' title='"._("department")."' alt='department'>", "attach" => "style='text-align:center;width:20px;'");
336                                 $field1 = array("string" => sprintf($linkopen,base64_encode($key),$val), "attach" => "style='' title='".$title."'");
337                                 $field2 = array("string" => preg_replace("/%KEY%/", base64_encode($key), $actions), "attach" => "style='width:60px;border-right:0px;text-align:right;'");
339                                 $divlist->AddEntry( array($field0,$field1,$field2));
341                         }
342                         $smarty->assign("departments",$divlist->DrawList() );
343                         $smarty->assign("departmentshead",$listhead);
344                         $smarty->assign("search_image", get_template_path('images/search.png'));
345                         $smarty->assign("tree_image", get_template_path('images/tree.png'));
346                         $smarty->assign("infoimage", get_template_path('images/info.png'));
347                         $smarty->assign("launchimage", get_template_path('images/launch.png'));
348                         $smarty->assign("deplist", $this->config->idepartments);
350                         foreach( array("depselect", "regex") as $type){
351                                 $smarty->assign("$type", $depfilter[$type]);
352                         }
354                         /* Extend if we are not using javascript */
355                         $smarty->assign("apply", apply_filter());
356                         $smarty->assign("alphabet", generate_alphabet());
357                         $smarty->assign("hint", print_sizelimit_warning());
359                         $display= $smarty->fetch(get_template_path('headpage.tpl', TRUE));
360                         return($display);
361                 }
363                 /* Show main page (tabs) */
364                 $display= $this->deptabs->execute();
366                 $display.= "<p style=\"text-align:right\">\n";
367                 $display.= "<input type=submit name=\"edit_finish\" value=\""._("Finish")."\">\n";
368                 $display.= "&nbsp;\n";
369                 $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
370                 $display.= "</p>";
372                 return ($display);
373         }
376         function reload()
377         {
378                 /* Get config */
379                 $depfilter= get_global('depfilter');
380                 // Added for dirlist function...
382                 if(isset($_POST['deplist'])){
383                         $depfilter= get_global("depfilter");
384                         $depfilter['depselect']= $this->config->departments[trim($_POST['deplist'])];
385                         register_global("depfilter", $depfilter);
386                 }
388                 /* Set base for all searches */
389                 $base= $depfilter['depselect'];
391                 /* Regex filter? */
392                 if ($depfilter['regex'] != ""){
393                         $regex= $depfilter['regex'];
394                 } else {
395                         $regex= "*";
396                 }
398                 // Configure Back Address
399                 $base_back = preg_replace("/^[^,]+,/","",$base);
401                 // Create Array to Test if we have a valid back button
402                 $tmp = array_flip($_SESSION['config']->departments);
404                 // In case of a valid back button create entry
405                 if(isset($tmp[$base_back])){
406                         $tmp2    ['dn']          = convert_department_dn($base_back);
408                         // If empty always go to top
409                         if(empty($tmp2['dn'])){
410                                 $tmp2['dn']="/";
411                         }
412                         $tmp2    ['description'][0] = _("..");
413                         $result[$tmp[$base_back]]=$tmp2;
414                 }
416                 $res= get_list2($this->ui->subtreeACL, "(&(|(ou=$regex)(description=$regex))(objectClass=gosaDepartment))", 
417                                 TRUE, $base, array("ou", "description"), TRUE);
419                 //hickert Replaced Block
420                 //    $this->departments= array();
421                 //    foreach ($res as $value){
422                 //           $title= convert_department_dn($value["dn"]);
423                 //           $this->departments[$title]= "$title [".$value["description"][0]."]";
424                 //           $this->departments[$value['dn']]= "[".$value["description"][0]."]";
425                 //    }
427                 $this->departments= array();
428                 foreach ($res as $value){
429                         if(isset($value["description"][0])){
430                                 $this->departments[$value['dn']]= convert_department_dn2($value['dn'])." - [".$value["description"][0]."]";
431                         }else{
432                                 $this->departments[$value['dn']]= convert_department_dn2($value['dn']);//$value["description"][0];
433                         }
434                 }
435                 natcasesort ($this->departments);
436                 reset ($this->departments);
438                 /* Unset global department list */
439                 unset($_SESSION['cached_departments']);
440         }
442         function remove_from_parent()
443         {
444                 $ldap= $this->config->get_ldap_link();
445                 $ldap->cd ($this->dn);
446                 $ldap->recursive_remove();
448                 /* Optionally execute a command after we're done */
449                 $this->postremove();
451                 /* Delete references to object groups */
452                 $ldap->cd ($this->config->current['BASE']);
453                 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
454                 while ($ldap->fetch()){
455                         $og= new ogroup($this->config, $ldap->getDN());
456                         unset($og->member[$this->dn]);
457                         $og->save ();
458                 }
460         }
462         function remove_lock()
463         {
464                 if (isset($this->dn)){
465                         del_lock ($this->dn);
466                 }
467         }
469         }
471         ?>