Repository navigation
Expand file tree
/
Copy pathdo_text_header.php
More file actions
321 lines (295 loc) · 8.99 KB
/
Copy pathdo_text_header.php
File metadata and controls
321 lines (295 loc) · 8.99 KB
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
<?php
/**
* \file
* \brief Responsible for drawing the header when reading texts
*
* Call: do_text_header.php?text=[textid]
*
* @package Lwt
* @author LWT Project <lwt-project@hotmail.com>
* @license Unlicense <http://unlicense.org/>
* @link https://hugofara.github.io/lwt/docs/html/do__text__header_8php.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 $textid ID of the text
*
* @global string $tbpref Table name prefix
*
* @since 2.0.3-fork
*
* @return null|string[]
*
* @psalm-return array<string, string>|null
*/
function getData($textid): ?array
{
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
{
?>
<h4>
<a href="edit_texts.php">
<?php echo_lwt_logo(); ?> LWT
</a>
|
<?php
quickMenu();
echo getPreviousAndNextTextLinks(
$textid, 'do_text.php?start=', false, ' | '
);
?> |
<a href="do_test.php?text=<?php echo $textid; ?>">
<img src="icn/question-balloon.png" title="Test" alt="Test" />
</a>
<a href="print_text.php?text=<?php echo $textid; ?>">
<img src="icn/printer.png" title="Print" alt="Print" />
</a>
<?php echo get_annotation_link($textid); ?>
<a href="edit_texts.php?chg=<?php echo $textid; ?>">
<img src="icn/document--pencil.png" title="Edit Text" alt="Edit Text" />
</a> |
<a
href="new_word.php?text=<?php echo $textid; ?>&lang=<?php echo $langid; ?>"
onclick="showRightFrames();"
>
<img src="icn/sticky-note--plus.png" title="New Term" alt="New Term" />
</a>
</h4>
<?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
{
?>
<table>
<tr>
<td>
<h3>READ ▶</h3>
</td>
<td class="width99pc">
<h3>
<?php
echo tohtml($title);
if (isset($sourceURI) && substr(trim($sourceURI), 0, 1) != '#') {
?>
<a href="<?php echo $sourceURI ?>">
<img src="<?php echo get_file_path('icn/chain.png') ?>" title="Text Source" alt="Text Source" />
</a>
<?php
}
?>
</h3>
</td>
</tr>
</table>
<?php
}
/**
* Prepare user settings for this text.
*
* @param string $textid Text ID
*
* @since 2.0.4-fork
*/
function do_settings($textid): void
{
// User settings
$showAll = getSettingZeroOrOne('showallwords', 1);
$showLearning = getSettingZeroOrOne('showlearningtranslations', 1);
?>
<table class="width99pc">
<tr>
<td>TO DO:
<span id="learnstatus"><?php echo texttodocount2($textid); ?></span>
</td>
<td
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.">
Show All
<input type="checkbox" id="showallwords" <?php echo get_checked($showAll); ?> onclick="showAllwordsClick();" />
</td>
<td
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.">
Learning Translations
<input type="checkbox" id="showlearningtranslations" <?php echo get_checked($showLearning); ?> onclick="showAllwordsClick();" />
</td>
<td id="thetextid" class="hide"><?php echo $textid; ?></td>
<td><button id="readTextButton">Read in browser</button></td>
</tr>
</table>
<?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 array $langDefs Definition of all languages. Normally $langDefs[$languageName][1] -> $languageCode
*
* @since 2.0.3-fork
*/
function browser_tts($text, $languageName): void
{
global $langDefs;
/**
* @var string $languageCode BCP 47 convention (i. e.: en-US) is suggested.
* Two-letter language code is enough (i. e. "en")
*/
$languageCode = $langDefs[$languageName][1];
/**
* @var string $phoneticText Phonetic reading for this text
*/
$phoneticText = phonetic_reading($text, $languageCode);
?>
<script type="text/javascript">
/// Main object for text-to-speech interaction with SpeechSynthesisUtterance
var text_reader = {
/// The text to read
text: <?php echo json_encode($phoneticText); ?>,
/// {string} ISO code for the language
lang: <?php echo json_encode($languageCode); ?>,
/// {string} Rate at wich the speech is done
rate: 0.8,
/**
* Reads a text using the browser text reader.
*
* @deprecated Since 2.3.0-fork, use of window.readTextAloud is recommended instead.
*/
readTextAloud: function () {
var msg = new SpeechSynthesisUtterance(this.text);
console.log('This function is deprecated, do not use it!')
msg.text = this.text;
msg.lang = this.lang;
msg.rate = this.rate;
window.speechSynthesis.cancel();
window.speechSynthesis.speak(msg);
},
};
/** Check browser compatibility before reading */
function init_reading() {
if (!('speechSynthesis' in window)) {
alert('Your browser does not support speechSynthesis!');
} else {
readRawTextAloud(text_reader.text, text_reader.lang);
}
}
/**
* Change the annotations display mode
*
* @param {string} mode The new annotation mode
*/
function annotationModeChanged(mode) {
console.log(mode);
}
</script>
<?php
}
/**
* Save the position of the audio reading for a text.
*
* @param string $textid ID of the text
*
* @since 2.0.4-fork
*/
function save_audio_position($textid): void
{
?>
<script type="text/javascript">
/**
* Save audio position
*/
function saveAudioPosition() {
if ($("#jquery_jplayer_1") === null || $("#jquery_jplayer_1").length == 0) {
return;
}
var pos = $("#jquery_jplayer_1").data("jPlayer").status.currentTime;
$.ajax({
type: "POST",
url:'inc/ajax_save_text_position.php',
data: {
id: '<?php echo $textid; ?>',
audioposition: pos
},
async: false
});
}
$(window).on('beforeunload', saveAudioPosition);
// We need to capture the text-to-speach event manually for Chrome
$(document).ready(function() {
$('#readTextButton').on('click', init_reading)
});
</script>
<?php
}
/**
* Main function for displaying header. It will print HTML content.
*
* @param string $textid ID of the requiered 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 = $record['TxTitle'];
$media = $record['TxAudioURI'];
if (!isset($media)) {
$media = '';
}
$media = trim($media);
saveSetting('currenttext', $textid);
if (!$only_body) {
pagestart_nobody($title, 'html, body {margin-bottom:0;}');
}
save_audio_position($textid);
do_header_row((int) $textid, $record['TxLgID']);
do_title($title, $record['TxSourceURI']);
do_settings($textid);
makeMediaPlayer($media, $record['TxAudioPosition']);
browser_tts($record["TxText"], $record["LgName"]);
if (!$only_body) {
pageend();
}
}
// Show the content automatically if text is in the request
if (false && getreq('text')) {
do_text_header_content(getreq('text'), false);
}
?>
You can’t perform that action at this time.