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";
118 $objects['c'] ['ACL'] = "country";
119 $objects['c'] ['TAB'] = "COUNTRY_TABS";
120 $objects['ou']['ACL'] = "department";
121 $objects['ou']['TAB'] = "DEPTABS";
122 $objects['l'] ['ACL'] = "locatity";
123 $objects['l'] ['TAB'] = "LOCALITY_TABS";
124 $objects['o'] ['ACL'] = "organization";
125 $objects['o'] ['TAB'] = "ORGANIZATION_TABS";
127 if(isset($objects[$s_entry])){
128 $tab = $objects[$s_entry]['TAB'];
129 $acl = $objects[$s_entry]['ACL'];
130 $this->deptabs= new deptabs($this->config,$this->config->data['TABS'][$tab], $this->dn,$acl);
131 }else{
132 trigger_error("Invalid / Not implemented countainer type.");
133 }
134 $this->deptabs->set_acl_base($this->DivListDepartment->selectedBase);
135 }
138 /***************
139 Edit entry
140 ***************/
142 /* Edit Entry if Posted action (s_action) == edit
143 * The entry which will be edited is defined in $s_entry
144 */
145 if (( $s_action=="edit") && (!isset($this->deptabs->config))){
146 $this->dn= $this->config->departments[trim($s_entry)];
148 if (($user= get_lock($this->dn)) != ""){
149 return(gen_locked_message ($user, $this->dn));
150 }
152 /* Lock the current entry, so everyone will get the above dialog */
153 add_lock ($this->dn, $this->ui->dn);
155 /* Register deptabs to trigger edit dialog */
156 $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn,"department");
157 $this->deptabs->set_acl_base($this->dn);
159 session::set('objectinfo',$this->dn);
160 }
163 /********************
164 Delete MULTIPLE entries requested, display confirm dialog
165 ********************/
167 if ($s_action=="del_multiple"){
168 $ids = $this->list_get_selected_items();
171 if(count($ids)){
172 $this->dns = array();
173 foreach($ids as $id){
174 $id = base64_decode($id);
175 $this->dns[$id] = $dn = $this->config->departments[$id];
176 }
178 /* Check locks */
179 if ($user= get_multiple_locks($this->dns)){
180 return(gen_locked_message($user,$this->dns));
181 }
183 $dns_names = array();
184 foreach($this->dns as $dn){
185 $dns_names[] = @LDAP::fix($dn);
186 }
187 add_lock ($this->dns, $this->ui->dn);
189 /* Lock the current entry, so nobody will edit it during deletion */
190 $smarty->assign("info", msgPool::deleteInfo($dns_names));
191 $smarty->assign("multiple", true);
192 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
193 }
194 }
197 /********************
198 Delete MULTIPLE entries confirmed
199 ********************/
201 /* Confirmation for deletion has been passed. Users should be deleted. */
202 if (isset($_POST['delete_multiple_department_confirm'])){
204 /* Remove user by user and check acls before removeing them */
205 foreach($this->dns as $key => $dn){
206 $acl = $this->ui->get_permissions($dn,"department/department");
207 if (preg_match('/d/', $acl)){
209 /* Delete request is permitted, perform LDAP action */
210 $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $dn,"department");
211 $this->deptabs->set_acl_base();
212 $this->deptabs->delete ();
213 $this->deptabs = NULL;
214 } else {
215 msg_dialog::display(_("Permission error"), msgPool::permDelete(), WARNING_DIALOG);
216 }
217 }
219 /* Remove lock file after successfull deletion */
220 $this->remove_lock();
221 $this->dns = array();
222 }
225 /********************
226 Delete MULTIPLE entries Canceled
227 ********************/
229 /* Remove lock */
230 if(isset($_POST['delete_multiple_department_cancel'])){
232 /* Remove lock file after successfull deletion */
233 $this->remove_lock();
234 $this->dns = array();
235 }
238 /***************
239 Delete entry
240 ***************/
242 /* Delete Entry if Posted action (s_action) == del
243 * The entry which will be deleted is defined in $s_entry
244 */
245 if ($s_action =="del"){
246 $this->dn= $this->config->departments[trim($s_entry)];
248 /* check acls */
249 $acl = $this->ui->get_permissions($this->dn,"department/department");
250 if(preg_match("/d/",$acl)){
252 /* Check locking */
253 if (($user= get_lock($this->dn)) != ""){
254 session::set('dn',$this->dn);
255 return(gen_locked_message($user, $this->dn));
256 } else {
257 add_lock ($this->dn, $this->ui->dn);
258 $smarty->assign("info", sprintf(_("You're about to delete the whole LDAP subtree placed under '%s'."), @LDAP::fix($this->dn)));
259 $smarty->assign("multiple", false);
260 $display.= $smarty->fetch (get_template_path('remove.tpl', TRUE));
261 return ($display);
262 }
263 }else{
264 msg_dialog::display(_("Permission error"), msgPool::permDelete(), WARNING_DIALOG);
265 }
266 }
269 /***************
270 Delete department confirmed
271 ***************/
273 /* If department deletion is accepted ...
274 * Finally delete department
275 */
276 if (isset($_POST['delete_department_confirm'])){
278 /* check acls */
279 $acl = $this->ui->get_permissions($this->dn,"department/department");
280 if(preg_match("/d/",$acl)){
281 $this->remove_from_parent();
282 } else {
283 msg_dialog::display(_("Permission error"), msgPool::permDelete(), WARNING_DIALOG);
284 }
285 }
288 /***************
289 Handle tagging/recursive move (Return output for an iframe)
290 ***************/
292 /* initiate recursive remove (Is called from iframe, generates output)*/
293 if(isset($_GET['PerformRecMove'])){
294 $this->deptabs->move_me();
295 $this->DivListDepartment->selectedBase = $this->deptabs->by_object['department']->dn;
296 exit();
297 }
299 /* This department must be tagged (Is called from iframe, generates output)*/
300 if(isset($_GET['TagDepartment'])){
301 $this->deptabs->by_object['department']->tag_objects();
302 exit();
303 }
306 /***************
307 Edit department finished
308 ***************/
310 if (is_object($this->deptabs) && // Ensure we have a valid deptab here
311 (isset($_POST['edit_finish']) || // If 'Save' button is pressed in the edit dialog.
312 isset($_POST['dep_move_confirm']) || // The move(rename) confirmation was given
313 $this->deptabs->move_done())){ // The move(rename) is done, we have to save the rest now.
315 /* Check tabs, will feed message array.
316 This call will also initiate a sav_object() call.
317 So don't move it below the moved check !.
318 */
319 $message= $this->deptabs->check();
321 /*************
322 MOVED ?
323 Check if this department has to be moved
324 *************/
325 if(!isset($_POST['dep_move_confirm']) && $this->deptabs->am_i_moved()){
326 return($smarty->fetch(get_template_path("dep_move_confirm.tpl",TRUE)));
327 }elseif(isset($_POST['dep_move_confirm']) && $this->deptabs->am_i_moved()){
328 $smarty = get_smarty();
329 $smarty->assign("src","?plug=".$_GET['plug']."&PerformRecMove&no_output_compression");
330 $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."));
331 return($smarty->fetch(get_template_path("dep_iframe.tpl",TRUE)));
332 }
334 /* Save, or display error message? */
335 if (count($message) == 0){
336 global $config;
338 $this->deptabs->save();
339 $config->get_departments();
340 $config->make_idepartments();
341 $this->config = $config;
343 /* This var indicated that there is an object which isn't saved right now. */
344 $this->ObjectInSaveMode = true;
346 /* This object must be tagged, so set ObjectTaggingRequested to true */
347 if($this->deptabs->by_object[$this->deptabs->base_name]->must_be_tagged()){
348 $smarty = get_smarty();
349 $smarty->assign("src","?plug=".$_GET['plug']."&TagDepartment&no_output_compression");
350 $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."));
351 return($smarty->fetch(get_template_path("dep_iframe.tpl",TRUE)));
352 }
354 } else {
355 /* Ok. There seem to be errors regarding to the tab data,
356 show message and continue as usual. */
357 msg_dialog::displayChecks($message);
358 }
359 }
362 /***************
363 In case of tagging/moving the object wasn't deleted, do it know
364 ***************/
366 /* If there is an unsaved object and all operations are done
367 remove locks & save object tab & unset current object */
368 if($this->ObjectInSaveMode){
369 $this->config->get_departments();
370 $this->ObjectInSaveMode = false;
371 if ($this->dn != "new"){
372 $this->remove_lock();
373 }
374 unset ($this->deptabs);
375 $this->deptabs= NULL;
376 session::un_set('objectinfo');
377 }
380 /***************
381 Dialog canceled
382 ***************/
384 /* User canceled edit oder delete
385 * Cancel dialog
386 */
387 if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) || isset($_POST['delete_department_confirm'])){
388 $this->remove_lock();
389 $this->deptabs= NULL;
390 session::un_set('objectinfo');
391 }
393 /* Headpage or normal plugin screen? */
394 if ($this->deptabs !== NULL){
396 /* Show main page (tabs) */
397 $display= $this->deptabs->execute();
398 if (!$this->deptabs->by_object[$this->deptabs->current]->dialog){
399 $display.= "<p style=\"text-align:right\">\n";
400 $display.= "<input type=submit name=\"edit_finish\" value=\"".msgPool::okButton()."\">\n";
401 $display.= " \n";
402 $display.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
403 $display.= "</p>";
404 }
405 return ($display);
406 }else{
407 /* Display dialog with group list */
408 $this->DivListDepartment->parent = $this;
409 $this->DivListDepartment->execute();
410 $this->reload();
411 $this->DivListDepartment->DepartmentsAdded = true;
412 $this->DivListDepartment->setEntries($this->departments);
413 return($this->DivListDepartment->Draw());
414 }
415 }
418 function reload()
419 {
420 /* Vairaible init */
421 $base = $this->DivListDepartment->selectedBase;
422 $base_back = preg_replace("/^[^,]+,/","",$base);
423 $Regex = $this->DivListDepartment->Regex;
425 // Create Array to Test if we have a valid back button
426 $config = session::get('config');
427 $tmp = $config->idepartments;
429 // In case of a valid back button create entry
430 if(isset($tmp[$base_back])){
431 $tmp2 ['dn'] = convert_department_dn($base_back);
433 // If empty always go to top
434 if(empty($tmp2['dn'])){
435 $tmp2['dn']="/";
436 }
437 $tmp2 ['description'][0] = ".. ".msgPool::backButton();
438 $result[$tmp[$base_back]]=$tmp2;
439 }
443 if($this->DivListDepartment->SubSearch){
444 $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
445 "department", $base, array("ou", "description"), GL_SIZELIMIT | GL_SUBSEARCH);
446 }else{
447 $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
448 "department", $base, array("ou", "description"), GL_SIZELIMIT );
449 }
452 /**/
453 $non_ou = get_list("(&(|(c=$Regex)(description=$Regex))(objectClass=country))",
454 "department", $base, array("c", "description"), GL_SIZELIMIT );
455 $non_ou = array_merge($non_ou,get_list("(&(|(l=$Regex)(description=$Regex))(objectClass=locality))",
456 "department", $base, array("l", "description"), GL_SIZELIMIT ));
457 $non_ou = array_merge($non_ou,get_list("(&(|(o=$Regex)(description=$Regex))(objectClass=organization))",
458 "department", $base, array("o", "description"), GL_SIZELIMIT ));
460 $this->departments= array();
461 foreach($non_ou as $obj){
462 $this->departments[] = $obj['dn'];
463 }
466 /* Add current base to the list of available departments, but only if its naming attribute is 'ou' */
467 if(preg_match("/^ou=/",$base)){
468 $this->departments [ convert_department_dn($base) ] = ".";
469 }
471 foreach ($res as $key => $value){
473 /* Don't display base as entry on subsearch */
474 if(($value['dn'] == $base) && ($this->DivListDepartment->SubSearch)){
475 continue;
476 }
478 $cdn= convert_department_dn($value['dn']);
480 /* Append to dep list */
481 if(isset($value["description"][0])){
482 $this->departments[$cdn]= get_sub_department($cdn)." - [".$value["description"][0]."]";
483 }else{
484 $this->departments[$cdn]= get_sub_department($cdn);//$value["description"][0];
485 }
486 }
488 natcasesort ($this->departments);
489 reset ($this->departments);
490 }
492 function remove_from_parent()
493 {
494 $ldap= $this->config->get_ldap_link();
495 $ldap->cd ($this->dn);
496 $ldap->recursive_remove();
498 /* Optionally execute a command after we're done */
499 $this->postremove();
501 /* Delete references to object groups */
502 $ldap->cd ($this->config->current['BASE']);
503 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
504 while ($ldap->fetch()){
505 $og= new ogroup($this->config, $ldap->getDN());
506 unset($og->member[$this->dn]);
507 $og->save ();
508 }
510 }
513 function list_get_selected_items()
514 {
515 $ids = array();
516 foreach($_POST as $name => $value){
517 if(preg_match("/^item_selected_[a-z0-9\\/\=]*$/i",$name)){
518 $id = preg_replace("/^item_selected_/","",$name);
519 $ids[$id] = $id;
520 }
521 }
522 return($ids);
523 }
526 function remove_lock()
527 {
528 if (isset($this->dn)){
529 del_lock ($this->dn);
530 }
531 if(isset($this->dn) && !empty($this->dn) && $this->dn != "new"){
532 del_lock($this->dn);
533 }
534 if(isset($this->dns) && is_array($this->dns) && count($this->dns)){
535 del_lock($this->dns);
536 }
537 }
539 function save_object()
540 {
541 /* reload department */
542 $this->config->get_departments();
544 $this->config->make_idepartments();
545 $this->DivListDepartment->config= $this->config;
546 $this->DivListDepartment->save_object();
547 }
549 }
550 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
551 ?>