News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

Show number of errors at top of forum

Started by Daniel15, January 26, 2007, 10:58:31 PM

Previous topic - Next topic

Daniel15

This has been turned into a mod, and is available at the SMF Mods site. Please see http://custom.simplemachines.org/mods/index.php?mod=645. This topic is kept mainly for people that want to understand how it works




Based off a request from Sarke, this topic will show you a way to show the number of errors in your error log, as well as the number of errors on the current page, at the top of the forum. It will add a message like this:
There are 26 errors in the error log
On this page, 2 errors were encountered (debug)

The 'Debug' link will reload the page, with debugging temporarily turned on (it will output all errors directly to the page, and also show some information at the bottom of the page). This will allow you to see what's causing the errors.

Normally, the number of errors on the page would be output normally in the template (in the main_above subtemplate). However, most errors actually occur in the templates. So, this outputs On this page, 0 errors were encountered, and then changes the number via a small JavaScript appended to the end of the page :)

OK, let's begin the editing!

Index.php

Code (Find) Select

// Load the current user's permissions.
loadPermissions();


Code (Add after) Select

// ---- Begin modification - Errors at top of page - http://www.simplemachines.org/community/index.php?topic=145814.0 ----
// How many errors do we have?
global $db_prefix, $context, $db_show_debug;
$result = db_query("
SELECT COUNT(*) AS count
FROM {$db_prefix}log_errors", __FILE__, __LINE__);
list ($context['error_log_count']) = mysql_fetch_row($result);
mysql_free_result($result);

// Are they an admin, and do they want debug mode?
// If so, turn $db_show_debug on for this page
if ($context['user']['is_admin'] && isset($_GET['show_debug']))
$db_show_debug = true;
// ---- End modification ----

This segment grabs the number of errors in the error log, and stores it in a variable called $context['error_log_count']. Additionally, if you're the admin, and pass a GET variable named show_debug, it turns the $db_show_debug variable on (enabling what I call "debugging mode"; I'm not sure if there's a proper name for it). This is used later on.

Sources/Errors.php
Code (Find) Select

global $settings, $modSettings, $db_show_debug;

Code (Replace with) Select

global $settings, $modSettings, $db_show_debug, $context;

// ---- Begin modification - Errors on top of page - http://www.simplemachines.org/community/index.php?topic=145814.0 ----
// Add one to the error count
$context['error_count'] = isset($context['error_count']) ? $context['error_count'] + 1 : 1;
// ---- End modification ----

This will increment a value called $context['error_count'] every time an error is encountered on the current page

Themes/default/languages/Modifications.english.php
Code (Find) Select

?>

Code (Add before) Select

// ---- Begin modification - Errors on top of page - http://www.simplemachines.org/community/index.php?topic=145814.0 ----
$txt['error_log_thereis'] = 'There is';
$txt['error_log_thereare'] = 'There are';
$txt['error_log_currently'] = 'currently';
$txt['error_log_error'] = 'error';
$txt['error_log_errors'] = 'errors';
$txt['error_log_inlog'] = 'in the <a href="?action=viewErrorLog;desc">error log</a>';

$txt['error_log_thispage'] = 'On this page,';
$txt['error_log_was'] = 'was';
$txt['error_log_were'] = 'were';
$txt['error_log_encountered'] = 'encountered';
// ---- End modification ----

These are language strings used on the actual page itself. If you're using another language, translate these, and put them into Modifications.[your language].php

Themes/[your theme]/index.template.php
Code (Find) Select

// Same with hours - only show it if it's above zero.
if ($context['user']['total_time_logged_in']['hours'] > 0)
echo $context['user']['total_time_logged_in']['hours'] . $txt['totalTimeLogged3'];

// But, let's always show minutes - Time wasted here: 0 minutes ;).
echo $context['user']['total_time_logged_in']['minutes'], $txt['totalTimeLogged4'], '<br />';
}

Code (Add after) Select

// ---- Begin modification - Errors on top of page - http://www.simplemachines.org/community/index.php?topic=145814.0 ----
// If they're an admin, show them how many errors we have in the log
if ($context['user']['is_admin'])
{
// Not set? Assume 0, then
if (!isset($context['error_log_count']))
$context['error_log_count'] = 0;
// No querystring set? Assume nothing
if (!isset($_SERVER['QUERY_STRING']))
$_SERVER['QUERY_STRING'] = '';

echo '
<br />', ($context['error_log_count'] == 1 ? $txt['error_log_thereis'] : $txt['error_log_thereare']), ' ', $context['error_log_count'], ' ', ($context['error_log_count'] == 1 ? $txt['error_log_error'] : $txt['error_log_errors']), ' ', $txt['error_log_inlog'], '<br />
', $txt['error_log_thispage'], ' <span id="error_count">0 ', $txt['error_log_errors'], '</span> ', $txt['error_log_encountered'], ' (<a href="', $scripturl, '?show_debug=1;', $_SERVER['QUERY_STRING'], '">debug</a>)<br />';
}
// ---- End modification ----

This bit shows the actual information at the top of the page. It ensures that the message makes grammatical sense (There is 1 error, There are 2 errors).
The errors on the current page has a hardcoded 0, but this is changed later using JavaScript (so we catch all the errors on the page).

Code (Find) Select

echo '
<div id="ajax_in_progress" style="display: none;', $context['browser']['is_ie'] && !$context['browser']['is_ie7'] ? 'position: absolute;' : '', '">', $txt['ajax_in_progress'], '</div>
</body></html>';

Code (Replace with) Select

echo '
<div id="ajax_in_progress" style="display: none;', $context['browser']['is_ie'] && !$context['browser']['is_ie7'] ? 'position: absolute;' : '', '">', $txt['ajax_in_progress'], '</div>';

// Are we an admin, and do we have any errors?
if ($context['user']['is_admin'] && isset($context['error_count']) && $context['error_count'] != 0)
// Change the error text at the top of the page
echo '
<script type="text/javascript" language="JavaScript">
setInnerHTML(document.getElementById("error_count"), "', $context['error_count'], ' ', ($context['error_count'] == 1 ? $txt['error_log_error'] . ' ' . $txt['error_log_was']: $txt['error_log_errors'] . ' ' . $txt['error_log_were']), '");
</script>';

echo '
</body></html>';

This segment adds a JavaScript to the end of the page, which changes the message at the top to reflect the number of errors on the current page.

And that's all. Refresh, and you should see the information at the top of the page. Please reply here if you have any problems :)
Daniel15, former Customisation team member, resigned due to lack of time. I still love everyone here :D.
Go to smfshop.com for SMFshop support, do NOT email or PM me!

Sarke

Nice, thanks! GJ!

This will be very useful for writing mods.

My MODs          Please don't PM me for support, post in the appropriate topic.

Daniel15

QuoteNice, thanks! GJ!
No problem :)
Do you reckon I should make this into a mod?

QuoteThis will be very useful for writing mods.
I personally have debugging turned on all the time when writing mods. If you want to do this, add:

$db_show_debug = true;

to your Settings.php file
Daniel15, former Customisation team member, resigned due to lack of time. I still love everyone here :D.
Go to smfshop.com for SMFshop support, do NOT email or PM me!

Tanix


Daniel15

Daniel15, former Customisation team member, resigned due to lack of time. I still love everyone here :D.
Go to smfshop.com for SMFshop support, do NOT email or PM me!

Tanix


Sarke


My MODs          Please don't PM me for support, post in the appropriate topic.

Advertisement: