MODx & Vbulletin 3.8.x - the latest forum posts

    Recently I wrote how to merge vbulletin and MODx users, but what is the use of this if the connection between the forum and the site is only in users? I would like some practical application of this merger, and to warm up, we will draw the output of the last N messages from the forum on the site.

    Easy peasy


    I want to say right away that this can be implemented at any time, and this article does not depend on the previous one.

    To begin with, we need to decide how messages will look on the site - in the form of a table? As a rule, these are consecutive lines of records indicating the date and time, author, topic and section. Another option is the first M characters of the message, to completely interest visitors.
    Therefore, we will create a forum_last_messages chunk and enter the view for each of the entries in it:
    [+threadtitle+] ([+replycount+]) от [+lastposter+] в [+postdate+]

    /[+forum+]

    [+shortmessage+]

    Actually, we designated a div with the class forum_message, so that later it can be made beautiful in css styles and format the text inside to fit our needs. Next, we insert a link to the forum indicating the display of the thread with the last message specified, its name and the number of responses in it. Also, if necessary, we can decorate the last flooding in the lodge.
    Next is a paragraph with the name of the section - made in such a way as not to put a tag

    - in the same css styles, you can format this paragraph using the selector .forum_message p {}. Well, then insert the placeholder of the cropped forum message. Personally, I did not need it, but I did it - the template engine will successfully process it and replace it with an empty space.

    Now the most interesting and at the same time simple. One request to the forum, one MODx method, one cycle and the forum_last_messages snippet is ready!

    config['Database']['dbname'])) ? 'FORUMBASE' : $vbulletin->config['Database']['dbname'] ;
    //тут надо пояснить: скорее всего база форума и сайта - различные, поэтому нам надо знать имя форумной базы 
    $forum_prefix =  (empty($vbulletin->config['Database']['tableprefix'])) ? 'vb_' : $vbulletin->config['Database']['tableprefix'] ;
    $count= (empty($count) || ($count<2)) ? 10 : $count; //кол-во сообщений для вывода, по умолчанию  10
    $sql  = 'SELECT t.title as topic, t.lastpostid, t.lastpost, t.lastposter, t.forumid, t.replycount, t.dateline, f.title as forum
    FROM `'.$forum_base.'`.`'.$forum_prefix.'thread` t, `'.$forum_base.'`.`'.$forum_prefix.'forum` f
    WHERE t.`visible` =1
    AND t.`open` =1
    AND f.forumid = t.forumid
    AND t.forumid NOT 
    IN (
        SELECT forumid
        FROM `'.$forum_base.'`.`'.$forum_prefix.'forumpermission`
    )
    ORDER BY t.lastpost DESC 
    LIMIT '.$count.';';
    $res = $modx->db->query($sql);
    $txt = '';
    while ($f_res = $modx->db->getRow($res, 'assoc')) {
    //элементы массива названы вполне себе понятно
    $txt .= $modx->parseChunk('forum_last_messages', 
        array(
            'forumlink' => $forumlink,
            'postid' => $f_res['lastpostid'],                       
            'threadtitle' => $f_res['topic'],                         
            'replycount' => $f_res['replycount'],
            'lastposter' => $f_res['lastposter'],
            'forum' => $f_res['forum'],
            'postdate' => date("H:i", $f_res['lastpost'])   //формат даты
            ),
        '[+',
        '+]'
        );
    }
    return $txt;
    ?>
    

    It seems to me that the code is ugly and simple :)

    In the series looper, I did not trim the text of the post for the simple reason that the text of the last message was not stored in the thread table, and to get it, you also need to pull the post table, which will affect performance.

    So, now using [! Forum_last_messages? Count = `17`!] On the site, we get a list of the latest fresh topics on the forum!

    By the way: Request to the forum selects all OPENfor anonymous topics and not a single closed one. Honestly, this is both a flaw and the fifth wheel, because if there is a merger with the forum and the user is logged in, we can easily find out which topics he has access to and which ones not, and select them. But is it necessary to make such movements, if it is easier to go to the personal account on the forum and see subscriptions to your favorite topics?

    Also popular now: