1 <?php
2 /*
3 * This code is part of GOsa (http://www.gosa-project.org)
4 * Copyright (C) 2003-2008 GONICUS GmbH
5 *
6 * ID: $$Id$$
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
23 class departmentManagement extends plugin
24 {
25 /* Definitions */
26 var $plHeadline= "Departments";
27 var $plDescription= "Manage Departments";
29 /* Headpage attributes */
30 var $last_dep_sorting= "invalid";
31 var $departments= array();
32 var $deptabs= NULL;
34 /* attribute list for save action */
35 var $attributes= array();
36 var $objectclasses= array();
38 /* Vars to handle operations after saving the department
39 Recursive move && tagging */
40 var $ObjectInSaveMode = false; // Is true, if current object wasn't saved right now
41 var $dns = array();
43 var $acl_module = array("department");
45 function departmentManagement (&$config, &$ui)
46 {
47 $this->ui= &$ui;
48 $this->dn= "";
49 $this->config= &$config;
50 $this->DivListDepartment = new divListDepartment($this->config,$this);
51 }
53 function execute()
54 {
55 global $config;
57 /* Call parent execute */
58 plugin::execute();
60 /***************
61 Var init
62 ***************/
64 session::set('LOCK_VARS_TO_USE',array("/^act$/","/^id$/","/^dep_edit_.*/","/^dep_del_.*/","/^item_selected/","/^remove_multiple_departments/","/^menu_action/"));
66 /* Reload departments */
67 $smarty = get_smarty();
68 $display = "";
69 $s_action = ""; // Will contain an action, like del or edit
70 $s_entry = ""; // The entry name for edit delete -...
73 /***************
74 Check posts
75 ***************/
77 // Check Post action
78 foreach($_POST as $key => $val){
79 // Post for delete
80 if(preg_match("/dep_del.*/",$key)){
81 $s_action = "del";
82 $s_entry = preg_replace("/dep_".$s_action."_/i","",$key);
83 $s_entry = preg_replace("/_.*$/","",$s_entry);
84 $s_entry = base64_decode($s_entry);
85 // Post for edit
86 }elseif(preg_match("/dep_edit_.*/",$key)){
87 $s_action="edit";
88 $s_entry = preg_replace("/dep_".$s_action."_/i","",$key);
89 $s_entry = preg_replace("/_.*$/","",$s_entry);
90 $s_entry = base64_decode($s_entry);
91 // Post for new
92 }elseif(preg_match("/^remove_multiple_departments/",$key)){
93 $s_action="del_multiple";
94 }
95 }
97 /* Create options */
98 if(isset($_POST['menu_action']) && preg_match("/^dep_new_/",$_POST['menu_action'])){
99 $s_action = "new";
100 $s_entry = preg_replace("/^dep_new_([a-z]*)/","\\1",$_POST['menu_action']);
101 }
103 /* handle remove from layers menu */
104 if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
105 $s_action = "del_multiple";
106 }
109 /***************
110 Create a new department
111 ***************/
113 /* New Entry if Posted action (s_action) == new
114 */
115 if ($s_action=="new"){
116 $this->dn= "new";
117 if($s_entry == "c"){
118 $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['COUNTRY_TABS'], $this->dn,"country");
119 }else{
120 $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn,"department");
121 }
122 $this->deptabs->set_acl_base($this->DivListDepartment->selectedBase);
123 }
126 /***************
127 Edit entry
128 ***************/
130 /* Edit Entry if Posted action (s_action) == edit
131 * The entry which will be edited is defined in $s_entry
132 */
133 if (( $s_action=="edit") && (!isset($this->deptabs->config))){
134 $this->dn= $this->config->departments[trim($s_entry)];
136 if (($user= get_lock($this->dn)) != ""){
137 return(gen_locked_message ($user, $this->dn));
138 }
140 /* Lock the current entry, so everyone will get the above dialog */
141 add_lock ($this->dn, $this->ui->dn);
143 /* Register deptabs to trigger edit dialog */
144 $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn,"department");
145 $this->deptabs->set_acl_base($this->dn);
147 session::set('objectinfo',$this->dn);
148 }
151 /********************
152 Delete MULTIPLE entries requested, display confirm dialog
153 ********************/
155 if ($s_action=="del_multiple"){
156 $ids = $this->list_get_selected_items();
159 if(count($ids)){
160 $this->dns = array();
161 foreach($ids as $id){
162 $id = base64_decode($id);
163 $this->dns[$id] = $dn = $this->config->departments[$id];
164 }
166 /* Check locks */
167 if ($user= get_multiple_locks($this->dns)){
168 return(gen_locked_message($user,$this->dns));
169 }
171 $dns_names = array();
172 foreach($this->dns as $dn){
173 $dns_names[] = @LDAP::fix($dn);
174 }
175 add_lock ($this->dns, $this->ui->dn);
177 /* Lock the current entry, so nobody will edit it during deletion */
178 $smarty->assign("info", msgPool::deleteInfo($dns_names));
179 $smarty->assign("multiple", true);
180 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
181 }
182 }
185 /********************
186 Delete MULTIPLE entries confirmed
187 ********************/
189 /* Confirmation for deletion has been passed. Users should be deleted. */
190 if (isset($_POST['delete_multiple_department_confirm'])){
192 /* Remove user by user and check acls before removeing them */
193 foreach($this->dns as $key => $dn){
194 $acl = $this->ui->get_permissions($dn,"department/department");
195 if (preg_match('/d/', $acl)){
197 /* Delete request is permitted, perform LDAP action */
198 $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $dn,"department");
199 $this->deptabs->set_acl_base();
200 $this->deptabs->delete ();
201 $this->deptabs = NULL;
202 } else {
203 msg_dialog::display(_("Permission error"), msgPool::permDelete(), WARNING_DIALOG);
204 }
205 }
207 /* Remove lock file after successfull deletion */
208 $this->remove_lock();
209 $this->dns = array();
210 }
213 /********************
214 Delete MULTIPLE entries Canceled
215 ********************/
217 /* Remove lock */
218 if(isset($_POST['delete_multiple_department_cancel'])){
220 /* Remove lock file after successfull deletion */
221 $this->remove_lock();
222 $this->dns = array();
223 }
226 /***************
227 Delete entry
228 ***************/
230 /* Delete Entry if Posted action (s_action) == del
231 * The entry which will be deleted is defined in $s_entry
232 */
233 if ($s_action =="del"){
234 $this->dn= $this->config->departments[trim($s_entry)];
236 /* check acls */
237 $acl = $this->ui->get_permissions($this->dn,"department/department");
238 if(preg_match("/d/",$acl)){
240 /* Check locking */
241 if (($user= get_lock($this->dn)) != ""){
242 session::set('dn',$this->dn);
243 return(gen_locked_message($user, $this->dn));
244 } else {
245 add_lock ($this->dn, $this->ui->dn);
246 $smarty->assign("info", sprintf(_("You're about to delete the whole LDAP subtree placed under '%s'."), @LDAP::fix($this->dn)));
247 $smarty->assign("multiple", false);
248 $display.= $smarty->fetch (get_template_path('remove.tpl', TRUE));
249 return ($display);
250 }
251 }else{
252 msg_dialog::display(_("Permission error"), msgPool::permDelete(), WARNING_DIALOG);
253 }
254 }
257 /***************
258 Delete department confirmed
259 ***************/
261 /* If department deletion is accepted ...
262 * Finally delete department
263 */
264 if (isset($_POST['delete_department_confirm'])){
266 /* check acls */
267 $acl = $this->ui->get_permissions($this->dn,"department/department");
268 if(preg_match("/d/",$acl)){
269 $this->remove_from_parent();
270 } else {
271 msg_dialog::display(_("Permission error"), msgPool::permDelete(), WARNING_DIALOG);
272 }
273 }
276 /***************
277 Handle tagging/recursive move (Return output for an iframe)
278 ***************/
280 /* initiate recursive remove (Is called from iframe, generates output)*/
281 if(isset($_GET['PerformRecMove'])){
282 $this->deptabs->move_me();
283 $this->DivListDepartment->selectedBase = $this->deptabs->by_object['department']->dn;
284 exit();
285 }
287 /* This department must be tagged (Is called from iframe, generates output)*/
288 if(isset($_GET['TagDepartment'])){
289 $this->deptabs->by_object['department']->tag_objects();
290 exit();
291 }
294 /***************
295 Edit department finished
296 ***************/
298 if (is_object($this->deptabs) && // Ensure we have a valid deptab here
299 (isset($_POST['edit_finish']) || // If 'Save' button is pressed in the edit dialog.
300 isset($_POST['dep_move_confirm']) || // The move(rename) confirmation was given
301 $this->deptabs->move_done())){ // The move(rename) is done, we have to save the rest now.
303 /* Check tabs, will feed message array.
304 This call will also initiate a sav_object() call.
305 So don't move it below the moved check !.
306 */
307 $message= $this->deptabs->check();
309 /*************
310 MOVED ?
311 Check if this department has to be moved
312 *************/
313 if(!isset($_POST['dep_move_confirm']) && $this->deptabs->am_i_moved()){
314 return($smarty->fetch(get_template_path("dep_move_confirm.tpl",TRUE)));
315 }elseif(isset($_POST['dep_move_confirm']) && $this->deptabs->am_i_moved()){
316 $smarty = get_smarty();
317 $smarty->assign("src","?plug=".$_GET['plug']."&PerformRecMove&no_output_compression");
318 $smarty->assign("message",_("As soon as the move operation has finished, you can scroll down to end of the page and press the 'Continue' button to continue with the department management dialog."));
319 return($smarty->fetch(get_template_path("dep_iframe.tpl",TRUE)));
320 }
322 /* Save, or display error message? */
323 if (count($message) == 0){
324 global $config;
326 $this->deptabs->save();
327 $config->get_departments();
328 $config->make_idepartments();
329 $this->config = $config;
331 /* This var indicated that there is an object which isn't saved right now. */
332 $this->ObjectInSaveMode = true;
334 /* This object must be tagged, so set ObjectTaggingRequested to true */
335 if($this->deptabs->by_object[$this->deptabs->base_name]->must_be_tagged()){
336 $smarty = get_smarty();
337 $smarty->assign("src","?plug=".$_GET['plug']."&TagDepartment&no_output_compression");
338 $smarty->assign("message",_("As soon as the tag operation has finished, you can scroll down to end of the page and press the 'Continue' button to continue with the department management dialog."));
339 return($smarty->fetch(get_template_path("dep_iframe.tpl",TRUE)));
340 }
342 } else {
343 /* Ok. There seem to be errors regarding to the tab data,
344 show message and continue as usual. */
345 msg_dialog::displayChecks($message);
346 }
347 }
350 /***************
351 In case of tagging/moving the object wasn't deleted, do it know
352 ***************/
354 /* If there is an unsaved object and all operations are done
355 remove locks & save object tab & unset current object */
356 if($this->ObjectInSaveMode){
357 $this->config->get_departments();
358 $this->ObjectInSaveMode = false;
359 if ($this->dn != "new"){
360 $this->remove_lock();
361 }
362 unset ($this->deptabs);
363 $this->deptabs= NULL;
364 session::un_set('objectinfo');
365 }
368 /***************
369 Dialog canceled
370 ***************/
372 /* User canceled edit oder delete
373 * Cancel dialog
374 */
375 if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) || isset($_POST['delete_department_confirm'])){
376 $this->remove_lock();
377 $this->deptabs= NULL;
378 session::un_set('objectinfo');
379 }
381 /* Headpage or normal plugin screen? */
382 if ($this->deptabs !== NULL){
384 /* Show main page (tabs) */
385 $display= $this->deptabs->execute();
386 if (!$this->deptabs->by_object[$this->deptabs->current]->dialog){
387 $display.= "<p style=\"text-align:right\">\n";
388 $display.= "<input type=submit name=\"edit_finish\" value=\"".msgPool::okButton()."\">\n";
389 $display.= " \n";
390 $display.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
391 $display.= "</p>";
392 }
393 return ($display);
394 }else{
395 /* Display dialog with group list */
396 $this->DivListDepartment->parent = $this;
397 $this->DivListDepartment->execute();
398 $this->reload();
399 $this->DivListDepartment->DepartmentsAdded = true;
400 $this->DivListDepartment->setEntries($this->departments);
401 return($this->DivListDepartment->Draw());
402 }
403 }
406 function reload()
407 {
408 /* Vairaible init */
409 $base = $this->DivListDepartment->selectedBase;
410 $base_back = preg_replace("/^[^,]+,/","",$base);
411 $Regex = $this->DivListDepartment->Regex;
413 // Create Array to Test if we have a valid back button
414 $config = session::get('config');
415 $tmp = $config->idepartments;
417 // In case of a valid back button create entry
418 if(isset($tmp[$base_back])){
419 $tmp2 ['dn'] = convert_department_dn($base_back);
421 // If empty always go to top
422 if(empty($tmp2['dn'])){
423 $tmp2['dn']="/";
424 }
425 $tmp2 ['description'][0] = ".. ".msgPool::backButton();
426 $result[$tmp[$base_back]]=$tmp2;
427 }
431 if($this->DivListDepartment->SubSearch){
432 $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
433 "department", $base, array("ou", "description"), GL_SIZELIMIT | GL_SUBSEARCH);
434 }else{
435 $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
436 "department", $base, array("ou", "description"), GL_SIZELIMIT );
437 }
440 /**/
441 $non_ou = get_list("(&(|(c=$Regex)(description=$Regex))(objectClass=country))",
442 "department", $base, array("c", "description"), GL_SIZELIMIT );
444 $this->departments= array();
445 foreach($non_ou as $obj){
446 $this->departments[] = $obj['dn'];
447 }
450 /* Add current base to the list of available departments, but only if its naming attribute is 'ou' */
451 if(preg_match("/^ou=/",$base)){
452 $this->departments [ convert_department_dn($base) ] = ".";
453 }
455 foreach ($res as $key => $value){
457 /* Don't display base as entry on subsearch */
458 if(($value['dn'] == $base) && ($this->DivListDepartment->SubSearch)){
459 continue;
460 }
462 $cdn= convert_department_dn($value['dn']);
464 /* Append to dep list */
465 if(isset($value["description"][0])){
466 $this->departments[$cdn]= get_sub_department($cdn)." - [".$value["description"][0]."]";
467 }else{
468 $this->departments[$cdn]= get_sub_department($cdn);//$value["description"][0];
469 }
470 }
472 natcasesort ($this->departments);
473 reset ($this->departments);
474 }
476 function remove_from_parent()
477 {
478 $ldap= $this->config->get_ldap_link();
479 $ldap->cd ($this->dn);
480 $ldap->recursive_remove();
482 /* Optionally execute a command after we're done */
483 $this->postremove();
485 /* Delete references to object groups */
486 $ldap->cd ($this->config->current['BASE']);
487 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
488 while ($ldap->fetch()){
489 $og= new ogroup($this->config, $ldap->getDN());
490 unset($og->member[$this->dn]);
491 $og->save ();
492 }
494 }
497 function list_get_selected_items()
498 {
499 $ids = array();
500 foreach($_POST as $name => $value){
501 if(preg_match("/^item_selected_[a-z0-9\\/\=]*$/i",$name)){
502 $id = preg_replace("/^item_selected_/","",$name);
503 $ids[$id] = $id;
504 }
505 }
506 return($ids);
507 }
510 function remove_lock()
511 {
512 if (isset($this->dn)){
513 del_lock ($this->dn);
514 }
515 if(isset($this->dn) && !empty($this->dn) && $this->dn != "new"){
516 del_lock($this->dn);
517 }
518 if(isset($this->dns) && is_array($this->dns) && count($this->dns)){
519 del_lock($this->dns);
520 }
521 }
523 function save_object()
524 {
525 /* reload department */
526 $this->config->get_departments();
528 $this->config->make_idepartments();
529 $this->DivListDepartment->config= $this->config;
530 $this->DivListDepartment->save_object();
531 }
533 }
534 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
535 ?>