Add website-type TFE support: URLs stored as thesis_files rows, HTMX-toggle on Site web format

This commit is contained in:
Pontoporeia
2026-05-07 21:24:46 +02:00
parent 9dc7ea98f2
commit ac0008df6c
10 changed files with 399 additions and 39 deletions

View File

@@ -331,9 +331,12 @@ class ThesisEditController
}
$filePath = $this->db->deleteThesisFile($fileId, $thesisId);
if ($filePath && defined('STORAGE_ROOT')) {
$abs = STORAGE_ROOT . '/' . $filePath;
if (file_exists($abs)) {
@unlink($abs);
// Skip filesystem deletion for website URLs (not real files)
if (!str_starts_with($filePath, 'http://') && !str_starts_with($filePath, 'https://')) {
$abs = STORAGE_ROOT . '/' . $filePath;
if (file_exists($abs)) {
@unlink($abs);
}
}
}
}
@@ -358,6 +361,9 @@ class ThesisEditController
if (!empty($files['files']['name'][0])) {
$this->handleThesisFiles($thesisId, $post, $files['files']);
}
// ── Website URL — add or update ──────────────────────────────────────
$this->handleWebsiteUrl($thesisId, $post);
}
// ── Private: file uploads ─────────────────────────────────────────────────
@@ -703,4 +709,50 @@ class ThesisEditController
}
return $info;
}
/**
* Add or update a website URL thesis_file row.
*
* If a website row already exists for this thesis, it is replaced.
* Otherwise a new row is inserted.
*/
private function handleWebsiteUrl(int $thesisId, array $post): void
{
$websiteUrl = trim($post['website_url'] ?? '');
// Remove existing website rows
$existingFiles = $this->db->getThesisFiles($thesisId);
foreach ($existingFiles as $f) {
if ($f['file_type'] === 'website') {
$this->db->deleteThesisFile((int)$f['id'], $thesisId);
}
}
if ($websiteUrl === '') {
return;
}
// Validate URL
$websiteUrl = filter_var($websiteUrl, FILTER_VALIDATE_URL);
if ($websiteUrl === false) {
error_log('ThesisEditController: invalid website URL, skipping');
return;
}
$label = trim($post['website_label'] ?? '');
$sortOrder = isset($post['website_order']) ? (int)$post['website_order'] : null;
$fileName = rtrim(preg_replace('#^https?://#i', '', $websiteUrl), '/');
$this->db->insertThesisFile(
$thesisId,
'website',
$websiteUrl,
$fileName,
0,
'text/html',
$label !== '' ? $label : null,
$sortOrder
);
error_log("ThesisEditController: website stored → $websiteUrl");
}
}