<?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    cns_warnings
 */

/**
 * Hook class.
 */
class Hook_preview_warnings
{
    /**
     * Find whether this preview hook applies.
     *
     * @return array Triplet: Whether it applies, the attachment ID type (may be null), whether the forum DB is used [optional]
     */
    public function applies() : array
    {
        $applies = (addon_installed('cns_warnings')) && (addon_installed('cns_forum')) && (get_page_name() == 'warnings');
        return [$applies, 'warnings', true];
    }

    /**
     * Run function for preview hooks.
     *
     * @return array A pair: The preview, the updated post Comcode (may be null)
     */
    public function run() : array
    {
        require_code('cns_warnings');
        require_code('cns_posts_action');
        require_code('cns_posts_action2');

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

        $message = post_param_string('message', '');

        // Attach punitive actions to end of message
        // TODO: redo this
        $_message = '';
        if ($message != '' && $_message != '') {
            $message .= "\n\n" . $_message;
        } else {
            $message .= $_message;
        }

        // No preview if message is empty
        if ($message == '') {
            return [new Tempcode(), null];
        }

        $message_tempcode = comcode_to_tempcode($message);

        $post_owner = get_member();
        $_post_date = time();
        $post_date = get_timezoned_date_time($_post_date);
        $post_title = do_lang('NEW_WARNING_TO_YOU');
        $whisper_to_member = post_param_integer('member_id');
        $emphasis = do_lang_tempcode('IMPORTANT');
        $class = 'cns-post-emphasis';

        // Member details
        $member_row = $GLOBALS['FORUM_DRIVER']->get_member_row($post_owner);
        $just_member_row = db_map_restrict($member_row, ['id', 'm_signature']);
        $signature = get_translated_tempcode('f_members', $just_member_row, 'm_signature', $GLOBALS['FORUM_DB']);
        $_postdetails_avatar = $GLOBALS['FORUM_DRIVER']->get_member_avatar_url($post_owner);
        if ($_postdetails_avatar != '') {
            $post_avatar = do_template('CNS_TOPIC_POST_AVATAR', ['_GUID' => '2683c09eabd7a9f1fdc57a20117483ef', 'AVATAR' => $_postdetails_avatar]);
        } else {
            $post_avatar = new Tempcode();
        }

        require_code('cns_groups');
        require_code('cns_general');
        require_code('cns_members');
        $poster_title = get_member_title($post_owner);

        // Poster box
        if (!is_guest($post_owner)) {
            require_code('cns_members2');
            $poster_details = render_member_box($post_owner, false, false, [], false);
            $poster_username = $GLOBALS['FORUM_DRIVER']->get_username($post_owner);
            $poster = do_template('CNS_POSTER_MEMBER', [
                '_GUID' => '976a6ceb631bbdcdd950b723cb5d2487',
                'ONLINE' => true,
                'ID' => strval($post_owner),
                'POSTER_DETAILS' => $poster_details,
                'PROFILE_URL' => $GLOBALS['FORUM_DRIVER']->member_profile_url($post_owner, true),
                'POSTER_USERNAME' => $poster_username,
                'HIGHLIGHT_NAME' => false,
            ]);
        } else {
            $poster_details = new Tempcode();
            $custom_fields = do_template('CNS_MEMBER_BOX_CUSTOM_FIELD', [
                '_GUID' => '9cbbc5913d8164970f19c38a210fda95',
                'NAME' => do_lang_tempcode('IP_ADDRESS'),
                'RAW' => get_ip_address(),
                'VALUE' => get_ip_address(),
                'MEMBER_ID' => null,
            ]);
            $poster_details = do_template('CNS_GUEST_DETAILS', ['_GUID' => '2db48e17db9f060c04386843f2d0f105', 'CUSTOM_FIELDS' => $custom_fields]);
            $poster_username = cns_get_safe_specified_poster_name();
            if ((has_actual_page_access(get_member(), 'admin_lookup')) && (addon_installed('securitylogging'))) {
                $ip_url = build_url(['page' => 'admin_lookup', 'type' => 'results', 'param' => get_ip_address()], get_module_zone('admin_lookup'));
            } else {
                $ip_url = new Tempcode();
            }
            $poster = do_template('CNS_POSTER_GUEST', [
                '_GUID' => '9c0ba6198663de96facc7399a08e8281',
                'LOOKUP_IP_URL' => $ip_url,
                'POSTER_DETAILS' => $poster_details,
                'POSTER_USERNAME' => $poster_username,
            ]);
        }

        // Rank images
        $rank_images = new Tempcode();
        $posters_groups = $GLOBALS['FORUM_DRIVER']->get_members_groups($post_owner, true);
        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);
            if ($rank_image != '') {
                $rank_images->attach(do_template('CNS_RANK_IMAGE', [
                    '_GUID' => 'a6a413fc07e05b28ab995b072718b755',
                    'GROUP_NAME' => $group_name,
                    'USERNAME' => $GLOBALS['FORUM_DRIVER']->get_username(get_member()),
                    'IMG' => $rank_image,
                    'IS_LEADER' => $group_leader == get_member(),
                ]));
            }
        }

        $last_edited = new Tempcode();

        $map = [
            '_GUID' => '354473f96b4f7324d2a9c476ff78f0d7',
            'GIVE_CONTEXT' => false,
            'POST_ID' => '',
            'TOPIC_FIRST_POST_ID' => '',
            'TOPIC_FIRST_POSTER' => strval(get_member()),
            'POST_TITLE' => $post_title,
            'CLASS' => $class,
            'EMPHASIS' => $emphasis,
            'FIRST_UNREAD' => '',
            'TOPIC_ID' => '',
            'ID' => '',
            'POST_DATE_RAW' => strval($_post_date),
            'POST_DATE' => $post_date,
            'NOT_VALIDATED' => false,
            'URL' => '',
            'POSTER' => $poster,
            'POST_AVATAR' => $post_avatar,
            'POSTER_TITLE' => $poster_title,
            'RANK_IMAGES' => $rank_images,
            'POST' => $message_tempcode,
            'LAST_EDITED' => $last_edited,
            'SIGNATURE' => $signature,
            'BUTTONS' => '',
            'POSTER_ID' => strval($post_owner),
        ];
        $post = do_template('CNS_TOPIC_POST', $map);
        $out = do_template('CNS_POST_BOX', ['_GUID' => '62bbfabfa5c16c2aa6724a0b79839626', 'GIVE_CONTEXT' => false, 'POST' => $post] + $map + ['ACTUAL_POST' => $message_tempcode]);

        return [$out, null];
    }
}
