Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/newsfeed/admin/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/newsfeed/admin/categories.php

<?php
require_once '../config/config.php';
require_admin();

$database = new Database();
$conn = $database->getConnection();
$category = new Category($conn);

$error = '';
$success = '';
$action = $_GET['action'] ?? 'list';
$id = intval($_GET['id'] ?? 0);

// Handle GET delete action
if ($action === 'delete' && $id) {
    if ($category->delete($id)) {
        flash_message('Category deleted successfully!', 'success');
    } else {
        flash_message('Failed to delete category', 'error');
    }
    redirect('categories.php');
}

// Handle form submissions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if ($_POST['form_action'] === 'create') {
        $name = sanitize_input($_POST['name'] ?? '');
        $type = sanitize_input($_POST['type'] ?? '');
        $description = sanitize_input($_POST['description'] ?? '');
        
        if (empty($name) || empty($type)) {
            $error = 'Please fill in all required fields';
        } elseif ($category->nameExists($name)) {
            $error = 'Category name already exists';
        } else {
            $category_data = [
                'name' => $name,
                'type' => $type,
                'description' => $description,
                'created_by' => $_SESSION['user_id']
            ];
            
            if ($category->create($category_data)) {
                flash_message('Category created successfully!', 'success');
                redirect('categories.php');
            } else {
                $error = 'Failed to create category';
            }
        }
    } elseif ($_POST['form_action'] === 'update') {
        $name = sanitize_input($_POST['name'] ?? '');
        $type = sanitize_input($_POST['type'] ?? '');
        $description = sanitize_input($_POST['description'] ?? '');
        $status = sanitize_input($_POST['status'] ?? '');
        
        if (empty($name) || empty($type)) {
            $error = 'Please fill in all required fields';
        } elseif ($category->nameExists($name, $id)) {
            $error = 'Category name already exists';
        } else {
            $update_data = [
                'name' => $name,
                'type' => $type,
                'description' => $description,
                'status' => $status
            ];
            
            if ($category->update($id, $update_data)) {
                flash_message('Category updated successfully!', 'success');
                redirect('categories.php');
            } else {
                $error = 'Failed to update category';
            }
        }
    } elseif ($_POST['form_action'] === 'delete') {
        if ($category->delete($id)) {
            flash_message('Category deleted successfully!', 'success');
        } else {
            flash_message('Failed to delete category', 'error');
        }
        redirect('categories.php');
    }
}

// Get category data for edit
$category_data = null;
if ($action === 'edit' && $id) {
    $category_data = $category->getById($id);
    if (!$category_data) {
        flash_message('Category not found', 'error');
        redirect('categories.php');
    }
}

// Get all categories for list view
$categories = $category->getAll(null);
$flash = get_flash_message();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Manage Categories - COP News Portal</title>
    <link rel="stylesheet" href="../assets/css/style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
</head>
<body>
    <header class="header">
        <nav class="navbar">
            <a href="../dashboard.php" class="logo">
                <i class="fas fa-church"></i>
                COP News Portal
            </a>
            <ul class="nav-links">
                <li><a href="../dashboard.php"><i class="fas fa-tachometer-alt"></i> Dashboard</a></li>
                <li><a href="../news/index.php"><i class="fas fa-newspaper"></i> News</a></li>
                <li><a href="index.php"><i class="fas fa-cog"></i> Admin</a></li>
                <li><a href="../profile.php"><i class="fas fa-user"></i> Profile</a></li>
                <li><a href="../logout.php"><i class="fas fa-sign-out-alt"></i> Logout</a></li>
            </ul>
        </nav>
    </header>

    <main class="container" style="margin-top: 2rem;">
        <?php if ($flash): ?>
            <div class="alert alert-<?php echo $flash['type']; ?>">
                <i class="fas fa-info-circle"></i> <?php echo $flash['message']; ?>
            </div>
        <?php endif; ?>

        <?php if ($error): ?>
            <div class="alert alert-error">
                <i class="fas fa-exclamation-circle"></i> <?php echo $error; ?>
            </div>
        <?php endif; ?>

        <?php if ($action === 'list'): ?>
            <!-- Categories List -->
            <div class="card">
                <div class="card-header">
                    <div class="flex justify-between items-center">
                        <h1><i class="fas fa-tags"></i> Manage Categories</h1>
                        <a href="?action=create" class="btn btn-primary">
                            <i class="fas fa-plus"></i> Add Category
                        </a>
                    </div>
                </div>
                <div class="card-body">
                    <?php if (empty($categories)): ?>
                        <div class="text-center p-4">
                            <i class="fas fa-tags" style="font-size: 4rem; color: var(--light-grey); margin-bottom: 1rem;"></i>
                            <h3>No categories found</h3>
                            <p style="color: var(--primary-grey);">Start by creating your first category.</p>
                            <a href="?action=create" class="btn btn-primary mt-2">
                                <i class="fas fa-plus"></i> Create Category
                            </a>
                        </div>
                    <?php else: ?>
                        <div class="table-responsive">
                            <table class="table">
                                <thead>
                                    <tr>
                                        <th>Name</th>
                                        <th>Type</th>
                                        <th>Description</th>
                                        <th>Status</th>
                                        <th>Created By</th>
                                        <th>Created</th>
                                        <th>Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($categories as $cat): ?>
                                        <tr>
                                            <td><strong><?php echo htmlspecialchars($cat['name']); ?></strong></td>
                                            <td>
                                                <span class="badge badge-type-<?php echo $cat['type']; ?>">
                                                    <?php echo ucwords(str_replace('_', ' ', $cat['type'])); ?>
                                                </span>
                                            </td>
                                            <td><?php echo htmlspecialchars(substr($cat['description'], 0, 50)); ?><?php echo strlen($cat['description']) > 50 ? '...' : ''; ?></td>
                                            <td>
                                                <span class="badge badge-<?php echo $cat['status']; ?>">
                                                    <?php echo ucfirst($cat['status']); ?>
                                                </span>
                                            </td>
                                            <td><?php echo htmlspecialchars($cat['created_by_name'] ?? 'System'); ?></td>
                                            <td><?php echo date('M j, Y', strtotime($cat['created_at'])); ?></td>
                                            <td>
                                                <div class="flex gap-1">
                                                    <a href="?action=edit&id=<?php echo $cat['id']; ?>" 
                                                       class="btn btn-sm btn-secondary">
                                                        <i class="fas fa-edit"></i>
                                                    </a>
                                                    <a href="?action=delete&id=<?php echo $cat['id']; ?>" 
                                                       class="btn btn-sm btn-danger"
                                                       onclick="return confirm('Are you sure you want to delete this category?')">
                                                        <i class="fas fa-trash"></i>
                                                    </a>
                                                </div>
                                            </td>
                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    <?php endif; ?>
                </div>
            </div>

        <?php elseif ($action === 'create'): ?>
            <!-- Create Category -->
            <div class="card">
                <div class="card-header">
                    <h1><i class="fas fa-plus"></i> Create Category</h1>
                </div>
                <div class="card-body">
                    <form method="POST" action="">
                        <input type="hidden" name="form_action" value="create">
                        
                        <div class="grid grid-2">
                            <div class="form-group">
                                <label for="name" class="form-label">Category Name *</label>
                                <input type="text" id="name" name="name" class="form-control" 
                                       value="<?php echo htmlspecialchars($_POST['name'] ?? ''); ?>" 
                                       placeholder="Enter category name..." required>
                            </div>
                            
                            <div class="form-group">
                                <label for="type" class="form-label">Category Type *</label>
                                <select id="type" name="type" class="form-control form-select" required>
                                    <option value="">Select Type</option>
                                    <option value="area_program" <?php echo ($_POST['type'] ?? '') === 'area_program' ? 'selected' : ''; ?>>Area Program</option>
                                    <option value="district_program" <?php echo ($_POST['type'] ?? '') === 'district_program' ? 'selected' : ''; ?>>District Program</option>
                                    <option value="local_program" <?php echo ($_POST['type'] ?? '') === 'local_program' ? 'selected' : ''; ?>>Local Program</option>
                                    <option value="general" <?php echo ($_POST['type'] ?? '') === 'general' ? 'selected' : ''; ?>>General</option>
                                </select>
                            </div>
                        </div>
                        
                        <div class="form-group">
                            <label for="description" class="form-label">Description</label>
                            <textarea id="description" name="description" class="form-control" rows="3" 
                                      placeholder="Category description..."><?php echo htmlspecialchars($_POST['description'] ?? ''); ?></textarea>
                        </div>
                        
                        <div class="flex gap-2">
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-save"></i> Create Category
                            </button>
                            <a href="categories.php" class="btn btn-secondary">
                                <i class="fas fa-times"></i> Cancel
                            </a>
                        </div>
                    </form>
                </div>
            </div>

        <?php elseif ($action === 'edit' && $category_data): ?>
            <!-- Edit Category -->
            <div class="card">
                <div class="card-header">
                    <h1><i class="fas fa-edit"></i> Edit Category</h1>
                </div>
                <div class="card-body">
                    <form method="POST" action="">
                        <input type="hidden" name="form_action" value="update">
                        
                        <div class="grid grid-2">
                            <div class="form-group">
                                <label for="name" class="form-label">Category Name *</label>
                                <input type="text" id="name" name="name" class="form-control" 
                                       value="<?php echo htmlspecialchars($_POST['name'] ?? $category_data['name']); ?>" 
                                       placeholder="Enter category name..." required>
                            </div>
                            
                            <div class="form-group">
                                <label for="type" class="form-label">Category Type *</label>
                                <select id="type" name="type" class="form-control form-select" required>
                                    <option value="">Select Type</option>
                                    <option value="area_program" <?php echo ($_POST['type'] ?? $category_data['type']) === 'area_program' ? 'selected' : ''; ?>>Area Program</option>
                                    <option value="district_program" <?php echo ($_POST['type'] ?? $category_data['type']) === 'district_program' ? 'selected' : ''; ?>>District Program</option>
                                    <option value="local_program" <?php echo ($_POST['type'] ?? $category_data['type']) === 'local_program' ? 'selected' : ''; ?>>Local Program</option>
                                    <option value="general" <?php echo ($_POST['type'] ?? $category_data['type']) === 'general' ? 'selected' : ''; ?>>General</option>
                                </select>
                            </div>
                        </div>
                        
                        <div class="form-group">
                            <label for="description" class="form-label">Description</label>
                            <textarea id="description" name="description" class="form-control" rows="3" 
                                      placeholder="Category description..."><?php echo htmlspecialchars($_POST['description'] ?? $category_data['description']); ?></textarea>
                        </div>
                        
                        <div class="form-group">
                            <label for="status" class="form-label">Status</label>
                            <select id="status" name="status" class="form-control form-select">
                                <option value="active" <?php echo ($_POST['status'] ?? $category_data['status']) === 'active' ? 'selected' : ''; ?>>Active</option>
                                <option value="inactive" <?php echo ($_POST['status'] ?? $category_data['status']) === 'inactive' ? 'selected' : ''; ?>>Inactive</option>
                            </select>
                        </div>
                        
                        <div class="flex gap-2">
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-save"></i> Update Category
                            </button>
                            <a href="categories.php" class="btn btn-secondary">
                                <i class="fas fa-times"></i> Cancel
                            </a>
                        </div>
                    </form>
                </div>
            </div>

        <?php elseif ($action === 'delete' && $id): ?>
            <!-- Delete Confirmation -->
            <?php
            $delete_category = $category->getById($id);
            if ($delete_category):
            ?>
            <div class="card">
                <div class="card-header">
                    <h1><i class="fas fa-trash"></i> Delete Category</h1>
                </div>
                <div class="card-body">
                    <div class="alert alert-warning">
                        <i class="fas fa-exclamation-triangle"></i>
                        <strong>Warning:</strong> This action cannot be undone. Are you sure you want to delete this category?
                    </div>

                    <div class="category-preview" style="background: var(--light-grey); padding: 1.5rem; border-radius: 8px; margin: 1.5rem 0;">
                        <h3><?php echo htmlspecialchars($delete_category['name']); ?></h3>
                        <p><strong>Type:</strong> <?php echo ucwords(str_replace('_', ' ', $delete_category['type'])); ?></p>
                        <?php if ($delete_category['description']): ?>
                            <p><strong>Description:</strong> <?php echo htmlspecialchars($delete_category['description']); ?></p>
                        <?php endif; ?>
                    </div>

                    <form method="POST" action="">
                        <input type="hidden" name="form_action" value="delete">
                        <div class="flex gap-2">
                            <button type="submit" class="btn btn-danger">
                                <i class="fas fa-trash"></i> Yes, Delete Category
                            </button>
                            <a href="categories.php" class="btn btn-secondary">
                                <i class="fas fa-times"></i> Cancel
                            </a>
                        </div>
                    </form>
                </div>
            </div>
            <?php endif; ?>
        <?php endif; ?>
    </main>

    <style>
        .table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 1rem;
        }
        .table th,
        .table td {
            padding: 0.75rem;
            text-align: left;
            border-bottom: 1px solid var(--light-grey);
        }
        .table th {
            background: var(--light-grey);
            font-weight: 600;
        }
        .table-responsive {
            overflow-x: auto;
        }
        .badge-type-area_program {
            background: var(--primary-blue);
            color: white;
        }
        .badge-type-district_program {
            background: var(--success);
            color: white;
        }
        .badge-type-local_program {
            background: var(--warning);
            color: white;
        }
        .badge-type-general {
            background: var(--primary-grey);
            color: white;
        }
        .gap-1 {
            gap: 0.25rem;
        }
    </style>
</body>
</html>

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