|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jidaikobo\Kontiki\Controllers\FileControllerTraits; |
|
4
|
|
|
|
|
5
|
|
|
use Jidaikobo\Kontiki\Utils\Pagination; |
|
6
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
|
7
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
|
8
|
|
|
|
|
9
|
|
|
trait ListTrait |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Handles the AJAX request to fetch the file list. |
|
13
|
|
|
* |
|
14
|
|
|
* This method retrieves a list of files from the model, applies security headers |
|
15
|
|
|
* to the response, and then renders a view to display the file list. |
|
16
|
|
|
* |
|
17
|
|
|
* @return Response |
|
18
|
|
|
*/ |
|
19
|
|
|
public function filelist(Request $request, Response $response): Response |
|
20
|
|
|
{ |
|
21
|
|
|
// Initialize Pagination and set total items |
|
22
|
|
|
$page = $request->getQueryParams()['page'] ?? 1; |
|
23
|
|
|
$itemsPerPage = 10; |
|
24
|
|
|
$pagination = new Pagination($page, $itemsPerPage); |
|
25
|
|
|
|
|
26
|
|
|
$keyword = $request->getQueryParams()['s'] ?? ''; |
|
27
|
|
|
$query = $this->model->getQuery(); |
|
28
|
|
|
$query = $this->model->buildSearchConditions($query, $keyword); |
|
29
|
|
|
$totalItems = $query->count(); |
|
30
|
|
|
|
|
31
|
|
|
$pagination->setTotalItems($totalItems); |
|
32
|
|
|
$paginationHtml = $pagination->render(env('BASEPATH', '') . "/filelist", true); |
|
33
|
|
|
|
|
34
|
|
|
$items = $query->limit($pagination->getLimit()) |
|
35
|
|
|
->offset($pagination->getOffset()) |
|
36
|
|
|
->orderBy('created_at', 'desc') |
|
37
|
|
|
->get() |
|
38
|
|
|
->map(fn($item) => (array) $item) |
|
39
|
|
|
->toArray(); |
|
40
|
|
|
|
|
41
|
|
|
$items = $this->processItemsForList($request, $items); |
|
42
|
|
|
|
|
43
|
|
|
$content = $this->view->fetch( |
|
44
|
|
|
'forms/incFilelist.php', |
|
45
|
|
|
[ |
|
46
|
|
|
'items' => $items, |
|
47
|
|
|
'pagination' => $paginationHtml |
|
48
|
|
|
] |
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
|
|
$response->getBody()->write($content); |
|
52
|
|
|
return $response->withHeader('Content-Type', 'text/html')->withStatus(200); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
private function processItemsForList(Request $request, array $items): array |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
|
|
foreach ($items as $key => $value) { |
|
58
|
|
|
$url = $this->pathToUrl($items[$key]['path']); |
|
|
|
|
|
|
59
|
|
|
$items[$key]['imageOrLink'] = $this->renderImageOrLink($url, $items[$key]['description'] ?? ''); |
|
60
|
|
|
$items[$key]['url'] = $url; |
|
61
|
|
|
$items[$key]['description'] = $items[$key]['description'] ?? ''; // don't use null |
|
62
|
|
|
$items[$key]['isImage'] = $this->isImageUrl($url); |
|
63
|
|
|
} |
|
64
|
|
|
return $items; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Determine whether a given URL points to an image file abased on its extension. |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $url The URL to check. |
|
71
|
|
|
* @return bool True if it's an image, false otherwise. |
|
72
|
|
|
*/ |
|
73
|
|
|
private function isImageUrl(string $url): bool |
|
74
|
|
|
{ |
|
75
|
|
|
$imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp']; |
|
76
|
|
|
$pathInfo = pathinfo(parse_url($url, PHP_URL_PATH)); |
|
77
|
|
|
|
|
78
|
|
|
return isset($pathInfo['extension']) && |
|
79
|
|
|
in_array(strtolower($pathInfo['extension']), $imageExtensions, true); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Render an image or a link based on the provided URL. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $url The input URL, either an image URL or a standard URL. |
|
86
|
|
|
* @param string|null $desc description text. |
|
87
|
|
|
* @return string The generated HTML. |
|
88
|
|
|
*/ |
|
89
|
|
|
private function renderImageOrLink(string $url, ?string $desc): string |
|
90
|
|
|
{ |
|
91
|
|
|
if ($this->isImageUrl($url)) { |
|
92
|
|
|
$descText = e($desc); |
|
93
|
|
|
$imgSrc = e($url); |
|
94
|
|
|
$imgTag = '<a href="' . $imgSrc . '"' |
|
95
|
|
|
. ' class="img-thumbnail"' |
|
96
|
|
|
. ' data-action="preview"' |
|
97
|
|
|
. ' data-url="' . $imgSrc . '"' |
|
98
|
|
|
. ' data-alt="' . $descText . '">' |
|
99
|
|
|
. '<img src="' . $imgSrc . '"' |
|
100
|
|
|
. ' alt="' . __('enlarge_x', 'Enlarge :name', ['name' => $descText]) . '"' |
|
101
|
|
|
. ' class="img-thumbnail">' |
|
102
|
|
|
. '</a>'; |
|
103
|
|
|
return $imgTag; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
// Otherwise, return an <a> tag for links |
|
107
|
|
|
$linkHref = e($url); |
|
108
|
|
|
$pathInfo = pathinfo(parse_url($url, PHP_URL_PATH)); |
|
109
|
|
|
$extension = isset($pathInfo['extension']) ? strtolower($pathInfo['extension']) : null; |
|
110
|
|
|
|
|
111
|
|
|
switch ($extension) { |
|
112
|
|
|
case 'pdf': |
|
113
|
|
|
$class = 'fa-file-pdf'; |
|
114
|
|
|
break; |
|
115
|
|
|
case 'zip': |
|
116
|
|
|
$class = 'fa-file-zip'; |
|
117
|
|
|
break; |
|
118
|
|
|
default: |
|
119
|
|
|
$class = 'fa-file-text'; |
|
120
|
|
|
break; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return '<a href="' . $linkHref . '" target="_blank" aria-label="' . __('downlaod') . '" download class="fa-solid ' . $class . ' display-3 mb-2"><span class="visually-hidden">' . __('downlaod_x', 'Download :name', ['name' => $desc]) . '</span></a>'; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|