Code

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