source: trunk/Modules/Dictionary/Dictionary.php

Last change on this file was 893, checked in by chronos, 15 months ago
  • Fixed: Class types casting for better type checking.
  • Fixed: XML direct export.
  • Modified: User class instance moved from Core class to ModuleUser class.
File size: 15.5 KB
Line 
1<?php
2
3class ModuleDictionary extends Module
4{
5 function __construct(System $System)
6 {
7 parent::__construct($System);
8 $this->Name = 'Dictionary';
9 $this->Version = '1.0';
10 $this->Creator = 'Chronos';
11 $this->License = 'GNU/GPL';
12 $this->Description = 'Custom dictionary for better translation coordination';
13 $this->Dependencies = array('Translation');
14 }
15
16 function DoStart(): void
17 {
18 $this->System->RegisterPage(['dictionary'], 'PageDictionary');
19 Core::Cast($this->System)->RegisterMenuItem(array(
20 'Name' => 'Dictionary',
21 'Title' => T('Dictionary'),
22 'Hint' => T('Dictionary words from WoW'),
23 'Link' => $this->System->Link('/dictionary/'),
24 'Permission' => LICENCE_ANONYMOUS,
25 'Icon' => '',
26 ), 1);
27 if (array_key_exists('Search', $this->System->ModuleManager->Modules))
28 $this->System->ModuleManager->Modules['Search']->RegisterSearch('dictionary',
29 T('Dictionary'), array('Text', 'Description'),
30 '(SELECT * FROM `Dictionary` WHERE `Language` = '.Core::Cast($this->System)->Config['OriginalLanguage'].') AS `T`',
31 $this->System->Link('/dictionary/?search='));
32 }
33}
34
35class PageDictionary extends Page
36{
37 function WriteTranslatNames($Text, $mode)
38 {
39 $Output = '';
40 if ($mode == 0) $Output .= '..'.T('Found names of items, places and NPC');
41 if ($mode == 1) $Output .= '..'.T('All untranslated');
42 if ($mode == 2) $Output .= '..'.T('Untranslated');
43 $Output .= '<table class="BaseTable">';
44 $Output .= '<tr><th>'.T('Original').'</th>'.'<th>'.T('Translation').'</th></tr>';
45
46 // TODO: Remove static table names in code. They should be controlled from database
47 $TablesColumn = array
48 (
49 'TextGameObject' => 'Name',
50 'TextCreature' => 'name',
51 'TextItem' => 'Name',
52 'TextTransport' => 'Name',
53 'TextAreaTriggerTeleport' => 'Name',
54 'TextAreaTriggerTavern' => 'Name',
55 'Dictionary' => 'Text',
56 'TextArea' => 'Name',
57 'TextAreaPOI' => 'Name',
58 'TextCharacterClass' => 'Name',
59 'TextCharacterRace' => 'Name1',
60 'TextItemSubClass' => 'Name',
61 'TextCreatureType' => 'Name',
62 );
63
64 $buff = GetTranslatNames($Text, $mode, GetTranslatNamesArray());
65
66 // $buff[] = array($Line['ID'],GetIDbyName($Table),$Line[$Column],$Line['Tran']);
67 foreach ($buff as $Line)
68 {
69 if ($mode == 0)
70 {
71 if (strpos(strtolower($Text), strtolower($Line[2])) > 0)
72 {
73 $Output .= '<tr><td>'.$Line[2].'</td>';
74 if ($Line[3] <> '') $Output .= '<td><a target="_NEW2" href="'.$this->System->Link('/form.php?group='.$Line[1].'&amp;ID='.$Line[0]).'">'.$Line[3].'</a></td></tr>';
75 else $Output .= '<td><a target="_NEW2" href="'.$this->System->Link('/form.php?group='.$Line[1].'&amp;ID='.$Line[0]).'">'.T('Translate').'</a></td></tr>';
76 }
77 } else
78 {
79 $Output .= '<tr><td>'.$Line[2].'</td>';
80 if ($Line[3] <> '') $Output .= '<td><a target="_NEW2" href="'.$this->System->Link('/form.php?group='.$Line[1].'&amp;ID='.$Line[0]).'">'.$Line[3].'</a></td></tr>';
81 else $Output .= '<td><a target="_NEW2" href="'.$this->System->Link('/form.php?group='.$Line[1].'&amp;ID='.$Line[0]).'">'.T('Translate').'</a></td></tr>';
82 }
83 }
84 $Output .= '</table>';
85 return $Output;
86 }
87
88 function DictionaryInsert()
89 {
90 $User = ModuleUser::Cast($this->System->GetModule('User'))->User;
91 $Output = '';
92 if ($User->Licence(LICENCE_USER))
93 {
94 $Output .= '<form action="?action=save" method="post">'.
95 '<fieldset><legend>'.T('New word insertion').'</legend>'.
96 '<table><tr><td>'.
97 T('Original word').':</td><td><input type="text" name="Original" /></td></tr>'.
98 '<tr><td>'.T('Translated').':</td><td><input type="text" name="Translated" /></td></tr>'.
99 '<tr><td>'.T('Language').':</td><td>'.WriteLanguages($_SESSION['language']).'</td></tr>'.
100 '<tr><td>'.T('Selectable description').':</td><td><input type="text" name="Description" /></td></tr>'.
101 '<tr><td colspan="2"><input type="submit" value="'.T('Save').'" /></td></tr>'.
102 '</td></tr></table>'.
103 '</fieldset>'.
104 '</form>';
105 } else $Output .= ShowMessage(T('Access denied'), MESSAGE_CRITICAL);
106 return $Output;
107 }
108
109 function DictionarySave()
110 {
111 $User = ModuleUser::Cast($this->System->GetModule('User'))->User;
112 if ($User->Licence(LICENCE_USER))
113 {
114 if (array_key_exists('Original', $_POST) and array_key_exists('Translated', $_POST) and array_key_exists('Description', $_POST))
115 {
116 // Check if original text exists and determine entry id
117 $DbResult = $this->Database->query('SELECT * FROM `Dictionary` WHERE '.
118 '`Text` = "'.$_POST['Original'].'" AND `Language`= '.Core::Cast($this->System)->Config['OriginalLanguage']);
119 if ($DbResult->num_rows > 0)
120 {
121 $DbRow = $DbResult->fetch_assoc();
122 $Entry = $DbRow['Entry'];
123 } else
124 {
125 $DbResult = $this->Database->query('SELECT MAX(`Entry`) FROM `Dictionary`');
126 $DbRow = $DbResult->fetch_row();
127 $Entry = $DbRow[0] + 1;
128 $this->Database->query('INSERT INTO `Dictionary` ( `Text` , `Entry` , `Description` , '.
129 '`User`, `Language` ) VALUES ("'.$_POST['Original'].'", "'.$Entry.'", "", NULL, '.Core::Cast($this->System)->Config['OriginalLanguage'].');');
130 }
131
132 $DbResult = $this->Database->query('SELECT `Id` FROM `Dictionary` WHERE '.
133 '`Entry` = '.$Entry.' AND `Language`='.$_POST['Language'].' AND `User`='.$User->Id);
134 if ($DbResult->num_rows > 0)
135 {
136 $DbRow = $DbResult->fetch_assoc();
137 $this->Database->query('UPDATE `Dictionary` SET `Text`="'.$_POST['Translated'].'", '.
138 '`Description` = "'.$_POST['Description'].'" WHERE Id='.$DbRow['Id']);
139 } else
140 $this->Database->query('INSERT INTO `Dictionary` ( `Text` , `Entry` , `Description` , '.
141 '`User`, `Language` ) VALUES ("'.$_POST['Translated'].'", "'.$Entry.'", "'.
142 $_POST['Description'].'", '.$User->Id.', '.$_POST['Language'].')');
143 $Output = ShowMessage('Záznam byl uložen!');
144 } else $Output = ShowMessage(T('You have to fill all column of form.'), MESSAGE_CRITICAL);
145 } else $Output = ShowMessage(T('Access denied'), MESSAGE_CRITICAL);
146 return $Output;
147 }
148
149 function DictionaryRemove()
150 {
151 $User = ModuleUser::Cast($this->System->GetModule('User'))->User;
152 if ($User->Licence(LICENCE_USER))
153 {
154 $this->Database->query('DELETE FROM `Dictionary` WHERE (`User`='.$User->Id.') AND (`Id`='.($_GET['id'] * 1).')');
155 $Output = ShowMessage(T('Record removed'));
156 } else $Output = ShowMessage(T('Access denied'), MESSAGE_CRITICAL);
157 return $Output;
158 }
159
160 function DictionaryModify()
161 {
162 $User = ModuleUser::Cast($this->System->GetModule('User'))->User;
163 if ($User->Licence(LICENCE_USER))
164 {
165 $DbResult = $this->Database->query('SELECT * FROM `Dictionary` WHERE `Id`='.($_GET['id'] * 1));
166 if ($DbResult->num_rows > 0)
167 {
168 $DbRow = $DbResult->fetch_assoc();
169 $DbResult = $this->Database->query('SELECT * FROM `Dictionary` WHERE (`User` IS NULL) '.
170 'AND (`Entry`='.$DbRow['Entry'].') AND (`Language`= '.Core::Cast($this->System)->Config['OriginalLanguage'].')');
171 $DbRow2 = $DbResult->fetch_assoc();
172 $Output = '<form action="?action=save" method="post">'.
173 '<fieldset><legend>Upravení slova</legend>'.
174 '<input type="hidden" name="id" value="'.$_GET['id'].'"/>'.
175 '<table><tr><td>'.
176 'Původní anglické slovo:</td><td><input type="text" name="Original" value="'.htmlspecialchars($DbRow2['Text']).'" /></td></tr>'.
177 '<tr><td>Přeložené:</td><td><input type="text" name="Translated" value="'.htmlspecialchars($DbRow['Text']).'" /></td></tr>'.
178 '<tr><td>'.T('Language').':</td><td>'.WriteLanguages($DbRow['Language']).'</td></tr>'.
179 '<tr><td>'.T('Description').':</td><td><input type="text" name="Description" value="'.htmlspecialchars($DbRow['Description']).'" /></td></tr>'.
180 '<tr><td colspan="2"><input type="submit" value="'.T('Save').'" /></td></tr>'.
181 '</td></tr></table>'.
182 '</fieldset>'.
183 '</form>';
184 } else $Output = ShowMessage(T('Item not found'), MESSAGE_CRITICAL);
185 } else $Output = ShowMessage(T('Access denied'), MESSAGE_CRITICAL);
186 return $Output;
187 }
188
189 function DictionaryGroup()
190 {
191 $TranslationTree = $this->System->ModuleManager->Modules['Translation']->GetTranslationTree();
192
193 $Output = '';
194 $GroupId = LoadGroupIdParameter();
195 if (array_key_exists('mode', $_GET)) $mode = $_GET['mode'];
196 else $mode = 0; //0 = exact names, 1=all names, 2=nontranslated
197 $Table = $TranslationTree[$GroupId]['TablePrefix'];
198
199 if (array_key_exists('ID', $_GET))
200 {
201 $TextID = $_GET['ID'];
202 $Output .= T('Translated names related to translation text').': <a target="_NEW" href="'.$this->System->Link('/form.php?group='.$GroupId.'&amp;ID='.$TextID).'">'.$TextID.'</a><br />'.
203 '<a href="?action=group&amp;mode=1&amp;group='.$GroupId.'&amp;ID='.$TextID.'" title="'.T('Show all similar results').'">'.T('All').'</a> '.
204 '<a href="?action=group&amp;mode=0&amp;group='.$GroupId.'&amp;ID='.$TextID.'" title="'.T('Show all exact results').'">'.T('Exact').'</a> '.
205 '<a href="?action=group&amp;mode=2&amp;group='.$GroupId.'&amp;ID='.$TextID.'" title="'.T('Show all untranslated').'">'.T('Untranslated').'</a> ';
206 $DbResult = $this->Database->query('SELECT * FROM '.$Table.' WHERE ID = '.$TextID);
207 $Line = $DbResult->fetch_assoc();
208 if (!$Line) $Output .= ShowMessage(T('Translation not found'), MESSAGE_CRITICAL);
209 else
210 {
211 $Text = '';
212 foreach ($TranslationTree[$GroupId]['Items'] as $Index => $TextItem)
213 $Text .= ' '.$Line[$TextItem['Column']];
214 $Output .= $this->WriteTranslatNames($Text, $mode);
215 }
216 }
217 return $Output;
218 }
219
220 function DictionaryShow()
221 {
222 $User = ModuleUser::Cast($this->System->GetModule('User'))->User;
223 global $LanguageList;
224
225 $Output = '<form action="?" method="get" style="margin: 0px; padding: 0px;">'.
226 '<table style="width: 100%; height: 100%;">';
227
228 if (array_key_exists('search', $_GET)) $Search = $_GET['search'];
229 else $Search = '';
230
231 $Output .= '<tr><td>
232 <input type="text" value="'.$Search.'" name="search" size="30" />
233 <input type="submit" value="'.T('Search').'" />';
234 if ($User->Licence(LICENCE_USER))
235 $Output .= ' <a href="?action=insert">'.T('Add word').'</a>';
236
237 $Output .= '</td></tr>'.
238 '<tr><td>'.T('Language').': ';
239 $Lang = '<a href="?language=">'.T('All').'</a>';
240 if ($_SESSION['language'] == '') $Output .= '<strong>'.$Lang.'</strong> ';
241 else $Output .= $Lang;
242 foreach ($LanguageList as $Language)
243 if ($Language['Enabled'] == 1)
244 {
245 $Lang = ' <a href="?language='.$Language['Id'].'">'.T($Language['Name']).'</a>';
246 if ($Language['Id'] == $_SESSION['language']) $Output .= '<strong>'.$Lang.'</strong> ';
247 else $Output .= $Lang;
248 }
249 $Output .= '</td></tr>'.
250 '<tr><td>';
251
252 if (!is_numeric($_SESSION['language'])) $LanguageFilter = '';
253 else $LanguageFilter = ' AND (`T1`.`Language` = '.$_SESSION['language'].')';
254
255 if ($Search <> '')
256 {
257 $Condition = ' AND (LOWER(`T1`.`Text`) LIKE LOWER("%'.$Search.'%")) OR '.
258 ' (LOWER(`T2`.`Text`) LIKE LOWER("%'.$Search.'%")) OR '.
259 '(LOWER(`T1`.`Description`) LIKE LOWER("%'.$Search.'%"))';
260 } else $Condition = '';
261 $sql = 'SELECT `User`.`Name` AS `UserName`, `User`.`Id` AS `UserId`, '.
262 '`Language`.`Name` AS `LangName`, `T1`.`Id` AS `Id`, '.
263 '`T1`.`Entry`, `T2`.`Text` AS `Original`, `T1`.`Text` AS `Translated`, `T1`.`Description` '.
264 'FROM `Dictionary` AS `T1` JOIN `Dictionary` AS `T2` '.
265 'ON (`T2`.`Entry` = `T1`.`Entry`) AND (`T2`.`Language` = '.Core::Cast($this->System)->Config['OriginalLanguage'].') '.
266 'JOIN `Language` ON `Language`.`Id` = `T1`.`Language` '.
267 'JOIN `User` ON `User`.`ID` = `T1`.`User` '.
268 'WHERE 1'.
269 $LanguageFilter.$Condition;
270
271 $DbResult = $this->Database->query('SELECT COUNT(*) FROM ('.$sql.') AS `T1`');
272 $DbRow = $DbResult->fetch_row();
273 $PageList = GetPageList($DbRow[0]);
274
275 $Output .= $PageList['Output'];
276
277 if (is_numeric($_SESSION['language'])) $LanguageName = $LanguageList[$_SESSION['language']]['Name'];
278 else $LanguageName = T('Translation');
279 $TableColumns = array(
280 array('Name' => 'Original', 'Title' => T('English')),
281 array('Name' => 'Translated', 'Title' => T($LanguageName)),
282 );
283 if (!is_numeric($_SESSION['language'])) $TableColumns[] = array('Name' => 'LangName', 'Title' => T('Language'));
284 $TableColumns[] = array('Name' => 'Description', 'Title' => T('Description'));
285 $TableColumns[] = array('Name' => 'UserName', 'Title' => T('Translator'));
286 if ($User->Licence(LICENCE_USER)) $TableColumns[] = array('Name' => '', 'Title' => T('Action'));
287 $Order = GetOrderTableHeader($TableColumns, 'Original');
288 $Output .= '<table class="BaseTable">'.$Order['Output'];
289
290 $sql_page = $sql.$Order['SQL'].$PageList['SQLLimit'];
291 $DbResult = $this->Database->query($sql_page);
292 while ($Line = $DbResult->fetch_assoc())
293 {
294 $Output .= '<tr>'.
295 '<td>'.htmlspecialchars($Line['Original']).'</td>'.
296 '<td><strong>'.htmlspecialchars($Line['Translated']).'</strong></td>';
297 if (!is_numeric($_SESSION['language'])) $Output .= '<td>'.T($Line['LangName']).'</td>';
298 $Output .= '<td>'.htmlspecialchars($Line['Description']).'</td>'.
299 '<td><a href="'.$this->System->Link('/user/?user='.$Line['UserId']).'">'.
300 $Line['UserName'].'</a></td>';
301 if ($User->Licence(LICENCE_USER))
302 {
303 if ($Line['UserId'] == $User->Id)
304 $Output .= '<td><a href="?action=remove&amp;id='.$Line['Id'].
305 '" onclick="return confirmAction(\''.T('Do you really want to delete item?').'\');">'.T('Delete').'</a>'.
306 ' <a href="?action=modify&amp;id='.$Line['Id'].'">'.T('Modify').'</a></td>';
307 else $Output .= '<td></td>';
308 }
309 $Output .= '</tr>';
310 }
311 $Output .= '</table>'.
312 $PageList['Output'].
313 '</td></tr>'.
314 '</table></form>';
315 return $Output;
316 }
317
318 function Show(): string
319 {
320 global $LanguageList;
321
322 $User = ModuleUser::Cast($this->System->GetModule('User'))->User;
323 $this->Title = T('Dictionary');
324
325 $LanguageList = GetLanguageList();
326
327 if (!isset($_SESSION['language']))
328 {
329 if ($User->Licence(LICENCE_USER))
330 {
331 $_SESSION['language'] = $User->Language;
332 } else $_SESSION['language'] = '';
333 }
334 if (array_key_exists('language', $_GET))
335 {
336 if ($_GET['language'] == '') $_SESSION['language'] = '';
337 else $_SESSION['language'] = $_GET['language'] * 1;
338 }
339
340 $Output = '<h3>'.T('Dictionary').'</h3>';
341
342 $ShowList = true;
343 if (array_key_exists('action', $_GET))
344 {
345 if ($_GET['action'] == 'group')
346 {
347 $Output .= $this->DictionaryGroup();
348 $ShowList = false;
349 }
350 else if ($_GET['action'] == 'insert') $Output .= $this->DictionaryInsert();
351 else if ($_GET['action'] == 'save') $Output .= $this->DictionarySave();
352 else if ($_GET['action'] == 'remove') $Output .= $this->DictionaryRemove();
353 else if ($_GET['action'] == 'modify') $Output .= $this->DictionaryModify();
354 else $Output .= ShowMessage(T('Unknown action'), MESSAGE_CRITICAL);
355 }
356 if ($ShowList == true) $Output .= $this->DictionaryShow();
357 return $Output;
358 }
359}
Note: See TracBrowser for help on using the repository browser.