From 23e48f808fb4c046c272327209f599ec4db47358 Mon Sep 17 00:00:00 2001 From: hickert Date: Wed, 28 Jul 2010 09:26:54 +0000 Subject: [PATCH] Updated Post Event handling -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 | 71 ++++++++------------ gosa-core/include/class_plugin.inc | 91 ++++++++++++++++++++++++-- 2 files changed, 113 insertions(+), 49 deletions(-) diff --git a/gosa-core/include/class_management.inc b/gosa-core/include/class_management.inc index beef85777..b2681dab0 100644 --- a/gosa-core/include/class_management.inc +++ b/gosa-core/include/class_management.inc @@ -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; + } } } diff --git a/gosa-core/include/class_plugin.inc b/gosa-core/include/class_plugin.inc index d0351e6d6..8a1890199 100644 --- a/gosa-core/include/class_plugin.inc +++ b/gosa-core/include/class_plugin.inc @@ -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(){ } -- 2.30.2