Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
Options

Recent discussions code

edited April 2015 in Vanilla 2.0 - 2.8

I have some code that shows recent discussions on my main site from the Vanilla forum.

I want it to only show the latest posts but currently it also shows the original thread mixed into the list too. Can someone help there?

Also, it shows the data next to each item, is there a way to show that in "Hours ago"/"Days ago" format?

Hope you can help, cheers guys.

/**
     * Get latest vanilla discussions
     * @param  integer $limit
     * @return array
     */
    public function getLatestDiscussions($limit)
    {
        $discussions = [];
        $displayMaxSub = 1;

        // No trailing slash
        $vanillaUrl = url('forum');

        $jsonStr = file_get_contents($vanillaUrl . '/discussions.json');

        if (!$jsonStr)
        {
            return [];
        }

        $json = json_decode($jsonStr, true);
        $i = 0;

        $discussions_count = count($json['Discussions']);

        foreach ($json['Discussions'] as $discussion)
        {
            if ($i >= $limit) break;

            $discussions[] = [
                'id'             => $discussion['DiscussionID'],
                'subject'        => $discussion['Name'],
                'body'           => $discussion['Body'],
                'username'       => $discussion['FirstName'],
                'date'           => $discussion['DateInserted'],
                'link'           => url('forum/discussion/' . $discussion['DiscussionID']),
            ];

            if ($discussion['CountComments'] > 1)
            {
                $jsonStr_single = file_get_contents($vanillaUrl . '/discussion.json?DiscussionID=' . $discussion['DiscussionID']);

                if ($jsonStr_single)
                {
                    $json_single = json_decode($jsonStr_single, true);

                    if (isset($json_single['Comments']))
                    {
                        $addedComments = 1;

                        $comments = array_reverse($json_single['Comments']);
                        foreach ($comments as $comment)
                        {
                            if ($addedComments === $displayMaxSub || $i === $limit) break;

                            $discussions[] = [
                                'id'       => $discussion['DiscussionID'],
                                'subject'  => 'RE: ' . $discussion['Name'],
                                'body'     => $comment['Body'],
                                'username' => $comment['InsertName'],
                                'date'     => $comment['DateInserted'],
                                'link'     => $this->generateCommentLink($comment['CommentID']),
                            ];

                            $addedComments++;
                            $i++;
                        }
                    }
                }
            }

            $i++;
        }

        return $discussions;
    }

Comments

Sign In or Register to comment.