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 function departmentManagement (&$config, &$ui)
44 {
45 $this->ui= &$ui;
46 $this->dn= "";
47 $this->config= &$config;
48 $this->DivListDepartment = new divListDepartment($this->config,$this);
49 }
51 function execute()
52 {
53 global $config;
55 /* Call parent execute */
56 plugin::execute();
58 /***************
59 Var init
60 ***************/
62 session::set('LOCK_VARS_TO_USE',array("/^act$/","/^id$/","/^dep_edit_.*/","/^dep_del_.*/","/^item_selected/","/^remove_multiple_departments/","/^menu_action/"));
64 /* Reload departments */
65 $smarty = get_smarty();
66 $display = "";
67 $s_action = ""; // Will contain an action, like del or edit
68 $s_entry = ""; // The entry name for edit delete -...
71 /***************
72 Check posts
73 ***************/
75 // Check Post action
76 foreach($_POST as $key => $val){
77 // Post for delete
78 if(preg_match("/dep_del.*/",$key)){
79 $s_action = "del";
80 $s_entry = preg_replace("/dep_".$s_action."_/i","",$key);
81 $s_entry = preg_replace("/_.*$/","",$s_entry);
82 $s_entry = base64_decode($s_entry);
83 // Post for edit
84 }elseif(preg_match("/dep_edit_.*/",$key)){
85 $s_action="edit";
86 $s_entry = preg_replace("/dep_".$s_action."_/i","",$key);
87 $s_entry = preg_replace("/_.*$/","",$s_entry);
88 $s_entry = base64_decode($s_entry);
89 // Post for new
90 }elseif(preg_match("/^remove_multiple_departments/",$key)){
91 $s_action="del_multiple";
92 }elseif(preg_match("/dep_new.*/",$key)){
93 $s_action="new";
94 }
95 }
97 /* Create options */
98 if(isset($_POST['menu_action']) && $_POST['menu_action'] == "dep_new"){
99 $s_action = "new";
100 }
102 /* handle remove from layers menu */
103 if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
104 $s_action = "del_multiple";
105 }
108 /***************
109 Create a new department
110 ***************/
112 /* New Entry if Posted action (s_action) == new
113 */
114 if ($s_action=="new"){
115 $this->dn= "new";
116 $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn,"department");
117 $this->deptabs->set_acl_base($this->DivListDepartment->selectedBase);
118 }
121 /***************
122 Edit entry
123 ***************/
125 /* Edit Entry if Posted action (s_action) == edit
126 * The entry which will be edited is defined in $s_entry
127 */
128 if (( $s_action=="edit") && (!isset($this->deptabs->config))){
129 $this->dn= $this->config->departments[trim($s_entry)];
131 if (($user= get_lock($this->dn)) != ""){
132 return(gen_locked_message ($user, $this->dn));
133 }
135 /* Lock the current entry, so everyone will get the above dialog */
136 add_lock ($this->dn, $this->ui->dn);
138 /* Register deptabs to trigger edit dialog */
139 $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn,"department");
140 $this->deptabs->set_acl_base($this->dn);
142 session::set('objectinfo',$this->dn);
143 }
146 /********************
147 Delete MULTIPLE entries requested, display confirm dialog
148 ********************/
150 if ($s_action=="del_multiple"){
151 $ids = $this->list_get_selected_items();
154 if(count($ids)){
155 foreach($ids as $id){
156 $id = base64_decode($id);
157 $this->dns[$id] = $dn = $this->config->departments[$id];
158 }
160 /* Check locks */
161 if ($user= get_multiple_locks($this->dns)){
162 return(gen_locked_message($user,$this->dns));
163 }
165 $dns_names = array();
166 foreach($this->dns as $dn){
167 $dns_names[] = @LDAP::fix($dn);
168 }
169 add_lock ($this->dns, $this->ui->dn);
171 /* Lock the current entry, so nobody will edit it during deletion */
172 $smarty->assign("info", msgPool::deleteInfo($dns_names));
173 $smarty->assign("multiple", true);
174 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
175 }
176 }
179 /********************
180 Delete MULTIPLE entries confirmed
181 ********************/
183 /* Confirmation for deletion has been passed. Users should be deleted. */
184 if (isset($_POST['delete_multiple_department_confirm'])){
186 /* Remove user by user and check acls before removeing them */
187 foreach($this->dns as $key => $dn){
188 $acl = $this->ui->get_permissions($dn,"department/department");
189 if (preg_match('/d/', $acl)){
191 /* Delete request is permitted, perform LDAP action */
192 $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $dn,"department");
193 $this->deptabs->set_acl_base();
194 $this->deptabs->delete ();
195 $this->deptabs = NULL;
196 } else {
197 msg_dialog::display(_("Permission error"), msgPool::permDelete(), WARNING_DIALOG);
198 }
199 }
201 /* Remove lock file after successfull deletion */
202 $this->remove_lock();
203 $this->dns = array();
204 }
207 /********************
208 Delete MULTIPLE entries Canceled
209 ********************/
211 /* Remove lock */
212 if(isset($_POST['delete_multiple_department_cancel'])){
214 /* Remove lock file after successfull deletion */
215 $this->remove_lock();
216 $this->dns = array();
217 }
220 /***************
221 Delete entry
222 ***************/
224 /* Delete Entry if Posted action (s_action) == del
225 * The entry which will be deleted is defined in $s_entry
226 */
227 if ($s_action =="del"){
228 $this->dn= $this->config->departments[trim($s_entry)];
230 /* check acls */
231 $acl = $this->ui->get_permissions($this->dn,"department/department");
232 if(preg_match("/d/",$acl)){
234 /* Check locking */
235 if (($user= get_lock($this->dn)) != ""){
236 session::set('dn',$this->dn);
237 return(gen_locked_message($user, $this->dn));
238 } else {
239 add_lock ($this->dn, $this->ui->dn);
240 $smarty->assign("info", sprintf(_("You're about to delete the whole LDAP subtree placed under '%s'."), @LDAP::fix($this->dn)));
241 $smarty->assign("multiple", false);
242 $display.= $smarty->fetch (get_template_path('remove.tpl', TRUE));
243 return ($display);
244 }
245 }else{
246 msg_dialog::display(_("Permission error"), msgPool::permDelete(), WARNING_DIALOG);
247 }
248 }
251 /***************
252 Delete department confirmed
253 ***************/
255 /* If department deletion is accepted ...
256 * Finally delete department
257 */
258 if (isset($_POST['delete_department_confirm'])){
260 /* check acls */
261 $acl = $this->ui->get_permissions($this->dn,"department/department");
262 if(preg_match("/d/",$acl)){
263 $this->remove_from_parent();
264 } else {
265 msg_dialog::display(_("Permission error"), msgPool::permDelete(), WARNING_DIALOG);
266 }
267 }
270 /***************
271 Handle tagging/recursive move (Return output for an iframe)
272 ***************/
274 /* initiate recursive remove (Is called from iframe, generates output)*/
275 if(isset($_GET['PerformRecMove'])){
276 $this->deptabs->move_me();
277 $this->DivListDepartment->selectedBase = $this->deptabs->by_object['department']->dn;
278 exit();
279 }
281 /* This department must be tagged (Is called from iframe, generates output)*/
282 if(isset($_GET['TagDepartment'])){
283 $this->deptabs->by_object['department']->tag_objects();
284 exit();
285 }
288 /***************
289 Edit department finished
290 ***************/
292 if (is_object($this->deptabs) && // Ensure we have a valid deptab here
293 (isset($_POST['edit_finish']) || // If 'Save' button is pressed in the edit dialog.
294 isset($_POST['dep_move_confirm']) || // The move(rename) confirmation was given
295 $this->deptabs->move_done())){ // The move(rename) is done, we have to save the rest now.
297 /* Check tabs, will feed message array.
298 This call will also initiate a sav_object() call.
299 So don't move it below the moved check !.
300 */
301 $message= $this->deptabs->check();
302 $obj = $this->deptabs->by_object['department'];
304 /*************
305 MOVED ?
306 Check if this department has to be moved
307 *************/
308 if(!isset($_POST['dep_move_confirm']) && $this->deptabs->am_i_moved()){
309 return($smarty->fetch(get_template_path("dep_move_confirm.tpl",TRUE)));
310 }elseif(isset($_POST['dep_move_confirm']) && $this->deptabs->am_i_moved()){
311 $smarty = get_smarty();
312 $smarty->assign("src","?plug=".$_GET['plug']."&PerformRecMove&no_output_compression");
313 $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."));
314 return($smarty->fetch(get_template_path("dep_iframe.tpl",TRUE)));
315 }
317 /* Save, or display error message? */
318 if (count($message) == 0){
319 global $config;
321 $this->deptabs->save();
322 $config->get_departments();
323 $config->make_idepartments();
324 $this->config = $config;
326 /* This var indicated that there is an object which isn't saved right now. */
327 $this->ObjectInSaveMode = true;
329 /* This object must be tagged, so set ObjectTaggingRequested to true */
330 if($this->deptabs->by_object['department']->must_be_tagged()){
331 $smarty = get_smarty();
332 $smarty->assign("src","?plug=".$_GET['plug']."&TagDepartment&no_output_compression");
333 $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."));
334 return($smarty->fetch(get_template_path("dep_iframe.tpl",TRUE)));
335 }
337 } else {
338 /* Ok. There seem to be errors regarding to the tab data,
339 show message and continue as usual. */
340 msg_dialog::displayChecks($message);
341 }
342 }
345 /***************
346 In case of tagging/moving the object wasn't deleted, do it know
347 ***************/
349 /* If there is an unsaved object and all operations are done
350 remove locks & save object tab & unset current object */
351 if($this->ObjectInSaveMode){
352 $this->config->get_departments();
353 $this->ObjectInSaveMode = false;
354 if ($this->dn != "new"){
355 $this->remove_lock();
356 }
357 unset ($this->deptabs);
358 $this->deptabs= NULL;
359 session::un_set('objectinfo');
360 }
363 /***************
364 Dialog canceled
365 ***************/
367 /* User canceled edit oder delete
368 * Cancel dialog
369 */
370 if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) || isset($_POST['delete_department_confirm'])){
371 $this->remove_lock();
372 $this->deptabs= NULL;
373 session::un_set('objectinfo');
374 }
376 /* Headpage or normal plugin screen? */
377 if ($this->deptabs !== NULL){
379 /* Show main page (tabs) */
380 $display= $this->deptabs->execute();
381 if (!$this->deptabs->by_object[$this->deptabs->current]->dialog){
382 $display.= "<p style=\"text-align:right\">\n";
383 $display.= "<input type=submit name=\"edit_finish\" value=\"".msgPool::okButton()."\">\n";
384 $display.= " \n";
385 $display.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
386 $display.= "</p>";
387 }
388 return ($display);
389 }else{
390 /* Display dialog with group list */
391 $this->DivListDepartment->parent = $this;
392 $this->DivListDepartment->execute();
393 $this->reload();
394 $this->DivListDepartment->DepartmentsAdded = true;
395 $this->DivListDepartment->setEntries($this->departments);
396 return($this->DivListDepartment->Draw());
397 }
398 }
401 function reload()
402 {
403 /* Vairaible init */
404 $base = $this->DivListDepartment->selectedBase;
405 $base_back = preg_replace("/^[^,]+,/","",$base);
406 $Regex = $this->DivListDepartment->Regex;
408 // Create Array to Test if we have a valid back button
409 $config = session::get('config');
410 $tmp = $config->idepartments;
412 // In case of a valid back button create entry
413 if(isset($tmp[$base_back])){
414 $tmp2 ['dn'] = convert_department_dn($base_back);
416 // If empty always go to top
417 if(empty($tmp2['dn'])){
418 $tmp2['dn']="/";
419 }
420 $tmp2 ['description'][0] = ".. ".msgPool::backButton();
421 $result[$tmp[$base_back]]=$tmp2;
422 }
424 if($this->DivListDepartment->SubSearch){
425 $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
426 "department", $base, array("ou", "description"), GL_SIZELIMIT | GL_SUBSEARCH);
427 }else{
428 $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
429 "department", $base, array("ou", "description"), GL_SIZELIMIT );
430 }
432 $this->departments= array();
434 /* Add current base to the list of available departments, but only if its naming attribute is 'ou' */
435 if(preg_match("/^ou=/",$base)){
436 $this->departments [ convert_department_dn($base) ] = ".";
437 }
439 foreach ($res as $key => $value){
441 /* Don't display base as entry on subsearch */
442 if(($value['dn'] == $base) && ($this->DivListDepartment->SubSearch)){
443 continue;
444 }
446 $cdn= convert_department_dn($value['dn']);
448 /* Append to dep list */
449 if(isset($value["description"][0])){
450 $this->departments[$cdn]= get_sub_department($cdn)." - [".$value["description"][0]."]";
451 }else{
452 $this->departments[$cdn]= get_sub_department($cdn);//$value["description"][0];
453 }
454 }
455 natcasesort ($this->departments);
456 reset ($this->departments);
457 }
459 function remove_from_parent()
460 {
461 $ldap= $this->config->get_ldap_link();
462 $ldap->cd ($this->dn);
463 $ldap->recursive_remove();
465 /* Optionally execute a command after we're done */
466 $this->postremove();
468 /* Delete references to object groups */
469 $ldap->cd ($this->config->current['BASE']);
470 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
471 while ($ldap->fetch()){
472 $og= new ogroup($this->config, $ldap->getDN());
473 unset($og->member[$this->dn]);
474 $og->save ();
475 }
477 }
480 function list_get_selected_items()
481 {
482 $ids = array();
483 foreach($_POST as $name => $value){
484 if(preg_match("/^item_selected_[a-z0-9\\/\=]*$/i",$name)){
485 $id = preg_replace("/^item_selected_/","",$name);
486 $ids[$id] = $id;
487 }
488 }
489 return($ids);
490 }
493 function remove_lock()
494 {
495 if (isset($this->dn)){
496 del_lock ($this->dn);
497 }
498 if(isset($this->dn) && !empty($this->dn) && $this->dn != "new"){
499 del_lock($this->dn);
500 }
501 if(isset($this->dns) && is_array($this->dns) && count($this->dns)){
502 del_lock($this->dns);
503 }
504 }
506 function save_object()
507 {
508 /* reload department */
509 $this->config->get_departments();
511 $this->config->make_idepartments();
512 $this->DivListDepartment->config= $this->config;
513 $this->DivListDepartment->save_object();
514 }
516 }
517 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
518 ?>