source: trunk/Modules/Translation/Translation.php

Last change on this file was 893, checked in by chronos, 21 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: 9.5 KB
Line 
1<?php
2
3include_once(dirname(__FILE__).'/Comparison.php');
4include_once(dirname(__FILE__).'/Form.php');
5include_once(dirname(__FILE__).'/Save.php');
6include_once(dirname(__FILE__).'/TranslationList.php');
7include_once(dirname(__FILE__).'/Progress.php');
8include_once(dirname(__FILE__).'/LoadNames.php');
9include_once(dirname(__FILE__).'/UserLevel.php');
10
11class ModuleTranslation extends Module
12{
13 public array $TranslationTree;
14
15 function __construct(System $System)
16 {
17 parent::__construct($System);
18 $this->Name = 'Translation';
19 $this->Version = '1.0';
20 $this->Creator = 'Chronos';
21 $this->License = 'GNU/GPL';
22 $this->Description = 'Translation of text items and groups from original language to other languages.';
23 $this->Dependencies = array('News', 'Search');
24 }
25
26 function DoStart(): void
27 {
28 $this->System->RegisterPage(['comparison.php'], 'PageTranslationComparison');
29 $this->System->RegisterPage(['form.php'], 'PageTranslationForm');
30 $this->System->RegisterPage(['save.php'], 'PageTranslationSave');
31 $this->System->RegisterPage(['progress'], 'PageProgress');
32 $this->System->RegisterPage(['translation-groups'], 'PageTranslationGroups');
33 $this->System->RegisterPage(['TranslationList.php'], 'PageTranslationList');
34 $this->System->RegisterPage(['LoadNames.php'], 'PageLoadNames');
35 $this->System->ModuleManager->Modules['News']->RegisterRSS(array('Title' => T('Last translations'),
36 'Channel' => 'translation', 'Callback' => array($this, 'ShowRSS'), 'Permission' => LICENCE_ANONYMOUS));
37 Core::Cast($this->System)->RegisterMenuItem(array(
38 'Title' => T('Completion status'),
39 'Hint' => 'Stav dokončení překládů',
40 'Link' => $this->System->Link('/progress/'),
41 'Permission' => LICENCE_ANONYMOUS,
42 'Icon' => '',
43 ), 1);
44 Core::Cast($this->System)->RegisterMenuItem(array(
45 'Title' => T('Data source'),
46 'Hint' => 'Informace o překladových skupinách',
47 'Link' => $this->System->Link('/translation-groups/'),
48 'Permission' => LICENCE_ANONYMOUS,
49 'Icon' => '',
50 ));
51 if (array_key_exists('Search', $this->System->ModuleManager->Modules))
52 {
53 $TranslationTree = $this->System->ModuleManager->Modules['Translation']->GetTranslationTree();
54 foreach ($TranslationTree as $Group)
55 {
56 $Table = $Group['TablePrefix'];
57
58 $Columns = array('ID', 'Entry');
59 foreach ($Group['Items'] as $Item)
60 {
61 if ($Item['Column'] != '') $Columns[] = $Item['Column'];
62 }
63
64 $this->System->ModuleManager->Modules['Search']->RegisterSearch('group'.$Group['Id'],
65 sprintf(T('Translation group "%s"'), $Group['Name']), $Columns, '`'.$Table.'`',
66 $this->System->Link('/TranslationList.php?group='.
67 $Group['Id'].'&amp;user=0&amp;state=0&amp;entry=&amp;text='));
68 }
69 }
70 Core::Cast($this->System)->RegisterPageBarItem('Right', 'TranslatedMenu', array($this, 'ShowTranslatedMenu'));
71 }
72
73 function ShowRSS()
74 {
75 $Items = array();
76 $DbResult = $this->Database->query('SELECT UNIX_TIMESTAMP(`Date`) AS `Date`, `User`.`Name` AS `UserName`, `Text` FROM `Log` '.
77 'JOIN `User` ON `User`.`ID` = `Log`.`User` WHERE `Type` = 1 ORDER BY `Date` DESC LIMIT 100');
78 while ($DbRow = $DbResult->fetch_assoc())
79 {
80 $Items[] = array
81 (
82 'Title' => strip_tags($DbRow['Text'].' ('.$DbRow['UserName'].')'),
83 'Link' => 'http://'.Core::Cast($this->System)->Config['Web']['Host'].$this->System->Link('/'),
84 'Description' => $DbRow['Text'],
85 'Time' => $DbRow['Date'],
86 );
87 }
88 $Output = GenerateRSS(array
89 (
90 'Title' => Core::Cast($this->System)->Config['Web']['Title'].' - '.T('Last translations'),
91 'Link' => 'https://'.Core::Cast($this->System)->Config['Web']['Host'].$this->System->Link('/'),
92 'Description' => Core::Cast($this->System)->Config['Web']['Description'],
93 'WebmasterEmail' => Core::Cast($this->System)->Config['Web']['AdminEmail'],
94 'Items' => $Items,
95 ));
96 return $Output;
97 }
98
99 function ShowBox()
100 {
101 $Count = 40;
102 $Output = '<strong>'.T('Last translated').':</strong>';
103 $Output .= '<div class="box">';
104
105 $GroupListQuery = 'SELECT `Group`.* FROM `Group`';
106 $Query = '';
107 $UnionItems = array();
108 $DbResult = $this->Database->query($GroupListQuery);
109 if ($DbResult->num_rows > 0)
110 {
111 while ($DbRow = $DbResult->fetch_assoc())
112 {
113 $UnionItems[] = 'SELECT `T`.`ID`, `T`.`Take`, `T`.`User`, `T`.`ModifyTime`, `T`.`Group`, `T`.`GroupName` '.
114 'FROM (SELECT `T`.`User`, `T`.`ID`, `T`.`ModifyTime`, '.
115 $DbRow['Id'].' AS `Group`, "'.addslashes($DbRow['Name']).'" AS `GroupName`, `T`.`Take` FROM `'.
116 $DbRow['TablePrefix'].'` AS `T`'.
117 ' WHERE (`T`.`Complete` = 1) AND (`T`.`Language` != '.Core::Cast($this->System)->Config['OriginalLanguage'].') ORDER BY `T`.`ModifyTime` DESC LIMIT '.
118 $Count.') AS `T`';
119 }
120 $Query = 'SELECT `TT`.*, `User`.`Name` AS `UserName`, `User`.`Id` AS `UserId` '.
121 ' FROM ('.implode(' UNION ', $UnionItems).') AS `TT`'.
122 ' JOIN `User` ON `User`.`Id` = `TT`.`User`'.
123 ' ORDER BY `ModifyTime` DESC LIMIT '.$Count;
124 $DbResult = $this->Database->query($Query);
125 $Output .= '<table class="MiniTable"><tr><th>'.T('Date').'</th><th>'.T('Who').'</th><th>'.T('New').'</th><th>'.T('Source').'</th><th>'.T('Group').'</th></tr>';
126 while ($DbRow = $DbResult->fetch_assoc())
127 {
128 $Output .= '<tr><td>'.HumanDate($DbRow['ModifyTime']).'</td>'.
129 '<td><a href="'.$this->System->Link('/user/?user='.$DbRow['UserId']).'">'.$DbRow['UserName'].'</a></td>'.
130 '<td><a href="'.$this->System->Link('/form.php?group='.$DbRow['Group'].'&amp;ID='.$DbRow['ID']).'">'.$DbRow['ID'].'</a></td>'.
131 '<td><a href="'.$this->System->Link('/form.php?group='.$DbRow['Group'].'&amp;ID='.$DbRow['Take']).'">'.$DbRow['Take'].'</a></td>'.
132 '<td><a href="'.$this->System->Link('/TranslationList.php?group='.$DbRow['Group'].'&amp;action=filter').'">'.T($DbRow['GroupName']).'</a></td></tr>';
133 }
134 $Output .= '</table>';
135 }
136 $Output .= '</div>';
137 return $Output;
138 }
139
140 function GetTranslationTree()
141 {
142 if (isset($this->TranslationTree)) return $this->TranslationTree;
143 else
144 {
145 $Result = array();
146 $Groups = array();
147 $DbResult = $this->System->Database->query('SELECT *, UNIX_TIMESTAMP(`LastImport`) AS `LastImportTime` FROM `Group`');
148 while ($DbRow = $DbResult->fetch_assoc())
149 $Groups[T($DbRow['Name'])] = $DbRow;
150 ksort($Groups);
151 foreach ($Groups as $Group)
152 {
153 $Group['Items'] = array();
154 $Group['Game'] = T($Group['Name']);
155 $Result[$Group['Id']] = $Group;
156 }
157 $DbResult = $this->System->Database->query('SELECT * FROM `GroupItem` ORDER BY `Group`, `Sequence`');
158 while ($DbRow = $DbResult->fetch_assoc())
159 {
160 $Result[$DbRow['Group']]['Items'][] = $DbRow;
161 }
162 $this->TranslationTree = $Result;
163 return $Result;
164 }
165 }
166
167 function ShowTranslatedMenu()
168 {
169 $User = ModuleUser::Cast($this->System->GetModule('User'))->User;
170 $TranslationTree = $this->GetTranslationTree();
171
172 $Output = '<strong>'.T('Translate groups').':</strong><br /><div id="TranslationMenu">';
173 $DbResult = $this->System->Database->select('Group', '`Id`, `Name`', '1 ORDER BY `Name`');
174 while ($Group = $DbResult->fetch_assoc())
175 {
176 $Groups[T($Group['Name'])] = $Group;
177 }
178 ksort($Groups);
179 foreach ($Groups as $Group)
180 {
181 $Output .= '<div id="menuitem-group'.$Group['Id'].'" onmouseover="show(\'group'.$Group['Id'].'\')" onmouseout="hide(\'group'.$Group['Id'].'\')">'.
182 '<a href="'.$this->System->Link('/TranslationList.php?group='.$Group['Id'].
183 '&amp;action=filter').'">'.str_replace(' ','&nbsp;', T($Group['Name'])).'</a></div>'.
184 '<div id="group'.$Group['Id'].'" class="hidden-menu-item" onmouseover="show(\'group'.$Group['Id'].'\')" onmouseout="hide(\'group'.$Group['Id'].'\')">';
185 $Output .= '&nbsp;<a title="Zde můžete začít překládat" href="'.
186 $this->System->Link('/TranslationList.php?group='.$Group['Id'].'&amp;state=1&amp;user=0&amp;entry=&amp;text=').'">'.T('Untranslated').'</a><br />'.
187 '&nbsp;<a title="Přeložené texty, můžete zde hlasovat, nebo opravovat překlady" href="'.
188 $this->System->Link('/TranslationList.php?group='.$Group['Id'].'&amp;state=2&amp;user=0&amp;entry=&amp;text=').'">'.T('Translated').'</a><br />';
189 if (isset($User) and $User->Licence(LICENCE_USER))
190 {
191 $Output .= '&nbsp;<a title="'.T('Unfinished translations').'" href="'.$this->System->Link('/TranslationList.php?group='.$Group['Id'].'&amp;state=3').'">'.T('Unfinished').'</a><br />'.
192 '&nbsp;<a title="Všechny překlady, které jste přeložil" href="'.
193 $this->System->Link('/TranslationList.php?group='.$Group['Id'].'&amp;state=1&amp;user='.
194 $User->Id).'&amp;entry=&amp;text=">'.T('Own').'</a><br />';
195 }
196 $Output .= '&nbsp;<a title="'.T('Compose special filter').'" href="'.
197 $this->System->Link('/TranslationList.php?group='.$Group['Id'].'&amp;action=filter').
198 '">'.T('Filter').'</a><br />';
199 $Output .= '</div>';
200 }
201 $Output .= '</div>';
202 return $Output;
203 }
204
205 static function Cast(Module $Module): ModuleTranslation
206 {
207 if ($Module instanceof ModuleTranslation)
208 {
209 return $Module;
210 }
211 throw new Exception('Expected '.ModuleTranslation::GetClassName().' type but got '.gettype($Module));
212 }
213}
Note: See TracBrowser for help on using the repository browser.