Code

Edit / Delete available for departments with spaces
[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                 /* Reload departments */
60                 $this->config->departments      = get_departments();
61                 $smarty                                         = get_smarty();
62                 $display                                        = "";
63                 $depfilter                                      = get_global("depfilter");
64                 $s_action                                       = "";  // Will contain an action, like del or edit
65                 $s_entry                                        = "";  // The entry name for edit delete -...
67                 $this->config->make_idepartments();
69                 if (isset($_POST['regex'])){
70                         $depfilter['regex']= $_POST['regex'];
71                 }
73                 // Check Post action
74                 foreach($_POST as $key => $val){
75                         // Post for delete
76                         if(preg_match("/dep_del.*/",$key)){
77                                 $s_action = "del";
78                                 $s_entry  = preg_replace("/dep_".$s_action."_/i","",$key);
79                                 $s_entry  = preg_replace("/_.*/","",$s_entry);
80                                 $s_entry  = base64_decode($s_entry);
81                         // Post for edit
82                         }elseif(preg_match("/dep_edit_.*/",$key)){
83                                 $s_action="edit";
84                                 $s_entry  = preg_replace("/dep_".$s_action."_/i","",$key);
85                                 $s_entry  = preg_replace("/_.*/","",$s_entry);
86                                 $s_entry  = base64_decode($s_entry);
87                                 // Post for new
88                         }elseif(preg_match("/dep_new.*/",$key)){
89                                 $s_action="new";
90                         }elseif(preg_match("/dep_back.*/i",$key)){
91                                 $s_action="back";
92                         }elseif(preg_match("/dep_home.*/i",$key)){
93                                 $s_action="home";
94                         }elseif(preg_match("/dep_root.*/i",$key)){
95                                 $s_action="root";
96                         }
97                 
98                 }
100                 if(isset($_POST['depselect']) && $_POST['depselect']){
101                         $depfilter['depselect']= $_POST['depselect'];
102             $this->reload();
103                 }
104                 
105                 if($s_action=="root"){
106                         $depfilter['depselect']=($this->config->current['BASE']);
107                         $this->reload();
108                 }
110                 if($s_action=="home"){
111                         $depfilter['depselect']=(preg_replace("/^[^,]+,/","",$this->ui->dn));
112                         $depfilter['depselect']=(preg_replace("/^[^,]+,/","",$depfilter['depselect']));
113                         $this->reload();
114                 }
116                 if($s_action=="back"){
117                         $base_back                      = preg_replace("/^[^,]+,/","",$depfilter['depselect']);
118                         $base_back          = convert_department_dn($base_back);
119             
120                         if(isset($this->config->departments[trim($base_back)])){
121                                 $depfilter['depselect']= $this->config->departments[trim($base_back)];
122                         }else{
123                                 $depfilter['depselect']= $this->config->departments["/"];
124                         }
125             $this->reload();
126                 }
128                 if(isset($_GET['act'])&&($_GET['act']=="dep_open")){
129                         $s_action="open";
130                         $s_entry = base64_decode($_GET['dep_id']);
131                         $depfilter['depselect']= $this->config->departments[trim($s_entry)];
132                         $this->reload();
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->acl= array(":all");
148                         $this->dn= "new";
150                         $this->deptabs= new deptabs($this->config,
151                                         $this->config->data['TABS']['DEPTABS'], $this->dn);
152                         $this->deptabs->set_acl($this->acl);
153                 }
155                 /* Edit Entry if Posted action (s_action) == edit 
156                  * The entry which will be edited is defined in $s_entry
157                  */
158                 if ($s_action=="edit"){
160                         $this->dn= $this->config->departments[trim($s_entry)];
162                         if (($user= get_lock($this->dn)) != ""){
163                                 return(gen_locked_message ($user, $this->dn));
164                         }
166                         /* Lock the current entry, so everyone will get the  above dialog */
167                         add_lock ($this->dn, $this->ui->dn);
169                         /* Set up the users ACL's for this 'dn' */
170                         $this->acl= get_permissions ($this->dn, $this->ui->subtreeACL);
172                         /* Register deptabs to trigger edit dialog */
173                         $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn);
174                         $this->deptabs->set_acl($this->acl);
175                         $_SESSION['objectinfo']= $this->dn;
176                 }
178                 /* Delete Entry if Posted action (s_action) == del 
179          * The entry which will be deleted is defined in $s_entry
180                  */
181                 if ($s_action =="del"){
182                         $this->dn= $this->config->departments[trim($s_entry)];
184                         /* Check locking */
185                         if (($user= get_lock($this->dn)) != ""){
186                                 $_SESSION['dn']= $this->dn;
187                                 return(gen_locked_message($user, $this->dn));
188                         } else {
189                                 add_lock ($this->dn, $this->ui->dn);
190                                 $smarty->assign("info", sprintf(_("You're about to delete the whole LDAP subtree placed under '%s'."), $this->dn));
191                                 $display.= $smarty->fetch (get_template_path('remove.tpl', TRUE));
192                                 return ($display);
193                         }
194                 }
196                 /* If department deletion is accepted ...
197                  * Finally delete department 
198                  */
199                 if (isset($_POST['delete_department_confirm'])){
200                         $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
201                         $acl= get_module_permission($acl, "department", $this->dn);
202                         if (chkacl($acl, "all") == ""){
203                                 $this->remove_from_parent();
204                                 gosa_log ("Department object'".$this->dn."' has been removed");
205                                 $this->reload ();
206                         } else {
207                                 print_red (_("You have no permission to remove this department."));
208                         }
209                 }
211                 /* Edit Complete ...
212                  * Finish request 
213          */
214                 if (isset($_POST['edit_finish'])){
216                         /* Check tabs, will feed message array */
217                         $message= $this->deptabs->check();
219                         /* Save, or display error message? */
220                         if (count($message) == 0){
222                                 /* Save user data to ldap */
223                                 $this->deptabs->save();
224                                 gosa_log ("Department object'".$this->dn."' has been saved");
226                                 /* Group has been saved successfully, remove lock from
227                                    LDAP. */
228                                 if ($this->dn != "new"){
229                                         del_lock ($this->dn);
230                                 }
232                                 /* There's no page reload so we have to read new users at
233                                    this point. */
234                                 $this->reload ();
235                                 unset ($this->deptabs);
236                                 $this->deptabs= NULL;
237                                 unset ($_SESSION['objectinfo']);
239                         } else {
240                                 /* Ok. There seem to be errors regarding to the tab data,
241                                    show message and continue as usual. */
242                                 show_errors($message);
243                         }
244                 }
247                 /* User cancelt edit oder delete
248          * Cancel dialog 
249          */
250                 if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) ||
251                                 isset($_POST['delete_department_confirm']) || isset($_POST['delete_lock'])){
253                         del_lock ($this->dn);
254                         unset($this->depdabs);
255                         $this->deptabs= NULL;
256                         unset ($_SESSION['objectinfo']);
257                 }
260                 /* Headpage or normal plugin screen? */
261                 if ($this->deptabs == NULL){
262                         /* Check sorting variable */
263                         $this->reload();
265                         /* Check for exeeded sizelimit */
266                         if (($message= check_sizelimit()) != ""){
267                                 return($message);
268                         }
270                         /* Prepare departments */
271                         $options= "";
272                         foreach ($this->config->idepartments as $key => $value){
273                                 if ($depfilter['depselect'] == $key){
274                                         $options.= "<option selected value='$key'>$value</option>";
275                                 } else {
276                                         $options.= "<option value='$key'>$value</option>";
277                                 }
278                         }
280                         /* Generate list head */
281                         $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
282                                                 " <input type='image' src='images/list_up.png' align='middle' title='"._("Go up one department")."' name='dep_back' alt='"._("Up")."'>&nbsp;".
283                                                 " <input type='image' src='images/list_root.png' align='middle' title='"._("Go to root department")."' name='dep_root' alt='"._("Root")."'>&nbsp;".
284                                                 " <input type='image' src='images/list_home.png' align='middle' title='"._("Go to users home department")."' name='dep_home' alt='"._("Home")."'>&nbsp;".
285                                                 " <img src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;".
286                                                 " <input type='image' src='images/list_new_department.png' align='middle' alt='"._("Create new department")."' name='dep_new'>&nbsp;".
287                                                 " <img src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;".
288                                                 _("Current base")."&nbsp;<select name='depselect' onChange='mainform.submit()'>$options</select>".
289                                                 " <input type='image' src='images/list_submit.png' align='middle' title='"._("Submit department")."' name='submit_department' alt='"._("Submit")."'>&nbsp;".
290                                                 "</div>";
292                         /* Show main page */
293                         $divlist = new divlist();
294                         $divlist->SetSummary(_("This table displays all departments, in the selected tree."));
295                         $divlist->SetEntriesPerPage(20);
296                         $actions= "<input type='image' src='images/edit.png'             alt='"._("edit")."'    name='dep_edit_%KEY%' title='"._("Edit this entry")."'>";
297                         $actions.= "<input type='image' src='images/edittrash.png' alt='"._("delete")."'        name='dep_del_%KEY%' title='"._("Delete this entry")."'>";
299                         $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=dep_open&amp;dep_id=%s'>%s</a>";
301                         $divlist->SetHeader(array(array("string" => "&nbsp;"),
302                                                                           array("string"=>_("Department name")),
303                                       array("string" =>_("Actions"), "attach" => "style='text-align:  right;border:none'")));
305                         foreach($this->departments as $key => $val) {
306                                 if(!isset($this->config->departments[trim($key)])){
307                                         $this->config->departments[trim($key)]="";
308                                 }
309                                 $field0 = array("string" => "<img src='images/folder.png' alt='department'>","attach"=>"style='width:20px;'");
310                                 $field1 = array("string" => sprintf($linkopen,base64_encode($key),$val), "attach" => "width='100%'");
311                                 $field2 = array("string" => preg_replace("/%KEY%/", base64_encode($key), $actions),"attach"=>"style='text-align:right;border:none'");
313                                 $divlist->AddEntry( array($field0,$field1,$field2));
315                         }
316                         $smarty->assign("departments",$divlist->DrawList() );
317                         $smarty->assign("departmentshead",$listhead);
318                         $smarty->assign("search_image", get_template_path('images/search.png'));
319                         $smarty->assign("tree_image", get_template_path('images/tree.png'));
320                         $smarty->assign("infoimage", get_template_path('images/info.png'));
321                         $smarty->assign("launchimage", get_template_path('images/launch.png'));
322                         $smarty->assign("deplist", $this->config->idepartments);
324                         foreach( array("depselect", "regex") as $type){
325                                 $smarty->assign("$type", $depfilter[$type]);
326                         }
328                         /* Extend if we are not using javascript */
329                         $smarty->assign("apply", apply_filter());
330                         $smarty->assign("alphabet", generate_alphabet());
331                         $smarty->assign("hint", print_sizelimit_warning());
333                         $display= $smarty->fetch(get_template_path('headpage.tpl', TRUE));
334                         return($display);
335                 }
337                 /* Show main page (tabs) */
338                 $display= $this->deptabs->execute();
340                 $display.= "<p style=\"text-align:right\">\n";
341                 $display.= "<input type=submit name=\"edit_finish\" value=\""._("Finish")."\">\n";
342                 $display.= "&nbsp;\n";
343                 $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
344                 $display.= "</p>";
346                 return ($display);
347         }
350         function reload()
351         {
352                 /* Get config */
353                 $depfilter= get_global('depfilter');
354                 // Added for dirlist function...
356                 if(isset($_POST['deplist'])){
357                         $depfilter= get_global("depfilter");
358                         $depfilter['depselect']= $this->config->departments[trim($_POST['deplist'])];
359                         register_global("depfilter", $depfilter);
360                 }
362                 /* Set base for all searches */
363                 $base= $depfilter['depselect'];
365                 /* Regex filter? */
366                 if ($depfilter['regex'] != ""){
367                         $regex= $depfilter['regex'];
368                 } else {
369                         $regex= "*";
370                 }
372                 // Configure Back Address
373                 $base_back = preg_replace("/^[^,]+,/","",$base);
375                 // Create Array to Test if we have a valid back button
376                 $tmp = array_flip($_SESSION['config']->departments);
378                 // In case of a valid back button create entry
379                 if(isset($tmp[$base_back])){
380                         $tmp2    ['dn']          = convert_department_dn($base_back);
382                         // If empty always go to top
383                         if(empty($tmp2['dn'])){
384                                 $tmp2['dn']="/";
385                         }
386                         $tmp2    ['description'][0] = _("..");
387                         $result[$tmp[$base_back]]=$tmp2;
388                 }
390                 $res= get_list2($this->ui->subtreeACL, "(&(|(ou=$regex)(description=$regex))(objectClass=gosaDepartment))", 
391                                 TRUE, $base, array("ou", "description"), TRUE);
393                 //hickert Replaced Block
394                 //    $this->departments= array();
395                 //    foreach ($res as $value){
396                 //           $title= convert_department_dn($value["dn"]);
397                 //           $this->departments[$title]= "$title [".$value["description"][0]."]";
398                 //           $this->departments[$value['dn']]= "[".$value["description"][0]."]";
399                 //    }
401                 $this->departments= array();
402                 foreach ($res as $value){
403                         if($value["description"][0]!=".."){
404                                 $this->departments[$value['dn']]= convert_department_dn2($value['dn'])." - [".$value["description"][0]."]";
405                         }else{
406                                 $this->departments[$value['dn']]=$value["description"][0];
407                         }
408                 }
409                 natcasesort ($this->departments);
410                 reset ($this->departments);
411         }
413         function remove_from_parent()
414         {
415                 $ldap= $this->config->get_ldap_link();
416                 $ldap->cd ($this->dn);
417                 $ldap->recursive_remove();
419                 /* Optionally execute a command after we're done */
420                 $this->postremove();
422                 /* Delete references to object groups */
423                 $ldap->cd ($this->config->current['BASE']);
424                 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
425                 while ($ldap->fetch()){
426                         $og= new ogroup($this->config, $ldap->getDN());
427                         unset($og->member[$this->dn]);
428                         $og->save ();
429                 }
431         }
433         function remove_lock()
434         {
435                 if (isset($this->dn)){
436                         del_lock ($this->dn);
437                 }
438         }
440         }
442         ?>