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
42 function departmentManagement (&$config, &$ui)
43 {
44 $this->ui= &$ui;
45 $this->dn= "";
46 $this->config= &$config;
47 $this->DivListDepartment = new divListDepartment($this->config,$this);
48 }
50 function execute()
51 {
52 global $config;
54 /* Call parent execute */
55 plugin::execute();
57 /***************
58 Var init
59 ***************/
61 session::set('LOCK_VARS_TO_USE',array("/^act$/","/^id$/","/^dep_edit_.*/","/^dep_del_.*/","/^item_selected/","/^remove_multiple_departments/"));
63 /* Reload departments */
64 $smarty = get_smarty();
65 $display = "";
66 $s_action = ""; // Will contain an action, like del or edit
67 $s_entry = ""; // The entry name for edit delete -...
70 /***************
71 Check posts
72 ***************/
74 // Check Post action
75 foreach($_POST as $key => $val){
76 // Post for delete
77 if(preg_match("/dep_del.*/",$key)){
78 $s_action = "del";
79 $s_entry = preg_replace("/dep_".$s_action."_/i","",$key);
80 $s_entry = preg_replace("/_.*$/","",$s_entry);
81 $s_entry = base64_decode($s_entry);
82 // Post for edit
83 }elseif(preg_match("/dep_edit_.*/",$key)){
84 $s_action="edit";
85 $s_entry = preg_replace("/dep_".$s_action."_/i","",$key);
86 $s_entry = preg_replace("/_.*$/","",$s_entry);
87 $s_entry = base64_decode($s_entry);
88 // Post for new
89 }elseif(preg_match("/^remove_multiple_departments/",$key)){
90 $s_action="del_multiple";
91 }elseif(preg_match("/dep_new.*/",$key)){
92 $s_action="new";
93 }
94 }
96 /* Create options */
97 if(isset($_POST['menu_action']) && $_POST['menu_action'] == "dep_new"){
98 $s_action = "new";
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";
115 $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn,"department");
116 $this->deptabs->set_acl_base($this->DivListDepartment->selectedBase);
117 }
120 /***************
121 Edit entry
122 ***************/
124 /* Edit Entry if Posted action (s_action) == edit
125 * The entry which will be edited is defined in $s_entry
126 */
127 if (( $s_action=="edit") && (!isset($this->deptabs->config))){
128 $this->dn= $this->config->departments[trim($s_entry)];
130 if (($user= get_lock($this->dn)) != ""){
131 return(gen_locked_message ($user, $this->dn));
132 }
134 /* Lock the current entry, so everyone will get the above dialog */
135 add_lock ($this->dn, $this->ui->dn);
137 /* Register deptabs to trigger edit dialog */
138 $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn,"department");
139 $this->deptabs->set_acl_base($this->dn);
141 session::set('objectinfo',$this->dn);
142 }
145 /********************
146 Delete MULTIPLE entries requested, display confirm dialog
147 ********************/
149 if ($s_action=="del_multiple"){
150 $ids = $this->list_get_selected_items();
153 if(count($ids)){
154 foreach($ids as $id){
155 $id = base64_decode($id);
156 $dn = $this->config->departments[$id];
158 if (($user= get_lock($dn)) != ""){
159 return(gen_locked_message ($user, $dn));
160 }
161 $this->dns[$id] = $dn;
162 }
164 $dns_names = "<br><pre>";
165 foreach($this->dns as $dn){
166 add_lock ($dn, $this->ui->dn);
167 $dns_names .= $dn."\n";
168 }
169 $dns_names .="</pre>";
171 /* Lock the current entry, so nobody will edit it during deletion */
172 if (count($this->dns) == 1){
173 $smarty->assign("info", sprintf(_("You're about to delete the following entry %s"), @LDAP::fix($dns_names)));
174 } else {
175 $smarty->assign("info", sprintf(_("You're about to delete the following entries %s"), @LDAP::fix($dns_names)));
176 }
177 $smarty->assign("multiple", true);
178 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
179 }
180 }
183 /********************
184 Delete MULTIPLE entries confirmed
185 ********************/
187 /* Confirmation for deletion has been passed. Users should be deleted. */
188 if (isset($_POST['delete_multiple_department_confirm'])){
190 /* Remove user by user and check acls before removeing them */
191 foreach($this->dns as $key => $dn){
192 $acl = $this->ui->get_permissions($dn,"department/department");
193 if (preg_match('/d/', $acl)){
195 /* Delete request is permitted, perform LDAP action */
196 $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $dn,"department");
197 $this->deptabs->set_acl_base();
198 $this->deptabs->delete ();
199 $this->deptabs = NULL;
200 } else {
201 msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), WARNING_DIALOG);
202 }
203 /* Remove lock file after successfull deletion */
204 del_lock ($dn);
205 unset($this->dns[$key]);
206 }
207 }
210 /********************
211 Delete MULTIPLE entries Canceled
212 ********************/
214 /* Remove lock */
215 if(isset($_POST['delete_multiple_department_cancel'])){
216 foreach($this->dns as $key => $dn){
217 del_lock ($dn);
218 unset($this->dns[$key]);
219 }
220 }
223 /***************
224 Delete entry
225 ***************/
227 /* Delete Entry if Posted action (s_action) == del
228 * The entry which will be deleted is defined in $s_entry
229 */
230 if ($s_action =="del"){
231 $this->dn= $this->config->departments[trim($s_entry)];
233 /* check acls */
234 $acl = $this->ui->get_permissions($this->dn,"department/department");
235 if(preg_match("/d/",$acl)){
237 /* Check locking */
238 if (($user= get_lock($this->dn)) != ""){
239 session::set('dn',$this->dn);
240 return(gen_locked_message($user, $this->dn));
241 } else {
242 add_lock ($this->dn, $this->ui->dn);
243 $smarty->assign("info", sprintf(_("You're about to delete the whole LDAP subtree placed under '%s'."), @LDAP::fix($this->dn)));
244 $smarty->assign("multiple", false);
245 $display.= $smarty->fetch (get_template_path('remove.tpl', TRUE));
246 return ($display);
247 }
248 }else{
249 msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), WARNING_DIALOG);
250 }
251 }
254 /***************
255 Delete department confirmed
256 ***************/
258 /* If department deletion is accepted ...
259 * Finally delete department
260 */
261 if (isset($_POST['delete_department_confirm'])){
263 /* check acls */
264 $acl = $this->ui->get_permissions($this->dn,"department/department");
265 if(preg_match("/d/",$acl)){
266 $this->remove_from_parent();
267 } else {
268 msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), WARNING_DIALOG);
269 }
270 }
273 /***************
274 Handle recursive move (Return output for an iframe)
275 ***************/
278 /* initiate recursive remove (Is called from iframe, generates output)*/
279 if(isset($_GET['PerformRecMove'])){
280 $this->deptabs->move_me();
281 $this->DivListDepartment->selectedBase = $this->deptabs->by_object['department']->dn;
282 exit();
283 }
285 /* This department must be tagged (Is called from iframe, generates output)*/
286 if(isset($_GET['TagDepartment'])){
287 $this->deptabs->by_object['department']->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();
306 $obj = $this->deptabs->by_object['department'];
308 /*************
309 MOVED ?
310 Check if this department is moved
311 *************/
312 if(!isset($_POST['dep_move_confirm']) && $this->deptabs->am_i_moved()){
313 return($smarty->fetch(get_template_path("dep_move_confirm.tpl",TRUE)));
314 }elseif(isset($_POST['dep_move_confirm']) && $this->deptabs->am_i_moved()){
315 $smarty = get_smarty();
316 $smarty->assign("src","?plug=".$_GET['plug']."&PerformRecMove&no_output_compression");
317 $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."));
318 return($smarty->fetch(get_template_path("dep_iframe.tpl",TRUE)));
319 }
321 /* Save, or display error message? */
322 if (count($message) == 0){
323 global $config;
325 $this->deptabs->save();
326 $config->get_departments();
327 $config->make_idepartments();
328 $this->config = $config;
330 /* This var indicated that there is an object which isn't saved right now. */
331 $this->ObjectInSaveMode = true;
333 /* This object must be tagged, so set ObjectTaggingRequested to true */
334 if($this->deptabs->by_object['department']->must_be_tagged()){
335 $smarty = get_smarty();
336 $smarty->assign("src","?plug=".$_GET['plug']."&TagDepartment&no_output_compression");
337 $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."));
338 return($smarty->fetch(get_template_path("dep_iframe.tpl",TRUE)));
339 }
341 } else {
342 /* Ok. There seem to be errors regarding to the tab data,
343 show message and continue as usual. */
344 msg_dialog::displayChecks($message);
345 }
346 }
349 /***************
350 In case of tagging/moving the object wasn't deleted, do it know
351 ***************/
353 /* If there is an unsaved object and all operations are done
354 remove locks & save object tab & unset current object */
355 if($this->ObjectInSaveMode){
356 $this->config->get_departments();
357 $this->ObjectInSaveMode = false;
358 if ($this->dn != "new"){
359 del_lock ($this->dn);
360 }
361 unset ($this->deptabs);
362 $this->deptabs= NULL;
363 session::un_set('objectinfo');
364 }
367 /***************
368 Dialog canceled
369 ***************/
371 /* User canceled edit oder delete
372 * Cancel dialog
373 */
374 if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) || isset($_POST['delete_department_confirm'])){
375 del_lock ($this->dn);
376 unset($this->depdabs);
377 $this->deptabs= NULL;
378 session::un_set('objectinfo');
379 }
381 /* Headpage or normal plugin screen? */
382 if ($this->deptabs !== NULL){
384 /* Show main page (tabs) */
385 $display= $this->deptabs->execute();
386 if (!$this->deptabs->by_object[$this->deptabs->current]->dialog){
387 $display.= "<p style=\"text-align:right\">\n";
388 $display.= "<input type=submit name=\"edit_finish\" value=\"".msgPool::okButton()."\">\n";
389 $display.= " \n";
390 $display.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
391 $display.= "</p>";
392 }
393 return ($display);
394 }else{
395 /* Display dialog with group list */
396 $this->DivListDepartment->parent = $this;
397 $this->DivListDepartment->execute();
398 $this->reload();
399 $this->DivListDepartment->DepartmentsAdded = true;
400 $this->DivListDepartment->setEntries($this->departments);
401 return($this->DivListDepartment->Draw());
402 }
403 }
406 function reload()
407 {
408 /* Vairaible init */
409 $base = $this->DivListDepartment->selectedBase;
410 $base_back = preg_replace("/^[^,]+,/","",$base);
411 $Regex = $this->DivListDepartment->Regex;
413 // Create Array to Test if we have a valid back button
414 $config = session::get('config');
415 $tmp = $config->idepartments;
417 // In case of a valid back button create entry
418 if(isset($tmp[$base_back])){
419 $tmp2 ['dn'] = convert_department_dn($base_back);
421 // If empty always go to top
422 if(empty($tmp2['dn'])){
423 $tmp2['dn']="/";
424 }
425 $tmp2 ['description'][0] = ".. "._("Back");
426 $result[$tmp[$base_back]]=$tmp2;
427 }
429 if($this->DivListDepartment->SubSearch){
430 $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
431 "department", $base, array("ou", "description"), GL_SIZELIMIT | GL_SUBSEARCH);
432 }else{
433 $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
434 "department", $base, array("ou", "description"), GL_SIZELIMIT );
435 }
437 $this->departments= array();
439 /* Add current base to the list of available departments, but only if its naming attribute is 'ou' */
440 if(preg_match("/^ou=/",$base)){
441 $this->departments [ convert_department_dn($base) ] = ".";
442 }
444 foreach ($res as $key => $value){
446 /* Don't display base as entry on subsearch */
447 if(($value['dn'] == $base) && ($this->DivListDepartment->SubSearch)){
448 continue;
449 }
451 $cdn= convert_department_dn($value['dn']);
453 /* Append to dep list */
454 if(isset($value["description"][0])){
455 $this->departments[$cdn]= get_sub_department($cdn)." - [".$value["description"][0]."]";
456 }else{
457 $this->departments[$cdn]= get_sub_department($cdn);//$value["description"][0];
458 }
459 }
460 natcasesort ($this->departments);
461 reset ($this->departments);
462 }
464 function remove_from_parent()
465 {
466 $ldap= $this->config->get_ldap_link();
467 $ldap->cd ($this->dn);
468 $ldap->recursive_remove();
470 /* Optionally execute a command after we're done */
471 $this->postremove();
473 /* Delete references to object groups */
474 $ldap->cd ($this->config->current['BASE']);
475 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
476 while ($ldap->fetch()){
477 $og= new ogroup($this->config, $ldap->getDN());
478 unset($og->member[$this->dn]);
479 $og->save ();
480 }
482 }
485 function list_get_selected_items()
486 {
487 $ids = array();
488 foreach($_POST as $name => $value){
489 if(preg_match("/^item_selected_[a-z0-9\\/\=]*$/i",$name)){
490 $id = preg_replace("/^item_selected_/","",$name);
491 $ids[$id] = $id;
492 }
493 }
494 return($ids);
495 }
498 function remove_lock()
499 {
500 if (isset($this->dn)){
501 del_lock ($this->dn);
502 }
503 }
505 function save_object()
506 {
507 /* reload department */
508 $this->config->get_departments();
510 $this->config->make_idepartments();
511 $this->DivListDepartment->config= $this->config;
512 $this->DivListDepartment->save_object();
513 }
515 }
516 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
517 ?>