ewel
12-11-2008, 01:38 PM
I am creating a plugin for Joomla and ran into problems with the right way to use PHP. After two days of trying and reading and trying I am completely stuck, and I hope someone can help me!
What I am trying to do is to call a function (which returns a variable) from another function in the same sub-class in order to put that between divs and create the final output in the function that is called by the system.
My problem is that nothing I have tried gets one function to return the other function.
To be more precise, dependent on a condition I want to either replace something in the article text or add to the article text. I did actually manage to get the other function returned in order to replace something in the article text, but I just cannot find a way to do so in order to add to the article text.
Since I managed to replace by using preg_replace_callback I thought I might manage to add by using call_user_func, but no luck so far. Obviously I am doing something wrong with the PHP code but having tried many things I have no idea what would be right.
What I have is the following:
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport('joomla.plugin.plugin');
class plgContentExample extends JPlugin
{
function plgContentExample( &$subject, $params )
{
parent::__construct( $subject, $params );
$plugin =& JPluginHelper::getPlugin('content', 'example');
$pluginParams = new JParameter( $plugin->params );
}
function my_example($matches)
{
$outputTEST = "Testing my example plugin";
return $outputTEST;
}
function onPrepareContent( &$article, &$params, $limitstart )
{
global $mainframe;
$plugin =& JPluginHelper::getPlugin('content', 'example');
$pluginParams = new JParameter( $plugin->params );
if($mycondition==1)
{
$regex = "#{(callmyexample)\s*(.*?)}#";
preg_match_all( $regex, $article->text, $matches );
$count = count( $matches[0] );
if ( $count )
{
$article->text = preg_replace_callback($regex,
array(&$this, 'my_example'), $article->text);//THIS WORKS
}
} else {
$myfunctionoutput = call_user_func('my_example');//THE TROUBLESOME LINE
//$myfunctionoutput = 'hi there';//WITH THIS THE REST WORKS
$mydisplayoutput = "<br/><div>".$myfunctionoutput."</div>";
$article->text = $article->text . $mydisplayoutput;
}
}
}
Could anyone help me find what I should put instead of the troublesome bit?
Many many thanks in advance!
E
What I am trying to do is to call a function (which returns a variable) from another function in the same sub-class in order to put that between divs and create the final output in the function that is called by the system.
My problem is that nothing I have tried gets one function to return the other function.
To be more precise, dependent on a condition I want to either replace something in the article text or add to the article text. I did actually manage to get the other function returned in order to replace something in the article text, but I just cannot find a way to do so in order to add to the article text.
Since I managed to replace by using preg_replace_callback I thought I might manage to add by using call_user_func, but no luck so far. Obviously I am doing something wrong with the PHP code but having tried many things I have no idea what would be right.
What I have is the following:
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport('joomla.plugin.plugin');
class plgContentExample extends JPlugin
{
function plgContentExample( &$subject, $params )
{
parent::__construct( $subject, $params );
$plugin =& JPluginHelper::getPlugin('content', 'example');
$pluginParams = new JParameter( $plugin->params );
}
function my_example($matches)
{
$outputTEST = "Testing my example plugin";
return $outputTEST;
}
function onPrepareContent( &$article, &$params, $limitstart )
{
global $mainframe;
$plugin =& JPluginHelper::getPlugin('content', 'example');
$pluginParams = new JParameter( $plugin->params );
if($mycondition==1)
{
$regex = "#{(callmyexample)\s*(.*?)}#";
preg_match_all( $regex, $article->text, $matches );
$count = count( $matches[0] );
if ( $count )
{
$article->text = preg_replace_callback($regex,
array(&$this, 'my_example'), $article->text);//THIS WORKS
}
} else {
$myfunctionoutput = call_user_func('my_example');//THE TROUBLESOME LINE
//$myfunctionoutput = 'hi there';//WITH THIS THE REST WORKS
$mydisplayoutput = "<br/><div>".$myfunctionoutput."</div>";
$article->text = $article->text . $mydisplayoutput;
}
}
}
Could anyone help me find what I should put instead of the troublesome bit?
Many many thanks in advance!
E