Test Icon
// ============================================= // CONTENT MANAGEMENT - FIXED VERSION 2026-01 // ============================================= function resetContentModal() { document.getElementById('contentModalTitle').textContent = 'Add New Content'; document.getElementById('content-id').value = ''; document.getElementById('content-title').value = ''; document.getElementById('content-type').value = 'document'; document.getElementById('content-status').value = 'published'; document.getElementById('content-public').checked = false; if (tinymce.get('content-description')) { tinymce.get('content-description').setContent(''); } } function loadContentToEdit(id) { fetch(`api/api_endpoints.php?action=get_content&id=${id}`, { headers: { 'X-CSRF-Token': 'ccdd6a536ed2db2d551635af1c5400a19d9b0c9e8068d5100d7cefe42bf9444d' } }) .then(r => r.json()) .then(data => { if (data.success && data.content) { const c = data.content; document.getElementById('contentModalTitle').textContent = 'Edit Content #' + c.id; document.getElementById('content-id').value = c.id; document.getElementById('content-title').value = c.title || ''; document.getElementById('content-type').value = c.type || 'document'; document.getElementById('content-status').value = c.status || 'draft'; document.getElementById('content-public').checked = !!c.is_public; if (tinymce.get('content-description')) { tinymce.get('content-description').setContent(c.description || ''); } new bootstrap.Modal(document.getElementById('contentModal')).show(); } else { alert('Cannot load content: ' + (data.message || 'Unknown error')); } }) .catch(e => alert('Error loading content: ' + e)); } function saveContent() { if (tinymce.get('content-description')) { tinymce.get('content-description').save(); } const form = document.getElementById('content-form'); const formData = new FormData(form); const id = document.getElementById('content-id').value.trim(); const isEdit = !!id && !isNaN(id) && Number(id) > 0; formData.append('action', isEdit ? 'update_content' : 'create_content'); console.log('→ Saving content', isEdit ? '(UPDATE)' : '(CREATE)', 'ID:', id); fetch('api/api_endpoints.php', { method: 'POST', body: formData }) .then(r => r.json()) .then(data => { console.log('← Save response:', data); if (data.success) { alert(data.message || 'Content saved successfully'); bootstrap.Modal.getInstance(document.getElementById('contentModal')).hide(); // Refresh your content list here if you have such function if (typeof refreshMyContent === 'function') refreshMyContent(); } else { alert('Save failed: ' + (data.message || 'Server error')); } }) .catch(e => { console.error(e); alert('Network error while saving content'); }); } // Connect save button document.addEventListener('DOMContentLoaded', () => { document.getElementById('save-content-btn')?.addEventListener('click', saveContent); });