]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Controllers/DynamicTranslationsController.php
c5fafce7d72ad13d99a0ff0b00a70014de9c5232
[galette.git] / galette / lib / Galette / Controllers / DynamicTranslationsController.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Galette dynamic translations controller
7 *
8 * PHP version 5
9 *
10 * Copyright © 2020 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
26 *
27 * @category Controllers
28 * @package Galette
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2020 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
33 * @version SVN: $Id$
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.9.4dev - 2020-05-02
36 */
37
38 namespace Galette\Controllers;
39
40 use Slim\Http\Request;
41 use Slim\Http\Response;
42 use Galette\Core\L10n;
43 use Analog\Analog;
44
45 /**
46 * Galette dynamic translations controller
47 *
48 * @category Controllers
49 * @name DynamicTranslationsController
50 * @package Galette
51 * @author Johan Cwiklinski <johan@x-tnd.be>
52 * @copyright 2020 The Galette Team
53 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
54 * @link http://galette.tuxfamily.org
55 * @since Available since 0.9.4dev - 2020-05-02
56 */
57
58 class DynamicTranslationsController extends AbstractController
59 {
60 /**
61 * Dynamic fields translations
62 *
63 * @param Request $request PSR Request
64 * @param Response $response PSR Response
65 * @param array $args Request arguments
66 *
67 * @return Response
68 */
69 public function dynamicTranslations(Request $request, Response $response, array $args = []) :Response
70 {
71 $text_orig = '';
72 if (isset($args['text_orig'])) {
73 $text_orig = $args['text_orig'];
74 } elseif (isset($_GET['text_orig'])) {
75 $text_orig = $_GET['text_orig'];
76 }
77
78 $params = [
79 'page_title' => _T("Translate labels")
80 ];
81
82 $nb_fields = 0;
83 try {
84 $select = $this->zdb->select(L10n::TABLE);
85 $select->columns(
86 array('nb' => new \Laminas\Db\Sql\Expression('COUNT(text_orig)'))
87 );
88 $results = $this->zdb->execute($select);
89 $result = $results->current();
90 $nb_fields = $result->nb;
91 } catch (Exception $e) {
92 Analog::log(
93 'An error occurred counting l10n entries | ' .
94 $e->getMessage(),
95 Analog::WARNING
96 );
97 }
98
99 if (is_numeric($nb_fields) && $nb_fields > 0) {
100 try {
101 $select = $this->zdb->select(L10n::TABLE);
102 $select->quantifier('DISTINCT')->columns(
103 array('text_orig')
104 )->order('text_orig');
105
106 $all_texts = $this->zdb->execute($select);
107
108 $orig = array();
109 foreach ($all_texts as $idx => $row) {
110 $orig[] = $row->text_orig;
111 }
112 $exists = true;
113 if ($text_orig == '') {
114 $text_orig = $orig[0];
115 } elseif (!in_array($text_orig, $orig)) {
116 $exists = false;
117 $this->flash->addMessage(
118 'error_detected',
119 str_replace(
120 '%s',
121 $text_orig,
122 _T("No translation for '%s'!<br/>Please fill and submit above form to create it.")
123 )
124 );
125 }
126
127 $trans = array();
128 /**
129 * FIXME : it would be faster to get all translations at once
130 * for a specific string
131 */
132 foreach ($this->i18n->getList() as $l) {
133 $text_trans = \getDynamicTranslation($text_orig, $l->getLongID());
134 $lang_name = $l->getName();
135 $trans[] = array(
136 'key' => $l->getLongID(),
137 'name' => ucwords($lang_name),
138 'text' => $text_trans
139 );
140 }
141
142 $params['exists'] = $exists;
143 $params['orig'] = $orig;
144 $params['trans'] = $trans;
145 } catch (\Exception $e) {
146 Analog::log(
147 'An error occurred retrieving l10n entries | ' .
148 $e->getMessage(),
149 Analog::WARNING
150 );
151 }
152 }
153
154 $params['text_orig'] = $text_orig;
155
156 // display page
157 $this->view->render(
158 $response,
159 'traduire_libelles.tpl',
160 $params
161 );
162 return $response;
163 }
164
165 /**
166 * Do dynamic fields translations
167 *
168 * @param Request $request PSR Request
169 * @param Response $response PSR Response
170 *
171 * @return Response
172 */
173 public function doDynamicTranslations(Request $request, Response $response) :Response
174 {
175 $post = $request->getParsedBody();
176 $error_detected = false;
177
178 if (isset($post['trans']) && isset($post['text_orig'])) {
179 if (isset($_POST['new']) && $_POST['new'] == 'true') {
180 //create translation if it does not exists yet
181 $res = \addDynamicTranslation(
182 $post['text_orig']
183 );
184 }
185
186 // Validate form
187 foreach ($post as $key => $value) {
188 if (substr($key, 0, 11) == 'text_trans_') {
189 $trans_lang = substr($key, 11);
190 $trans_lang = str_replace('_utf8', '.utf8', $trans_lang);
191 $res = \updateDynamicTranslation(
192 $post['text_orig'],
193 $trans_lang,
194 $value
195 );
196 if ($res !== true) {
197 $error_detected = true;
198 $this->flash->addMessage(
199 'error_detected',
200 preg_replace(
201 array(
202 '/%label/',
203 '/%lang/'
204 ),
205 array(
206 $post['text_orig'],
207 $trans_lang
208 ),
209 _T("An error occurred saving label `%label` for language `%lang`")
210 )
211 );
212 }
213 }
214 }
215
216 if ($error_detected === false) {
217 $this->flash->addMessage(
218 'success_detected',
219 _T("Labels has been sucessfully translated!")
220 );
221 }
222 }
223
224 return $response
225 ->withStatus(301)
226 ->withHeader('Location', $this->router->pathFor(
227 'dynamicTranslations',
228 ['text_orig' => $post['text_orig']]
229 ));
230 }
231 }