3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2018 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
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.
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.
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/>.
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
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.9.1dev - 2018-03-10
38 namespace Galette\Entity
;
41 use Galette\Core\L10n
;
42 use Laminas\Db\Sql\Expression
;
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
59 protected $warnings = [];
62 * Add a translation stored in the database
64 * @param string $text_orig Text to translate
68 protected function addTranslation($text_orig)
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'))
79 'text_orig' => $text_orig,
80 'text_locale' => $lang->getLongID()
84 $results = $this->zdb
->execute($select);
85 $result = $results->current();
88 $nref = $result->text_nref
;
91 if (is_numeric($nref) && $nref > 0) {
92 //already existing, update
94 'text_nref' => new Expression('text_nref+1')
97 'Entry for `' . $text_orig .
98 '` dynamic translation already exists.',
102 $owhere = $select->where
;
104 $update = $this->zdb
->update(L10n
::TABLE
);
105 $update->set($values)->where($owhere);
106 $this->zdb
->execute($update);
109 // User is supposed to use current language as original text.
110 $text_trans = $text_orig;
111 if ($lang->getLongID() != $i18n->getLongID()) {
115 'text_orig' => $text_orig,
116 'text_locale' => $lang->getLongID(),
117 'text_trans' => $text_trans
120 $insert = $this->zdb
->insert(L10n
::TABLE
);
121 $insert->values($values);
122 $this->zdb
->execute($insert);
126 } catch (\Exception
$e) {
128 'An error occurred adding dynamic translation for `' .
129 $text_orig . '` | ' . $e->getMessage(),
133 $this->warnings
[] = str_replace(
136 _T('Unable to add dynamic translation for %field :(')
144 * Update a translation stored in the database
146 * @param string $text_orig Text to translate
147 * @param string $text_locale The locale
148 * @param string $text_trans Translated text
152 protected function updateTranslation($text_orig, $text_locale, $text_trans)
155 //check if translation already exists
156 $select = $this->zdb
->select(L10n
::TABLE
);
157 $select->columns(array('text_nref'))->where(
159 'text_orig' => $text_orig,
160 'text_locale' => $text_locale
164 $results = $this->zdb
->execute($select);
165 $result = $results->current();
169 $nref = $result->text_nref
;
170 $exists = (is_numeric($nref) && $nref > 0);
174 'text_trans' => $text_trans
178 $owhere = $select->where
;
180 $update = $this->zdb
->update(L10n
::TABLE
);
181 $update->set($values)->where($owhere);
182 $this->zdb
->execute($update);
184 $values['text_orig'] = $text_orig;
185 $values['text_locale'] = $text_locale;
187 $insert = $this->zdb
->insert(L10n
::TABLE
);
188 $insert->values($values);
189 $this->zdb
->execute($insert);
192 } catch (\Exception
$e) {
194 'An error occurred updating dynamic translation for `' .
195 $text_orig . '` | ' . $e->getMessage(),
199 $this->warnings
[] = str_replace(
202 _T('Unable to update dynamic translation for %field :(')
210 * Delete a translation stored in the database
212 * @param string $text_orig Text to translate
216 protected function deleteTranslation($text_orig)
219 $delete = $this->zdb
->delete(L10n
::TABLE
);
222 'text_orig' => $text_orig
225 $this->zdb
->execute($delete);
227 } catch (\Exception
$e) {
229 'An error occurred deleting dynamic translation for `' .
230 $text_orig . ' | ' . $e->getMessage(),
234 $this->warnings
[] = str_replace(
237 _T('Unable to remove old dynamic translation for %field :(')
249 public function getWarnings()
251 return $this->warnings
;