|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jidaikobo\Kontiki\Utils; |
|
4
|
|
|
|
|
5
|
|
|
use Jidaikobo\Kontiki\Models\ModelInterface; |
|
6
|
|
|
use Jidaikobo\MarkdownExtra; |
|
7
|
|
|
|
|
8
|
|
|
class MessageUtils |
|
9
|
|
|
{ |
|
10
|
|
|
public static function errorHtml(array $errors, ModelInterface $model): string |
|
11
|
|
|
{ |
|
12
|
|
|
// フィールド定義を取得 |
|
13
|
|
|
$fieldDefinitions = $model->getFields(); |
|
14
|
|
|
|
|
15
|
|
|
$html = '<div class="errormessages" role="status">'; |
|
16
|
|
|
$html .= '<ul class="alert alert-danger p-3 ps-5 pt-0 mt-3 mb-3 fs-6">'; |
|
17
|
|
|
|
|
18
|
|
|
foreach ($errors as $field => $errorDetails) { |
|
19
|
|
|
$messages = $errorDetails['messages'] ?? []; |
|
20
|
|
|
$htmlName = $errorDetails['htmlName'] ?? $field; |
|
21
|
|
|
|
|
22
|
|
|
// フォーム全体のエラー |
|
23
|
|
|
if ($field === 0) { |
|
24
|
|
|
$html .= '<li class="pt-3">' . __('found_the_problem', 'Found the problem'); |
|
25
|
|
|
$html .= '<ul class="ps-3">'; |
|
26
|
|
|
foreach ($messages as $message) { |
|
27
|
|
|
$html .= sprintf('<li class="pt-2">%s</li>', e($message)); |
|
28
|
|
|
} |
|
29
|
|
|
$html .= '</ul></li>'; |
|
30
|
|
|
continue; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
// フィールド固有のエラー |
|
34
|
|
|
$id = FormUtils::nameToId($htmlName); |
|
35
|
|
|
|
|
36
|
|
|
$html .= sprintf('<li id="errormessage_%s" class="pt-3">', e($id)); |
|
37
|
|
|
|
|
38
|
|
|
// モデルからラベルを取得 |
|
39
|
|
|
$labelText = $fieldDefinitions[$field]['label'] ?? ucfirst($field); |
|
40
|
|
|
|
|
41
|
|
|
$html .= sprintf( |
|
42
|
|
|
'<a href="#%s" class="alert-link">%s</a>', |
|
43
|
|
|
e($id), |
|
44
|
|
|
__('error_at_x', 'Error at :name', ['name' => $labelText]), |
|
45
|
|
|
); |
|
46
|
|
|
|
|
47
|
|
|
$html .= '<ul class="ps-3">'; |
|
48
|
|
|
foreach ($messages as $message) { |
|
49
|
|
|
$html .= sprintf( |
|
50
|
|
|
'<li class="pt-2">%s%s</li>', |
|
51
|
|
|
$labelText, |
|
52
|
|
|
e($message) |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
$html .= '</ul></li>'; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$html .= '</ul></div>'; |
|
59
|
|
|
|
|
60
|
|
|
return $html; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Generates a section for displaying status messages with appropriate styling. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $message The message to display within the section. |
|
67
|
|
|
* @param string $status The status of the message. |
|
68
|
|
|
* @param bool $escape Whether to escape the message. |
|
69
|
|
|
* @return string HTML output for the status section. |
|
70
|
|
|
*/ |
|
71
|
|
|
public static function alertHtml( |
|
72
|
|
|
string $message, |
|
73
|
|
|
string $status = "success", |
|
74
|
|
|
bool $escape = true |
|
75
|
|
|
): string { |
|
76
|
|
|
// Define a status-class mapping table |
|
77
|
|
|
$statusClasses = [ |
|
78
|
|
|
'success' => 'alert alert-success', |
|
79
|
|
|
'info' => 'alert alert-info', |
|
80
|
|
|
'warning' => 'alert alert-warning', |
|
81
|
|
|
'danger' => 'alert alert-danger', |
|
82
|
|
|
]; |
|
83
|
|
|
|
|
84
|
|
|
// Get the CSS class from the table, default to "alert-secondary" |
|
85
|
|
|
$statusClass = $statusClasses[$status] ?? 'alert alert-secondary'; |
|
86
|
|
|
|
|
87
|
|
|
// Escape message if required |
|
88
|
|
|
$message = $escape ? e($message) : $message; |
|
89
|
|
|
|
|
90
|
|
|
// apply markdown instead of using HTML |
|
91
|
|
|
$message = MarkdownExtra::defaultTransform($message); |
|
92
|
|
|
$message = strip_tags($message, '<a>'); |
|
93
|
|
|
|
|
94
|
|
|
// Generate and return the HTML |
|
95
|
|
|
return sprintf( |
|
96
|
|
|
'<section class="%s" role="status"><p class="mb-0">%s</p></section>', |
|
97
|
|
|
e($statusClass), |
|
98
|
|
|
$message |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|