PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/Localization/NetteTranslator/Panel.php

https://bitbucket.org/tomas_vahalik/framework
PHP | 184 lines | 117 code | 26 blank | 41 comment | 16 complexity | f5e4c3c9dd705e7be49c35ddaf311746 MD5 | raw file
  1. <?php
  2. namespace NetteTranslator;
  3. use Nette;
  4. /**
  5. * Panel for Nette DebugBar, which enables you to translate strings
  6. * directly from your browser.
  7. *
  8. * @author Jan Smitka <jan@smitka.org>
  9. * @author Patrik Votoček <patrik@votocek.cz>
  10. * @author Vaclav Vrbka <gmvasek@php-info.cz>
  11. */
  12. class Panel extends Nette\Object implements Nette\Diagnostics\IBarPanel
  13. {
  14. const XHR_HEADER = 'X-Translation-Client';
  15. const SESSION_NAMESPACE = 'NetteTranslator-Panel';
  16. const LANGUAGE_KEY = 'X-NetteTranslator-Lang';
  17. const FILE_KEY = 'X-NetteTranslator-File';
  18. /* Layout constants */
  19. const LAYOUT_HORIZONTAL = 1;
  20. const LAYOUT_VERTICAL = 2;
  21. /** @var int TranslationPanel layout */
  22. protected $layout = self::LAYOUT_VERTICAL;
  23. /** @var int Height of the editor */
  24. protected $height = 410;
  25. /** @var Nette\DI\Container */
  26. protected $container;
  27. /** @var IEditable */
  28. protected $translator;
  29. public function __construct(Nette\DI\Container $container, IEditable $translator, $layout = NULL, $height = NULL)
  30. {
  31. $this->container = $container;
  32. $this->translator = $translator;
  33. if ($height !== NULL) {
  34. if (!is_numeric($height))
  35. throw new \InvalidArgumentException('Panel height has to be a numeric value.');
  36. $this->height = $height;
  37. }
  38. if ($layout !== NULL) {
  39. $this->layout = $layout;
  40. if ($height === NULL)
  41. $this->height = 500;
  42. }
  43. $this->processRequest();
  44. }
  45. /**
  46. * Return's panel ID.
  47. * @return string
  48. */
  49. public function getId()
  50. {
  51. return 'translation-panel';
  52. }
  53. /**
  54. * Returns the code for the panel tab.
  55. * @return string
  56. */
  57. public function getTab()
  58. {
  59. ob_start();
  60. require __DIR__ . '/tab.phtml';
  61. return ob_get_clean();
  62. }
  63. /**
  64. * Returns the code for the panel itself.
  65. * @return string
  66. */
  67. public function getPanel()
  68. {
  69. $translator = $this->translator;
  70. $files = array_keys($translator->getFiles());
  71. $strings = $translator->getStrings();
  72. $requests = $this->container->application->requests;
  73. $count = count($requests);
  74. $presenterName = ($count > 0) ? $requests[count($requests) - 1]->presenterName : NULL;
  75. $module = (!$presenterName) ? : strtolower(str_replace(':', '.', ltrim(substr($presenterName, 0, -(strlen(strrchr($presenterName, ':')))), ':')));
  76. $activeFile = (in_array($module, $files)) ? $module : $files[0];
  77. if ($this->container->session->isStarted()) {
  78. $session = $this->container->session->getSection(static::SESSION_NAMESPACE);
  79. $untranslatedStack = isset($session['stack']) ? $session['stack'] : array();
  80. foreach ($strings as $string => $data) {
  81. if (!$data) {
  82. $untranslatedStack[$string] = FALSE;
  83. }
  84. }
  85. $session['stack'] = $untranslatedStack;
  86. foreach ($untranslatedStack as $string => $value) {
  87. if (!isset($strings[$string]))
  88. $strings[$string] = FALSE;
  89. }
  90. }
  91. ob_start();
  92. require __DIR__ . '/panel.phtml';
  93. return ob_get_clean();
  94. }
  95. /**
  96. * Handles an incomuing request and saves the data if necessary.
  97. */
  98. private function processRequest()
  99. {
  100. // Try starting the session
  101. try {
  102. $session = $this->container->session->getSection(self::SESSION_NAMESPACE);
  103. } catch (Nette\InvalidStateException $e) {
  104. $session = FALSE;
  105. }
  106. $request = $this->container->httpRequest;
  107. if ($request->isPost() && $request->isAjax() && $request->getHeader(self::XHR_HEADER)) {
  108. $data = json_decode(file_get_contents('php://input'));
  109. $translator = $this->translator;
  110. if ($data) {
  111. if ($session) {
  112. $stack = isset($session['stack']) ? $session['stack'] : array();
  113. }
  114. $translator->lang = $data->{self::LANGUAGE_KEY};
  115. $file = $data->{self::FILE_KEY};
  116. unset($data->{self::LANGUAGE_KEY}, $data->{self::FILE_KEY});
  117. foreach ($data as $string => $value) {
  118. $translator->setTranslation($string, $value, $file);
  119. if ($session && isset($stack[$string]))
  120. unset($stack[$string]);
  121. }
  122. $translator->save($file);
  123. if ($session)
  124. $session['stack'] = $stack;
  125. }
  126. exit;
  127. }
  128. }
  129. /**
  130. * Return an odrdinal number suffix.
  131. * @param string $count
  132. * @return string
  133. */
  134. protected function ordinalSuffix($count)
  135. {
  136. switch (substr($count, -1)) {
  137. case '1':
  138. return 'st';
  139. case '2':
  140. return 'nd';
  141. case '3':
  142. return 'rd';
  143. default:
  144. return 'th';
  145. }
  146. }
  147. /**
  148. * Register this panel
  149. *
  150. * @param NetteTranslator\IEditable $translator
  151. * @param int $layout
  152. * @param int $height
  153. */
  154. public static function register(Nette\DI\Container $container, IEditable $translator, $layout = NULL, $height = NULL)
  155. {
  156. Nette\Diagnostics\Debugger::$bar->addPanel(new static($container, $translator, $layout, $height));
  157. }
  158. }