Code

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