Need assistance with developing my first add-on

Luxus

Well-known member
I am trying to make an addon that adds a new helper to xenforo. I called this helper simply "rank" and it displays a rank dependent from the users post count.

I did this tutorial and am at step 4, where I add the function of my helper and am stuck.

Here is what I have so far:

Listener.php:

PHP:
<?php
//Our class name
class UserRank_Listener
{
    /**
    * Listen to the "init_dependencies" code event.
    *
    * @param XenForo_Dependencies_Abstract $dependencies
    * @param array $data
    */
    public static function init(XenForo_Dependencies_Abstract $dependencies, array $data)
    {
        //Get the static variable $helperCallbacks and add a new item in the array.
        XenForo_Template_Helper_Core::$helperCallbacks += array(
            'rank' => array('UserRank_Helpers', 'helperRank')
        );
    }
}
?>

Helpers.php:
PHP:
<?php
//Our class helper (we can put any helpers in here we want)
class UserRank_Helpers
{
    public static function helperRank ($string)
    {
        //We only return the argument, dont do nothing.
        return $string;
    }
}
?>

Currently I have this code in my messega_user_info template that I want to call with my new helper:

HTML:
<xen:if is="{$user.message_count} > 29 AND {$user.message_count} < 100">
	<li class="pairsInlineranks">
		<img src="styles/mystyle/xenforo/ranks/rank1.png">
	</li>
</xen:if>

<xen:if is="{$user.message_count} > 99 AND {$user.message_count} < 300">
	<li class="pairsInlineranks">
		<img src="styles/mystyle/xenforo/ranks/rank2.png">
	</li>
</xen:if>

<xen:if is="{$user.message_count} > 299 AND {$user.message_count} < 600">
	<li class="pairsInlineranks">
		<img src="styles/mystyle/xenforo/ranks/rank3.png">
	</li>
</xen:if>

<xen:if is="{$user.message_count} > 599 AND {$user.message_count} < 1000">
	<li class="pairsInlineranks">
		<img src="styles/mystyle/xenforo/ranks/rank4.png">
	</li>
</xen:if>

<xen:if is="{$user.message_count} > 999 AND {$user.message_count} < 1500">
	<li class="pairsInlineranks">
		<img src="styles/mystyle/xenforo/ranks/rank5.png">
	</li>
</xen:if>

<xen:if is="{$user.message_count} > 1499 AND {$user.message_count} < 2000">
	<li class="pairsInlineranks">
		<img src="styles/mystyle/xenforo/ranks/rank6.png">
	</li>
</xen:if>
<xen:if is="{$user.message_count} > 1999 AND {$user.message_count} < 2500">
	<li class="pairsInlineranks">
		<img src="styles/mystyle/xenforo/ranks/rank7.png">
	</li>
</xen:if>

<xen:if is="{$user.message_count} > 2499 AND {$user.message_count} < 3000">
	<li class="pairsInlineranks">
		<img src="styles/mystyle/xenforo/ranks/rank8.png">
	</li>
</xen:if>

<xen:if is="{$user.message_count} > 2999 AND {$user.message_count} < 3500">
	<li class="pairsInlineranks">
		<img src="styles/mystyle/xenforo/ranks/rank9.png">
	</li>
</xen:if>

<xen:if is="{$user.message_count} > 3499 AND {$user.message_count} < 4000">
	<li class="pairsInlineranks">
		<img src="styles/mystyle/xenforo/ranks/rank10.png">
	</li>
</xen:if>

<xen:if is="{$user.message_count} > 3999 AND {$user.message_count} < 4500">
	<li class="pairsInlineranks">
		<img src="styles/mystyle/xenforo/ranks/rank11.png">
	</li>
</xen:if>

<xen:if is="{$user.message_count} > 4499 AND {$user.message_count} < 5000">
	<li class="pairsInlineranks">
		<img src="styles/mystyle/xenforo/ranks/rank12.png">
	</li>
</xen:if>

<xen:if is="{$user.message_count} > 4999 AND {$user.message_count} < 5500">
	<li class="pairsInlineranks">
		<img src="styles/mystyle/xenforo/ranks/rank13.png">
	</li>
</xen:if>

<xen:if is="{$user.message_count} > 5499 AND {$user.message_count} < 6000">
	<li class="pairsInlineranks">
		<img src="styles/mystyle/xenforo/ranks/rank14.png">
	</li>
</xen:if>

<xen:if is="{$user.message_count} > 5999 AND {$user.message_count} < 6500">
	<li class="pairsInlineranks">
		<img src="styles/mystyle/xenforo/ranks/rank15.png">
	</li>
</xen:if>

<xen:if is="{$user.message_count} > 6499 AND {$user.message_count} < 7000">
	<li class="pairsInlineranks">
		<img src="styles/mystyle/xenforo/ranks/rank16.png">
	</li>
</xen:if>

<xen:if is="{$user.message_count} > 6999 AND {$user.message_count} < 7500">
	<li class="pairsInlineranks">
		<img src="styles/mystyle/xenforo/ranks/rank17.png">
	</li>
</xen:if>

<xen:if is="{$user.message_count} > 7499 AND {$user.message_count} < 8000">
	<li class="pairsInlineranks">
		<img src="styles/mystyle/xenforo/ranks/rank18.png">
	</li>
</xen:if>

<xen:if is="{$user.message_count} > 7999 AND {$user.message_count} < 8500">
	<li class="pairsInlineranks">
		<img src="styles/mystyle/xenforo/ranks/rank19.png">
	</li>
</xen:if>

<xen:if is="{$user.message_count} > 8499 AND {$user.message_count} < 10000">
	<li class="pairsInlineranks">
		<img src="styles/mystyle/xenforo/ranks/rank20.png">
	</li>
</xen:if>

<xen:if is="{$user.message_count} > 9999">
	<li class="pairsInlineranks">
		<img src="styles/mystyle/xenforo/ranks/rank21.png">
	</li>
</xen:if>

Basically what I want to achieve is whenever I put

{xen:helper rank, $something}

in a template, it should call the template code from above.
 
You need to change the function in the helper to create a new template when it's called.

It'll be something like:
PHP:
return $template->create('messega_user_info',$viewParams)->render();
rather than:
PHP:
return $string;

You'll also need to instantiate the XenForo template object too.

There's a learning point, now try and play around! :)
 
I tried to put the template code directly in php. Here is my helpers.php

PHP:
<?php
 
//Our class helper (we can put any helpers in here we want)
class UserRank_Helpers
{
    public static function helperRank ($rank)
    {
$rank = "
<xen:if is=\"{$user.message_count} > 29 AND {$user.message_count} < 100\">
<li class=\"pairsInlineranks\">
<img src=\"styles/mystyle/xenforo/ranks/rank1.png\">
</li>
</xen:if>
 
<xen:if is=\"{$user.message_count} > 99 AND {$user.message_count} < 300\">
<li class=\"pairsInlineranks\">
<img src=\"styles/mystyle/xenforo/ranks/rank2.png\">
</li>
</xen:if>
 
<xen:if is=\"{$user.message_count} > 299 AND {$user.message_count} < 600\">
<li class=\"pairsInlineranks\">
<img src=\"styles/mystyle/xenforo/ranks/rank3.png\">
</li>
</xen:if>
 
<xen:if is=\"{$user.message_count} > 599 AND {$user.message_count} < 1000\">
<li class=\"pairsInlineranks\">
<img src=\"styles/mystyle/xenforo/ranks/rank4.png\">
</li>
</xen:if>
 
<xen:if is=\"{$user.message_count} > 999 AND {$user.message_count} < 1500\">
<li class=\"pairsInlineranks\">
<img src=\"styles/mystyle/xenforo/ranks/rank5.png\">
</li>
</xen:if>
 
<xen:if is=\"{$user.message_count} > 1499 AND {$user.message_count} < 2000\">
<li class=\"pairsInlineranks\">
<img src=\"styles/mystyle/xenforo/ranks/rank6.png\">
</li>
</xen:if>
<xen:if is=\"{$user.message_count} > 1999 AND {$user.message_count} < 2500\">
<li class=\"pairsInlineranks\">
<img src=\"styles/mystyle/xenforo/ranks/rank7.png\">
</li>
</xen:if>
 
<xen:if is=\"{$user.message_count} > 2499 AND {$user.message_count} < 3000\">
<li class=\"pairsInlineranks\">
<img src=\"styles/mystyle/xenforo/ranks/rank8.png\">
</li>
</xen:if>
 
<xen:if is=\"{$user.message_count} > 2999 AND {$user.message_count} < 3500\">
<li class=\"pairsInlineranks\">
<img src=\"styles/mystyle/xenforo/ranks/rank9.png\">
</li>
</xen:if>
 
<xen:if is=\"{$user.message_count} > 3499 AND {$user.message_count} < 4000\">
<li class=\"pairsInlineranks\">
<img src=\"styles/mystyle/xenforo/ranks/rank10.png\">
</li>
</xen:if>
 
<xen:if is=\"{$user.message_count} > 3999 AND {$user.message_count} < 4500\">
<li class=\"pairsInlineranks\">
<img src=\"styles/mystyle/xenforo/ranks/rank11.png\">
</li>
</xen:if>
 
<xen:if is=\"{$user.message_count} > 4499 AND {$user.message_count} < 5000\">
<li class=\"pairsInlineranks\">
<img src=\"styles/mystyle/xenforo/ranks/rank12.png\">
</li>
</xen:if>
 
<xen:if is=\"{$user.message_count} > 4999 AND {$user.message_count} < 5500\">
<li class=\"pairsInlineranks\">
<img src=\"styles/mystyle/xenforo/ranks/rank13.png\">
</li>
</xen:if>
 
<xen:if is=\"{$user.message_count} > 5499 AND {$user.message_count} < 6000\">
<li class=\"pairsInlineranks\">
<img src=\"styles/mystyle/xenforo/ranks/rank14.png\">
</li>
</xen:if>
 
<xen:if is=\"{$user.message_count} > 5999 AND {$user.message_count} < 6500\">
<li class=\"pairsInlineranks\">
<img src=\"styles/mystyle/xenforo/ranks/rank15.png\">
</li>
</xen:if>
 
<xen:if is=\"{$user.message_count} > 6499 AND {$user.message_count} < 7000\">
<li class=\"pairsInlineranks\">
<img src=\"styles/mystyle/xenforo/ranks/rank16.png\">
</li>
</xen:if>
 
<xen:if is=\"{$user.message_count} > 6999 AND {$user.message_count} < 7500\">
<li class=\"pairsInlineranks\">
<img src=\"styles/mystyle/xenforo/ranks/rank17.png\">
</li>
</xen:if>
 
<xen:if is=\"{$user.message_count} > 7499 AND {$user.message_count} < 8000\">
<li class=\"pairsInlineranks\">
<img src=\"styles/mystyle/xenforo/ranks/rank18.png\">
</li>
</xen:if>
 
<xen:if is=\"{$user.message_count} > 7999 AND {$user.message_count} < 8500\">
<li class=\"pairsInlineranks\">
<img src=\"styles/mystyle/xenforo/ranks/rank19.png\">
</li>
</xen:if>
 
<xen:if is=\"{$user.message_count} > 8499 AND {$user.message_count} < 10000\">
<li class=\"pairsInlineranks\">
<img src=\"styles/mystyle/xenforo/ranks/rank20.png\">
</li>
</xen:if>
 
<xen:if is=\"{$user.message_count} > 9999\">
<li class=\"pairsInlineranks\">
<img src=\"styles/mystyle/xenforo/ranks/rank21.png\">
</li>
</xen:if>
";
        //We only return the argument, dont do nothing.
        return "$rank";
    }
}
?>

But the periods seem to make a problem. If I put {xen:helper rank, $rank} in my template, I get this

Parse error: syntax error, unexpected '.', expecting '}' in C:\xampp\htdocs\xen\library\UserRank\Helpers.php on line 9

Your method is probably better, but first I want to get my method to work. My method works with raw html, but the periods are a problem for now.

Edit: Nevermind, I give up. Will use the old-school template editing.
 
Why not do all the calculations in the function and simply return the image:
PHP:
public static function helperRank(array $user)
{
$image = '';
if (!empty($user) && isset($user['message_count']))
{
$messageCount = $user['message_count'];
$rank = 0;
 
if ($messageCount > 29 && $messageCount < 100)
{
$rank = 1;
}
else if ($messageCount > 99 && $messageCount < 300)
{
$rank = 2;
}
else if ($messageCount > 299 && $messageCount < 600)
{
$rank = 3;
}
 
if ($rank)
{
$image = '<li class="pairsInlineranks"><img src="styles/mystyle/xenforo/ranks/rank' . $rank . '.png"></li>';
}
}
 
return $image;
}

Then in the template use:
{xen:helper rank, $user}
 
Thank you so much syndol, it works. (y)
Now I can display my ranks in the member list, member card, visitorPanel block, member profile, threads and other places by using {xen:helper rank, $user}. Awesome!
 
Top Bottom