Code

Updated Post Event handling
authorhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Wed, 28 Jul 2010 09:26:54 +0000 (09:26 +0000)
committerhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Wed, 28 Jul 2010 09:26:54 +0000 (09:26 +0000)
-Created function named _callHook which can execute all event types.
 This method replaces all the extra methods which wre more or less duplicates, except for one line.

git-svn-id: https://oss.gonicus.de/repositories/gosa/branches/2.6@19178 594d385d-05f5-0310-b6e9-bd551577e9d8

gosa-core/include/class_management.inc
gosa-core/include/class_plugin.inc

index beef857772dde0cdf05974c25fa6c3b082e133a2..b2681dab074be94864f9e39ee2278ac5f71fadc9 100644 (file)
@@ -866,62 +866,43 @@ class management
     $this->filter = $str;
   }
 
-
   function postcreate() {
-    $this->_handlePostEvent('POSTCREATE');
+      $this->handle_post_events('add');
   }
   function postmodify(){
-    $this->_handlePostEvent('POSTMODIFY');
+      $this->handle_post_events('modify');
   }
   function postremove(){
-    $this->_handlePostEvent('POSTREMOVE');
+      $this->handle_post_events('remove');
   }
 
-  function _handlePostEvent($type)
+  function is_modal_dialog()
   {
-
-    /* Find postcreate entries for this class */
-    $command= $this->config->search(get_class($this), $type,array('menu', 'tabs'));
-    if ($command != ""){
-
-      /* Walk through attribute list */
-      foreach ($this->attributes as $attr){
-        if (!is_array($this->$attr)){
-          $add_attrs[$attr] = $this->$attr;
-        }
-      }
-      $add_attrs['dn']=$this->dn;
-
-      $tmp = array();
-      foreach($add_attrs as $name => $value){
-        $tmp[$name] =  strlen($name);
-      }
-      arsort($tmp);
-
-      /* Additional attributes */
-      foreach ($tmp as $name => $len){
-        $value = $add_attrs[$name];
-        $command= str_replace("%$name", "$value", $command);
-      }
-
-      if (check_command($command)){
-        @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
-            $command, "Execute");
-        exec($command,$arr);
-        foreach($arr as $str){
-          @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
-              $command, "Result: ".$str);
-        }
-      } else {
-        $message= msgPool::cmdnotfound($type, get_class($this));
-        msg_dialog::display(_("Error"), $message, ERROR_DIALOG);
-      }
-    }
+      return(is_object($this->tabObject) || is_object($this->dialogObject));
   }
 
-  function is_modal_dialog()
+  /*! \brief    Forward command execution request
+   *             to the correct method.
+   */
+  function handle_post_events($mode, $addAttrs= array())
   {
-    return(is_object($this->tabObject) || is_object($this->dialogObject));
+      if(!in_array($mode, array('add','remove','modify'))){
+          trigger_error(sprintf("Invalid post event type given %s! Valid types are [add,modify,remove].", $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;
+      }
   }
 }
 
index d0351e6d63fb9b26fd794c4b99ea6078d00e1e4a..8a18901991bdfdeb1d8335c0df04b9b042705b4b 100644 (file)
@@ -1147,24 +1147,107 @@ class plugin
   }
 
 
-  function handle_post_events($mode, $add_attrs= array())
+  /*! \brief    Forward command execution requests
+   *             to the hook execution 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].", $mode));
+      return;
+    }
     switch ($mode){
       case "add":
-        $this->postcreate($add_attrs);
+        plugin::callHook($this,"POSTCREATE", $addAttrs);
       break;
 
       case "modify":
-        $this->postmodify($add_attrs);
+        plugin::callHook($this,"POSTMODIFY", $addAttrs);
       break;
 
       case "remove":
-        $this->postremove($add_attrs);
+        plugin::callHook($this,"POSTREMOVE", $addAttrs);
       break;
     }
   }
 
 
+  /*! \brief    Calls external hooks which are defined for this plugin (gosa.conf)
+   *            Replaces placeholder by class values of this plugin instance.
+   *  @param    Allows to a add special replacements.
+   */
+  static function callHook($plugin, $cmd, $addAttrs= array(), &$returnOutput = array(), &$returnCode = NULL)
+  {
+      global $config;
+      $command= $config->search(get_class($plugin), "POSTCREATE",array('menu', 'tabs'));
+      if ($command != ""){
+
+          // Walk trough attributes list and add the plugins attributes.
+          foreach ($plugin->attributes as $attr){
+              if (!is_array($plugin->$attr)){
+                  $addAttrs[$attr] = $plugin->$attr;
+              }
+          }
+          $ui = get_userinfo();
+          $addAttrs['callerDN']=$ui->dn;
+          $addAttrs['dn']=$plugin->dn;
+          $addAttrs['location']=$config->current['NAME'];
+
+          // Sort attributes by length, ensures correct replacement
+          $tmp = array();
+          foreach($addAttrs as $name => $value){
+              $tmp[$name] =  strlen($name);
+          }
+          arsort($tmp);
+
+          // Now replace the placeholder
+          foreach ($tmp as $name => $len){
+              $value = $addAttrs[$name];
+              $command= str_replace("%$name", "$value", $command);
+          }
+          // If there are still some %.. in our command, try to fill these with some other class vars
+          if(preg_match("/%/",$command)){
+              $attrs = get_object_vars($plugin);
+              foreach($attrs as $name => $value){
+                  if(is_array($value)){
+                      $s = "";
+                      foreach($value as $val){
+                          if(is_string($val) || is_int($val) || is_float($val) || is_bool($val)){
+                              $s .= '"'.$val.'",';
+                          }
+                      }
+                      $value = '['.trim($s,',').']';
+                  }
+                  if(!is_string($value) && !is_int($value) && !is_float($value) && !is_bool($value)){
+                      continue;
+                  }
+                  $command= preg_replace("/%$name/", $value, $command);
+              }
+          }
+
+          if (check_command($command)){
+
+              @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,$command,"Execute");
+              exec($command, $arr, $returnCode);
+              $returnOutput = $arr;
+
+              if($returnCode != 0){
+                  $str = implode("\n",$arr);
+                  @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__, $command, "Execution failed code: ".$returnCode);
+                  $message= msgPool::cmdexecfailed($cmd,$command, get_class($plugin));
+                  msg_dialog::display(_("Error"), $message, ERROR_DIALOG);
+              }elseif(is_array($arr)){
+                  $str = implode("\n",$arr);
+                  @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__, $command, "Result: ".$str);
+              }
+          } else {
+              $message= msgPool::cmdinvalid($cmd,$command, get_class($plugin));
+              msg_dialog::display(_("Error"), $message, ERROR_DIALOG);
+          }
+      }
+  }
+
+
   function saveCopyDialog(){
   }