External post

Laric

Active member
From an external script how would I go about creating a thread with the initial post in it?

And the post would be in a forum where I (as the user) don't have access?

(A newly registered user will use an external script to answer a few questions and the results will be posted in the staff section of my forum)
 
Take a look at how it's done in: /library/XenForo/ControllerPublic/Forum.php

This method, in particular:
XenForo_ControllerPublic_Forum::actionAddThread()
 
You can use the datawriter for this;)

I've created an Helper Class for my Add-ons which helps me creating users,threads,posts, etc..

PHP:
**
 * This is my helper collection, to make coders live easier;)
 *
 * @copyright ragtek
 * @version 1.0.2
 * @package ragtek/helper
 */

class Ragtek_Helper_DataWriter
{
    /**
     *
     * @param XenForo_Visitor $user
     * @param <int> $forumId
     * @param <str> $subject
     * @param <str> $message
     * @return <array> Thread Information
     */
    static public function createThread(XenForo_Visitor $user, $forumId, $subject, $message)
    {
        $writer = XenForo_DataWriter::create('XenForo_DataWriter_Discussion_Thread');
        $writer->set('user_id', $user['user_id']);
        $writer->set('username', $user['username']);
        $writer->set('title', $subject);
            $postWriter = $writer->getFirstMessageDw();
            $postWriter->set('message', $message);
        $writer->set('node_id', $forumId);
        $writer->preSave();
        $writer->save();
        return $writer->getMergedData();
    }

  ....
}

//usage:
Ragtek_Helper_DataWriter::createThread($visitor,
                            XenForo_Application::get('options')->ragtekContactThreadForumId,
                            $input['subject'], $input['message']);
YOu can see it here in action => http://xenforo.com/community/posts/88981/
 
Data writers, Models, etc. can be utilized outside of your conventional MVC environment as well. You just need to set up the autoloader and initialize the xf Framework from your script; to make use of xf classes.

PHP:
// Required? Not sure.
chdir('/path/to/xf/root/');

$startTime = microtime(true);
$fileDir = dirname(__FILE__);

require($fileDir . '/library/XenForo/Autoloader.php');
XenForo_Autoloader::getInstance()->setupAutoloader($fileDir . '/library');

// Cannot confirm if starting the application is required or not
XenForo_Application::initialize($fileDir . '/library', $fileDir);
XenForo_Application::set('page_start_time', $startTime);
 
Top Bottom