]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Entity/I18nTrait.php
230d2f81fe1e3fcee5474187d11e16fe5ef35e54
[galette.git] / galette / lib / Galette / Entity / I18nTrait.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * I18n
7 *
8 * PHP version 5
9 *
10 * Copyright © 2018 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 Entity
28 * @package Galette
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2018 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.1dev - 2018-03-10
36 */
37
38 namespace Galette\Entity;
39
40 use Analog\Analog;
41 use Galette\Core\L10n;
42 use Laminas\Db\Sql\Expression;
43
44 /**
45 * Files
46 *
47 * @category Entity
48 * @name I18nTrait
49 * @package Galette
50 * @author Johan Cwiklinski <johan@x-tnd.be>
51 * @copyright 2018 The Galette Team
52 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
53 * @link http://galette.tuxfamily.org
54 * @since Available since 0.9.1dev - 2018-03-10
55 */
56
57 trait I18nTrait
58 {
59 protected $warnings = [];
60
61 /**
62 * Add a translation stored in the database
63 *
64 * @param string $text_orig Text to translate
65 *
66 * @return boolean
67 */
68 protected function addTranslation($text_orig)
69 {
70 global $i18n;
71
72 try {
73 foreach ($i18n->getList() as $lang) {
74 //check if translation already exists
75 $select = $this->zdb->select(L10n::TABLE);
76 $select->columns(array('text_nref'))
77 ->where(
78 array(
79 'text_orig' => $text_orig,
80 'text_locale' => $lang->getLongID()
81 )
82 );
83
84 $results = $this->zdb->execute($select);
85 $result = $results->current();
86 $nref = 0;
87 if ($result) {
88 $nref = $result->text_nref;
89 }
90
91 if (is_numeric($nref) && $nref > 0) {
92 //already existing, update
93 $values = array(
94 'text_nref' => new Expression('text_nref+1')
95 );
96 Analog::log(
97 'Entry for `' . $text_orig .
98 '` dynamic translation already exists.',
99 Analog::INFO
100 );
101
102 $owhere = $select->where;
103
104 $update = $this->zdb->update(L10n::TABLE);
105 $update->set($values)->where($owhere);
106 $this->zdb->execute($update);
107 } else {
108 //add new entry
109 // User is supposed to use current language as original text.
110 $text_trans = $text_orig;
111 if ($lang->getLongID() != $i18n->getLongID()) {
112 $text_trans = '';
113 }
114 $values = array(
115 'text_orig' => $text_orig,
116 'text_locale' => $lang->getLongID(),
117 'text_trans' => $text_trans
118 );
119
120 $insert = $this->zdb->insert(L10n::TABLE);
121 $insert->values($values);
122 $this->zdb->execute($insert);
123 }
124 }
125 return true;
126 } catch (\Exception $e) {
127 Analog::log(
128 'An error occurred adding dynamic translation for `' .
129 $text_orig . '` | ' . $e->getMessage(),
130 Analog::ERROR
131 );
132
133 $this->warnings[] = str_replace(
134 '%field',
135 $text_orig,
136 _T('Unable to add dynamic translation for %field :(')
137 );
138
139 return false;
140 }
141 }
142
143 /**
144 * Update a translation stored in the database
145 *
146 * @param string $text_orig Text to translate
147 * @param string $text_locale The locale
148 * @param string $text_trans Translated text
149 *
150 * @return boolean
151 */
152 protected function updateTranslation($text_orig, $text_locale, $text_trans)
153 {
154 try {
155 //check if translation already exists
156 $select = $this->zdb->select(L10n::TABLE);
157 $select->columns(array('text_nref'))->where(
158 array(
159 'text_orig' => $text_orig,
160 'text_locale' => $text_locale
161 )
162 );
163
164 $results = $this->zdb->execute($select);
165 $result = $results->current();
166
167 $exists = false;
168 if ($result) {
169 $nref = $result->text_nref;
170 $exists = (is_numeric($nref) && $nref > 0);
171 }
172
173 $values = array(
174 'text_trans' => $text_trans
175 );
176
177 if ($exists) {
178 $owhere = $select->where;
179
180 $update = $this->zdb->update(L10n::TABLE);
181 $update->set($values)->where($owhere);
182 $this->zdb->execute($update);
183 } else {
184 $values['text_orig'] = $text_orig;
185 $values['text_locale'] = $text_locale;
186
187 $insert = $this->zdb->insert(L10n::TABLE);
188 $insert->values($values);
189 $this->zdb->execute($insert);
190 }
191 return true;
192 } catch (\Exception $e) {
193 Analog::log(
194 'An error occurred updating dynamic translation for `' .
195 $text_orig . '` | ' . $e->getMessage(),
196 Analog::ERROR
197 );
198
199 $this->warnings[] = str_replace(
200 '%field',
201 $text_orig,
202 _T('Unable to update dynamic translation for %field :(')
203 );
204
205 return false;
206 }
207 }
208
209 /**
210 * Delete a translation stored in the database
211 *
212 * @param string $text_orig Text to translate
213 *
214 * @return boolean
215 */
216 protected function deleteTranslation($text_orig)
217 {
218 try {
219 $delete = $this->zdb->delete(L10n::TABLE);
220 $delete->where(
221 array(
222 'text_orig' => $text_orig
223 )
224 );
225 $this->zdb->execute($delete);
226 return true;
227 } catch (\Exception $e) {
228 Analog::log(
229 'An error occurred deleting dynamic translation for `' .
230 $text_orig . ' | ' . $e->getMessage(),
231 Analog::ERROR
232 );
233
234 $this->warnings[] = str_replace(
235 '%field',
236 $text_orig,
237 _T('Unable to remove old dynamic translation for %field :(')
238 );
239
240 return false;
241 }
242 }
243
244 /**
245 * Get warnings
246 *
247 * @return array
248 */
249 public function getWarnings()
250 {
251 return $this->warnings;
252 }
253 }