Daily Lucky Numbers:
6
10
21
29
41
42

Article to fetch Latest Post in Topic instead of First Post

Started by Aaron, May 29, 2022, 06:38:34 PM

Previous topic - Next topic

Aaron

Hi, I'm using Ehportal currently which is working great. I was wanting to make the home/portal page to display the latest posts from certain topics (topics only I/the admin can post in), which would then of course display the latest post at the top (regardless of which topic it is).

Is this doable for the Article feature or would it be better instead to make a php/html block for display on the home/portal page instead and specify topics by ID that way?

Thank you

Chen Zhen


I can help you with a PHP block that will contain some specifics for the query.

The ssi_queryPosts function in SSI.php sounds like it will do what you want.
You just need to give it arguments to target your query.

Chen Zhen

#2
This is just an example that you can tweak.

Create a board that has a profile setting of read only which will only allow admins to post in it.
Make your posts in that board that you want to appear on your front page block.

Now create a PHP block with the following code in it:
$admin_group_id = 0;
$admin_ids = array(1);
$board_ids = array(1);
$board_profile = 4;

//$query_where = 'mem.id_member IN ({array_int:admin_ids}) AND FIND_IN_SET({int:groupid}, b.member_groups) != 0 AND b.id_profile = {int:read_only} AND m.id_board IN ({array_int:board_ids})';

$query_where = 'mem.id_member IN ({array_int:admin_ids}) AND FIND_IN_SET({int:groupid}, b.member_groups) != 0 AND b.id_profile = {int:read_only}';
$query_where_params = array('groupid' => $admin_group_id, 'admin_ids' => $admin_ids, 'board_ids' => $board_ids, 'read_only' => $board_profile);
$query_limit = 10;
$query_order = 'm.id_msg DESC';
$limit_body = true;
$override_permissions = false;

$queryPosts = ehPortal_queryPosts($query_where, $query_where_params, $query_limit, $query_order, $output_method, $limit_body, $override_permissions);

function ehPortal_queryPosts($query_where, $query_where_params, $query_limit, $query_order, $limit_body, $override_permissions)
{
    global $scripturl, $txt, $user_info;
    global $modSettings, $smcFunc, $context;

    if (!empty($modSettings['enable_likes']))
        $context['can_like'] = allowedTo('likes_like');

    // Find all the posts. Newer ones will have higher IDs.
    $request = $smcFunc['db_query']('substring', '
        SELECT
            m.poster_time, m.subject, m.id_topic, m.id_member, m.id_msg, m.id_board, m.likes, b.name AS board_name, b.member_groups, b.id_profile,
            COALESCE(mem.real_name, m.poster_name) AS poster_name, ' . ($user_info['is_guest'] ? '1 AS is_read, 0 AS new_from' : '
            COALESCE(lt.id_msg, lmr.id_msg, 0) >= m.id_msg_modified AS is_read,
            COALESCE(lt.id_msg, lmr.id_msg, -1) + 1 AS new_from') . ', ' . ($limit_body ? 'SUBSTRING(m.body, 1, 384) AS body' : 'm.body') . ', m.smileys_enabled
        FROM {db_prefix}messages AS m
            INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)' . ($modSettings['postmod_active'] ? '
            INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)' : '') . '
            LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)' . (!$user_info['is_guest'] ? '
            LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = m.id_topic AND lt.id_member = {int:current_member})
            LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = m.id_board AND lmr.id_member = {int:current_member})' : '') . '
        WHERE 1=1 ' . ($override_permissions ? '' : '
            AND {query_wanna_see_board}') . ($modSettings['postmod_active'] ? '
            AND m.approved = {int:is_approved}
            AND t.approved = {int:is_approved}' : '') . '
        ' . (empty($query_where) ? '' : 'AND ' . $query_where) . '
        ORDER BY ' . $query_order . '
        ' . ($query_limit == '' ? '' : 'LIMIT ' . $query_limit),
        array_merge($query_where_params, array(
            'current_member' => $user_info['id'],
            'is_approved' => 1,
        ))
    );
    $posts = array();
    while ($row = $smcFunc['db_fetch_assoc']($request))
    {
        $row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']);

        // Censor it!
        censorText($row['subject']);
        censorText($row['body']);

        $preview = strip_tags(strtr($row['body'], array('<br>' => '&#10;')));

        // Build the array.
        $posts[$row['id_msg']] = array(
            'id' => $row['id_msg'],
            'board' => array(
                'id' => $row['id_board'],
                'name' => $row['board_name'],
                'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
                'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['board_name'] . '</a>'
            ),
            'topic' => $row['id_topic'],
            'poster' => array(
                'id' => $row['id_member'],
                'name' => $row['poster_name'],
                'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'],
                'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>'
            ),
            'subject' => $row['subject'],
            'short_subject' => shorten_subject($row['subject'], 25),
            'preview' => $smcFunc['strlen']($preview) > 128 ? $smcFunc['substr']($preview, 0, 128) . '...' : $preview,
            'body' => $row['body'],
            'time' => timeformat($row['poster_time']),
            'timestamp' => $row['poster_time'],
            'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . ';topicseen#new',
            'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'] . '" rel="nofollow">' . $row['subject'] . '</a>',
            'new' => !empty($row['is_read']),
            'is_new' => empty($row['is_read']),
            'new_from' => $row['new_from'],
        );

        // Get the likes for each message.
        if (!empty($modSettings['enable_likes']))
            $posts[$row['id_msg']]['likes'] = array(
                'count' => $row['likes'],
                'you' => in_array($row['id_msg'], prepareLikesContext($row['id_topic'])),
                'can_like' => !$context['user']['is_guest'] && $row['id_member'] != $context['user']['id'] && !empty($context['can_like']),
            );
    }
    $smcFunc['db_free_result']($request);   

    echo '
        <div style="width: 100%;overflow: hidden;">
            <div style="width: 100%;text-align: center;margin: 0 auto;color: blue;font-size: 1.5em;text-decoration: underline;padding: 1em;">IMPORTANT POSTS</div>';
    foreach ($posts as $post) {
        $date = date('Y-m-d', $post['timestamp']);
        echo '
            <div>Board: ' . $post['board']['link'] . '</div>
            <div>Topic: ' . $post['link'] . '</div>
            <div>Author: ' . $post['poster']['link'] . '</div>
            <div>Date: ' . $date . '</div>
            <hr />';
    }
    echo '
        </div>';
}



You can change the $query_where, $query_where_params and the database SELECT to filter what you're trying to display.
The above finds the latest 10 posts from member id #1 (more can be added to the array) & admin group id 0 in boards set to read-only. 
I put in $board_ids to help show you as an example but the above query doesn't use it because that string is commented out.

Aaron

Hi thank you very much it works perfect!