source: trunk/Modules/Translation/Save.php

Last change on this file was 900, checked in by chronos, 8 months ago
  • Fixed: More form input fields validation.
  • Modified: Code cleanup.
File size: 13.7 KB
Line 
1<?php
2
3class PageTranslationSave extends Page
4{
5 function Translate($Group, $TextID, $Complete, $Language)
6 {
7 $User = ModuleUser::Cast($this->System->GetModule('User'))->User;
8 $Output = '';
9 $Table = $Group['TablePrefix'];
10 $CompleteText = array(T('unfinished'), T('finished'));
11
12 // Get source text record from database by ID
13 $DbResult = $this->Database->query('SELECT * FROM `'.$Table.'` WHERE `ID`='.$TextID);
14 if ($DbResult->num_rows > 0)
15 {
16 $SourceText = $DbResult->fetch_assoc();
17
18 // Get data for english original
19 $DbResult = $this->Database->query('SELECT * FROM `'.$Table.'` WHERE (`Entry`='.$SourceText['Entry'].') '.
20 'AND (`Language` = '.Core::Cast($this->System)->Config['OriginalLanguage'].') AND (`VersionStart` = '.$SourceText['VersionStart'].') '.
21 'AND (`VersionEnd` = '.$SourceText['VersionEnd'].')');
22 if ($DbResult->num_rows > 0)
23 {
24 $EnglishText = $DbResult->fetch_assoc();
25
26 // Get all similar english texts
27 $Filter = array();
28 foreach ($Group['Items'] as $GroupItem)
29 {
30 if (($GroupItem['Visible'] == 1) and ($EnglishText[$GroupItem['Column']] != ''))
31 $Filter[] = '(`'.$GroupItem['Column'].'` = "'.addslashes($EnglishText[$GroupItem['Column']]).'")';
32 }
33 if (count($Filter) > 0) $Filter = ' AND ('.implode(' OR ', $Filter).')';
34 else $Filter = ' AND 0';
35
36 $Query = 'SELECT * FROM `'.$Table.'` WHERE (`Language` = '.Core::Cast($this->System)->Config['OriginalLanguage'].')'.$Filter;
37 $DbResult = $this->Database->query($Query);
38 while ($EnglishFound = $DbResult->fetch_assoc())
39 {
40 // Get user translation paired to found english item entry
41 $DbResult2 = $this->Database->query('SELECT * FROM `'.$Table.'` WHERE (`User` = '.$User->Id.
42 ') AND (`Entry` = '.$EnglishFound['Entry'].') AND (`VersionStart` = '.$EnglishFound['VersionStart'].
43 ') AND (`VersionEnd` = '.$EnglishFound['VersionEnd'].')');
44 if ($DbResult2->num_rows > 0)
45 {
46 // Update existed user translation
47 $ExistedText = $DbResult2->fetch_assoc();
48 $Modified = false;
49 $Values = '`Language` = '.$Language;
50 if ($Language != $ExistedText['Language']) $Modified = true;
51
52 $Completable = true;
53 $CompleteParts = $ExistedText['CompleteParts'];
54 foreach ($Group['Items'] as $GroupItem)
55 {
56 if ($GroupItem['Visible'] == 1)
57 {
58 if ($EnglishFound[$GroupItem['Column']] == $EnglishText[$GroupItem['Column']])
59 {
60 if (array_key_exists($GroupItem['Column'], $_POST))
61 {
62 if ($_POST[$GroupItem['Column']] != $ExistedText[$GroupItem['Column']])
63 $Modified = true;
64
65 $Values .= ', `'.$GroupItem['Column'].'` = "'.$_POST[$GroupItem['Column']].'"';
66 $CompleteParts |= (1 << ($GroupItem['Sequence'] - 1));
67 }
68 }
69 if ((($CompleteParts & (1 << ($GroupItem['Sequence'] - 1))) == 0) and
70 ($EnglishFound[$GroupItem['Column']] != '')) $Completable = false;
71 }
72 }
73 if ($Completable) $NewComplete = 1; // All parts of text are completed. Make entire text as completed
74 else $NewComplete = 0;
75
76 // Update completion status for edited translation item
77 if ($SourceText['ID'] == $ExistedText['ID'])
78 {
79 $NewComplete = $Complete; // Our original user text, set complete according user choice
80 if ($Complete == 1)
81 {
82 foreach ($Group['Items'] as $GroupItem)
83 if ($GroupItem['Visible'] == 1) $CompleteParts |= (1 << ($GroupItem['Sequence'] - 1));
84 }
85 }
86 $Values .= ', `Complete`='.$NewComplete.', `CompleteParts` = '.$CompleteParts;
87 if (($NewComplete != $ExistedText['Complete']) or ($CompleteParts != $ExistedText['CompleteParts']))
88 $Modified = true;
89
90 $Values .= ', `ModifyTime` = NOW()';
91 if ($Modified)
92 {
93 // Update user translation
94 $this->Database->query('UPDATE `'.$Table.'` SET '.$Values.' WHERE `ID` = '.$ExistedText['ID']);
95
96 $Output .= sprintf(T('Modifications in translation %s stored as %s'),
97 '<a href="'.$this->System->Link('/form.php?group='.
98 $Group['Id'].'&amp;ID='.$ExistedText['ID']).'">'.$ExistedText['ID'].'</a> ('.
99 $ExistedText['Entry'].')', $CompleteText[$NewComplete]).'<br/>';
100 $this->System->ModuleManager->Modules['Log']->WriteLog(
101 sprintf(T('Modifications in translation %s stored as %s'),
102 '<a href="'.$this->System->Link('/form.php?group='.
103 $Group['Id'].'&amp;ID='.$ExistedText['ID']).'">'.$ExistedText['ID'].'</a> ('.
104 $ExistedText['Entry'].')', $CompleteText[$NewComplete]),
105 LOG_TYPE_TRANSLATION);
106 }
107 } else
108 {
109 // Insert new user translation
110 $TakeID = $EnglishFound['ID'];
111 $Columns = '`Entry`, `VersionStart`, `VersionEnd`, `Language`, `User`, `Take`, `ModifyTime`';
112 $Values = $EnglishFound['Entry'].', '.$EnglishFound['VersionStart'].', '.
113 $EnglishFound['VersionEnd'].', '.$Language.', '.$User->Id.', '.$TakeID.', NOW()';
114
115 $CompleteParts = 0;
116 $Completable = true;
117 foreach ($Group['Items'] as $GroupItem)
118 {
119 $Columns .= ', `'.$GroupItem['Column'].'`';
120 if ($GroupItem['Visible'] == 1)
121 {
122 if ($EnglishFound[$GroupItem['Column']] == $EnglishText[$GroupItem['Column']])
123 {
124 // Read form user data
125 if (array_key_exists($GroupItem['Column'], $_POST))
126 {
127 $Values .= ', "'.$_POST[$GroupItem['Column']].'"';
128 $CompleteParts |= (1 << ($GroupItem['Sequence'] - 1));
129 } else $Values .= ', "'.addslashes($EnglishFound[$GroupItem['Column']]).'"';
130 } else $Values .= ', "'.addslashes($EnglishFound[$GroupItem['Column']]).'"';
131 if ((($CompleteParts & (1 << ($GroupItem['Sequence'] - 1))) == 0) and ($EnglishFound[$GroupItem['Column']] != '')) $Completable = false;
132 } else
133 {
134 // Read from english text
135 $Values .= ', "'.$EnglishFound[$GroupItem['Column']].'"';
136 }
137 }
138 if ($Completable) $NewComplete = 1; // All parts of text are completed. Make entire text as completed
139 else $NewComplete = 0;
140 // Update completion status for edited translation item
141 if ($SourceText['ID'] == $EnglishFound['ID'])
142 {
143 $TakeID = $TextID;
144 $NewComplete = $Complete; // Our original user text, set complete according user choice
145 if ($Complete == 1)
146 {
147 foreach ($Group['Items'] as $GroupItem)
148 if ($GroupItem['Visible'] == 1) $CompleteParts |= (1 << ($GroupItem['Sequence'] - 1));
149 }
150 }
151 $Columns .= ', `Complete`, `CompleteParts`';
152 $Values .= ', '.$NewComplete.', '.$CompleteParts;
153 $this->Database->query('INSERT INTO `'.$Table.'` ('.$Columns.') VALUES ('.$Values.')');
154 $LastID = $this->Database->insert_id;
155
156 $Output .= sprintf(T('Text %s from group %s saved as %s. Handed over from %s.'),
157 '<a href="'.$this->System->Link('/form.php?group='.
158 $Group['Id'].'&amp;ID='.$LastID).'">'.$LastID.'</a>',
159 '<a href="'.$this->System->Link('/TranslationList.php?group='.
160 $Group['Id'].'&amp;user=0&amp;action=filter').'">'.$Group['Name'].'</a>',
161 $CompleteText[$NewComplete], '<a href="'.$this->System->Link('/form.php?group='.$Group['Id'].'&amp;ID='.$TakeID).'">'.$TakeID.'</a>').
162 '<br/>';
163 $this->System->ModuleManager->Modules['Log']->WriteLog(sprintf(T('Text %s from group %s saved as %s. Handed over from %s.'),
164 '<a href="'.$this->System->Link('/form.php?group='.
165 $Group['Id'].'&amp;ID='.$LastID).'">'.$LastID.'</a>',
166 '<a href="'.$this->System->Link('/TranslationList.php?group='.
167 $Group['Id'].'&amp;user=0&amp;action=filter').'">'.$Group['Name'].'</a>',
168 $CompleteText[$NewComplete], '<a href="'.$this->System->Link('/form.php?group='.$Group['Id'].'&amp;ID='.$TakeID).'">'.$TakeID.'</a>'),
169 LOG_TYPE_TRANSLATION);
170 }
171 }
172 } else $Output .= ShowMessage('Anglický originál k překladu nenalezen.', MESSAGE_CRITICAL);
173 } else $Output .= ShowMessage('Zadaná položka nenalezena.', MESSAGE_CRITICAL);
174 return $Output;
175 }
176
177 function Show(): string
178 {
179 global $Message, $MessageType;
180
181 $User = ModuleUser::Cast($this->System->GetModule('User'))->User;
182 $Output = '';
183 $TranslationTree = $this->System->ModuleManager->Modules['Translation']->GetTranslationTree();
184
185 unset($Message);
186 $this->System->ModuleManager->Modules['FrontPage']->HandleLoginForm();
187 if (isset($Message)) $Output .= ShowMessage($Message, $MessageType);
188
189 $GroupId = LoadGroupIdParameter();
190 $Group = $TranslationTree[$GroupId];
191 $Table = $Group['TablePrefix'];
192 if ($User->Licence(LICENCE_USER))
193 {
194 if (array_key_exists('ID', $_POST) and is_numeric($_POST['ID']))
195 {
196 $Entry = $_POST['entry'] * 1;
197 $TextID = $_POST['ID'] * 1;
198 $Language = $_POST['Language'] * 1;
199 if (array_key_exists('End', $_POST)) $Complete = 1;
200 else $Complete = 0;
201
202 $Output .= $this->Translate($Group, $TextID, $Complete, $Language);
203
204 $Output .= $this->ShowRedirection($GroupId, $Table, $TextID);
205
206 UserLevelUpdate($User->Id);
207 } else $Output .= ShowMessage('Položka nenalezena', MESSAGE_CRITICAL);
208 } else
209 {
210 // User automatically logged out. Show login dialog and allow to save retry.
211 if (array_key_exists('ID', $_POST) and array_key_exists('entry', $_POST) and array_key_exists('Language', $_POST) and array_key_exists('user', $_POST))
212 {
213 $Output .= 'Byli jste automaticky odhlášeni. Pro <strong>Uložení překladu</strong> se musíte přihlásit zde:<br /><br />'.
214 '<form action="save.php?action=login&amp;group='.$GroupId.'" method="post"><div>'.
215 '<input type="hidden" name="entry" value="'.$_POST['entry'].'" />'.
216 '<input type="hidden" name="user" value="'.$_POST['user'].'" />'.
217 '<input type="hidden" name="ID" value="'.$_POST['ID'].'" />'.
218 '<input type="hidden" name="Language" value="'.$_POST['Language'].'" />';
219
220 foreach ($TranslationTree[$GroupId]['Items'] as $Index => $TextItem)
221 {
222 if (array_key_exists($TextItem['Column'], $_POST)) $Value = $_POST[$TextItem['Column']]; else $Value = '';
223 $Output .= '<input id="'.$TextItem['Column'].'" name="'.$TextItem['Column'].'" type="hidden" value="'.htmlspecialchars($Value).'" />';
224 }
225 $Output .= '<table>'.
226 '<tr>'.
227 '<td>'.T('Name').': <input type="text" name="LoginUser" size="13" /></td>'.
228 '</tr><tr>'.
229 '<td>'.T('Password').': <input type="password" name="LoginPass" size="13" /></td>'.
230 '</tr><tr>'.
231 '<th><input type="submit" value="'.T('Login and save translation').'" /></th>'.
232 '</tr>'.
233 '</table>';
234 } else $Output = ShowMessage('Nezadány požadované údaje.', MESSAGE_CRITICAL);
235 }
236 return $Output;
237 }
238
239 function ShowRedirection($GroupId, $Table, $TextID)
240 {
241 $User = ModuleUser::Cast($this->System->GetModule('User'))->User;
242 // Address and redirecting
243 $Output = '<br />'.T('Translate').': <a href="'.$this->System->Link('/TranslationList.php?group='.$GroupId.'&amp;state=1&amp;user=0&entry=').'">'.T('Not translated').'</a> ';
244
245 $prev = FollowingTran($TextID, $Table, $GroupId, True);
246 $next = FollowingTran($TextID, $Table, $GroupId);
247 $Output .= '<br /><br />';
248 $DbResult = $this->Database->query('SELECT `Redirecting` FROM `User` WHERE `ID`='.$User->Id);
249 $redirecting = $DbResult->fetch_assoc();
250
251 switch ($redirecting['Redirecting'])
252 {
253 case 1:
254 $Output .= '<script type="text/javascript" language="JavaScript" charset="utf-8">'.
255 'setTimeout("parent.location.href=\''.htmlspecialchars_decode('TranslationList.php?group='.$GroupId.'&amp;state=1&amp;user=0').'\'", 1500)'.
256 '</script>';
257 break;
258 case 2:
259 if ($next <> '')
260 $Output .= '<script type="text/javascript" language="JavaScript" charset="utf-8">'.
261 'setTimeout("parent.location.href=\''.htmlspecialchars_decode($next).'\'", 1500)'.
262 '</script>';
263 break;
264 case 3:
265 if ($prev <> '')
266 $Output .= '<script type="text/javascript" language="JavaScript" charset="utf-8">'.
267 'setTimeout("parent.location.href=\''.htmlspecialchars_decode($prev).'\'", 1500)'.
268 '</script>';
269 break;
270 }
271
272 $Output .= sprintf(T('You can be redirected automatically from this page. You can set where you want to be transfered here: %s'), '<a href="'.
273 $this->System->Link('/options/').'" title="'.T('User settings').'">'.T('Options').'</a>');
274 return $Output;
275 }
276}
Note: See TracBrowser for help on using the repository browser.