/** * HVAC TEC Form Fields Injector * * This script manually injects form fields when TEC Community Events * fails to render them properly. This is a workaround for the TEC bug * where forms render with only hidden fields. * * @package HVAC_Community_Events * @since 2.0.0 */ (function($) { 'use strict'; const HVACFormInjector = { /** * Initialize the form field injector */ init: function() { console.log('[HVAC Form Injector] Initializing...'); // Wait for document ready $(document).ready(() => { // Check if we're on a page with TEC form if ($('form[data-datepicker_format]').length > 0) { this.checkAndInjectFields(); } }); }, /** * Check if form fields exist and inject if missing */ checkAndInjectFields: function() { const $form = $('form[data-datepicker_format]'); if (!$form.length) { console.log('[HVAC Form Injector] No TEC form found'); return; } // Check if form has visible fields const visibleInputs = $form.find('input:visible:not([type="hidden"])').length; const textareas = $form.find('textarea:visible').length; if (visibleInputs === 0 && textareas === 0) { console.log('[HVAC Form Injector] Form has no visible fields, injecting...'); this.injectFormFields($form); } else { console.log('[HVAC Form Injector] Form already has fields'); } }, /** * Inject the missing form fields */ injectFormFields: function($form) { // Create form HTML structure matching TEC's expected layout const formHTML = `

Event Title *

Event Description *

Event Summary (Excerpt)

Brief summary for search results and previews

Event Date & Time *

Venue

Organizer

Event Categories

Event Tags

Separate tags with commas

Featured Image

Upload an image for your event (JPG, PNG, GIF)

`; // Insert the fields into the form $form.append(formHTML); // Load existing venues and organizers via AJAX this.loadVenuesAndOrganizers(); // Set up event handlers this.setupEventHandlers(); console.log('[HVAC Form Injector] Form fields injected successfully'); }, /** * Load venues and organizers via AJAX */ loadVenuesAndOrganizers: function() { // Load venues if (typeof hvac_ajax !== 'undefined') { $.post(hvac_ajax.ajax_url, { action: 'hvac_get_venues', nonce: hvac_ajax.nonce }, function(response) { if (response.success && response.data) { const $select = $('#EventVenueID'); response.data.forEach(venue => { $select.append(``); }); } }); // Load organizers $.post(hvac_ajax.ajax_url, { action: 'hvac_get_organizers', nonce: hvac_ajax.nonce }, function(response) { if (response.success && response.data) { const $select = $('#EventOrganizerID'); response.data.forEach(organizer => { $select.append(``); }); } }); } }, /** * Set up event handlers for dynamic fields */ setupEventHandlers: function() { // Show/hide new venue fields $('#EventVenueID').on('change', function() { if ($(this).val() === 'new') { $('#new-venue-fields').slideDown(); } else { $('#new-venue-fields').slideUp(); } }); // Show/hide new organizer fields $('#EventOrganizerID').on('change', function() { if ($(this).val() === 'new') { $('#new-organizer-fields').slideDown(); } else { $('#new-organizer-fields').slideUp(); } }); // Form submission handler $('form[data-datepicker_format]').on('submit', function(e) { console.log('[HVAC Form Injector] Form submitted'); // Validate required fields const title = $('#EventTitle').val(); const description = $('#EventDescription').val(); const startDate = $('#EventStartDate').val(); const endDate = $('#EventEndDate').val(); if (!title || !description || !startDate || !endDate) { e.preventDefault(); alert('Please fill in all required fields'); return false; } // If REST API is available, use it for submission if (typeof HVACRestEventSubmission !== 'undefined') { e.preventDefault(); HVACRestEventSubmission.submitForm(this); return false; } // Otherwise let the form submit normally return true; }); } }; // Initialize when document is ready $(document).ready(function() { HVACFormInjector.init(); }); // Also try to initialize on window load $(window).on('load', function() { HVACFormInjector.checkAndInjectFields(); }); })(jQuery);