# FormHub — Multi-Tenant Form Data Management System

Unified form submission hub for multiple static websites. Receive form submissions via REST API, store them in one MySQL/SQLite database, and manage them with role-based access.

**Location:** `D:/projects/form-central`

## Features

- Multi-tenant: Clients → Projects → Forms → Submissions (single app, single DB)
- Roles: Super Admin, Client Admin, Project User
- REST API: `POST /api/form-submit` with API key auth
- Dynamic form fields stored as JSON
- Dashboard with Chart.js, unread badges, activity feed
- CRUD for Clients, Projects, Users, Form Types, API Keys
- Submission management: status workflow, notes, bulk actions, export CSV/PDF
- Reports with filters and export
- Activity logs, login tracking, failed login lockout
- Settings, SMTP, reCAPTCHA-ready keys, IP blacklist
- Database backup (Super Admin), system health, maintenance mode
- Dark mode toggle, responsive Bootstrap 5 admin UI
- Shared hosting / cPanel compatible

## Requirements

- PHP 8.2+
- Composer
- MySQL 5.7+ / MariaDB / SQLite (local demo uses SQLite)
- Extensions: OpenSSL, PDO, Mbstring, Tokenizer, XML, Ctype, JSON, Fileinfo
- Optional: GD, Zip (for advanced PDF/image handling)

## Installation

### 1. Configure environment

```bash
cd D:/projects/form-central
cp .env.example .env   # if needed
php artisan key:generate
```

### 2. Database (MySQL — production / shared hosting)

Edit `.env`:

```env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=formhub
DB_USERNAME=root
DB_PASSWORD=your_password
```

Create the empty database in phpMyAdmin / cPanel, then:

```bash
php artisan migrate --seed
php artisan storage:link
```

### 3. Database (SQLite — local demo)

Already configured by default:

```env
DB_CONNECTION=sqlite
DB_DATABASE=database/formhub.sqlite
```

```bash
# ensure file exists
type nul > database\formhub.sqlite   # Windows
# or: touch database/formhub.sqlite  # Linux/Mac

php artisan migrate --seed
php artisan storage:link
```

### 4. Run locally

```bash
php artisan serve --host=127.0.0.1 --port=8088
```

Open: http://127.0.0.1:8088

> Note: Ports 8000/8001 may already be used by other local projects.

### 5. Queue (email notifications)

```bash
php artisan queue:work
```

Set `QUEUE_CONNECTION=database` (default) or `sync` for simple hosting.

## Demo Logins

| Role | Email | Password |
|------|-------|----------|
| Super Admin | admin@formcentral.test | Admin@12345 |
| Client Admin | client@acmedigital.test | Client@12345 |
| Project User | user@acmedigital.test | User@12345 |

After seeding, the console prints a sample API key for **Acme Corporate Website**.

Retrieve keys anytime:

```bash
php artisan tinker --execute="echo App\Models\ApiKey::first()->key;"
```

Or from the admin UI: **API Keys** / **Projects → Show**.

## API Usage

### Endpoint

```
POST /api/form-submit
```

### Authentication

Send the project API key via header **or** body:

```
X-API-Key: fk_xxxxxxxx
```

or

```json
{ "api_key": "fk_xxxxxxxx" }
```

### JSON example

```bash
curl -X POST http://127.0.0.1:8000/api/form-submit \
  -H "Content-Type: application/json" \
  -H "X-API-Key: YOUR_API_KEY" \
  -d "{\"form_name\":\"Contact\",\"name\":\"John Doe\",\"email\":\"john@example.com\",\"phone\":\"9999999999\",\"message\":\"Hello from website\"}"
```

### HTML form (urlencoded)

```html
<form action="https://your-domain.com/api/form-submit" method="POST">
  <input type="hidden" name="api_key" value="YOUR_API_KEY">
  <input type="hidden" name="form_name" value="Contact">
  <!-- Honeypot (leave empty) -->
  <input type="text" name="honeypot" style="display:none" tabindex="-1" autocomplete="off">
  <input type="text" name="name" required>
  <input type="email" name="email" required>
  <input type="text" name="phone">
  <textarea name="message"></textarea>
  <button type="submit">Send</button>
</form>
```

### Success response (201)

```json
{
  "success": true,
  "message": "Form submitted successfully.",
  "data": {
    "id": 21,
    "form_name": "Contact",
    "status": "new",
    "is_duplicate": false,
    "submitted_at": "2026-07-30T16:00:00+00:00"
  }
}
```

Any extra fields (`budget`, `city`, `company`, etc.) are stored in JSON `form_data` — no schema changes needed.

See in-app **API Docs** page and `docs/Form_Central_API.postman_collection.json`.

## cPanel / Shared Hosting

1. Upload project files (or deploy via Git).
2. Point the domain/subdomain document root to the `public` folder.
3. Set PHP 8.2 in MultiPHP Manager.
4. Create MySQL DB + user; update `.env`.
5. Run via SSH (or a one-time artisan runner):

```bash
composer install --no-dev -o
php artisan key:generate
php artisan migrate --seed
php artisan storage:link
php artisan config:cache
php artisan route:cache
php artisan view:cache
```

6. Ensure `storage/` and `bootstrap/cache/` are writable.

## Roles & Permissions

| Capability | Super Admin | Client Admin | Project User |
|------------|:-----------:|:------------:|:------------:|
| All clients | ✓ | own only | — |
| Projects | all | own client | assigned |
| Users | all | own client | — |
| Submissions | all | own client | assigned projects |
| Settings / Backup | ✓ | — | — |
| Reports / Export | ✓ | ✓ | ✓ |

## Project Structure

```
app/
  Http/Controllers/Admin|Api|Auth
  Http/Middleware
  Models
  Services
  Traits
  Helpers
  Mail
database/migrations|seeders
resources/views/admin|auth|layouts|emails|api-docs
routes/web.php|api.php
public/css|js
docs/
```

## Security

- API key validation + project/client status checks
- Rate limiting on API (`throttle:60,1`) and form submit service
- Honeypot field support (`honeypot` / `website_url_hp`)
- IP blacklist
- reCAPTCHA keys configurable (ready for front-end integration)
- Session timeout middleware
- Failed login tracking & temporary lockout (5 attempts → 30 min)
- Soft deletes on core entities
- CSRF on web forms; middleware-protected admin routes

## Version

**FormHub v1.0.0** — Laravel 12 / PHP 8.2

## License

Proprietary / project deliverable — adapt as needed for your organization.
