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 /* Vars to handle operations after saving the department
43 Recursive move && tagging */
44 var $ObjectInSaveMode = false; // Is true, if current object wasn't saved right now
45 var $ObjectTaggingRequested = false; // Object must be tagged, an iframe will be shown.
46 var $RecursiveRemoveRequested = false; // Is true, if this object must be moved, an iframe will be displayed in this case
48 function departmentManagement ($config, $ui)
49 {
50 $this->ui= $ui;
51 $this->dn= "";
52 $this->config= $config;
53 $this->DivListDepartment = new divListDepartment($this->config,$this);
54 }
56 function execute()
57 {
58 global $config;
60 /* Call parent execute */
61 plugin::execute();
63 /***************
64 Var init
65 ***************/
67 $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^dep_edit_.*/","/^dep_del_.*/");
69 /* Reload departments */
70 $smarty = get_smarty();
71 $display = "";
72 $s_action = ""; // Will contain an action, like del or edit
73 $s_entry = ""; // The entry name for edit delete -...
76 /***************
77 Check posts
78 ***************/
80 // Check Post action
81 foreach($_POST as $key => $val){
82 // Post for delete
83 if(preg_match("/dep_del.*/",$key)){
84 $s_action = "del";
85 $s_entry = preg_replace("/dep_".$s_action."_/i","",$key);
86 $s_entry = preg_replace("/_.*$/","",$s_entry);
87 $s_entry = base64_decode($s_entry);
88 // Post for edit
89 }elseif(preg_match("/dep_edit_.*/",$key)){
90 $s_action="edit";
91 $s_entry = preg_replace("/dep_".$s_action."_/i","",$key);
92 $s_entry = preg_replace("/_.*$/","",$s_entry);
93 $s_entry = base64_decode($s_entry);
94 // Post for new
95 }elseif(preg_match("/dep_new.*/",$key)){
96 $s_action="new";
97 }
99 }
102 /***************
103 Create a new department
104 ***************/
106 /* New Entry if Posted action (s_action) == new
107 */
108 if ($s_action=="new"){
109 $this->dn= "new";
110 $this->deptabs= new deptabs($this->config,
111 $this->config->data['TABS']['DEPTABS'], $this->dn);
112 }
115 /***************
116 Edit entry
117 ***************/
119 /* Edit Entry if Posted action (s_action) == edit
120 * The entry which will be edited is defined in $s_entry
121 */
122 if (( $s_action=="edit") && (!isset($this->deptabs->config))){
123 $this->dn= $this->config->departments[trim($s_entry)];
125 if (($user= get_lock($this->dn)) != ""){
126 return(gen_locked_message ($user, $this->dn));
127 }
129 /* Lock the current entry, so everyone will get the above dialog */
130 add_lock ($this->dn, $this->ui->dn);
132 /* Set up the users ACL's for this 'dn' */
133 $this->acl= get_permissions ($this->dn, $this->ui->subtreeACL);
135 /* Register deptabs to trigger edit dialog */
136 $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn);
137 $this->deptabs->set_acl($this->acl);
138 $_SESSION['objectinfo']= $this->dn;
139 }
142 /***************
143 Delete entry
144 ***************/
146 /* Delete Entry if Posted action (s_action) == del
147 * The entry which will be deleted is defined in $s_entry
148 */
149 if ($s_action =="del"){
150 $this->dn= $this->config->departments[trim($s_entry)];
152 /* Check locking */
153 if (($user= get_lock($this->dn)) != ""){
154 $_SESSION['dn']= $this->dn;
155 return(gen_locked_message($user, $this->dn));
156 } else {
157 add_lock ($this->dn, $this->ui->dn);
158 $smarty->assign("info", sprintf(_("You're about to delete the whole LDAP subtree placed under '%s'."), @LDAP::fix($this->dn)));
159 $display.= $smarty->fetch (get_template_path('remove.tpl', TRUE));
160 return ($display);
161 }
162 }
165 /***************
166 Delete department confirmed
167 ***************/
169 /* If department deletion is accepted ...
170 * Finally delete department
171 */
172 if (isset($_POST['delete_department_confirm'])){
173 $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
174 $acl= get_module_permission($acl, "department", $this->dn);
175 if (chkacl($acl, "delete") == ""){
176 $this->remove_from_parent();
177 gosa_log ("Department object'".$this->dn."' has been removed");
178 } else {
179 print_red (_("You have no permission to remove this department."));
180 }
181 }
184 /***************
185 Edit department finished
186 ***************/
188 if ((isset($_POST['edit_finish'])) && (isset($this->deptabs->config))){
190 /* Check tabs, will feed message array */
191 $message= $this->deptabs->check();
193 /* Save, or display error message? */
194 if (count($message) == 0){
196 $this->deptabs->save(true);
198 /* This object must be tagged, so set ObjectTaggingRequested to true */
199 if($this->deptabs->by_object['department']->must_be_tagged()){
200 $this->ObjectTaggingRequested = true;
201 }
203 /* Get recursive move is required, set RecursiveRemoveRequested to true */
204 if($this->deptabs->by_object['department']->am_i_moved()){
205 $this->RecursiveRemoveRequested = true;
206 }
208 /* This var indicated that there is an object which isn't saved right now. */
209 $this->ObjectInSaveMode = true;
211 $this->config->get_departments($this->dn);
212 $this->config->make_idepartments();
214 $this->DivListDepartment->config=$this->config;
216 } else {
217 /* Ok. There seem to be errors regarding to the tab data,
218 show message and continue as usual. */
219 show_errors($message);
220 }
221 }
224 /***************
225 Handle Tagging (Return output for an iframe)
226 ***************/
228 /* This department must be tagged (Is called from iframe, generates output)*/
229 if(isset($_GET['TagDepartment'])){
230 $this->deptabs->by_object['department']->tag_objects();
231 exit();
232 }
235 /***************
236 Handle recursive move (Return output for an iframe)
237 ***************/
239 /* initiate recursive remove (Is called from iframe, generates output)*/
240 if(isset($_GET['PerformRecMove'])){
241 $this->deptabs->by_object['department']->recursive_move("","",true);
242 exit();
243 }
246 /***************
247 Return iframes, which call tagging / recusrsive move
248 ***************/
250 /* While one of these vars below isset, we must return an iframe,
251 to perform requested operation */
252 if($this->ObjectTaggingRequested){
253 $this->ObjectTaggingRequested = false;
254 return($this->deptabs->by_object['department']->ShowTagFrame());
255 }
256 if($this->RecursiveRemoveRequested){
257 $this->RecursiveRemoveRequested = false;
258 return($this->deptabs->by_object['department']->ShowMoveFrame());
259 }
262 /***************
263 In case of tagging/moving the object wasn't deleted, do it know
264 ***************/
266 /* If there is an unsaved object and all operations are done
267 remove locks & save object tab & unset current object */
268 if($this->ObjectInSaveMode && (!$this->RecursiveRemoveRequested) && (!$this->ObjectTaggingRequested)){
269 $this->deptabs->save();
270 $this->ObjectInSaveMode = false;
271 if ($this->dn != "new"){
272 del_lock ($this->dn);
273 }
274 gosa_log ("Department object'".$this->dn."' has been saved");
275 unset ($this->deptabs);
276 $this->deptabs= NULL;
277 unset ($_SESSION['objectinfo']);
278 }
281 /***************
282 Dialog canceled
283 ***************/
285 /* User canceled edit oder delete
286 * Cancel dialog
287 */
288 if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) || isset($_POST['delete_department_confirm'])){
289 del_lock ($this->dn);
290 unset($this->depdabs);
291 $this->deptabs= NULL;
292 unset ($_SESSION['objectinfo']);
293 }
295 /* Headpage or normal plugin screen? */
296 if ($this->deptabs != NULL){
298 /* Show main page (tabs) */
299 $display= $this->deptabs->execute();
300 if (!$this->deptabs->by_object[$this->deptabs->current]->dialog){
301 $display.= "<p style=\"text-align:right\">\n";
302 $display.= "<input type=submit name=\"edit_finish\" value=\""._("Save")."\">\n";
303 $display.= " \n";
304 $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
305 $display.= "</p>";
306 }
307 return ($display);
308 }else{
309 /* Display dialog with group list */
310 $this->DivListDepartment->parent = $this;
311 $this->DivListDepartment->execute();
312 $this->reload();
313 $this->DivListDepartment->DepartmentsAdded = true;
314 $this->DivListDepartment->setEntries($this->departments);
315 return($this->DivListDepartment->Draw());
316 }
317 }
320 function reload()
321 {
322 /* Vairaible init */
323 $base = $this->DivListDepartment->selectedBase;
324 $base_back = preg_replace("/^[^,]+,/","",$base);
325 $Regex = $this->DivListDepartment->Regex;
327 // Create Array to Test if we have a valid back button
328 $tmp = $_SESSION['config']->idepartments;
330 // In case of a valid back button create entry
331 if(isset($tmp[$base_back])){
332 $tmp2 ['dn'] = convert_department_dn($base_back);
334 // If empty always go to top
335 if(empty($tmp2['dn'])){
336 $tmp2['dn']="/";
337 }
338 $tmp2 ['description'][0] = _("..");
339 $result[$tmp[$base_back]]=$tmp2;
340 }
342 if($this->DivListDepartment->SubSearch){
343 $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
344 $this->ui->subtreeACL, $base, array("ou", "description"), GL_SIZELIMIT | GL_SUBSEARCH);
345 }else{
346 $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
347 $this->ui->subtreeACL, $base, array("ou", "description"), GL_SIZELIMIT );
348 }
350 $this->departments= array();
351 foreach ($res as $key => $value){
353 /* Don't display base as entry on subsearch */
354 if(($value['dn'] == $base) && ($this->DivListDepartment->SubSearch)){
355 continue;
356 }
358 $cdn= convert_department_dn($value['dn']);
360 /* Append to dep list */
361 if(isset($value["description"][0])){
362 $this->departments[$cdn]= get_sub_department($cdn)." - [".$value["description"][0]."]";
363 }else{
364 $this->departments[$cdn]= get_sub_department($cdn);//$value["description"][0];
365 }
366 }
367 natcasesort ($this->departments);
368 reset ($this->departments);
369 }
371 function remove_from_parent()
372 {
373 $ldap= $this->config->get_ldap_link();
374 $ldap->cd ($this->dn);
375 $ldap->recursive_remove();
377 /* Optionally execute a command after we're done */
378 $this->postremove();
380 /* Delete references to object groups */
381 $ldap->cd ($this->config->current['BASE']);
382 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
383 while ($ldap->fetch()){
384 $og= new ogroup($this->config, $ldap->getDN());
385 unset($og->member[$this->dn]);
386 $og->save ();
387 }
389 }
391 function remove_lock()
392 {
393 if (isset($this->dn)){
394 del_lock ($this->dn);
395 }
396 }
398 function save_object()
399 {
400 /* reload department */
401 $this->config->get_departments();
402 $config = $this->config;
404 $this->config->make_idepartments();
405 $this->DivListDepartment->save_object();
406 }
408 }
409 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
410 ?>