Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/portal/modules/directory/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/portal/modules/directory/standalone-add.php

<?php
require_once '../../config/config.php';
require_once '../../classes/DirectoryManager.php';

// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
    header('Location: ../../login.php');
    exit;
}

$page_title = "Add/Edit Directory Entry";
$directoryManager = new DirectoryManager();

// Check if editing
$editing = isset($_GET['id']);
$entry = null;

if ($editing) {
    $entry = $directoryManager->getEntryById($_GET['id']);
    if (!$entry) {
        header('Location: standalone.php');
        exit;
    }
}

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $data = [
        'title' => $_POST['title'] ?? null,
        'first_name' => $_POST['first_name'],
        'middle_name' => $_POST['middle_name'] ?? null,
        'last_name' => $_POST['last_name'],
        'gender' => $_POST['gender'],
        'phone' => $_POST['phone'] ?? null,
        'email' => $_POST['email'] ?? null,
        'address_line' => $_POST['address_line'] ?? null,
        'city' => $_POST['city'] ?? null,
        'area_id' => !empty($_POST['area_id']) ? $_POST['area_id'] : null,
        'district_id' => !empty($_POST['district_id']) ? $_POST['district_id'] : null,
        'assembly_id' => !empty($_POST['assembly_id']) ? $_POST['assembly_id'] : null,
        'occupation' => $_POST['occupation'] ?? null,
        'position' => $_POST['position'] ?? null,
        'notes' => $_POST['notes'] ?? null,
        'photo' => null, // Handle photo upload if needed
        'created_by' => $_SESSION['user_id']
    ];
    
    if ($editing) {
        if ($directoryManager->updateEntry($_GET['id'], $data)) {
            header('Location: standalone.php');
            exit;
        } else {
            $error = "Failed to update entry.";
        }
    } else {
        if ($directoryManager->addEntry($data)) {
            header('Location: standalone.php');
            exit;
        } else {
            $error = "Failed to add entry.";
        }
    }
}

// Get database connection
$db = Database::getInstance()->getConnection();

// Get areas, districts, assemblies for dropdowns
$areas_query = "SELECT * FROM areas ORDER BY area_name";
$stmt = $db->query($areas_query);
$areas = $stmt->fetchAll();

$districts_query = "SELECT * FROM districts ORDER BY district_name";
$stmt = $db->query($districts_query);
$districts = $stmt->fetchAll();

// Get assemblies based on district if editing
$assemblies = [];
if ($editing && !empty($entry['district_id'])) {
    $assemblies_query = "SELECT * FROM assemblies WHERE district_id = :district_id ORDER BY assembly_name";
    $stmt = $db->prepare($assemblies_query);
    $stmt->execute([':district_id' => $entry['district_id']]);
    $assemblies = $stmt->fetchAll();
}

$titles = ['Rev.', 'Pastor', 'Elder', 'Deacon', 'Deaconess', 'Mr.', 'Mrs.', 'Miss', 'Ms.', 'Dr.', 'Prof.', 'Hon.'];

include '../../includes/header.php';
include '../../includes/sidebar.php';
?>

<!-- Main Content -->
<main class="main-content md:ml-64 pt-16">
    <div class="min-h-screen bg-gray-50 py-8">
        <div class="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8">
        <!-- Header -->
        <div class="mb-8">
            <div class="gradient-bg rounded-xl shadow-lg p-8 text-white">
                <div class="flex items-center justify-between">
                    <div>
                        <h1 class="text-3xl font-bold mb-2"><?= $editing ? 'Edit' : 'Add New' ?> Directory Entry</h1>
                        <p class="text-blue-100">Fill in the details below</p>
                    </div>
                    <a href="standalone.php" class="bg-white text-primary px-6 py-3 rounded-lg font-semibold hover:bg-blue-50 transition-all duration-300 shadow-md inline-flex items-center">
                        <svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18"/>
                        </svg>
                        Back to List
                    </a>
                </div>
            </div>
        </div>

        <!-- Error Message -->
        <?php if (isset($error)): ?>
        <div class="bg-red-50 border border-red-200 text-red-800 px-6 py-4 rounded-lg mb-6 flex items-center">
            <svg class="w-5 h-5 mr-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/>
            </svg>
            <?= $error ?>
        </div>
        <?php endif; ?>

        <!-- Form -->
        <form method="POST" action="" class="space-y-6">
            <!-- Personal Information -->
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="px-6 py-4 bg-gradient-primary text-white">
                    <h2 class="text-xl font-bold flex items-center">
                        <svg class="w-6 h-6 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"/>
                        </svg>
                        Personal Information
                    </h2>
                </div>
                <div class="p-6 space-y-4">
                    <div class="grid grid-cols-1 md:grid-cols-4 gap-4">
                        <div>
                            <label class="block text-sm font-semibold text-gray-700 mb-2">Title</label>
                            <select name="title" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-primary">
                                <option value="">Select</option>
                                <?php foreach ($titles as $title): ?>
                                    <option value="<?= $title ?>" <?= ($entry['title'] ?? '') == $title ? 'selected' : '' ?>>
                                        <?= $title ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div>
                            <label class="block text-sm font-semibold text-gray-700 mb-2">First Name *</label>
                            <input type="text" name="first_name" value="<?= htmlspecialchars($entry['first_name'] ?? '') ?>" required class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-primary">
                        </div>
                        <div>
                            <label class="block text-sm font-semibold text-gray-700 mb-2">Middle Name</label>
                            <input type="text" name="middle_name" value="<?= htmlspecialchars($entry['middle_name'] ?? '') ?>" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-primary">
                        </div>
                        <div>
                            <label class="block text-sm font-semibold text-gray-700 mb-2">Last Name *</label>
                            <input type="text" name="last_name" value="<?= htmlspecialchars($entry['last_name'] ?? '') ?>" required class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-primary">
                        </div>
                    </div>
                    
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <div>
                            <label class="block text-sm font-semibold text-gray-700 mb-2">Gender *</label>
                            <select name="gender" required class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-primary">
                                <option value="">Select Gender</option>
                                <option value="Male" <?= ($entry['gender'] ?? '') == 'Male' ? 'selected' : '' ?>>Male</option>
                                <option value="Female" <?= ($entry['gender'] ?? '') == 'Female' ? 'selected' : '' ?>>Female</option>
                            </select>
                        </div>
                        <div>
                            <label class="block text-sm font-semibold text-gray-700 mb-2">Occupation</label>
                            <input type="text" name="occupation" value="<?= htmlspecialchars($entry['occupation'] ?? '') ?>" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-primary">
                        </div>
                    </div>
                    
                    <div>
                        <label class="block text-sm font-semibold text-gray-700 mb-2">Position/Role</label>
                        <input type="text" name="position" value="<?= htmlspecialchars($entry['position'] ?? '') ?>" placeholder="e.g., Board Member, Consultant, etc." class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-primary">
                    </div>
                </div>
            </div>

            <!-- Contact Information -->
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="px-6 py-4 bg-gradient-secondary text-white">
                    <h2 class="text-xl font-bold flex items-center">
                        <svg class="w-6 h-6 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z"/>
                        </svg>
                        Contact Information
                    </h2>
                </div>
                <div class="p-6 space-y-4">
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <div>
                            <label class="block text-sm font-semibold text-gray-700 mb-2">Phone Number</label>
                            <input type="tel" name="phone" value="<?= htmlspecialchars($entry['phone'] ?? '') ?>" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-primary">
                        </div>
                        <div>
                            <label class="block text-sm font-semibold text-gray-700 mb-2">Email Address</label>
                            <input type="email" name="email" value="<?= htmlspecialchars($entry['email'] ?? '') ?>" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-primary">
                        </div>
                    </div>
                    
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <div>
                            <label class="block text-sm font-semibold text-gray-700 mb-2">Address Line</label>
                            <input type="text" name="address_line" value="<?= htmlspecialchars($entry['address_line'] ?? '') ?>" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-primary">
                        </div>
                        <div>
                            <label class="block text-sm font-semibold text-gray-700 mb-2">City</label>
                            <input type="text" name="city" value="<?= htmlspecialchars($entry['city'] ?? '') ?>" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-primary">
                        </div>
                    </div>
                </div>
            </div>

            <!-- Church Location -->
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="px-6 py-4 bg-gradient-tertiary text-white">
                    <h2 class="text-xl font-bold flex items-center">
                        <svg class="w-6 h-6 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17.657 16.657L13.414 20.9a1.998 1.998 0 01-2.827 0l-4.244-4.243a8 8 0 1111.314 0z"/>
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 11a3 3 0 11-6 0 3 3 0 016 0z"/>
                        </svg>
                        Church Location
                    </h2>
                </div>
                <div class="p-6 space-y-4">
                    <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                        <div>
                            <label class="block text-sm font-semibold text-gray-700 mb-2">Area</label>
                            <select name="area_id" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-primary">
                                <option value="">Select Area</option>
                                <?php foreach ($areas as $area): ?>
                                    <option value="<?= $area['area_id'] ?>" <?= ($entry['area_id'] ?? '') == $area['area_id'] ? 'selected' : '' ?>>
                                        <?= htmlspecialchars($area['area_name']) ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div>
                            <label class="block text-sm font-semibold text-gray-700 mb-2">District</label>
                            <select name="district_id" id="districtSelect" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-primary">
                                <option value="">Select District</option>
                                <?php foreach ($districts as $district): ?>
                                    <option value="<?= $district['id'] ?>" <?= ($entry['district_id'] ?? '') == $district['id'] ? 'selected' : '' ?>>
                                        <?= htmlspecialchars($district['district_name']) ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div>
                            <label class="block text-sm font-semibold text-gray-700 mb-2">Assembly</label>
                            <select name="assembly_id" id="assemblySelect" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-primary">
                                <option value="">Select Assembly</option>
                                <?php foreach ($assemblies as $assembly): ?>
                                    <option value="<?= $assembly['id'] ?>" <?= ($entry['assembly_id'] ?? '') == $assembly['id'] ? 'selected' : '' ?>>
                                        <?= htmlspecialchars($assembly['assembly_name']) ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Additional Notes -->
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="px-6 py-4 bg-gray-700 text-white">
                    <h2 class="text-xl font-bold flex items-center">
                        <svg class="w-6 h-6 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/>
                        </svg>
                        Additional Notes
                    </h2>
                </div>
                <div class="p-6">
                    <textarea name="notes" rows="4" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-primary" placeholder="Any additional information..."><?= htmlspecialchars($entry['notes'] ?? '') ?></textarea>
                </div>
            </div>

            <!-- Submit Button -->
            <div class="flex justify-end gap-3">
                <a href="standalone.php" class="px-6 py-3 border border-gray-300 rounded-lg font-semibold text-gray-700 hover:bg-gray-50 transition-all duration-300">
                    Cancel
                </a>
                <button type="submit" class="btn-gradient px-8 py-3 rounded-lg font-semibold hover:shadow-lg transition-all duration-300">
                    <?= $editing ? 'Update Entry' : 'Add Entry' ?>
                </button>
            </div>
        </form>
        </div>
    </div>
</main>

<script>
// Dynamic assembly loading based on district selection
document.getElementById('districtSelect').addEventListener('change', function() {
    const districtId = this.value;
    const assemblySelect = document.getElementById('assemblySelect');
    
    if (!districtId) {
        assemblySelect.innerHTML = '<option value="">Select Assembly</option>';
        return;
    }
    
    fetch(`../../api/get-assemblies.php?district_id=${districtId}`)
        .then(response => response.json())
        .then(data => {
            assemblySelect.innerHTML = '<option value="">Select Assembly</option>';
            data.forEach(assembly => {
                assemblySelect.innerHTML += `<option value="${assembly.id}">${assembly.assembly_name}</option>`;
            });
        })
        .catch(error => console.error('Error loading assemblies:', error));
});
</script>

<?php include '../../includes/footer.php'; ?>

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists