# Current Issues — XAMXAM (2026-05-10)

## 1. FK constraint violation on thesis save (create + edit)

**Symptom:** `⚠ SQLSTATE[23000]: Integrity constraint violation: 19 FOREIGN KEY constraint failed`

**Triggers:**
- Editing an imported CSV thesis, changing access type to Interdit, save
- Opening any imported thesis edit form and saving *without changes*  
- Saving the add form with empty fields — form below "Cadre académique" disappears (PHP dies mid-render)

**Root cause found so far:** `Database::createThesis()` at line ~1860 was doing `(int)$data['orientation_id']` which converts SQL null → PHP `null` → `(int)null` = `0`. Since no row with ID 0 exists in `orientations`, this triggers FK violation. Fixed in commit `55088c94` by using `$v ? (int)$v : null` pattern.

**Still happening after fix** — suggests another code path has same issue, or the fix wasn't complete. The `updateThesis` path was already safe (uses `?: null`), but the error persists.

**Debugging added:**
- `[DB:updateThesis]` log line with all FK values before query (commit `55088c94`)
- `[ThesisEdit] Step 1-6 OK` step-level logging (commit `8734d964`)
- `ErrorHandler::log()` with full trace on catch (commit `03ad73f3`)

**Dev server output:** No error_log visible in dev mode — PHP built-in server sends errors to stderr which may not be captured.

**Next steps:** 
- Enable `display_errors=1` in dev mode so FK errors render in browser
- Check if `setThesisFormats`, `setThesisLanguages`, `setThesisTags` paths also have `(int)null` → `0` issues
- Check `formulaire.php` action file — does it also use `createThesis`?

---

## 2. Dev server debugging output

**Symptom:** No error output visible in browser when PHP crashes.

**Current config** (`bootstrap.php`):
- Dev mode (cli-server): `display_errors=1`, `error_reporting=E_ALL`
- Production: `display_errors=0`, `log_errors=1`

**But:** the admin action files override this:
- `formulaire.php` line 5-7: `ini_set('display_errors', 0); ini_set('log_errors', 1);`
- `edit.php` action: no override (uses bootstrap defaults)

**Action needed:** Don't suppress display_errors in dev mode. Check `php_sapi_name()` before overriding.

---

## 3. Console warnings

```
Layout was forced before the page was fully loaded. node.js:416:1
[file-upload-queue] XamxamInitFileUploads called (twice)
```
- `file-upload-queue.js` called twice — the script might be included twice (check `add.php` template + the `form.php` partial)

---

## 4. Tags: lowercase + dedup + CSV import

**Status:** Implemented across all paths:
- Frontend JS: `normalizeTag()` with `replace(/\s+/g, ' ')`, lowercase
- Server fragment: `preg_replace('/\s+/', ' ', strtolower(...))`  
- Both controllers: `fn(string $t) => strtolower(trim(preg_replace('/\s+/', ' ', $t)))`
- CSV import: same normalization (commit `8734d964`)
- Minimum 3 tags enforced (commit `8734d964`)

## 5. ErrorHandler coverage

**Status:** Applied to 12 admin action files + 6 public controllers + 2 form controllers + partage entry point (commit `03ad73f3`). 77 unit test assertions.

## Relevant commits (most recent first)

```
55088c94 Fix FK violation: (int)null → 0 in createThesis
27378b42 ErrorHandler tests: 77 assertions
4d9296fd ErrorHandler: precise FK field extraction from SQLite
03ad73f3 ErrorHandler: shared logging across all actions/controllers  
6b6c62d1 Error logging: step-by-step transaction tracing
8734d964 Mots-clés: collapse spaces, minimum 3 keywords
dfe1186b Mots-clés: lowercase, dedup, keyboard nav, absolute dropdown
8d04d4ba Mots-clés: lowercase enforcement, deduplication, absolute dropdown
7fe53f8c Mots-clés: interactive HTMX tag search
dd110cc5 Admin mobile block: fix inline style beating media query
```

## Key files to review

- `app/src/Database.php` — `createThesis()` line ~1830, `updateThesis()` line ~1751, `setThesisFormats/Languages/Tags`
- `app/src/Controllers/ThesisCreateController.php` — `submit()` line ~146, `validateAndSanitise()` line ~312
- `app/src/Controllers/ThesisEditController.php` — `save()` line ~158
- `app/public/admin/actions/formulaire.php` — calls `$ctrl->submit()` (create path)
- `app/public/admin/actions/edit.php` — calls `$ctrl->save()` (edit path)
- `app/public/admin/index.php` — CSV import at line ~220
