<?php
function prefix_info()
{
return array(
"name" => "Prefix-Plugin",
"description" => "adds a prefix before the thread-subject",
"website" => "http://mods.mybboard.net",
"author" => "XxAnimusxX",
"authorsite" => "http://www.k-under.de",
"version" => "1.0",
"guid" => "3fb2b1ec27e31aa2b05bb8d444bfda55",
"compatibility" => "14*"
);
}
$plugins->add_hook("forumdisplay_thread", "addPrefix_forumdisplay");
$plugins->add_hook("newthread_end", "addPrefix_newthread");
$plugins->add_hook("editpost_end", "addPrefix_editpost");
$plugins->add_hook("editpost_do_editpost_start", "savePrefix_editpost");
$plugins->add_hook("newthread_do_newthread_end", "savePrefix_newthread");
$plugins->add_hook("admin_config_menu", "prefix_toolsmenu");
$plugins->add_hook("admin_config_action_handler", "prefix_actionhandler");
function prefix_toolsmenu($sub_menu)
{
$sub_menu[] = array(
"id" => "prefix",
"title" => "Prefix-Manager",
"link" => "index.php?module=config/prefix"
);
}
function prefix_actionhandler($action)
{
$action['prefix'] = array('active' => 'prefix', 'file' => 'prefix.php');
}
function prefix_is_installed()
{
global $db;
if ($db->field_exists("prefix", "threads"))
{
return true;
}
return false;
}
function prefix_install()
{
global $db;
$db->query("ALTER TABLE ".TABLE_PREFIX."threads ADD prefix VARCHAR(126) NOT NULL");
$find = preg_quote("{\$gotounread}");
$replace = "{\$gotounread}{\$thread['prefix']}";
prefix_replace_templatesets("forumdisplay_thread", $find, $replace);
$find = preg_quote("trow2\"><input");
$replace = "trow2\">{\$prefix_menu}<input";
prefix_replace_templatesets("newthread", $find, $replace);
prefix_replace_templatesets("editpost", $find, $replace);
$db->query("CREATE TABLE ".TABLE_PREFIX."prefix (
pid INT UNSIGNED NOT NULL AUTO_INCREMENT,
title VARCHAR(126) NOT NULL,
fid TEXT NOT NULL,
gid TEXT NOT NULL,
PRIMARY KEY (pid)
)");
}
function prefix_uninstall()
{
global $db;
$db->query("ALTER TABLE ".TABLE_PREFIX."threads DROP prefix");
$find = preg_quote("{\$gotounread}{\$thread['prefix']}");
$replace = "{\$gotounread}";
prefix_replace_templatesets("forumdisplay_thread", $find, $replace);
$find = preg_quote("trow2\">{\$prefix_menu}<input");
$replace = "trow2\"><input";
prefix_replace_templatesets("newthread", $find, $replace);
prefix_replace_templatesets("editpost", $find, $replace);
$db->query("DROP TABLE ".TABLE_PREFIX."prefix");
}
function addPrefix_forumdisplay()
{
global $thread;
if ($thread['prefix'])
{
$thread['prefix'] = "<strong>[".$thread['prefix']."]</strong> ";
}
}
function addPrefix_newthread()
{
global $prefix_menu, $fid;
$prefix_menu = build_prefixmenu($fid);
}
function addPrefix_editpost()
{
global $thread, $fid, $pid, $prefix_menu;
if ($thread['firstpost'] != $pid) return false;
$prefix_menu = build_prefixmenu($fid, $thread['prefix']);
}
function savePrefix_editpost()
{
global $mybb, $thread, $db;
$pid = intval($mybb->input['prefix']);
if (!$thread['prefix'] && !$pid) return false;
$prefix = $db->fetch_array($db->simple_select("prefix", "title", "pid=".$pid));
if (($prefix['title'] == $thread['prefix']) || (!$prefix['title'] && $pid)) return false;
$db->update_query("threads", array("prefix"=>$prefix['title']), "tid=".$thread['tid']);
}
function savePrefix_newthread()
{
global $mybb, $db, $tid;
$pid = intval($mybb->input['prefix']);
if (!$pid) return false;
$prefix = $db->fetch_array($db->simple_select("prefix", "title", "pid=".$pid));
if (!$prefix['title']) return false;
$db->update_query("threads", array("prefix"=>$prefix['title']), "tid=".$tid);
}
function build_prefixmenu($fid = 0, $selected = "")
{
global $mybb, $db;
if (!$fid) return false;
$query = $db->simple_select("prefix");
$menu = "";
while ($prefix = $db->fetch_array($query))
{
$checked = "";
$gids = explode(",", $prefix['gid']);
$fids = explode(",", $prefix['fid']);
$group = in_array($mybb->user['usergroup'], $gids);
if ((in_array($fid, $fids) && $group) || (!$prefix['fid'] && $group))
{
if ($prefix['title'] == $selected) $checked = " selected";
$menu.="<option value=\"".$prefix['pid']."\"".$checked.">".$prefix['title']."</option>";
}
}
if ($menu)
{
return "<select name='prefix'><option value=\"0\"></option>".$menu."</select> ";
}
}
function prefix_replace_templatesets($template, $find, $replace)
{
global $db;
$query = $db->simple_select("templates", "template, tid", "title='".$template."'");
while ($template = $db->fetch_array($query))
{
$tid = $template['tid']; $body = $template['template'];
$body = preg_replace("!".$find."!", $replace, $body);
$db->update_query("templates", array("template" => $db->escape_string($body)), "tid=".$tid);
}
}
?>