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

Trim and BBCode

Mythotical

Well-known member
#1

In my portal system I need to trim latest news article to a certain amount of characters before the read more link appears, but I can't figure that out as well as I need to convert bbcode for displaying on my portal.

Here is my latest news model:
PHP:
class xPortal_Model_News extends XenForo_Model
{
    public function getNews()
    {
        $nOption = XenForo_Application::get('options')->xportal_news;

        $nThread = $this->limitQueryResults('
            SELECT
                thread.title, thread.first_post_id, thread.username, post.*, user.*
            FROM xf_thread AS thread
            LEFT JOIN xf_post AS post ON (post.post_id = thread.first_post_id)
            LEFT JOIN xf_user AS user ON (user.username = thread.username)
            WHERE thread.node_id = ?
        ', $nOption);

        return $this->_getDb()->fetchRow($nThread, $nOption);
    }
}
Same thing for my latest posts. Here is my latest posts model:
PHP:
class xPortal_Model_LatestPosts extends XenForo_Model
{
    public function getLatestPosts()
    {
        $lPosts = $this->limitQueryResults('
            SELECT post.*, thread.*
            FROM xf_post AS post
            LEFT JOIN xf_thread AS thread ON (thread.thread_id = post.thread_id)
            WHERE message_state = "visible"
            ORDER BY post.post_date
        ', 5);

return $this->_getDb()->fetchAll($lPosts);
    }
}
Thanks in advance
 

James

Well-known member
#4
I'm not sure but that seems accurate. If you search for 'snippet' in your templates it'll probably return a result.

I was thinking instead of trimming in your code, you could just trim in your template using native XenForo syntax.
 
R

ragtek

Guest
#5
That's what i'm using in my global feed add-on:

PHP:
$message = XenForo_Helper_String::wholeWordTrim($thread['message'], $descriptionLenght);
$message = XenForo_Helper_String::bbCodeStrip($message);
 
R

ragtek

Guest
#9
Start reading the code:p

helperSnippet (that's what's called when you use the 'snippet' helper) has an options parameter as 3rd parameter which allows you to strip the bbcode & do other cool stuff...!

PHP:
public static function helperSnippet($string, $maxLength = 0, array $options = array())
    {
        $options = array_merge(array(
            'term' => '',
            'emClass' => '',
            'stripQuote' => false
        ), $options);

        $string = XenForo_Helper_String::bbCodeStrip($string, $options['stripQuote']);

        if ($maxLength)
        {
            $string = XenForo_Helper_String::wholeWordTrimAroundSearchTerm($string, $maxLength, $options['term']);
        }

        $string = trim($string);
        $string = XenForo_Helper_String::censorString($string);

        if ($options['term'] && $options['emClass'])
        {
            return XenForo_Helper_String::highlightSearchTerm($string, $options['term'], $options['emClass']);
        }
        else
        {
            return htmlspecialchars($string);
        }
    }
 

Mythotical

Well-known member
#10
When it comes to the helper stuff I never figured they would have extra options. Thanks for pointing it out.

I'm playing with the new cool toy you just showed me. LOL
 
R

ragtek

Guest
#15
lol
ok i think i've misunderstood you.

YOu want to PARSE it and not to remove it, right?:D

then you'll need the helperBbCode helper method
 

Mythotical

Well-known member
#19
Ok Ragtek, I threw it together again, here is the error I get:
Invalid error type specified in /library/XenForo/Template/Helper/Core.php, line 95
This is what I have in the template:
HTML:
{xen:helper BbCode, $parser, $lPosts}
I have also did this:
HTML:
{xen:helper snippet, {xen:helper BbCode, $parser, $lPosts}, $trim}
Still get the same error above. I read the code but I assume $parser is not correct and I'm at a loss of what the correct variable would be.