Code

681d8381b17bf8002a3240eccdc1768ddaa17573
[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     // Add a dummy filter editing button.
182     if(userFilter::userFilteringAvailable()){
183       $dummy = "<button type='submit' name='editFilter'>!!</button>";
184       $this->registerAction("editFilter", "editFilter");
186       print_a(userFilter::getFilter());
187     }
188  
189     // Display list
190     return($dummy.$this->renderList());
191   }
192  
193   function editFilter()
194   {
195     $headpage = $this->getHeadpage();
196     $this->dialogObject = new userFilter($this->config,$headpage->categories);
197   }
198  
199   function renderList()
200   {
201     $this->headpage->update();
202     $display = $this->headpage->render();
203     return($this->getHeader().$display);
204   }
206   function getHeadpage()
207   {
208     return($this->headpage);
209   }
211   function getFilter()
212   {
213     return($this->filter);
214   }
216   /*! \brief  Generates the plugin header which is displayed whenever a tab object is 
217    *           opened.
218    */
219   protected function getHeader()
220   {
221     // We do not display any headers right now.
222     if(1 || $this->skipHeader) return("");
223   }
226   /*! \brief  Generates the footer which is used whenever a tab object is 
227    *           displayed.
228    */
229   protected function _getTabFooter()
230   {
231     // Do not display tab footer for non tab objects 
232     if(!($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug)){
233       return("");
234     }
236     // Check if there is a dialog opened - We don't need any buttons in this case. 
237     if($this->tabObject->by_object[$this->tabObject->current]){
238       $current = $this->tabObject->by_object[$this->tabObject->current];  
239       if(isset($current->dialog) && (is_object($current->dialog) || $current->dialog)){
240         return("");
241       }
242     }
244     // Skip footer if requested;
245     if($this->skipFooter) return("");
247     // In case an of locked entry, we may have opened a read-only tab.
248     $str = "";
249     if(isset($this->tabObject->read_only) && $this->tabObject->read_only == TRUE){
250       $str.= "<p style=\"text-align:right\">
251         <input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">
252         </p>";
253       return($str);
254     }else{
256       // Display ok, (apply) and cancel buttons
257       $str.= "<p style=\"text-align:right\">\n";
258       $str.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
259       $str.= "&nbsp;\n";
260       if($this->displayApplyBtn){
261         $str.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
262         $str.= "&nbsp;\n";
263       }
264       $str.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\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     $disallowed = array();
281     $this->dns = array();
283     @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$target,"Entry removel requested!");
285     // Check permissons for each target 
286     foreach($target as $dn){
287       $acl = $this->ui->get_permissions($dn, $this->aclCategory."/".$this->aclPlugin);
288       if(preg_match("/d/",$acl)){
289         $this->dns[] = $dn;
290       }else{
291         $disallowed[] = $dn;
292       }
293     }
294     if(count($disallowed)){
295       msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
296     }
298     // We've at least one entry to delete.
299     if(count($this->dns)){
301       // check locks
302       if ($user= get_multiple_locks($this->dns)){
303         return(gen_locked_message($user,$this->dns));
304       }
306       // Add locks
307       $dns_names = array();
308       foreach($this->dns as $dn){
309         $dns_names[] =LDAP::fix($dn);
310       }
311       add_lock ($this->dns, $this->ui->dn);
313       // Display confirmation dialog.
314       $smarty = get_smarty();
315       $smarty->assign("info", msgPool::deleteInfo($dns_names,_($this->objectName)));
316       $smarty->assign("multiple", true);
317       return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
318     }
319   }  
322   /*! \brief  Object removal was confirmed, now remove the requested entries. 
323    *      
324    *  @param  String  'action'  The name of the action which was the used as trigger.
325    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
326    *  @param  Array   'all'     A combination of both 'action' and 'target'.
327    */
328   function removeEntryConfirmed($action="",$target=array(),$all=array(),
329       $altTabClass="",$altTabType="",$altAclCategory="")
330   {
331     $tabType = $this->tabType;
332     $tabClass = $this->tabClass;
333     $aclCategory = $this->aclCategory;
334     if(!empty($altTabClass)) $tabClass = $altTabClass;
335     if(!empty($altTabType)) $tabType = $altTabType;
336     if(!empty($altAclCategory)) $aclCategory = $altAclCategory;
338     @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$target,"Entry removel confirmed!");
340     foreach($this->dns as $key => $dn){
342       // Check permissions, are we allowed to remove this object? 
343       $acl = $this->ui->get_permissions($dn, $this->aclCategory."/".$this->aclPlugin);
344       if(preg_match("/d/",$acl)){
346         // Delete the object
347         $this->dn = $dn;
348         $this->tabObject= new $tabClass($this->config,$this->config->data['TABS'][$tabType], $this->dn, $aclCategory, true, true);
349         $this->tabObject->set_acl_base($this->dn);
350         $this->tabObject->parent = &$this;
351         $this->tabObject->delete ();
353         // Remove the lock for the current object.
354         del_lock($this->dn);        
355       } else {
356         msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
357         new log("security","groups/".get_class($this),$dn,array(),"Tried to trick deletion.");
358       }
359     }
361     // Cleanup
362     $this->remove_lock();
363     $this->closeDialogs();
364   }
367   /*! \brief  Detects actions/events send by the ui
368    *           and the corresponding targets.
369    */
370   function detectPostActions()
371   {
372     if(!is_object($this->headpage)){
373       trigger_error("No valid headpage given....!");
374       return(array());
375     }
376     $action= $this->headpage->getAction();
377     if(isset($_POST['edit_apply']))  $action['action'] = "apply";    
378     if(isset($_POST['edit_finish'])) $action['action'] = "save";    
379     if(isset($_POST['edit_cancel'])) $action['action'] = "cancel";    
380     if(isset($_POST['delete_confirmed'])) $action['action'] = "removeConfirmed";   
381     if(isset($_POST['delete_cancel'])) $action['action'] = "cancelDelete";   
382     if(isset($_POST['editFilter'])) $action['action'] = "editFilter";   
383     if(isset($_POST['saveFilter'])) $action['action'] = "save";   
384     if(isset($_POST['cancelFilter'])) $action['action'] = "cancel";   
386     // Detect Snapshot actions
387     if(isset($_POST['CreateSnapshot'])) $action['action'] = "saveSnapshot";   
388     if(isset($_POST['CancelSnapshot'])) $action['action'] = "cancelSnapshot";   
389     foreach($_POST as $name => $value){
390       $once =TRUE;
391       if(preg_match("/^RestoreSnapShot_/",$name) && $once){
392         $once = FALSE;
393         $entry = base64_decode(preg_replace("/^RestoreSnapShot_([^_]*)_[xy]$/i","\\1",$name));
394         $action['action'] = "restoreSnapshot";
395         $action['targets'] = array($entry);
396       }
397     }
399     return($action);
400   }
403   /*! \brief  Calls the registered method for a given action/event.
404    */
405   function handleActions($action)
406   {
407     // Start action  
408     if(isset($this->actions[$action['action']])){
409       $func = $this->actions[$action['action']];
410       if(!isset($action['targets']))$action['targets']= array(); 
411       return($this->$func($action['action'],$action['targets'],$action));
412     }
413   } 
416   /*! \brief  Opens the snapshot creation dialog for the given target.
417    *      
418    *  @param  String  'action'  The name of the action which was the used as trigger.
419    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
420    *  @param  Array   'all'     A combination of both 'action' and 'target'.
421    */
422   function createSnapshotDialog($action="",$target=array(),$all=array())
423   {
424     @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$target,"Snaptshot creation initiated!");
426     foreach($target as $entry){
427       if(!empty($entry) && $this->ui->allow_snapshot_create($entry,$this->aclCategory)){
428         $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
429         $this->dialogObject->aclCategories = array($this->aclCategory);
430         $this->dialogObject->parent = &$this;
432       }else{
433         msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to create a snapshot for %s."),$entry),
434             ERROR_DIALOG);
435       }
436     }
437   }
440   /*! \brief  Creates a snapshot new entry - This method is called when the somebody
441    *           clicks 'save' in the "Create snapshot dialog" (see ::createSnapshotDialog).
442    *      
443    *  @param  String  'action'  The name of the action which was the used as trigger.
444    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
445    *  @param  Array   'all'     A combination of both 'action' and 'target'.
446    */
447   function saveSnapshot($action="",$target=array(),$all=array())
448   { 
449     if(!is_object($this->dialogObject)) return;
450     $this->dialogObject->save_object();
451     $msgs = $this->dialogObject->check();
452     if(count($msgs)){
453       foreach($msgs as $msg){
454         msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
455       }
456     }else{
457       $this->dn =  $this->dialogObject->dn;
458       $this->snapHandler->create_snapshot( $this->dn,$this->dialogObject->CurrentDescription);
459       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Snaptshot created!");
460       $this->closeDialogs();
461     }
462   }
465   /*! \brief  Restores a snapshot object.
466    *          The dn of the snapshot entry has to be given as ['target'] parameter.  
467    *      
468    *  @param  String  'action'  The name of the action which was the used as trigger.
469    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
470    *  @param  Array   'all'     A combination of both 'action' and 'target'.
471    */
472   function restoreSnapshot($action="",$target=array(),$all=array())
473   {
474     $entry = array_pop($target);
475     if(!empty($entry) && $this->ui->allow_snapshot_restore($entry,$this->aclCategory)){
476       $this->snapHandler->restore_snapshot($entry);
477       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Snaptshot restored!");
478       $this->closeDialogs();
479     }else{
480       msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s."),$entry),
481           ERROR_DIALOG);
482     }
483   }
486   /*! \brief  Displays the "Restore snapshot dialog" for a given target. 
487    *          If no target is specified, open the restore removed object 
488    *           dialog.
489    *  @param  String  'action'  The name of the action which was the used as trigger.
490    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
491    *  @param  Array   'all'     A combination of both 'action' and 'target'.
492    */
493   function restoreSnapshotDialog($action="",$target=array(),$all=array())
494   {
495     // Set current restore base for snapshot handling.
496     if(is_object($this->snapHandler)){
497       $bases = array();
498       foreach($this->storagePoints as $sp){
499         $bases[] = $sp.$this->headpage->getBase();
500       }
501     }
503     // No bases specified? Try base  
504     if(!count($bases)) $bases[] = $this->headpage->getBase();
506     // No target, open the restore removed object dialog.
507     if(!count($target)){ 
508       $entry = $this->headpage->getBase();
509       if(!empty($entry) && $this->ui->allow_snapshot_restore($entry,$this->aclCategory)){
510         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$entry,"Snaptshot restoring initiated!");
511         $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
512         $this->dialogObject->set_snapshot_bases($bases);
513         $this->dialogObject->display_all_removed_objects = true;
514         $this->dialogObject->display_restore_dialog = true;
515         $this->dialogObject->parent = &$this;
516       }else{
517         msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s."),$entry),
518             ERROR_DIALOG);
519       } 
520     }else{
522       // Display the restore points for a given object.
523       $entry = array_pop($target);
524       if(!empty($entry) && $this->ui->allow_snapshot_restore($entry,$this->aclCategory)){
525         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$entry,"Snaptshot restoring initiated!");
526         $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
527         $this->dialogObject->set_snapshot_bases($bases);
528         $this->dialogObject->display_restore_dialog = true;
529         $this->dialogObject->parent = &$this;
530       }else{
531         msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s."),$entry),
532             ERROR_DIALOG);
533       } 
534     }
535   }
538   /*! \brief  This method intiates the object creation.
539    *          
540    *  @param  String  'action'  The name of the action which was the used as trigger.
541    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
542    *  @param  Array   'all'     A combination of both 'action' and 'target'.
543    */
544   function newEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
545   {
546     /* To handle mutliple object types overload this method.
547      * ...
548      *   registerAction('newUser', 'newEntry');
549      *   registerAction('newGroup','newEntry');
550      * ... 
551      * 
552      * function newEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory)
553      * {
554      *   switch($action){
555      *     case 'newUser' : {
556      *       mangement::newEntry($action,$target,$all,"usertabs","USERTABS","users");
557      *     }
558      *     case 'newGroup' : {
559      *       mangement::newEntry($action,$target,$all,"grouptabs","GROUPTABS","groups");
560      *     }
561      *   }
562      * }
563      **/ 
564     $tabType = $this->tabType;
565     $tabClass = $this->tabClass;
566     $aclCategory = $this->aclCategory;
567     if(!empty($altTabClass)) $tabClass = $altTabClass;
568     if(!empty($altTabType)) $tabType = $altTabType;
569     if(!empty($altAclCategory)) $aclCategory = $altAclCategory;
571     // Check locking & lock entry if required 
572     $this->displayApplyBtn = FALSE;
573     $this->dn = "new";
574     $this->is_new = TRUE;
575     $this->is_single_edit = FALSE;
576     $this->is_multiple_edit = FALSE;
578     set_object_info($this->dn);
580     // Open object.
581     if(empty($tabClass) || empty($tabType)){
582       // No tab type defined
583     }else{
584       if (isset($this->config->data['TABS'][$tabType])) {
585         $this->tabObject= new $tabClass($this->config,$this->config->data['TABS'][$tabType], $this->dn, $aclCategory);
586         $this->tabObject->set_acl_base($this->headpage->getBase());
587         $this->tabObject->parent = &$this;
588         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Create new entry initiated!");
589       } else {
590         msg_dialog::display(_("Error"), sprintf(_("No tab declaration for '%s' found in your configuration file. Cannot create plugin instance!"), $tabType), ERROR_DIALOG);
591       }
592     }
593   }
596   /*! \brief  This method opens an existing object or a list of existing objects to be edited. 
597    *                  
598    * 
599    *  @param  String  'action'  The name of the action which was the used as trigger.
600    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
601    *  @param  Array   'all'     A combination of both 'action' and 'target'.
602    */
603   function editEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
604   {
605     /* To handle mutliple object types overload this method.
606      * ...
607      *   registerAction('editUser', 'editEntry');
608      *   registerAction('editGroup','editEntry');
609      * ... 
610      * 
611      * function editEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory)
612      * {
613      *   switch($action){
614      *     case 'editUser' : {
615      *       mangement::editEntry($action,$target,$all,"usertabs","USERTABS","users");
616      *     }
617      *     case 'editGroup' : {
618      *       mangement::editEntry($action,$target,$all,"grouptabs","GROUPTABS","groups");
619      *     }
620      *   }
621      * }
622      **/
624     // Do not create a new tabObject while there is already one opened,
625     //  the user may have just pressed F5 to reload the page.
626     if(is_object($this->tabObject)){
627       return;
628     }
629  
630     $tabType = $this->tabType;
631     $tabClass = $this->tabClass;
632     $aclCategory = $this->aclCategory;
633     if(!empty($altTabClass)) $tabClass = $altTabClass;
634     if(!empty($altTabType)) $tabType = $altTabType;
635     if(!empty($altAclCategory)) $aclCategory = $altAclCategory;
637     // Single edit - we only got one object dn.
638     if(count($target) == 1){
639       $this->displayApplyBtn = TRUE;
640       $this->is_new = FALSE;
641       $this->is_single_edit = TRUE;
642       $this->is_multiple_edit = FALSE;
644       // Get the dn of the object and creates lock
645       $this->dn = array_pop($target);
646       set_object_info($this->dn);
647       $user = get_lock($this->dn);
648       if ($user != ""){
649         return(gen_locked_message ($user, $this->dn,TRUE));
650       }
651       add_lock ($this->dn, $this->ui->dn);
653       // Open object.
654       if(empty($tabClass) || empty($tabType)){
655         trigger_error("We can't edit any object(s). 'tabClass' or 'tabType' is empty!");
656       }else{
657         $tab = $tabClass;
658         $this->tabObject= new $tab($this->config,$this->config->data['TABS'][$tabType], $this->dn,$aclCategory);
659         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Edit entry initiated!");
660         $this->tabObject->set_acl_base($this->dn);
661         $this->tabObject->parent = &$this;
662       }
663     }else{
665       // We've multiple entries to edit.
666       $this->is_new = FALSE;
667       $this->is_singel_edit = FALSE;
668       $this->is_multiple_edit = TRUE;
670       // Open multiple edit handler.
671       if(empty($tabClass) || empty($tabType)){
672         trigger_error("We can't edit any object(s). 'tabClass' or 'tabType' is empty!");
673       }else{
674         $this->dns = $target;
675         $tmp = new multi_plug($this->config,$tabClass,$this->config->data['TABS'][$tabType],
676             $this->dns,$this->headpage->getBase(),$aclCategory);
678         // Check for locked entries
679         if ($tmp->entries_locked()){
680           return($tmp->display_lock_message());
681         }
683         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Edit entry initiated!");
685         // Now lock entries.
686         if($tmp->multiple_available()){
687           $tmp->lock_entries($this->ui->dn);
688           $this->tabObject = $tmp;
689           set_object_info($this->tabObject->get_object_info());
690         }
691       }
692     }
693   }
696   /*! \brief  Save object modifications and closes dialogs (returns to object listing).
697    *          - Calls '::check' to validate the given input.
698    *          - Calls '::save' to save back object modifications (e.g. to ldap).
699    *          - Calls '::remove_locks' to remove eventually created locks.
700    *          - Calls '::closeDialogs' to return to the object listing.
701    */
702   protected function saveChanges()
703   {
704     if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
705       $this->tabObject->save_object();
706       $msgs = $this->tabObject->check();
707       if(count($msgs)){
708         msg_dialog::displayChecks($msgs); 
709         return("");
710       }else{
711         $this->tabObject->save();
712         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Entry saved!");
713         $this->remove_lock();
714         $this->closeDialogs();
715       }
716     }elseif($this->dialogObject instanceOf plugin){
717       $this->dialogObject->save_object();
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();
727       }
728     }
729   }
732   /*! \brief  Save object modifications and keep dialogs opened. 
733    *          - Calls '::check' to validate the given input.
734    *          - Calls '::save' to save back object modifications (e.g. to ldap).
735    */
736   protected function applyChanges()
737   {
738     if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
739       $this->tabObject->save_object();
740       $msgs = $this->tabObject->check();
741       if(count($msgs)){
742         msg_dialog::displayChecks($msgs); 
743         return("");
744       }else{
745         $this->tabObject->save();
746         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Modifications applied!");
747         $this->tabObject->re_init();
748       }
749     }
750   }
753   /*! \brief  This method closes dialogs
754    *           and cleans up the cached object info and the ui.
755    */
756   protected function closeDialogs()
757   {
758     $this->last_dn = $this->dn;
759     $this->last_dns = $this->dns;
760     $this->last_tabObject = $this->tabObject;
761     $this->last_dialogObject = $this->dialogObject;
762     $this->dn = "";
763     $this->dns = array();
764     $this->tabObject = null;
765     $this->dialogObject = null;
766     $this->skipFooter = FALSE;
767     set_object_info();
768   }
771   /*! \brief  Editing an object was caneled. 
772    *          Close dialogs/tabs and remove locks.
773    */
774   protected function cancelEdit()
775   {
776     $this->remove_lock();
777     $this->closeDialogs();
778   }
781   /*! \brief  Every click in the list user interface sends an event
782    *           here can we connect those events to a method. 
783    *          eg.  ::registerEvent('new','createUser')
784    *          When the action/event new is send, the method 'createUser' 
785    *           will be called.
786    */
787   function registerAction($action,$target)
788   {
789     $this->actions[$action] = $target;
790   }
793   /*! \brief  Removes ldap object locks created by this class.
794    *          Whenever an object is edited, we create locks to avoid 
795    *           concurrent modifications.
796    *          This locks will automatically removed here.
797    */
798   function remove_lock()
799   {
800     if(!empty($this->dn) && $this->dn != "new"){
801       del_lock($this->dn);
802     }
803     if(count($this->dns)){
804       del_lock($this->dns);
805     }
806   }
809   /*! \brief  This method is used to queue and process copy&paste actions. 
810    *          Allows to copy, cut and paste mutliple entries at once.
811    *  @param  String  'action'  The name of the action which was the used as trigger.
812    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
813    *  @param  Array   'all'     A combination of both 'action' and 'target'.
814    */
815   function copyPasteHandler($action="",$target=array(),$all=array(), 
816       $altTabClass ="", $altTabType = "", $altAclCategory="",$altAclPlugin="")
817   {
818     // Return without any actions while copy&paste handler is disabled.
819     if(!is_object($this->cpHandler))  return("");
821     $tabType = $this->tabType;
822     $tabClass = $this->tabClass;
823     $aclCategory = $this->aclCategory;
824     $aclPlugin = $this->aclPlugin;
825     if(!empty($altTabClass)) $tabClass = $altTabClass;
826     if(!empty($altTabType)) $tabType = $altTabType;
827     if(!empty($altAclCategory)) $aclCategory = $altAclCategory;
828     if(!empty($altAclPlugin)) $aclPlugin = $altAclPlugin;
830     // Save user input
831     $this->cpHandler->save_object();
833     // Add entries to queue 
834     if($action == "copy" || $action == "cut"){
835       $this->cpHandler->cleanup_queue();
836       foreach($target as $dn){
837         if($action == "copy" && $this->ui->is_copyable($dn,$aclCategory,$aclPlugin)){
838           $this->cpHandler->add_to_queue($dn,"copy",$tabClass,$tabType,$aclCategory,$this);
839           @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$dn,"Entry copied!");
840         }
841         if($action == "cut" && $this->ui->is_cutable($dn,$aclCategory,$aclPlugin)){
842           $this->cpHandler->add_to_queue($dn,"cut",$tabClass,$tabType,$aclCategory,$this);
843           @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$dn,"Entry cutted!");
844         }
845       }
846     }
848     // Initiate pasting
849     if($action == "paste"){
850       $this->cpPastingStarted = TRUE;
851     }
853     // Display any c&p dialogs, eg. object modifications required before pasting.
854     if($this->cpPastingStarted && $this->cpHandler->entries_queued()){
855       $this->cpHandler->SetVar("base",$this->headpage->getBase());
856       $data = $this->cpHandler->execute();
857       if(!empty($data)){
858         return($data);
859       }
860     }
862     // Automatically disable pasting process since there is no entry left to paste.
863     if(!$this->cpHandler->entries_queued()){
864       $this->cpPastingStarted = FALSE;
865     }
866     return("");
867   }
870   function setFilter($str) {
871     $this->filter = $str;
872   }
875   function postcreate() {
876     $this->_handlePostEvent('POSTCREATE');
877   }
878   function postmodify(){
879     $this->_handlePostEvent('POSTMODIFY');
880   }
881   function postremove(){
882     $this->_handlePostEvent('POSTREMOVE');
883   }
885   function _handlePostEvent($type)
886   {
888     /* Find postcreate entries for this class */
889     $command= $this->config->search(get_class($this), $type,array('menu', 'tabs'));
890     if ($command != ""){
892       /* Walk through attribute list */
893       foreach ($this->attributes as $attr){
894         if (!is_array($this->$attr)){
895           $add_attrs[$attr] = $this->$attr;
896         }
897       }
898       $add_attrs['dn']=$this->dn;
900       $tmp = array();
901       foreach($add_attrs as $name => $value){
902         $tmp[$name] =  strlen($name);
903       }
904       arsort($tmp);
906       /* Additional attributes */
907       foreach ($tmp as $name => $len){
908         $value = $add_attrs[$name];
909         $command= str_replace("%$name", "$value", $command);
910       }
912       if (check_command($command)){
913         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
914             $command, "Execute");
915         exec($command,$arr);
916         foreach($arr as $str){
917           @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
918               $command, "Result: ".$str);
919         }
920       } else {
921         $message= msgPool::cmdnotfound($type, get_class($this));
922         msg_dialog::display(_("Error"), $message, ERROR_DIALOG);
923       }
924     }
925   }
927   function is_modal_dialog()
928   {
929     return(is_object($this->tabObject) || is_object($this->dialogObject));
930   }
933   /*! \brief    Forward command execution request
934    *             to the correct method.
935    */
936   function handle_post_events($mode, $addAttrs= array())
937   {
938     if(!in_array($mode, array('add','remove','modify'))){
939       trigger_error(sprintf("Invalid post event type given '%s'! Valid types are [add,modify,remove].", $mode));
940       return;
941     }
942     switch ($mode){
943       case "add":
944         plugin::callHook($this,"POSTCREATE", $addAttrs);
945       break;
947       case "modify":
948         plugin::callHook($this,"POSTMODIFY", $addAttrs);
949       break;
951       case "remove":
952         plugin::callHook($this,"POSTREMOVE", $addAttrs);
953       break;
954     }
955   }
958 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
959 ?>