Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/attendance/database/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/attendance/database/schema.sql

-- Church Attendance Management System Database Schema

CREATE DATABASE IF NOT EXISTS church_attendance;
USE church_attendance;

-- Users table (superuser, admin users)
CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) UNIQUE NOT NULL,
    email VARCHAR(100) UNIQUE NOT NULL,
    password VARCHAR(255) NOT NULL,
    full_name VARCHAR(100) NOT NULL,
    phone VARCHAR(20),
    role ENUM('superuser', 'admin', 'user') DEFAULT 'user',
    is_active BOOLEAN DEFAULT TRUE,
    profile_image VARCHAR(255),
    location_id INT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    last_login TIMESTAMP NULL
);

-- Locations table (Districts, Assemblies)
CREATE TABLE locations (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    type ENUM('district', 'assembly') NOT NULL,
    parent_id INT NULL,
    address TEXT,
    contact_person VARCHAR(100),
    contact_phone VARCHAR(20),
    contact_email VARCHAR(100),
    is_active BOOLEAN DEFAULT TRUE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (parent_id) REFERENCES locations(id) ON DELETE SET NULL
);

-- Programs table
CREATE TABLE programs (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    description TEXT,
    start_date DATE,
    end_date DATE,
    location_id INT,
    is_active BOOLEAN DEFAULT TRUE,
    created_by INT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (location_id) REFERENCES locations(id) ON DELETE SET NULL,
    FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE SET NULL
);

-- Form templates table
CREATE TABLE form_templates (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    description TEXT,
    fields JSON NOT NULL,
    is_active BOOLEAN DEFAULT TRUE,
    created_by INT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE SET NULL
);

-- Attendance records table
CREATE TABLE attendance_records (
    id INT AUTO_INCREMENT PRIMARY KEY,
    program_id INT NOT NULL,
    district_id INT NULL,
    assembly_id INT NULL,
    district_name VARCHAR(100),
    assembly_name VARCHAR(100),
    full_name VARCHAR(100) NOT NULL,
    email VARCHAR(100),
    telephone VARCHAR(20),
    additional_data JSON,
    ip_address VARCHAR(45),
    user_agent TEXT,
    submitted_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (program_id) REFERENCES programs(id) ON DELETE CASCADE,
    FOREIGN KEY (district_id) REFERENCES locations(id) ON DELETE SET NULL,
    FOREIGN KEY (assembly_id) REFERENCES locations(id) ON DELETE SET NULL
);

-- Settings table
CREATE TABLE settings (
    id INT AUTO_INCREMENT PRIMARY KEY,
    setting_key VARCHAR(100) UNIQUE NOT NULL,
    setting_value TEXT,
    setting_type ENUM('string', 'integer', 'boolean', 'json') DEFAULT 'string',
    description TEXT,
    updated_by INT,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (updated_by) REFERENCES users(id) ON DELETE SET NULL
);

-- Notifications table
CREATE TABLE notifications (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT,
    title VARCHAR(200) NOT NULL,
    message TEXT NOT NULL,
    type ENUM('info', 'warning', 'error', 'success') DEFAULT 'info',
    is_read BOOLEAN DEFAULT FALSE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

-- Audit logs table
CREATE TABLE audit_logs (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT,
    action VARCHAR(100) NOT NULL,
    details TEXT,
    ip_address VARCHAR(45),
    user_agent TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL
);

-- Backups table
CREATE TABLE backups (
    id INT AUTO_INCREMENT PRIMARY KEY,
    filename VARCHAR(255) NOT NULL,
    file_size BIGINT,
    backup_type ENUM('manual', 'automatic') DEFAULT 'manual',
    created_by INT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE SET NULL
);

-- Insert default superuser
INSERT INTO users (username, email, password, full_name, role, is_active) VALUES 
('nabibo', 'nabibo2@yahoo.co.uk', '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', 'Super Administrator', 'superuser', TRUE);
-- Password is: password123

-- Insert default settings
INSERT INTO settings (setting_key, setting_value, setting_type, description) VALUES
('site_title', 'Church Attendance Management System', 'string', 'Website title'),
('site_logo', 'assets/images/logo.png', 'string', 'Website logo path'),
('footer_title', 'Church Attendance System © 2024', 'string', 'Footer text'),
('theme_primary_color', '#3B82F6', 'string', 'Primary theme color'),
('theme_secondary_color', '#F59E0B', 'string', 'Secondary theme color'),
('maintenance_mode', '0', 'boolean', 'Site maintenance mode'),
('email_notifications', '1', 'boolean', 'Enable email notifications'),
('sms_notifications', '0', 'boolean', 'Enable SMS notifications'),
('backup_frequency', 'weekly', 'string', 'Automatic backup frequency'),
('timezone', 'UTC', 'string', 'System timezone');

-- Sample locations
INSERT INTO locations (name, type, parent_id, address, contact_person, contact_phone, contact_email) VALUES
('Central District', 'district', NULL, '123 Main Street', 'John Doe', '+1234567890', 'central@church.org'),
('North District', 'district', NULL, '456 North Ave', 'Jane Smith', '+1234567891', 'north@church.org'),
('Central Assembly', 'assembly', 1, '123 Main Street', 'Pastor John', '+1234567892', 'central.assembly@church.org'),
('North Assembly', 'assembly', 2, '456 North Ave', 'Pastor Jane', '+1234567893', 'north.assembly@church.org');

-- Sample programs
INSERT INTO programs (name, description, start_date, end_date, location_id, created_by) VALUES
('Sunday Service', 'Weekly Sunday worship service', '2024-01-01', '2024-12-31', 1, 1),
('Youth Meeting', 'Monthly youth gathering', '2024-01-01', '2024-12-31', 2, 1),
('Bible Study', 'Weekly Bible study sessions', '2024-01-01', '2024-12-31', 3, 1);

-- Sample form template
INSERT INTO form_templates (name, description, fields, created_by) VALUES
('Standard Attendance Form', 'Default attendance form for church programs', 
'[
    {"name": "district_name", "label": "District Name", "type": "text", "required": true},
    {"name": "assembly_name", "label": "Assembly Name", "type": "text", "required": true},
    {"name": "full_name", "label": "Full Name", "type": "text", "required": true},
    {"name": "email", "label": "Email Address", "type": "email", "required": false},
    {"name": "telephone", "label": "Phone Number", "type": "tel", "required": false},
    {"name": "first_time", "label": "Is this your first time?", "type": "radio", "options": ["Yes", "No"], "required": false}
]', 1);

-- Create indexes for better performance
CREATE INDEX idx_users_email ON users(email);
CREATE INDEX idx_users_role ON users(role);
CREATE INDEX idx_locations_type ON locations(type);
CREATE INDEX idx_locations_parent ON locations(parent_id);
CREATE INDEX idx_programs_location ON programs(location_id);
CREATE INDEX idx_attendance_program ON attendance_records(program_id);
CREATE INDEX idx_attendance_district ON attendance_records(district_id);
CREATE INDEX idx_attendance_assembly ON attendance_records(assembly_id);
CREATE INDEX idx_attendance_date ON attendance_records(submitted_at);
CREATE INDEX idx_audit_user ON audit_logs(user_id);
CREATE INDEX idx_audit_date ON audit_logs(created_at);
CREATE INDEX idx_notifications_user ON notifications(user_id);

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