• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

How to user Model

Cory Booth

Well-known member
#1
I'm a bit new to this MVC thing (be thankfull you aren't my partner at work trying to teach me LOL).
So... As not to bother Jaxel as I am sure he is busy, but this is related to his Porta module.
One of his features uses a Class to retrieve "Recent Threads".
As here:

PHP:
public function getModule($options)
    {
        $visitorID = XenForo_Visitor::getUserId();

        $conditions = array(
            'deleted' => false,
            'moderated' => false,
        );

        $fetchOptions = array(
            'join' => XenForo_Model_Thread::FETCH_FORUM | XenForo_Model_Thread::FETCH_USER,
            'readUserId' => $visitorID,
            'postCountUserId' => $visitorID,
            'order' => 'last_post_date',
            'orderDirection' => 'desc',
            'limit' => $options['recentthreads_limit'],
        );

        $threads = $this->getModelFromCache('XenForo_Model_Thread')->getThreads($conditions, $fetchOptions);

        foreach ($threads AS &$thread)
        {
            $thread['content_type'] = 'thread';
            $thread['content_id'] = $thread['thread_id'];
        }

        $threads = $this->getModelFromCache('XenForo_Model_Search')->getViewableSearchResults($threads);

        foreach ($threads AS &$thread)
        {
            $thread = $this->getModelFromCache('XenForo_Model_Thread')->prepareThread($thread, $thread);
            $thread['canInlineMod'] = false;
            $thread['canEditThread'] = false;
            $thread['showForumLink'] = true;
        }

        return $threads;
    }
I am trying to figure out how to reject the threads from a specific node.

I was thinking something like:
PHP:
        foreach ($threads AS &$thread)
        {
if ($thread['node_id'] != 131) {
            $thread['content_type'] = 'thread';
            $thread['content_id'] = $thread['thread_id'];
}
        }
But that causes an error.
Strangely, switching the != to = removes the error, but the expected output is wrong still.

Any ideas?
 

Cory Booth

Well-known member
#2
Getting closer!

PHP:
		$conditions = array(
			'deleted' => false,
			'moderated' => false,
            'forum_id' =>  131,
		);
Except I want it the other way around. forum_id <> 131
 

Jaxel

Well-known member
#3
PHP:
foreach ($threads AS $key => &$thread)
{
    if ($thread['node_id'] == '131')
    {
        unset($threads[$key]);
        continue;
    }

    $thread['content_type'] = 'thread';
    $thread['content_id'] = $thread['thread_id'];
}
 

Cory Booth

Well-known member
#4
PHP:
foreach ($threads AS $key => &$thread)
{
    if ($thread['node_id'] == '131')
    {
        unset($threads[$key]);
        continue;
    }

    $thread['content_type'] = 'thread';
    $thread['content_id'] = $thread['thread_id'];
}
THANKS! That seems to do the trick (except it drops the returns from 12 to 7...)
Hmmm
I'll keep playing... Thanks again - very much!
Yep, the query still loops only 12 times and returns those values <> 131 - but if every new post was in forum_id 131 then there would be no returns.
 

Cory Booth

Well-known member
#5
PHP:
foreach ($threads AS $key => &$thread)
{
    if ($thread['node_id'] == '131')
    {
        unset($threads[$key]);
        continue;
    }

    $thread['content_type'] = 'thread';
    $thread['content_id'] = $thread['thread_id'];
}
What about something up at the conditions?
Like:
PHP:
// 'forum_id' => 26 OR 27 OR 33 OR 34 OR 35 OR 76 OR 77 OR 78 OR 79 OR 92 OR 93 OR 94 OR 103 OR 104 OR 107 OR 109 OR 110 OR 112 OR 113 OR 114 OR 115 OR 117 OR 128 OR 129 OR 130,
I can do forum_id = 131 which will return the forum id 131 in the recent threads, so above I'm trying to send every forum_id except 131.
Still tweaking...
 

Cory Booth

Well-known member
#6
OK, I figured this out but not the way I wish it could be.

So I saw your code Jaxel and like everything else, it works!
Except...
The count of thread array has already been passed, so the total thread that are evaluated doesn't change if the "skipped" forum is found. So if 5 forum threads are skipped and your total count is 10, then you'll only have 5 recent threads.

I discovered the Thread.php prepareThreadConditions function as part of the xenforo system, and it provides a condition to evaluate forum_id which is equated to the node_id. But... The function is a = function so I couldn't figure out how to pass an IN or != or <> or something similar.

So.....

I added
PHP:
        if (!empty($conditions['forumid']))
		{
			$sqlConditions[] = 'thread.node_id < ' . $db->quote($conditions['forumid']);
		}
        if (!empty($conditions['userid']))
		{
			$sqlConditions[] = 'thread.user_id <> ' . $db->quote($conditions['userid']);
		}
And this provided me two extra conditions of a forumid less than....
And a userid <> to....

Don't like modifying core files, but don't know any other way to do this.