-
Notifications
You must be signed in to change notification settings - Fork 39
/
Panel.php
184 lines (158 loc) · 4.51 KB
/
Panel.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
<?php
namespace NetteTranslator;
use Nette;
/**
* Panel for Nette DebugBar, which enables you to translate strings
* directly from your browser.
*
* @author Jan Smitka <[email protected]>
* @author Patrik Votoček <[email protected]>
* @author Vaclav Vrbka <[email protected]>
*/
class Panel extends Nette\Object implements Nette\Diagnostics\IBarPanel
{
const XHR_HEADER = 'X-Translation-Client';
const SESSION_NAMESPACE = 'NetteTranslator-Panel';
const LANGUAGE_KEY = 'X-NetteTranslator-Lang';
const FILE_KEY = 'X-NetteTranslator-File';
/* Layout constants */
const LAYOUT_HORIZONTAL = 1;
const LAYOUT_VERTICAL = 2;
/** @var int TranslationPanel layout */
protected $layout = self::LAYOUT_VERTICAL;
/** @var int Height of the editor */
protected $height = 410;
/** @var Nette\DI\IContainer */
protected $container;
/** @var IEditable */
protected $translator;
public function __construct(Nette\DI\IContainer $container, IEditable $translator, $layout = NULL, $height = NULL)
{
$this->container = $container;
$this->translator = $translator;
if ($height !== NULL) {
if (!is_numeric($height))
throw new \InvalidArgumentException('Panel height has to be a numeric value.');
$this->height = $height;
}
if ($layout !== NULL) {
$this->layout = $layout;
if ($height === NULL)
$this->height = 500;
}
$this->processRequest();
}
/**
* Return's panel ID.
* @return string
*/
public function getId()
{
return 'translation-panel';
}
/**
* Returns the code for the panel tab.
* @return string
*/
public function getTab()
{
ob_start();
require __DIR__ . '/tab.phtml';
return ob_get_clean();
}
/**
* Returns the code for the panel itself.
* @return string
*/
public function getPanel()
{
$translator = $this->translator;
$files = array_keys($translator->getFiles());
$strings = $translator->getStrings();
$requests = $this->container->application->requests;
$count = count($requests);
$presenterName = ($count > 0) ? $requests[count($requests) - 1]->presenterName : NULL;
$module = (!$presenterName) ? : strtolower(str_replace(':', '.', ltrim(substr($presenterName, 0, -(strlen(strrchr($presenterName, ':')))), ':')));
$activeFile = (in_array($module, $files)) ? $module : $files[0];
if ($this->container->session->isStarted()) {
$session = $this->container->session->getSection(static::SESSION_NAMESPACE);
$untranslatedStack = isset($session['stack']) ? $session['stack'] : array();
foreach ($strings as $string => $data) {
if (!$data) {
$untranslatedStack[$string] = FALSE;
}
}
$session['stack'] = $untranslatedStack;
foreach ($untranslatedStack as $string => $value) {
if (!isset($strings[$string]))
$strings[$string] = FALSE;
}
}
ob_start();
require __DIR__ . '/panel.phtml';
return ob_get_clean();
}
/**
* Handles an incomuing request and saves the data if necessary.
*/
private function processRequest()
{
// Try starting the session
try {
$session = $this->container->session->getSection(self::SESSION_NAMESPACE);
} catch (Nette\InvalidStateException $e) {
$session = FALSE;
}
$request = $this->container->httpRequest;
if ($request->isPost() && $request->isAjax() && $request->getHeader(self::XHR_HEADER)) {
$data = json_decode(file_get_contents('php://input'));
$translator = $this->translator;
if ($data) {
if ($session) {
$stack = isset($session['stack']) ? $session['stack'] : array();
}
$translator->lang = $data->{self::LANGUAGE_KEY};
$file = $data->{self::FILE_KEY};
unset($data->{self::LANGUAGE_KEY}, $data->{self::FILE_KEY});
foreach ($data as $string => $value) {
$translator->setTranslation($string, $value, $file);
if ($session && isset($stack[$string]))
unset($stack[$string]);
}
$translator->save($file);
if ($session)
$session['stack'] = $stack;
}
exit;
}
}
/**
* Return an odrdinal number suffix.
* @param string $count
* @return string
*/
protected function ordinalSuffix($count)
{
switch (substr($count, -1)) {
case '1':
return 'st';
case '2':
return 'nd';
case '3':
return 'rd';
default:
return 'th';
}
}
/**
* Register this panel
*
* @param NetteTranslator\IEditable $translator
* @param int $layout
* @param int $height
*/
public static function register(Nette\DI\IContainer $container, IEditable $translator, $layout = NULL, $height = NULL)
{
Nette\Diagnostics\Debugger::$bar->addPanel(new static($container, $translator, $layout, $height));
}
}