Code

b63a7019841051b0f6468b280d7f0dfdfa0a35f5
[gosa.git] / gosa-core / include / class_management.inc
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("cancelSnapshot","closeDialogs");
123     $this->registerAction("config-filter","editFilter");
124     $this->registerAction("saveFilter","saveFilter");
126     // To temporay disable the filter caching UNcomment this line.
127     #session::global_un_set(get_class($this)."_filter");
128   }
130   /*! \brief  Execute this plugin
131    *          Handle actions/events, locking, snapshots, dialogs, tabs,...
132    */
133   function execute()
134   {
135     // Ensure that html posts and gets are kept even if we see a 'Entry islocked' dialog.
136     $vars = array('/^act$/','/^listing/','/^PID$/','/^FILTER_PID$/');
137     session::set('LOCK_VARS_TO_USE',$vars);
139     pathNavigator::registerPlugin($this);
141     /* Display the copy & paste dialog, if it is currently open */
142     $ret = $this->copyPasteHandler("",array());
143     if($ret){
144       return($this->getHeader().$ret);
145     }
147     // Update filter
148     if ($this->filter) {
149       $this->filter->update();
150       session::global_set(get_class($this)."_filter", $this->filter);
151       session::set('autocomplete', $this->filter);
152     }
154     // Handle actions (POSTs and GETs)
155     $str = $this->handleActions($this->detectPostActions());
156     if($str) return($this->getHeader().$str);
158     // Open single dialog objects
159     if(is_object($this->dialogObject)){
160       if(method_exists($this->dialogObject,'save_object')) $this->dialogObject->save_object(); 
161       if(method_exists($this->dialogObject,'execute')){
162         $display = $this->dialogObject->execute(); 
163         $display.= $this->_getTabFooter();
164         return($this->getHeader().$display);
165       } 
166     }
168     // Display tab object.
169     if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
170 #      $this->tabObject->save_object();
171       $display = $this->tabObject->execute();
172       $display.= $this->_getTabFooter();
173       return($this->getHeader().$display);
174     }
176     // Set current restore base for snapshot handling.
177     if(is_object($this->snapHandler)){
178       $bases = array();
179       foreach($this->storagePoints as $sp){
180         $bases[] = $sp.$this->headpage->getBase();
181       }
183       // No bases specified? Try base  
184       if(!count($bases)) $bases[] = $this->headpage->getBase();
186       $this->snapHandler->setSnapshotBases($bases);
187     }
188   
189     // Display list
190     return($this->renderList());
191   }
192  
193   function editFilter()
194   {
195     $this->dialogObject = new userFilter($this->config,$this->getHeadpage());
196   }
197  
198   function renderList()
199   {
200     $this->headpage->update();
201     $display = $this->headpage->render();
202     return($this->getHeader().$display);
203   }
205   function getHeadpage()
206   {
207     return($this->headpage);
208   }
210   function getFilter()
211   {
212     return($this->filter);
213   }
215   /*! \brief  Generates the plugin header which is displayed whenever a tab object is 
216    *           opened.
217    */
218   protected function getHeader()
219   {
220     // We do not display any headers right now.
221     if(1 || $this->skipHeader) return("");
222   }
225   /*! \brief  Generates the footer which is used whenever a tab object is 
226    *           displayed.
227    */
228   protected function _getTabFooter()
229   {
230     // Do not display tab footer for non tab objects 
231     if(!($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug)){
232       return("");
233     }
235     // Check if there is a dialog opened - We don't need any buttons in this case. 
236     if($this->tabObject->by_object[$this->tabObject->current]){
237       $current = $this->tabObject->by_object[$this->tabObject->current];  
238       if(isset($current->dialog) && (is_object($current->dialog) || $current->dialog)){
239         return("");
240       }
241     }
243     // Skip footer if requested;
244     if($this->skipFooter) return("");
246     // In case an of locked entry, we may have opened a read-only tab.
247     $str = "";
248     if(isset($this->tabObject->read_only) && $this->tabObject->read_only == TRUE){
249       $str.= "
250         <p style=\"text-align:right\">
251           <button type=submit name=\"edit_cancel\">".msgPool::cancelButton()."</button>
252         </p>";
253       return($str);
254     }else{
256       // Display ok, (apply) and cancel buttons
257       $str.= "<p style=\"text-align:right\">\n";
258       $str.= "<button type=\"submit\" name=\"edit_finish\" style=\"width:80px\">".msgPool::okButton()."</button>\n";
259       $str.= "&nbsp;\n";
260       if($this->displayApplyBtn){
261         $str.= "<button type=\"submit\" name=\"edit_apply\">".msgPool::applyButton()."</button>\n";
262         $str.= "&nbsp;\n";
263       }
264       $str.= "<button type=\"submit\" name=\"edit_cancel\">".msgPool::cancelButton()."</button>\n";
265       $str.= "</p>";
266     }
267     return($str);
268   }
271   /*! \brief  Initiates the removal for the given entries
272    *           and displays a confirmation dialog.
273    *      
274    *  @param  String  'action'  The name of the action which was the used as trigger.
275    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
276    *  @param  Array   'all'     A combination of both 'action' and 'target'.
277    */
278   protected function removeEntryRequested($action="",$target=array(),$all=array())
279   {
280     // Close dialogs and remove locks for currently handled dns
281     $this->cancelEdit();
282   
283     $disallowed = array();
284     $this->dns = array();
286     @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$target,"Entry removel requested!");
288     // Check permissons for each target 
289     foreach($target as $dn){
290       $acl = $this->ui->get_permissions($dn, $this->aclCategory."/".$this->aclPlugin);
291       if(preg_match("/d/",$acl)){
292         $this->dns[] = $dn;
293       }else{
294         $disallowed[] = $dn;
295       }
296     }
297     if(count($disallowed)){
298       msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
299     }
301     // We've at least one entry to delete.
302     if(count($this->dns)){
304       // check locks
305       if ($user= get_multiple_locks($this->dns)){
306         return(gen_locked_message($user,$this->dns));
307       }
309       // Add locks
310       $dns_names = array();
311       $types = array();
312       $h = $this->getHeadpage();
314       // Build list of object -labels
315       foreach($h->objectTypes as $type){
316         $map[$type['objectClass']]= $type['label'];
317       }
319       foreach($this->dns as $dn){
320         $tmp = $h->getType($dn);
321         if(isset($map[$tmp])){
322           $dns_names[LDAP::fix($dn)] = _($map[$tmp]);
323         }else{
324           $dns_names[] =LDAP::fix($dn);
325         }
326       }
327       add_lock ($this->dns, $this->ui->dn);
329       // Display confirmation dialog.
330       $smarty = get_smarty();
331       $smarty->assign("info", msgPool::deleteInfo($dns_names));
332       $smarty->assign("multiple", true);
333       return($smarty->fetch(get_template_path('removeEntries.tpl')));
334     }
335   }  
338   /*! \brief  Object removal was confirmed, now remove the requested entries. 
339    *      
340    *  @param  String  'action'  The name of the action which was the used as trigger.
341    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
342    *  @param  Array   'all'     A combination of both 'action' and 'target'.
343    */
344   function removeEntryConfirmed($action="",$target=array(),$all=array(),
345       $altTabClass="",$altTabType="",  $altAclCategory="",$altAclPlugin="")
346   {
347     $tabType = $this->tabType;
348     $tabClass = $this->tabClass;
349     $aclCategory = $this->aclCategory;
350     $aclPlugin = $this->aclPlugin;
351     if(!empty($altTabClass)) $tabClass = $altTabClass;
352     if(!empty($altTabType)) $tabType = $altTabType;
353     if(!empty($altAclCategory)) $aclCategory = $altAclCategory;
354     if(!empty($altAclPlugin)) $aclPlugin = $altAclPlugin;
356     @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$target,"Entry removel confirmed!");
358     foreach($this->dns as $key => $dn){
360       // Check permissions, are we allowed to remove this object? 
361       $acl = $this->ui->get_permissions($dn, $aclCategory."/".$aclPlugin);
362       if(preg_match("/d/",$acl)){
364         // Delete the object
365         $this->dn = $dn;
366         $this->tabObject= new $tabClass($this->config,$this->config->data['TABS'][$tabType], $this->dn, $aclCategory, true, true);
367         $this->tabObject->set_acl_base($this->dn);
368         $this->tabObject->parent = &$this;
369         $this->tabObject->delete ();
371         // Remove the lock for the current object.
372         del_lock($this->dn);        
373       } else {
374         msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
375         new log("security","groups/".get_class($this),$dn,array(),"Tried to trick deletion.");
376       }
377     }
379     // Cleanup
380     $this->remove_lock();
381     $this->closeDialogs();
382   }
385   /*! \brief  Detects actions/events send by the ui
386    *           and the corresponding targets.
387    */
388   function detectPostActions()
389   {
390     if(!is_object($this->headpage)){
391       trigger_error("No valid headpage given....!");
392       return(array());
393     }
394     $action= $this->headpage->getAction();
395     if(isset($_POST['edit_apply']))  $action['action'] = "apply";    
396     if(isset($_POST['edit_finish'])) $action['action'] = "save";    
397     if(isset($_POST['edit_cancel'])) $action['action'] = "cancel";    
398     if(isset($_POST['delete_confirmed'])) $action['action'] = "removeConfirmed";   
399     if(isset($_POST['delete_cancel'])) $action['action'] = "cancelDelete";   
400     if(isset($_POST['saveFilter'])) $action['action'] = "saveFilter";   
401     if(isset($_POST['cancelFilter'])) $action['action'] = "cancel";   
403     // Detect Snapshot actions
404     if(isset($_POST['CreateSnapshot'])) $action['action'] = "saveSnapshot";   
405     if(isset($_POST['CancelSnapshot'])) $action['action'] = "cancelSnapshot";   
406     foreach($_POST as $name => $value){
407       $once =TRUE;
408       if(preg_match("/^RestoreSnapShot_/",$name) && $once){
409         $once = FALSE;
410         $entry = base64_decode(preg_replace("/^RestoreSnapShot_([^_]*)_[xy]$/i","\\1",$name));
411         $action['action'] = "restoreSnapshot";
412         $action['targets'] = array($entry);
413       }
414     }
416     return($action);
417   }
420   /*! \brief  Calls the registered method for a given action/event.
421    */
422   function handleActions($action)
423   {
424     // Start action  
425     if(isset($this->actions[$action['action']])){
426       $func = $this->actions[$action['action']];
427       if(!isset($action['targets']))$action['targets']= array(); 
428       return($this->$func($action['action'],$action['targets'],$action));
429     }
430   } 
433   /*! \brief  Opens the snapshot creation dialog for the given target.
434    *      
435    *  @param  String  'action'  The name of the action which was the used as trigger.
436    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
437    *  @param  Array   'all'     A combination of both 'action' and 'target'.
438    */
439   function createSnapshotDialog($action="",$target=array(),$all=array())
440   {
441     @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$target,"Snaptshot creation initiated!");
443     foreach($target as $entry){
444       if(!empty($entry) && $this->ui->allow_snapshot_create($entry,$this->aclCategory)){
445         $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
446         $this->dialogObject->aclCategories = array($this->aclCategory);
447         $this->dialogObject->parent = &$this;
449       }else{
450         msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to create a snapshot for %s."),$entry),
451             ERROR_DIALOG);
452       }
453     }
454   }
457   /*! \brief  Creates a snapshot new entry - This method is called when the somebody
458    *           clicks 'save' in the "Create snapshot dialog" (see ::createSnapshotDialog).
459    *      
460    *  @param  String  'action'  The name of the action which was the used as trigger.
461    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
462    *  @param  Array   'all'     A combination of both 'action' and 'target'.
463    */
464   function saveSnapshot($action="",$target=array(),$all=array())
465   { 
466     if(!is_object($this->dialogObject)) return;
467     $this->dialogObject->save_object();
468     $msgs = $this->dialogObject->check();
469     if(count($msgs)){
470       foreach($msgs as $msg){
471         msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
472       }
473     }else{
474       $this->dn =  $this->dialogObject->dn;
475       $this->snapHandler->create_snapshot( $this->dn,$this->dialogObject->CurrentDescription);
476       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Snaptshot created!");
477       $this->closeDialogs();
478     }
479   }
482   /*! \brief  Restores a snapshot object.
483    *          The dn of the snapshot entry has to be given as ['target'] parameter.  
484    *      
485    *  @param  String  'action'  The name of the action which was the used as trigger.
486    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
487    *  @param  Array   'all'     A combination of both 'action' and 'target'.
488    */
489   function restoreSnapshot($action="",$target=array(),$all=array())
490   {
491     $entry = array_pop($target);
492     if(!empty($entry) && $this->ui->allow_snapshot_restore($entry,$this->aclCategory)){
493       $this->snapHandler->restore_snapshot($entry);
494       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Snaptshot restored!");
495       $this->closeDialogs();
496     }else{
497       msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s."),$entry),
498           ERROR_DIALOG);
499     }
500   }
503   /*! \brief  Displays the "Restore snapshot dialog" for a given target. 
504    *          If no target is specified, open the restore removed object 
505    *           dialog.
506    *  @param  String  'action'  The name of the action which was the used as trigger.
507    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
508    *  @param  Array   'all'     A combination of both 'action' and 'target'.
509    */
510   function restoreSnapshotDialog($action="",$target=array(),$all=array())
511   {
512     // Set current restore base for snapshot handling.
513     if(is_object($this->snapHandler)){
514       $bases = array();
515       foreach($this->storagePoints as $sp){
516         $bases[] = $sp.$this->headpage->getBase();
517       }
518     }
520     // No bases specified? Try base  
521     if(!count($bases)) $bases[] = $this->headpage->getBase();
523     // No target, open the restore removed object dialog.
524     if(!count($target)){ 
525       $entry = $this->headpage->getBase();
526       if(!empty($entry) && $this->ui->allow_snapshot_restore($entry,$this->aclCategory)){
527         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$entry,"Snaptshot restoring initiated!");
528         $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
529         $this->dialogObject->set_snapshot_bases($bases);
530         $this->dialogObject->display_all_removed_objects = true;
531         $this->dialogObject->display_restore_dialog = true;
532         $this->dialogObject->parent = &$this;
533       }else{
534         msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s."),$entry),
535             ERROR_DIALOG);
536       } 
537     }else{
539       // Display the restore points for a given object.
540       $entry = array_pop($target);
541       if(!empty($entry) && $this->ui->allow_snapshot_restore($entry,$this->aclCategory)){
542         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$entry,"Snaptshot restoring initiated!");
543         $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
544         $this->dialogObject->set_snapshot_bases($bases);
545         $this->dialogObject->display_restore_dialog = true;
546         $this->dialogObject->parent = &$this;
547       }else{
548         msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s."),$entry),
549             ERROR_DIALOG);
550       } 
551     }
552   }
555   /*! \brief  This method intiates the object creation.
556    *          
557    *  @param  String  'action'  The name of the action which was the used as trigger.
558    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
559    *  @param  Array   'all'     A combination of both 'action' and 'target'.
560    */
561   function newEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
562   {
563     /* To handle mutliple object types overload this method.
564      * ...
565      *   registerAction('newUser', 'newEntry');
566      *   registerAction('newGroup','newEntry');
567      * ... 
568      * 
569      * function newEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory)
570      * {
571      *   switch($action){
572      *     case 'newUser' : {
573      *       mangement::newEntry($action,$target,$all,"usertabs","USERTABS","users");
574      *     }
575      *     case 'newGroup' : {
576      *       mangement::newEntry($action,$target,$all,"grouptabs","GROUPTABS","groups");
577      *     }
578      *   }
579      * }
580      **/ 
581     $tabType = $this->tabType;
582     $tabClass = $this->tabClass;
583     $aclCategory = $this->aclCategory;
584     if(!empty($altTabClass)) $tabClass = $altTabClass;
585     if(!empty($altTabType)) $tabType = $altTabType;
586     if(!empty($altAclCategory)) $aclCategory = $altAclCategory;
588     // Check locking & lock entry if required 
589     $this->displayApplyBtn = FALSE;
590     $this->dn = "new";
591     $this->is_new = TRUE;
592     $this->is_single_edit = FALSE;
593     $this->is_multiple_edit = FALSE;
595     set_object_info($this->dn);
597     // Open object.
598     if(empty($tabClass) || empty($tabType)){
599       // No tab type defined
600     }else{
601       if (isset($this->config->data['TABS'][$tabType])) {
602         $this->tabObject= new $tabClass($this->config,$this->config->data['TABS'][$tabType], $this->dn, $aclCategory);
603         $this->tabObject->set_acl_base($this->headpage->getBase());
604         $this->tabObject->parent = &$this;
605         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Create new entry initiated!");
606       } else {
607         msg_dialog::display(_("Error"), sprintf(_("No tab declaration for '%s' found in your configuration file. Cannot create plugin instance!"), $tabType), ERROR_DIALOG);
608       }
609     }
610   }
613   /*! \brief  This method opens an existing object or a list of existing objects to be edited. 
614    *                  
615    * 
616    *  @param  String  'action'  The name of the action which was the used as trigger.
617    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
618    *  @param  Array   'all'     A combination of both 'action' and 'target'.
619    */
620   function editEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
621   {
622     /* To handle mutliple object types overload this method.
623      * ...
624      *   registerAction('editUser', 'editEntry');
625      *   registerAction('editGroup','editEntry');
626      * ... 
627      * 
628      * function editEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory)
629      * {
630      *   switch($action){
631      *     case 'editUser' : {
632      *       mangement::editEntry($action,$target,$all,"usertabs","USERTABS","users");
633      *     }
634      *     case 'editGroup' : {
635      *       mangement::editEntry($action,$target,$all,"grouptabs","GROUPTABS","groups");
636      *     }
637      *   }
638      * }
639      **/
641     // Do not create a new tabObject while there is already one opened,
642     //  the user may have just pressed F5 to reload the page.
643     if(is_object($this->tabObject)){
644       return;
645     }
646  
647     $tabType = $this->tabType;
648     $tabClass = $this->tabClass;
649     $aclCategory = $this->aclCategory;
650     if(!empty($altTabClass)) $tabClass = $altTabClass;
651     if(!empty($altTabType)) $tabType = $altTabType;
652     if(!empty($altAclCategory)) $aclCategory = $altAclCategory;
654     // Single edit - we only got one object dn.
655     if(count($target) == 1){
656       $this->displayApplyBtn = TRUE;
657       $this->is_new = FALSE;
658       $this->is_single_edit = TRUE;
659       $this->is_multiple_edit = FALSE;
661       // Get the dn of the object and creates lock
662       $this->dn = array_pop($target);
663       set_object_info($this->dn);
664       $user = get_lock($this->dn);
665       if ($user != ""){
666         return(gen_locked_message ($user, $this->dn,TRUE));
667       }
668       add_lock ($this->dn, $this->ui->dn);
670       // Open object.
671       if(empty($tabClass) || empty($tabType)){
672         trigger_error("We can't edit any object(s). 'tabClass' or 'tabType' is empty!");
673       }else{
674         $tab = $tabClass;
675         $this->tabObject= new $tab($this->config,$this->config->data['TABS'][$tabType], $this->dn,$aclCategory);
676         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Edit entry initiated!");
677         $this->tabObject->set_acl_base($this->dn);
678         $this->tabObject->parent = &$this;
679       }
680     }else{
682       // We've multiple entries to edit.
683       $this->is_new = FALSE;
684       $this->is_singel_edit = FALSE;
685       $this->is_multiple_edit = TRUE;
687       // Open multiple edit handler.
688       if(empty($tabClass) || empty($tabType)){
689         trigger_error("We can't edit any object(s). 'tabClass' or 'tabType' is empty!");
690       }else{
691         $this->dns = $target;
692         $tmp = new multi_plug($this->config,$tabClass,$this->config->data['TABS'][$tabType],
693             $this->dns,$this->headpage->getBase(),$aclCategory);
695         // Check for locked entries
696         if ($tmp->entries_locked()){
697           return($tmp->display_lock_message());
698         }
700         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Edit entry initiated!");
702         // Now lock entries.
703         if($tmp->multiple_available()){
704           $tmp->lock_entries($this->ui->dn);
705           $this->tabObject = $tmp;
706           set_object_info($this->tabObject->get_object_info());
707         }
708       }
709     }
710   }
713   /*! \brief  Save filter modifcations.
714    */
715   protected function saveFilter()
716   {
717     if($this->dialogObject instanceOf userFilter){
718       $msgs = $this->dialogObject->check();
719       if(count($msgs)){
720         msg_dialog::displayChecks($msgs); 
721         return("");
722       }else{
723         $this->dialogObject->save();
724         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Entry saved!");
725         $this->remove_lock();
726         $this->closeDialogs();
728         // Ask filter to reload information
729         $this->filter->reloadFilters();
730       }
731     }
732   }
735   /*! \brief  Save object modifications and closes dialogs (returns to object listing).
736    *          - Calls '::check' to validate the given input.
737    *          - Calls '::save' to save back object modifications (e.g. to ldap).
738    *          - Calls '::remove_locks' to remove eventually created locks.
739    *          - Calls '::closeDialogs' to return to the object listing.
740    */
741   protected function saveChanges()
742   {
743     if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
744       $this->tabObject->save_object();
745       $msgs = $this->tabObject->check();
746       if(count($msgs)){
747         msg_dialog::displayChecks($msgs); 
748         return("");
749       }else{
750         $this->tabObject->save();
751         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Entry saved!");
752         $this->remove_lock();
753         $this->closeDialogs();
754       }
755     }elseif($this->dialogObject instanceOf plugin){
756       $this->dialogObject->save_object();
757       $msgs = $this->dialogObject->check();
758       if(count($msgs)){
759         msg_dialog::displayChecks($msgs); 
760         return("");
761       }else{
762         $this->dialogObject->save();
763         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Entry saved!");
764         $this->remove_lock();
765         $this->closeDialogs();
766       }
767     }
768   }
771   /*! \brief  Save object modifications and keep dialogs opened. 
772    *          - Calls '::check' to validate the given input.
773    *          - Calls '::save' to save back object modifications (e.g. to ldap).
774    */
775   protected function applyChanges()
776   {
777     if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
778       $this->tabObject->save_object();
779       $msgs = $this->tabObject->check();
780       if(count($msgs)){
781         msg_dialog::displayChecks($msgs); 
782         return("");
783       }else{
784         $this->tabObject->save();
785         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Modifications applied!");
786         $this->tabObject->re_init();
787       }
788     }
789   }
792   /*! \brief  This method closes dialogs
793    *           and cleans up the cached object info and the ui.
794    */
795   protected function closeDialogs()
796   {
797     $this->last_dn = $this->dn;
798     $this->last_dns = $this->dns;
799     $this->last_tabObject = $this->tabObject;
800     $this->last_dialogObject = $this->dialogObject;
801     $this->dn = "";
802     $this->dns = array();
803     $this->tabObject = null;
804     $this->dialogObject = null;
805     $this->skipFooter = FALSE;
806     set_object_info();
807   }
810   /*! \brief  Editing an object was caneled. 
811    *          Close dialogs/tabs and remove locks.
812    */
813   protected function cancelEdit()
814   {
815     $this->remove_lock();
816     $this->closeDialogs();
817   }
820   /*! \brief  Every click in the list user interface sends an event
821    *           here can we connect those events to a method. 
822    *          eg.  ::registerEvent('new','createUser')
823    *          When the action/event new is send, the method 'createUser' 
824    *           will be called.
825    */
826   function registerAction($action,$target)
827   {
828     $this->actions[$action] = $target;
829   }
832   /*! \brief  Removes ldap object locks created by this class.
833    *          Whenever an object is edited, we create locks to avoid 
834    *           concurrent modifications.
835    *          This locks will automatically removed here.
836    */
837   function remove_lock()
838   {
839     if(!empty($this->dn) && $this->dn != "new"){
840       del_lock($this->dn);
841     }
842     if(count($this->dns)){
843       del_lock($this->dns);
844     }
845   }
848   /*! \brief  This method is used to queue and process copy&paste actions. 
849    *          Allows to copy, cut and paste mutliple entries at once.
850    *  @param  String  'action'  The name of the action which was the used as trigger.
851    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
852    *  @param  Array   'all'     A combination of both 'action' and 'target'.
853    */
854   function copyPasteHandler($action="",$target=array(),$all=array(), 
855       $altTabClass ="", $altTabType = "", $altAclCategory="",$altAclPlugin="")
856   {
857     // Return without any actions while copy&paste handler is disabled.
858     if(!is_object($this->cpHandler))  return("");
860     $tabType = $this->tabType;
861     $tabClass = $this->tabClass;
862     $aclCategory = $this->aclCategory;
863     $aclPlugin = $this->aclPlugin;
864     if(!empty($altTabClass)) $tabClass = $altTabClass;
865     if(!empty($altTabType)) $tabType = $altTabType;
866     if(!empty($altAclCategory)) $aclCategory = $altAclCategory;
867     if(!empty($altAclPlugin)) $aclPlugin = $altAclPlugin;
869     // Save user input
870     $this->cpHandler->save_object();
872     // Add entries to queue 
873     if($action == "copy" || $action == "cut"){
874       $this->cpHandler->cleanup_queue();
875       foreach($target as $dn){
876         if($action == "copy" && $this->ui->is_copyable($dn,$aclCategory,$aclPlugin)){
877           $this->cpHandler->add_to_queue($dn,"copy",$tabClass,$tabType,$aclCategory,$this);
878           @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$dn,"Entry copied!");
879         }
880         if($action == "cut" && $this->ui->is_cutable($dn,$aclCategory,$aclPlugin)){
881           $this->cpHandler->add_to_queue($dn,"cut",$tabClass,$tabType,$aclCategory,$this);
882           @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$dn,"Entry cutted!");
883         }
884       }
885     }
887     // Initiate pasting
888     if($action == "paste"){
889       $this->cpPastingStarted = TRUE;
890     }
892     // Display any c&p dialogs, eg. object modifications required before pasting.
893     if($this->cpPastingStarted && $this->cpHandler->entries_queued()){
894       $this->cpHandler->SetVar("base",$this->headpage->getBase());
895       $data = $this->cpHandler->execute();
896       if(!empty($data)){
897         return($data);
898       }
899     }
901     // Automatically disable pasting process since there is no entry left to paste.
902     if(!$this->cpHandler->entries_queued()){
903       $this->cpPastingStarted = FALSE;
904     }
905     return("");
906   }
909   function setFilter($str) {
910     $this->filter = $str;
911   }
914   function postcreate() {
915     $this->_handlePostEvent('POSTCREATE');
916   }
917   function postmodify(){
918     $this->_handlePostEvent('POSTMODIFY');
919   }
920   function postremove(){
921     $this->_handlePostEvent('POSTREMOVE');
922   }
924   function _handlePostEvent($type)
925   {
927     /* Find postcreate entries for this class */
928     $command= $this->config->search(get_class($this), $type,array('menu', 'tabs'));
929     if ($command != ""){
931       /* Walk through attribute list */
932       foreach ($this->attributes as $attr){
933         if (!is_array($this->$attr)){
934           $add_attrs[$attr] = $this->$attr;
935         }
936       }
937       $add_attrs['dn']=$this->dn;
939       $tmp = array();
940       foreach($add_attrs as $name => $value){
941         $tmp[$name] =  strlen($name);
942       }
943       arsort($tmp);
945       /* Additional attributes */
946       foreach ($tmp as $name => $len){
947         $value = $add_attrs[$name];
948         $command= str_replace("%$name", "$value", $command);
949       }
951       if (check_command($command)){
952         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
953             $command, "Execute");
954         exec($command,$arr);
955         foreach($arr as $str){
956           @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
957               $command, "Result: ".$str);
958         }
959       } else {
960         $message= msgPool::cmdnotfound($type, get_class($this));
961         msg_dialog::display(_("Error"), $message, ERROR_DIALOG);
962       }
963     }
964   }
966   function is_modal_dialog()
967   {
968     return(is_object($this->tabObject) || is_object($this->dialogObject));
969   }
972   /*! \brief    Forward command execution request
973    *             to the correct method.
974    */
975   function handle_post_events($mode, $addAttrs= array())
976   {
977     if(!in_array($mode, array('add','remove','modify'))){
978       trigger_error(sprintf("Invalid post event type given '%s'! Valid types are [add,modify,remove].", $mode));
979       return;
980     }
981     switch ($mode){
982       case "add":
983         plugin::callHook($this,"POSTCREATE", $addAttrs);
984       break;
986       case "modify":
987         plugin::callHook($this,"POSTMODIFY", $addAttrs);
988       break;
990       case "remove":
991         plugin::callHook($this,"POSTREMOVE", $addAttrs);
992       break;
993     }
994   }
997 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
998 ?>