Code

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