Code

Avoid PHP warnings
[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, 
367             $aclCategory, true, true);
368         $this->tabObject->set_acl_base($this->dn);
369         $this->tabObject->parent = &$this;
370         $this->tabObject->delete ();
372         // Remove the lock for the current object.
373         del_lock($this->dn);        
374       } else {
375         msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
376         new log("security","groups/".get_class($this),$dn,array(),"Tried to trick deletion.");
377       }
378     }
380     // Cleanup
381     $this->remove_lock();
382     $this->closeDialogs();
383   }
386   /*! \brief  Detects actions/events send by the ui
387    *           and the corresponding targets.
388    */
389   function detectPostActions()
390   {
391     if(!is_object($this->headpage)){
392       trigger_error("No valid headpage given....!");
393       return(array());
394     }
395     $action= $this->headpage->getAction();
396     if(isset($_POST['edit_apply']))  $action['action'] = "apply";    
397     if(isset($_POST['edit_finish'])) $action['action'] = "save";    
398     if(isset($_POST['edit_cancel'])) $action['action'] = "cancel";    
399     if(isset($_POST['delete_confirmed'])) $action['action'] = "removeConfirmed";   
400     if(isset($_POST['delete_cancel'])) $action['action'] = "cancelDelete";   
401     if(isset($_POST['saveFilter'])) $action['action'] = "saveFilter";   
402     if(isset($_POST['cancelFilter'])) $action['action'] = "cancel";   
404     // Detect Snapshot actions
405     if(isset($_POST['CreateSnapshot'])) $action['action'] = "saveSnapshot";   
406     if(isset($_POST['CancelSnapshot'])) $action['action'] = "cancelSnapshot";   
407     foreach($_POST as $name => $value){
408       $once =TRUE;
409       if(preg_match("/^RestoreSnapShot_/",$name) && $once){
410         $once = FALSE;
411         $entry = base64_decode(preg_replace("/^RestoreSnapShot_([^_]*)_[xy]$/i","\\1",$name));
412         $action['action'] = "restoreSnapshot";
413         $action['targets'] = array($entry);
414       }
415     }
417     return($action);
418   }
421   /*! \brief  Calls the registered method for a given action/event.
422    */
423   function handleActions($action)
424   {
425     // Start action  
426     if(isset($this->actions[$action['action']])){
427       $func = $this->actions[$action['action']];
428       if(!isset($action['targets']))$action['targets']= array(); 
429       return($this->$func($action['action'],$action['targets'],$action));
430     }
431   } 
434   /*! \brief  Opens the snapshot creation dialog for the given target.
435    *      
436    *  @param  String  'action'  The name of the action which was the used as trigger.
437    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
438    *  @param  Array   'all'     A combination of both 'action' and 'target'.
439    */
440   function createSnapshotDialog($action="",$target=array(),$all=array())
441   {
442     @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$target,"Snaptshot creation initiated!");
444     foreach($target as $entry){
445       if(!empty($entry) && $this->ui->allow_snapshot_create($entry,$this->aclCategory)){
446         $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
447         $this->dialogObject->aclCategories = array($this->aclCategory);
448         $this->dialogObject->parent = &$this;
450       }else{
451         msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to create a snapshot for %s."),$entry),
452             ERROR_DIALOG);
453       }
454     }
455   }
458   /*! \brief  Creates a snapshot new entry - This method is called when the somebody
459    *           clicks 'save' in the "Create snapshot dialog" (see ::createSnapshotDialog).
460    *      
461    *  @param  String  'action'  The name of the action which was the used as trigger.
462    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
463    *  @param  Array   'all'     A combination of both 'action' and 'target'.
464    */
465   function saveSnapshot($action="",$target=array(),$all=array())
466   { 
467     if(!is_object($this->dialogObject)) return;
468     $this->dialogObject->save_object();
469     $msgs = $this->dialogObject->check();
470     if(count($msgs)){
471       foreach($msgs as $msg){
472         msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
473       }
474     }else{
475       $this->dn =  $this->dialogObject->dn;
476       $this->snapHandler->create_snapshot( $this->dn,$this->dialogObject->CurrentDescription);
477       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Snaptshot created!");
478       $this->closeDialogs();
479     }
480   }
483   /*! \brief  Restores a snapshot object.
484    *          The dn of the snapshot entry has to be given as ['target'] parameter.  
485    *      
486    *  @param  String  'action'  The name of the action which was the used as trigger.
487    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
488    *  @param  Array   'all'     A combination of both 'action' and 'target'.
489    */
490   function restoreSnapshot($action="",$target=array(),$all=array())
491   {
492     $entry = array_pop($target);
493     if(!empty($entry) && $this->ui->allow_snapshot_restore($entry,$this->aclCategory)){
494       $this->snapHandler->restore_snapshot($entry);
495       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Snaptshot restored!");
496       $this->closeDialogs();
497     }else{
498       msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s."),$entry),
499           ERROR_DIALOG);
500     }
501   }
504   /*! \brief  Displays the "Restore snapshot dialog" for a given target. 
505    *          If no target is specified, open the restore removed object 
506    *           dialog.
507    *  @param  String  'action'  The name of the action which was the used as trigger.
508    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
509    *  @param  Array   'all'     A combination of both 'action' and 'target'.
510    */
511   function restoreSnapshotDialog($action="",$target=array(),$all=array())
512   {
513     // Set current restore base for snapshot handling.
514     if(is_object($this->snapHandler)){
515       $bases = array();
516       foreach($this->storagePoints as $sp){
517         $bases[] = $sp.$this->headpage->getBase();
518       }
519     }
521     // No bases specified? Try base  
522     if(!count($bases)) $bases[] = $this->headpage->getBase();
524     // No target, open the restore removed object dialog.
525     if(!count($target)){ 
526       $entry = $this->headpage->getBase();
527       if(!empty($entry) && $this->ui->allow_snapshot_restore($entry,$this->aclCategory)){
528         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$entry,"Snaptshot restoring initiated!");
529         $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
530         $this->dialogObject->set_snapshot_bases($bases);
531         $this->dialogObject->display_all_removed_objects = true;
532         $this->dialogObject->display_restore_dialog = true;
533         $this->dialogObject->parent = &$this;
534       }else{
535         msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s."),$entry),
536             ERROR_DIALOG);
537       } 
538     }else{
540       // Display the restore points for a given object.
541       $entry = array_pop($target);
542       if(!empty($entry) && $this->ui->allow_snapshot_restore($entry,$this->aclCategory)){
543         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$entry,"Snaptshot restoring initiated!");
544         $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
545         $this->dialogObject->set_snapshot_bases($bases);
546         $this->dialogObject->display_restore_dialog = true;
547         $this->dialogObject->parent = &$this;
548       }else{
549         msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s."),$entry),
550             ERROR_DIALOG);
551       } 
552     }
553   }
556   /*! \brief  This method intiates the object creation.
557    *          
558    *  @param  String  'action'  The name of the action which was the used as trigger.
559    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
560    *  @param  Array   'all'     A combination of both 'action' and 'target'.
561    */
562   function newEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
563   {
564     /* To handle mutliple object types overload this method.
565      * ...
566      *   registerAction('newUser', 'newEntry');
567      *   registerAction('newGroup','newEntry');
568      * ... 
569      * 
570      * function newEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory)
571      * {
572      *   switch($action){
573      *     case 'newUser' : {
574      *       mangement::newEntry($action,$target,$all,"usertabs","USERTABS","users");
575      *     }
576      *     case 'newGroup' : {
577      *       mangement::newEntry($action,$target,$all,"grouptabs","GROUPTABS","groups");
578      *     }
579      *   }
580      * }
581      **/ 
582     $tabType = $this->tabType;
583     $tabClass = $this->tabClass;
584     $aclCategory = $this->aclCategory;
585     if(!empty($altTabClass)) $tabClass = $altTabClass;
586     if(!empty($altTabType)) $tabType = $altTabType;
587     if(!empty($altAclCategory)) $aclCategory = $altAclCategory;
589     // Check locking & lock entry if required 
590     $this->displayApplyBtn = FALSE;
591     $this->dn = "new";
592     $this->is_new = TRUE;
593     $this->is_single_edit = FALSE;
594     $this->is_multiple_edit = FALSE;
596     set_object_info($this->dn);
598     // Open object.
599     if(empty($tabClass) || empty($tabType)){
600       // No tab type defined
601     }else{
602       if (isset($this->config->data['TABS'][$tabType])) {
603         $this->tabObject= new $tabClass($this->config,$this->config->data['TABS'][$tabType], $this->dn, $aclCategory);
604         $this->tabObject->set_acl_base($this->headpage->getBase());
605         $this->tabObject->parent = &$this;
606         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Create new entry initiated!");
607       } else {
608         msg_dialog::display(_("Error"), sprintf(_("No tab declaration for '%s' found in your configuration file. Cannot create plugin instance!"), $tabType), ERROR_DIALOG);
609       }
610     }
611   }
614   /*! \brief  This method opens an existing object or a list of existing objects to be edited. 
615    *                  
616    * 
617    *  @param  String  'action'  The name of the action which was the used as trigger.
618    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
619    *  @param  Array   'all'     A combination of both 'action' and 'target'.
620    */
621   function editEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
622   {
623     /* To handle mutliple object types overload this method.
624      * ...
625      *   registerAction('editUser', 'editEntry');
626      *   registerAction('editGroup','editEntry');
627      * ... 
628      * 
629      * function editEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory)
630      * {
631      *   switch($action){
632      *     case 'editUser' : {
633      *       mangement::editEntry($action,$target,$all,"usertabs","USERTABS","users");
634      *     }
635      *     case 'editGroup' : {
636      *       mangement::editEntry($action,$target,$all,"grouptabs","GROUPTABS","groups");
637      *     }
638      *   }
639      * }
640      **/
642     // Do not create a new tabObject while there is already one opened,
643     //  the user may have just pressed F5 to reload the page.
644     if(is_object($this->tabObject)){
645       return;
646     }
647  
648     $tabType = $this->tabType;
649     $tabClass = $this->tabClass;
650     $aclCategory = $this->aclCategory;
651     if(!empty($altTabClass)) $tabClass = $altTabClass;
652     if(!empty($altTabType)) $tabType = $altTabType;
653     if(!empty($altAclCategory)) $aclCategory = $altAclCategory;
655     // Single edit - we only got one object dn.
656     if(count($target) == 1){
657       $this->displayApplyBtn = TRUE;
658       $this->is_new = FALSE;
659       $this->is_single_edit = TRUE;
660       $this->is_multiple_edit = FALSE;
662       // Get the dn of the object and creates lock
663       $this->dn = array_pop($target);
664       set_object_info($this->dn);
665       $user = get_lock($this->dn);
666       if ($user != ""){
667         return(gen_locked_message ($user, $this->dn,TRUE));
668       }
669       add_lock ($this->dn, $this->ui->dn);
671       // Open object.
672       if(empty($tabClass) || empty($tabType)){
673         trigger_error("We can't edit any object(s). 'tabClass' or 'tabType' is empty!");
674       }else{
675         $tab = $tabClass;
676         $this->tabObject= new $tab($this->config,$this->config->data['TABS'][$tabType], $this->dn,$aclCategory);
677         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Edit entry initiated!");
678         $this->tabObject->set_acl_base($this->dn);
679         $this->tabObject->parent = &$this;
680       }
681     }else{
683       // We've multiple entries to edit.
684       $this->is_new = FALSE;
685       $this->is_singel_edit = FALSE;
686       $this->is_multiple_edit = TRUE;
688       // Open multiple edit handler.
689       if(empty($tabClass) || empty($tabType)){
690         trigger_error("We can't edit any object(s). 'tabClass' or 'tabType' is empty!");
691       }else{
692         $this->dns = $target;
693         $tmp = new multi_plug($this->config,$tabClass,$this->config->data['TABS'][$tabType],
694             $this->dns,$this->headpage->getBase(),$aclCategory);
696         // Check for locked entries
697         if ($tmp->entries_locked()){
698           return($tmp->display_lock_message());
699         }
701         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Edit entry initiated!");
703         // Now lock entries.
704         if($tmp->multiple_available()){
705           $tmp->lock_entries($this->ui->dn);
706           $this->tabObject = $tmp;
707           set_object_info($this->tabObject->get_object_info());
708         }
709       }
710     }
711   }
714   /*! \brief  Save filter modifcations.
715    */
716   protected function saveFilter()
717   {
718     if($this->dialogObject instanceOf userFilter){
719       $msgs = $this->dialogObject->check();
720       if(count($msgs)){
721         msg_dialog::displayChecks($msgs); 
722         return("");
723       }else{
724         $this->dialogObject->save();
725         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Entry saved!");
726         $this->remove_lock();
727         $this->closeDialogs();
729         // Ask filter to reload information
730         $this->filter->reloadFilters();
731       }
732     }
733   }
736   /*! \brief  Save object modifications and closes dialogs (returns to object listing).
737    *          - Calls '::check' to validate the given input.
738    *          - Calls '::save' to save back object modifications (e.g. to ldap).
739    *          - Calls '::remove_locks' to remove eventually created locks.
740    *          - Calls '::closeDialogs' to return to the object listing.
741    */
742   protected function saveChanges()
743   {
744     if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
745       $this->tabObject->save_object();
746       $msgs = $this->tabObject->check();
747       if(count($msgs)){
748         msg_dialog::displayChecks($msgs); 
749         return("");
750       }else{
751         $this->tabObject->save();
752         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Entry saved!");
753         $this->remove_lock();
754         $this->closeDialogs();
755       }
756     }elseif($this->dialogObject instanceOf plugin){
757       $this->dialogObject->save_object();
758       $msgs = $this->dialogObject->check();
759       if(count($msgs)){
760         msg_dialog::displayChecks($msgs); 
761         return("");
762       }else{
763         $this->dialogObject->save();
764         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Entry saved!");
765         $this->remove_lock();
766         $this->closeDialogs();
767       }
768     }
769   }
772   /*! \brief  Save object modifications and keep dialogs opened. 
773    *          - Calls '::check' to validate the given input.
774    *          - Calls '::save' to save back object modifications (e.g. to ldap).
775    */
776   protected function applyChanges()
777   {
778     if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
779       $this->tabObject->save_object();
780       $msgs = $this->tabObject->check();
781       if(count($msgs)){
782         msg_dialog::displayChecks($msgs); 
783         return("");
784       }else{
785         $this->tabObject->save();
786         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Modifications applied!");
787         $this->tabObject->re_init();
788       }
789     }
790   }
793   /*! \brief  This method closes dialogs
794    *           and cleans up the cached object info and the ui.
795    */
796   protected function closeDialogs()
797   {
798     $this->last_dn = $this->dn;
799     $this->last_dns = $this->dns;
800     $this->last_tabObject = $this->tabObject;
801     $this->last_dialogObject = $this->dialogObject;
802     $this->dn = "";
803     $this->dns = array();
804     $this->tabObject = null;
805     $this->dialogObject = null;
806     $this->skipFooter = FALSE;
807     set_object_info();
808   }
811   /*! \brief  Editing an object was caneled. 
812    *          Close dialogs/tabs and remove locks.
813    */
814   protected function cancelEdit()
815   {
816     $this->remove_lock();
817     $this->closeDialogs();
818   }
821   /*! \brief  Every click in the list user interface sends an event
822    *           here can we connect those events to a method. 
823    *          eg.  ::registerEvent('new','createUser')
824    *          When the action/event new is send, the method 'createUser' 
825    *           will be called.
826    */
827   function registerAction($action,$target)
828   {
829     $this->actions[$action] = $target;
830   }
833   /*! \brief  Removes ldap object locks created by this class.
834    *          Whenever an object is edited, we create locks to avoid 
835    *           concurrent modifications.
836    *          This locks will automatically removed here.
837    */
838   function remove_lock()
839   {
840     if(!empty($this->dn) && $this->dn != "new"){
841       del_lock($this->dn);
842     }
843     if(count($this->dns)){
844       del_lock($this->dns);
845     }
846   }
849   /*! \brief  This method is used to queue and process copy&paste actions. 
850    *          Allows to copy, cut and paste mutliple entries at once.
851    *  @param  String  'action'  The name of the action which was the used as trigger.
852    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
853    *  @param  Array   'all'     A combination of both 'action' and 'target'.
854    */
855   function copyPasteHandler($action="",$target=array(),$all=array(), 
856       $altTabClass ="", $altTabType = "", $altAclCategory="",$altAclPlugin="")
857   {
858     // Return without any actions while copy&paste handler is disabled.
859     if(!is_object($this->cpHandler))  return("");
861     $tabType = $this->tabType;
862     $tabClass = $this->tabClass;
863     $aclCategory = $this->aclCategory;
864     $aclPlugin = $this->aclPlugin;
865     if(!empty($altTabClass)) $tabClass = $altTabClass;
866     if(!empty($altTabType)) $tabType = $altTabType;
867     if(!empty($altAclCategory)) $aclCategory = $altAclCategory;
868     if(!empty($altAclPlugin)) $aclPlugin = $altAclPlugin;
870     // Save user input
871     $this->cpHandler->save_object();
873     // Add entries to queue 
874     if($action == "copy" || $action == "cut"){
875       $this->cpHandler->cleanup_queue();
876       foreach($target as $dn){
877         if($action == "copy" && $this->ui->is_copyable($dn,$aclCategory,$aclPlugin)){
878           $this->cpHandler->add_to_queue($dn,"copy",$tabClass,$tabType,$aclCategory,$this);
879           @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$dn,"Entry copied!");
880         }
881         if($action == "cut" && $this->ui->is_cutable($dn,$aclCategory,$aclPlugin)){
882           $this->cpHandler->add_to_queue($dn,"cut",$tabClass,$tabType,$aclCategory,$this);
883           @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$dn,"Entry cutted!");
884         }
885       }
886     }
888     // Initiate pasting
889     if($action == "paste"){
890       $this->cpPastingStarted = TRUE;
891     }
893     // Display any c&p dialogs, eg. object modifications required before pasting.
894     if($this->cpPastingStarted && $this->cpHandler->entries_queued()){
895       $this->cpHandler->SetVar("base",$this->headpage->getBase());
896       $data = $this->cpHandler->execute();
897       if(!empty($data)){
898         return($data);
899       }
900     }
902     // Automatically disable pasting process since there is no entry left to paste.
903     if(!$this->cpHandler->entries_queued()){
904       $this->cpPastingStarted = FALSE;
905     }
906     return("");
907   }
910   function setFilter($str) {
911     $this->filter = $str;
912   }
915   function postcreate() {
916     $this->_handlePostEvent('POSTCREATE');
917   }
918   function postmodify(){
919     $this->_handlePostEvent('POSTMODIFY');
920   }
921   function postremove(){
922     $this->_handlePostEvent('POSTREMOVE');
923   }
925   function _handlePostEvent($type)
926   {
928     /* Find postcreate entries for this class */
929     $command= $this->config->search(get_class($this), $type,array('menu', 'tabs'));
930     if ($command != ""){
932       /* Walk through attribute list */
933       foreach ($this->attributes as $attr){
934         if (!is_array($this->$attr)){
935           $add_attrs[$attr] = $this->$attr;
936         }
937       }
938       $add_attrs['dn']=$this->dn;
940       $tmp = array();
941       foreach($add_attrs as $name => $value){
942         $tmp[$name] =  strlen($name);
943       }
944       arsort($tmp);
946       /* Additional attributes */
947       foreach ($tmp as $name => $len){
948         $value = $add_attrs[$name];
949         $command= str_replace("%$name", "$value", $command);
950       }
952       if (check_command($command)){
953         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
954             $command, "Execute");
955         exec($command,$arr);
956         foreach($arr as $str){
957           @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
958               $command, "Result: ".$str);
959         }
960       } else {
961         $message= msgPool::cmdnotfound($type, get_class($this));
962         msg_dialog::display(_("Error"), $message, ERROR_DIALOG);
963       }
964     }
965   }
967   function is_modal_dialog()
968   {
969     return(is_object($this->tabObject) || is_object($this->dialogObject));
970   }
973   /*! \brief    Forward command execution request
974    *             to the correct method.
975    */
976   function handle_post_events($mode, $addAttrs= array())
977   {
978     if(!in_array($mode, array('add','remove','modify'))){
979       trigger_error(sprintf("Invalid post event type given '%s'! Valid types are [add,modify,remove].", $mode));
980       return;
981     }
982     switch ($mode){
983       case "add":
984         plugin::callHook($this,"POSTCREATE", $addAttrs);
985       break;
987       case "modify":
988         plugin::callHook($this,"POSTMODIFY", $addAttrs);
989       break;
991       case "remove":
992         plugin::callHook($this,"POSTREMOVE", $addAttrs);
993       break;
994     }
995   }
998 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
999 ?>