MyBB Community Forums

Full Version: Recount all PMs
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am working on a simple plugin which allows you to prune old PMs automatically. (I use task system)

Thx to @Jones H - we have a code for pruning all unread/read PMs (you can set own time period for both of them).

Now I need one thing - recount PMs after pruning, but I am lost here (PMs are deleted but not recounted, so you see counter which is not updated). I need something server resources friendly. 

Two ideas:
1) @Euan
UPDATE mybb_users u SET totalpms = (SELECT COUNT(pmid) FROM mybb_privatemessages pm WHERE pm.uid = u.uid), unreadpms = (SELECT COUNT(pmid) FROM mybb_privatemessages pm WHERE pm.uid = u.uid AND status='0' AND folder='1');



$queryString  = <<<SQL
UPDATE mybb_users u SET 
    totalpms = (SELECT COUNT(pmid) FROM mybb_privatemessages pm WHERE pm.uid = u.uid),
    unreadpms = (SELECT COUNT(pmid) FROM mybb_privatemessages pm WHERE
        pm.uid = u.uid AND status='0' AND folder='1');
SQL;

$db->write_query($queryString);

2) @Jones H
require_once MYBB_ROOT."inc/functions_user.php";

// Don't know whether it's recipients but it's something like that
$users = array();
$query = $db->simple_select("privatemessages", "uid, recipients", $where);
while($pm = $db->fetch_array($query))
{
    $users[] = $pm['uid'];
    foreach($pm['recipients']['to'] as $uid)
        $users[] = $uid;
    foreach($pm['recipients']['bcc'] as $uid)
        $users[] = $uid;
}
$users = array_unique($users);
foreach($users as $uid)
    update_pm_count($uid); 


These two files are unchaged. I have one testing version where I did some changes, but I think it is not a good approach.
What do you think? Thanks

P.S. I will add setting for this simple plugin if I fix this recount issue
look in the function acp_recount_private_messages()