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 management
24 {
25 // Public
26 public $config = null;
27 public $ui = null;
29 // The plugin description
30 public $plugname = "unconfigured";
31 public $plIcon = "unconfigured";
32 public $plDescription = "unconfigured";
33 public $plHeadline = "unconfigured";
35 // The currently used object(s) (e.g. in edit, removal)
36 public $dn = ""; // this is public due to some compatibility problems with class plugin..
37 protected $dns = array();
39 // The last used object(s).
40 protected $last_dn = "";
41 protected $last_dns = array();
43 // The common places the displayed objects are stored in. (e.g. array("ou=groups,",".."))
44 protected $storagePoints = array();
46 // The tab definitions to use for the current object.
47 protected $tabClass = ""; // e.g. usertabs
48 protected $tabType = ""; // e.g. USERTABS
49 protected $aclPlugin = ""; // e.g. generic
50 protected $aclCategory = ""; // e.g. users
51 protected $objectName = ""; // e.g. users
53 // The opened object.
54 protected $tabObject = null;
55 protected $dialogObject = null;
57 // The last opened object.
58 protected $last_tabObject = null;
59 protected $last_dialogObject = null;
61 // Whether to display the apply button or not
62 protected $displayApplyBtn = FALSE;
64 // Whether to display a header or not.
65 protected $skipHeader = false;
67 // Whether to display a footer or not.
68 protected $skipFooter = false;
70 // Copy&Paste handler
71 protected $cpHandler = null;
73 // Indicates that we want to paste objects right now.
74 protected $cpPastingStarted = FALSE;
76 // The Snapshot handler class.
77 protected $snapHandler = null;
79 // The listing handlers
80 private $headpage = null;
81 private $filter = null;
83 // A list of configured actions/events
84 protected $actions = array();
86 // Attributes managed by this plugin, can be used in post events;
87 public $attributes = array();
89 function __construct(&$config,$ui,$plugname, $headpage)
90 {
91 $this->plugname = $plugname;
92 $this->headpage = $headpage;
93 $this->ui = $ui;
94 $this->config = $config;
96 if($this->cpHandler) $this->headpage->setCopyPasteHandler($this->cpHandler);
97 if($this->snapHandler) $this->headpage->setSnapshotHandler($this->snapHandler);
99 if(empty($this->plIcon)){
100 $this->plIcon = "plugins/".$plugname."/images/plugin.png";
101 }
103 // Register default actions
104 $this->registerAction("new", "newEntry");
105 $this->registerAction("edit", "editEntry");
106 $this->registerAction("apply", "applyChanges");
107 $this->registerAction("save", "saveChanges");
108 $this->registerAction("cancel", "cancelEdit");
109 $this->registerAction("cancelDelete", "cancelEdit");
110 $this->registerAction("remove", "removeEntryRequested");
111 $this->registerAction("removeConfirmed", "removeEntryConfirmed");
113 $this->registerAction("copy", "copyPasteHandler");
114 $this->registerAction("cut", "copyPasteHandler");
115 $this->registerAction("paste", "copyPasteHandler");
117 $this->registerAction("snapshot", "createSnapshotDialog");
118 $this->registerAction("restore", "restoreSnapshotDialog");
119 $this->registerAction("saveSnapshot","saveSnapshot");
120 $this->registerAction("restoreSnapshot","restoreSnapshot");
121 $this->registerAction("removeSnapshotConfirmed","removeSnapshotConfirmed");
122 $this->registerAction("cancelSnapshot","closeDialogs");
124 $this->registerAction("config-filter","editFilter");
125 $this->registerAction("saveFilter","saveFilter");
126 $this->registerAction("cancelFilter","cancelFilter");
128 // To temporay disable the filter caching UNcomment this line.
129 #session::global_un_set(get_class($this)."_filter");
130 }
132 /*! \brief Execute this plugin
133 * Handle actions/events, locking, snapshots, dialogs, tabs,...
134 */
135 function execute()
136 {
137 // Ensure that html posts and gets are kept even if we see a 'Entry islocked' dialog.
138 $vars = array('/^act$/','/^listing/','/^PID$/','/^FILTER_PID$/');
139 session::set('LOCK_VARS_TO_USE',$vars);
141 pathNavigator::registerPlugin($this);
143 /* Display the copy & paste dialog, if it is currently open */
144 $ret = $this->copyPasteHandler("",array());
145 if($ret){
146 return($this->getHeader().$ret);
147 }
149 // Update filter
150 if ($this->filter) {
151 $this->filter->update();
152 session::global_set(get_class($this)."_filter", $this->filter);
153 session::set('autocomplete', $this->filter);
154 }
156 // Handle actions (POSTs and GETs)
157 $str = $this->handleActions($this->detectPostActions());
158 if($str) return($this->getHeader().$str);
160 // Open single dialog objects
161 if(is_object($this->dialogObject)){
162 if(method_exists($this->dialogObject,'save_object')) $this->dialogObject->save_object();
163 if(method_exists($this->dialogObject,'execute')){
164 $display = $this->dialogObject->execute();
165 $display.= $this->_getTabFooter();
166 return($this->getHeader().$display);
167 }
168 }
170 // Display tab object.
171 if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
172 # $this->tabObject->save_object();
173 $display = $this->tabObject->execute();
174 $display.= $this->_getTabFooter();
175 return($this->getHeader().$display);
176 }
178 // Set current restore base for snapshot handling.
179 if(is_object($this->snapHandler)){
180 $bases = array();
181 foreach($this->storagePoints as $sp){
182 $bases[] = $sp.$this->headpage->getBase();
183 }
185 // No bases specified? Try base
186 if(!count($bases)) $bases[] = $this->headpage->getBase();
188 $this->snapHandler->setSnapshotBases($bases);
189 }
191 // Display list
192 return($this->renderList());
193 }
195 function editFilter()
196 {
197 $this->dialogObject = new userFilter($this->config,$this->getHeadpage());
198 }
200 function renderList()
201 {
202 $this->headpage->update();
203 $display = $this->headpage->render();
204 return($this->getHeader().$display);
205 }
207 function getHeadpage()
208 {
209 return($this->headpage);
210 }
212 function getFilter()
213 {
214 return($this->filter);
215 }
217 /*! \brief Generates the plugin header which is displayed whenever a tab object is
218 * opened.
219 */
220 protected function getHeader()
221 {
222 // We do not display any headers right now.
223 if(1 || $this->skipHeader) return("");
224 }
227 /*! \brief Generates the footer which is used whenever a tab object is
228 * displayed.
229 */
230 protected function _getTabFooter()
231 {
232 // Do not display tab footer for non tab objects
233 if(!($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug)){
234 return("");
235 }
237 // Check if there is a dialog opened - We don't need any buttons in this case.
238 if($this->tabObject->by_object[$this->tabObject->current]){
239 $current = $this->tabObject->by_object[$this->tabObject->current];
240 if(isset($current->dialog) && (is_object($current->dialog) || $current->dialog)){
241 return("");
242 }
243 }
245 // Skip footer if requested;
246 if($this->skipFooter) return("");
248 // In case an of locked entry, we may have opened a read-only tab.
249 $str = "";
250 if(isset($this->tabObject->read_only) && $this->tabObject->read_only == TRUE){
251 $str.= "
252 <p style=\"text-align:right\">
253 <button type=submit name=\"edit_cancel\">".msgPool::cancelButton()."</button>
254 </p>";
255 return($str);
256 }else{
258 // Display ok, (apply) and cancel buttons
259 $str.= "<p style=\"text-align:right\">\n";
260 $str.= "<button type=\"submit\" name=\"edit_finish\">".msgPool::okButton()."</button>\n";
261 $str.= " \n";
262 if($this->displayApplyBtn){
263 $str.= "<button type=\"submit\" name=\"edit_apply\">".msgPool::applyButton()."</button>\n";
264 $str.= " \n";
265 }
266 $str.= "<button type=\"submit\" name=\"edit_cancel\">".msgPool::cancelButton()."</button>\n";
267 $str.= "</p>";
268 }
269 return($str);
270 }
273 /*! \brief Initiates the removal for the given entries
274 * and displays a confirmation dialog.
275 *
276 * @param String 'action' The name of the action which was the used as trigger.
277 * @param Array 'target' A list of object dns, which should be affected by this method.
278 * @param Array 'all' A combination of both 'action' and 'target'.
279 */
280 protected function removeEntryRequested($action="",$target=array(),$all=array())
281 {
282 // Close dialogs and remove locks for currently handled dns
283 $this->cancelEdit();
285 $disallowed = array();
286 $this->dns = array();
288 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$target,"Entry removel requested!");
290 // Check permissons for each target
291 $h = $this->getHeadpage();
292 $oTypes = array_reverse($h->objectTypes);
293 foreach($target as $dn){
294 $entry = $h->getEntry($dn);
295 $obj = $h->getObjectType($oTypes, $entry['objectClass']);
296 $acl = $this->ui->get_permissions($dn, $obj['category']."/".$obj['class']);
297 if(preg_match("/d/",$acl)){
298 $this->dns[] = $dn;
299 }else{
300 $disallowed[] = $dn;
301 }
302 }
303 if(count($disallowed)){
304 msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
305 }
307 // We've at least one entry to delete.
308 if(count($this->dns)){
310 // check locks
311 if ($user= get_multiple_locks($this->dns)){
312 return(gen_locked_message($user,$this->dns));
313 }
315 // Add locks
316 $dns_names = array();
317 $types = array();
319 // Build list of object -labels
320 foreach($h->objectTypes as $type){
321 $map[$type['objectClass']]= $type['label'];
322 }
324 foreach($this->dns as $dn){
325 $tmp = $h->getType($dn);
326 if(isset($map[$tmp])){
327 $dns_names[LDAP::fix($dn)] = _($map[$tmp]);
328 }else{
329 $dns_names[] =LDAP::fix($dn);
330 }
331 }
332 add_lock ($this->dns, $this->ui->dn);
334 // Display confirmation dialog.
335 $smarty = get_smarty();
336 $smarty->assign("info", msgPool::deleteInfo($dns_names));
337 $smarty->assign("multiple", true);
338 return($smarty->fetch(get_template_path('removeEntries.tpl')));
339 }
340 }
343 /*! \brief Object removal was confirmed, now remove the requested entries.
344 *
345 * @param String 'action' The name of the action which was the used as trigger.
346 * @param Array 'target' A list of object dns, which should be affected by this method.
347 * @param Array 'all' A combination of both 'action' and 'target'.
348 */
349 function removeEntryConfirmed($action="",$target=array(),$all=array(),
350 $altTabClass="",$altTabType="", $altAclCategory="",$altAclPlugin="")
351 {
352 $tabType = $this->tabType;
353 $tabClass = $this->tabClass;
354 $aclCategory = $this->aclCategory;
355 $aclPlugin = $this->aclPlugin;
356 if(!empty($altTabClass)) $tabClass = $altTabClass;
357 if(!empty($altTabType)) $tabType = $altTabType;
358 if(!empty($altAclCategory)) $aclCategory = $altAclCategory;
359 if(!empty($altAclPlugin)) $aclPlugin = $altAclPlugin;
361 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$target,"Entry removel confirmed!");
363 // Check permissons for each target
364 $h = $this->getHeadpage();
365 $oTypes = array_reverse($h->objectTypes);
366 foreach($this->dns as $key => $dn){
367 $entry = $h->getEntry($dn);
368 $obj = $h->getObjectType($oTypes, $entry['objectClass']);
369 $acl = $this->ui->get_permissions($dn, $obj['category']."/".$obj['class']);
371 // Check permissions, are we allowed to remove this object?
372 if(preg_match("/d/",$acl)){
374 // Delete the object
375 $this->dn = $dn;
376 $this->tabObject= new $tabClass($this->config,$this->config->data['TABS'][$tabType], $this->dn,
377 $aclCategory, true, true);
378 $this->tabObject->set_acl_base($this->dn);
379 $this->tabObject->parent = &$this;
380 $this->tabObject->delete ();
382 // Remove the lock for the current object.
383 del_lock($this->dn);
384 } else {
385 msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
386 new log("security","groups/".get_class($this),$dn,array(),"Tried to trick deletion.");
387 }
388 }
390 // Cleanup
391 $this->remove_lock();
392 $this->closeDialogs();
393 }
396 /*! \brief Detects actions/events send by the ui
397 * and the corresponding targets.
398 */
399 function detectPostActions()
400 {
401 if(!is_object($this->headpage)){
402 trigger_error("No valid headpage given....!");
403 return(array());
404 }
405 $action= $this->headpage->getAction();
406 if(isset($_POST['edit_apply'])) $action['action'] = "apply";
407 if(isset($_POST['edit_finish'])) $action['action'] = "save";
408 if(isset($_POST['edit_cancel'])) $action['action'] = "cancel";
409 if(isset($_POST['delete_confirmed'])) $action['action'] = "removeConfirmed";
410 if(isset($_POST['delete_snapshot_confirm'])) $action['action'] = "removeSnapshotConfirmed";
411 if(isset($_POST['delete_cancel'])) $action['action'] = "cancelDelete";
412 if(isset($_POST['saveFilter'])) $action['action'] = "saveFilter";
413 if(isset($_POST['cancelFilter'])) $action['action'] = "cancelFilter";
415 // Detect Snapshot actions
416 if(isset($_POST['CreateSnapshot'])) $action['action'] = "saveSnapshot";
417 if(isset($_POST['CancelSnapshot'])) $action['action'] = "cancelSnapshot";
418 foreach($_POST as $name => $value){
419 $once =TRUE;
420 if(preg_match("/^RestoreSnapShot_/",$name) && $once){
421 $once = FALSE;
422 $entry = base64_decode(preg_replace("/^RestoreSnapShot_(.*)$/i","\\1",$name));
423 $action['action'] = "restoreSnapshot";
424 $action['targets'] = array($entry);
425 }
426 }
428 return($action);
429 }
432 /*! \brief Calls the registered method for a given action/event.
433 */
434 function handleActions($action)
435 {
436 // Start action
437 if(isset($this->actions[$action['action']])){
438 $func = $this->actions[$action['action']];
439 if(!isset($action['targets']))$action['targets']= array();
440 return($this->$func($action['action'],$action['targets'],$action));
441 }
442 }
445 /*! \brief Opens the snapshot creation dialog for the given target.
446 *
447 * @param String 'action' The name of the action which was the used as trigger.
448 * @param Array 'target' A list of object dns, which should be affected by this method.
449 * @param Array 'all' A combination of both 'action' and 'target'.
450 */
451 function createSnapshotDialog($action="",$target=array(),$all=array())
452 {
453 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$target,"Snaptshot creation initiated!");
455 foreach($target as $entry){
456 if(!empty($entry) && $this->ui->allow_snapshot_create($entry,$this->aclCategory)){
457 $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
458 $this->dialogObject->aclCategories = array($this->aclCategory);
459 $this->dialogObject->parent = &$this;
461 }else{
462 msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to create a snapshot for %s!"), bold($entry)),
463 ERROR_DIALOG);
464 }
465 }
466 }
469 /*! \brief Creates a snapshot new entry - This method is called when the somebody
470 * clicks 'save' in the "Create snapshot dialog" (see ::createSnapshotDialog).
471 *
472 * @param String 'action' The name of the action which was the used as trigger.
473 * @param Array 'target' A list of object dns, which should be affected by this method.
474 * @param Array 'all' A combination of both 'action' and 'target'.
475 */
476 function saveSnapshot($action="",$target=array(),$all=array())
477 {
478 if(!is_object($this->dialogObject)) return;
479 $this->dialogObject->save_object();
480 $msgs = $this->dialogObject->check();
481 if(count($msgs)){
482 foreach($msgs as $msg){
483 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
484 }
485 }else{
486 $this->dn = $this->dialogObject->dn;
487 $this->snapHandler->create_snapshot( $this->dn,$this->dialogObject->CurrentDescription);
488 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Snaptshot created!");
489 $this->closeDialogs();
490 }
491 }
494 /*! \brief Restores a snapshot object.
495 * The dn of the snapshot entry has to be given as ['target'] parameter.
496 *
497 * @param String 'action' The name of the action which was the used as trigger.
498 * @param Array 'target' A list of object dns, which should be affected by this method.
499 * @param Array 'all' A combination of both 'action' and 'target'.
500 */
501 function restoreSnapshot($action="",$target=array(),$all=array())
502 {
503 $entry = array_pop($target);
504 if(!empty($entry) && $this->ui->allow_snapshot_restore($entry,$this->aclCategory)){
505 $this->snapHandler->restore_snapshot($entry);
506 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Snaptshot restored!");
507 $this->closeDialogs();
508 }else{
509 msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s!"), bold($entry)),
510 ERROR_DIALOG);
511 }
512 }
515 /*! \brief Removes a snapshot object.
516 */
517 function removeSnapshotConfirmed($action="",$target=array(),$all=array())
518 {
519 $entry = $this->dialogObject->del_dn;
520 if(!empty($entry) && $this->ui->allow_snapshot_create($entry,$this->aclCategory)){
521 $this->snapHandler->remove_snapshot($entry);
522 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$entry,"Snaptshot removed!");
523 }else{
524 msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to remove a snapshot for %s!"), bold($entry)),
525 ERROR_DIALOG);
526 }
527 }
530 /*! \brief Displays the "Restore snapshot dialog" for a given target.
531 * If no target is specified, open the restore removed object
532 * dialog.
533 * @param String 'action' The name of the action which was the used as trigger.
534 * @param Array 'target' A list of object dns, which should be affected by this method.
535 * @param Array 'all' A combination of both 'action' and 'target'.
536 */
537 function restoreSnapshotDialog($action="",$target=array(),$all=array())
538 {
539 // Set current restore base for snapshot handling.
540 if(is_object($this->snapHandler)){
541 $bases = array();
542 foreach($this->storagePoints as $sp){
543 $bases[] = $sp.$this->headpage->getBase();
544 }
545 }
547 // No bases specified? Try base
548 if(!count($bases)) $bases[] = $this->headpage->getBase();
550 // No target, open the restore removed object dialog.
551 if(!count($target)){
552 $entry = $this->headpage->getBase();
553 if(!empty($entry) && $this->ui->allow_snapshot_restore($entry,$this->aclCategory)){
554 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$entry,"Snaptshot restoring initiated!");
555 $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
556 $this->dialogObject->set_snapshot_bases($bases);
557 $this->dialogObject->display_all_removed_objects = true;
558 $this->dialogObject->display_restore_dialog = true;
559 $this->dialogObject->parent = &$this;
560 }else{
561 msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s!"), bold($entry)),
562 ERROR_DIALOG);
563 }
564 }else{
566 // Display the restore points for a given object.
567 $entry = array_pop($target);
568 if(!empty($entry) && $this->ui->allow_snapshot_restore($entry,$this->aclCategory)){
569 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$entry,"Snaptshot restoring initiated!");
570 $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
571 $this->dialogObject->set_snapshot_bases($bases);
572 $this->dialogObject->display_restore_dialog = true;
573 $this->dialogObject->parent = &$this;
574 }else{
575 msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s!"), bold($entry)),
576 ERROR_DIALOG);
577 }
578 }
579 }
582 /*! \brief This method intiates the object creation.
583 *
584 * @param String 'action' The name of the action which was the used as trigger.
585 * @param Array 'target' A list of object dns, which should be affected by this method.
586 * @param Array 'all' A combination of both 'action' and 'target'.
587 */
588 function newEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
589 {
590 /* To handle mutliple object types overload this method.
591 * ...
592 * registerAction('newUser', 'newEntry');
593 * registerAction('newGroup','newEntry');
594 * ...
595 *
596 * function newEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory)
597 * {
598 * switch($action){
599 * case 'newUser' : {
600 * mangement::newEntry($action,$target,$all,"usertabs","USERTABS","users");
601 * }
602 * case 'newGroup' : {
603 * mangement::newEntry($action,$target,$all,"grouptabs","GROUPTABS","groups");
604 * }
605 * }
606 * }
607 **/
608 $tabType = $this->tabType;
609 $tabClass = $this->tabClass;
610 $aclCategory = $this->aclCategory;
611 if(!empty($altTabClass)) $tabClass = $altTabClass;
612 if(!empty($altTabType)) $tabType = $altTabType;
613 if(!empty($altAclCategory)) $aclCategory = $altAclCategory;
615 // Check locking & lock entry if required
616 $this->displayApplyBtn = FALSE;
617 $this->dn = "new";
618 $this->is_new = TRUE;
619 $this->is_single_edit = FALSE;
620 $this->is_multiple_edit = FALSE;
622 set_object_info($this->dn);
624 // Open object.
625 if(empty($tabClass) || empty($tabType)){
626 // No tab type defined
627 }else{
629 if (isset($this->config->data['TABS'][$tabType])) {
631 // Check if the base plugin is available - it is mostly responsible for object creation and removal.
632 $first = $this->config->data['TABS'][$tabType][0];
633 if(!class_available($first['CLASS'])){
634 msg_dialog::display(_("Internal error"),
635 sprintf(_("Cannot instantiate tabbed-plug-in, the base plugin (%s) is not available!"), $first['CLASS']),
636 ERROR_DIALOG);
637 }else{
638 $this->tabObject= new $tabClass($this->config,$this->config->data['TABS'][$tabType], $this->dn, $aclCategory);
639 $this->tabObject->set_acl_base($this->headpage->getBase());
640 $this->tabObject->parent = &$this;
641 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Create new entry initiated!");
642 }
643 } else {
644 msg_dialog::display(_("Error"), sprintf(_("No tab definition for %s found in configuration file: cannot create plugin instance!"), bold($tabType)), ERROR_DIALOG);
645 }
646 }
647 }
650 /*! \brief This method opens an existing object or a list of existing objects to be edited.
651 *
652 *
653 * @param String 'action' The name of the action which was the used as trigger.
654 * @param Array 'target' A list of object dns, which should be affected by this method.
655 * @param Array 'all' A combination of both 'action' and 'target'.
656 */
657 function editEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
658 {
659 /* To handle mutliple object types overload this method.
660 * ...
661 * registerAction('editUser', 'editEntry');
662 * registerAction('editGroup','editEntry');
663 * ...
664 *
665 * function editEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory)
666 * {
667 * switch($action){
668 * case 'editUser' : {
669 * mangement::editEntry($action,$target,$all,"usertabs","USERTABS","users");
670 * }
671 * case 'editGroup' : {
672 * mangement::editEntry($action,$target,$all,"grouptabs","GROUPTABS","groups");
673 * }
674 * }
675 * }
676 **/
678 // Do not create a new tabObject while there is already one opened,
679 // the user may have just pressed F5 to reload the page.
680 if(is_object($this->tabObject)){
681 return;
682 }
684 $tabType = $this->tabType;
685 $tabClass = $this->tabClass;
686 $aclCategory = $this->aclCategory;
687 if(!empty($altTabClass)) $tabClass = $altTabClass;
688 if(!empty($altTabType)) $tabType = $altTabType;
689 if(!empty($altAclCategory)) $aclCategory = $altAclCategory;
691 $this->displayApplyBtn = count($target) == 1;
693 // Single edit - we only got one object dn.
694 if(count($target) == 1){
695 $this->is_new = FALSE;
696 $this->is_single_edit = TRUE;
697 $this->is_multiple_edit = FALSE;
699 // Get the dn of the object and creates lock
700 $this->dn = array_pop($target);
701 set_object_info($this->dn);
702 $user = get_lock($this->dn);
703 if ($user != ""){
704 return(gen_locked_message ($user, array($this->dn),TRUE));
705 }
706 add_lock ($this->dn, $this->ui->dn);
708 // Open object.
709 if(empty($tabClass) || empty($tabType)){
710 trigger_error("We can't edit any object(s). 'tabClass' or 'tabType' is empty!");
711 }else{
713 $tab = $tabClass;
715 // Check if the base plugin is available - it is mostly responsible for object creation and removal.
716 $first = $this->config->data['TABS'][$tabType][0];
717 if(!class_available($first['CLASS'])){
718 msg_dialog::display(_("Internal error"),
719 sprintf(_("Cannot instantiate tabbed-plug-in, the base plugin (%s) is not available!"), $first['CLASS']),
720 ERROR_DIALOG);
721 }else{
722 $this->tabObject= new $tab($this->config,$this->config->data['TABS'][$tabType], $this->dn,$aclCategory);
723 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Edit entry initiated!");
724 $this->tabObject->set_acl_base($this->dn);
725 $this->tabObject->parent = &$this;
726 }
727 }
728 }else{
730 // We've multiple entries to edit.
731 $this->is_new = FALSE;
732 $this->is_singel_edit = FALSE;
733 $this->is_multiple_edit = TRUE;
735 // Open multiple edit handler.
736 if(empty($tabClass) || empty($tabType)){
737 trigger_error("We can't edit any object(s). 'tabClass' or 'tabType' is empty!");
738 }else{
739 $this->dns = $target;
740 $tmp = new multi_plug($this->config,$tabClass,$this->config->data['TABS'][$tabType],
741 $this->dns,$this->headpage->getBase(),$aclCategory);
743 // Check for locked entries
744 if ($tmp->entries_locked()){
745 return($tmp->display_lock_message());
746 }
748 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Edit entry initiated!");
750 // Now lock entries.
751 if($tmp->multiple_available()){
752 $tmp->lock_entries($this->ui->dn);
753 $this->tabObject = $tmp;
754 set_object_info($this->tabObject->get_object_info());
755 }
756 }
757 }
758 }
761 /*! \brief Close filter dialog
762 */
763 protected function cancelFilter()
764 {
765 if($this->dialogObject instanceOf userFilter){
766 $this->remove_lock();
767 $this->closeDialogs();
768 }
769 }
772 /*! \brief Save filter modifcations.
773 */
774 protected function saveFilter()
775 {
776 if($this->dialogObject instanceOf userFilter){
777 $msgs = $this->dialogObject->check();
778 if(count($msgs)){
779 msg_dialog::displayChecks($msgs);
780 return("");
781 }else{
782 $this->dialogObject->save();
783 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Entry saved!");
784 $this->remove_lock();
785 $this->closeDialogs();
787 // Ask filter to reload information
788 $this->filter->reloadFilters();
789 }
790 }
791 }
794 /*! \brief Save object modifications and closes dialogs (returns to object listing).
795 * - Calls '::check' to validate the given input.
796 * - Calls '::save' to save back object modifications (e.g. to ldap).
797 * - Calls '::remove_locks' to remove eventually created locks.
798 * - Calls '::closeDialogs' to return to the object listing.
799 */
800 protected function saveChanges()
801 {
802 if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
803 $this->tabObject->save_object();
804 $msgs = $this->tabObject->check();
805 if(count($msgs)){
806 msg_dialog::displayChecks($msgs);
807 return("");
808 }else{
809 $this->tabObject->save();
810 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Entry saved!");
811 $this->remove_lock();
812 $this->closeDialogs();
813 }
814 }elseif($this->dialogObject instanceOf plugin){
815 $this->dialogObject->save_object();
816 $msgs = $this->dialogObject->check();
817 if(count($msgs)){
818 msg_dialog::displayChecks($msgs);
819 return("");
820 }else{
821 $this->dialogObject->save();
822 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Entry saved!");
823 $this->remove_lock();
824 $this->closeDialogs();
825 }
826 }
827 }
830 /*! \brief Save object modifications and keep dialogs opened.
831 * - Calls '::check' to validate the given input.
832 * - Calls '::save' to save back object modifications (e.g. to ldap).
833 */
834 protected function applyChanges()
835 {
836 if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
837 $this->tabObject->save_object();
838 $msgs = $this->tabObject->check();
839 if(count($msgs)){
840 msg_dialog::displayChecks($msgs);
841 return("");
842 }else{
843 $this->tabObject->save();
844 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Modifications applied!");
845 $this->tabObject->re_init();
846 }
847 }
848 }
851 /*! \brief This method closes dialogs
852 * and cleans up the cached object info and the ui.
853 */
854 protected function closeDialogs()
855 {
856 $this->last_dn = $this->dn;
857 $this->last_dns = $this->dns;
858 $this->last_tabObject = $this->tabObject;
859 $this->last_dialogObject = $this->dialogObject;
860 $this->dn = "";
861 $this->dns = array();
862 $this->tabObject = null;
863 $this->dialogObject = null;
864 $this->skipFooter = FALSE;
865 set_object_info();
866 }
869 /*! \brief Editing an object was caneled.
870 * Close dialogs/tabs and remove locks.
871 */
872 protected function cancelEdit()
873 {
874 $this->remove_lock();
875 $this->closeDialogs();
876 }
879 /*! \brief Every click in the list user interface sends an event
880 * here can we connect those events to a method.
881 * eg. ::registerEvent('new','createUser')
882 * When the action/event new is send, the method 'createUser'
883 * will be called.
884 */
885 function registerAction($action,$target)
886 {
887 $this->actions[$action] = $target;
888 }
891 /*! \brief Removes ldap object locks created by this class.
892 * Whenever an object is edited, we create locks to avoid
893 * concurrent modifications.
894 * This locks will automatically removed here.
895 */
896 function remove_lock()
897 {
898 if(!empty($this->dn) && $this->dn != "new"){
899 del_lock($this->dn);
900 }
901 if(count($this->dns)){
902 del_lock($this->dns);
903 }
904 }
907 /*! \brief This method is used to queue and process copy&paste actions.
908 * Allows to copy, cut and paste mutliple entries at once.
909 * @param String 'action' The name of the action which was the used as trigger.
910 * @param Array 'target' A list of object dns, which should be affected by this method.
911 * @param Array 'all' A combination of both 'action' and 'target'.
912 */
913 function copyPasteHandler($action="",$target=array(),$all=array(),
914 $altTabClass ="", $altTabType = "", $altAclCategory="",$altAclPlugin="")
915 {
916 // Return without any actions while copy&paste handler is disabled.
917 if(!is_object($this->cpHandler)) return("");
919 $tabType = $this->tabType;
920 $tabClass = $this->tabClass;
921 $aclCategory = $this->aclCategory;
922 $aclPlugin = $this->aclPlugin;
923 if(!empty($altTabClass)) $tabClass = $altTabClass;
924 if(!empty($altTabType)) $tabType = $altTabType;
925 if(!empty($altAclCategory)) $aclCategory = $altAclCategory;
926 if(!empty($altAclPlugin)) $aclPlugin = $altAclPlugin;
928 // Save user input
929 $this->cpHandler->save_object();
931 // Add entries to queue
932 if($action == "copy" || $action == "cut"){
933 $this->cpHandler->cleanup_queue();
934 foreach($target as $dn){
935 if($action == "copy" && $this->ui->is_copyable($dn,$aclCategory,$aclPlugin)){
936 $this->cpHandler->add_to_queue($dn,"copy",$tabClass,$tabType,$aclCategory,$this);
937 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$dn,"Entry copied!");
938 }
939 if($action == "cut" && $this->ui->is_cutable($dn,$aclCategory,$aclPlugin)){
940 $this->cpHandler->add_to_queue($dn,"cut",$tabClass,$tabType,$aclCategory,$this);
941 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$dn,"Entry cutted!");
942 }
943 }
944 }
946 // Initiate pasting
947 if($action == "paste"){
948 $this->cpPastingStarted = TRUE;
949 }
951 // Display any c&p dialogs, eg. object modifications required before pasting.
952 if($this->cpPastingStarted && $this->cpHandler->entries_queued()){
953 $this->cpHandler->SetVar("base",$this->headpage->getBase());
954 $data = $this->cpHandler->execute();
955 if(!empty($data)){
956 return($data);
957 }
958 }
960 // Automatically disable pasting process since there is no entry left to paste.
961 if(!$this->cpHandler->entries_queued()){
962 $this->cpPastingStarted = FALSE;
963 }
964 return("");
965 }
968 function setFilter($str) {
969 $this->filter = $str;
970 }
973 function postcreate() {
974 $this->handle_post_events('add');
975 }
976 function postmodify(){
977 $this->handle_post_events('modify');
978 }
979 function postremove(){
980 $this->handle_post_events('remove');
981 }
983 function is_modal_dialog()
984 {
985 return(is_object($this->tabObject) || is_object($this->dialogObject));
986 }
989 /*! \brief Forward command execution request
990 * to the correct method.
991 */
992 function handle_post_events($mode, $addAttrs= array())
993 {
994 if(!in_array($mode, array('add','remove','modify'))){
995 trigger_error(sprintf("Invalid post event type given %s! Valid types are [add,modify,remove].", bold($mode)));
996 return;
997 }
998 switch ($mode){
999 case "add":
1000 plugin::callHook($this,"POSTCREATE", $addAttrs);
1001 break;
1003 case "modify":
1004 plugin::callHook($this,"POSTMODIFY", $addAttrs);
1005 break;
1007 case "remove":
1008 plugin::callHook($this,"POSTREMOVE", $addAttrs);
1009 break;
1010 }
1011 }
1012 }
1014 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1015 ?>