TP-Docs
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media

Recent

Welcome to TinyPortal. Please login or sign up.

Members
Stats
  • Total Posts: 195,787
  • Total Topics: 21,281
  • Online today: 65
  • Online ever: 8,223 (February 19, 2025, 04:35:35 AM)
Users Online
  • Users: 0
  • Guests: 48
  • Total: 48

Promote to Front Page

Started by jernatety, December 17, 2018, 02:51:44 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

sgm09

Here's the code for the promoted topics block, as generated by chatgpt.
global $context, $smcFunc, $scripturl, $settings, $txt, $modSettings;

// Number of promoted topics to show
$promoted_limit = 15;

// Check if there are promoted topics
if (!empty($context['TPortal']['frontpage_topics']))
{
    $request = $smcFunc['db_query']('', '
        SELECT t.id_topic, t.id_first_msg, m.subject, m.id_member, m.poster_name, m.poster_time
        FROM {db_prefix}topics AS t
        INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
        WHERE t.id_topic IN ({array_int:topics})
        ORDER BY m.poster_time DESC
        LIMIT {int:limit}',
        array(
            'topics' => array_map('intval', explode(',', $context['TPortal']['frontpage_topics'])),
            'limit' => $promoted_limit,
        )
    );

    $topics = array();

    while ($row = $smcFunc['db_fetch_assoc']($request))
    {
        $topics[] = $row;
    }
    $smcFunc['db_free_result']($request);

    echo '
    <div style="display: flex; flex-wrap: wrap; gap: 1rem; width: 100%;">';

    foreach ($topics as $topic)
    {
        $member_id = (int) $topic['id_member'];
        $avatar_img = '';

        if (!empty($member_id)) {
            // Query avatar info directly
            $request2 = $smcFunc['db_query']('', '
                SELECT avatar, email_address
                FROM {db_prefix}members
                WHERE id_member = {int:id_member}
                LIMIT 1',
                array(
                    'id_member' => $member_id,
                )
            );

            if ($request2) {
                if ($row2 = $smcFunc['db_fetch_assoc']($request2)) {
                    $avatar_data = set_avatar_data(array(
                        'avatar' => $row2['avatar'],
                        'email' => $row2['email_address'],
                    ));

                    if (!empty($avatar_data['image'])) {
                        $avatar_img = $avatar_data['image'];
                    }
                }
                $smcFunc['db_free_result']($request2);
            }
        }

        // Fallback default avatar
        if (empty($avatar_img)) {
            $avatar_img = '<img src="' . $settings['images_url'] . '/default_avatar.png" alt="" style="width:50px;height:50px;border-radius:50%;">';
        }

        echo '
        <div style="display: flex; align-items: center; border: 1px solid #ccc; border-radius: 8px; padding: 10px; width: 300px; box-sizing: border-box;">
            <div style="flex-shrink: 0; margin-right: 10px;">
                <a href="', $scripturl, '?action=profile;u=', $member_id, '">', $avatar_img, '</a>
            </div>
            <div style="flex-grow: 1;">
                <div style="font-weight: bold; margin-bottom: 5px;">
                    <a href="', $scripturl, '?topic=', $topic['id_topic'], '.0">', $topic['subject'], '</a>
                </div>
                <div style="font-size: smaller; color: #666;">', $txt['by'], ' ', htmlspecialchars($topic['poster_name']), ' | ', timeformat($topic['poster_time']), '</div>
            </div>
        </div>';
    }

    echo '
    </div>';

}
else
{
    echo '<div class="smalltext">No promoted topics available.</div>';
}

@rjen

I do like the idea, althought there are some layout details I would change...
Although the 'Promote to Frontpage' was originally used for frontpage display, we could 'revamp' the function in a future release.

What would make it more universal:
- rename the 'Promote to Frontpage' to 'Make Promoted Topic'
- I can add an extra option in Article settings to allow switching the Promotion on and off (regardless of the Frontpage setting)
- I would add a standard block to Tinyportal to show Promoted topics

For the block I would like to use your input as the starting point. Would that be ok for you?
Running Latest TP on SMF2.1 at: www.fjr-club.nl

sgm09

Sure, it's just some chatgpt generated code. Here's an even nicer one that works on sticky posts, and retrieves the first img it finds in the post, or else falls back to avatar (was meant for movie posters).

global $context, $smcFunc, $scripturl, $settings, $txt, $modSettings;
// Number of sticky topics to show
$sticky_limit = 15;

// Query to retrieve sticky topics and their message bodies
$request = $smcFunc['db_query']('', '
    SELECT t.id_topic, t.id_first_msg, m.subject, m.body, m.id_member, m.poster_name, m.poster_time,
           mem.real_name
    FROM {db_prefix}topics AS t
    INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
    LEFT JOIN {db_prefix}members AS mem ON (m.id_member = mem.id_member)
    WHERE t.is_sticky = 1
    ORDER BY m.poster_time DESC
    LIMIT {int:limit}',
    array(
        'limit' => $sticky_limit,
    )
);

$topics = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
{
    $topics[] = $row;
}
$smcFunc['db_free_result']($request);

if (!empty($topics))
{
    echo '
    <div style="display: flex; flex-wrap: wrap; gap: 0.75rem; width: 100%;">';
    foreach ($topics as $topic)
    {
        // Extract image from [img] BBCode in the message body
        $image_html = '<img src="' . $settings['images_url'] . '/default_avatar.png" alt="" style="width:60px;height:60px;object-fit:cover;">';

        if (!empty($topic['body'])) {
            if (preg_match('/\[img(?:[^\]]*)\](.*?)\[\/img\]/i', $topic['body'], $matches)) {
                $img_url = trim($matches[1]);
                if (stripos($img_url, 'http') === 0) {
                    $image_html = '<img src="' . htmlspecialchars($img_url) . '" alt="" style="width:60px;height:88px;object-fit:cover;">';
                }
            }
        }

        $member_id = (int) $topic['id_member'];

        echo '
        <div style="display: flex; align-items: center; border: 1px solid #ccc; border-radius: 6px; padding: 2px; width: 280px; box-sizing: border-box;">
            <div style="flex-shrink: 0; margin-right: 6px;">
                <a href="', $scripturl, '?action=profile;u=', $member_id, '">', $image_html, '</a>
            </div>
            <div style="flex-grow: 1;">
                <div style="font-weight: bold;">
                    <a href="', $scripturl, '?topic=', $topic['id_topic'], '.0">', $topic['subject'], '</a>
                </div>
                <div style="font-size: smaller; color: #666; margin-top: 2px;">', $txt['by'], ' <a href="', $scripturl, '?action=profile;u=', $member_id, '">', !empty($topic['real_name']) ? htmlspecialchars($topic['real_name']) : htmlspecialchars($topic['poster_name']), '</a> | ', timeformat($topic['poster_time']), '</div>
            </div>
        </div>';
    }
    echo '
    </div>';
}
else
{
    echo '<div class="smalltext">No sticky topics available.</div>';
}


@rjen

Thanks, the blockcodes have some issues with the avatars, and as far as I can see the code also does not respect the member permissions, meaning that the blocks will show topics to users that do not have permissions to view the boards.

This may work for some users, but not all.

Will have a look
Running Latest TP on SMF2.1 at: www.fjr-club.nl

sgm09

Well that depends on each forum's use case, if they want or not to show topics based on permissions. It will render the title and author, but won't allow actual access to topic. Didn't have any issues with the avatars over here.

@rjen

The avatar issue is most likely because my forum has custom avatars folders
Running Latest TP on SMF2.1 at: www.fjr-club.nl

@rjen

I made some updates to the beta version for TinyPortal 3.0.3

Functional changes:
- new setting in the main settings to activate the Promote Topic button
- new Block type that can be used to show Promoted topics, similar to Recent topics

I am playing around with the new block, may update the layout and add some more settings.
Obviously you can use the promoted topics in your own code as you started...
Running Latest TP on SMF2.1 at: www.fjr-club.nl

This website is proudly hosted on Crocweb Cloud Website Hosting.