/**
* Helper for XenForo Thread Datawriter to create easy a thread
* @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();
}
<?php
class EWRmedio_Model_Threads extends XenForo_Model
{
public function buildThread($media)
{
$options = XenForo_Application::get('options');
if (!$options->EWRmedio_autoforum) { return false; }
if (!$forum = $this->getModelFromCache('XenForo_Model_Forum')->getForumById($options->EWRmedio_autoforum))
{
return false;
}
$category = $this->getModelFromCache('EWRmedio_Model_Categories')->getCategoryByID($media['category_id']);
$service = $this->getModelFromCache('EWRmedio_Model_Services')->getServiceByID($media['service_id']);
$keywords = explode(",", $media['media_keywords']);
foreach ($keywords AS &$keyword)
{
$keyword = trim($keyword);
$keyword = "[URL=".XenForo_Link::buildPublicLink('full:media_keyword', array('keyword_text' => $keyword))."]".$keyword."[/URL]";
}
$media['media_keywords'] = implode(", ", $keywords);
$media['duration'] = new XenForo_Phrase(array('x_minutes', 'count' => $media['media_minutes'])) . " " .
new XenForo_Phrase(array('x_seconds', 'time' => $media['media_seconds']));
$writer = XenForo_DataWriter::create('XenForo_DataWriter_Discussion_Thread');
$writer->set('user_id', $media['user_id']);
$writer->set('username', $media['username']);
$writer->set('title', new XenForo_Phrase(array('auto_create_media_thread_title', 'title' => $media['media_title'])));
$writer->set('node_id', $forum['node_id']);
if ($options->EWRmedio_autolock)
{
$writer->set('discussion_open', 0);
}
$postWriter = $writer->getFirstMessageDw();
$postWriter->set('message', new XenForo_Phrase(array(
'auto_create_media_thread_message',
'media' => $media['media_id'],
'date' => date('l, M d, Y', $media['media_date']),
'title' => $media['media_title'],
'description' => $media['media_description'],
'duration' => $media['duration'],
'keywords' => $media['media_keywords'],
'catname' => $category['category_name'],
'srvname' => $service['service_name'],
'username' => $media['username'],
'link' => XenForo_Link::buildPublicLink('full:media', $media),
'catlink' => XenForo_Link::buildPublicLink('full:media_category', $category),
'srvlink' => XenForo_Link::buildPublicLink('full:media_service', $service),
'userlink' => XenForo_Link::buildPublicLink('full:media_user', $media),
)));
$writer->save();
$thread = $writer->getMergedData();
$this->getModelFromCache('XenForo_Model_Thread')->markThreadRead($thread, $forum, XenForo_Application::$time, $media['user_id']);
$dw = XenForo_DataWriter::create('EWRmedio_DataWriter_Media');
$dw->setExistingData($media);
$dw->set('thread_id', $thread['thread_id']);
$dw->save();
return true;
}
public function postToThread($comment, $media)
{
if (!$thread = $this->getModelFromCache('XenForo_Model_Thread')->getThreadById($media['thread_id']))
{
$dw = XenForo_DataWriter::create('EWRmedio_DataWriter_Media');
$dw->setExistingData($media);
$dw->set('thread_id', '0');
$dw->save();
return false;
}
$writer = XenForo_DataWriter::create('XenForo_DataWriter_DiscussionMessage_Post');
$writer->set('user_id', $comment['user_id']);
$writer->set('username', $comment['username']);
$writer->set('message', new XenForo_Phrase(array(
'autoposted_from_comments_x',
'link' => XenForo_Link::buildPublicLink('full:media', $media),
'title' => $media['media_title'],
'message' => $comment['comment_message'],
)));
$writer->set('ip_id', $comment['comment_ip']);
$writer->set('thread_id', $thread['thread_id']);
$writer->save();
return true;
}
public function closeThread($threadID)
{
if (!$thread = $this->getModelFromCache('XenForo_Model_Thread')->getThreadById($threadID))
{
return false;
}
$visitor = XenForo_Visitor::getInstance();
$writer = XenForo_DataWriter::create('XenForo_DataWriter_DiscussionMessage_Post');
$writer->set('user_id', $visitor['user_id']);
$writer->set('username', $visitor['username']);
$writer->set('message', new XenForo_Phrase('auto_create_media_thread_deleted'));
$writer->set('thread_id', $thread['thread_id']);
$writer->save();
$threadWriter = XenForo_DataWriter::create('XenForo_DataWriter_Discussion_Thread');
$threadWriter->setExistingData($thread['thread_id']);
$threadWriter->set('discussion_open', 0);
$threadWriter->save();
}
}
yes, it "could" cause some issues, IF you don't think on all the necessary "sub tasks".Hate to bump a new thread but I've been searching for this for ages so thank you very much Ragtek and Jaxel.
I do have one quick question though, would this work if I was doing this from an external program (not an add-on), or would it be more effective just to insert directly to the database without going through XenForo itself. If I did skip using XenForo to insert a new thread would that cause any issues?
yes, it "could" cause some issues, IF you don't think on all the necessary "sub tasks".
Just check the thread datawriter to see what's he doing.
... check it now... then read the rest here...
As you see, it's "complex", its much easier to use it
And yes, it's possible to do it outside of the xenforo.
We use essential cookies to make this site work, and optional cookies to enhance your experience.