Code

We're html5, right?
[gosa.git] / gosa-core / include / class_management.inc
index dd70b733254ab02430c4acfeae24fd8d221d4673..66bdc4cf2b73a201e86ef9ecc36b881d2bb39540 100644 (file)
@@ -3,7 +3,7 @@
  * This code is part of GOsa (http://www.gosa-project.org)
  * Copyright (C) 2003-2008 GONICUS GmbH
  *
- * ID: $$Id: class_plugin.inc 14584 2009-10-12 14:04:22Z hickert $$
+ * ID: $$Id$$
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -33,7 +33,7 @@ class management
   public $plHeadline    = "unconfigured";
 
   // The currently used object(s) (e.g. in edit, removal)
-  protected $dn = "";
+  public $dn = "";  // this is public due to some compatibility problems with class plugin..
   protected $dns = array();
 
   // The last used object(s).
@@ -46,6 +46,7 @@ class management
   // The tab definitions to use for the current object.
   protected $tabClass = "";         // e.g. usertabs
   protected $tabType = "";          // e.g. USERTABS
+  protected $aclPlugin = "";        // e.g. generic
   protected $aclCategory = "";      // e.g. users
   protected $objectName = "";       // e.g. users
 
@@ -58,7 +59,13 @@ class management
   protected $last_dialogObject = null;
 
   // Whether to display the apply button or not
-  protected $displayApplyBtn = "";
+  protected $displayApplyBtn = FALSE;
+
+  // Whether to display a header or not.
+  protected $skipHeader = false;
+
+  // Whether to display a footer or not.
+  protected $skipFooter = false;
 
   // Copy&Paste handler
   protected $cpHandler = null;
@@ -70,13 +77,16 @@ class management
   protected $snapHandler = null;
 
   // The listing handlers
-  protected $headpage = null;
-  protected $filter = null;
+  private $headpage = null;
+  private $filter = null;
 
   // A list of configured actions/events
   protected $actions = array();
 
-  function  __construct($config,$ui,$plugname, $headpage)
+  // Attributes managed by this plugin, can be used in post events;
+  public $attributes = array(); 
+
+  function  __construct(&$config,$ui,$plugname, $headpage)
   {
     $this->plugname = $plugname;
     $this->headpage = $headpage;
@@ -96,6 +106,7 @@ class management
     $this->registerAction("apply",  "applyChanges");
     $this->registerAction("save",   "saveChanges");
     $this->registerAction("cancel", "cancelEdit");
+    $this->registerAction("cancelDelete", "cancelEdit");
     $this->registerAction("remove", "removeEntryRequested");
     $this->registerAction("removeConfirmed", "removeEntryConfirmed");
 
@@ -107,7 +118,15 @@ class management
     $this->registerAction("restore",     "restoreSnapshotDialog");
     $this->registerAction("saveSnapshot","saveSnapshot");
     $this->registerAction("restoreSnapshot","restoreSnapshot");
+    $this->registerAction("removeSnapshotConfirmed","removeSnapshotConfirmed");
     $this->registerAction("cancelSnapshot","closeDialogs");
+
+    $this->registerAction("config-filter","editFilter");
+    $this->registerAction("saveFilter","saveFilter");
+    $this->registerAction("cancelFilter","cancelFilter");
+
+    // To temporay disable the filter caching UNcomment this line.
+    #session::global_un_set(get_class($this)."_filter");
   }
 
   /*! \brief  Execute this plugin
@@ -119,6 +138,8 @@ class management
     $vars = array('/^act$/','/^listing/','/^PID$/','/^FILTER_PID$/');
     session::set('LOCK_VARS_TO_USE',$vars);
 
+    pathNavigator::registerPlugin($this);
+
     /* Display the copy & paste dialog, if it is currently open */
     $ret = $this->copyPasteHandler("",array());
     if($ret){
@@ -130,9 +151,6 @@ class management
       $this->filter->update();
       session::global_set(get_class($this)."_filter", $this->filter);
       session::set('autocomplete', $this->filter);
-      if (!$this->filter->isValid()){
-        msg_dialog::display(_("Filter error"), _("The filter is incomplete!"), ERROR_DIALOG);
-      }
     }
 
     // Handle actions (POSTs and GETs)
@@ -151,7 +169,7 @@ class management
 
     // Display tab object.
     if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
-      $this->tabObject->save_object();
+#      $this->tabObject->save_object();
       $display = $this->tabObject->execute();
       $display.= $this->_getTabFooter();
       return($this->getHeader().$display);
@@ -163,29 +181,46 @@ class management
       foreach($this->storagePoints as $sp){
         $bases[] = $sp.$this->headpage->getBase();
       }
+
+      // No bases specified? Try base  
+      if(!count($bases)) $bases[] = $this->headpage->getBase();
+
       $this->snapHandler->setSnapshotBases($bases);
     }
-
+  
+    // Display list
+    return($this->renderList());
+  }
+  function editFilter()
+  {
+    $this->dialogObject = new userFilter($this->config,$this->getHeadpage());
+  }
+  function renderList()
+  {
     $this->headpage->update();
     $display = $this->headpage->render();
     return($this->getHeader().$display);
   }
 
+  function getHeadpage()
+  {
+    return($this->headpage);
+  }
 
+  function getFilter()
+  {
+    return($this->filter);
+  }
 
   /*! \brief  Generates the plugin header which is displayed whenever a tab object is 
    *           opened.
    */
   protected function getHeader()
   {
-    if (get_object_info() != ""){
-      $display= print_header(get_template_path($this->plIcon),_($this->plDescription),
-          "<img alt=\"\" class=\"center\" src=\"".get_template_path('images/lists/locked.png')."\">".
-          LDAP::fix(get_object_info()));
-    } else {
-      $display= print_header(get_template_path($this->plIcon),_($this->plDescription));
-    }
-    return($display);
+    // We do not display any headers right now.
+    if(1 || $this->skipHeader) return("");
   }
 
 
@@ -202,29 +237,33 @@ class management
     // Check if there is a dialog opened - We don't need any buttons in this case. 
     if($this->tabObject->by_object[$this->tabObject->current]){
       $current = $this->tabObject->by_object[$this->tabObject->current];  
-      if(is_object($current->dialog)){
+      if(isset($current->dialog) && (is_object($current->dialog) || $current->dialog)){
         return("");
       }
     }
 
+    // Skip footer if requested;
+    if($this->skipFooter) return("");
+
     // In case an of locked entry, we may have opened a read-only tab.
     $str = "";
     if(isset($this->tabObject->read_only) && $this->tabObject->read_only == TRUE){
-      $str.= "<p style=\"text-align:right\">
-        <input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">
+      $str.= "
+        <p style=\"text-align:right\">
+          <button type=submit name=\"edit_cancel\">".msgPool::cancelButton()."</button>
         </p>";
       return($str);
     }else{
 
       // Display ok, (apply) and cancel buttons
       $str.= "<p style=\"text-align:right\">\n";
-      $str.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
+      $str.= "<button type=\"submit\" name=\"edit_finish\">".msgPool::okButton()."</button>\n";
       $str.= "&nbsp;\n";
       if($this->displayApplyBtn){
-        $str.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
+        $str.= "<button type=\"submit\" name=\"edit_apply\">".msgPool::applyButton()."</button>\n";
         $str.= "&nbsp;\n";
       }
-      $str.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
+      $str.= "<button type=\"submit\" name=\"edit_cancel\">".msgPool::cancelButton()."</button>\n";
       $str.= "</p>";
     }
     return($str);
@@ -240,14 +279,21 @@ class management
    */
   protected function removeEntryRequested($action="",$target=array(),$all=array())
   {
+    // Close dialogs and remove locks for currently handled dns
+    $this->cancelEdit();
+  
     $disallowed = array();
     $this->dns = array();
 
     @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$target,"Entry removel requested!");
 
     // Check permissons for each target 
+    $h = $this->getHeadpage();
+    $oTypes = array_reverse($h->objectTypes);
     foreach($target as $dn){
-      $acl = $this->ui->get_permissions($dn, $this->aclCategory."/".$this->aclPlugin);
+      $entry = $h->getEntry($dn);
+      $obj   = $h->getObjectType($oTypes, $entry['objectClass']);
+      $acl = $this->ui->get_permissions($dn, $obj['category']."/".$obj['class']);
       if(preg_match("/d/",$acl)){
         $this->dns[] = $dn;
       }else{
@@ -268,16 +314,28 @@ class management
 
       // Add locks
       $dns_names = array();
+      $types = array();
+
+      // Build list of object -labels
+      foreach($h->objectTypes as $type){
+        $map[$type['objectClass']]= $type['label'];
+      }
+
       foreach($this->dns as $dn){
-        $dns_names[] =LDAP::fix($dn);
+        $tmp = $h->getType($dn);
+        if(isset($map[$tmp])){
+          $dns_names[LDAP::fix($dn)] = _($map[$tmp]);
+        }else{
+          $dns_names[] =LDAP::fix($dn);
+        }
       }
       add_lock ($this->dns, $this->ui->dn);
 
       // Display confirmation dialog.
       $smarty = get_smarty();
-      $smarty->assign("info", msgPool::deleteInfo($dns_names,_($this->objectName)));
+      $smarty->assign("info", msgPool::deleteInfo($dns_names));
       $smarty->assign("multiple", true);
-      return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
+      return($smarty->fetch(get_template_path('removeEntries.tpl')));
     }
   }  
 
@@ -289,27 +347,36 @@ class management
    *  @param  Array   'all'     A combination of both 'action' and 'target'.
    */
   function removeEntryConfirmed($action="",$target=array(),$all=array(),
-      $altTabClass="",$altTabType="",$altAclCategory="")
+      $altTabClass="",$altTabType="",  $altAclCategory="",$altAclPlugin="")
   {
     $tabType = $this->tabType;
     $tabClass = $this->tabClass;
     $aclCategory = $this->aclCategory;
+    $aclPlugin = $this->aclPlugin;
     if(!empty($altTabClass)) $tabClass = $altTabClass;
     if(!empty($altTabType)) $tabType = $altTabType;
     if(!empty($altAclCategory)) $aclCategory = $altAclCategory;
+    if(!empty($altAclPlugin)) $aclPlugin = $altAclPlugin;
 
     @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$target,"Entry removel confirmed!");
 
+    // Check permissons for each target 
+    $h = $this->getHeadpage();
+    $oTypes = array_reverse($h->objectTypes);
     foreach($this->dns as $key => $dn){
+      $entry = $h->getEntry($dn);
+      $obj   = $h->getObjectType($oTypes, $entry['objectClass']);
+      $acl = $this->ui->get_permissions($dn, $obj['category']."/".$obj['class']);
 
       // Check permissions, are we allowed to remove this object? 
-      $acl = $this->ui->get_permissions($dn, $this->aclCategory."/".$this->aclPlugin);
       if(preg_match("/d/",$acl)){
 
         // Delete the object
         $this->dn = $dn;
-        $this->tabObject= new $tabClass($this->config,$this->config->data['TABS'][$tabType], $this->dn, $aclCategory);
+        $this->tabObject= new $tabClass($this->config,$this->config->data['TABS'][$tabType], $this->dn, 
+            $aclCategory, true, true);
         $this->tabObject->set_acl_base($this->dn);
+        $this->tabObject->parent = &$this;
         $this->tabObject->delete ();
 
         // Remove the lock for the current object.
@@ -331,11 +398,19 @@ class management
    */
   function detectPostActions()
   {
+    if(!is_object($this->headpage)){
+      trigger_error("No valid headpage given....!");
+      return(array());
+    }
     $action= $this->headpage->getAction();
     if(isset($_POST['edit_apply']))  $action['action'] = "apply";    
     if(isset($_POST['edit_finish'])) $action['action'] = "save";    
     if(isset($_POST['edit_cancel'])) $action['action'] = "cancel";    
     if(isset($_POST['delete_confirmed'])) $action['action'] = "removeConfirmed";   
+    if(isset($_POST['delete_snapshot_confirm'])) $action['action'] = "removeSnapshotConfirmed";   
+    if(isset($_POST['delete_cancel'])) $action['action'] = "cancelDelete";   
+    if(isset($_POST['saveFilter'])) $action['action'] = "saveFilter";   
+    if(isset($_POST['cancelFilter'])) $action['action'] = "cancelFilter";   
 
     // Detect Snapshot actions
     if(isset($_POST['CreateSnapshot'])) $action['action'] = "saveSnapshot";   
@@ -344,7 +419,7 @@ class management
       $once =TRUE;
       if(preg_match("/^RestoreSnapShot_/",$name) && $once){
         $once = FALSE;
-        $entry = base64_decode(preg_replace("/^RestoreSnapShot_([^_]*)_[xy]$/i","\\1",$name));
+        $entry = base64_decode(preg_replace("/^RestoreSnapShot_(.*)$/i","\\1",$name));
         $action['action'] = "restoreSnapshot";
         $action['targets'] = array($entry);
       }
@@ -381,9 +456,10 @@ class management
       if(!empty($entry) && $this->ui->allow_snapshot_create($entry,$this->aclCategory)){
         $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
         $this->dialogObject->aclCategories = array($this->aclCategory);
+        $this->dialogObject->parent = &$this;
 
       }else{
-        msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to create a snapshot for %s."),$entry),
+        msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to create a snapshot for %s!"), bold($entry)),
             ERROR_DIALOG);
       }
     }
@@ -397,8 +473,9 @@ class management
    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
    *  @param  Array   'all'     A combination of both 'action' and 'target'.
    */
-  function saveSnapsho($action="",$target=array(),$all=array())
-  {
+  function saveSnapshot($action="",$target=array(),$all=array())
+  { 
+    if(!is_object($this->dialogObject)) return;
     $this->dialogObject->save_object();
     $msgs = $this->dialogObject->check();
     if(count($msgs)){
@@ -429,7 +506,22 @@ class management
       @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Snaptshot restored!");
       $this->closeDialogs();
     }else{
-      msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s."),$entry),
+      msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s!"), bold($entry)),
+          ERROR_DIALOG);
+    }
+  }
+
+
+  /*! \brief  Removes a snapshot object.
+   */
+  function removeSnapshotConfirmed($action="",$target=array(),$all=array())
+  {
+    $entry = $this->dialogObject->del_dn;
+    if(!empty($entry) && $this->ui->allow_snapshot_create($entry,$this->aclCategory)){
+      $this->snapHandler->remove_snapshot($entry);
+      @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$entry,"Snaptshot removed!");
+    }else{
+      msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to remove a snapshot for %s!"), bold($entry)),
           ERROR_DIALOG);
     }
   }
@@ -452,6 +544,9 @@ class management
       }
     }
 
+    // No bases specified? Try base  
+    if(!count($bases)) $bases[] = $this->headpage->getBase();
+
     // No target, open the restore removed object dialog.
     if(!count($target)){ 
       $entry = $this->headpage->getBase();
@@ -461,8 +556,9 @@ class management
         $this->dialogObject->set_snapshot_bases($bases);
         $this->dialogObject->display_all_removed_objects = true;
         $this->dialogObject->display_restore_dialog = true;
+        $this->dialogObject->parent = &$this;
       }else{
-        msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s."),$entry),
+        msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s!"), bold($entry)),
             ERROR_DIALOG);
       } 
     }else{
@@ -474,8 +570,9 @@ class management
         $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
         $this->dialogObject->set_snapshot_bases($bases);
         $this->dialogObject->display_restore_dialog = true;
+        $this->dialogObject->parent = &$this;
       }else{
-        msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s."),$entry),
+        msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s!"), bold($entry)),
             ERROR_DIALOG);
       } 
     }
@@ -528,9 +625,14 @@ class management
     if(empty($tabClass) || empty($tabType)){
       // No tab type defined
     }else{
-      $this->tabObject= new $tabClass($this->config,$this->config->data['TABS'][$tabType], $this->dn, $aclCategory);
-      $this->tabObject->set_acl_base($this->headpage->getBase());
-      @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Create new entry initiated!");
+      if (isset($this->config->data['TABS'][$tabType])) {
+        $this->tabObject= new $tabClass($this->config,$this->config->data['TABS'][$tabType], $this->dn, $aclCategory);
+        $this->tabObject->set_acl_base($this->headpage->getBase());
+        $this->tabObject->parent = &$this;
+        @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Create new entry initiated!");
+      } else {
+        msg_dialog::display(_("Error"), sprintf(_("No tab definition for %s found in configuration file: cannot create plugin instance!"), bold($tabType)), ERROR_DIALOG);
+      }
     }
   }
 
@@ -561,7 +663,14 @@ class management
      *     }
      *   }
      * }
-     **/ 
+     **/
+
+    // Do not create a new tabObject while there is already one opened,
+    //  the user may have just pressed F5 to reload the page.
+    if(is_object($this->tabObject)){
+      return;
+    }
     $tabType = $this->tabType;
     $tabClass = $this->tabClass;
     $aclCategory = $this->aclCategory;
@@ -581,7 +690,7 @@ class management
       set_object_info($this->dn);
       $user = get_lock($this->dn);
       if ($user != ""){
-        return(gen_locked_message ($user, $this->dn,TRUE));
+        return(gen_locked_message ($user, array($this->dn),TRUE));
       }
       add_lock ($this->dn, $this->ui->dn);
 
@@ -593,6 +702,7 @@ class management
         $this->tabObject= new $tab($this->config,$this->config->data['TABS'][$tabType], $this->dn,$aclCategory);
         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Edit entry initiated!");
         $this->tabObject->set_acl_base($this->dn);
+        $this->tabObject->parent = &$this;
       }
     }else{
 
@@ -617,8 +727,8 @@ class management
         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Edit entry initiated!");
 
         // Now lock entries.
-        $tmp->lock_entries($this->ui->dn);
         if($tmp->multiple_available()){
+          $tmp->lock_entries($this->ui->dn);
           $this->tabObject = $tmp;
           set_object_info($this->tabObject->get_object_info());
         }
@@ -627,6 +737,39 @@ class management
   }
 
 
+  /*! \brief  Close filter dialog
+   */
+  protected function cancelFilter()
+  {
+      if($this->dialogObject instanceOf userFilter){
+          $this->remove_lock();
+          $this->closeDialogs();
+      }
+  }
+
+
+  /*! \brief  Save filter modifcations.
+   */
+  protected function saveFilter()
+  {
+    if($this->dialogObject instanceOf userFilter){
+      $msgs = $this->dialogObject->check();
+      if(count($msgs)){
+        msg_dialog::displayChecks($msgs); 
+        return("");
+      }else{
+        $this->dialogObject->save();
+        @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Entry saved!");
+        $this->remove_lock();
+        $this->closeDialogs();
+
+        // Ask filter to reload information
+        $this->filter->reloadFilters();
+      }
+    }
+  }
+
+
   /*! \brief  Save object modifications and closes dialogs (returns to object listing).
    *          - Calls '::check' to validate the given input.
    *          - Calls '::save' to save back object modifications (e.g. to ldap).
@@ -647,6 +790,18 @@ class management
         $this->remove_lock();
         $this->closeDialogs();
       }
+    }elseif($this->dialogObject instanceOf plugin){
+      $this->dialogObject->save_object();
+      $msgs = $this->dialogObject->check();
+      if(count($msgs)){
+        msg_dialog::displayChecks($msgs); 
+        return("");
+      }else{
+        $this->dialogObject->save();
+        @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Entry saved!");
+        $this->remove_lock();
+        $this->closeDialogs();
+      }
     }
   }
 
@@ -685,6 +840,7 @@ class management
     $this->dns = array();
     $this->tabObject = null;
     $this->dialogObject = null;
+    $this->skipFooter = FALSE;
     set_object_info();
   }
 
@@ -756,11 +912,11 @@ class management
       $this->cpHandler->cleanup_queue();
       foreach($target as $dn){
         if($action == "copy" && $this->ui->is_copyable($dn,$aclCategory,$aclPlugin)){
-          $this->cpHandler->add_to_queue($dn,"copy",$tabClass,$tabType,$aclCategory);
+          $this->cpHandler->add_to_queue($dn,"copy",$tabClass,$tabType,$aclCategory,$this);
           @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$dn,"Entry copied!");
         }
         if($action == "cut" && $this->ui->is_cutable($dn,$aclCategory,$aclPlugin)){
-          $this->cpHandler->add_to_queue($dn,"cut",$tabClass,$tabType,$aclCategory);
+          $this->cpHandler->add_to_queue($dn,"cut",$tabClass,$tabType,$aclCategory,$this);
           @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$dn,"Entry cutted!");
         }
       }
@@ -792,6 +948,46 @@ class management
     $this->filter = $str;
   }
 
+
+  function postcreate() {
+    $this->handle_post_events('create');
+  }
+  function postmodify(){
+    $this->handle_post_events('modify');
+  }
+  function postremove(){
+    $this->handle_post_events('remove');
+  }
+
+  function is_modal_dialog()
+  {
+    return(is_object($this->tabObject) || is_object($this->dialogObject));
+  }
+
+
+  /*! \brief    Forward command execution request
+   *             to the correct method.
+   */
+  function handle_post_events($mode, $addAttrs= array())
+  {
+    if(!in_array($mode, array('add','remove','modify'))){
+      trigger_error(sprintf("Invalid post event type given %s! Valid types are [add,modify,remove].", bold($mode)));
+      return;
+    }
+    switch ($mode){
+      case "add":
+        plugin::callHook($this,"POSTCREATE", $addAttrs);
+      break;
+
+      case "modify":
+        plugin::callHook($this,"POSTMODIFY", $addAttrs);
+      break;
+
+      case "remove":
+        plugin::callHook($this,"POSTREMOVE", $addAttrs);
+      break;
+    }
+  }
 }
 
 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler: