MyBB Community Forums
Post handler - Printable Version

+- MyBB Community Forums (http://community.mybb.com)
+-- Forum: Resources (/forum-8.html)
+--- Forum: Plugins (/forum-73.html)
+---- Forum: Plugin Development (/forum-68.html)
+---- Thread: Post handler (/thread-114493.html)



Post handler - Mr Josh - 03-05-2012 12:46 PM

How do I create a post with the post handler located in inc/datahandlers/post.php

Is there a documentation or something?

Thanks Smile


RE: Post handler - ranjani - 03-05-2012 12:58 PM

you may trace a couple of plugins related to automatic posting .. eg. welcome topic , advanced reports ..


RE: Post handler - Euan T - 03-05-2012 03:58 PM

Something along this sort of lines ought to work, though I haven't tested it:

PHP Code:
<?php
require_once MYBB_ROOT."inc/datahandlers/post.php";
$posthandler = new PostDataHandler("insert");

$post = array(
    
"tid" => 0// Thread ID
    
"replyto" => 0// Reply to ID
    
"fid" => 0// Forum ID
    
"subject" => ''// Post subject
    
"icon" => ''// Post icon
    
"uid" => 1// User ID
    
"username" => ''// Username
    
"message" => ''// Message
    
"ipaddress" => get_ip(),
);


$post['options'] = array(
    
"signature" => 1,
    
"subscriptionmethod" => 1,
    
"disablesmilies" => 0
);

$posthandler->set_data($post);

$valid_post $posthandler->validate_post();

$post_errors = array();

if(!
$valid_post)
{
    
$post_errors $posthandler->get_friendly_errors();
}

$posthandler->insert_post();
?>



RE: Post handler - seeker - 03-06-2012 03:47 AM

(03-05-2012 03:58 PM)euantor Wrote:  Something along this sort of lines ought to work, though I haven't tested it:
.......

I will test it ASAP, thank you euantor.
If you ever find the time, could you please post similar code for a new thread?


RE: Post handler - Beardy - 03-06-2012 05:12 AM

(03-06-2012 03:47 AM)seeker Wrote:  [...] could you please post similar code for a new thread?

Code for inserting a new thread is a lot similar to the code for inserting a new post (pieced together from newthread.php, also untested):

PHP Code:
<?php
require_once MYBB_ROOT."inc/datahandlers/post.php";
$posthandler = new PostDataHandler("insert");
$posthandler->action "thread";

$new_thread = array(
    
"fid" => 0// Forum ID to create thread
    
"subject" => ''// Subject
    
"prefix" => ''// Thread prefix
    
"icon" => ''// Thread Icon
    
"uid" => 0// Poster ID
    
"username" => ''// Poster Username
    
"message" => ''// Post message
    
"ipaddress" => get_ip(), 
);


$new_thread['options'] = array(
    
"signature" => 1,
    
"subscriptionmethod" => 1,
    
"disablesmilies" => 0
);

$posthandler->set_data($new_thread);

$valid_thread $posthandler->validate_post();

$post_errors = array();

if(!
$valid_thread)
{
    
$post_errors $posthandler->get_friendly_errors();
}

if(
count($post_errors) > 0)
{
    
$thread_errors inline_error($post_errors);
    
$mybb->input['action'] = "newthread";        

else 
{
    
$posthandler->insert_thread();
}
?>

Also, for future reference, if you have a program that is able to search across multiple files (like Notepad++'s find in files feature), you can search for a function and see how it has been used across the mybb core or in other plugins.

In this case, searching for something like '$posthandler->insert_' would show up both cases where the $posthandler was used to insert new posts and threads.


RE: Post handler - seeker - 03-07-2012 06:53 AM

"...a program that is able to search across multiple files..."
I've searched through the code many times, but having both these here is a good way to save time.
Thanks Beardy.