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,"department");
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" || $s_action == "del"){
190 if($s_action == "del"){
191 $ids = array($s_entry);
192 }else{
193 $ids = $this->list_get_selected_items();
194 }
196 if(count($ids)){
197 $this->dns = array();
198 foreach($ids as $id){
199 $this->dns[$id] = $dn = $this->departments[$id]['dn'];
200 }
202 /* Check locks */
203 if ($user= get_multiple_locks($this->dns)){
204 return(gen_locked_message($user,$this->dns));
205 }
207 $dns_names = array();
208 foreach($this->dns as $dn){
209 $dns_names[] = LDAP::fix($dn);
210 }
211 add_lock ($this->dns, $this->ui->dn);
213 /* Lock the current entry, so nobody will edit it during deletion */
214 $smarty->assign("info", msgPool::deleteInfo($dns_names));
215 $smarty->assign("multiple", true);
216 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
217 }
218 }
221 /********************
222 Delete MULTIPLE entries confirmed
223 ********************/
225 /* Confirmation for deletion has been passed. Users should be deleted. */
226 if (isset($_POST['delete_multiple_department_confirm'])){
228 /* Do not remove departments that will already be deleted by its parents
229 */
230 foreach($this->dns as $key => $dn){
231 foreach($this->dns as $key2 => $dn2){
232 if($dn == $dn2) continue;
233 if(preg_match("/".preg_quote($dn,'/')."$/",$dn2)){
234 unset($this->dns[$key2]);
235 }
236 }
237 }
239 /* Remove user by user and check acls before removeing them */
240 foreach($this->dns as $key => $dn){
241 $acl = $this->ui->get_permissions($dn,"department/department");
242 if (preg_match('/d/', $acl)){
244 /* Delete request is permitted, perform LDAP action */
245 $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $dn,"department");
246 $this->deptabs->set_acl_base();
247 $this->deptabs->delete ();
248 $this->deptabs = NULL;
249 } else {
250 msg_dialog::display(_("Permission error"), msgPool::permDelete(), WARNING_DIALOG);
251 }
252 }
254 /* Remove lock file after successfull deletion */
255 $this->remove_lock();
256 $this->dns = array();
257 }
260 /********************
261 Delete MULTIPLE entries Canceled
262 ********************/
264 /* Remove lock */
265 if(isset($_POST['delete_multiple_department_cancel'])){
267 /* Remove lock file after successfull deletion */
268 $this->remove_lock();
269 $this->dns = array();
270 }
273 /***************
274 Handle tagging/recursive move (Return output for an iframe)
275 ***************/
277 /* initiate recursive remove (Is called from iframe, generates output)*/
278 if(isset($_GET['PerformRecMove'])){
279 $this->deptabs->move_me();
280 $this->DivListDepartment->selectedBase = $this->deptabs->by_object['department']->dn;
281 exit();
282 }
284 /* This department must be tagged (Is called from iframe, generates output)*/
285 if(isset($_GET['TagDepartment'])){
286 $base_name = $this->deptabs->base_name;
287 $this->deptabs->by_object[$base_name]->tag_objects();
288 exit();
289 }
292 /***************
293 Edit department finished
294 ***************/
296 if (is_object($this->deptabs) && // Ensure we have a valid deptab here
297 (isset($_POST['edit_finish']) || // If 'Save' button is pressed in the edit dialog.
298 isset($_POST['dep_move_confirm']) || // The move(rename) confirmation was given
299 $this->deptabs->move_done())){ // The move(rename) is done, we have to save the rest now.
301 /* Check tabs, will feed message array.
302 This call will also initiate a sav_object() call.
303 So don't move it below the moved check !.
304 */
305 $message= $this->deptabs->check();
307 /*************
308 MOVED ?
309 Check if this department has to be moved
310 *************/
311 if(!isset($_POST['dep_move_confirm']) && $this->deptabs->am_i_moved()){
312 return($smarty->fetch(get_template_path("dep_move_confirm.tpl",TRUE)));
313 }elseif(isset($_POST['dep_move_confirm']) && $this->deptabs->am_i_moved()){
314 $smarty = get_smarty();
315 $smarty->assign("src","?plug=".$_GET['plug']."&PerformRecMove&no_output_compression");
316 $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."));
317 return($smarty->fetch(get_template_path("dep_iframe.tpl",TRUE)));
318 }
320 /* Save, or display error message? */
321 if (count($message) == 0){
322 global $config;
324 $this->deptabs->save();
325 $config->get_departments();
326 $config->make_idepartments();
327 $this->config = $config;
329 /* This var indicated that there is an object which isn't saved right now. */
330 $this->ObjectInSaveMode = true;
332 /* This object must be tagged, so set ObjectTaggingRequested to true */
333 if($this->deptabs->by_object[$this->deptabs->base_name]->must_be_tagged()){
334 $smarty = get_smarty();
335 $smarty->assign("src","?plug=".$_GET['plug']."&TagDepartment&no_output_compression");
336 $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."));
337 return($smarty->fetch(get_template_path("dep_iframe.tpl",TRUE)));
338 }
340 } else {
341 /* Ok. There seem to be errors regarding to the tab data,
342 show message and continue as usual. */
343 msg_dialog::displayChecks($message);
344 }
345 }
348 /***************
349 In case of tagging/moving the object wasn't deleted, do it know
350 ***************/
352 /* If there is an unsaved object and all operations are done
353 remove locks & save object tab & unset current object */
354 if($this->ObjectInSaveMode){
355 $this->config->get_departments();
356 $this->ObjectInSaveMode = false;
357 if ($this->dn != "new"){
358 $this->remove_lock();
359 }
360 unset ($this->deptabs);
361 $this->deptabs= NULL;
362 session::un_set('objectinfo');
363 }
366 /***************
367 Dialog canceled
368 ***************/
370 /* User canceled edit oder delete
371 * Cancel dialog
372 */
373 if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) || isset($_POST['delete_department_confirm'])){
374 $this->remove_lock();
375 $this->deptabs= NULL;
376 session::un_set('objectinfo');
377 }
379 /* Headpage or normal plugin screen? */
380 if ($this->deptabs !== NULL){
382 /* Show main page (tabs) */
383 $display= $this->deptabs->execute();
384 if (!$this->deptabs->by_object[$this->deptabs->current]->dialog){
385 $display.= "<p style=\"text-align:right\">\n";
386 $display.= "<input type=submit name=\"edit_finish\" value=\"".msgPool::okButton()."\">\n";
387 $display.= " \n";
388 $display.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
389 $display.= "</p>";
390 }
391 return ($display);
392 }else{
393 /* Display dialog with group list */
394 $this->DivListDepartment->parent = $this;
395 $this->DivListDepartment->execute();
396 $this->reload();
397 $this->DivListDepartment->DepartmentsAdded = true;
398 $this->DivListDepartment->setEntries($this->departments);
399 return($this->DivListDepartment->Draw());
400 }
401 }
404 function reload()
405 {
406 /* Vairaible init */
407 $base = $this->DivListDepartment->selectedBase;
408 $base_back = preg_replace("/^[^,]+,/","",$base);
409 $Regex = $this->DivListDepartment->Regex;
411 // Create Array to Test if we have a valid back button
412 $config = session::get('config');
413 $tmp = $config->idepartments;
415 $types = $this->get_support_departments();
417 /* Create search filter for each department type
418 */
420 /* Get all departments within this subtree */
421 $s_filter = "";
422 $s_attrs = array("description","objectClass");
423 $s_acls = array();
424 foreach($types as $oc => $data){
425 $s_filter.= "(&(objectClass=gosaDepartment)(objectClass=".$data['OC'].")".
426 "(|(".$data['ATTR']."=".$Regex.")(description=".$Regex.")))";
427 $s_attrs[]= $data['ATTR'];
428 $s_acls[] = "department/".$data['ACL'];
429 }
431 if($this->DivListDepartment->SubSearch){
432 $res = get_list("(|".$s_filter.")",$s_acls,$base,$s_attrs,GL_SIZELIMIT | GL_SUBSEARCH);
433 }else{
434 $res = get_list("(|".$s_filter.")",$s_acls,$base,$s_attrs,GL_SIZELIMIT);
435 }
437 /* Prepare list (sortable index)
438 */
439 $deps = array();
440 $this->departments = array();
441 foreach($res as $attr){
443 /* Don't display base as entry on subsearch */
444 if(($attr['dn'] == $base) && ($this->DivListDepartment->SubSearch)){
445 continue;
446 }
448 /* Detect type and create sortable index
449 */
450 $found =FALSE;
451 foreach($types as $oc => $data){
452 if(in_array($data['OC'],$attr['objectClass']) && isset($attr[$data['ATTR']][0])){
453 $name = $attr[$data['ATTR']][0]."-".$attr['dn'];
454 $deps[$name] = $attr;
455 $found =TRUE;
456 break;
457 }
458 }
459 }
460 uksort($deps, 'strnatcasecmp');
462 /* In case of a valid back button create entry
463 */
464 if(isset($tmp[$base])){
465 $ldap = $this->config->get_ldap_link();
466 $ldap->cd($this->config->current['BASE']);
467 $ldap->cat($base );
468 $attr = $ldap->fetch();
469 foreach($types as $oc => $type){
470 if(in_array($oc,$attr['objectClass'])){
471 $attr[$type['ATTR']][0] = ".";
472 }
473 }
474 if(isset($attr['description'])){
475 unset($attr['description']);
476 }
477 $deps= array_merge(array($attr),$deps);
478 }
480 $this->departments = array_values($deps);
481 }
483 function remove_from_parent()
484 {
485 $ldap= $this->config->get_ldap_link();
486 $ldap->cd ($this->dn);
487 $ldap->recursive_remove();
489 /* Optionally execute a command after we're done */
490 $this->postremove();
492 /* Delete references to object groups */
493 $ldap->cd ($this->config->current['BASE']);
494 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
495 while ($ldap->fetch()){
496 $og= new ogroup($this->config, $ldap->getDN());
497 unset($og->member[$this->dn]);
498 $og->save ();
499 }
501 }
504 function list_get_selected_items()
505 {
506 $ids = array();
507 foreach($_POST as $name => $value){
508 if(preg_match("/^item_selected_[a-z0-9\\/\=]*$/i",$name)){
509 $id = preg_replace("/^item_selected_/","",$name);
510 $ids[$id] = $id;
511 }
512 }
513 return($ids);
514 }
517 function remove_lock()
518 {
519 if (isset($this->dn)){
520 del_lock ($this->dn);
521 }
522 if(isset($this->dn) && !empty($this->dn) && $this->dn != "new"){
523 del_lock($this->dn);
524 }
525 if(isset($this->dns) && is_array($this->dns) && count($this->dns)){
526 del_lock($this->dns);
527 }
528 }
530 function save_object()
531 {
532 /* reload department */
533 # $this->config->get_departments();
534 # $this->config->make_idepartments();
535 $this->DivListDepartment->config= $this->config;
536 $this->DivListDepartment->save_object();
537 }
540 /*! \brief Returns information about all container types that GOsa con handle.
541 @return Array Informations about departments supported by GOsa.
542 */
543 public static function get_support_departments()
544 {
545 /* Domain component
546 */
547 $types = array();
548 $types['dcObject']['ACL'] = "dcObject";
549 $types['dcObject']['CLASS'] = "dcObject";
550 $types['dcObject']['ATTR'] = "dc";
551 $types['dcObject']['TAB'] = "DCOBJECT_TABS";
552 $types['dcObject']['OC'] = "dcObject";
553 $types['dcObject']['IMG'] = "plugins/departments/images/dc.png";
554 $types['dcObject']['IMG_FULL']= "plugins/departments/images/dc.png";
555 $types['dcObject']['TITLE'] = _("Domain Component");
556 $types['dcObject']['TPL'] = "dcObject.tpl";
558 /* Country object
559 */
560 $types['country']['ACL'] = "country";
561 $types['country']['CLASS'] = "country";
562 $types['country']['TAB'] = "COUNTRY_TABS";
563 $types['country']['ATTR'] = "c";
564 $types['country']['OC'] = "country";
565 $types['country']['IMG'] = "plugins/departments/images/country.png";
566 $types['country']['IMG_FULL']= "plugins/departments/images/country.png";
567 $types['country']['TITLE'] = _("Country");
568 $types['country']['TPL'] = "country.tpl";
570 /* Locality object
571 */
572 $types['locality']['ACL'] = "locality";
573 $types['locality']['CLASS'] = "locality";
574 $types['locality']['TAB'] = "LOCALITY_TABS";
575 $types['locality']['ATTR'] = "l";
576 $types['locality']['OC'] = "locality";
577 $types['locality']['IMG'] = "plugins/departments/images/locality.png";
578 $types['locality']['IMG_FULL']= "plugins/departments/images/locality.png";
579 $types['locality']['TITLE'] = _("Locality");
580 $types['locality']['TPL'] = "locality.tpl";
582 /* Organization
583 */
584 $types['organization']['ACL'] = "organization";
585 $types['organization']['CLASS'] = "organization";
586 $types['organization']['TAB'] = "ORGANIZATION_TABS";
587 $types['organization']['ATTR'] = "o";
588 $types['organization']['OC'] = "organization";
589 $types['organization']['IMG'] = "plugins/departments/images/organization.png";
590 $types['organization']['IMG_FULL']= "plugins/departments/images/organization.png";
591 $types['organization']['TITLE'] = _("Organization");
592 $types['organization']['TPL'] = "organization.tpl";
594 /* Department
595 */
596 $types['organizationalUnit']['ACL'] = "department";
597 $types['organizationalUnit']['CLASS'] = "department";
598 $types['organizationalUnit']['TAB'] = "DEPTABS";
599 $types['organizationalUnit']['ATTR'] = "ou";
600 $types['organizationalUnit']['OC'] = "organizationalUnit";
601 $types['organizationalUnit']['IMG'] = "images/lists/folder.png";//plugins/departments/images/department.png";
602 $types['organizationalUnit']['IMG_FULL']= "images/lists/folder-full.png";//:wplugins/departments/images/department.png";
603 $types['organizationalUnit']['TITLE'] = _("Department");
604 $types['organizationalUnit']['TPL'] = "generic.tpl";
607 # /* Alias
608 # */
609 # $types['alias']['ACL'] = "alias";
610 # $types['alias']['CLASS'] = "alias";
611 # $types['alias']['TAB'] = "ALIASTABS";
612 # $types['alias']['ATTR'] = "???";
613 # $types['alias']['OC'] = "???";
614 # $types['alias']['IMG'] = "plugins/departments/images/department_alias.png";
615 # $types['alias']['IMG_FULL']= "plugins/departments/images/department_alias.png";
616 # $types['alias']['TITLE'] = _("Alias");
617 # $types['alias']['TPL'] = "alias.tpl";
618 #
619 # /* Referal
620 # */
621 # $types['referal']['ACL'] = "referal";
622 # $types['referal']['CLASS'] = "referal";
623 # $types['referal']['TAB'] = "REFERALTABS";
624 # $types['referal']['ATTR'] = "???";
625 # $types['referal']['OC'] = "???";
626 # $types['referal']['IMG'] = "plugins/departments/images/department_referal.png";
627 # $types['referal']['IMG_FULL']= "plugins/departments/images/department_referal.png";
628 # $types['referal']['TITLE'] = _("Referal");
629 # $types['referal']['TPL'] = "referal.tpl";
631 return($types);
632 }
633 }
634 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
635 ?>