]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Entity/Texts.php
Locale was not updated on translator
[galette.git] / galette / lib / Galette / Entity / Texts.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Texts handling
7 *
8 * PHP version 5
9 *
10 * Copyright © 2007-2014 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 John Perr <johnperr@abul.org>
31 * @author Johan Cwiklinski <joahn@x-tnd.be>
32 * @copyright 2007-2014 The Galette Team
33 * @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 Avaialble since 0.7dev - 2007-07-16
36 */
37
38 namespace Galette\Entity;
39
40 use Analog\Analog;
41 use Laminas\Db\Sql\Expression;
42 use Galette\Core\Preferences;
43 use Slim\Router;
44
45 /**
46 * Texts class for galette
47 *
48 * @category Entity
49 * @name Texts
50 * @package Galette
51 * @author John Perr <johnperr@abul.org>
52 * @author Johan Cwiklinski <joahn@x-tnd.be>
53 * @copyright 2007-2014 The Galette Team
54 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
55 * @link http://galette.tuxfamily.org
56 * @since Avaialble since 0.7dev - 2007-07-16
57 */
58 class Texts
59 {
60 private $all_texts;
61 const TABLE = "texts";
62 const PK = 'tid';
63 const DEFAULT_REF = 'sub';
64
65 private $patterns;
66 private $replaces;
67 private $defaults;
68
69 /**
70 * Main constructor
71 *
72 * @param Preferences $preferences Galette's preferences
73 * @param Router $router Router instance
74 * @param array $replaces Data that will be used as replacments
75 */
76 public function __construct(Preferences $preferences, Router $router = null, $replaces = null)
77 {
78 $this->patterns = array(
79 'asso_name' => '/{ASSO_NAME}/',
80 'asso_slogan' => '/{ASSO_SLOGAN}/',
81 'id_adh' => '/{ID_ADH}/',
82 'name_adh' => '/{NAME_ADH}/',
83 'lastname_adh' => '/{LASTNAME_ADH}/',
84 'firstname_adh' => '/{FIRSTNAME_ADH}/',
85 'login_adh' => '/{LOGIN}/',
86 'mail_adh' => '/{MAIL_ADH}/',
87 'login_uri' => '/{LOGIN_URI}/',
88 'change_pass_uri' => '/{CHG_PWD_URI}/',
89 'link_validity' => '/{LINK_VALIDITY}/',
90 'deadline' => '/{DEADLINE}/',
91 'contrib_info' => '/{CONTRIB_INFO}/',
92 'days_remaining' => '/{DAYS_REMAINING}/',
93 'days_expired' => '/{DAYS_EXPIRED}/',
94 'contrib_amount' => '/{CONTRIB_AMOUNT}/',
95 'contrib_type' => '/{CONTRIB_TYPE}/',
96 'breakline' => '/{BR}/',
97 'newline' => '/{NEWLINE}/',
98 'link_membercard' => '/{LINK_MEMBERCARD}/',
99 'link_contribpdf' => '/{LINK_CONTRIBPDF}/'
100 );
101
102 $login_uri = '';
103 if ($router !== null) {
104 $login_uri = $preferences->getURL() . $router->pathFor('login');
105 }
106
107 $this->replaces = array(
108 'asso_name' => $preferences->pref_nom,
109 'asso_slogan' => $preferences->pref_slogan,
110 'id_adh' => null,
111 'name_adh' => null,
112 'lastname_adh' => null,
113 'firstname_adh' => null,
114 'login_adh' => null,
115 'mail_adh' => null,
116 'login_uri' => $login_uri,
117 'change_pass_uri' => null,
118 'link_validity' => null,
119 'deadline' => null,
120 'contrib_info' => null,
121 'days_remaining' => null,
122 'days_expired' => null,
123 'contrib_amount' => null,
124 'contrib_type' => null,
125 'breakline' => "\r\n",
126 'newline' => "\r\n\r\n",
127 'link_membercard' => null,
128 'link_contribpdf' => null
129 );
130
131 if ($replaces != null && is_array($replaces)) {
132 $this->setReplaces($replaces);
133 }
134 }
135
136 /**
137 * Set replacements values
138 *
139 * @param array $replaces Replacements values
140 *
141 * @return void
142 */
143 public function setReplaces($replaces)
144 {
145 //let's populate replacement array with values provided
146 foreach ($replaces as $k => $v) {
147 $this->replaces[$k] = $v;
148 }
149 }
150
151 /**
152 * Get specific text
153 *
154 * @param string $ref Reference of text to get
155 * @param string $lang Language texts to get
156 *
157 * @return array of all text fields for one language.
158 */
159 public function getTexts($ref, $lang)
160 {
161 global $zdb, $i18n;
162
163 //check if language is set and exists
164 $langs = $i18n->getList();
165 $is_lang_ok = false;
166 foreach ($langs as $l) {
167 if ($lang === $l->getID()) {
168 $is_lang_ok = true;
169 break;
170 }
171 }
172
173 if ($is_lang_ok !== true) {
174 Analog::log(
175 'Language ' . $lang .
176 ' does not exists. Falling back to default Galette lang.',
177 Analog::ERROR
178 );
179 $lang = $i18n->getID();
180 }
181
182 try {
183 $select = $zdb->select(self::TABLE);
184 $select->where(
185 array(
186 'tref' => $ref,
187 'tlang' => $lang
188 )
189 );
190 $results = $zdb->execute($select);
191 $result = $results->current();
192 if ($result) {
193 $this->all_texts = $result;
194 } else {
195 //hum... no result... That means text do not exist in the
196 //database, let's add it
197 $default = null;
198 $this->defaults = $this->getAllDefaults(); //load defaults
199 foreach ($this->defaults as $d) {
200 if ($d['tref'] == $ref && $d['tlang'] == $lang) {
201 $default = $d;
202 break;
203 }
204 }
205 if ($default !== null) {
206 $values = array(
207 'tref' => $default['tref'],
208 'tsubject' => $default['tsubject'],
209 'tbody' => $default['tbody'],
210 'tlang' => $default['tlang'],
211 'tcomment' => $default['tcomment']
212 );
213
214 try {
215 $this->insert($zdb, [$values]);
216 return $this->getTexts($ref, $lang);
217 } catch (\Exception $e) {
218 Analog::log(
219 'Unable to add missing requested text "' . $ref .
220 ' (' . $lang . ') | ' . $e->getMessage(),
221 Analog::WARNING
222 );
223 }
224 } else {
225 Analog::log(
226 'Unable to find missing requested text "' . $ref .
227 ' (' . $lang . ')',
228 Analog::WARNING
229 );
230 }
231 }
232 return $this->all_texts;
233 } catch (\Exception $e) {
234 Analog::log(
235 'Cannot get text `' . $ref . '` for lang `' . $lang . '` | ' .
236 $e->getMessage(),
237 Analog::WARNING
238 );
239 return false;
240 }
241 }
242
243 /**
244 * Set text
245 *
246 * @param string $ref Texte ref to locate
247 * @param string $lang Texte language to locate
248 * @param string $subject Subject to set
249 * @param string $body Body text to set
250 *
251 * @return integer|false affected rows (0 if record did not change)
252 * or false on error
253 */
254 public function setTexts($ref, $lang, $subject, $body)
255 {
256 global $zdb;
257 //set texts
258
259 try {
260 $values = array(
261 'tsubject' => $subject,
262 'tbody' => $body,
263 );
264
265 $update = $zdb->update(self::TABLE);
266 $update->set($values)->where(
267 array(
268 'tref' => $ref,
269 'tlang' => $lang
270 )
271 );
272 $zdb->execute($update);
273
274 return true;
275 } catch (\Exception $e) {
276 Analog::log(
277 'An error has occurred while storing email text. | ' .
278 $e->getMessage(),
279 Analog::ERROR
280 );
281 return false;
282 }
283 }
284
285 /**
286 * Ref List
287 *
288 * @param string $lang Requested language
289 *
290 * @return array: list of references used for texts
291 */
292 public function getRefs($lang)
293 {
294 global $zdb;
295
296 try {
297 $select = $zdb->select(self::TABLE);
298 $select->columns(
299 array('tref', 'tcomment')
300 )->where(array('tlang' => $lang));
301
302 $refs = [];
303 $results = $zdb->execute($select);
304 foreach ($results as $result) {
305 $refs[] = $result;
306 }
307 return $refs;
308 } catch (\Exception $e) {
309 Analog::log(
310 'Cannot get refs for lang `' . $lang . '` | ' .
311 $e->getMessage(),
312 Analog::WARNING
313 );
314 return false;
315 }
316 }
317
318 /**
319 * Initialize texts at install time
320 *
321 * @param boolean $check_first Check first if it seem initialized
322 *
323 * @return boolean|Exception false if no need to initialize, true if data
324 * has been initialized, Exception if error
325 */
326 public function installInit($check_first = true)
327 {
328 global $zdb;
329
330 try {
331 //first of all, let's check if data seem to have already
332 //been initialized
333 $this->defaults = $this->getAllDefaults(); //load defaults
334 $proceed = false;
335 if ($check_first === true) {
336 $select = $zdb->select(self::TABLE);
337 $select->columns(
338 array(
339 'counter' => new Expression('COUNT(' . self::PK . ')')
340 )
341 );
342
343 $results = $zdb->execute($select);
344 $result = $results->current();
345 $count = $result->counter;
346 if ($count == 0) {
347 //if we got no values in texts table, let's proceed
348 $proceed = true;
349 } else {
350 if ($count < count($this->defaults)) {
351 return $this->checkUpdate();
352 }
353 return false;
354 }
355 } else {
356 $proceed = true;
357 }
358
359 if ($proceed === true) {
360 //first, we drop all values
361 $delete = $zdb->delete(self::TABLE);
362 $zdb->execute($delete);
363
364 $zdb->handleSequence(
365 self::TABLE,
366 count($this->defaults)
367 );
368
369 $this->insert($zdb, $this->defaults);
370
371 Analog::log(
372 'Default texts were successfully stored into database.',
373 Analog::INFO
374 );
375 return true;
376 }
377 } catch (\Exception $e) {
378 Analog::log(
379 'Unable to initialize default texts.' . $e->getMessage(),
380 Analog::WARNING
381 );
382 return $e;
383 }
384 }
385
386 /**
387 * Checks for missing texts in the database
388 *
389 * @return boolean
390 */
391 private function checkUpdate()
392 {
393 global $zdb;
394
395 try {
396 $select = $zdb->select(self::TABLE);
397 $dblist = $zdb->execute($select);
398
399 $list = [];
400 foreach ($dblist as $dbentry) {
401 $list[] = $dbentry;
402 }
403
404 $missing = array();
405 foreach ($this->defaults as $default) {
406 $exists = false;
407 foreach ($list as $text) {
408 if (
409 $text->tref == $default['tref']
410 && $text->tlang == $default['tlang']
411 ) {
412 $exists = true;
413 continue;
414 }
415 }
416
417 if ($exists === false) {
418 //text does not exists in database, insert it.
419 $missing[] = $default;
420 }
421 }
422
423 if (count($missing) > 0) {
424 $this->insert($zdb, $missing);
425
426 Analog::log(
427 'Missing texts were successfully stored into database.',
428 Analog::INFO
429 );
430 return true;
431 }
432 } catch (\Exception $e) {
433 Analog::log(
434 'An error occurred checking missing texts.' . $e->getMessage(),
435 Analog::WARNING
436 );
437 throw $e;
438 }
439 }
440
441 /**
442 * Get the subject, with all replacements done
443 *
444 * @return string
445 */
446 public function getSubject()
447 {
448 return preg_replace(
449 $this->patterns,
450 $this->replaces,
451 $this->all_texts->tsubject
452 );
453 }
454
455 /**
456 * Get the body, with all replacements done
457 *
458 * @return string
459 */
460 public function getBody()
461 {
462 return preg_replace(
463 $this->patterns,
464 $this->replaces,
465 $this->all_texts->tbody
466 );
467 }
468
469 /**
470 * Insert values in database
471 *
472 * @param Db $zdb Database instance
473 * @param array $values Values to insert
474 *
475 * @return void
476 */
477 private function insert($zdb, $values)
478 {
479 $insert = $zdb->insert(self::TABLE);
480 $insert->values(
481 array(
482 'tref' => ':tref',
483 'tsubject' => ':tsubject',
484 'tbody' => ':tbody',
485 'tlang' => ':tlang',
486 'tcomment' => ':tcomment'
487 )
488 );
489 $stmt = $zdb->sql->prepareStatementForSqlObject($insert);
490
491 foreach ($values as $value) {
492 $stmt->execute($value);
493 }
494 }
495
496 /**
497 * Get default mail texts for all languages
498 *
499 * @return array
500 */
501 public function getAllDefaults()
502 {
503 global $i18n;
504
505 $all = [];
506 foreach (array_keys($i18n->getArrayList()) as $lang) {
507 $all = array_merge($all, $this->getDefaultTexts($lang));
508 }
509
510 return $all;
511 }
512
513 /**
514 * Get default texts for specified language
515 *
516 * @param string $lang Requested lang. Defaults to en_US
517 *
518 * @return array
519 */
520 public function getDefaultTexts($lang = 'en_US')
521 {
522 global $i18n;
523
524 $current_lang = $i18n->getID();
525
526 $i18n->changeLanguage($lang);
527
528 //do the magic!
529 include GALETTE_ROOT . 'includes/fields_defs/texts_fields.php';
530 $texts = [];
531
532 foreach ($texts_fields as $text_field) {
533 unset($text_field['tid']);
534 $text_field['tlang'] = $lang;
535 $texts[] = $text_field;
536 }
537
538 //reset to current lang
539 $i18n->changeLanguage($current_lang);
540 return $texts;
541 }
542 }