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,$this->config->data['TABS']['DEPTABS'], $this->dn,"department");
111 $this->deptabs->set_acl_base($this->DivListDepartment->selectedBase);
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 /* Register deptabs to trigger edit dialog */
133 $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn,"department");
134 $this->deptabs->set_acl_base($this->dn);
136 $_SESSION['objectinfo']= $this->dn;
137 }
140 /***************
141 Delete entry
142 ***************/
144 /* Delete Entry if Posted action (s_action) == del
145 * The entry which will be deleted is defined in $s_entry
146 */
147 if ($s_action =="del"){
148 $this->dn= $this->config->departments[trim($s_entry)];
150 /* check acls */
151 $acl = $this->ui->get_permissions($this->dn,"department/department");
152 if(preg_match("/d/",$acl)){
154 /* Check locking */
155 if (($user= get_lock($this->dn)) != ""){
156 $_SESSION['dn']= $this->dn;
157 return(gen_locked_message($user, $this->dn));
158 } else {
159 add_lock ($this->dn, $this->ui->dn);
160 $smarty->assign("info", sprintf(_("You're about to delete the whole LDAP subtree placed under '%s'."), @LDAP::fix($this->dn)));
161 $display.= $smarty->fetch (get_template_path('remove.tpl', TRUE));
162 return ($display);
163 }
164 }else{
165 print_red (_("You have no permission to remove this department."));
166 }
167 }
170 /***************
171 Delete department confirmed
172 ***************/
174 /* If department deletion is accepted ...
175 * Finally delete department
176 */
177 if (isset($_POST['delete_department_confirm'])){
179 /* check acls */
180 $acl = $this->ui->get_permissions($this->dn,"department/department");
181 if(preg_match("/d/",$acl)){
182 $this->remove_from_parent();
183 gosa_log ("Department object'".$this->dn."' has been removed");
184 } else {
185 print_red (_("You have no permission to remove this department."));
186 }
187 }
190 /***************
191 Edit department finished
192 ***************/
194 if ((isset($_POST['edit_finish'])) && (isset($this->deptabs->config)) && !isset($_POST['dep_move_confirm'])){
195 $this->deptabs->save_object();
196 $obj = $this->deptabs->by_object['department'];
197 if($obj->orig_dn != "new"){
198 if($obj->orig_ou != $obj->ou || $obj->base != $obj->orig_base){
199 return($smarty->fetch(get_template_path("dep_move_confirm.tpl",TRUE)));
200 }
201 }
202 }
204 /* Save changes */
205 if ((isset($_POST['edit_finish'])|| isset($_POST['dep_move_confirm'])) && (isset($this->deptabs->config))){
207 /* Check tabs, will feed message array */
208 $message= $this->deptabs->check();
210 /* Save, or display error message? */
211 if (count($message) == 0){
213 $this->deptabs->save(true);
214 $this->config->get_departments();
216 /* This object must be tagged, so set ObjectTaggingRequested to true */
217 if($this->deptabs->by_object['department']->must_be_tagged()){
218 $this->ObjectTaggingRequested = true;
219 }
221 /* Get recursive move is required, set RecursiveRemoveRequested to true */
222 if($this->deptabs->by_object['department']->am_i_moved()){
223 $this->RecursiveRemoveRequested = true;
224 }
226 /* This var indicated that there is an object which isn't saved right now. */
227 $this->ObjectInSaveMode = true;
229 } else {
230 /* Ok. There seem to be errors regarding to the tab data,
231 show message and continue as usual. */
232 show_errors($message);
233 }
234 }
237 /***************
238 Handle Tagging (Return output for an iframe)
239 ***************/
241 /* This department must be tagged (Is called from iframe, generates output)*/
242 if(isset($_GET['TagDepartment'])){
243 $this->deptabs->by_object['department']->tag_objects();
244 exit();
245 }
248 /***************
249 Handle recursive move (Return output for an iframe)
250 ***************/
252 /* initiate recursive remove (Is called from iframe, generates output)*/
253 if(isset($_GET['PerformRecMove'])){
254 $this->deptabs->by_object['department']->recursive_move("","",true);
255 $this->DivListDepartment->selectedBase = $this->deptabs->by_object['department']->dn;
256 exit();
257 }
260 /***************
261 Return iframes, which call tagging / recusrsive move
262 ***************/
264 /* While one of these vars below isset, we must return an iframe,
265 to perform requested operation */
266 if($this->ObjectTaggingRequested){
267 $this->ObjectTaggingRequested = false;
268 return($this->deptabs->by_object['department']->ShowTagFrame());
269 }
270 if($this->RecursiveRemoveRequested){
271 $this->RecursiveRemoveRequested = false;
272 return($this->deptabs->by_object['department']->ShowMoveFrame());
273 }
276 /***************
277 In case of tagging/moving the object wasn't deleted, do it know
278 ***************/
280 /* If there is an unsaved object and all operations are done
281 remove locks & save object tab & unset current object */
282 if($this->ObjectInSaveMode && (!$this->RecursiveRemoveRequested) && (!$this->ObjectTaggingRequested)){
283 $this->deptabs->save();
284 $this->config->get_departments();
285 $this->ObjectInSaveMode = false;
286 if ($this->dn != "new"){
287 del_lock ($this->dn);
288 }
289 gosa_log ("Department object'".$this->dn."' has been saved");
290 unset ($this->deptabs);
291 $this->deptabs= NULL;
292 unset ($_SESSION['objectinfo']);
293 }
296 /***************
297 Dialog canceled
298 ***************/
300 /* User canceled edit oder delete
301 * Cancel dialog
302 */
303 if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) || isset($_POST['delete_department_confirm'])){
304 del_lock ($this->dn);
305 unset($this->depdabs);
306 $this->deptabs= NULL;
307 unset ($_SESSION['objectinfo']);
308 }
310 /* Headpage or normal plugin screen? */
311 if ($this->deptabs != NULL){
313 /* Show main page (tabs) */
314 $display= $this->deptabs->execute();
315 if (!$this->deptabs->by_object[$this->deptabs->current]->dialog){
316 $display.= "<p style=\"text-align:right\">\n";
317 $display.= "<input type=submit name=\"edit_finish\" value=\""._("Save")."\">\n";
318 $display.= " \n";
319 $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
320 $display.= "</p>";
321 }
322 return ($display);
323 }else{
324 /* Display dialog with group list */
325 $this->DivListDepartment->parent = $this;
326 $this->DivListDepartment->execute();
327 $this->reload();
328 $this->DivListDepartment->DepartmentsAdded = true;
329 $this->DivListDepartment->setEntries($this->departments);
330 return($this->DivListDepartment->Draw());
331 }
332 }
335 function reload()
336 {
337 /* Vairaible init */
338 $base = $this->DivListDepartment->selectedBase;
339 $base_back = preg_replace("/^[^,]+,/","",$base);
340 $Regex = $this->DivListDepartment->Regex;
342 // Create Array to Test if we have a valid back button
343 $tmp = $_SESSION['config']->idepartments;
345 // In case of a valid back button create entry
346 if(isset($tmp[$base_back])){
347 $tmp2 ['dn'] = convert_department_dn($base_back);
349 // If empty always go to top
350 if(empty($tmp2['dn'])){
351 $tmp2['dn']="/";
352 }
353 $tmp2 ['description'][0] = _("..");
354 $result[$tmp[$base_back]]=$tmp2;
355 }
357 if($this->DivListDepartment->SubSearch){
358 $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
359 "department", $base, array("ou", "description"), GL_SIZELIMIT | GL_SUBSEARCH);
360 }else{
361 $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
362 "department", $base, array("ou", "description"), GL_SIZELIMIT );
363 }
365 $this->departments= array();
367 /* Add current base to the list of available departments, but only if its naming attribute is 'ou' */
368 if(preg_match("/^ou=/",$base)){
369 $this->departments [ convert_department_dn($base) ] = ".";
370 }
372 foreach ($res as $key => $value){
374 /* Don't display base as entry on subsearch */
375 if(($value['dn'] == $base) && ($this->DivListDepartment->SubSearch)){
376 continue;
377 }
379 $cdn= convert_department_dn($value['dn']);
381 /* Append to dep list */
382 if(isset($value["description"][0])){
383 $this->departments[$cdn]= get_sub_department($cdn)." - [".$value["description"][0]."]";
384 }else{
385 $this->departments[$cdn]= get_sub_department($cdn);//$value["description"][0];
386 }
387 }
388 natcasesort ($this->departments);
389 reset ($this->departments);
390 }
392 function remove_from_parent()
393 {
394 $ldap= $this->config->get_ldap_link();
395 $ldap->cd ($this->dn);
396 $ldap->recursive_remove();
398 /* Optionally execute a command after we're done */
399 $this->postremove();
401 /* Delete references to object groups */
402 $ldap->cd ($this->config->current['BASE']);
403 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
404 while ($ldap->fetch()){
405 $og= new ogroup($this->config, $ldap->getDN());
406 unset($og->member[$this->dn]);
407 $og->save ();
408 }
410 }
412 function remove_lock()
413 {
414 if (isset($this->dn)){
415 del_lock ($this->dn);
416 }
417 }
419 function save_object()
420 {
421 /* reload department */
422 $this->config->get_departments();
424 $this->config->make_idepartments();
425 $this->DivListDepartment->config= $this->config;
426 $this->DivListDepartment->save_object();
427 }
429 }
430 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
431 ?>