<?php /*

 Composr
 Copyright (c) Christopher Graham, 2004-2024

 See docs/LICENSE.md for full licensing information.


 NOTE TO PROGRAMMERS:
   Do not edit this file. If you need to make changes, save your changed file to the appropriate *_custom folder
   **** If you ignore this advice, then your website upgrades (e.g. for bug fixes) will likely kill your changes ****

*/

/**
 * @license    http://opensource.org/licenses/cpal_1.0 Common Public Attribution License
 * @copyright  Christopher Graham
 * @package    core_cns
 */

/**
 * Show a post, isolated of the other posts in it's topic.
 *
 * @param  array $row The post row
 * @param  boolean $use_post_title Whether to include context (i.e. say WHAT this is, not just show the actual content). Also uses the post title, as opposed to the post's topic's title.
 * @param  boolean $give_context Whether to embed a link to the topic
 * @param  boolean $include_breadcrumbs Whether to include breadcrumbs (if there are any)
 * @param  ?AUTO_LINK $root Virtual root to use (null: none)
 * @param  ID_TEXT $guid Overridden GUID to send to templates (blank: none)
 * @param  ?Tempcode $text_summary Text summary for result (e.g. highlighted portion of actual file from search result) (null: none)
 * @param  ID_TEXT $zone The zone to display in
 * @return Tempcode The isolated post
 */
function render_post_box(array $row, bool $use_post_title = false, bool $give_context = true, bool $include_breadcrumbs = true, ?int $root = null, string $guid = '', ?object $text_summary = null, string $zone = '_SEARCH') : object
{
    if ($row === null) { // Should never happen, but we need to be defensive
        return new Tempcode();
    }

    if (!addon_installed('cns_forum')) { // Templates rely on cns_forum to render
        return new Tempcode();
    }

    require_lang('cns');
    require_css('cns');

    require_code('cns_groups');
    require_code('cns_forums');

    static $poster_details_cache = [];
    if (isset($poster_details_cache[$row['p_posting_member']])) {
        list($poster_title, $avatar, $post_avatar, $rank_images, $poster_details, $poster) = $poster_details_cache[$row['p_posting_member']];
    } else {
        // Poster title
        $primary_group = $GLOBALS['FORUM_DRIVER']->get_member_row_field($row['p_posting_member'], 'm_primary_group');
        if ($primary_group !== null) {
            if (addon_installed('cns_member_titles')) {
                $poster_title = $GLOBALS['CNS_DRIVER']->get_member_row_field($row['p_posting_member'], 'm_title');
                if ($poster_title == '') {
                    $poster_title = get_translated_text(cns_get_group_property($primary_group, 'title'), $GLOBALS['FORUM_DB']);
                }
            } else {
                $poster_title = '';
            }
            $avatar = $GLOBALS['FORUM_DRIVER']->get_member_avatar_url($row['p_posting_member']);
            $posters_groups = $GLOBALS['FORUM_DRIVER']->get_members_groups($row['p_posting_member'], true);
        } else {
            $poster_title = '';
            $avatar = '';
            $posters_groups = [];
        }

        // Avatar
        if (is_guest($row['p_posting_member'])) {
            if ($row['p_poster_name_if_guest'] == do_lang('SYSTEM')) {
                $avatar = find_theme_image('cns_default_avatars/system', true);
            }
        }
        if ($avatar != '') {
            $post_avatar = do_template('CNS_TOPIC_POST_AVATAR', ['_GUID' => ($guid != '') ? $guid : 'f5769e8994880817dc441f70bbeb070e', 'AVATAR' => $avatar]);
        } else {
            $post_avatar = new Tempcode();
        }

        // Rank images
        $rank_images = new Tempcode();
        foreach ($posters_groups as $group) {
            $rank_image = cns_get_group_property($group, 'rank_image');
            $group_leader = cns_get_group_property($group, 'group_lead_member');
            $group_name = cns_get_group_name($group);
            $rank_image_pri_only = cns_get_group_property($group, 'rank_image_pri_only');
            if (($rank_image != '') && (($rank_image_pri_only == 0) || ($group == $primary_group))) {
                $rank_images->attach(do_template('CNS_RANK_IMAGE', [
                    '_GUID' => 'ad383e495f77445ddb4d9107a9ebf269',
                    'GROUP_NAME' => $group_name,
                    'USERNAME' => $GLOBALS['FORUM_DRIVER']->get_username($row['p_posting_member']),
                    'IMG' => $rank_image,
                    'IS_LEADER' => $group_leader == $row['p_posting_member'],
                ]));
            }
        }

        // Poster details
        if ((!is_guest($row['p_posting_member'])) && ($primary_group !== null)) {
            require_code('cns_members2');
            $poster_details = render_member_box($row['p_posting_member'], false, false, [], false);
        } else {
            $poster_details = new Tempcode();
        }
        if (addon_installed('cns_forum')) {
            if ((!is_guest($row['p_posting_member'])) && ($primary_group !== null)) {
                require_code('users2');
                if ((!is_guest($row['p_posting_member'])) && ($primary_group !== null)) {
                    $poster = do_template('CNS_POSTER_MEMBER', [
                        '_GUID' => ($guid != '') ? $guid : 'ab1724a9d97f93e097cf49b50eeafa66',
                        'ONLINE' => member_is_online($row['p_posting_member']),
                        'ID' => strval($row['p_posting_member']),
                        'POSTER_DETAILS' => $poster_details,
                        'PROFILE_URL' => $GLOBALS['FORUM_DRIVER']->member_profile_url($row['p_posting_member'], true),
                        'POSTER_USERNAME' => $GLOBALS['FORUM_DRIVER']->get_username($row['p_posting_member']),
                        'HIGHLIGHT_NAME' => null,
                    ]);
                } else {
                    $poster = do_template('CNS_POSTER_GUEST', [
                        '_GUID' => '260a204ec51a3a79896f4e39325f025e',
                        'LOOKUP_IP_URL' => '',
                        'POSTER_DETAILS' => $poster_details,
                        'POSTER_USERNAME' => ($row['p_poster_name_if_guest'] != '') ? $row['p_poster_name_if_guest'] : do_lang('GUEST'),
                    ]);
                }
            } else {
                $poster = do_template('CNS_POSTER_GUEST', [
                    '_GUID' => ($guid != '') ? $guid : 'bb1724a9d97f93e097cf49b50eeafa66',
                    'LOOKUP_IP_URL' => '',
                    'POSTER_DETAILS' => $poster_details,
                    'POSTER_USERNAME' => ($row['p_poster_name_if_guest'] != '') ? $row['p_poster_name_if_guest'] : do_lang('GUEST'),
                ]);
            }
        } else {
            $poster = make_string_tempcode(escape_html(($row['p_poster_name_if_guest'] != '') ? $row['p_poster_name_if_guest'] : do_lang('GUEST')));
        }

        $poster_details_cache[$row['p_posting_member']] = [$poster_title, $avatar, $post_avatar, $rank_images, $poster_details, $poster];
    }

    // Last edited
    if ($row['p_last_edit_time'] !== null) {
        $last_edited = do_template('CNS_TOPIC_POST_LAST_EDITED', [
            '_GUID' => ($guid != '') ? $guid : 'cb1724a9d97f93e097cf49b50eeafa66',
            'LAST_EDIT_DATE_RAW' => ($row['p_last_edit_time'] === null) ? '' : strval($row['p_last_edit_time']),
            'LAST_EDIT_DATE' => get_timezoned_date_time_tempcode($row['p_last_edit_time']),
            'LAST_EDIT_PROFILE_URL' => ($row['p_last_edit_member'] === null) ? '' : $GLOBALS['FORUM_DRIVER']->member_profile_url($row['p_last_edit_member'], true),
            'LAST_EDIT_USERNAME' => ($row['p_last_edit_member'] === null) ? '' : $GLOBALS['FORUM_DRIVER']->get_username($row['p_last_edit_member']),
        ]);
    } else {
        $last_edited = new Tempcode();
    }
    $last_edited_raw = ($row['p_last_edit_time'] === null) ? '' : strval($row['p_last_edit_time']);

    // Breadcrumbs
    $breadcrumbs = null;
    if ($include_breadcrumbs) {
        $breadcrumbs = breadcrumb_segments_to_tempcode(cns_forum_breadcrumbs($row['p_cache_forum_id'], null, null, false, ($root === null) ? get_param_integer('keep_forum_root', null) : $root));
    }

    // Misc stuff
    $poster_id = $row['p_posting_member'];
    $map = ['page' => 'topicview', 'type' => 'findpost', 'id' => $row['id']];
    if ($root !== null) {
        $map['keep_forum_root'] = $root;
    }
    $post_url = build_url($map, $zone);
    $post_url->attach('#post_' . strval($row['id']));
    if ($text_summary === null) {
        $just_post_row = db_map_restrict($row, ['id', 'p_post'], ['id' => 'p_id']);
        $post = get_translated_tempcode('f_posts', $just_post_row, 'p_post', $GLOBALS['FORUM_DB']);
    } else {
        $post = $text_summary;
    }
    $post_date = get_timezoned_date_time_tempcode($row['p_time']);
    $post_date_raw = $row['p_time'];
    if ($use_post_title) {
        $post_title = $row['p_title'];
    } else {
        $post_title = $GLOBALS['FORUM_DB']->query_select_value('f_topics', 't_cache_first_title', ['id' => $row['p_topic_id']]);
        if ($row['p_title'] != $post_title) {
            $post_title .= ': ' . $row['p_title'];
        }
    }

    // Emphasis? PP to?
    list($post_class, $emphasis) = cns_get_post_emphasis($row, null);

    // Feedback
    require_code('feedback');
    actualise_rating(true, 'post', strval($row['id']), get_self_url(), $row['p_title']);
    $rating = display_rating(get_self_url(), $row['p_title'], 'post', strval($row['id']), $give_context ? 'RATING_INLINE_STATIC' : 'RATING_INLINE_DYNAMIC', $row['p_posting_member']);

    // Render
    $map = [
        '_GUID' => ($guid != '') ? $guid : '9456f4fe4b8fb1bf34f606fcb2bcc9d3',
        'ID' => strval($row['id']),
        'GIVE_CONTEXT' => $give_context,
        'TOPIC_FIRST_POST_ID' => '',
        'TOPIC_FIRST_POSTER' => '',
        'POST_ID' => strval($row['id']),
        'URL' => $post_url,
        'CLASS' => $post_class,
        'EMPHASIS' => $emphasis,
        'FIRST_UNREAD' => '',
        'POSTER_TITLE' => $poster_title,
        'POST_TITLE' => $post_title,
        'POST_DATE_RAW' => strval($post_date_raw),
        'POST_DATE' => $post_date,
        'POST' => $post,
        'TOPIC_ID' => ($row['p_topic_id'] === null) ? '' : strval($row['p_topic_id']),
        'LAST_EDITED_RAW' => $last_edited_raw,
        'LAST_EDITED' => $last_edited,
        'POSTER_ID' => strval($poster_id),
        'POSTER' => $poster,
        'POSTER_DETAILS' => $poster_details,
        'POST_AVATAR' => $post_avatar,
        'RANK_IMAGES' => $rank_images,
        'BUTTONS' => '',
        'SIGNATURE' => '',
        'NOT_VALIDATED' => '',
        'DESCRIPTION' => '',
        'PREVIEWING' => true,
        'RATING' => $rating,
    ];

    $_post = do_template('CNS_TOPIC_POST', $map);
    $tpl = do_template('CNS_POST_BOX', [
        '_GUID' => ($guid != '') ? $guid : '9456f4fe4b8fb1bf34f606fcb2bcc9d7',
        'BREADCRUMBS' => $breadcrumbs,
        'POST' => $_post,
    ] + $map + ['ACTUAL_POST' => $post]);

    if ($give_context) {
        $poster = $GLOBALS['FORUM_DRIVER']->member_profile_hyperlink($row['p_posting_member']);
        $date = get_timezoned_date_time($row['p_time']);
        if (array_key_exists('t_cache_first_title', $row)) {
            $topic_row = $row;
        } else {
            $topic_rows = $GLOBALS['FORUM_DB']->query_select('f_topics', ['*'], ['id' => $row['p_topic_id']], '', 1);
            if (array_key_exists(0, $topic_rows)) {
                $topic_row = $topic_rows[0];
            } else {
                $topic_row = ['t_cache_first_title' => ''];
            }
        }
        if ($topic_row['t_cache_first_title'] == '') {
            $topic_post_rows = $GLOBALS['FORUM_DB']->query_select('f_posts', ['p_title', 'p_time'], ['p_topic_id' => $row['p_topic_id']], 'ORDER BY p_time ASC', 1);
            if (array_key_exists(0, $topic_post_rows)) {
                $topic_row['t_cache_first_title'] = $topic_post_rows[0]['p_title'];
            }
        }
        $link = hyperlink($GLOBALS['FORUM_DRIVER']->topic_url($row['p_topic_id'], '', true), $topic_row['t_cache_first_title'], false, true);
        $title = do_lang_tempcode('FORUM_POST_ISOLATED_RESULT', escape_html(strval($row['id'])), $poster, [escape_html($date), $link]);

        return do_template('SIMPLE_PREVIEW_BOX', [
            '_GUID' => ($guid != '') ? $guid : '84ac17a5855ceed1c47c5d3ef6cf4f3d',
            'ID' => strval($row['id']),
            'TITLE' => $title,
            'SUMMARY' => $tpl,
            'RESOURCE_TYPE' => 'post',
        ]);
    }

    return $tpl;
}
