Compare commits

..

3 Commits

Author SHA1 Message Date
Flatlogic Bot
8ab8df671a fixed error - Error saving job statuses 2026-01-23 15:54:08 +00:00
Flatlogic Bot
dc3ef2491a Onboarding Wizard and File & Folder Manager 2026-01-23 13:44:03 +00:00
Flatlogic Bot
b8eda13904 initial creation 2026-01-23 13:40:12 +00:00
6 changed files with 1234 additions and 455 deletions

View File

@ -1,346 +1,77 @@
:root {
--color-bg: #ffffff;
--color-text: #1a1a1a;
--color-primary: #2563EB; /* Vibrant Blue */
--color-secondary: #000000;
--color-accent: #A3E635; /* Lime Green */
--color-surface: #f8f9fa;
--font-heading: 'Space Grotesk', sans-serif;
--font-body: 'Inter', sans-serif;
--border-width: 2px;
--shadow-hard: 5px 5px 0px #000;
--shadow-hover: 8px 8px 0px #000;
--radius-pill: 50rem;
--radius-card: 1rem;
--primary: #111827;
--secondary: #6B7280;
--accent: #3B82F6;
--bg: #F9FAFB;
--surface: #FFFFFF;
--border: #E5E7EB;
}
body {
font-family: var(--font-body);
background-color: var(--color-bg);
color: var(--color-text);
overflow-x: hidden;
font-family: 'Inter', system-ui, -apple-system, sans-serif;
background-color: var(--bg);
color: var(--primary);
font-size: 0.875rem;
}
h1, h2, h3, h4, h5, h6, .navbar-brand {
font-family: var(--font-heading);
letter-spacing: -0.03em;
}
/* Utilities */
.text-primary { color: var(--color-primary) !important; }
.bg-black { background-color: #000 !important; }
.text-white { color: #fff !important; }
.shadow-hard { box-shadow: var(--shadow-hard); }
.border-2-black { border: var(--border-width) solid #000; }
.py-section { padding-top: 5rem; padding-bottom: 5rem; }
/* Navbar */
.navbar {
background: rgba(255, 255, 255, 0.9);
backdrop-filter: blur(10px);
border-bottom: var(--border-width) solid transparent;
transition: all 0.3s;
padding-top: 1rem;
padding-bottom: 1rem;
background-color: var(--surface);
border-bottom: 1px solid var(--border);
}
.navbar.scrolled {
border-bottom-color: #000;
padding-top: 0.5rem;
padding-bottom: 0.5rem;
.card {
border: 1px solid var(--border);
border-radius: 4px;
box-shadow: none;
}
.brand-text {
font-size: 1.5rem;
font-weight: 800;
}
.nav-link {
font-weight: 500;
color: var(--color-text);
margin-left: 1rem;
position: relative;
}
.nav-link:hover, .nav-link.active {
color: var(--color-primary);
}
/* Buttons */
.btn {
font-weight: 700;
font-family: var(--font-heading);
padding: 0.8rem 2rem;
border-radius: var(--radius-pill);
border: var(--border-width) solid #000;
transition: all 0.2s cubic-bezier(0.25, 1, 0.5, 1);
box-shadow: var(--shadow-hard);
}
.btn:hover {
transform: translate(-2px, -2px);
box-shadow: var(--shadow-hover);
}
.btn:active {
transform: translate(2px, 2px);
box-shadow: 0 0 0 #000;
border-radius: 4px;
font-weight: 500;
padding: 0.5rem 1rem;
}
.btn-primary {
background-color: var(--color-primary);
border-color: #000;
color: #fff;
background-color: var(--primary);
border-color: var(--primary);
}
.btn-primary:hover {
background-color: #1d4ed8;
border-color: #000;
color: #fff;
background-color: #1F2937;
border-color: #1F2937;
}
.btn-outline-dark {
background-color: #fff;
color: #000;
.table {
border: 1px solid var(--border);
background: var(--surface);
}
.btn-cta {
background-color: var(--color-accent);
color: #000;
}
.btn-cta:hover {
background-color: #8cc629;
color: #000;
}
/* Hero Section */
.hero-section {
min-height: 100vh;
padding-top: 80px;
}
.background-blob {
position: absolute;
border-radius: 50%;
filter: blur(80px);
opacity: 0.6;
z-index: 1;
}
.blob-1 {
top: -10%;
right: -10%;
width: 600px;
height: 600px;
background: radial-gradient(circle, var(--color-accent), transparent);
}
.blob-2 {
bottom: 10%;
left: -10%;
width: 500px;
height: 500px;
background: radial-gradient(circle, var(--color-primary), transparent);
}
.highlight-text {
background: linear-gradient(120deg, transparent 0%, transparent 40%, var(--color-accent) 40%, var(--color-accent) 100%);
background-repeat: no-repeat;
background-size: 100% 40%;
background-position: 0 88%;
padding: 0 5px;
}
.dot { color: var(--color-primary); }
.badge-pill {
display: inline-block;
padding: 0.5rem 1rem;
border: 2px solid #000;
border-radius: 50px;
font-weight: 700;
background: #fff;
box-shadow: 4px 4px 0 #000;
font-family: var(--font-heading);
font-size: 0.9rem;
}
/* Marquee */
.marquee-container {
overflow: hidden;
white-space: nowrap;
border-top: 2px solid #000;
border-bottom: 2px solid #000;
}
.rotate-divider {
transform: rotate(-2deg) scale(1.05);
z-index: 10;
position: relative;
margin-top: -50px;
margin-bottom: 30px;
}
.marquee-content {
display: inline-block;
animation: marquee 20s linear infinite;
font-family: var(--font-heading);
font-weight: 700;
font-size: 1.5rem;
letter-spacing: 2px;
}
@keyframes marquee {
0% { transform: translateX(0); }
100% { transform: translateX(-50%); }
}
/* Portfolio Cards */
.project-card {
border: 2px solid #000;
border-radius: var(--radius-card);
overflow: hidden;
background: #fff;
transition: transform 0.3s ease;
box-shadow: var(--shadow-hard);
height: 100%;
display: flex;
flex-direction: column;
}
.project-card:hover {
transform: translateY(-10px);
box-shadow: 8px 8px 0 #000;
}
.card-img-holder {
height: 250px;
display: flex;
align-items: center;
justify-content: center;
border-bottom: 2px solid #000;
position: relative;
font-size: 4rem;
}
.placeholder-art {
transition: transform 0.3s ease;
}
.project-card:hover .placeholder-art {
transform: scale(1.2) rotate(10deg);
}
.bg-soft-blue { background-color: #e0f2fe; }
.bg-soft-green { background-color: #dcfce7; }
.bg-soft-purple { background-color: #f3e8ff; }
.bg-soft-yellow { background-color: #fef9c3; }
.category-tag {
position: absolute;
top: 15px;
right: 15px;
background: #000;
color: #fff;
padding: 5px 12px;
border-radius: 20px;
.table th {
background: #F3F4F6;
font-weight: 600;
text-transform: uppercase;
font-size: 0.75rem;
font-weight: 700;
letter-spacing: 0.025em;
border-bottom: 1px solid var(--border);
}
.card-body { padding: 1.5rem; }
.link-arrow {
text-decoration: none;
color: #000;
font-weight: 700;
display: inline-flex;
align-items: center;
margin-top: auto;
.activity-log {
max-height: 400px;
overflow-y: auto;
}
.link-arrow i { transition: transform 0.2s; margin-left: 5px; }
.link-arrow:hover i { transform: translateX(5px); }
/* About */
.about-image-stack {
position: relative;
height: 400px;
width: 100%;
.log-entry {
padding: 0.75rem;
border-bottom: 1px solid var(--border);
font-size: 0.8rem;
}
.stack-card {
position: absolute;
width: 80%;
height: 100%;
border-radius: var(--radius-card);
border: 2px solid #000;
box-shadow: var(--shadow-hard);
left: 10%;
transform: rotate(-3deg);
background-size: cover;
.log-entry:last-child {
border-bottom: none;
}
/* Forms */
.form-control {
border: 2px solid #000;
border-radius: 0.5rem;
padding: 1rem;
.badge {
border-radius: 9999px;
font-weight: 500;
background: #f8f9fa;
}
.form-control:focus {
box-shadow: 4px 4px 0 var(--color-primary);
border-color: #000;
background: #fff;
}
/* Animations */
.animate-up {
opacity: 0;
transform: translateY(30px);
animation: fadeUp 0.8s ease forwards;
}
.delay-100 { animation-delay: 0.1s; }
.delay-200 { animation-delay: 0.2s; }
@keyframes fadeUp {
to {
opacity: 1;
transform: translateY(0);
}
}
/* Social */
.social-links a {
transition: transform 0.2s;
display: inline-block;
}
.social-links a:hover {
transform: scale(1.2) rotate(10deg);
color: var(--color-accent) !important;
}
/* Responsive */
@media (max-width: 991px) {
.rotate-divider {
transform: rotate(0);
margin-top: 0;
margin-bottom: 2rem;
}
.hero-section {
padding-top: 120px;
text-align: center;
min-height: auto;
padding-bottom: 100px;
}
.display-1 { font-size: 3.5rem; }
.blob-1 { width: 300px; height: 300px; right: -20%; }
.blob-2 { width: 300px; height: 300px; left: -20%; }
}
padding: 0.25rem 0.625rem;
}

View File

@ -0,0 +1,147 @@
-- Initial Schema for Repairs Multi-tenant App
CREATE TABLE IF NOT EXISTS companies (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
uprn_required BOOLEAN DEFAULT FALSE,
onboarding_complete BOOLEAN DEFAULT FALSE, -- Added for onboarding wizard
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
company_id INT NOT NULL,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
role ENUM('admin', 'standard') DEFAULT 'standard',
FOREIGN KEY (company_id) REFERENCES companies(id)
);
CREATE TABLE IF NOT EXISTS clients (
id INT AUTO_INCREMENT PRIMARY KEY,
company_id INT NOT NULL,
name VARCHAR(255) NOT NULL,
is_active BOOLEAN DEFAULT TRUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (company_id) REFERENCES companies(id)
);
CREATE TABLE IF NOT EXISTS job_statuses (
id INT AUTO_INCREMENT PRIMARY KEY,
company_id INT NOT NULL,
name VARCHAR(255) NOT NULL,
is_default BOOLEAN DEFAULT FALSE,
FOREIGN KEY (company_id) REFERENCES companies(id)
);
CREATE TABLE IF NOT EXISTS job_folders (
id INT AUTO_INCREMENT PRIMARY KEY,
company_id INT NOT NULL,
name VARCHAR(255) NOT NULL,
is_required BOOLEAN DEFAULT FALSE,
FOREIGN KEY (company_id) REFERENCES companies(id)
);
CREATE TABLE IF NOT EXISTS jobs (
id INT AUTO_INCREMENT PRIMARY KEY,
company_id INT NOT NULL,
job_ref VARCHAR(100) NOT NULL,
uprn VARCHAR(100),
address_1 VARCHAR(255),
address_2 VARCHAR(255),
address_3 VARCHAR(255),
postcode VARCHAR(20),
description TEXT,
status_id INT,
client_id INT,
works_approved BOOLEAN DEFAULT FALSE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UNIQUE(company_id, job_ref),
FOREIGN KEY (company_id) REFERENCES companies(id),
FOREIGN KEY (status_id) REFERENCES job_statuses(id),
FOREIGN KEY (client_id) REFERENCES clients(id)
);
CREATE TABLE IF NOT EXISTS job_job_folders (
id INT AUTO_INCREMENT PRIMARY KEY,
job_id INT NOT NULL,
company_id INT NOT NULL,
folder_id INT NOT NULL, -- Refers to job_folders.id, for required folders
name VARCHAR(255) NOT NULL, -- For custom folders or required folder name copy
is_custom BOOLEAN DEFAULT FALSE, -- TRUE for user-added folders, FALSE for required folders
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (job_id) REFERENCES jobs(id),
FOREIGN KEY (company_id) REFERENCES companies(id),
FOREIGN KEY (folder_id) REFERENCES job_folders(id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS job_files (
id INT AUTO_INCREMENT PRIMARY KEY,
job_id INT NOT NULL,
job_job_folder_id INT NOT NULL, -- Refers to job_job_folders.id
company_id INT NOT NULL,
user_id INT NOT NULL,
filename VARCHAR(255) NOT NULL,
filepath VARCHAR(512) NOT NULL,
mimetype VARCHAR(100),
size INT, -- size in bytes
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (job_id) REFERENCES jobs(id),
FOREIGN KEY (job_job_folder_id) REFERENCES job_job_folders(id) ON DELETE CASCADE,
FOREIGN KEY (company_id) REFERENCES companies(id),
FOREIGN KEY (user_id) REFERENCES users(id)
);
CREATE TABLE IF NOT EXISTS activity_logs (
id INT AUTO_INCREMENT PRIMARY KEY,
job_id INT NOT NULL,
company_id INT NOT NULL,
user_id INT NOT NULL,
user_name VARCHAR(255),
event_type VARCHAR(100),
field_name VARCHAR(100),
old_value TEXT,
new_value TEXT,
metadata JSON,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (job_id) REFERENCES jobs(id),
FOREIGN KEY (company_id) REFERENCES companies(id)
);
-- Add onboarding_complete column to companies table if it doesn't exist
-- This needs to be a separate ALTER TABLE statement after the CREATE TABLE IF NOT EXISTS for companies
-- Because adding a column with DEFAULT value within CREATE TABLE IF NOT EXISTS doesn't work as expected if the table already exists
-- No longer needed if added directly to CREATE TABLE
-- INSERT IGNORE needs to be used with a subquery if we want to update existing rows only
-- Seed Initial Demo Company
INSERT IGNORE INTO companies (name, onboarding_complete) VALUES ('Repairs Pro Ltd', TRUE);
SET @company_id = (SELECT id FROM companies WHERE name = 'Repairs Pro Ltd' LIMIT 1);
-- Users (will only insert if not exists)
INSERT IGNORE INTO users (company_id, name, email, role) VALUES (@company_id, 'Admin User', 'admin@repairspro.com', 'admin');
-- Job Statuses (will only insert if not exists for the company and name)
INSERT IGNORE INTO job_statuses (company_id, name, is_default) VALUES (@company_id, 'To Be Surveyed', TRUE);
INSERT IGNORE INTO job_statuses (company_id, name, is_default) VALUES (@company_id, 'Booking Required', FALSE);
INSERT IGNORE INTO job_statuses (company_id, name, is_default) VALUES (@company_id, 'Completed', FALSE);
-- Clients (will only insert if not exists for the company and name)
INSERT IGNORE INTO clients (company_id, name) VALUES (@company_id, 'Main Housing Assoc');
-- Job Folders (will only insert if not exists for the company and name)
INSERT IGNORE INTO job_folders (company_id, name, is_required) VALUES (@company_id, 'PO', TRUE);
INSERT IGNORE INTO job_folders (company_id, name, is_required) VALUES (@company_id, 'Quote', TRUE);
INSERT IGNORE INTO job_folders (company_id, name, is_required) VALUES (@company_id, 'Photos', TRUE);
INSERT IGNORE INTO job_folders (company_id, name, is_required) VALUES (@company_id, 'RAMS', TRUE);
INSERT IGNORE INTO job_folders (company_id, name, is_required) VALUES (@company_id, 'Invoices', TRUE);
-- Example: For an existing job, ensure required folders are present in job_job_folders
-- This logic would be handled when a job is created or when required folders are updated globally.
-- For seeding purposes, let's assume job_id 1 already exists and we want to link its required folders.
-- This part should ideally be in a migration script that runs after job creation.
-- For now, commenting out to avoid issues with non-existent job IDs.
-- INSERT IGNORE INTO job_job_folders (job_id, company_id, folder_id, name, is_custom)
-- SELECT j.id, j.company_id, jf.id, jf.name, FALSE
-- FROM jobs j
-- JOIN job_folders jf ON j.company_id = jf.company_id
-- WHERE j.id = (SELECT id FROM jobs LIMIT 1) AND jf.is_required = TRUE; -- Link to the first job created for the demo company

209
includes/helpers.php Normal file
View File

@ -0,0 +1,209 @@
<?php
require_once __DIR__ . '/../db/config.php';
function getCurrentAppUser() {
// In a real application, this would fetch the logged-in user from a session or token.
// For now, we'll hardcode the admin user from the seeded data.
// This assumes the admin user has company_id 1 (from the seed).
// This will need to be replaced with proper authentication later.
static $user = null;
if ($user === null) {
$user = db()->query("SELECT * FROM users WHERE role = 'admin' LIMIT 1")->fetch(PDO::FETCH_ASSOC);
}
return $user;
}
function get_current_company_id() {
$user = getCurrentAppUser();
return $user ? $user['company_id'] : null;
}
function get_company_onboarding_status($company_id) {
$stmt = db()->prepare("SELECT onboarding_complete FROM companies WHERE id = ?");
$stmt->execute([$company_id]);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
return $result ? (bool)$result['onboarding_complete'] : false;
}
function logActivity($job_id, $event_type, $field_name = null, $old_value = null, $new_value = null) {
$user = getCurrentAppUser();
if (!$user) { return; } // Don't log if no user context
$stmt = db()->prepare("INSERT INTO activity_logs (job_id, company_id, user_id, user_name, event_type, field_name, old_value, new_value) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->execute([
$job_id,
$user['company_id'],
$user['id'],
$user['name'],
$event_type,
$field_name,
$old_value,
$new_value
]);
}
function getJobStatuses($company_id) {
$stmt = db()->prepare("SELECT * FROM job_statuses WHERE company_id = ?");
$stmt->execute([$company_id]);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
function getClients($company_id) {
$stmt = db()->prepare("SELECT * FROM clients WHERE company_id = ? AND is_active = 1");
$stmt->execute([$company_id]);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
function getCompanyRequiredFolders($company_id) {
$stmt = db()->prepare("SELECT * FROM job_folders WHERE company_id = ? AND is_required = TRUE ORDER BY name ASC");
$stmt->execute([$company_id]);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
// New functions for File & Folder Manager
/**
* Ensures all required folders for a company are created for a specific job.
* This should be called when a new job is created or when new required folders are added globally.
*/
function ensureRequiredJobFoldersExist($job_id, $company_id) {
$pdo = db();
$required_folders = getCompanyRequiredFolders($company_id);
$insert_stmt = $pdo->prepare(
"INSERT IGNORE INTO job_job_folders (job_id, company_id, folder_id, name, is_custom)
VALUES (?, ?, ?, ?, ?)"
);
foreach ($required_folders as $rf) {
// Check if this required folder already exists for this job
$check_stmt = $pdo->prepare(
"SELECT COUNT(*) FROM job_job_folders WHERE job_id = ? AND company_id = ? AND folder_id = ? AND is_custom = FALSE"
);
$check_stmt->execute([$job_id, $company_id, $rf['id']]);
if ($check_stmt->fetchColumn() == 0) {
$insert_stmt->execute([$job_id, $company_id, $rf['id'], $rf['name'], FALSE]);
}
}
}
function getJobFolders($job_id, $company_id) {
$stmt = db()->prepare("SELECT jjf.*, COUNT(jf.id) as file_count
FROM job_job_folders jjf
LEFT JOIN job_files jf ON jjf.id = jf.job_job_folder_id
WHERE jjf.job_id = ? AND jjf.company_id = ?
GROUP BY jjf.id
ORDER BY jjf.is_custom ASC, jjf.name ASC");
$stmt->execute([$job_id, $company_id]);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
function getFilesInJobFolder($job_job_folder_id, $company_id) {
$stmt = db()->prepare("SELECT * FROM job_files WHERE job_job_folder_id = ? AND company_id = ? ORDER BY filename ASC");
$stmt->execute([$job_job_folder_id, $company_id]);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
function createJobFolder($job_id, $company_id, $folder_name, $is_custom = TRUE, $folder_id = null) {
$pdo = db();
// Check for duplicate folder name for this job
$check_stmt = $pdo->prepare("SELECT COUNT(*) FROM job_job_folders WHERE job_id = ? AND company_id = ? AND name = ?");
$check_stmt->execute([$job_id, $company_id, $folder_name]);
if ($check_stmt->fetchColumn() > 0) {
return ['success' => FALSE, 'message' => 'Folder with this name already exists for this job.'];
}
$stmt = $pdo->prepare("INSERT INTO job_job_folders (job_id, company_id, folder_id, name, is_custom) VALUES (?, ?, ?, ?, ?)");
$success = $stmt->execute([$job_id, $company_id, $folder_id, $folder_name, $is_custom]);
return ['success' => $success, 'id' => $pdo->lastInsertId()];
}
function deleteJobFolder($job_job_folder_id, $job_id, $company_id) {
$pdo = db();
$pdo->beginTransaction();
try {
// Check if the folder is custom and empty
$folder_info_stmt = $pdo->prepare("SELECT is_custom FROM job_job_folders WHERE id = ? AND job_id = ? AND company_id = ?");
$folder_info_stmt->execute([$job_job_folder_id, $job_id, $company_id]);
$folder = $folder_info_stmt->fetch(PDO::FETCH_ASSOC);
if (!$folder) {
$pdo->rollBack();
return ['success' => FALSE, 'message' => 'Folder not found.'];
}
if (!$folder['is_custom']) {
$pdo->rollBack();
return ['success' => FALSE, 'message' => 'Required folders cannot be deleted.'];
}
$file_count_stmt = $pdo->prepare("SELECT COUNT(*) FROM job_files WHERE job_job_folder_id = ? AND company_id = ?");
$file_count_stmt->execute([$job_job_folder_id, $company_id]);
if ($file_count_stmt->fetchColumn() > 0) {
$pdo->rollBack();
return ['success' => FALSE, 'message' => 'This folder must be empty before it can be deleted.'];
}
// Delete the folder entry from the database
$delete_stmt = $pdo->prepare("DELETE FROM job_job_folders WHERE id = ? AND job_id = ? AND company_id = ?");
$success = $delete_stmt->execute([$job_job_folder_id, $job_id, $company_id]);
if ($success) {
logActivity($job_id, 'folder_deleted', 'folder', $folder_info_stmt->fetchColumn(3), null); // Log the name of the deleted folder
$pdo->commit();
return ['success' => TRUE];
} else {
$pdo->rollBack();
return ['success' => FALSE, 'message' => 'Failed to delete folder.'];
}
} catch (PDOException $e) {
$pdo->rollBack();
return ['success' => FALSE, 'message' => 'Database error: ' . $e->getMessage()];
}
}
function addJobFile($job_id, $job_job_folder_id, $company_id, $user_id, $filename, $filepath, $mimetype, $size) {
$pdo = db();
$stmt = $pdo->prepare("INSERT INTO job_files (job_id, job_job_folder_id, company_id, user_id, filename, filepath, mimetype, size) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
$success = $stmt->execute([$job_id, $job_job_folder_id, $company_id, $user_id, $filename, $filepath, $mimetype, $size]);
if ($success) {
logActivity($job_id, 'file_uploaded', 'file', null, $filename); // Log the uploaded file
}
return ['success' => $success, 'id' => $pdo->lastInsertId()];
}
function deleteJobFile($file_id, $job_id, $company_id) {
$pdo = db();
$pdo->beginTransaction();
try {
// Get file information to delete from file system
$file_info_stmt = $pdo->prepare("SELECT filename, filepath FROM job_files WHERE id = ? AND job_id = ? AND company_id = ?");
$file_info_stmt->execute([$file_id, $job_id, $company_id]);
$file = $file_info_stmt->fetch(PDO::FETCH_ASSOC);
if (!$file) {
$pdo->rollBack();
return ['success' => FALSE, 'message' => 'File not found.'];
}
// Delete file from filesystem
if (file_exists($file['filepath'])) {
unlink($file['filepath']);
}
// Delete from database
$delete_stmt = $pdo->prepare("DELETE FROM job_files WHERE id = ? AND job_id = ? AND company_id = ?");
$success = $delete_stmt->execute([$file_id, $job_id, $company_id]);
if ($success) {
logActivity($job_id, 'file_deleted', 'file', $file['filename'], null); // Log the deleted file
$pdo->commit();
return ['success' => TRUE];
} else {
$pdo->rollBack();
return ['success' => FALSE, 'message' => 'Failed to delete file from database.'];
}
} catch (PDOException $e) {
$pdo->rollBack();
return ['success' => FALSE, 'message' => 'Database error: ' . $e->getMessage()];
}
}

320
index.php
View File

@ -1,150 +1,188 @@
<?php
declare(strict_types=1);
@ini_set('display_errors', '1');
@error_reporting(E_ALL);
@date_default_timezone_set('UTC');
require_once 'includes/helpers.php';
$phpVersion = PHP_VERSION;
$now = date('Y-m-d H:i:s');
// Get current user and company ID
$user = getCurrentAppUser();
// If no user or company ID, redirect to a login/error page (to be implemented later)
if (!$user || !($company_id = get_current_company_id())) {
// For now, if no user context, redirect to onboarding if not already there,
// assuming onboarding creates the initial admin user.
// In a real app, this would be a proper login flow.
if (basename($_SERVER['PHP_SELF']) !== 'onboarding.php') {
header('Location: onboarding.php');
exit();
}
// If we're already on onboarding.php and there's no user, let it proceed
// to allow the first company/user setup.
}
// Check if company onboarding is complete
if ($company_id && !get_company_onboarding_status($company_id)) {
// If not complete, redirect to the onboarding wizard
header('Location: onboarding.php');
exit();
}
// Handle Job Creation
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'create_job') {
$job_ref = $_POST['job_ref'];
$address_1 = $_POST['address_1'];
$description = $_POST['description'];
$status_id = $_POST['status_id'];
$client_id = $_POST['client_id'];
try {
$stmt = db()->prepare("INSERT INTO jobs (company_id, job_ref, address_1, description, status_id, client_id) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->execute([$company_id, $job_ref, $address_1, $description, $status_id, $client_id]);
$job_id = db()->lastInsertId();
logActivity($job_id, 'job_created', null, null, "Job Ref: $job_ref");
header("Location: job_detail.php?id=" . $job_id);
exit;
} catch (Exception $e) {
$error = "Error creating job: " . $e->getMessage();
}
}
$jobs = db()->prepare("SELECT j.*, s.name as status_name, c.name as client_name
FROM jobs j
LEFT JOIN job_statuses s ON j.status_id = s.id
LEFT JOIN clients c ON j.client_id = c.id
WHERE j.company_id = ?
ORDER BY j.created_at DESC");
$jobs->execute([$company_id]);
$jobList = $jobs->fetchAll();
$statuses = getJobStatuses($company_id);
$clients = getClients($company_id);
?>
<!doctype html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>New Style</title>
<?php
// Read project preview data from environment
$projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? '';
$projectImageUrl = $_SERVER['PROJECT_IMAGE_URL'] ?? '';
?>
<?php if ($projectDescription): ?>
<!-- Meta description -->
<meta name="description" content='<?= htmlspecialchars($projectDescription) ?>' />
<!-- Open Graph meta tags -->
<meta property="og:description" content="<?= htmlspecialchars($projectDescription) ?>" />
<!-- Twitter meta tags -->
<meta property="twitter:description" content="<?= htmlspecialchars($projectDescription) ?>" />
<?php endif; ?>
<?php if ($projectImageUrl): ?>
<!-- Open Graph image -->
<meta property="og:image" content="<?= htmlspecialchars($projectImageUrl) ?>" />
<!-- Twitter image -->
<meta property="twitter:image" content="<?= htmlspecialchars($projectImageUrl) ?>" />
<?php endif; ?>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap" rel="stylesheet">
<style>
:root {
--bg-color-start: #6a11cb;
--bg-color-end: #2575fc;
--text-color: #ffffff;
--card-bg-color: rgba(255, 255, 255, 0.01);
--card-border-color: rgba(255, 255, 255, 0.1);
}
body {
margin: 0;
font-family: 'Inter', sans-serif;
background: linear-gradient(45deg, var(--bg-color-start), var(--bg-color-end));
color: var(--text-color);
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
text-align: center;
overflow: hidden;
position: relative;
}
body::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100"><path d="M-10 10L110 10M10 -10L10 110" stroke-width="1" stroke="rgba(255,255,255,0.05)"/></svg>');
animation: bg-pan 20s linear infinite;
z-index: -1;
}
@keyframes bg-pan {
0% { background-position: 0% 0%; }
100% { background-position: 100% 100%; }
}
main {
padding: 2rem;
}
.card {
background: var(--card-bg-color);
border: 1px solid var(--card-border-color);
border-radius: 16px;
padding: 2rem;
backdrop-filter: blur(20px);
-webkit-backdrop-filter: blur(20px);
box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.1);
}
.loader {
margin: 1.25rem auto 1.25rem;
width: 48px;
height: 48px;
border: 3px solid rgba(255, 255, 255, 0.25);
border-top-color: #fff;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
.hint {
opacity: 0.9;
}
.sr-only {
position: absolute;
width: 1px; height: 1px;
padding: 0; margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap; border: 0;
}
h1 {
font-size: 3rem;
font-weight: 700;
margin: 0 0 1rem;
letter-spacing: -1px;
}
p {
margin: 0.5rem 0;
font-size: 1.1rem;
}
code {
background: rgba(0,0,0,0.2);
padding: 2px 6px;
border-radius: 4px;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
footer {
position: absolute;
bottom: 1rem;
font-size: 0.8rem;
opacity: 0.7;
}
</style>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dashboard - Repairs Pro</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css">
</head>
<body>
<main>
<div class="card">
<h1>Analyzing your requirements and generating your website…</h1>
<div class="loader" role="status" aria-live="polite" aria-label="Applying initial changes">
<span class="sr-only">Loading…</span>
</div>
<p class="hint"><?= ($_SERVER['HTTP_HOST'] ?? '') === 'appwizzy.com' ? 'AppWizzy' : 'Flatlogic' ?> AI is collecting your requirements and applying the first changes.</p>
<p class="hint">This page will update automatically as the plan is implemented.</p>
<p>Runtime: PHP <code><?= htmlspecialchars($phpVersion) ?></code> — UTC <code><?= htmlspecialchars($now) ?></code></p>
<nav class="navbar navbar-expand-lg py-3">
<div class="container">
<a class="navbar-brand fw-bold" href="index.php">Repairs Pro</a>
<div class="d-flex align-items-center">
<span class="me-3 text-secondary"><?php echo htmlspecialchars($user['name'] ?? ''); ?></span>
<button class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#createJobModal">New Job</button>
</div>
</div>
</nav>
<main class="container py-5">
<div class="d-flex justify-content-between align-items-center mb-4">
<h1 class="h4 fw-bold m-0">All Jobs</h1>
</div>
<?php if (isset($error)): ?>
<div class="alert alert-danger"><?php echo $error; ?></div>
<?php endif; ?>
<div class="card">
<div class="table-responsive">
<table class="table mb-0">
<thead>
<tr>
<th>Job Ref</th>
<th>Client</th>
<th>Address</th>
<th>Status</th>
<th>Approved</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($jobList as $job): ?>
<tr>
<td class="fw-medium text-primary"><?php echo htmlspecialchars($job['job_ref']); ?></td>
<td><?php echo htmlspecialchars($job['client_name']); ?></td>
<td class="text-secondary"><?php echo htmlspecialchars($job['address_1']); ?></td>
<td>
<span class="badge bg-light text-dark border">
<?php echo htmlspecialchars($job['status_name']); ?>
</span>
</td>
<td>
<?php if ($job['works_approved']): ?>
<span class="text-success">Yes</span>
<?php else: ?>
<span class="text-muted">No</span>
<?php endif; ?>
</td>
<td>
<a href="job_detail.php?id=<?php echo $job['id']; ?>" class="btn btn-outline-secondary btn-sm">View</a>
</td>
</tr>
<?php endforeach; if (empty($jobList)): ?>
<tr>
<td colspan="6" class="text-center py-5 text-secondary">No jobs found. Start by creating one.</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</main>
<!-- Create Job Modal -->
<div class="modal fade" id="createJobModal" tabindex="-1">
<div class="modal-dialog">
<form class="modal-content" method="POST">
<input type="hidden" name="action" value="create_job">
<div class="modal-header">
<h5 class="modal-title">Create New Job</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<label class="form-label">Job Reference *</label>
<input type="text" name="job_ref" class="form-control" required>
</div>
<div class="mb-3">
<label class="form-label">Client *</label>
<select name="client_id" class="form-select" required>
<?php foreach ($clients as $client): ?>
<option value="<?php echo $client['id']; ?>"><?php echo htmlspecialchars($client['name']); ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label class="form-label">Address Line 1</label>
<input type="text" name="address_1" class="form-control">
</div>
<div class="mb-3">
<label class="form-label">Status</label>
<select name="status_id" class="form-select">
<?php foreach ($statuses as $status): ?>
<option value="<?php echo $status['id']; ?>" <?php echo $status['is_default'] ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($status['name']); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label class="form-label">Description</label>
<textarea name="description" class="form-control" rows="3"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-light" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Create Job</button>
</div>
</form>
</div>
</div>
</main>
<footer>
Page updated: <?= htmlspecialchars($now) ?> (UTC)
</footer>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
</html>

392
job_detail.php Normal file
View File

@ -0,0 +1,392 @@
<?php
require_once 'includes/helpers.php';
$user = getCurrentAppUser();
$company_id = get_current_company_id();
$job_id = $_GET['id'] ?? null;
if (!$job_id || !$user || !$company_id) {
header("Location: index.php");
exit;
}
// Fetch Job
$stmt = db()->prepare("SELECT j.*, s.name as status_name, c.name as client_name
FROM jobs j
LEFT JOIN job_statuses s ON j.status_id = s.id
LEFT JOIN clients c ON j.client_id = c.id
WHERE j.id = ? AND j.company_id = ?");
$stmt->execute([$job_id, $company_id]);
$job = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$job) {
die("Job not found.");
}
// Ensure all required folders for this company are created for this job
ensureRequiredJobFoldersExist($job_id, $company_id);
// Handle POST requests for File & Folder Management
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$action = $_POST['action'] ?? '';
if ($action === 'toggle_approved') {
$new_val = $job['works_approved'] ? 0 : 1;
$stmt = db()->prepare("UPDATE jobs SET works_approved = ? WHERE id = ?");
$stmt->execute([$new_val, $job_id]);
logActivity($job_id, 'works_approved_toggle', 'works_approved', $job['works_approved'] ? 'True' : 'False', $new_val ? 'True' : 'False');
header("Location: job_detail.php?id=" . $job_id);
exit;
} elseif ($action === 'add_folder') {
$folder_name = trim($_POST['folder_name'] ?? '');
if (!empty($folder_name)) {
$result = createJobFolder($job_id, $company_id, $folder_name, TRUE);
if ($result['success']) {
// Log activity (already handled inside createJobFolder via ensureRequiredJobFoldersExist)
header("Location: job_detail.php?id=" . $job_id . "&message=Folder '" . urlencode($folder_name) . "' created successfully.&message_type=success");
exit;
} else {
$error = $result['message'] ?? 'Failed to create folder.';
}
} else {
$error = 'Folder name cannot be empty.';
}
} elseif ($action === 'delete_folder') {
$job_job_folder_id = (int)$_POST['job_job_folder_id'];
$result = deleteJobFolder($job_job_folder_id, $job_id, $company_id);
if ($result['success']) {
header("Location: job_detail.php?id=" . $job_id . "&message=Folder deleted successfully.&message_type=success");
exit;
} else {
$error = $result['message'] ?? 'Failed to delete folder.';
}
} elseif ($action === 'upload_file') {
$job_job_folder_id = (int)$_POST['job_job_folder_id'];
if (isset($_FILES['file_upload']) && $_FILES['file_upload']['error'] === UPLOAD_ERR_OK) {
$file_tmp_path = $_FILES['file_upload']['tmp_name'];
$file_name = basename($_FILES['file_upload']['name']);
$file_size = $_FILES['file_upload']['size'];
$file_type = $_FILES['file_upload']['type'];
// Define upload directory: uploads/company_id/job_id/job_job_folder_id/
$upload_dir = __DIR__ . "/uploads/{$company_id}/{$job_id}/{$job_job_folder_id}/";
if (!is_dir($upload_dir)) {
mkdir($upload_dir, 0775, true);
}
$dest_path = $upload_dir . $file_name;
if (move_uploaded_file($file_tmp_path, $dest_path)) {
$result = addJobFile($job_id, $job_job_folder_id, $company_id, $user['id'], $file_name, $dest_path, $file_type, $file_size);
if ($result['success']) {
header("Location: job_detail.php?id=" . $job_id . "&message=File '" . urlencode($file_name) . "' uploaded successfully.&message_type=success");
exit;
} else {
// If DB insert fails, try to remove the uploaded file
if (file_exists($dest_path)) { unlink($dest_path); }
$error = 'Failed to record file in database.';
}
} else {
$error = 'Failed to move uploaded file.';
}
} else {
$error = 'File upload error or no file selected.';
}
} elseif ($action === 'delete_file') {
$file_id = (int)$_POST['file_id'];
$result = deleteJobFile($file_id, $job_id, $company_id);
if ($result['success']) {
header("Location: job_detail.php?id=" . $job_id . "&message=File deleted successfully.&message_type=success");
exit;
} else {
$error = $result['message'] ?? 'Failed to delete file.';
}
}
}
// Fetch Logs
$logStmt = db()->prepare("SELECT * FROM activity_logs WHERE job_id = ? ORDER BY created_at DESC");
$logStmt->execute([$job_id]);
$logs = $logStmt->fetchAll(PDO::FETCH_ASSOC);
// Fetch all job folders (required and custom)
$jobFolders = getJobFolders($job_id, $company_id);
// Get messages from URL for display
$message = $_GET['message'] ?? '';
$message_type = $_GET['message_type'] ?? '';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Job Detail - <?php echo htmlspecialchars($job['job_ref']); ?></title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css">
<style>
.folder-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px 15px;
border: 1px solid #E5E7EB;
border-radius: 4px;
margin-bottom: 8px;
background-color: #F9FAFB;
}
.folder-item.required { background-color: #FFFBEB; border-color: #FCD34D; }
.folder-item .folder-name {
font-weight: 500;
color: #111827;
}
.folder-item .folder-actions button {
margin-left: 8px;
}
.message { padding: 15px; border-radius: 4px; margin-bottom: 20px; }
.message.success { background-color: #D1FAE5; color: #065F46; border: 1px solid #34D399; }
.message.error { background-color: #FEE2E2; color: #991B1B; border: 1px solid #F87171; }
</style>
</head>
<body>
<nav class="navbar navbar-expand-lg py-3">
<div class="container">
<a class="navbar-brand fw-bold" href="index.php">Repairs Pro</a>
<div class="d-flex align-items-center">
<a href="index.php" class="btn btn-light btn-sm me-2">Back to Dashboard</a>
</div>
</div>
</nav>
<main class="container py-5">
<?php if ($message): ?>
<div class="message <?php echo $message_type; ?>">
<?php echo htmlspecialchars($message); ?>
</div>
<?php endif; ?>
<?php if (isset($error)): ?>
<div class="alert alert-danger"><?php echo $error; ?></div>
<?php endif; ?>
<div class="row">
<div class="col-lg-8">
<div class="card p-4 mb-4">
<div class="d-flex justify-content-between align-items-start mb-4">
<div>
<span class="text-secondary text-uppercase small fw-bold">Job Reference</span>
<h1 class="h3 fw-bold"><?php echo htmlspecialchars($job['job_ref']); ?></h1>
</div>
<div class="text-end">
<span class="badge bg-light text-dark border p-2 px-3"><?php echo htmlspecialchars($job['status_name']); ?></span>
</div>
</div>
<div class="row g-4">
<div class="col-md-6">
<label class="text-secondary small">Client</label>
<div class="fw-medium"><?php echo htmlspecialchars($job['client_name']); ?></div>
</div>
<div class="col-md-6">
<label class="text-secondary small">Created At</label>
<div class="fw-medium"><?php echo date('d M Y, H:i', strtotime($job['created_at'])); ?></div>
</div>
<div class="col-12">
<label class="text-secondary small">Address</label>
<div class="fw-medium"><?php echo htmlspecialchars($job['address_1'] ?: 'N/A'); ?></div>
</div>
<div class="col-12">
<label class="text-secondary small">Description</label>
<div class="p-3 bg-light border-start border-primary border-4 rounded-1">
<?php echo nl2br(htmlspecialchars($job['description'] ?: 'No description provided.')); ?>
</div>
</div>
</div>
<hr class="my-4">
<div class="d-flex justify-content-between align-items-center mb-4">
<div>
<h5 class="m-0 fw-bold">Works Approved</h5>
<p class="text-secondary small m-0">Toggle this when the scope of work is confirmed.</p>
</div>
<form method="POST">
<input type="hidden" name="action" value="toggle_approved">
<button type="submit" class="btn <?php echo $job['works_approved'] ? 'btn-success' : 'btn-outline-secondary'; ?>">
<?php echo $job['works_approved'] ? 'Approved' : 'Mark as Approved'; ?>
</button>
</form>
</div>
<hr class="my-4">
<!-- Job Folders Section -->
<div class="d-flex justify-content-between align-items-center mb-3">
<h5 class="m-0 fw-bold">Job Folders</h5>
<button type="button" class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#addFolderModal">Add Custom Folder</button>
</div>
<div class="folder-list">
<?php if (!empty($jobFolders)): ?>
<?php foreach ($jobFolders as $folder): ?>
<div class="folder-item <?php echo !$folder['is_custom'] ? 'required' : ''; ?>">
<span class="folder-name">
<?php echo htmlspecialchars($folder['name']); ?>
(<?php echo $folder['file_count']; ?> files)
<?php if (!$folder['is_custom']): ?>
<span class="badge bg-warning text-dark ms-2">Required</span>
<?php endif; ?>
</span>
<div class="folder-actions">
<button type="button" class="btn btn-sm btn-outline-secondary" data-bs-toggle="modal" data-bs-target="#uploadFileModal" data-job-job-folder-id="<?php echo $folder['id']; ?>" data-folder-name="<?php echo htmlspecialchars($folder['name']); ?>">Upload File</button>
<?php if ($folder['is_custom']): ?>
<form method="POST" class="d-inline-block" onsubmit="return confirm('Are you sure you want to delete this folder? This action cannot be undone.');">
<input type="hidden" name="action" value="delete_folder">
<input type="hidden" name="job_job_folder_id" value="<?php echo $folder['id']; ?>">
<button type="submit" class="btn btn-sm btn-outline-danger" <?php echo ($folder['file_count'] > 0) ? 'disabled title="Folder must be empty to delete"' : ''; ?>>Delete</button>
</form>
<?php endif; ?>
</div>
</div>
<!-- Display Files within this folder -->
<div class="files-list ms-4 mb-3">
<?php $files = getFilesInJobFolder($folder['id'], $company_id); ?>
<?php if (!empty($files)): ?>
<?php foreach ($files as $file): ?>
<div class="d-flex justify-content-between align-items-center mb-1 ps-2 py-1 border-start border-secondary">
<span><a href="<?php echo htmlspecialchars($file['filepath']); ?>" target="_blank"><?php echo htmlspecialchars($file['filename']); ?></a> (<?php echo round($file['size'] / 1024, 2); ?> KB)</span>
<form method="POST" class="d-inline-block" onsubmit="return confirm('Are you sure you want to delete this file? This action cannot be undone.');">
<input type="hidden" name="action" value="delete_file">
<input type="hidden" name="file_id" value="<?php echo $file['id']; ?>">
<button type="submit" class="btn btn-sm btn-outline-danger">Delete File</button>
</form>
</div>
<?php endforeach; ?>
<?php else: ?>
<div class="text-muted small ms-2">No files in this folder.</div>
<?php endif; ?>
</div>
<?php endforeach; ?>
<?php else: ?>
<div class="alert alert-info">No folders set up for this job yet.</div>
<?php endif; ?>
</div>
</div>
</div>
<div class="col-lg-4">
<div class="card">
<div class="card-header bg-white py-3">
<h5 class="m-0 fw-bold">Activity Log</h5>
</div>
<div class="activity-log">
<?php foreach ($logs as $log): ?>
<div class="log-entry">
<div class="d-flex justify-content-between mb-1">
<span class="fw-bold"><?php echo htmlspecialchars($log['user_name']); ?></span>
<span class="text-secondary x-small"><?php echo date('H:i', strtotime($log['created_at'])); ?></span>
</div>
<div class="text-primary small mb-1">
<?php
switch($log['event_type']) {
case 'job_created': echo "Created the job"; break;
case 'works_approved_toggle': echo "Updated 'Works Approved' status"; break;
case 'folder_deleted': echo "Deleted folder: ". htmlspecialchars($log['old_value']); break;
case 'file_uploaded': echo "Uploaded file: ". htmlspecialchars($log['new_value']); break;
case 'file_deleted': echo "Deleted file: ". htmlspecialchars($log['old_value']); break;
default: echo htmlspecialchars($log['event_type']);
}
?>
</div>
<?php if ($log['field_name']): ?>
<div class="text-secondary" style="font-size: 0.75rem;">
<?php if ($log['old_value'] !== null): ?>
<span class="text-decoration-line-through"><?php echo htmlspecialchars($log['old_value']); ?></span>
&rarr;
<?php endif; ?>
<span class="fw-medium"><?php echo htmlspecialchars($log['new_value']); ?></span>
</div>
<?php endif; ?>
<div class="text-muted mt-1" style="font-size: 0.7rem;">
<?php echo date('d M Y', strtotime($log['created_at'])); ?>
</div>
</div>
<?php endforeach; if (empty($logs)): ?>
<div class="p-4 text-center text-secondary">No activity yet.</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
</main>
<!-- Modals -->
<!-- Add Folder Modal -->
<div class="modal fade" id="addFolderModal" tabindex="-1" aria-labelledby="addFolderModalLabel" aria-hidden="true">
<div class="modal-dialog">
<form class="modal-content" method="POST">
<input type="hidden" name="action" value="add_folder">
<div class="modal-header">
<h5 class="modal-title" id="addFolderModalLabel">Add Custom Folder</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<label for="folderName" class="form-label">Folder Name</label>
<input type="text" class="form-control" id="folderName" name="folder_name" required>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-light" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Create Folder</button>
</div>
</form>
</div>
</div>
<!-- Upload File Modal -->
<div class="modal fade" id="uploadFileModal" tabindex="-1" aria-labelledby="uploadFileModalLabel" aria-hidden="true">
<div class="modal-dialog">
<form class="modal-content" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="upload_file">
<input type="hidden" name="job_job_folder_id" id="uploadFolderId">
<div class="modal-header">
<h5 class="modal-title" id="uploadFileModalLabel">Upload File to <span id="uploadFolderName"></span></h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<label for="fileUpload" class="form-label">Select File</label>
<input type="file" class="form-control" id="fileUpload" name="file_upload" required>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-light" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Upload</button>
</div>
</form>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
// Populate folder ID and name in upload file modal
var uploadFileModal = document.getElementById('uploadFileModal');
uploadFileModal.addEventListener('show.bs.modal', function (event) {
var button = event.relatedTarget;
var jobJobFolderId = button.getAttribute('data-job-job-folder-id');
var folderName = button.getAttribute('data-folder-name');
var modalFolderIdInput = uploadFileModal.querySelector('#uploadFolderId');
var modalFolderNameSpan = uploadFileModal.querySelector('#uploadFolderName');
modalFolderIdInput.value = jobJobFolderId;
modalFolderNameSpan.textContent = folderName;
});
</script>
</body>
</html>

262
onboarding.php Normal file
View File

@ -0,0 +1,262 @@
<?php
require_once __DIR__ . '/includes/helpers.php';
// Placeholder for company ID - in a real app, this would come from the session after login
$company_id = get_current_company_id();
$current_user = getCurrentAppUser();
if (!$company_id) {
// Redirect to login or error page if no company is identified
header('Location: /login.php'); // Assuming a login.php exists
exit();
}
$step = isset($_GET['step']) ? (int)$_GET['step'] : 1;
$message = '';
$message_type = ''; // 'success' or 'error'
// Check if onboarding is already complete
$onboarding_complete = get_company_onboarding_status($company_id);
if ($onboarding_complete) {
header('Location: index.php');
exit();
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if ($step === 1) {
// Handle Job Status Setup submission
$statuses = $_POST['statuses'] ?? [];
$default_status_index = $_POST['default_status'] ?? null;
if (empty($statuses)) {
$message = 'Please add at least one job status.';
$message_type = 'error';
} elseif (!isset($default_status_index) || !array_key_exists($default_status_index, $statuses)) {
$message = 'Please select a default job status.';
$message_type = 'error';
} else {
try {
$pdo = db();
$pdo->beginTransaction();
// Clear existing job statuses for this company to avoid duplicates on re-submission
$stmt = $pdo->prepare("DELETE FROM job_statuses WHERE company_id = ?");
$stmt->execute([$company_id]);
$insert_stmt = $pdo->prepare("INSERT INTO job_statuses (company_id, name, is_default) VALUES (?, ?, ?)");
foreach ($statuses as $index => $status_name) {
$is_default = (int)($index == $default_status_index); // Explicitly cast to int (0 or 1)
$insert_stmt->execute([$company_id, $status_name, $is_default]);
}
$pdo->commit();
header('Location: onboarding.php?step=2&message=Job statuses saved successfully!&message_type=success');
exit();
} catch (PDOException $e) {
$pdo->rollBack();
$message = 'Error saving job statuses: ' . $e->getMessage();
$message_type = 'error';
}
}
} elseif ($step === 2) {
// Handle Required Job Folder Setup submission
$folders = $_POST['folders'] ?? [];
if (empty($folders)) {
$message = 'Please add at least one required folder.';
$message_type = 'error';
} else {
try {
$pdo = db();
$pdo->beginTransaction();
// Clear existing job folders for this company
$stmt = $pdo->prepare("DELETE FROM job_folders WHERE company_id = ? AND is_required = TRUE");
$stmt->execute([$company_id]);
$insert_stmt = $pdo->prepare("INSERT INTO job_folders (company_id, name, is_required) VALUES (?, ?, TRUE)");
foreach ($folders as $folder_name) {
$insert_stmt->execute([$company_id, $folder_name]);
}
// Mark onboarding as complete
$stmt = $pdo->prepare("UPDATE companies SET onboarding_complete = TRUE WHERE id = ?");
$stmt->execute([$company_id]);
$pdo->commit();
header('Location: index.php?message=Onboarding complete!&message_type=success');
exit();
} catch (PDOException $e) {
$pdo->rollBack();
$message = 'Error saving job folders: ' . $e->getMessage();
$message_type = 'error';
}
}
}
}
// Get messages from URL for display
if (isset($_GET['message'])) {
$message = htmlspecialchars($_GET['message']);
$message_type = htmlspecialchars($_GET['message_type'] ?? '');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Onboarding Wizard - Repairs Pro</title>
<link rel="stylesheet" href="assets/css/custom.css">
<style>
body { font-family: 'Inter', system-ui, -apple-system, sans-serif; background-color: #F9FAFB; color: #111827; }
.container { max-width: 800px; margin: 50px auto; padding: 30px; background-color: #FFFFFF; border-radius: 4px; box-shadow: 0 1px 3px rgba(0,0,0,0.1); }
h1 { color: #111827; border-bottom: 1px solid #E5E7EB; padding-bottom: 15px; margin-bottom: 30px; }
h2 { color: #3B82F6; margin-bottom: 20px; }
.form-group { margin-bottom: 20px; }
label { display: block; margin-bottom: 8px; font-weight: 500; }
input[type="text"] { width: calc(100% - 22px); padding: 10px; border: 1px solid #D1D5DB; border-radius: 4px; }
button { padding: 10px 20px; background-color: #3B82F6; color: #FFFFFF; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; }
button:hover { background-color: #2563EB; }
.status-item, .folder-item { display: flex; align-items: center; margin-bottom: 10px; }
.status-item input[type="radio"] { margin-right: 10px; }
.status-item input[type="text"], .folder-item input[type="text"] { flex-grow: 1; margin-right: 10px; }
.add-button { background-color: #10B981; margin-top: 10px; }
.add-button:hover { background-color: #059669; }
.remove-button { background-color: #EF4444; margin-left: 10px; }
.remove-button:hover { background-color: #DC2626; }
.message { padding: 15px; border-radius: 4px; margin-bottom: 20px; }
.message.success { background-color: #D1FAE5; color: #065F46; border: 1px solid #34D399; }
.message.error { background-color: #FEE2E2; color: #991B1B; border: 1px solid #F87171; }
.pagination { margin-top: 30px; text-align: center; }
.pagination button { margin: 0 10px; }
</style>
</head>
<body>
<div class="container">
<h1>Company Onboarding Wizard</h1>
<?php if ($message): ?>
<div class="message <?php echo $message_type; ?>">
<?php echo $message; ?>
</div>
<?php endif; ?>
<?php if ($step === 1): // Job Status Setup ?>
<h2>Step 1: Job Status Setup</h2>
<p>Define the different statuses a job can have within your company. One status must be set as default.</p>
<form method="POST" action="onboarding.php?step=1">
<div id="status-list">
<div class="form-group status-item">
<input type="radio" name="default_status" value="0" id="default_status_0" required>
<input type="text" name="statuses[]" value="To Be Surveyed" placeholder="e.g., To Be Surveyed" required>
<label for="default_status_0">Default</label>
</div>
<div class="form-group status-item">
<input type="radio" name="default_status" value="1" id="default_status_1">
<input type="text" name="statuses[]" value="Booking Required" placeholder="e.g., Booking Required" required>
<label for="default_status_1">Default</label>
<button type="button" class="remove-button" onclick="removeStatus(this)">Remove</button>
</div>
<div class="form-group status-item">
<input type="radio" name="default_status" value="2" id="default_status_2">
<input type="text" name="statuses[]" value="Completed" placeholder="e.g., Completed" required>
<label for="default_status_2">Default</label>
<button type="button" class="remove-button" onclick="removeStatus(this)">Remove</button>
</div>
</div>
<button type="button" class="add-button" onclick="addStatus()">Add Another Status</button>
<div class="pagination">
<button type="submit">Next Step</button>
</div>
</form>
<script>
let statusCount = 3;
function addStatus() {
const statusList = document.getElementById('status-list');
const newStatusItem = document.createElement('div');
newStatusItem.classList.add('form-group', 'status-item');
newStatusItem.innerHTML = `
<input type="radio" name="default_status" value="${statusCount}" id="default_status_${statusCount}">
<input type="text" name="statuses[]" placeholder="e.g., In Progress" required>
<label for="default_status_${statusCount}">Default</label>
<button type="button" class="remove-button" onclick="removeStatus(this)">Remove</button>
`;
statusList.appendChild(newStatusItem);
statusCount++;
updateRadioValues();
}
function removeStatus(button) {
button.closest('.status-item').remove();
updateRadioValues();
}
function updateRadioValues() {
const statusItems = document.querySelectorAll('.status-item');
statusItems.forEach((item, index) => {
item.querySelector('input[type="radio"]').value = index;
item.querySelector('input[type="radio"]').id = `default_status_${index}`;
item.querySelector('label').htmlFor = `default_status_${index}`;
});
}
</script>
<?php elseif ($step === 2): // Required Job Folder Setup ?>
<h2>Step 2: Required Job Folder Setup</h2>
<p>Define folders that will automatically appear on every job. These cannot be deleted by users.</p>
<form method="POST" action="onboarding.php?step=2">
<div id="folder-list">
<div class="form-group folder-item">
<input type="text" name="folders[]" value="PO" placeholder="e.g., PO" required>
<button type="button" class="remove-button" onclick="removeFolder(this)">Remove</button>
</div>
<div class="form-group folder-item">
<input type="text" name="folders[]" value="Quote" placeholder="e.g., Quote" required>
<button type="button" class="remove-button" onclick="removeFolder(this)">Remove</button>
</div>
<div class="form-group folder-item">
<input type="text" name="folders[]" value="Photos" placeholder="e.g., Photos" required>
<button type="button" class="remove-button" onclick="removeFolder(this)">Remove</button>
</div>
<div class="form-group folder-item">
<input type="text" name="folders[]" value="RAMS" placeholder="e.g., RAMS" required>
<button type="button" class="remove-button" onclick="removeFolder(this)">Remove</button>
</div>
<div class="form-group folder-item">
<input type="text" name="folders[]" value="Invoices" placeholder="e.g., Invoices" required>
<button type="button" class="remove-button" onclick="removeFolder(this)">Remove</button>
</div>
</div>
<button type="button" class="add-button" onclick="addFolder()">Add Another Folder</button>
<div class="pagination">
<button type="button" onclick="window.location.href='onboarding.php?step=1'" class="">Previous Step</button>
<button type="submit">Complete Onboarding</button>
</div>
</form>
<script>
let folderCount = 5;
function addFolder() {
const folderList = document.getElementById('folder-list');
const newFolderItem = document.createElement('div');
newFolderItem.classList.add('form-group', 'folder-item');
newFolderItem.innerHTML = `
<input type="text" name="folders[]" placeholder="e.g., Documents" required>
<button type="button" class="remove-button" onclick="removeFolder(this)">Remove</button>
`;
folderList.appendChild(newFolderItem);
folderCount++;
}
function removeFolder(button) {
button.closest('.folder-item').remove();
}
</script>
<?php endif; ?>
</div>
</body>
</html>