MyBB Community Forums

Full Version: attachment display hook?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I am in the planning phases for a plugin that would implement an attachment display quota for certain user groups.

I've looked through the plugin hooks list.  By name attachment_start seems like it might be of potential use to me but I'm not certain.

Is attachment_start the hook to use when a user clicks on an attachment to view the full size?  I know in vBulletin there is/was a specific hook attachment_display.  I do not see anything obviously comparable in the MyBB hook list.

Beyond that is there an actual API document for the plugin hooks?  It is one thing to have a list of the hook names but that's not quite the same as knowing what they do.
From my understanding, you would have to add the changes you want to the template
postbit_attachments

For example, with a plugin:

function pluginname_activate()
{
	global $db;
	require MYBB_ROOT.'/inc/adminfunctions_templates.php';
	find_replace_templatesets('postbit_attachments','#'.preg_quote('{$post['attachmentlist']}').'#','{$post['attachmentlist']}{$quota}');
}

This would change the template when your plugin is activated, there is also a deactivate to remove it:
function pluginname_deactivate()
{
	global $db;
	require MYBB_ROOT.'/inc/adminfunctions_templates.php';
	find_replace_templatesets('postbit_attachments','#'.preg_quote('{$quota}').'#','');
}

Then within your code you would do something like:

$plugins->add_hook('postbit','pluginname_your_function');

and do the required code inside the function

function pluginname_your_function()
{
    $query = $db->simple_select("attachments", "SUM(filesize) AS total_upload_size", "uid='{$mybb->user['uid']}'");
    $quota = $db->fetch_field($query, "total_upload_size");
}

To generate the $quota variable for the template.

What I am unsure here is whether this would work for the postbit_attachments template directly or if you would have to generate your own template and then do something like this:

function pluginname_your_function()
{
    global $db, $mybb, $templates, $lang;
    $query = $db->simple_select("attachments", "SUM(filesize) AS total_upload_size", "uid='{$mybb->user['uid']}'");
    // the name used in your template that is not the same of the postbit_attachments
    $your_quota_variable_name = $db->fetch_field($query, "total_upload_size");
    eval('$quota = "'.$templates->get('your_template_name').'";');
}

but perhaps a more experienced user will point this out for us Wink
Yes, attachment_start is a correct hook to start with your plugin.