based on this info databaes (-- phpMyAdmin SQL Dump -- version 5.2.1 --…
- User
based on this info databaes (-- phpMyAdmin SQL Dump -- version 5.2.1 -- https://www.phpmyadmin.net/
-- Host: 127.0.0.1:3306 -- Generation Time: Nov 10, 2024 at 08:15 AM -- Server version: 10.11.9-MariaDB -- PHP Version: 7.2.34
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; START TRANSACTION; SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT /; /!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS /; /!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION /; /!40101 SET NAMES utf8mb4 */;
-- -- Database:
u406552082_kaakalia
-- -- Table structure for table
customersCREATE TABLE
customers(idint(11) NOT NULL,namevarchar(100) NOT NULL,contactvarchar(50) DEFAULT NULL,delivery_addresstext DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- -- Table structure for table
driversCREATE TABLE
drivers(idint(11) NOT NULL,namevarchar(100) NOT NULL,numbervarchar(15) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- -- Table structure for table
ordersCREATE TABLE
orders(idint(11) NOT NULL,customer_idint(11) NOT NULL,statusenum('Pending','In Progress','Completed','Delivered') DEFAULT 'Pending',priorityenum('Normal','Important','Quick') DEFAULT 'Normal',order_datedatetime DEFAULT current_timestamp(),delivery_datetimedatetime DEFAULT NULL,delivery_datedate DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- -- Table structure for table
order_itemsCREATE TABLE
order_items(idint(11) NOT NULL,order_idint(11) NOT NULL,product_idint(11) NOT NULL,quantityint(11) NOT NULL DEFAULT 1,packaging_preferencesvarchar(255) DEFAULT NULL,discount_percentagedecimal(5,2) DEFAULT 0.00,discount_reasonvarchar(255) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- -- Table structure for table
productsCREATE TABLE
products(idint(11) NOT NULL,namevarchar(100) NOT NULL,sizevarchar(50) DEFAULT NULL,pricedecimal(10,2) NOT NULL,categoryvarchar(50) DEFAULT NULL,thumbnailvarchar(255) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- -- Table structure for table
usersCREATE TABLE
users(idint(11) NOT NULL,usernamevarchar(50) NOT NULL,passwordvarchar(255) NOT NULL,roleenum('admin','staff') NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- -- Table structure for table
workspace_notesCREATE TABLE
workspace_notes(idint(11) NOT NULL,user_idint(11) NOT NULL,titlevarchar(255) NOT NULL,contenttext NOT NULL,created_atdatetime DEFAULT current_timestamp(),updated_atdatetime DEFAULT current_timestamp() ON UPDATE current_timestamp() ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- -- Table structure for table
workspace_plansCREATE TABLE
workspace_plans(idint(11) NOT NULL,user_idint(11) NOT NULL,titlevarchar(255) NOT NULL,descriptiontext DEFAULT NULL,due_datedatetime DEFAULT NULL,statusenum('Not Started','In Progress','Completed') DEFAULT 'Not Started',created_atdatetime DEFAULT current_timestamp(),updated_atdatetime DEFAULT current_timestamp() ON UPDATE current_timestamp() ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- -- Table structure for table
workspace_postsCREATE TABLE
workspace_posts(idint(11) NOT NULL,user_idint(11) NOT NULL,platformenum('Instagram','Facebook','Twitter','Other') NOT NULL,contenttext NOT NULL,media_urlvarchar(255) DEFAULT NULL,scheduled_datetimedatetime DEFAULT NULL,statusenum('Scheduled','Posted','Failed') DEFAULT 'Scheduled',created_atdatetime DEFAULT current_timestamp(),updated_atdatetime DEFAULT current_timestamp() ON UPDATE current_timestamp() ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;-- -- Indexes for dumped tables
-- -- Indexes for table
customersALTER TABLE
customersADD PRIMARY KEY (id);-- -- Indexes for table
driversALTER TABLE
driversADD PRIMARY KEY (id);-- -- Indexes for table
ordersALTER TABLE
ordersADD PRIMARY KEY (id), ADD KEYcustomer_id(customer_id), ADD KEYidx_orders_customer_id(customer_id);-- -- Indexes for table
order_itemsALTER TABLE
order_itemsADD PRIMARY KEY (id), ADD KEYorder_id(order_id), ADD KEYproduct_id(product_id), ADD KEYidx_order_items_order_id(order_id), ADD KEYidx_order_items_product_id(product_id);-- -- Indexes for table
productsALTER TABLE
productsADD PRIMARY KEY (id);-- -- Indexes for table
usersALTER TABLE
usersADD PRIMARY KEY (id), ADD UNIQUE KEYusername(username);-- -- Indexes for table
workspace_notesALTER TABLE
workspace_notesADD PRIMARY KEY (id), ADD KEYuser_id(user_id);-- -- Indexes for table
workspace_plansALTER TABLE
workspace_plansADD PRIMARY KEY (id), ADD KEYuser_id(user_id);-- -- Indexes for table
workspace_postsALTER TABLE
workspace_postsADD PRIMARY KEY (id), ADD KEYuser_id(user_id);-- -- AUTO_INCREMENT for dumped tables
-- -- AUTO_INCREMENT for table
customersALTER TABLE
customersMODIFYidint(11) NOT NULL AUTO_INCREMENT;-- -- AUTO_INCREMENT for table
driversALTER TABLE
driversMODIFYidint(11) NOT NULL AUTO_INCREMENT;-- -- AUTO_INCREMENT for table
ordersALTER TABLE
ordersMODIFYidint(11) NOT NULL AUTO_INCREMENT;-- -- AUTO_INCREMENT for table
order_itemsALTER TABLE
order_itemsMODIFYidint(11) NOT NULL AUTO_INCREMENT;-- -- AUTO_INCREMENT for table
productsALTER TABLE
productsMODIFYidint(11) NOT NULL AUTO_INCREMENT;-- -- AUTO_INCREMENT for table
usersALTER TABLE
usersMODIFYidint(11) NOT NULL AUTO_INCREMENT;-- -- AUTO_INCREMENT for table
workspace_notesALTER TABLE
workspace_notesMODIFYidint(11) NOT NULL AUTO_INCREMENT;-- -- AUTO_INCREMENT for table
workspace_plansALTER TABLE
workspace_plansMODIFYidint(11) NOT NULL AUTO_INCREMENT;-- -- AUTO_INCREMENT for table
workspace_postsALTER TABLE
workspace_postsMODIFYidint(11) NOT NULL AUTO_INCREMENT;-- -- Constraints for dumped tables
-- -- Constraints for table
ordersALTER TABLE
ordersADD CONSTRAINTfk_orders_customer_idFOREIGN KEY (customer_id) REFERENCEScustomers(id) ON DELETE CASCADE ON UPDATE CASCADE, ADD CONSTRAINTorders_ibfk_1FOREIGN KEY (customer_id) REFERENCEScustomers(id) ON DELETE CASCADE;-- -- Constraints for table
order_itemsALTER TABLE
order_itemsADD CONSTRAINTfk_order_items_order_idFOREIGN KEY (order_id) REFERENCESorders(id) ON DELETE CASCADE ON UPDATE CASCADE, ADD CONSTRAINTfk_order_items_product_idFOREIGN KEY (product_id) REFERENCESproducts(id) ON DELETE CASCADE ON UPDATE CASCADE, ADD CONSTRAINTorder_items_ibfk_1FOREIGN KEY (order_id) REFERENCESorders(id) ON DELETE CASCADE, ADD CONSTRAINTorder_items_ibfk_2FOREIGN KEY (product_id) REFERENCESproducts(id) ON DELETE CASCADE;-- -- Constraints for table
workspace_notesALTER TABLE
workspace_notesADD CONSTRAINTfk_workspace_notes_user_idFOREIGN KEY (user_id) REFERENCESusers(id) ON DELETE CASCADE;-- -- Constraints for table
workspace_plansALTER TABLE
workspace_plansADD CONSTRAINTfk_workspace_plans_user_idFOREIGN KEY (user_id) REFERENCESusers(id) ON DELETE CASCADE;-- -- Constraints for table
workspace_postsALTER TABLE
workspace_postsADD CONSTRAINTfk_workspace_posts_user_idFOREIGN KEY (user_id) REFERENCESusers(id) ON DELETE CASCADE; COMMIT;/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT /; /!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS /; /!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;) in this codes (<?php // /public/workspace/create_note.php
// Enable error reporting for debugging (disable in production) ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
// Start output buffering to prevent "headers already sent" errors ob_start();
// Include authentication, database connection, and utility functions require_once DIR . '/../../includes/auth.php'; require_once DIR . '/../../includes/db_connect.php'; require_once DIR . '/../../includes/functions.php'; // Contains sanitize_input()
_SESSION['user_id']; $errors = [];
if (_SERVER['REQUEST_METHOD'] == 'POST') { title = sanitize_input(content = sanitize_input($_POST['content'] ?? '');
// Validation if (empty($title)) { $errors[] = "Title is required."; } if (empty($content)) { $errors[] = "Content is required."; } if (empty($errors)) { $sql = "INSERT INTO workspace_notes (user_id, title, content) VALUES (?, ?, ?)"; if ($stmt = $conn->prepare($sql)) { $stmt->bind_param("iss", $user_id, $title, $content); if ($stmt->execute()) { header("Location: notes.php"); exit(); } else { $errors[] = "Failed to create note. Please try again."; } $stmt->close(); } else { $errors[] = "Database error: Unable to prepare statement."; } }} ?>
<!-- Main Content --> <main class="container mx-auto p-6 mt-24 flex-grow"> <h1 class="text-2xl font-bold mb-6">Create New Note</h1> <!-- Display Errors --> <?php if(!empty($errors)): ?> <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-4"> <ul> <?php foreach($errors as $error): ?> <li><?= htmlspecialchars($error) ?></li> <?php endforeach; ?> </ul> </div> <?php endif; ?> <!-- Note Creation Form --> <form action="create_note.php" method="POST" class="bg-white p-6 rounded shadow-md"> <div class="mb-4"> <label class="block text-gray-700">Title<span class="text-red-500">*</span></label> <input type="text" name="title" class="w-full px-3 py-2 border rounded focus:outline-none focus:ring focus:border-blue-300" required value="<?= isset($_POST['title']) ? htmlspecialchars($_POST['title']) : '' ?>"> </div> <div class="mb-4"> <label class="block text-gray-700">Content<span class="text-red-500">*</span></label> <textarea name="content" class="w-full px-3 py-2 border rounded focus:outline-none focus:ring focus:border-blue-300" rows="8" required><?= isset($_POST['content']) ? htmlspecialchars($_POST['content']) : '' ?></textarea> </div> <div class="flex justify-between"> <a href="notes.php" class="px-4 py-2 bg-gray-500 text-white rounded hover:bg-gray-600">Cancel</a> <button type="submit" class="px-4 py-2 bg-purple-500 text-white rounded hover:bg-purple-600">Create Note</button> </div> </form> </main> <!-- Footer --> <footer class="bg-gray-800 text-white text-center p-4"> © 2024 Kaak Alia Bakery. All rights reserved. </footer>// Enable error reporting for debugging (disable in production) ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
// Start output buffering to prevent "headers already sent" errors ob_start();
// Include authentication, database connection, and utility functions require_once DIR . '/../../includes/auth.php'; require_once DIR . '/../../includes/db_connect.php'; require_once DIR . '/../../includes/functions.php'; // Contains sanitize_input()
_SESSION['user_id']; $errors = [];
// Pre-fill due_date if passed via GET (e.g., from calendar selection) _GET['due_date'] ?? '');
if (_SERVER['REQUEST_METHOD'] == 'POST') { title = sanitize_input(description = sanitize_input(due_date = sanitize_input(status = sanitize_input($_POST['status'] ?? 'Not Started');
// Validation if (empty($title)) { $errors[] = "Title is required."; } if (empty($due_date)) { $errors[] = "Due date is required."; } // Validate status $valid_statuses = ['Not Started', 'In Progress', 'Completed']; if (!in_array($status, $valid_statuses)) { $errors[] = "Invalid status selected."; } // Validate due_date format if (!strtotime($due_date)) { $errors[] = "Invalid due date format."; } if (empty($errors)) { $sql = "INSERT INTO workspace_plans (user_id, title, description, due_date, status) VALUES (?, ?, ?, ?, ?)"; if ($stmt = $conn->prepare($sql)) { $stmt->bind_param("issss", $user_id, $title, $description, $due_date, $status); if ($stmt->execute()) { header("Location: plans.php"); exit(); } else { $errors[] = "Failed to create plan. Please try again."; } $stmt->close(); } else { $errors[] = "Database error: Unable to prepare statement."; } }} ?>
<!-- Main Content --> <main class="container mx-auto p-6 mt-24 flex-grow"> <h1 class="text-2xl font-bold mb-6">Create New Plan</h1> <!-- Display Errors --> <?php if(!empty($errors)): ?> <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-4"> <ul> <?php foreach($errors as $error): ?> <li><?= htmlspecialchars($error) ?></li> <?php endforeach; ?> </ul> </div> <?php endif; ?> <!-- Plan Creation Form --> <form action="create_plan.php" method="POST" class="bg-white p-6 rounded shadow-md"> <div class="mb-4"> <label class="block text-gray-700">Title<span class="text-red-500">*</span></label> <input type="text" name="title" class="w-full px-3 py-2 border rounded focus:outline-none focus:ring focus:border-blue-300" required value="<?= isset($_POST['title']) ? htmlspecialchars($_POST['title']) : '' ?>"> </div> <div class="mb-4"> <label class="block text-gray-700">Description</label> <textarea name="description" class="w-full px-3 py-2 border rounded focus:outline-none focus:ring focus:border-blue-300" rows="4"><?= isset($_POST['description']) ? htmlspecialchars($_POST['description']) : '' ?></textarea> </div> <div class="mb-4"> <label class="block text-gray-700">Due Date<span class="text-red-500">*</span></label> <input type="datetime-local" name="due_date" class="w-full px-3 py-2 border rounded focus:outline-none focus:ring focus:border-blue-300" required value="<?= isset($_POST['due_date']) ? htmlspecialchars($_POST['due_date']) : $pre_fill_due_date ?>"> </div> <div class="mb-4"> <label class="block text-gray-700">Status</label> <select name="status" class="w-full px-3 py-2 border rounded focus:outline-none focus:ring focus:border-blue-300"> <option value="Not Started" <?= (isset($_POST['status']) && $_POST['status'] == 'Not Started') ? 'selected' : '' ?>>Not Started</option> <option value="In Progress" <?= (isset($_POST['status']) && $_POST['status'] == 'In Progress') ? 'selected' : '' ?>>In Progress</option> <option value="Completed" <?= (isset($_POST['status']) && $_POST['status'] == 'Completed') ? 'selected' : '' ?>>Completed</option> </select> </div> <div class="flex justify-between"> <a href="plans.php" class="px-4 py-2 bg-gray-500 text-white rounded hover:bg-gray-600">Cancel</a> <button type="submit" class="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600">Create Plan</button> </div> </form> </main> <!-- Footer --> <footer class="bg-gray-800 text-white text-center p-4"> © 2024 Kaak Alia Bakery. All rights reserved. </footer>// Enable error reporting for debugging (disable in production) ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
// Start output buffering to prevent "headers already sent" errors ob_start();
// Include authentication, database connection, and utility functions require_once DIR . '/../../includes/auth.php'; require_once DIR . '/../../includes/db_connect.php'; require_once DIR . '/../../includes/functions.php'; // Contains sanitize_input()
_SESSION['user_id']; $errors = [];
if (_SERVER['REQUEST_METHOD'] == 'POST') { platform = sanitize_input(content = sanitize_input(scheduled_datetime = sanitize_input(status = 'Scheduled'; // Default status
// Handle media upload if any $media_url = null; if (isset($_FILES['media']) && $_FILES['media']['error'] == UPLOAD_ERR_OK) { $allowed_types = ['image/jpeg', 'image/png', 'video/mp4', 'image/gif']; if (in_array($_FILES['media']['type'], $allowed_types)) { $upload_dir = __DIR__ . '/../../assets/uploads/workspace_posts/'; if (!is_dir($upload_dir)) { mkdir($upload_dir, 0755, true); } // Generate a unique filename to prevent overwriting $file_ext = pathinfo($_FILES['media']['name'], PATHINFO_EXTENSION); $filename = time() . '_' . bin2hex(random_bytes(8)) . '.' . $file_ext; $filepath = $upload_dir . $filename; if (move_uploaded_file($_FILES['media']['tmp_name'], $filepath)) { // Store relative path $media_url = '/assets/uploads/workspace_posts/' . $filename; } else { $errors[] = "Failed to upload media."; } } else { $errors[] = "Unsupported media type. Allowed types: JPEG, PNG, GIF, MP4."; } } // Validation if (empty($platform)) { $errors[] = "Platform is required."; } if (empty($content)) { $errors[] = "Content is required."; } if (empty($scheduled_datetime)) { $errors[] = "Scheduled date and time are required."; } // Validate platform $valid_platforms = ['Instagram', 'Facebook', 'Twitter', 'Other']; if (!in_array($platform, $valid_platforms)) { $errors[] = "Invalid platform selected."; } // Validate scheduled_datetime if (!strtotime($scheduled_datetime)) { $errors[] = "Invalid scheduled date and time."; } if (empty($errors)) { $sql = "INSERT INTO workspace_posts (user_id, platform, content, media_url, scheduled_datetime, status) VALUES (?, ?, ?, ?, ?, ?)"; if ($stmt = $conn->prepare($sql)) { $stmt->bind_param("isssss", $user_id, $platform, $content, $media_url, $scheduled_datetime, $status); if ($stmt->execute()) { header("Location: posts.php"); exit(); } else { $errors[] = "Failed to schedule post. Please try again."; } $stmt->close(); } else { $errors[] = "Database error: Unable to prepare statement."; } }} ?>
<!-- Main Content --> <main class="container mx-auto p-6 mt-24 flex-grow"> <h1 class="text-2xl font-bold mb-6">Schedule New Post</h1> <!-- Display Errors --> <?php if(!empty($errors)): ?> <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-4"> <ul> <?php foreach($errors as $error): ?> <li><?= htmlspecialchars($error) ?></li> <?php endforeach; ?> </ul> </div> <?php endif; ?> <!-- Post Scheduling Form --> <form action="create_post.php" method="POST" enctype="multipart/form-data" class="bg-white p-6 rounded shadow-md"> <div class="mb-4"> <label class="block text-gray-700">Platform<span class="text-red-500">*</span></label> <select name="platform" class="w-full px-3 py-2 border rounded focus:outline-none focus:ring focus:border-blue-300" required> <option value="">-- Select Platform --</option> <option value="Instagram" <?= (isset($_POST['platform']) && $_POST['platform'] == 'Instagram') ? 'selected' : '' ?>>Instagram</option> <option value="Facebook" <?= (isset($_POST['platform']) && $_POST['platform'] == 'Facebook') ? 'selected' : '' ?>>Facebook</option> <option value="Twitter" <?= (isset($_POST['platform']) && $_POST['platform'] == 'Twitter') ? 'selected' : '' ?>>Twitter</option> <option value="Other" <?= (isset($_POST['platform']) && $_POST['platform'] == 'Other') ? 'selected' : '' ?>>Other</option> </select> </div> <div class="mb-4"> <label class="block text-gray-700">Content<span class="text-red-500">*</span></label> <textarea name="content" class="w-full px-3 py-2 border rounded focus:outline-none focus:ring focus:border-blue-300" rows="6" required><?= isset($_POST['content']) ? htmlspecialchars($_POST['content']) : '' ?></textarea> </div> <div class="mb-4"> <label class="block text-gray-700">Media (Optional)</label> <input type="file" name="media" class="w-full px-3 py-2 border rounded focus:outline-none focus:ring focus:border-blue-300"> <p class="text-sm text-gray-500 mt-1">Supported formats: JPEG, PNG, GIF, MP4.</p> </div> <div class="mb-4"> <label class="block text-gray-700">Scheduled Date & Time<span class="text-red-500">*</span></label> <input type="datetime-local" name="scheduled_datetime" class="w-full px-3 py-2 border rounded focus:outline-none focus:ring focus:border-blue-300" required value="<?= isset($_POST['scheduled_datetime']) ? htmlspecialchars($_POST['scheduled_datetime']) : '' ?>"> </div> <div class="flex justify-between"> <a href="posts.php" class="px-4 py-2 bg-gray-500 text-white rounded hover:bg-gray-600">Cancel</a> <button type="submit" class="px-4 py-2 bg-green-500 text-white rounded hover:bg-green-600">Schedule Post</button> </div> </form> </main> <!-- Footer --> <footer class="bg-gray-800 text-white text-center p-4 fixed bottom-0 left-0 w-full"> © 2024 Kaak Alia Bakery. All rights reserved. </footer> - Assistant
Certainly! Below are the complete PHP scripts for Delete, Edit, and View functionalities for Notes, Plans, and Posts within your workspace application. Additionally, the View Plan page includes the ability to change the status of a plan.
Each section includes:
- Delete Functionality
- Edit Functionality
- View Page
1. Delete Functionality
a. Delete Note (
delete_note.php)This script deletes a specific note after verifying that it belongs to the authenticated user.
<?php // /public/workspace/delete_note.php // Enable error reporting for debugging (disable in production) ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); // Start output buffering ob_start(); // Include authentication and database connection require_once __DIR__ . '/../../includes/auth.php'; require_once __DIR__ . '/../../includes/db_connect.php'; // Get note ID from GET parameter $note_id = isset($_GET['id']) ? intval($_GET['id']) : 0; // Get current user ID $user_id = $_SESSION['user_id']; if ($note_id > 0) { // Verify that the note belongs to the user $sql = "SELECT id FROM workspace_notes WHERE id = ? AND user_id = ?"; if ($stmt = $conn->prepare($sql)) { $stmt->bind_param("ii", $note_id, $user_id); $stmt->execute(); $stmt->store_result(); if ($stmt->num_rows > 0) { // Proceed to delete $stmt->close(); $delete_sql = "DELETE FROM workspace_notes WHERE id = ? AND user_id = ?"; if ($delete_stmt = $conn->prepare($delete_sql)) { $delete_stmt->bind_param("ii", $note_id, $user_id); if ($delete_stmt->execute()) { // Success, redirect to notes.php header("Location: notes.php?message=Note+deleted+successfully"); exit(); } else { // Deletion failed header("Location: notes.php?error=Failed+to+delete+note"); exit(); } } else { // Prepare failed header("Location: notes.php?error=Database+error"); exit(); } } else { // Note not found or doesn't belong to user header("Location: notes.php?error=Note+not+found"); exit(); } } else { // Prepare failed header("Location: notes.php?error=Database+error"); exit(); } } else { // Invalid note ID header("Location: notes.php?error=Invalid+note+ID"); exit(); } // Flush the output buffer ob_end_flush(); ?>b. Delete Plan (
delete_plan.php)This script deletes a specific plan after verifying ownership.
<?php // /public/workspace/delete_plan.php // Enable error reporting for debugging (disable in production) ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); // Start output buffering ob_start(); // Include authentication and database connection require_once __DIR__ . '/../../includes/auth.php'; require_once __DIR__ . '/../../includes/db_connect.php'; // Get plan ID from GET parameter $plan_id = isset($_GET['id']) ? intval($_GET['id']) : 0; // Get current user ID $user_id = $_SESSION['user_id']; if ($plan_id > 0) { // Verify that the plan belongs to the user $sql = "SELECT id FROM workspace_plans WHERE id = ? AND user_id = ?"; if ($stmt = $conn->prepare($sql)) { $stmt->bind_param("ii", $plan_id, $user_id); $stmt->execute(); $stmt->store_result(); if ($stmt->num_rows > 0) { // Proceed to delete $stmt->close(); $delete_sql = "DELETE FROM workspace_plans WHERE id = ? AND user_id = ?"; if ($delete_stmt = $conn->prepare($delete_sql)) { $delete_stmt->bind_param("ii", $plan_id, $user_id); if ($delete_stmt->execute()) { // Success, redirect to plans.php header("Location: plans.php?message=Plan+deleted+successfully"); exit(); } else { // Deletion failed header("Location: plans.php?error=Failed+to+delete+plan"); exit(); } } else { // Prepare failed header("Location: plans.php?error=Database+error"); exit(); } } else { // Plan not found or doesn't belong to user header("Location: plans.php?error=Plan+not+found"); exit(); } } else { // Prepare failed header("Location: plans.php?error=Database+error"); exit(); } } else { // Invalid plan ID header("Location: plans.php?error=Invalid+plan+ID"); exit(); } // Flush the output buffer ob_end_flush(); ?>c. Delete Post (
delete_post.php)This script deletes a specific post and its associated media file after verifying ownership.
<?php // /public/workspace/delete_post.php // Enable error reporting for debugging (disable in production) ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); // Start output buffering ob_start(); // Include authentication and database connection require_once __DIR__ . '/../../includes/auth.php'; require_once __DIR__ . '/../../includes/db_connect.php'; // Get post ID from GET parameter $post_id = isset($_GET['id']) ? intval($_GET['id']) : 0; // Get current user ID $user_id = $_SESSION['user_id']; if ($post_id > 0) { // Verify that the post belongs to the user $sql = "SELECT id, media_url FROM workspace_posts WHERE id = ? AND user_id = ?"; if ($stmt = $conn->prepare($sql)) { $stmt->bind_param("ii", $post_id, $user_id); $stmt->execute(); $stmt->bind_result($id, $media_url); if ($stmt->fetch()) { // Proceed to delete $stmt->close(); $delete_sql = "DELETE FROM workspace_posts WHERE id = ? AND user_id = ?"; if ($delete_stmt = $conn->prepare($delete_sql)) { $delete_stmt->bind_param("ii", $post_id, $user_id); if ($delete_stmt->execute()) { // If media exists, delete the file if ($media_url) { $file_path = __DIR__ . '/../../' . ltrim($media_url, '/'); if (file_exists($file_path)) { unlink($file_path); } } // Success, redirect to posts.php header("Location: posts.php?message=Post+deleted+successfully"); exit(); } else { // Deletion failed header("Location: posts.php?error=Failed+to+delete+post"); exit(); } } else { // Prepare failed header("Location: posts.php?error=Database+error"); exit(); } } else { // Post not found or doesn't belong to user header("Location: posts.php?error=Post+not+found"); exit(); } } else { // Prepare failed header("Location: posts.php?error=Database+error"); exit(); } } else { // Invalid post ID header("Location: posts.php?error=Invalid+post+ID"); exit(); } // Flush the output buffer ob_end_flush(); ?>
2. Edit Functionality
a. Edit Note (
edit_note.php)This script allows users to edit an existing note.
<?php // /public/workspace/edit_note.php // Enable error reporting for debugging (disable in production) ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); // Start output buffering ob_start(); // Include authentication, database connection, and utility functions require_once __DIR__ . '/../../includes/auth.php'; require_once __DIR__ . '/../../includes/db_connect.php'; require_once __DIR__ . '/../../includes/functions.php'; // Contains sanitize_input() $user_id = $_SESSION['user_id']; $note_id = isset($_GET['id']) ? intval($_GET['id']) : 0; $errors = []; // Initialize variables $title = ''; $content = ''; // Fetch existing note data if GET request if ($_SERVER['REQUEST_METHOD'] == 'GET') { if ($note_id > 0) { $sql = "SELECT title, content FROM workspace_notes WHERE id = ? AND user_id = ?"; if ($stmt = $conn->prepare($sql)) { $stmt->bind_param("ii", $note_id, $user_id); $stmt->execute(); $stmt->bind_result($title, $content); if ($stmt->fetch()) { // Data fetched } else { $errors[] = "Note not found."; } $stmt->close(); } else { $errors[] = "Database error: Unable to prepare statement."; } } else { $errors[] = "Invalid note ID."; } } // Handle POST request for form submission if ($_SERVER['REQUEST_METHOD'] == 'POST') { $title = sanitize_input($_POST['title'] ?? ''); $content = sanitize_input($_POST['content'] ?? ''); // Validation if (empty($title)) { $errors[] = "Title is required."; } if (empty($content)) { $errors[] = "Content is required."; } if ($note_id > 0) { // Verify ownership before updating $sql_check = "SELECT id FROM workspace_notes WHERE id = ? AND user_id = ?"; if ($stmt_check = $conn->prepare($sql_check)) { $stmt_check->bind_param("ii", $note_id, $user_id); $stmt_check->execute(); $stmt_check->store_result(); if ($stmt_check->num_rows > 0) { // Proceed to update $stmt_check->close(); $update_sql = "UPDATE workspace_notes SET title = ?, content = ?, updated_at = CURRENT_TIMESTAMP() WHERE id = ? AND user_id = ?"; if ($stmt_update = $conn->prepare($update_sql)) { $stmt_update->bind_param("ssii", $title, $content, $note_id, $user_id); if ($stmt_update->execute()) { // Success, redirect to view_note.php header("Location: view_note.php?id=" . $note_id . "&message=Note+updated+successfully"); exit(); } else { $errors[] = "Failed to update note. Please try again."; } } else { $errors[] = "Database error: Unable to prepare update statement."; } } else { $errors[] = "Note not found or you don't have permission to edit this note."; } } else { $errors[] = "Database error: Unable to prepare statement."; } } else { $errors[] = "Invalid note ID."; } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Edit Note - Workspace</title> <!-- Tailwind CSS --> <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet"> <!-- Bootstrap Icons --> <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css" rel="stylesheet"> </head> <body class="bg-gray-100 min-h-screen flex flex-col"> <!-- Header --> <?php include __DIR__ . '/../../includes/header.php'; ?> <!-- Main Content --> <main class="container mx-auto p-6 mt-24 flex-grow"> <h1 class="text-2xl font-bold mb-6">Edit Note</h1> <!-- Display Errors --> <?php if(!empty($errors)): ?> <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-4"> <ul> <?php foreach($errors as $error): ?> <li><?= htmlspecialchars($error) ?></li> <?php endforeach; ?> </ul> </div> <?php endif; ?> <!-- Note Edit Form --> <?php if(empty($errors) || $_SERVER['REQUEST_METHOD'] == 'POST'): ?> <form action="edit_note.php?id=<?= htmlspecialchars($note_id) ?>" method="POST" class="bg-white p-6 rounded shadow-md"> <div class="mb-4"> <label class="block text-gray-700">Title<span class="text-red-500">*</span></label> <input type="text" name="title" class="w-full px-3 py-2 border rounded focus:outline-none focus:ring focus:border-blue-300" required value="<?= htmlspecialchars($title) ?>"> </div> <div class="mb-4"> <label class="block text-gray-700">Content<span class="text-red-500">*</span></label> <textarea name="content" class="w-full px-3 py-2 border rounded focus:outline-none focus:ring focus:border-blue-300" rows="8" required><?= htmlspecialchars($content) ?></textarea> </div> <div class="flex justify-between"> <a href="view_note.php?id=<?= htmlspecialchars($note_id) ?>" class="px-4 py-2 bg-gray-500 text-white rounded hover:bg-gray-600">Cancel</a> <button type="submit" class="px-4 py-2 bg-purple-500 text-white rounded hover:bg-purple-600">Update Note</button> </div> </form> <?php endif; ?> </main> <!-- Footer --> <footer class="bg-gray-800 text-white text-center p-4"> © 2024 Kaak Alia Bakery. All rights reserved. </footer> </body> </html> <?php // Flush the output buffer ob_end_flush(); ?>b. Edit Plan (
edit_plan.php)This script allows users to edit an existing plan, including its status.
<?php // /public/workspace/edit_plan.php // Enable error reporting for debugging (disable in production) ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); // Start output buffering ob_start(); // Include authentication, database connection, and utility functions require_once __DIR__ . '/../../includes/auth.php'; require_once __DIR__ . '/../../includes/db_connect.php'; require_once __DIR__ . '/../../includes/functions.php'; // Contains sanitize_input() $user_id = $_SESSION['user_id']; $plan_id = isset($_GET['id']) ? intval($_GET['id']) : 0; $errors = []; // Initialize variables $title = ''; $description = ''; $due_date = ''; $status = 'Not Started'; // Fetch existing plan data if GET request if ($_SERVER['REQUEST_METHOD'] == 'GET') { if ($plan_id > 0) { $sql = "SELECT title, description, due_date, status FROM workspace_plans WHERE id = ? AND user_id = ?"; if ($stmt = $conn->prepare($sql)) { $stmt->bind_param("ii", $plan_id, $user_id); $stmt->execute(); $stmt->bind_result($title, $description, $due_date, $status); if ($stmt->fetch()) { // Data fetched } else { $errors[] = "Plan not found."; } $stmt->close(); } else { $errors[] = "Database error: Unable to prepare statement."; } } else { $errors[] = "Invalid plan ID."; } } // Handle POST request for form submission if ($_SERVER['REQUEST_METHOD'] == 'POST') { $title = sanitize_input($_POST['title'] ?? ''); $description = sanitize_input($_POST['description'] ?? ''); $due_date = sanitize_input($_POST['due_date'] ?? ''); $status = sanitize_input($_POST['status'] ?? 'Not Started'); // Validation if (empty($title)) { $errors[] = "Title is required."; } if (empty($due_date)) { $errors[] = "Due date is required."; } // Validate status $valid_statuses = ['Not Started', 'In Progress', 'Completed']; if (!in_array($status, $valid_statuses)) { $errors[] = "Invalid status selected."; } // Validate due_date format if (!strtotime($due_date)) { $errors[] = "Invalid due date format."; } if ($plan_id > 0) { // Verify ownership before updating $sql_check = "SELECT id FROM workspace_plans WHERE id = ? AND user_id = ?"; if ($stmt_check = $conn->prepare($sql_check)) { $stmt_check->bind_param("ii", $plan_id, $user_id); $stmt_check->execute(); $stmt_check->store_result(); if ($stmt_check->num_rows > 0) { // Proceed to update $stmt_check->close(); $update_sql = "UPDATE workspace_plans SET title = ?, description = ?, due_date = ?, status = ?, updated_at = CURRENT_TIMESTAMP() WHERE id = ? AND user_id = ?"; if ($stmt_update = $conn->prepare($update_sql)) { $stmt_update->bind_param("ssssii", $title, $description, $due_date, $status, $plan_id, $user_id); if ($stmt_update->execute()) { // Success, redirect to view_plan.php header("Location: view_plan.php?id=" . $plan_id . "&message=Plan+updated+successfully"); exit(); } else { $errors[] = "Failed to update plan. Please try again."; } } else { $errors[] = "Database error: Unable to prepare update statement."; } } else { $errors[] = "Plan not found or you don't have permission to edit this plan."; } } else { $errors[] = "Database error: Unable to prepare statement."; } } else { $errors[] = "Invalid plan ID."; } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Edit Plan - Workspace</title> <!-- Tailwind CSS --> <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet"> <!-- Bootstrap Icons --> <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css" rel="stylesheet"> </head> <body class="bg-gray-100 min-h-screen flex flex-col"> <!-- Header --> <?php include __DIR__ . '/../../includes/header.php'; ?> <!-- Main Content --> <main class="container mx-auto p-6 mt-24 flex-grow"> <h1 class="text-2xl font-bold mb-6">Edit Plan</h1> <!-- Display Errors --> <?php if(!empty($errors)): ?> <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-4"> <ul> <?php foreach($errors as $error): ?> <li><?= htmlspecialchars($error) ?></li> <?php endforeach; ?> </ul> </div> <?php endif; ?> <!-- Plan Edit Form --> <?php if(empty($errors) || $_SERVER['REQUEST_METHOD'] == 'POST'): ?> <form action="edit_plan.php?id=<?= htmlspecialchars($plan_id) ?>" method="POST" class="bg-white p-6 rounded shadow-md"> <div class="mb-4"> <label class="block text-gray-700">Title<span class="text-red-500">*</span></label> <input type="text" name="title" class="w-full px-3 py-2 border rounded focus:outline-none focus:ring focus:border-blue-300" required value="<?= htmlspecialchars($title) ?>"> </div> <div class="mb-4"> <label class="block text-gray-700">Description</label> <textarea name="description" class="w-full px-3 py-2 border rounded focus:outline-none focus:ring focus:border-blue-300" rows="4"><?= htmlspecialchars($description) ?></textarea> </div> <div class="mb-4"> <label class="block text-gray-700">Due Date<span class="text-red-500">*</span></label> <input type="datetime-local" name="due_date" class="w-full px-3 py-2 border rounded focus:outline-none focus:ring focus:border-blue-300" required value="<?= htmlspecialchars($due_date) ?>"> </div> <div class="mb-4"> <label class="block text-gray-700">Status</label> <select name="status" class="w-full px-3 py-2 border rounded focus:outline-none focus:ring focus:border-blue-300"> <option value="Not Started" <?= ($status == 'Not Started') ? 'selected' : '' ?>>Not Started</option> <option value="In Progress" <?= ($status == 'In Progress') ? 'selected' : '' ?>>In Progress</option> <option value="Completed" <?= ($status == 'Completed') ? 'selected' : '' ?>>Completed</option> </select> </div> <div class="flex justify-between"> <a href="view_plan.php?id=<?= htmlspecialchars($plan_id) ?>" class="px-4 py-2 bg-gray-500 text-white rounded hover:bg-gray-600">Cancel</a> <button type="submit" class="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600">Update Plan</button> </div> </form> <?php endif; ?> </main> <!-- Footer --> <footer class="bg-gray-800 text-white text-center p-4"> © 2024 Kaak Alia Bakery. All rights reserved. </footer> </body> </html> <?php // Flush the output buffer ob_end_flush(); ?>c. Edit Post (
edit_post.php)This script allows users to edit an existing post, including uploading a new media file.
<?php // /public/workspace/edit_post.php // Enable error reporting for debugging (disable in production) ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); // Start output buffering ob_start(); // Include authentication, database connection, and utility functions require_once __DIR__ . '/../../includes/auth.php'; require_once __DIR__ . '/../../includes/db_connect.php'; require_once __DIR__ . '/../../includes/functions.php'; // Contains sanitize_input() $user_id = $_SESSION['user_id']; $post_id = isset($_GET['id']) ? intval($_GET['id']) : 0; $errors = []; // Initialize variables $platform = ''; $content = ''; $scheduled_datetime = ''; $status = 'Scheduled'; $media_url = ''; if ($_SERVER['REQUEST_METHOD'] == 'GET') { if ($post_id > 0) { $sql = "SELECT platform, content, media_url, scheduled_datetime, status FROM workspace_posts WHERE id = ? AND user_id = ?"; if ($stmt = $conn->prepare($sql)) { $stmt->bind_param("ii", $post_id, $user_id); $stmt->execute(); $stmt->bind_result($platform, $content, $media_url, $scheduled_datetime, $status); if ($stmt->fetch()) { // Data fetched } else { $errors[] = "Post not found."; } $stmt->close(); } else { $errors[] = "Database error: Unable to prepare statement."; } } else { $errors[] = "Invalid post ID."; } } if ($_SERVER['REQUEST_METHOD'] == 'POST') { $platform = sanitize_input($_POST['platform'] ?? ''); $content = sanitize_input($_POST['content'] ?? ''); $scheduled_datetime = sanitize_input($_POST['scheduled_datetime'] ?? ''); $status = 'Scheduled'; // Default or possibly allow editing // Handle media upload if any $new_media_url = $media_url; // Existing media_url if (isset($_FILES['media']) && $_FILES['media']['error'] != UPLOAD_ERR_NO_FILE) { if ($_FILES['media']['error'] == UPLOAD_ERR_OK) { $allowed_types = ['image/jpeg', 'image/png', 'video/mp4', 'image/gif']; if (in_array($_FILES['media']['type'], $allowed_types)) { $upload_dir = __DIR__ . '/../../assets/uploads/workspace_posts/'; if (!is_dir($upload_dir)) { mkdir($upload_dir, 0755, true); } // Generate a unique filename $file_ext = pathinfo($_FILES['media']['name'], PATHINFO_EXTENSION); $filename = time() . '_' . bin2hex(random_bytes(8)) . '.' . $file_ext; $filepath = $upload_dir . $filename; if (move_uploaded_file($_FILES['media']['tmp_name'], $filepath)) { // Delete old media file if exists if ($media_url) { $old_file_path = __DIR__ . '/../../' . ltrim($media_url, '/'); if (file_exists($old_file_path)) { unlink($old_file_path); } } // Store relative path $new_media_url = '/assets/uploads/workspace_posts/' . $filename; } else { $errors[] = "Failed to upload media."; } } else { $errors[] = "Unsupported media type. Allowed types: JPEG, PNG, GIF, MP4."; } } else { $errors[] = "Error uploading media."; } } // Validation if (empty($platform)) { $errors[] = "Platform is required."; } if (empty($content)) { $errors[] = "Content is required."; } if (empty($scheduled_datetime)) { $errors[] = "Scheduled date and time are required."; } // Validate platform $valid_platforms = ['Instagram', 'Facebook', 'Twitter', 'Other']; if (!in_array($platform, $valid_platforms)) { $errors[] = "Invalid platform selected."; } // Validate scheduled_datetime if (!strtotime($scheduled_datetime)) { $errors[] = "Invalid scheduled date and time."; } if (empty($errors)) { if ($post_id > 0) { // Verify ownership $sql_check = "SELECT id, media_url FROM workspace_posts WHERE id = ? AND user_id = ?"; if ($stmt_check = $conn->prepare($sql_check)) { $stmt_check->bind_param("ii", $post_id, $user_id); $stmt_check->execute(); $stmt_check->store_result(); if ($stmt_check->num_rows > 0) { $stmt_check->bind_result($id, $existing_media_url); $stmt_check->fetch(); $stmt_check->close(); // Update post $update_sql = "UPDATE workspace_posts SET platform = ?, content = ?, media_url = ?, scheduled_datetime = ?, status = ?, updated_at = CURRENT_TIMESTAMP() WHERE id = ? AND user_id = ?"; if ($stmt_update = $conn->prepare($update_sql)) { $stmt_update->bind_param("ssssiii", $platform, $content, $new_media_url, $scheduled_datetime, $status, $post_id, $user_id); if ($stmt_update->execute()) { // Success, redirect to view_post.php header("Location: view_post.php?id=" . $post_id . "&message=Post+updated+successfully"); exit(); } else { $errors[] = "Failed to update post. Please try again."; } } else { $errors[] = "Database error: Unable to prepare update statement."; } } else { $errors[] = "Post not found or you don't have permission to edit this post."; } } else { $errors[] = "Database error: Unable to prepare statement."; } } else { $errors[] = "Invalid post ID."; } } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Edit Post - Workspace</title> <!-- Tailwind CSS --> <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet"> <!-- Bootstrap Icons --> <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css" rel="stylesheet"> </head> <body class="bg-gray-100 min-h-screen flex flex-col"> <!-- Header --> <?php include __DIR__ . '/../../includes/header.php'; ?> <!-- Main Content --> <main class="container mx-auto p-6 mt-24 flex-grow"> <h1 class="text-2xl font-bold mb-6">Edit Post</h1> <!-- Display Errors --> <?php if(!empty($errors)): ?> <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-4"> <ul> <?php foreach($errors as $error): ?> <li><?= htmlspecialchars($error) ?></li> <?php endforeach; ?> </ul> </div> <?php endif; ?> <!-- Post Edit Form --> <?php if(empty($errors) || $_SERVER['REQUEST_METHOD'] == 'POST'): ?> <form action="edit_post.php?id=<?= htmlspecialchars($post_id) ?>" method="POST" enctype="multipart/form-data" class="bg-white p-6 rounded shadow-md"> <div class="mb-4"> <label class="block text-gray-700">Platform<span class="text-red-500">*</span></label> <select name="platform" class="w-full px-3 py-2 border rounded focus:outline-none focus:ring focus:border-blue-300" required> <option value="">-- Select Platform --</option> <option value="Instagram" <?= ($platform == 'Instagram') ? 'selected' : '' ?>>Instagram</option> <option value="Facebook" <?= ($platform == 'Facebook') ? 'selected' : '' ?>>Facebook</option> <option value="Twitter" <?= ($platform == 'Twitter') ? 'selected' : '' ?>>Twitter</option> <option value="Other" <?= ($platform == 'Other') ? 'selected' : '' ?>>Other</option> </select> </div> <div class="mb-4"> <label class="block text-gray-700">Content<span class="text-red-500">*</span></label> <textarea name="content" class="w-full px-3 py-2 border rounded focus:outline-none focus:ring focus:border-blue-300" rows="6" required><?= htmlspecialchars($content) ?></textarea> </div> <div class="mb-4"> <label class="block text-gray-700">Media (Optional)</label> <?php if ($media_url): ?> <div class="mb-2"> <?php $file_ext = strtolower(pathinfo($media_url, PATHINFO_EXTENSION)); if (in_array($file_ext, ['jpg', 'jpeg', 'png', 'gif'])): ?> <img src="<?= htmlspecialchars($media_url) ?>" alt="Media" class="max-w-sm"> <?php elseif (in_array($file_ext, ['mp4'])): ?> <video controls class="max-w-sm"> <source src="<?= htmlspecialchars($media_url) ?>" type="video/mp4"> Your browser does not support the video tag. </video> <?php endif; ?> </div> <?php endif; ?> <input type="file" name="media" class="w-full px-3 py-2 border rounded focus:outline-none focus:ring focus:border-blue-300"> <p class="text-sm text-gray-500 mt-1">Supported formats: JPEG, PNG, GIF, MP4.</p> </div> <div class="mb-4"> <label class="block text-gray-700">Scheduled Date & Time<span class="text-red-500">*</span></label> <input type="datetime-local" name="scheduled_datetime" class="w-full px-3 py-2 border rounded focus:outline-none focus:ring focus:border-blue-300" required value="<?= htmlspecialchars($scheduled_datetime) ?>"> </div> <div class="flex justify-between"> <a href="view_post.php?id=<?= htmlspecialchars($post_id) ?>" class="px-4 py-2 bg-gray-500 text-white rounded hover:bg-gray-600">Cancel</a> <button type="submit" class="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600">Update Post</button> </div> </form> <?php endif; ?> </main> <!-- Footer --> <footer class="bg-gray-800 text-white text-center p-4"> © 2024 Kaak Alia Bakery. All rights reserved. </footer> </body> </html> <?php // Flush the output buffer ob_end_flush(); ?>
3. View Page
a. View Note (
view_note.php)This page displays the details of a specific note and provides options to edit or delete it.
<?php // /public/workspace/view_note.php // Enable error reporting for debugging (disable in production) ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); // Start output buffering ob_start(); // Include authentication and database connection require_once __DIR__ . '/../../includes/auth.php'; require_once __DIR__ . '/../../includes/db_connect.php'; $user_id = $_SESSION['user_id']; $note_id = isset($_GET['id']) ? intval($_GET['id']) : 0; $note = null; $errors = []; // Fetch note details if ($note_id > 0) { $sql = "SELECT title, content, created_at, updated_at FROM workspace_notes WHERE id = ? AND user_id = ?"; if ($stmt = $conn->prepare($sql)) { $stmt->bind_param("ii", $note_id, $user_id); $stmt->execute(); $stmt->bind_result($title, $content, $created_at, $updated_at); if ($stmt->fetch()) { $note = [ 'id' => $note_id, 'title' => $title, 'content' => $content, 'created_at' => $created_at, 'updated_at' => $updated_at ]; } else { $errors[] = "Note not found."; } $stmt->close(); } else { $errors[] = "Database error: Unable to prepare statement."; } } else { $errors[] = "Invalid note ID."; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>View Note - Workspace</title> <!-- Tailwind CSS --> <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet"> <!-- Bootstrap Icons --> <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css" rel="stylesheet"> </head> <body class="bg-gray-100 min-h-screen flex flex-col"> <!-- Header --> <?php include __DIR__ . '/../../includes/header.php'; ?> <!-- Main Content --> <main class="container mx-auto p-6 mt-24 flex-grow"> <?php if(!empty($errors)): ?> <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-4"> <ul> <?php foreach($errors as $error): ?> <li><?= htmlspecialchars($error) ?></li> <?php endforeach; ?> </ul> </div> <?php endif; ?> <?php if($note): ?> <div class="bg-white p-6 rounded shadow-md"> <h1 class="text-2xl font-bold mb-4"><?= htmlspecialchars($note['title']) ?></h1> <p class="text-gray-700 mb-4"><?= nl2br(htmlspecialchars($note['content'])) ?></p> <p class="text-sm text-gray-500">Created at: <?= htmlspecialchars($note['created_at']) ?></p> <p class="text-sm text-gray-500">Last updated: <?= htmlspecialchars($note['updated_at']) ?></p> <div class="mt-6 flex space-x-4"> <a href="edit_note.php?id=<?= htmlspecialchars($note['id']) ?>" class="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600">Edit</a> <a href="delete_note.php?id=<?= htmlspecialchars($note['id']) ?>" class="px-4 py-2 bg-red-500 text-white rounded hover:bg-red-600" onclick="return confirm('Are you sure you want to delete this note?');">Delete</a> <a href="notes.php" class="px-4 py-2 bg-gray-500 text-white rounded hover:bg-gray-600">Back to Notes</a> </div> </div> <?php endif; ?> </main> <!-- Footer --> <footer class="bg-gray-800 text-white text-center p-4"> © 2024 Kaak Alia Bakery. All rights reserved. </footer> </body> </html> <?php // Flush the output buffer ob_end_flush(); ?>b. View Plan (
view_plan.php)This page displays the details of a specific plan and allows the user to change its status.
<?php // /public/workspace/view_plan.php // Enable error reporting for debugging (disable in production) ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); // Start output buffering ob_start(); // Include authentication, database connection, and utility functions require_once __DIR__ . '/../../includes/auth.php'; require_once __DIR__ . '/../../includes/db_connect.php'; require_once __DIR__ . '/../../includes/functions.php'; // Contains sanitize_input() $user_id = $_SESSION['user_id']; $plan_id = isset($_GET['id']) ? intval($_GET['id']) : 0; $plan = null; $errors = []; $success_message = ''; // Handle status update if POST request if ($_SERVER['REQUEST_METHOD'] == 'POST') { $new_status = sanitize_input($_POST['status'] ?? ''); $valid_statuses = ['Not Started', 'In Progress', 'Completed']; if (!in_array($new_status, $valid_statuses)) { $errors[] = "Invalid status selected."; } else { if ($plan_id > 0) { // Verify ownership $sql_check = "SELECT id FROM workspace_plans WHERE id = ? AND user_id = ?"; if ($stmt_check = $conn->prepare($sql_check)) { $stmt_check->bind_param("ii", $plan_id, $user_id); $stmt_check->execute(); $stmt_check->store_result(); if ($stmt_check->num_rows > 0) { $stmt_check->close(); // Update status $update_sql = "UPDATE workspace_plans SET status = ?, updated_at = CURRENT_TIMESTAMP() WHERE id = ? AND user_id = ?"; if ($stmt_update = $conn->prepare($update_sql)) { $stmt_update->bind_param("sii", $new_status, $plan_id, $user_id); if ($stmt_update->execute()) { $success_message = "Status updated successfully."; } else { $errors[] = "Failed to update status. Please try again."; } $stmt_update->close(); } else { $errors[] = "Database error: Unable to prepare update statement."; } } else { $errors[] = "Plan not found or you don't have permission to edit this plan."; } } else { $errors[] = "Database error: Unable to prepare statement."; } } else { $errors[] = "Invalid plan ID."; } } } // Fetch plan details if ($plan_id > 0) { $sql = "SELECT title, description, due_date, status, created_at, updated_at FROM workspace_plans WHERE id = ? AND user_id = ?"; if ($stmt = $conn->prepare($sql)) { $stmt->bind_param("ii", $plan_id, $user_id); $stmt->execute(); $stmt->bind_result($title, $description, $due_date, $status, $created_at, $updated_at); if ($stmt->fetch()) { $plan = [ 'id' => $plan_id, 'title' => $title, 'description' => $description, 'due_date' => $due_date, 'status' => $status, 'created_at' => $created_at, 'updated_at' => $updated_at ]; } else { $errors[] = "Plan not found."; } $stmt->close(); } else { $errors[] = "Database error: Unable to prepare statement."; } } else { $errors[] = "Invalid plan ID."; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>View Plan - Workspace</title> <!-- Tailwind CSS --> <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet"> <!-- Bootstrap Icons --> <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css" rel="stylesheet"> </head> <body class="bg-gray-100 min-h-screen flex flex-col"> <!-- Header --> <?php include __DIR__ . '/../../includes/header.php'; ?> <!-- Main Content --> <main class="container mx-auto p-6 mt-24 flex-grow"> <?php if(!empty($errors)): ?> <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-4"> <ul> <?php foreach($errors as $error): ?> <li><?= htmlspecialchars($error) ?></li> <?php endforeach; ?> </ul> </div> <?php endif; ?> <?php if($plan): ?> <?php if($success_message): ?> <div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded mb-4"> <?= htmlspecialchars($success_message) ?> </div> <?php endif; ?> <div class="bg-white p-6 rounded shadow-md"> <h1 class="text-2xl font-bold mb-4"><?= htmlspecialchars($plan['title']) ?></h1> <p class="text-gray-700 mb-4"><?= nl2br(htmlspecialchars($plan['description'])) ?></p> <p class="text-gray-700 mb-4"> Due Date: <?= htmlspecialchars(date("F j, Y, g:i a", strtotime($plan['due_date']))) ?><br> Status: <?= htmlspecialchars($plan['status']) ?> </p> <p class="text-sm text-gray-500">Created at: <?= htmlspecialchars($plan['created_at']) ?></p> <p class="text-sm text-gray-500">Last updated: <?= htmlspecialchars($plan['updated_at']) ?></p> <!-- Status Update Form --> <form action="view_plan.php?id=<?= htmlspecialchars($plan['id']) ?>" method="POST" class="mt-6"> <label class="block text-gray-700 mb-2">Update Status</label> <select name="status" class="w-full px-3 py-2 border rounded focus:outline-none focus:ring focus:border-blue-300"> <option value="Not Started" <?= ($plan['status'] == 'Not Started') ? 'selected' : '' ?>>Not Started</option> <option value="In Progress" <?= ($plan['status'] == 'In Progress') ? 'selected' : '' ?>>In Progress</option> <option value="Completed" <?= ($plan['status'] == 'Completed') ? 'selected' : '' ?>>Completed</option> </select> <button type="submit" class="mt-3 px-4 py-2 bg-green-500 text-white rounded hover:bg-green-600">Update Status</button> </form> <div class="mt-6 flex space-x-4"> <a href="edit_plan.php?id=<?= htmlspecialchars($plan['id']) ?>" class="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600">Edit</a> <a href="delete_plan.php?id=<?= htmlspecialchars($plan['id']) ?>" class="px-4 py-2 bg-red-500 text-white rounded hover:bg-red-600" onclick="return confirm('Are you sure you want to delete this plan?');">Delete</a> <a href="plans.php" class="px-4 py-2 bg-gray-500 text-white rounded hover:bg-gray-600">Back to Plans</a> </div> </div> <?php endif; ?> </main> <!-- Footer --> <footer class="bg-gray-800 text-white text-center p-4"> © 2024 Kaak Alia Bakery. All rights reserved. </footer> </body> </html> <?php // Flush the output buffer ob_end_flush(); ?>c. View Post (
view_post.php)This page displays the details of a specific post and provides options to edit or delete it.
<?php // /public/workspace/view_post.php // Enable error reporting for debugging (disable in production) ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); // Start output buffering ob_start(); // Include authentication and database connection require_once __DIR__ . '/../../includes/auth.php'; require_once __DIR__ . '/../../includes/db_connect.php'; $user_id = $_SESSION['user_id']; $post_id = isset($_GET['id']) ? intval($_GET['id']) : 0; $post = null; $errors = []; // Fetch post details if ($post_id > 0) { $sql = "SELECT platform, content, media_url, scheduled_datetime, status, created_at, updated_at FROM workspace_posts WHERE id = ? AND user_id = ?"; if ($stmt = $conn->prepare($sql)) { $stmt->bind_param("ii", $post_id, $user_id); $stmt->execute(); $stmt->bind_result($platform, $content, $media_url, $scheduled_datetime, $status, $created_at, $updated_at); if ($stmt->fetch()) { $post = [ 'id' => $post_id, 'platform' => $platform, 'content' => $content, 'media_url' => $media_url, 'scheduled_datetime' => $scheduled_datetime, 'status' => $status, 'created_at' => $created_at, 'updated_at' => $updated_at ]; } else { $errors[] = "Post not found."; } $stmt->close(); } else { $errors[] = "Database error: Unable to prepare statement."; } } else { $errors[] = "Invalid post ID."; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>View Post - Workspace</title> <!-- Tailwind CSS --> <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet"> <!-- Bootstrap Icons --> <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css" rel="stylesheet"> </head> <body class="bg-gray-100 min-h-screen flex flex-col"> <!-- Header --> <?php include __DIR__ . '/../../includes/header.php'; ?> <!-- Main Content --> <main class="container mx-auto p-6 mt-24 flex-grow"> <?php if(!empty($errors)): ?> <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-4"> <ul> <?php foreach($errors as $error): ?> <li><?= htmlspecialchars($error) ?></li> <?php endforeach; ?> </ul> </div> <?php endif; ?> <?php if($post): ?> <div class="bg-white p-6 rounded shadow-md"> <h1 class="text-2xl font-bold mb-4"><?= htmlspecialchars($post['platform']) ?> Post</h1> <p class="text-gray-700 mb-4"><?= nl2br(htmlspecialchars($post['content'])) ?></p> <?php if ($post['media_url']): ?> <div class="mb-4"> <?php $file_ext = strtolower(pathinfo($post['media_url'], PATHINFO_EXTENSION)); if (in_array($file_ext, ['jpg', 'jpeg', 'png', 'gif'])): ?> <img src="<?= htmlspecialchars($post['media_url']) ?>" alt="Media" class="max-w-sm"> <?php elseif (in_array($file_ext, ['mp4'])): ?> <video controls class="max-w-sm"> <source src="<?= htmlspecialchars($post['media_url']) ?>" type="video/mp4"> Your browser does not support the video tag. </video> <?php endif; ?> </div> <?php endif; ?> <p class="text-gray-700 mb-4"> Scheduled Date & Time: <?= htmlspecialchars(date("F j, Y, g:i a", strtotime($post['scheduled_datetime']))) ?><br> Status: <?= htmlspecialchars($post['status']) ?> </p> <p class="text-sm text-gray-500">Created at: <?= htmlspecialchars($post['created_at']) ?></p> <p class="text-sm text-gray-500">Last updated: <?= htmlspecialchars($post['updated_at']) ?></p> <div class="mt-6 flex space-x-4"> <a href="edit_post.php?id=<?= htmlspecialchars($post['id']) ?>" class="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600">Edit</a> <a href="delete_post.php?id=<?= htmlspecialchars($post['id']) ?>" class="px-4 py-2 bg-red-500 text-white rounded hover:bg-red-600" onclick="return confirm('Are you sure you want to delete this post?');">Delete</a> <a href="posts.php" class="px-4 py-2 bg-gray-500 text-white rounded hover:bg-gray-600">Back to Posts</a> </div> </div> <?php endif; ?> </main> <!-- Footer --> <footer class="bg-gray-800 text-white text-center p-4"> © 2024 Kaak Alia Bakery. All rights reserved. </footer> </body> </html> <?php // Flush the output buffer ob_end_flush(); ?>
4. Important Considerations
a. Security
- Authentication: Ensure that all scripts include proper authentication by verifying
$_SESSION['user_id']. - Authorization: Verify that the resource (note, plan, or post) belongs to the authenticated user before performing any operations.
- Sanitization: Always sanitize user inputs using functions like
sanitize_input()to prevent XSS and SQL Injection attacks. - Prepared Statements: Use prepared statements for all database interactions to enhance security.
- File Uploads: Validate uploaded files by checking their MIME types and use unique filenames to prevent overwriting. Also, ensure that the upload directories are not publicly accessible or execute permissions are restricted.
b. User Feedback
- Utilize success and error messages to inform users about the outcome of their actions.
- Redirect users appropriately after operations like delete or update to enhance user experience.
c. Consistent UI
- Ensure that all forms and actions maintain a consistent look and feel, aligning with your existing design using Tailwind CSS and Bootstrap Icons.
d. Error Handling
- Display meaningful error messages to users without exposing sensitive information.
- Log errors server-side for debugging purposes while keeping user-facing messages generic.
By implementing the above scripts, your workspace application will now support Create, View, Edit, and Delete operations for Notes, Plans, and Posts, along with the ability to change the status of plans directly from their view pages. Make sure to place these scripts in the appropriate directories and adjust paths if necessary based on your project's structure.
If you have any further questions or need additional functionalities, feel free to ask!