1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Utility script that can translate a text with Mailcode commands |
4
|
|
|
* to any of the available translation syntaxes. Meant to be opened |
5
|
|
|
* in a browser. |
6
|
|
|
* |
7
|
|
|
* @package Mailcode |
8
|
|
|
* @subpackage Tools |
9
|
|
|
* @author Sebastian Mordziol <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
use AppUtils\Request; |
15
|
|
|
use Mailcode\Mailcode; |
16
|
|
|
use Mailcode\Mailcode_Exception; |
17
|
|
|
use Mailcode\Mailcode_Translator_Exception; |
18
|
|
|
use function \AppLocalize\pt; |
19
|
|
|
use function \AppLocalize\pts; |
20
|
|
|
|
21
|
|
|
require_once 'prepend.php'; |
22
|
|
|
|
23
|
|
|
$request = new Request(); |
24
|
|
|
$mailcode = Mailcode::create(); |
25
|
|
|
$translator = $mailcode->createTranslator(); |
26
|
|
|
$syntaxes = $translator->getSyntaxes(); |
27
|
|
|
|
28
|
|
|
$commandsText = ''; |
29
|
|
|
$translated = ''; |
30
|
|
|
$error = null; |
31
|
|
|
$activeSyntax = $syntaxes[0]->getTypeID(); |
32
|
|
|
|
33
|
|
|
if($request->getBool('translate')) |
34
|
|
|
{ |
35
|
|
|
$commandsText = $request->getParam('mailcode'); |
36
|
|
|
$activeSyntax = (string)$request->registerParam('syntax') |
37
|
|
|
->setEnum($translator->getSyntaxNames()) |
38
|
|
|
->get($activeSyntax); |
39
|
|
|
|
40
|
|
|
try |
41
|
|
|
{ |
42
|
|
|
$translated = translateMailcode($commandsText, $activeSyntax); |
43
|
|
|
} |
44
|
|
|
catch (Mailcode_Exception $e) |
45
|
|
|
{ |
46
|
|
|
$translated = ''; |
47
|
|
|
$error = $e->getMessage(); |
48
|
|
|
|
49
|
|
|
$collection = $e->getCollection(); |
50
|
|
|
if($collection) |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
$first = $collection->getFirstError(); |
53
|
|
|
$error = $first->getMessage(); |
54
|
|
|
$matched = $first->getMatchedText(); |
55
|
|
|
if(!empty($matched)) { |
56
|
|
|
$error .= '<br>'.t('In command:').' <code>'.$matched.'</code>'; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Translates the specified text to Mailcode. |
64
|
|
|
* |
65
|
|
|
* @param string $subject |
66
|
|
|
* @param string $syntax |
67
|
|
|
* @return string |
68
|
|
|
* @throws Mailcode_Exception |
69
|
|
|
* @throws Mailcode_Translator_Exception |
70
|
|
|
*/ |
71
|
|
|
function translateMailcode(string $subject, string $syntax) : string |
72
|
|
|
{ |
73
|
|
|
$mailcode = Mailcode::create(); |
74
|
|
|
$translator = $mailcode->createTranslator()->createSyntax($syntax); |
75
|
|
|
|
76
|
|
|
$safeguard = $mailcode->createSafeguard($subject); |
77
|
|
|
return $translator->translateSafeguard($safeguard); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
?><!doctype html> |
81
|
|
|
<html lang="en"> |
82
|
|
|
<head> |
83
|
|
|
<meta charset="utf-8"> |
84
|
|
|
<title>Mailcode commands translator</title> |
85
|
|
|
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous"> |
86
|
|
|
<style> |
87
|
|
|
<?php echo $mailcode->createStyler()->getCSS() ?> |
88
|
|
|
BODY{ |
89
|
|
|
padding: 2rem 0; |
90
|
|
|
} |
91
|
|
|
TEXTAREA{ |
92
|
|
|
width: 100%; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
</style> |
96
|
|
|
</head> |
97
|
|
|
<body> |
98
|
|
|
<div class="container"> |
99
|
|
|
<h1><?php pt('Commands translation') ?></h1> |
100
|
|
|
<p> |
101
|
|
|
<?php |
102
|
|
|
pts('Enter the text that contains the mailcode commands to convert, and choose the output language to convert them to.'); |
103
|
|
|
pts('Can be any kind of text, including HTML/XML.'); |
104
|
|
|
?> |
105
|
|
|
</p> |
106
|
|
|
<p class="text-warning"> |
107
|
|
|
<strong><?php pt('Note:') ?></strong> |
108
|
|
|
<?php pt('The Mailcode commands must be valid.') ?> |
109
|
|
|
</p> |
110
|
|
|
<form method="post"> |
111
|
|
|
<p> |
112
|
|
|
<textarea class="form-control" name="mailcode" rows="10"><?php echo htmlspecialchars($commandsText) ?></textarea> |
113
|
|
|
</p> |
114
|
|
|
<p> |
115
|
|
|
<select name="syntax" class="form-control"> |
116
|
|
|
<?php |
117
|
|
|
foreach ($syntaxes as $syntax) |
118
|
|
|
{ |
119
|
|
|
$selected = ''; |
120
|
|
|
$typeID = $syntax->getTypeID(); |
121
|
|
|
|
122
|
|
|
if($typeID === $activeSyntax) { |
123
|
|
|
$selected = ' selected'; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
?> |
127
|
|
|
<option value="<?php echo $syntax->getTypeID() ?>>" <?php echo $selected ?>> |
128
|
|
|
<?php echo $syntax->getTypeID() ?> |
129
|
|
|
</option> |
130
|
|
|
<?php |
131
|
|
|
} |
132
|
|
|
?> |
133
|
|
|
</select> |
134
|
|
|
</p> |
135
|
|
|
<button type="submit" name="translate" value="yes" class="btn btn-primary"> |
136
|
|
|
<?php pt('Translate commands') ?> |
137
|
|
|
</button> |
138
|
|
|
</form> |
139
|
|
|
<p></p><br> |
140
|
|
|
<h2><?php pt('Translated commands') ?></h2> |
141
|
|
|
<?php |
142
|
|
|
if(empty($commandsText)) |
143
|
|
|
{ |
144
|
|
|
?> |
145
|
|
|
<div class="alert alert-info"> |
146
|
|
|
<?php pt('No commands specified.') ?> |
147
|
|
|
</div> |
148
|
|
|
<?php |
149
|
|
|
} |
150
|
|
|
else if($error) |
151
|
|
|
{ |
152
|
|
|
?> |
153
|
|
|
<div class="alert alert-danger"> |
154
|
|
|
<strong><?php pt('Error parsing commands:') ?></strong> |
155
|
|
|
<?php echo $error ?> |
156
|
|
|
</div> |
157
|
|
|
<?php |
158
|
|
|
} |
159
|
|
|
else |
160
|
|
|
{ |
161
|
|
|
?> |
162
|
|
|
<textarea class="form-control" rows="10"><?php echo htmlspecialchars($translated) ?></textarea> |
163
|
|
|
<?php |
164
|
|
|
} |
165
|
|
|
?> |
166
|
|
|
</div> |
167
|
|
|
</body> |
168
|
|
|
</html> |
169
|
|
|
|