MyBB Community Forums

Full Version: Calling the statistics template dvz shoutbox
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello
I have a little problem, tries plugin dvz shoutbox call statistics top shouter by writing code in the plug-in file


Let's start connecting hook in the plugin
$plugins->add_hook('index_end', ['dvz_shoutbox_stats', 'load_window']);	// Hook stats

Then at the end of the file I wrote a piece of code responsible for statistics

	// Statystyki
function dvz_shoutbox_stats()
{
   global $db, $mybb, $templates, $theme, $wpisy, $users, $top_spamer, $users_online_o, $top_spamer_noformatted, $shshshs, $our_shouts, $timesearch, $shouts, $username, $user, $users_online, $anon_online, $invisiblemark, $onlinemembers, $guests_online, $spiders, $cache, $bots_online;
    

 $query = $db->query("SELECT count(id) as id FROM ".TABLE_PREFIX."dvz_shoutbox");

	// Wpisy
    $row = $db->fetch_array($query);
    $wpisy = $row['id'];

    $query3 = $db->query("SELECT d.uid, u.username, u.usergroup, u.displaygroup, u.uid, u.avatar, count(*) as shouters
                          FROM ".TABLE_PREFIX."dvz_shoutbox d
                          LEFT JOIN ".TABLE_PREFIX."users u ON (u.uid=d.uid)
                          GROUP BY d.uid
                          ORDER BY shouters
                          DESC LIMIT 1");
    $row3 = $db->fetch_array($query3);

	// Najwiekszy spamer
	$shouts = $row3['shouters'];
    $top_spamer_noformatted = $row3['username'];
    $top_spamer = build_profile_link(format_name($row3['username'], $row3['usergroup'], $row3['displaygroup']), $row3['uid']);

	// Wpisy uzytkownika
    $query4 = $db->query("SELECT count(id) as id, uid FROM ".TABLE_PREFIX."dvz_shoutbox WHERE uid='".$mybb->user['uid']."'");
    $our_shouts = $db->fetch_field($query4, "id");
	
}
He then proceeded to edit the template dvz shoutbox, adding the variables that are responsible for displaying statistics

<div id="shoutbox" class="front{$classes}">

<div class="head">
<strong>{$lang->dvz_sb_shoutbox}</strong>
<p class="right"><a href="{$mybb->settings['bburl']}/index.php?action=shoutbox_archive">&laquo; {$lang->dvz_sb_archivelink}</a></p>
</div>

<div class="body">

{$panel}

<div class="window" style="height:{$mybb->settings['dvz_sb_height']}px">
<div class="data">
{$html}
</div>
</div>
</div>
<script type="text/javascript" src="{$mybb->settings['bburl']}/jscripts/dvz_shoutbox.js"></script>
{$javascript}  
</div>
<div class="shoutbox-stats"> 
    <ul class="grid group" style="min-height: 20px;">
        <li class="col-1-3"><span class="stat">Twoich shoutów: <span class="block">{$our_shouts}</span></span></li>
        <li class="col-1-3"><span class="stat">Wszystkich shoutów: <span class="block">{$wpisy}</span></span></li>
        <li class="col-1-3"><span class="stat">Największy spamer: <span class="block">{$top_spamer_noformatted} ({$shouts})</span></span></li>
    </ul>
</div>


Unfortunately, after all those steps forum returns an error on the home page and do not work statistics

Warning [2] call_user_func_array() expects parameter 1 to be a valid callback, class 'dvz_shoutbox_stats' not found - Line: 133 - File: inc/class_plugins.php PHP 5.6.24 (Linux)
File 	Line 	Function
[PHP] 	  	errorHandler->error
/inc/class_plugins.php 	133 	call_user_func_array
/index.php 	387 	pluginSystem->run_hooks
[/php]
Since your function is not part of dvz_shoutbox_stats class, you should call the function directly:

$plugins->add_hook('index_end', 'dvz_shoutbox_stats');
Only at the moment as I call it so added variables in the template dvz_shoutbox not return any result
Can't understand your last statement, sorry. Also, if that code is going to run once in a while using AJAX calls, you'd better optimize it as continuously and massively querying the database is going to kill your server.
Given by you hook produces statistics only in the template index - the main page
Where statistics wants to call the template dvz shoutbox
That's how your hook looked. You have not understood how hooks work. The first parameter of $plugins->add_hook() is meant to be the hook you're hooking to, whereas the second parameter is your executable function.

So you're injecting your function to the index_end hook. If you want to load it up in the DVZ Shoutbox template, you should look at its source code and search for a suitable hook.
That's the problem that I fail to find the hook to be zipped into a template dvz_shoutbox
I've just checked the plugin and there are not available hooks. You have to modify the plugin, either adding a custom hook or adding your code directly to the plugin.

Line 847 of inc/plugins/dvz_shoutbox.php:

eval('$dvz_shoutbox = "' . $templates->get('dvz_shoutbox') . '";');

Either you add a custom hook:

global $plugins;
$plugins->run_hooks('dvz_statistics');

And then in your plugin you register it:

$plugins->add_hook('dvz_statistics', 'dvz_shoutbox_stats');

Or you manually add dvz_shoutbox_stats() code before that line.