forked from chaosarium/lwt
-
Notifications
You must be signed in to change notification settings - Fork 20
/
do_text_header.php
330 lines (306 loc) · 9.31 KB
/
do_text_header.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
/**
* \file
* \brief Responsible for drawing the header when reading texts
*
* Call: do_text_header.php?text=[textid]
*
* PHP version 8.1
*
* @package Lwt
* @author LWT Project <[email protected]>
* @license Unlicense <http://unlicense.org/>
* @link https://hugofara.github.io/lwt/docs/php/files/do-text-header.html
* @since 1.0.3
*/
require_once 'inc/session_utility.php';
// To get the BCP 47 language tag
require_once 'inc/langdefs.php' ;
/**
* Get the text and language data associated with the text.
*
* @param string|int $textid ID of the text
*
* @global string $tbpref Table name prefix
*
* @since 2.0.3-fork
*
* @return (float|int|null|string)[]|false|null LgName, TxLgID, TxText, TxTitle, TxAudioURI, TxSourceURI, TxAudioPosition for the text.
*
* @psalm-return array<string, float|int|null|string>|false|null
*/
function getData($textid)
{
global $tbpref;
$sql =
'SELECT LgName, TxLgID, TxText, TxTitle, TxAudioURI, TxSourceURI, TxAudioPosition
FROM ' . $tbpref . 'texts
JOIN ' . $tbpref . 'languages ON TxLgID = LgID
WHERE TxID = ' . $textid;
$res = do_mysqli_query($sql);
$record = mysqli_fetch_assoc($res);
mysqli_free_result($res);
return $record;
}
/**
* Print the main title row.
*
* @param int $textid Text ID
* @param string $langid Language ID to navigate between
* texts of same language
*
* @since 2.0.4-fork
*/
function do_header_row($textid, $langid): void
{
?>
<div class="flex-header">
<div>
<a href="edit_texts.php" target="_top">
<?php echo_lwt_logo(); ?>
</a>
</div>
<div>
<?php
echo getPreviousAndNextTextLinks(
$textid,
'do_text.php?start=',
false,
''
);
?>
</div>
<div>
<a href="do_test.php?text=<?php echo $textid; ?>" target="_top">
<img src="icn/question-balloon.png" title="Test" alt="Test" />
</a>
<a href="print_text.php?text=<?php echo $textid; ?>" target="_top">
<img src="icn/printer.png" title="Print" alt="Print" />
</a>
<?php echo get_annotation_link($textid); ?>
<a target="_top" href="edit_texts.php?chg=<?php echo $textid; ?>">
<img src="icn/document--pencil.png" title="Edit Text" alt="Edit Text" />
</a>
</div>
<div>
<a
href="new_word.php?text=<?php echo $textid; ?>&lang=<?php echo $langid; ?>"
target="ro" onclick="showRightFrames();"
>
<img src="icn/sticky-note--plus.png" title="New Term" alt="New Term" />
</a>
</div>
<div>
<?php quickMenu(); ?>
</div>
</div>
<?php
}
/**
* Print the title of the text.
*
* @param string $title Title of the text
* @param string $sourceURI URL of the text (if any)
*
* @since 2.0.4-fork
*/
function do_title($title, $sourceURI): void
{
?>
<h1>READ ▶
<?php
echo tohtml($title);
if (isset($sourceURI) && !str_starts_with(trim($sourceURI), '#')) {
?>
<a href="<?php echo $sourceURI ?>" target="_blank">
<img src="<?php echo get_file_path('icn/chain.png') ?>" title="Text Source" alt="Text Source" />
</a>
<?php
}
?>
</h1>
<?php
}
/**
* Prepare user settings for this text.
*
* @param string|int $textid Text ID
*
* @since 2.0.4-fork
*/
function do_settings($textid): void
{
// User settings
$showAll = getSettingZeroOrOne('showallwords', 1);
$showLearning = getSettingZeroOrOne('showlearningtranslations', 1);
?>
<div class="flex-spaced">
<div>
Unknown words:
<span id="learnstatus"><?php echo todo_words_content((int) $textid); ?></span>
</div>
<div
title="[Show All] = ON: ALL terms are shown, and all multi-word terms are shown as superscripts before the first word. The superscript indicates the number of words in the multi-word term.
[Show All] = OFF: Multi-word terms now hide single words and shorter or overlapping multi-word terms.">
<label for="showallwords">Show All</label>
<input type="checkbox" id="showallwords" <?php echo get_checked($showAll); ?>
onclick="showAllwordsClick();" />
</div>
<div
title="[Learning Translations] = ON: Terms with Learning Level 1 display their translations under the term.
[Learning Translations] = OFF: No translations are shown in the reading mode.">
<label for="showlearningtranslations">Translations</label>
<input type="checkbox" id="showlearningtranslations"
<?php echo get_checked($showLearning); ?> onclick="showAllwordsClick();" />
</div>
<div id="thetextid" class="hide"><?php echo $textid; ?></div>
<div><button id="readTextButton">Read in browser</button></div>
</div>
<?php
}
/**
* Prints javascript data and function to read text
* in your browser.
*
* @param string $text Text to read
* @param string $languageName Full name of the language (i. e.: "English")
*
* @global string $tbpref
*
* @since 2.0.3-fork
* @since 2.9.1-fork Function may work even when the language name was manually changed.
*/
function browser_tts($text, $languageName): void
{
global $tbpref;
$lg_id = (int) get_first_value(
"SELECT LgID as value
FROM {$tbpref}languages
WHERE LgName = " . convert_string_to_sqlsyntax($languageName)
);
$languageCode = getLanguageCode($lg_id, LWT_LANGUAGES_ARRAY);
// Phonetic reading for this text. As it can be long do not use AJAX
$phoneticText = phoneticReading($text, $lg_id);
$voiceApi = get_first_value(
"SELECT LgTTSVoiceAPI AS value FROM {$tbpref}languages
WHERE LgID = $lg_id"
);
?>
<script type="text/javascript">
/// Main object for text-to-speech interaction with SpeechSynthesisUtterance
const text_reader = {
/// The text to read
text: <?php echo json_encode($phoneticText); ?>,
/// {string} ISO code for the language
lang: getLangFromDict(LWT_DATA.language.translator_link) || <?php echo json_encode($languageCode); ?>,
/// {string} Rate at wich the speech is done, deprecated since 2.10.0
rate: 0.8
};
LWT_DATA.language.ttsVoiceApi = <?php echo json_encode($voiceApi); ?>;
/**
* Check browser compatibility before reading
*/
function init_reading() {
if (!('speechSynthesis' in window)) {
alert('Your browser does not support speechSynthesis!');
return;
}
readRawTextAloud(
text_reader.text, getLangFromDict(LWT_DATA.language.translator_link) || text_reader.lang
);
}
/** Start and stop the reading feature. */
function toggle_reading() {
const synth = window.speechSynthesis;
if (synth === undefined) {
alert('Your browser does not support speechSynthesis!');
return;
}
if (synth.speaking) {
synth.cancel();
} else {
init_reading();
}
}
/**
* Change the annotations display mode
*
* @param {string} mode The new annotation mode
*/
function annotationModeChanged(mode) {
console.log(mode);
// 2.9.0: seems to be a debug function, candidate to deletion
}
</script>
<?php
}
/**
* Save the position of the audio reading for a text.
*
* @param string|int $textid ID of the text
*
* @since 2.0.4-fork
*/
function save_audio_position($textid): void
{
if (is_string($textid)) {
$textid_int = (int) $textid;
} else {
$textid_int = $textid;
}
?>
<script type="text/javascript">
/**
* Save text status, for instance audio position
*/
function saveTextStatus() {
// Save audio if present
if ($("#jquery_jplayer_1") === null || $("#jquery_jplayer_1").length == 0) {
return;
}
const pos = $("#jquery_jplayer_1").data("jPlayer").status.currentTime;
const text_id = <?php echo $textid_int; ?>;
saveAudioPosition(text_id, pos);
}
$(window).on('beforeunload', saveTextStatus);
// We need to capture the text-to-speach event manually for Chrome
$(document).ready(function() {
$('#readTextButton').on('click', toggle_reading)
});
</script>
<?php
}
/**
* Main function for displaying header. It will print HTML content.
*
* @param string|int $textid ID of the required text
* @param bool $only_body If true, only show the inner body. If false, create a
* complete HTML document.
*
* @since 2.0.3-fork
*/
function do_text_header_content($textid, $only_body = true): void
{
$record = getData($textid);
$title = (string) $record['TxTitle'];
if (isset($record['TxAudioURI'])) {
$media = (string) $record['TxAudioURI'];
} else {
$media = '';
}
$media = trim($media);
saveSetting('currenttext', $textid);
if (!$only_body) {
pagestart_nobody($title, 'html, body {margin-bottom:0;}');
}
save_audio_position((int) $textid);
do_header_row((int) $textid, $record['TxLgID']);
do_title($title, $record['TxSourceURI']);
do_settings($textid);
makeMediaPlayer($media, (int) $record['TxAudioPosition']);
browser_tts($record["TxText"], $record["LgName"]);
if (!$only_body) {
pageend();
}
}
?>