<?php
/**
 * Event Form Handler
 *
 * @package HVAC_Community_Events
 */

namespace HVAC_Community_Events;

/**
 * Class Event_Form_Handler
 * 
 * Handles event form submission field mapping
 */
class Event_Form_Handler {
    
    /**
     * Constructor
     */
    public function __construct() {
        add_filter('tec_events_community_submission_form_data', array($this, 'map_description_field'), 10, 1);
        add_filter('tec_events_community_submission_validate_before', array($this, 'map_description_before_validation'), 5, 1);
    }
    
    /**
     * Map tcepostcontent to post_content before validation
     *
     * @param array $submission_data The form submission data
     * @return array Modified submission data
     */
    public function map_description_before_validation($submission_data) {
        // If tcepostcontent exists but post_content doesn't, map it
        if (isset($submission_data['tcepostcontent']) && empty($submission_data['post_content'])) {
            $submission_data['post_content'] = $submission_data['tcepostcontent'];
        }
        
        return $submission_data;
    }
    
    /**
     * Map description field for form data
     *
     * @param array $form_data The form data
     * @return array Modified form data
     */
    public function map_description_field($form_data) {
        // Ensure post_content is set from tcepostcontent
        if (isset($_POST['tcepostcontent']) && empty($_POST['post_content'])) {
            $_POST['post_content'] = $_POST['tcepostcontent'];
            $form_data['post_content'] = $_POST['tcepostcontent'];
        }
        
        return $form_data;
    }
}