Skip to content

Commit 368cc6b

Browse files
author
HugoFara
committed
refactor(routes): use RESTful URL for text print-plain
Change /text/print-plain?text={id} to /text/{id}/print-plain - Add new RESTful route with legacy fallback - Update TextPrintController::printPlain to accept route param - Update views and TypeScript to use new URL pattern
1 parent d300a00 commit 368cc6b

File tree

8 files changed

+24
-15
lines changed

8 files changed

+24
-15
lines changed

src/Modules/Review/Views/header.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
<a href="/text/read?start=<?php echo $textId; ?>" target="_top">
5151
<?php echo \Lwt\Shared\UI\Helpers\IconHelper::render('book-open', ['title' => 'Read', 'alt' => 'Read']); ?>
5252
</a>
53-
<a href="/text/print-plain?text=<?php echo $textId; ?>" target="_top">
53+
<a href="/text/<?php echo $textId; ?>/print-plain" target="_top">
5454
<?php echo \Lwt\Shared\UI\Helpers\IconHelper::render('printer', ['title' => 'Print', 'alt' => 'Print']); ?>
5555
</a>
5656
<?php echo (new AnnotationService())->getAnnotationLink($textId); ?>

src/Modules/Text/Http/TextPrintController.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,19 @@ public function getPrintService(): TextPrintService
8282
/**
8383
* Print plain text with annotations (replaces text_print_plain.php).
8484
*
85-
* Route: /text/print-plain?text=[textid]&ann=[annotationcode]&status=[statuscode]
85+
* Routes:
86+
* - GET /text/{text:int}/print-plain (new RESTful route)
87+
* - GET /text/print-plain?text=[textid] (legacy route)
8688
*
87-
* @param array $params Route parameters
89+
* Optional query params: ann=[annotationcode]&status=[statuscode]
90+
*
91+
* @param int|null $text Text ID (injected from route parameter)
8892
*
8993
* @return void
9094
*
9195
* @psalm-suppress UnusedVariable Variables are used in included view files
9296
*/
93-
public function printPlain(array $params): void
97+
public function printPlain(?int $text = null): void
9498
{
9599
include_once self::BACKEND_PATH . '/Core/Bootstrap/db_bootstrap.php';
96100
include_once dirname(__DIR__) . '/Application/Services/TextStatisticsService.php';
@@ -101,7 +105,8 @@ public function printPlain(array $params): void
101105
include_once dirname(__DIR__, 2) . '/Vocabulary/Application/Services/ExpressionService.php';
102106
include_once __DIR__ . '/../../../Shared/Infrastructure/Database/Restore.php';
103107

104-
$textId = (int) $this->param('text', '0');
108+
// Support both new route param injection and legacy query param
109+
$textId = $text ?? (int) $this->param('text', '0');
105110

106111
if ($textId === 0) {
107112
$this->redirect('/text/edit');
@@ -289,7 +294,7 @@ public function deleteAnnotation(array $params): void
289294

290295
$deleted = $this->printService->deleteAnnotation($textId);
291296
if ($deleted) {
292-
$this->redirect('/text/print-plain?text=' . $textId);
297+
$this->redirect('/text/' . $textId . '/print-plain');
293298
}
294299

295300
// If deletion failed, redirect back to print view

src/Modules/Text/Views/edit_list.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ class="dropdown-menu card-dropdown"
304304
@click.outside="open = false"
305305
x-cloak>
306306
<div class="dropdown-content">
307-
<a :href="'/text/print-plain?text=' + text.id" class="dropdown-item">
307+
<a :href="'/text/' + text.id + '/print-plain'" class="dropdown-item">
308308
<?php echo IconHelper::render('printer', ['size' => 14]); ?>
309309
<span>Print</span>
310310
</a>

src/Modules/Text/Views/print_alpine.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
$hasAnnotation = $viewData['hasAnnotation'];
7979

8080
// Determine URLs based on mode
81-
$printUrl = $mode === 'plain' ? '/text/print-plain?text=' : '/text/{id}/print';
81+
$printUrl = $mode === 'plain' ? '/text/{id}/print-plain' : '/text/{id}/print';
8282
?>
8383
<!-- Alpine.js container -->
8484
<div x-data="textPrintApp()" x-init="init()" x-cloak>

src/Modules/Text/Views/read_desktop.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102
<a href="/review?text=<?php echo $textId; ?>" class="button is-small">Review</a>
103103
</div>
104104
<div class="control">
105-
<a href="/text/print-plain?text=<?php echo $textId; ?>" class="button is-small">Print</a>
105+
<a href="/text/<?php echo $textId; ?>/print-plain" class="button is-small">Print</a>
106106
</div>
107107
<div class="control">
108108
<a href="/texts/<?php echo $textId; ?>/edit" class="button is-small">Edit</a>

src/Modules/Text/Views/read_header.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
echo \Lwt\Shared\UI\Helpers\IconHelper::render('circle-help', ['title' => 'Review', 'alt' => 'Review']);
9191
?>
9292
</a>
93-
<a href="/text/print-plain?text=<?php echo $textId; ?>" target="_top">
93+
<a href="/text/<?php echo $textId; ?>/print-plain" target="_top">
9494
<?php echo \Lwt\Shared\UI\Helpers\IconHelper::render('printer', ['title' => 'Print', 'alt' => 'Print']); ?>
9595
</a>
9696
<?php echo (new AnnotationService())->getAnnotationLink($textId); ?>

src/backend/Router/routes.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ function registerRoutes(Router $router): void
9999
$router->get('/text/{text:int}/print/edit', 'Lwt\\Modules\\Text\\Http\\TextPrintController@editAnnotation', AUTH_MIDDLEWARE);
100100
// RESTful route: DELETE /text/123/annotation
101101
$router->delete('/text/{text:int}/annotation', 'Lwt\\Modules\\Text\\Http\\TextPrintController@deleteAnnotation', AUTH_MIDDLEWARE);
102+
// RESTful route: /text/123/print-plain
103+
$router->get('/text/{text:int}/print-plain', 'Lwt\\Modules\\Text\\Http\\TextPrintController@printPlain', AUTH_MIDDLEWARE);
104+
// Legacy route: /text/print-plain?text=123
102105
$router->registerWithMiddleware(
103106
'/text/print-plain',
104107
'Lwt\\Modules\\Text\\Http\\TextPrintController@printPlain',

src/frontend/js/modules/text/pages/text_print_app.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,18 +155,19 @@ function getPageConfig(): PageConfig {
155155
// Fallback: try to get text ID from URL
156156
const pathname = window.location.pathname;
157157

158-
// RESTful URL pattern: /text/{id}/print or /text/{id}/print/edit
159-
const printMatch = pathname.match(/\/text\/(\d+)\/print(?:\/edit)?$/);
158+
// RESTful URL pattern: /text/{id}/print, /text/{id}/print/edit, or /text/{id}/print-plain
159+
const printMatch = pathname.match(/\/text\/(\d+)\/print(?:-plain|\/edit)?$/);
160160
if (printMatch) {
161161
const textId = parseInt(printMatch[1], 10);
162162
const isEdit = pathname.endsWith('/print/edit');
163+
const isPlain = pathname.endsWith('/print-plain');
163164
return {
164165
textId,
165-
mode: isEdit ? 'edit' : 'annotated'
166+
mode: isEdit ? 'edit' : isPlain ? 'plain' : 'annotated'
166167
};
167168
}
168169

169-
// Legacy/plain print URL pattern: /text/print-plain?text={id}
170+
// Legacy plain print URL pattern: /text/print-plain?text={id}
170171
const params = new URLSearchParams(window.location.search);
171172
const textId = parseInt(params.get('text') || '0', 10);
172173

@@ -529,7 +530,7 @@ export function textPrintAppData(): TextPrintAppData {
529530
if (response.redirected) {
530531
window.location.href = response.url;
531532
} else if (response.ok) {
532-
window.location.href = `/text/print-plain?text=${textId}`;
533+
window.location.href = `/text/${textId}/print-plain`;
533534
}
534535
})
535536
.catch((error) => {

0 commit comments

Comments
 (0)