forked from chaosarium/lwt
-
Notifications
You must be signed in to change notification settings - Fork 20
/
display_impr_text_text.php
224 lines (206 loc) · 5.57 KB
/
display_impr_text_text.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
<?php
/**
* \file
* \brief Display an improved annotated text (text frame)
*
* Call: display_impr_text_text.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/display-impr-text-text.html
* @since 1.5.0
*/
require_once 'inc/session_utility.php';
/**
* Return the annotatino of a text.
*
* @param int $textid Text ID
*
* @return string Text annotations
*/
function get_annotated_text($textid)
{
global $tbpref;
$ann = get_first_value(
"SELECT TxAnnotatedText AS value
FROM " . $tbpref . "texts
WHERE TxID = " . $textid
);
return (string)$ann;
}
/**
* Get settings for this text.
*
* @param int $textid Text ID
*
* @return array{0: int, 1: bool} Text size, and if this text
* is rigth-to-left.
*/
function get_display_impr_text_text_data($textid)
{
global $tbpref;
$sql = "SELECT LgTextSize, LgRightToLeft
FROM {$tbpref}texts
JOIN {$tbpref}languages ON LgID = TxLgID
WHERE TxID = $textid";
$res = do_mysqli_query($sql);
$record = mysqli_fetch_assoc($res);
$textsize = (int) $record['LgTextSize'];
$rtlScript = (bool) $record['LgRightToLeft'];
mysqli_free_result($res);
return array($textsize, $rtlScript);
}
/**
* Prepare JavaScript interactions for the text content.
*
* @return void
*/
function do_diplay_impr_text_text_js()
{
?>
<script type="text/javascript">
//<![CDATA[
/** When user clicks an annotation. */
function click_ann() {
const attr = $(this).attr('style');
if(attr !== undefined && attr !== false && attr !== '') {
$(this).removeAttr('style');
}
else {
$(this).css('color', '#C8DCF0');
$(this).css('background-color', '#C8DCF0');
}
}
/** When user clicks the text. */
function click_text() {
const bc = $('body').css('color');
if ($(this).css('color') != bc) {
$(this).css('color', 'inherit');
$(this).css('background-color', '');
} else {
$(this).css('color','#E5E4E2');
$(this).css('background-color', '#E5E4E2');
}
}
$(document).ready(function(){
$('.anntransruby2').on('click', click_ann);
$('.anntermruby').on('click', click_text);
});
//]]>
</script>
<?php
}
/**
* Make the main content for a printed text.
*
* @param string $ann Annotations separated b tabulations "\t"
* @param string $textsize Text size
* @param bool $rtlScript True if this text is right-to-left
*
* @return void
*/
function do_diplay_impr_text_text_area($ann, $textsize, $rtlScript)
{
echo '<div id="print"' . ($rtlScript ? ' dir="rtl"' : '') . '>';
echo '<p style="font-size:' . $textsize . '%;line-height: 1.35; margin-bottom: 10px; ">';
$items = preg_split('/[\n]/u', $ann);
foreach ($items as $item) {
do_display_impr_text_text_word($item, $textsize);
}
echo "</p></div>";
}
/**
* Parse the annotations (translation/romanization) and return them.
*
* @param string[] $vals Annotations values
*
* @return array{0: string, 1: string} Translation and romanization.
*
* @global string $tbpref Database table prefix.
*/
function get_word_annotations($vals)
{
global $tbpref;
$trans = '';
$c = count($vals);
$rom = '';
if ($c > 2) {
if ($vals[2] !== '') {
$wid = (int)$vals[2];
$rom = get_first_value(
"SELECT WoRomanization AS value
FROM " . $tbpref . "words WHERE WoID = " . $wid
);
if (!isset($rom)) {
$rom = '';
}
}
}
if ($c > 3) {
$trans = $vals[3];
}
if ($trans == '*') {
$trans = $vals[1] . " "; // <- U+200A HAIR SPACE
}
return array($trans, $rom);
}
/**
* Display a single word item.
*
* @param string $item Word item, values separated by a tabulation.
* @param string $textsize Text size
*
* @return void
*/
function do_display_impr_text_text_word($item, $textsize)
{
$vals = preg_split('/[\t]/u', $item);
if ((int)$vals[0] > -1) {
list($trans, $rom) = get_word_annotations($vals);
echo ' <ruby>
<rb>
<span class="click anntermruby" style="color:black;"' .
($rom == '' ? '' : (' title="' . tohtml($rom) . '"')) . '>' .
tohtml($vals[1]) .
'</span>
</rb>
<rt>
<span class="click anntransruby2">' . tohtml($trans) . '</span>
</rt>
</ruby> ';
} elseif (count($vals) >= 2) {
echo str_replace(
"¶",
'</p>
<p style="font-size:' . $textsize .
'%;line-height: 1.3; margin-bottom: 10px;">',
" " . tohtml($vals[1])
);
}
}
/**
* Main function to do a complete printed text text content.
*
* @param int|null $textid Text ID, we will use page request if not provided.
*
* @return void
*/
function do_display_impr_text_text_main($textid = null)
{
if ($textid === null) {
$textid = (int)getreq('text');
}
do_diplay_impr_text_text_js();
$ann = get_annotated_text($textid);
if ($textid == 0 || strlen($ann) <= 0) {
header("Location: edit_texts.php");
exit();
}
list($textsize, $rtlScript) = get_display_impr_text_text_data($textid);
saveSetting('currenttext', $textid);
do_diplay_impr_text_text_area($ann, $textsize, $rtlScript);
}
?>