]>
git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Entity/I18nTrait.php
ce6f30ec607a453150e3dac8f11684eafcda95b5
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 Zend\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
60 * Add a translation stored in the database
62 * @param string $text_orig Text to translate
66 protected function addTranslation($text_orig)
71 foreach ($i18n->getList() as $lang) {
72 //check if translation already exists
73 $select = $this->zdb
->select(L10n
::TABLE
);
74 $select->columns(array('text_nref'))
77 'text_orig' => $text_orig,
78 'text_locale' => $lang->getLongID()
82 $results = $this->zdb
->execute($select);
83 $result = $results->current();
86 $nref = $result->text_nref
;
89 if (is_numeric($nref) && $nref > 0) {
90 //already existing, update
92 'text_nref' => new Expression('text_nref+1')
95 'Entry for `' . $text_orig .
96 '` dynamic translation already exists.',
101 $owhere = $select->where
;
103 $update = $this->zdb
->update(L10n
::TABLE
);
104 $update->set($values)->where($owhere);
105 $this->zdb
->execute($update);
108 // User is supposed to use current language as original text.
109 $text_trans = $text_orig;
110 if ($lang->getLongID() != $i18n->getLongID()) {
114 'text_orig' => $text_orig,
115 'text_locale' => $lang->getLongID(),
116 'text_trans' => $text_trans
119 $insert = $this->zdb
->insert(L10n
::TABLE
);
120 $insert->values($values);
121 $this->zdb
->execute($insert);
125 } catch (\Exception
$e) {
127 'An error occurred adding dynamic translation for `' .
128 $text_orig . '` | ' . $e->getMessage(),
136 * Update a translation stored in the database
138 * @param string $text_orig Text to translate
139 * @param string $text_locale The locale
140 * @param string $text_trans Translated text
144 protected function updateDynamicTranslation($text_orig, $text_locale, $text_trans)
147 //check if translation already exists
148 $select = $this->zdb
->select(L10n
::TABLE
);
149 $select->columns(array('text_nref'))->where(
151 'text_orig' => $text_orig,
152 'text_locale' => $text_locale
156 $results = $this->zdb
->execute($select);
157 $result = $results->current();
161 $nref = $result->text_nref
;
162 $exists = (is_numeric($nref) && $nref > 0);
166 'text_trans' => $text_trans
171 $owhere = $select->where
;
173 $update = $this->zdb
->update(L10n
::TABLE
);
174 $update->set($values)->where($owhere);
175 $this->zdb
->execute($update);
177 $values['text_orig'] = $text_orig;
178 $values['text_locale'] = $text_locale;
180 $insert = $this->zdb
->insert(L10n
::TABLE
);
181 $insert->values($values);
182 $this->zdb
->execute($insert);
185 } catch (Exception
$e) {
187 'An error occurred updating dynamic translation for `' .
188 $text_orig . '` | ' . $e->getMessage(),
196 * Delete a translation stored in the database
198 * @param string $text_orig Text to translate
202 protected function deleteTranslation($text_orig)
207 $delete = $this->zdb
->delete(L10n
::TABLE
);
210 'text_orig' => $text_orig
213 $this->zdb
->execute($delete);
215 } catch (Exception
$e) {
217 'An error occurred deleting dynamic translation for `' .
218 $text_orig . '` (lang `' . $lang->getLongID() . '`) | ' .
227 * Load dynamic fields for member
231 /*private function loadDynamicFields()
233 if (!property_exists($this, 'login')) {
236 $login = $this->login;
238 $this->dynamics = new DynamicFields($this->zdb, $login, $this);
246 /*public function getDynamicFields()
248 return $this->dynamics;
252 * Extract posted values for dynamic fields
254 * @param array $post Posted values
258 /*protected function dynamicsCheck($post)
260 if ($this->dynamics === null) {
262 'Dynamics fields have not been loaded, cannot be checked. (from: ' . __METHOD__ . ')',
265 $this->loadDynamicFields();
268 $fields = $this->dynamics->getFields();
270 foreach ($post as $key => $value) {
271 // if the field is enabled, check it
272 if (!isset($disabled[$key])) {
273 if (substr($key, 0, 11) == 'info_field_') {
274 list($field_id, $val_index) = explode('_', substr($key, 11));
275 if (is_numeric($field_id)
276 && is_numeric($val_index)
278 if ($fields[$field_id]->isRequired() && (trim($value) === '' || $value == null)) {
279 $this->errors[] = str_replace(
282 _T('Missing required field %field')
285 if ($fields[$field_id] instanceof File) {
288 'member_%d_field_%d_value_%d',
293 unlink(GALETTE_FILES_PATH . $filename);
294 $this->dynamics->setValue($this->id, $field_id, $val_index, '');
297 if ($value !== null && trim($value) !== '') {
298 $this->dynamics->setValue($this->id, $field_id, $val_index, $value);
300 $this->dynamics->unsetValue($this->id, $field_id, $val_index);
314 * Stores dynamic fields
316 * @param boolean $transaction True if a transaction already exists
320 /*protected function dynamicsStore($transaction = false)
322 if ($this->dynamics === null) {
324 'Dynamics fields have not been loaded, cannot be checked. (from: ' . __METHOD__ . ')',
327 $this->loadDynamicFields();
329 return $this->dynamics->storeValues($this->id, $transaction);
333 * Store dynamic Files
335 * @param array $files Posted files
339 /*protected function dynamicsFiles($files)
341 if ($this->dynamics === null) {
343 'Dynamics fields have not been loaded, cannot be checked. (from: ' . __METHOD__ . ')',
346 $this->loadDynamicFields();
348 $fields = $this->dynamics->getFields();
351 foreach ($files as $key => $file) {
352 // if the field is disabled, skip it
353 if (isset($disabled[$key])) {
357 if (substr($key, 0, 11) != 'info_field_') {
361 list($field_id, $val_index) = explode('_', substr($key, 11));
362 if (! is_numeric($field_id) || ! is_numeric($val_index)) {
366 if ($file['error'] == UPLOAD_ERR_NO_FILE
367 && $file['name'] == ''
368 && $file['tmp_name'] == '') {
371 } elseif ($file['error'] !== UPLOAD_ERR_OK) {
372 Analog::log("file upload error", Analog::ERROR);
376 $tmp_filename = $file['tmp_name'];
377 if ($tmp_filename == '') {
378 Analog::log("empty temporary filename", Analog::ERROR);
382 if (!is_uploaded_file($tmp_filename)) {
383 Analog::log("not an uploaded file", Analog::ERROR);
388 $fields[$field_id]->getSize() ?
389 $fields[$field_id]->getSize() * 1024 :
390 File::DEFAULT_MAX_FILE_SIZE * 1024;
391 if ($file['size'] > $max_size) {
393 "file too large: " . $file['size'] . " Ko, vs $max_size Ko allowed",
396 $this->errors[] = preg_replace(
399 _T("File is too big. Maximum allowed size is %dKo")
404 $new_filename = sprintf(
405 'member_%d_field_%d_value_%d',
410 Analog::log("new file: $new_filename", Analog::DEBUG);
414 GALETTE_FILES_PATH . $new_filename
416 $this->dynamics->setValue($this->id, $field_id, $val_index, $file['name']);
420 if ($store === true) {
421 $this->dynamicsStore();
430 /*public function getErrors()
432 return $this->errors;