]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Entity/I18nTrait.php
ce6f30ec607a453150e3dac8f11684eafcda95b5
[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 Zend\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 /**
60 * Add a translation stored in the database
61 *
62 * @param string $text_orig Text to translate
63 *
64 * @return boolean
65 */
66 protected function addTranslation($text_orig)
67 {
68 global $i18n;
69
70 try {
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'))
75 ->where(
76 array(
77 'text_orig' => $text_orig,
78 'text_locale' => $lang->getLongID()
79 )
80 );
81
82 $results = $this->zdb->execute($select);
83 $result = $results->current();
84 $nref = 0;
85 if ($result) {
86 $nref = $result->text_nref;
87 }
88
89 if (is_numeric($nref) && $nref > 0) {
90 //already existing, update
91 $values = array(
92 'text_nref' => new Expression('text_nref+1')
93 );
94 Analog::log(
95 'Entry for `' . $text_orig .
96 '` dynamic translation already exists.',
97 Analog::INFO
98 );
99
100 $where = array();
101 $owhere = $select->where;
102
103 $update = $this->zdb->update(L10n::TABLE);
104 $update->set($values)->where($owhere);
105 $this->zdb->execute($update);
106 } else {
107 //add new entry
108 // User is supposed to use current language as original text.
109 $text_trans = $text_orig;
110 if ($lang->getLongID() != $i18n->getLongID()) {
111 $text_trans = '';
112 }
113 $values = array(
114 'text_orig' => $text_orig,
115 'text_locale' => $lang->getLongID(),
116 'text_trans' => $text_trans
117 );
118
119 $insert = $this->zdb->insert(L10n::TABLE);
120 $insert->values($values);
121 $this->zdb->execute($insert);
122 }
123 }
124 return true;
125 } catch (\Exception $e) {
126 Analog::log(
127 'An error occurred adding dynamic translation for `' .
128 $text_orig . '` | ' . $e->getMessage(),
129 Analog::ERROR
130 );
131 throw $e;
132 }
133 }
134
135 /**
136 * Update a translation stored in the database
137 *
138 * @param string $text_orig Text to translate
139 * @param string $text_locale The locale
140 * @param string $text_trans Translated text
141 *
142 * @return boolean
143 */
144 protected function updateDynamicTranslation($text_orig, $text_locale, $text_trans)
145 {
146 try {
147 //check if translation already exists
148 $select = $this->zdb->select(L10n::TABLE);
149 $select->columns(array('text_nref'))->where(
150 array(
151 'text_orig' => $text_orig,
152 'text_locale' => $text_locale
153 )
154 );
155
156 $results = $this->zdb->execute($select);
157 $result = $results->current();
158
159 $exists = false;
160 if ($result) {
161 $nref = $result->text_nref;
162 $exists = (is_numeric($nref) && $nref > 0);
163 }
164
165 $values = array(
166 'text_trans' => $text_trans
167 );
168
169 if ($exists) {
170 $where = array();
171 $owhere = $select->where;
172
173 $update = $this->zdb->update(L10n::TABLE);
174 $update->set($values)->where($owhere);
175 $this->zdb->execute($update);
176 } else {
177 $values['text_orig'] = $text_orig;
178 $values['text_locale'] = $text_locale;
179
180 $insert = $this->zdb->insert(L10n::TABLE);
181 $insert->values($values);
182 $this->zdb->execute($insert);
183 }
184 return true;
185 } catch (Exception $e) {
186 Analog::log(
187 'An error occurred updating dynamic translation for `' .
188 $text_orig . '` | ' . $e->getMessage(),
189 Analog::ERROR
190 );
191 return false;
192 }
193 }
194
195 /**
196 * Delete a translation stored in the database
197 *
198 * @param string $text_orig Text to translate
199 *
200 * @return boolean
201 */
202 protected function deleteTranslation($text_orig)
203 {
204 global $i18n;
205
206 try {
207 $delete = $this->zdb->delete(L10n::TABLE);
208 $delete->where(
209 array(
210 'text_orig' => $text_orig
211 )
212 );
213 $this->zdb->execute($delete);
214 return true;
215 } catch (Exception $e) {
216 Analog::log(
217 'An error occurred deleting dynamic translation for `' .
218 $text_orig . '` (lang `' . $lang->getLongID() . '`) | ' .
219 $e->getMessage(),
220 Analog::ERROR
221 );
222 throw $e;
223 }
224 }
225
226 /**
227 * Load dynamic fields for member
228 *
229 * @return void
230 */
231 /*private function loadDynamicFields()
232 {
233 if (!property_exists($this, 'login')) {
234 global $login;
235 } else {
236 $login = $this->login;
237 }
238 $this->dynamics = new DynamicFields($this->zdb, $login, $this);
239 }*/
240
241 /**
242 * Get dynamic fields
243 *
244 * @return array
245 */
246 /*public function getDynamicFields()
247 {
248 return $this->dynamics;
249 }*/
250
251 /**
252 * Extract posted values for dynamic fields
253 *
254 * @param array $post Posted values
255 *
256 * @return boolean
257 */
258 /*protected function dynamicsCheck($post)
259 {
260 if ($this->dynamics === null) {
261 Analog::log(
262 'Dynamics fields have not been loaded, cannot be checked. (from: ' . __METHOD__ . ')',
263 Analog::WARNING
264 );
265 $this->loadDynamicFields();
266 }
267 if ($post != null) {
268 $fields = $this->dynamics->getFields();
269
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)
277 ) {
278 if ($fields[$field_id]->isRequired() && (trim($value) === '' || $value == null)) {
279 $this->errors[] = str_replace(
280 '%field',
281 $field->getName(),
282 _T('Missing required field %field')
283 );
284 } else {
285 if ($fields[$field_id] instanceof File) {
286 //delete checkbox
287 $filename = sprintf(
288 'member_%d_field_%d_value_%d',
289 $this->id,
290 $field_id,
291 $val_index
292 );
293 unlink(GALETTE_FILES_PATH . $filename);
294 $this->dynamics->setValue($this->id, $field_id, $val_index, '');
295 } else {
296 //actual field value
297 if ($value !== null && trim($value) !== '') {
298 $this->dynamics->setValue($this->id, $field_id, $val_index, $value);
299 } else {
300 $this->dynamics->unsetValue($this->id, $field_id, $val_index);
301 }
302 }
303 }
304 }
305 }
306 }
307 }
308
309 return true;
310 }
311 }*/
312
313 /**
314 * Stores dynamic fields
315 *
316 * @param boolean $transaction True if a transaction already exists
317 *
318 * @return boolean
319 */
320 /*protected function dynamicsStore($transaction = false)
321 {
322 if ($this->dynamics === null) {
323 Analog::log(
324 'Dynamics fields have not been loaded, cannot be checked. (from: ' . __METHOD__ . ')',
325 Analog::WARNING
326 );
327 $this->loadDynamicFields();
328 }
329 return $this->dynamics->storeValues($this->id, $transaction);
330 }*/
331
332 /**
333 * Store dynamic Files
334 *
335 * @param array $files Posted files
336 *
337 * @return void
338 */
339 /*protected function dynamicsFiles($files)
340 {
341 if ($this->dynamics === null) {
342 Analog::log(
343 'Dynamics fields have not been loaded, cannot be checked. (from: ' . __METHOD__ . ')',
344 Analog::WARNING
345 );
346 $this->loadDynamicFields();
347 }
348 $fields = $this->dynamics->getFields();
349 $store = false;
350
351 foreach ($files as $key => $file) {
352 // if the field is disabled, skip it
353 if (isset($disabled[$key])) {
354 continue;
355 }
356
357 if (substr($key, 0, 11) != 'info_field_') {
358 continue;
359 }
360
361 list($field_id, $val_index) = explode('_', substr($key, 11));
362 if (! is_numeric($field_id) || ! is_numeric($val_index)) {
363 continue;
364 }
365
366 if ($file['error'] == UPLOAD_ERR_NO_FILE
367 && $file['name'] == ''
368 && $file['tmp_name'] == '') {
369 //not upload atempt.
370 continue;
371 } elseif ($file['error'] !== UPLOAD_ERR_OK) {
372 Analog::log("file upload error", Analog::ERROR);
373 continue;
374 }
375
376 $tmp_filename = $file['tmp_name'];
377 if ($tmp_filename == '') {
378 Analog::log("empty temporary filename", Analog::ERROR);
379 continue;
380 }
381
382 if (!is_uploaded_file($tmp_filename)) {
383 Analog::log("not an uploaded file", Analog::ERROR);
384 continue;
385 }
386
387 $max_size =
388 $fields[$field_id]->getSize() ?
389 $fields[$field_id]->getSize() * 1024 :
390 File::DEFAULT_MAX_FILE_SIZE * 1024;
391 if ($file['size'] > $max_size) {
392 Analog::log(
393 "file too large: " . $file['size'] . " Ko, vs $max_size Ko allowed",
394 Analog::ERROR
395 );
396 $this->errors[] = preg_replace(
397 '|%d|',
398 $max_size,
399 _T("File is too big. Maximum allowed size is %dKo")
400 );
401 continue;
402 }
403
404 $new_filename = sprintf(
405 'member_%d_field_%d_value_%d',
406 $this->id,
407 $field_id,
408 $val_index
409 );
410 Analog::log("new file: $new_filename", Analog::DEBUG);
411
412 move_uploaded_file(
413 $tmp_filename,
414 GALETTE_FILES_PATH . $new_filename
415 );
416 $this->dynamics->setValue($this->id, $field_id, $val_index, $file['name']);
417 $store = true;
418 }
419
420 if ($store === true) {
421 $this->dynamicsStore();
422 }
423 }*/
424
425 /**
426 * Get errors
427 *
428 * @return array
429 */
430 /*public function getErrors()
431 {
432 return $this->errors;
433 }*/
434 }