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

/**
 * Block class.
 */
class Block_main_contact_catalogues
{
    /**
     * Find details of the block.
     *
     * @return ?array Map of block info (null: block is disabled)
     */
    public function info() : ?array
    {
        $info = [];
        $info['author'] = 'Chris Graham';
        $info['organisation'] = 'Composr';
        $info['hacked_by'] = null;
        $info['hack_version'] = null;
        $info['version'] = 2;
        $info['locked'] = false;
        $info['min_cms_version'] = 11.0;
        $info['addon'] = 'catalogues';
        $info['parameters'] = ['to', 'param', 'subject', 'body_prefix', 'body_suffix', 'subject_prefix', 'subject_suffix', 'redirect', 'guid'];
        return $info;
    }

    /**
     * Find caching details for the block.
     *
     * @return ?array Map of cache details (cache_on and ttl) (null: block is disabled)
     */
    public function caching_environment() : ?array
    {
        $info = [];
        $info['cache_on'] = <<<'PHP'
        (post_param_string('_block_id', '') != '')
        ?
        null // No cache when submitting the form
        :
        [
            md5(get_ip_address() . ' ' . strval(get_member())), // Separate cache by member / IP so CAPTCHA works
            array_key_exists('param', $map) ? $map['param'] : '',
            array_key_exists('to', $map) ? $map['to'] : '',
            array_key_exists('guid', $map) ? $map['guid'] : '',
            array_key_exists('redirect', $map) ? $map['redirect'] : '',
            array_key_exists('subject', $map) ? $map['subject'] : '',
            array_key_exists('body_prefix', $map) ? $map['body_prefix'] : '',
            array_key_exists('body_suffix', $map) ? $map['body_suffix'] : '',
            array_key_exists('subject_prefix', $map) ? $map['subject_prefix'] : '',
            array_key_exists('subject_suffix', $map) ? $map['subject_suffix'] : '',
        ]
PHP;
        $info['ttl'] = 60 * 24 * 7;
        return $info;
    }

    /**
     * Execute the block.
     *
     * @param  array $map A map of parameters
     * @return Tempcode The result of execution
     */
    public function run(array $map) : object
    {
        $error_msg = new Tempcode();
        if (!addon_installed__messaged('catalogues', $error_msg)) {
            return $error_msg;
        }

        require_code('fields');

        require_code('mail');
        require_code('mail_forms');

        $text = new Tempcode();

        // Options...

        if (addon_installed('captcha')) {
            require_code('captcha');
            $use_captcha = ((get_option('captcha_on_feedback') == '1') && (use_captcha()));
        } else {
            $use_captcha = false;
        }

        $catalogue_name = array_key_exists('param', $map) ? $map['param'] : '';
        if ($catalogue_name == '') {
            $catalogue_name = $GLOBALS['SITE_DB']->query_select_value('catalogues', 'c_name'); // Random/arbitrary (first one that comes out of the DB)
        }

        $special_fields = $GLOBALS['SITE_DB']->query_select('catalogue_fields', ['*'], ['c_name' => $catalogue_name], 'ORDER BY cf_order,' . $GLOBALS['SITE_DB']->translate_field_ref('cf_name'));

        $subject = array_key_exists('subject', $map) ? $map['subject'] : '';
        if ($subject == '') {
            $subject = get_translated_text($GLOBALS['SITE_DB']->query_select_value('catalogues', 'c_title', ['c_name' => $catalogue_name]));
        }

        $to_email = array_key_exists('to', $map) ? $map['to'] : '';
        if ($to_email == '') {
            $to_email = null;
        }

        $body_prefix = array_key_exists('body_prefix', $map) ? $map['body_prefix'] : '';
        $body_suffix = array_key_exists('body_suffix', $map) ? $map['body_suffix'] : '';
        $subject_prefix = array_key_exists('subject_prefix', $map) ? $map['subject_prefix'] : '';
        $subject_suffix = array_key_exists('subject_suffix', $map) ? $map['subject_suffix'] : '';

        $block_id = md5(serialize($map));

        // Submission...

        if (post_param_string('_block_id', '') == $block_id) {
            // Check CAPTCHA
            if ($use_captcha) {
                enforce_captcha();
            }

            // Data
            $field_results = [];
            foreach ($special_fields as $field_num => $field) {
                $ob = get_fields_hook($field['cf_type']);
                $inputted_value = $ob->inputted_to_field_value(false, $field, null);

                // Required field validation (a standard for all field hooks except tick)
                if (($field['cf_type'] != 'tick') && ($field['cf_required'] == 1) && (($inputted_value == '') || ($inputted_value === null) || (($inputted_value == STRING_MAGIC_NULL) && !fractional_edit()))) {
                    return do_template('RED_ALERT', ['_GUID' => 'cee591bbaf826cee57a152b641ce831e', 'TEXT' => do_lang_tempcode('_REQUIRED_NOT_FILLED_IN', get_translated_tempcode('catalogue_fields', $field, 'cf_name'))]);
                }

                if ($inputted_value !== null) {
                    $field_results[get_translated_text($field['cf_name'])] = $inputted_value;
                }
            }

            require_code('antispam');
            inject_action_spamcheck(null, post_param_string('email', null, INPUT_FILTER_POST_IDENTIFIER));

            if (addon_installed('stats')) {
                require_code('stats');
                log_stats_event(do_lang('FORM', null, null, null, get_site_default_lang()) . '-' . $catalogue_name);
            }

            // Send e-mail
            form_to_email($subject, $subject_prefix, $subject_suffix, $body_prefix, $body_suffix, $field_results, $to_email, false);

            // Redirect/messaging
            require_code('site2');
            $redirect = array_key_exists('redirect', $map) ? $map['redirect'] : '';
            if ($redirect != '') {
                $redirect = page_link_to_url($redirect);
                redirect_exit($redirect, null, do_lang_tempcode('SUCCESS'));
            } else {
                redirect_exit(get_self_url(), null, do_lang_tempcode('SUCCESS'));
            }
        }

        // Form (note that this needs updated if changed in main_catalogues_form)...

        require_code('form_templates');

        $fields = new Tempcode();

        $hidden = new Tempcode();

        if ($use_captcha) {
            $fields->attach(form_input_captcha($hidden));
            $text->attach(do_lang_tempcode('captcha:FORM_TIME_SECURITY'));
        }

        $field_groups = [];

        url_default_parameters__enable();

        foreach ($special_fields as $field_num => $field) {
            $ob = get_fields_hook($field['cf_type']);

            $_cf_name = get_translated_text($field['cf_name']);
            $field_cat = '';
            $matches = [];
            if (strpos($_cf_name, ': ') !== false) {
                $field_cat = substr($_cf_name, 0, strpos($_cf_name, ': '));
                if ($field_cat . ': ' == $_cf_name) {
                    $_cf_name = $field_cat; // Just been pulled out as heading, nothing after ": "
                } else {
                    $_cf_name = substr($_cf_name, strpos($_cf_name, ': ') + 2);
                }
            }
            if (!array_key_exists($field_cat, $field_groups)) {
                $field_groups[$field_cat] = new Tempcode();
            }

            $_cf_description = escape_html(get_translated_text($field['cf_description']));

            $GLOBALS['NO_DEV_MODE_FULLSTOP_CHECK'] = true;
            $result = $ob->get_field_inputter($_cf_name, $_cf_description, $field, null, true, !array_key_exists($field_num + 1, $special_fields));
            $GLOBALS['NO_DEV_MODE_FULLSTOP_CHECK'] = false;

            if ($result === null) {
                continue;
            }

            if (is_array($result)) {
                $field_groups[$field_cat]->attach($result[0]);
            } else {
                $field_groups[$field_cat]->attach($result);
            }

            if (option_value_from_field_array($field, 'tag') == 'email') {
                $hidden->attach(form_input_hidden('field_tagged__field_' . strval($field['id']), 'email'));
            }
            if (option_value_from_field_array($field, 'tag') == 'name') {
                $hidden->attach(form_input_hidden('field_tagged__field_' . strval($field['id']), 'name'));
            }
            if (option_value_from_field_array($field, 'tag') == 'subject') {
                $hidden->attach(form_input_hidden('field_tagged__field_' . strval($field['id']), 'subject'));
            }

            unset($result);
            unset($ob);
        }

        if (array_key_exists('', $field_groups)) { // Blank prefix must go first
            $field_groups_blank = $field_groups[''];
            unset($field_groups['']);
            $field_groups = array_merge([$field_groups_blank], $field_groups);
        }
        foreach ($field_groups as $field_group_title => $extra_fields) {
            if (is_integer($field_group_title)) {
                $field_group_title = ($field_group_title == 0) ? '' : strval($field_group_title);
            }

            if ($field_group_title != '') {
                $fields->attach(do_template('FORM_SCREEN_FIELD_SPACER', ['_GUID' => 'c0b9f22ef5767da57a1ff65c06af96a1', 'TITLE' => $field_group_title]));
            }
            $fields->attach($extra_fields);
        }

        url_default_parameters__disable();

        $hidden->attach(form_input_hidden('subject', $subject));
        $hidden->attach(form_input_hidden('_block_id', $block_id));

        $url = get_self_url();

        $guid = isset($map['guid']) ? $map['guid'] : '7dc3957edf3b47399b688d72fae54128';

        return do_template('FORM', [
            '_GUID' => $guid,
            'FIELDS' => $fields,
            'HIDDEN' => $hidden,
            'SUBMIT_ICON' => 'buttons/send',
            'SUBMIT_NAME' => do_lang_tempcode('SEND'),
            'URL' => $url,
            'TEXT' => $text,
            'SECONDARY_FORM' => true,
            'ANALYTIC_EVENT_CATEGORY' => $subject,
        ]);
    }
}
