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 /* Possible department types
430 */
431 $types['country'] = array("ACL" => "country", "ATTR" => "c",
432 "CLS" => "(objectClass=country)",
433 "IMG" => "plugins/departments/images/country.png", "ABBR" => "c");
434 $types['locality'] = array("ACL" => "locality", "ATTR" => "l",
435 "CLS" => "(objectClass=locality)",
436 "IMG" => "plugins/departments/images/country.png", "ABBR" => "l");
437 $types['organizationalUnit'] = array("ACL" => "department", "ATTR" => "ou",
438 "CLS" => "(&(objectClass=gosaDepartment)(objectClass=organizationalUnit))",
439 "IMG" => "plugins/departments/images/country.png", "ABBR" => "ou");
440 $types['organization'] = array("ACL" => "organization", "ATTR" => "o",
441 "CLS" => "(objectClass=organization)",
442 "IMG" => "plugins/departments/images/country.png", "ABBR" => "o");
443 $types['Alias??'] = array("ACL" => "alias" , "ATTR" => "alias?",
444 "CLS" => "(objectClass=alias??)",
445 "IMG" => "plugins/departments/images/country.png", "ABBR" => "alias");
446 $types['Referal??'] = array("ACL" => "referal", "ATTR" => "referal?",
447 "CLS" => "(objectClass=referal??)",
448 "IMG" => "plugins/departments/images/country.png", "ABBR" => "referal");
450 /* Create search filter for each department type
451 */
452 $res = array();
453 if($this->DivListDepartment->SubSearch){
454 foreach($types as $oc => $data){
455 $res= array_merge($res,get_list("(&(|(".$data['ATTR']."=$Regex)(description=$Regex))".$data['CLS'].")",
456 "department", $base, array($data['ATTR'],"objectClass", "description"), GL_SIZELIMIT | GL_SUBSEARCH));
457 }
458 }else{
459 foreach($types as $oc => $data){
460 $res= array_merge($res,get_list("(&(|(".$data['ATTR']."=$Regex)(description=$Regex))".$data['CLS'].")",
461 "department", $base, array($data['ATTR'], "objectClass","description"), GL_SIZELIMIT ));
462 }
463 }
465 /* Prepare list (sortable index)
466 */
467 $deps = array();
468 $this->departments = array();
469 foreach($res as $attr){
471 /* Don't display base as entry on subsearch */
472 if(($attr['dn'] == $base) && ($this->DivListDepartment->SubSearch)){
473 continue;
474 }
476 /* Detect type and create sortable index
477 */
478 $found =FALSE;
479 foreach($types as $oc => $data){
480 if(in_array($oc,$attr['objectClass'])){
481 $name = $attr[$data['ATTR']][0]."-".$attr['dn'];
482 $deps[$name] = $attr;
483 $found =TRUE;
484 break;
485 }
486 }
487 if(!$found ) trigger_error("Not found!");
488 }
489 uksort($deps, 'strnatcasecmp');
491 /* In case of a valid back button create entry
492 */
493 if(isset($tmp[$base_back])){
494 $tmp = array();
495 $tmp['dn'] = $base_back;
496 $tmp['ou'][0] = "..";
497 $tmp['objectClass'] = array("organizationalUnit","gosaDepartment");
498 $tmp['description'][0] = msgPool::backButton();
499 $deps= array_merge(array($tmp),$deps);
500 }
502 $this->departments = array_values($deps);
503 }
505 function remove_from_parent()
506 {
507 $ldap= $this->config->get_ldap_link();
508 $ldap->cd ($this->dn);
509 $ldap->recursive_remove();
511 /* Optionally execute a command after we're done */
512 $this->postremove();
514 /* Delete references to object groups */
515 $ldap->cd ($this->config->current['BASE']);
516 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
517 while ($ldap->fetch()){
518 $og= new ogroup($this->config, $ldap->getDN());
519 unset($og->member[$this->dn]);
520 $og->save ();
521 }
523 }
526 function list_get_selected_items()
527 {
528 $ids = array();
529 foreach($_POST as $name => $value){
530 if(preg_match("/^item_selected_[a-z0-9\\/\=]*$/i",$name)){
531 $id = preg_replace("/^item_selected_/","",$name);
532 $ids[$id] = $id;
533 }
534 }
535 return($ids);
536 }
539 function remove_lock()
540 {
541 if (isset($this->dn)){
542 del_lock ($this->dn);
543 }
544 if(isset($this->dn) && !empty($this->dn) && $this->dn != "new"){
545 del_lock($this->dn);
546 }
547 if(isset($this->dns) && is_array($this->dns) && count($this->dns)){
548 del_lock($this->dns);
549 }
550 }
552 function save_object()
553 {
554 /* reload department */
555 $this->config->get_departments();
557 $this->config->make_idepartments();
558 $this->DivListDepartment->config= $this->config;
559 $this->DivListDepartment->save_object();
560 }
562 }
563 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
564 ?>