MyBB Community Forums

Full Version: Datahandlers help in newthread.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So finally being frustrated after working and spending hours behind datahandler (something not done previously by me), seeking a bit help.

What I did is basically created a text field which takes input from user on new thread and edit post page and stores into the database.

Edit post just works fine because I've value of tid as the post was already created. But newthread part is tricky one, hence I needed to use datahandlers hook, tried 2-3 of them.

I can't seem to get thread id (tid) anyhow.

Any sample code or guidance shall be a boon.

Thank you.
If you're inserting it into a separate table then Try using newthread_do_newthread_end hook, something like this;
$plugins->add_hook("newthread_do_newthread_end", "something_newthread");
function something_newthread()
{
	global $mybb, $db, $thread_info;
	$insert_tags = array(
		"something_newthread" => $db->escape_string($mybb->input['something_newthread']),
		"tid" => intval($thread_info['tid'])
	);
	$db->insert_query("threads", $insert_tags);
}
First you validate the user input at "datahandler_post_validate_thread":
$plugins->add_hook('datahandler_post_validate_thread', 'validate_input');

function validate_input(&$dh)
{
	global $mybb;

	if(validate_email_format($mybb->input['email']))
	{
		$dh->data['email'] = $mybb->input['email'];
	}
	else
	{
		global $lang;

		isset($lang->invalid_email) or $lang->invalid_email = 'Invalid e-mail address.';

		$dh->set_error('invalid_email');
	}
}

You add your data at the insert/update array at "datahandler_post_insert_thread"/"datahandler_post_update_thread".
$plugins->add_hook('datahandler_post_insert_thread', 'do_thread');
$plugins->add_hook('datahandler_post_update_thread', 'do_thread');

function do_thread(&$dh)
{
	global $db;

	if($dh->action == 'thread' && $dh->method == 'insert')
	{
		$dh->thread_insert_data['email'] = $db->escape_string($dh->data['email']);
	}
	elseif($dh->action == 'post' && $dh->method == 'update')
	{
		$dh->thread_update_data['email'] = $db->escape_string($dh->data['email']);
	}
}
if you are just trying to add content to the thread or post table in a new field then just use these hooks

//insert date/time into post/thread vars before commiting
$plugins->add_hook("datahandler_post_insert_post", "delayedpost_add_date2post"); //new post in existing thread
$plugins->add_hook("datahandler_post_insert_thread", "delayedpost_add_date2thread"); //new thread
$plugins->add_hook("datahandler_post_insert_thread_post", "delayedpost_add_date2post"); //new post in new thread

and just add a new array key (as the column name with the variable assigned to it). This is a snipped example

function delayedpost_add_date2post(&$handler)
{
	global $mybb, $db, $lang;

	//if inserting new post
	if($handler->post_insert_data['uid'] != "")
	{
		//update data handler with requested datetime in server time
		$handler->post_insert_data['publishdate'] = $dateline;
			
		//since mybb does not save post and mod options, we need to
		$handler->post_insert_data['draft_poptions'] = serialize($handler->data['options']);
		$handler->post_insert_data['draft_moptions'] = serialize($handler->data['modoptions']);
	}
	
	//if updating a post
	if($handler->post_update_data['uid'] != "")
	{
		//update data handler with requested datetime in server time
		$handler->post_update_data['publishdate'] = $dateline;
		//since mybb does not save post and mod options, we need to
		$handler->post_update_data['draft_poptions'] = serialize($handler->data['options']);
		$handler->post_update_data['draft_moptions'] = serialize($handler->data['modoptions']);
	}

Now if you need to update a separate table at post time then you need to use "newthread_do_newthread_end" and just make $posthandler global in your function and then you can use $posthandler->pid and $posthandler->tid
Thank you all, it worked. Smile