MyBB Community Forums

Full Version: Using the PM datahandler's "datahandler_pm_insert" hook
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
What I did is to hook at datahandler_pm_insert and use control_object to edit the insert_query, inside the object I insert the PM as usual and get the PMID and run my own function right after.

No sure if I explain myself, to make it short I just insert my own function at the end of the insert_query method using control_object.
that whole bit of original PM code is a bit strange and inefficient. The pm count update is run every recipient instead of as s single query once all messages are sent instead of a "update where uid in (list of uids)" since the update value is a constant, and the good return value would be a simple array that has $pms[$uid]=$pmid would make it easier on everyone working in that area.
Hmm, I can't seem to find the code I wrote for this.

Anyway, in the handler the current PMID is stored as $handler->pmid. You need two/three hooks to do this.

function datahandler_pm_insert_hook($data)
{
	if(!$data->pmid)
	{
		return;
	}

	// This code will run for multiple PMs
	do_something_with_the_pm($data->pmid);
}

function private_do_send_end_hook()
{
	// The handler PMID is either the only PM sent or the last one to be sent
	global $pmhandler;

	do_something_with_the_pm($pmhandler->pmid);
}

function do_something_with_the_pm($pmid)
{
	// This can either store all PMIDs in some sort of static variable to run
	// all at once or can be managed individually
	global $mybb, $plugins;

	if(!isset($mybb->settings['mass_pm_manage']))
	{
		// We haven't stored any PMs yet, setup a hook in the redirect
		// This will be different depending on where the PM is being sent from
		// This is for the private.php version
		$plugins->add_hook('redirect', 'manage_all_pms');

		$mybb->settings['mass_pm_manage'] = array();
	}

	$mybb->settings['mass_pm_manage'][] = $pmid;
}

function manage_all_pms($redirect)
{
	global $mybb;

	print_r($mybb->settings['mass_pm_manage']);
}

Might need some tweaking.
that is only valid for a single recipient message. with multiple recipients, the pmid object is always the last ID and it is not accessible via hooks.
Are you sure? The recipients are grouped and the looped through as single/multiple recipients are handled in exactly the same way - with $this->pmid being set whenever a PM is inserted into the database.
I'll give your code a go and see if it does what I need to Tomm. Seems like a long winded solution to something that should ideally be relatively simple unfortunately.
(2013-02-11, 02:50 PM)Tomm M Wrote: [ -> ]Are you sure? The recipients are grouped and the looped through as single/multiple recipients are handled in exactly the same way - with $this->pmid being set whenever a PM is inserted into the database.

yes I am sure

		// Save a copy of the PM for each of our recipients
		foreach($pm['recipients'] as $recipient)
		{

			//snipped a bunch

			$this->pm_insert_data['uid'] = $recipient['uid'];
			$this->pm_insert_data['toid'] = $recipient['uid'];

			$plugins->run_hooks("datahandler_pm_insert", $this);
			$this->pmid = $db->insert_query("privatemessages", $this->pm_insert_data);

			// If PM noices/alerts are on, show!
			if($recipient['pmnotice'] == 1)
			{
				$updated_user = array(
					"pmnotice" => 2
				);
				$db->update_query("users", $updated_user, "uid='{$recipient['uid']}'");
			}

			// Update private message count (total, new and unread) for recipient
			require_once MYBB_ROOT."/inc/functions_user.php";
			update_pm_count($recipient['uid'], 7, $recipient['lastactive']);
		}

as you can see, the $this>pmid is overwritten with each recipient

now if you guys made it something like

			$this->pmid[$recipient['uid']] = $db->insert_query("privatemessages", $this->pm_insert_data);

then it may be useful in the PM "end" hook.
Also, that will require multiple hooks everywhere, something Euantor wants to avoid. You can also just edit the core and add your function right after the PM is inserted.
(2013-02-11, 05:01 PM)pavemen Wrote: [ -> ]as you can see, the $this>pmid is overwritten with each recipient

Which the code I posted should handle. The only caveat is if it is only going to one user you need to hook into the main code (where the datahandler request originated from) to retrieve the pmid (explained in the code I posted).
(2013-02-12, 02:32 AM)Omar G. Wrote: [ -> ]Also, that will require multiple hooks everywhere, something Euantor wants to avoid. You can also just edit the core and add your function right after the PM is inserted.

Modifying the core, regardless of the method, is never a good idea. I would run 10 hooks if it meant not forcing users to modify files themselves.
Pages: 1 2 3