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 // 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 // Post for new
90 }elseif(preg_match("/^remove_multiple_departments/",$key)){
91 $s_action="del_multiple";
92 }
93 }
95 /* Create options */
96 if(isset($_POST['menu_action']) && preg_match("/^dep_new_/",$_POST['menu_action'])){
97 $s_action = "new";
98 $s_entry = preg_replace("/^dep_new_([a-z]*)/","\\1",$_POST['menu_action']);
99 }
101 /* handle remove from layers menu */
102 if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
103 $s_action = "del_multiple";
104 }
107 /***************
108 Create a new department
109 ***************/
111 /* New Entry if Posted action (s_action) == new
112 */
113 if ($s_action=="new"){
114 $this->dn= "new";
116 $types = $this->get_support_departments();
117 $type = "";
118 foreach($types as $key => $data){
119 if($data['ATTR'] == $s_entry){
120 $type = $key;
121 break;
122 }
123 }
125 if(isset($types[$type])){
126 $tab = $types[$type]['TAB'];
127 $acl = $types[$type]['ACL'];
128 $this->deptabs= new deptabs($this->config,$this->config->data['TABS'][$tab], $this->dn,$acl);
129 $this->deptabs->set_acl_base($this->DivListDepartment->selectedBase);
130 }else{
131 trigger_error("Invalid / Not implemented countainer type.");
132 }
133 }
136 /***************
137 Edit entry
138 ***************/
140 /* Edit Entry if Posted action (s_action) == edit
141 * The entry which will be edited is defined in $s_entry
142 */
143 if (( $s_action=="edit") && (!isset($this->deptabs->config))){
145 $types = $this->get_support_departments();
147 $this->dn= $this->departments[trim($s_entry)]['dn'];
148 $entry = $this->departments[trim($s_entry)];
150 /* Detect department type
151 */
152 $data = array();
153 foreach($types as $oc => $type){
154 if(in_array($oc,$entry['objectClass'])){
155 $data = $type;
156 break;
157 }
158 }
160 /* Check if the department type was editable
161 */
162 if(!count($data)){
163 trigger_error("Unknown department type skipped '".$this->dn."'.");
164 }elseif(empty($data['TAB'])){
165 // Do nothing, this object is currently not editable
166 }else{
168 if (($user= get_lock($this->dn)) != ""){
169 return(gen_locked_message ($user, $this->dn));
170 }
172 /* Lock the current entry, so everyone will get the above dialog */
173 add_lock ($this->dn, $this->ui->dn);
175 /* Register deptabs to trigger edit dialog */
176 $this->deptabs= new deptabs($this->config,$this->config->data['TABS'][$data['TAB']], $this->dn,"department");
177 $this->deptabs->set_acl_base($this->dn);
179 session::set('objectinfo',$this->dn);
180 }
181 }
184 /********************
185 Delete MULTIPLE entries requested, display confirm dialog
186 ********************/
188 if ($s_action=="del_multiple"){
189 $ids = $this->list_get_selected_items();
192 if(count($ids)){
193 $this->dns = array();
194 foreach($ids as $id){
195 $id = base64_decode($id);
196 $this->dns[$id] = $dn = $this->config->departments[$id];
197 }
199 /* Check locks */
200 if ($user= get_multiple_locks($this->dns)){
201 return(gen_locked_message($user,$this->dns));
202 }
204 $dns_names = array();
205 foreach($this->dns as $dn){
206 $dns_names[] = @LDAP::fix($dn);
207 }
208 add_lock ($this->dns, $this->ui->dn);
210 /* Lock the current entry, so nobody will edit it during deletion */
211 $smarty->assign("info", msgPool::deleteInfo($dns_names));
212 $smarty->assign("multiple", true);
213 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
214 }
215 }
218 /********************
219 Delete MULTIPLE entries confirmed
220 ********************/
222 /* Confirmation for deletion has been passed. Users should be deleted. */
223 if (isset($_POST['delete_multiple_department_confirm'])){
225 /* Remove user by user and check acls before removeing them */
226 foreach($this->dns as $key => $dn){
227 $acl = $this->ui->get_permissions($dn,"department/department");
228 if (preg_match('/d/', $acl)){
230 /* Delete request is permitted, perform LDAP action */
231 $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $dn,"department");
232 $this->deptabs->set_acl_base();
233 $this->deptabs->delete ();
234 $this->deptabs = NULL;
235 } else {
236 msg_dialog::display(_("Permission error"), msgPool::permDelete(), WARNING_DIALOG);
237 }
238 }
240 /* Remove lock file after successfull deletion */
241 $this->remove_lock();
242 $this->dns = array();
243 }
246 /********************
247 Delete MULTIPLE entries Canceled
248 ********************/
250 /* Remove lock */
251 if(isset($_POST['delete_multiple_department_cancel'])){
253 /* Remove lock file after successfull deletion */
254 $this->remove_lock();
255 $this->dns = array();
256 }
259 /***************
260 Delete entry
261 ***************/
263 /* Delete Entry if Posted action (s_action) == del
264 * The entry which will be deleted is defined in $s_entry
265 */
266 if ($s_action =="del"){
267 $this->dn= $this->config->departments[trim($s_entry)];
269 /* check acls */
270 $acl = $this->ui->get_permissions($this->dn,"department/department");
271 if(preg_match("/d/",$acl)){
273 /* Check locking */
274 if (($user= get_lock($this->dn)) != ""){
275 session::set('dn',$this->dn);
276 return(gen_locked_message($user, $this->dn));
277 } else {
278 add_lock ($this->dn, $this->ui->dn);
279 $smarty->assign("info", sprintf(_("You're about to delete the whole LDAP subtree placed under '%s'."), @LDAP::fix($this->dn)));
280 $smarty->assign("multiple", false);
281 $display.= $smarty->fetch (get_template_path('remove.tpl', TRUE));
282 return ($display);
283 }
284 }else{
285 msg_dialog::display(_("Permission error"), msgPool::permDelete(), WARNING_DIALOG);
286 }
287 }
290 /***************
291 Delete department confirmed
292 ***************/
294 /* If department deletion is accepted ...
295 * Finally delete department
296 */
297 if (isset($_POST['delete_department_confirm'])){
299 /* check acls */
300 $acl = $this->ui->get_permissions($this->dn,"department/department");
301 if(preg_match("/d/",$acl)){
302 $this->remove_from_parent();
303 } else {
304 msg_dialog::display(_("Permission error"), msgPool::permDelete(), WARNING_DIALOG);
305 }
306 }
309 /***************
310 Handle tagging/recursive move (Return output for an iframe)
311 ***************/
313 /* initiate recursive remove (Is called from iframe, generates output)*/
314 if(isset($_GET['PerformRecMove'])){
315 $this->deptabs->move_me();
316 $this->DivListDepartment->selectedBase = $this->deptabs->by_object['department']->dn;
317 exit();
318 }
320 /* This department must be tagged (Is called from iframe, generates output)*/
321 if(isset($_GET['TagDepartment'])){
322 $base_name = $this->deptabs->base_name;
323 $this->deptabs->by_object[$base_name]->tag_objects();
324 exit();
325 }
328 /***************
329 Edit department finished
330 ***************/
332 if (is_object($this->deptabs) && // Ensure we have a valid deptab here
333 (isset($_POST['edit_finish']) || // If 'Save' button is pressed in the edit dialog.
334 isset($_POST['dep_move_confirm']) || // The move(rename) confirmation was given
335 $this->deptabs->move_done())){ // The move(rename) is done, we have to save the rest now.
337 /* Check tabs, will feed message array.
338 This call will also initiate a sav_object() call.
339 So don't move it below the moved check !.
340 */
341 $message= $this->deptabs->check();
343 /*************
344 MOVED ?
345 Check if this department has to be moved
346 *************/
347 if(!isset($_POST['dep_move_confirm']) && $this->deptabs->am_i_moved()){
348 return($smarty->fetch(get_template_path("dep_move_confirm.tpl",TRUE)));
349 }elseif(isset($_POST['dep_move_confirm']) && $this->deptabs->am_i_moved()){
350 $smarty = get_smarty();
351 $smarty->assign("src","?plug=".$_GET['plug']."&PerformRecMove&no_output_compression");
352 $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."));
353 return($smarty->fetch(get_template_path("dep_iframe.tpl",TRUE)));
354 }
356 /* Save, or display error message? */
357 if (count($message) == 0){
358 global $config;
360 $this->deptabs->save();
361 $config->get_departments();
362 $config->make_idepartments();
363 $this->config = $config;
365 /* This var indicated that there is an object which isn't saved right now. */
366 $this->ObjectInSaveMode = true;
368 /* This object must be tagged, so set ObjectTaggingRequested to true */
369 if($this->deptabs->by_object[$this->deptabs->base_name]->must_be_tagged()){
370 $smarty = get_smarty();
371 $smarty->assign("src","?plug=".$_GET['plug']."&TagDepartment&no_output_compression");
372 $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."));
373 return($smarty->fetch(get_template_path("dep_iframe.tpl",TRUE)));
374 }
376 } else {
377 /* Ok. There seem to be errors regarding to the tab data,
378 show message and continue as usual. */
379 msg_dialog::displayChecks($message);
380 }
381 }
384 /***************
385 In case of tagging/moving the object wasn't deleted, do it know
386 ***************/
388 /* If there is an unsaved object and all operations are done
389 remove locks & save object tab & unset current object */
390 if($this->ObjectInSaveMode){
391 $this->config->get_departments();
392 $this->ObjectInSaveMode = false;
393 if ($this->dn != "new"){
394 $this->remove_lock();
395 }
396 unset ($this->deptabs);
397 $this->deptabs= NULL;
398 session::un_set('objectinfo');
399 }
402 /***************
403 Dialog canceled
404 ***************/
406 /* User canceled edit oder delete
407 * Cancel dialog
408 */
409 if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) || isset($_POST['delete_department_confirm'])){
410 $this->remove_lock();
411 $this->deptabs= NULL;
412 session::un_set('objectinfo');
413 }
415 /* Headpage or normal plugin screen? */
416 if ($this->deptabs !== NULL){
418 /* Show main page (tabs) */
419 $display= $this->deptabs->execute();
420 if (!$this->deptabs->by_object[$this->deptabs->current]->dialog){
421 $display.= "<p style=\"text-align:right\">\n";
422 $display.= "<input type=submit name=\"edit_finish\" value=\"".msgPool::okButton()."\">\n";
423 $display.= " \n";
424 $display.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
425 $display.= "</p>";
426 }
427 return ($display);
428 }else{
429 /* Display dialog with group list */
430 $this->DivListDepartment->parent = $this;
431 $this->DivListDepartment->execute();
432 $this->reload();
433 $this->DivListDepartment->DepartmentsAdded = true;
434 $this->DivListDepartment->setEntries($this->departments);
435 return($this->DivListDepartment->Draw());
436 }
437 }
440 function reload()
441 {
442 /* Vairaible init */
443 $base = $this->DivListDepartment->selectedBase;
444 $base_back = preg_replace("/^[^,]+,/","",$base);
445 $Regex = $this->DivListDepartment->Regex;
447 // Create Array to Test if we have a valid back button
448 $config = session::get('config');
449 $tmp = $config->idepartments;
451 $types = $this->get_support_departments();
453 /* Create search filter for each department type
454 */
455 $res = array();
456 if($this->DivListDepartment->SubSearch){
457 foreach($types as $oc => $data){
458 $oc_f = "(&(objectClass=".$data['OC'].")(objectClass=gosaDepartment))";
459 $res= array_merge($res,get_list("(&(|(".$data['ATTR']."=$Regex)(description=$Regex))".$oc_f.")",
460 "department", $base, array($data['ATTR'],"objectClass", "description"), GL_SIZELIMIT | GL_SUBSEARCH));
461 }
462 }else{
463 foreach($types as $oc => $data){
464 $oc_f = "(&(objectClass=".$data['OC'].")(objectClass=gosaDepartment))";
465 $res= array_merge($res,get_list("(&(|(".$data['ATTR']."=$Regex)(description=$Regex))".$oc_f.")",
466 "department", $base, array($data['ATTR'], "objectClass","description"), GL_SIZELIMIT ));
467 }
468 }
470 /* Prepare list (sortable index)
471 */
472 $deps = array();
473 $this->departments = array();
474 foreach($res as $attr){
476 /* Don't display base as entry on subsearch */
477 if(($attr['dn'] == $base) && ($this->DivListDepartment->SubSearch)){
478 continue;
479 }
481 /* Detect type and create sortable index
482 */
483 $found =FALSE;
484 foreach($types as $oc => $data){
485 if(in_array($data['OC'],$attr['objectClass']) && isset($attr[$data['ATTR']][0])){
486 $name = $attr[$data['ATTR']][0]."-".$attr['dn'];
487 $deps[$name] = $attr;
488 $found =TRUE;
489 break;
490 }
491 }
492 }
493 uksort($deps, 'strnatcasecmp');
495 /* In case of a valid back button create entry
496 */
497 if(isset($tmp[$base])){
498 $ldap = $this->config->get_ldap_link();
499 $ldap->cd($this->config->current['BASE']);
500 $ldap->cat($base );
501 $attr = $ldap->fetch();
502 foreach($types as $oc => $type){
503 if(in_array($oc,$attr['objectClass'])){
504 $attr[$type['ATTR']][0] = ".";
505 }
506 }
507 if(isset($attr['description'])){
508 unset($attr['description']);
509 }
510 $deps= array_merge(array($attr),$deps);
511 }
513 $this->departments = array_values($deps);
514 }
516 function remove_from_parent()
517 {
518 $ldap= $this->config->get_ldap_link();
519 $ldap->cd ($this->dn);
520 $ldap->recursive_remove();
522 /* Optionally execute a command after we're done */
523 $this->postremove();
525 /* Delete references to object groups */
526 $ldap->cd ($this->config->current['BASE']);
527 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
528 while ($ldap->fetch()){
529 $og= new ogroup($this->config, $ldap->getDN());
530 unset($og->member[$this->dn]);
531 $og->save ();
532 }
534 }
537 function list_get_selected_items()
538 {
539 $ids = array();
540 foreach($_POST as $name => $value){
541 if(preg_match("/^item_selected_[a-z0-9\\/\=]*$/i",$name)){
542 $id = preg_replace("/^item_selected_/","",$name);
543 $ids[$id] = $id;
544 }
545 }
546 return($ids);
547 }
550 function remove_lock()
551 {
552 if (isset($this->dn)){
553 del_lock ($this->dn);
554 }
555 if(isset($this->dn) && !empty($this->dn) && $this->dn != "new"){
556 del_lock($this->dn);
557 }
558 if(isset($this->dns) && is_array($this->dns) && count($this->dns)){
559 del_lock($this->dns);
560 }
561 }
563 function save_object()
564 {
565 /* reload department */
566 $this->config->get_departments();
568 $this->config->make_idepartments();
569 $this->DivListDepartment->config= $this->config;
570 $this->DivListDepartment->save_object();
571 }
574 /*! \brief Returns information about all container types that GOsa con handle.
575 @return Array Informations about departments supported by GOsa.
576 */
577 public static function get_support_departments()
578 {
579 /* Domain component
580 */
581 $types = array();
582 $types['dcObject']['ACL'] = "dcObject";
583 $types['dcObject']['CLASS'] = "dcObject";
584 $types['dcObject']['ATTR'] = "dc";
585 $types['dcObject']['TAB'] = "DCOBJECT_TABS";
586 $types['dcObject']['OC'] = "dcObject";
587 $types['dcObject']['IMG'] = "plugins/departments/images/dcObject.png";
588 $types['dcObject']['IMG_FULL']= "plugins/departments/images/dcObject.png";
589 $types['dcObject']['TITLE'] = _("Domain Component");
590 $types['dcObject']['TPL'] = "dcObject.tpl";
592 /* Country object
593 */
594 $types['country']['ACL'] = "country";
595 $types['country']['CLASS'] = "country";
596 $types['country']['TAB'] = "COUNTRY_TABS";
597 $types['country']['ATTR'] = "c";
598 $types['country']['OC'] = "country";
599 $types['country']['IMG'] = "plugins/departments/images/country.png";
600 $types['country']['IMG_FULL']= "plugins/departments/images/country.png";
601 $types['country']['TITLE'] = _("Country");
602 $types['country']['TPL'] = "country.tpl";
604 /* Locality object
605 */
606 $types['locality']['ACL'] = "locality";
607 $types['locality']['CLASS'] = "locality";
608 $types['locality']['TAB'] = "LOCALITY_TABS";
609 $types['locality']['ATTR'] = "l";
610 $types['locality']['OC'] = "locality";
611 $types['locality']['IMG'] = "plugins/departments/images/locality.png";
612 $types['locality']['IMG_FULL']= "plugins/departments/images/locality.png";
613 $types['locality']['TITLE'] = _("Locality");
614 $types['locality']['TPL'] = "locality.tpl";
616 /* Organization
617 */
618 $types['organization']['ACL'] = "organization";
619 $types['organization']['CLASS'] = "organization";
620 $types['organization']['TAB'] = "ORGANIZATION_TABS";
621 $types['organization']['ATTR'] = "o";
622 $types['organization']['OC'] = "organization";
623 $types['organization']['IMG'] = "plugins/departments/images/organization.png";
624 $types['organization']['IMG_FULL']= "plugins/departments/images/organization.png";
625 $types['organization']['TITLE'] = _("Organization");
626 $types['organization']['TPL'] = "organization.tpl";
628 /* Organization
629 */
630 $types['organizationalUnit']['ACL'] = "department";
631 $types['organizationalUnit']['CLASS'] = "department";
632 $types['organizationalUnit']['TAB'] = "DEPTABS";
633 $types['organizationalUnit']['ATTR'] = "ou";
634 $types['organizationalUnit']['OC'] = "organizationalUnit";
635 $types['organizationalUnit']['IMG'] = "plugins/departments/images/department.png";
636 $types['organizationalUnit']['IMG_FULL']= "plugins/departments/images/department.png";
637 $types['organizationalUnit']['TITLE'] = _("Department");
638 $types['organizationalUnit']['TPL'] = "generic.tpl";
640 /* Alias
641 */
642 $types['alias']['ACL'] = "alias";
643 $types['alias']['CLASS'] = "alias";
644 $types['alias']['TAB'] = "ALIASTABS";
645 $types['alias']['ATTR'] = "???";
646 $types['alias']['OC'] = "???";
647 $types['alias']['IMG'] = "plugins/departments/images/alias.png";
648 $types['alias']['IMG_FULL']= "plugins/departments/images/alias.png";
649 $types['alias']['TITLE'] = _("Alias");
650 $types['alias']['TPL'] = "alias.tpl";
652 /* Referal
653 */
654 $types['referal']['ACL'] = "referal";
655 $types['referal']['CLASS'] = "referal";
656 $types['referal']['TAB'] = "REFERALTABS";
657 $types['referal']['ATTR'] = "???";
658 $types['referal']['OC'] = "???";
659 $types['referal']['IMG'] = "plugins/departments/images/referal.png";
660 $types['referal']['IMG_FULL']= "plugins/departments/images/referal.png";
661 $types['referal']['TITLE'] = _("Referal");
662 $types['referal']['TPL'] = "referal.tpl";
664 return($types);
665 }
666 }
667 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
668 ?>