XarayaTranslationMemory  1.6
WebBackend.php
Go to the documentation of this file.
1 <?php
2 
3 /***************************************************************************
4  * Copyright (C) 2005-2020 by Ferenc Veres *
5  * lion@netngine.hu *
6  * *
7  * This program is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19  ***************************************************************************/
20 include('TranslationBackend.php');
21 include_once('export/ExporterFactory.php');
22 
32 {
33  public $xStringStack;
34  public $xBrowserHelper;
41  function __construct()
42  {
43  $this->xActiveRootID = (empty($_POST["xActiveRootID"]) ? 0 : $_POST["xActiveRootID"]);
44  parent::__construct();
45  }
46 
51  public function EventHandler()
52  {
53  global $_POST;
54 
55  // Handle Events
56  $this->Event = (empty($_POST["__Event"]) ? "" : $_POST["__Event"] );
57 
58  if($this->Event != "")
59  {
60 
61  // Move web variables into local controls
62 
63  $this->xBrowserHelper = (empty($_POST["xBrowserHelper"]) ? "" : $_POST["xBrowserHelper"]);
64  $this->txtEditString = (empty($_POST["txtEditString_".$this->xBrowserHelper]) ? "" : $_POST["txtEditString_".$this->xBrowserHelper] );
65  $this->txtEditString = trim($this->txtEditString);
66 
67  $this->txtFindText = (empty($_POST["txtFindText"]) ? "" : $_POST["txtFindText"] );
68  $this->ddlFindPercent = (empty($_POST["ddlFindPercent"]) ? 60 : $_POST["ddlFindPercent"]);
69  $this->xStringID = (empty($_POST["xStringID"]) ? 0 : $_POST["xStringID"]);
70  $this->xProjectID = (empty($_POST["xProjectID"]) ? 0 : $_POST["xProjectID"]);
71  $this->ddlLocaleID = (empty($_POST["xOldLocaleID"]) ? "" : $_POST["xOldLocaleID"]);
72  $this->xOldLocaleID = (empty($_POST["xOldLocaleID"]) ? "" : $_POST["xOldLocaleID"]);
73  $this->ddlSearchType = (empty($_POST["ddlSearchType"]) ? 0 : $_POST["ddlSearchType"]);
74  $this->xStringStack = (empty($_POST["xStringStack"]) ? "" : $_POST["xStringStack"]);
75  $this->xDelStringID = (empty($_POST["xDelStringID"]) ? "" : $_POST["xDelStringID"]);
76  $this->xDelProjectID = (empty($_POST["xDelProjectID"]) ? "" : $_POST["xDelProjectID"]);
77 
78  // Store search results checkboxes if any checked (for "save to all checked" operation)
79  if(empty($_POST["chkSearchSelects"]))
80  {
81  unset($this->chkSearchSelects);
82  }
83  else
84  {
85  $this->chkSearchSelects = $_POST["chkSearchSelects"];
86  }
87 
88  // If no valid root yet, try with posted ddlLocaleID too.
89  if($this->xProjectID == 0)
90  {
91  $this->InitLanguage();
92  }
93 
94  //echo "EVENT:".$this->Event;
95 
96  switch($this->Event)
97  {
98  case "find":
99  $this->Event_FindCustomString();
100  break;
101 
102  case "editstring":
103  $this->Event_EditString();
104  break;
105 
106  case "deletestring":
107  $this->Event_DeleteString();
108  break;
109 
110  case "deleteproject":
111  $this->Event_DeleteProject();
112  break;
113 
114  case "savenextmissing":
115  case "savepopstack":
116  case "savenext":
117  case "nextmissing":
118  case "next":
119  case "save":
120  case "savetoallchecked":
121  $this->Event_SaveNavigate();
122  break;
123 
124  case "setproject":
125  $this->Event_SetProject();
126  break;
127 
128  case "setroot":
129  $this->Event_SetRoot();
130  break;
131 
132  case "setlocale":
133  $this->Event_SetLocale();
134  break;
135 
136  case "exportpack":
137  $this->Event_ExportPack();
138  break;
139 
140  }
141 
142 
143  // If anything to edit, set browser input field suffix (for browser form cache)
144  if(!empty($this->LocatedString))
145  {
146  $this->xBrowserHelper = md5($this->LocatedString->GetOriginal());
147  }
148 
149 
150  }
151  }
152 
157  private function Event_FindCustomString()
158  {
159  // Search the string
160  $this->FindStrings();
161 
162  if($this->xStringID != 0)
163  {
164  $this->SyncToString();
165  }
166  else
167  {
168  $this->SyncToProject();
169  }
170  }
171 
172 
177  private function Event_EditString()
178  {
179  // Load the selected string and related project paths
180  $this->SyncToString();
181 
182  // Find similar strings
183  if(!empty($this->LocatedString))
184  {
185  $this->ddlSearchType = 0;
186  $this->txtFindText = $this->LocatedString->GetOriginal();
187  $this->FindStrings();
188  }
189  }
190 
195  private function Event_DeleteString()
196  {
197  $this->DeleteString();
198 
199  if($this->xDelStringID == $this->xStringID)
200  {
201  // If it was the current string, go back to the first in this project.
202  $this->xStringID = 0;
203  $this->SyncToProject();
204  }
205  else
206  {
207  // Not current string, so sync back to that string
208  $this->SyncToString();
209  }
210 
211  // Search unchanged
212  $this->FindStrings();
213  }
214 
219  private function Event_DeleteProject()
220  {
221  // Deletes project and returns its parent.
222  $this->xProjectID = $this->DeleteProject($this->xDelProjectID);
223  $this->xStringID = 0;
224  $this->SyncToProject();
225 
226  // Search unchanged
227  $this->FindStrings();
228  }
229 
234  private function Event_SaveNavigate()
235  {
236  // Save the string??
237  if($this->Event == "savenext" ||
238  $this->Event == "savenextmissing" ||
239  $this->Event == "savepopstack" ||
240  $this->Event == "save" ||
241  $this->Event == "savetoallchecked")
242  {
243  $this->SaveString();
244  }
245 
246  // Save to all checked search result values?
247  if($this->Event == "savetoallchecked")
248  {
249  if(count($this->chkSearchSelects) > 0)
250  {
251  foreach($this->chkSearchSelects as $stringID)
252  {
253  // Load each string from the DB, change it and save changes
254  $string = new TranslationString($stringID);
255  $string->SetTranslation($this->txtEditString);
256  $string->Save();
257  }
258  }
259  }
260 
261  // How to find the next string?
262  $locType = LOCATE_CURR;
263 
264  if($this->Event == "savenext" || $this->Event == "next")
265  {
266  $locType = LOCATE_NEXT;
267  }
268  else if($this->Event == "savenextmissing" || $this->Event == "nextmissing")
269  {
270  $locType = LOCATE_NEXTMISSING;
271  }
272  else if($this->Event == "savepopstack")
273  {
274  // Also locate curr, but we take the last string from stack!
275  $locType = LOCATE_CURR;
276 
277  $stack = split(",", $this->xStringStack);
278  $this->xStringID = array_pop($stack);
279  $this->xStringStack = join($stack, ",");
280  }
281 
282  // Locate it on the selected way
283  $this->LocateString($locType);
284 
285  $this->SyncToString();
286 
287  // Find similar strings
288  if(!empty($this->LocatedString))
289  {
290  $this->ddlSearchType = 0;
291  $this->txtFindText = $this->LocatedString->GetOriginal();
292  $this->FindStrings();
293  }
294  }
295 
300  private function Event_SetProject()
301  {
302  $this->SyncToProject();
303 
304  // Find similar strings
305  if(!empty($this->LocatedString))
306  {
307  $this->ddlSearchType = 0;
308  $this->txtFindText = $this->LocatedString->GetOriginal();
309  $this->FindStrings();
310  }
311 
312  }
313 
317  private function Event_SetRoot()
318  {
319  $id = (empty($_POST["ddlActiveRoot"]) ? "0" : $_POST["ddlActiveRoot"]);
320 
321  // Try to load selected root project
322  $proj = new TranslationProject();
323  if($id && $proj->LoadByID($id, true) && $proj->GetParentID() == 0)
324  {
325  $this->xActiveRootID = $id;
326  $this->xProjectID = $id;
327  $this->SyncToProject();
328  }
329  else
330  {
331  throw new Exception("Invalid root project selected to set.");
332  }
333  }
334 
339  private function Event_SetLocale()
340  {
341  $this->xOldLocaleID = (empty($_POST["ddlLocaleID"]) ? "" : $_POST["ddlLocaleID"]);
342  $this->ddlLocaleID = $this->xOldLocaleID;
343  $this->InitLanguage();
344 
345  $this->SyncToProject();
346  }
347 
352  private function Event_ExportPack()
353  {
354  global $CONFIG;
355 
356  // Check if exporting is enabled, quit if not
357  if(!$CONFIG['enable_web_export'])
358  {
359  throw new Exception("Web GUI export function is disabled in configuration.");
360  }
361 
362  // Find selected locale's name
363  $localeName = "";
364  foreach(TranslationProject::$LocaleIDCache as $name => $id)
365  {
366  if($id == $this->ddlLocaleID)
367  {
368  $localeName = $name;
369  break;
370  }
371  }
372 
373  if($localeName == "")
374  {
375  throw new Exception("Cannot find locale name for current locale id: " + $this->ddlLocaleID);
376  }
377 
378  // Create exporter
379  $root = $this->RootProjects[$this->xActiveRootID];
380  $factory = new ExporterFactory();
381  $proc = $factory->NewExporter($root->GetName());
382 
383  // Export to the config.inc.php specified output location
384  $proc->Export($localeName, true);
385 
386  // Compress the dir with TAR
387  $languagePack = $localeName."_".date('Y-m-d').".tar.gz";
388  system("tar -C ".$CONFIG['export_path']." --exclude $localeName/php -czf ".$CONFIG['language_pack_dir']."/$languagePack $localeName", $ret);
389  if($ret != 0)
390  {
391  echo("<p>Tar returned $ret, the package may be incomplete.</p>");
392  }
393  $link = $CONFIG['language_pack_url']."/$languagePack";
394  echo("<p>Language pack saved to: <a href=\"$link\">$link</a>.</p>");
395 
396  // Sync controls
397  if($this->xStringID != 0)
398  {
399  $this->SyncToString();
400  }
401  else
402  {
403  $this->SyncToProject();
404  }
405  }
406 
410  private function SyncToProject()
411  {
412  $this->xStringID = 0;
413 
414  if($this->xProjectID == 0)
415  {
416  return;
417  }
418 
419  // Load the project's strings
420  $this->LoadProjectStrings();
421 
422  if($this->CurrentStrings->valid())
423  {
424  // If there are strings, take the first one and Load it
425  $this->xStringID = $this->CurrentStrings->current()->GetStringID();
426  $this->LocateString();
427 
428  // Find similar strings
429  $this->txtFindText = $this->LocatedString->GetOriginal();
430  $this->FindStrings();
431  }
432  else
433  {
434  // Forget last string ID if no strings for this project.
435  $this->xStringID = 0;
436  }
437 
438  // Load related project tree too
439  $this->FindProjectPath();
440  }
441 
446  private function SyncToString()
447  {
448  if($this->xStringID == 0)
449  {
450  return;
451  }
452 
453  $this->LocateString();
454 
455  if(!empty($this->LocatedString))
456  {
457  // If found the string, load related project info
458  $this->xProjectID = $this->LocatedString->GetProjectID();
459  $this->LoadProjectStrings();
460  }
461 
462  // Load related project tree too
463  $this->FindProjectPath();
464  }
465 
466 }
Event_SaveNavigate()
Definition: WebBackend.php:234
Event_DeleteString()
Definition: WebBackend.php:195
Event_DeleteProject()
Definition: WebBackend.php:219
if($argc !=3||empty(TranslationProject::$LocaleIDCache[$argv[1]])) $factory
Definition: export.php:61
Event_EditString()
Definition: WebBackend.php:177
const LOCATE_NEXT
Event_SetProject()
Definition: WebBackend.php:300
$proc
Definition: export.php:62
$CONFIG['db_software']
Definition: config.inc.php:24
const LOCATE_NEXTMISSING
const LOCATE_CURR
Event_FindCustomString()
Definition: WebBackend.php:157
Event_ExportPack()
Definition: WebBackend.php:352
LocateString($locate=LOCATE_CURR)