3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2007-2014 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 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
38 namespace Galette\Entity
;
41 use Laminas\Db\Sql\Expression
;
42 use Galette\Core\Preferences
;
46 * Texts class for 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
61 const TABLE
= "texts";
63 const DEFAULT_REF
= 'sub';
72 * @param Preferences $preferences Galette's preferences
73 * @param Router $router Router instance
74 * @param array $replaces Data that will be used as replacments
76 public function __construct(Preferences
$preferences, Router
$router = null, $replaces = null)
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}/'
103 if ($router !== null) {
104 $login_uri = $preferences->getURL() . $router->pathFor('login');
107 $this->replaces
= array(
108 'asso_name' => $preferences->pref_nom
,
109 'asso_slogan' => $preferences->pref_slogan
,
112 'lastname_adh' => null,
113 'firstname_adh' => null,
116 'login_uri' => $login_uri,
117 'change_pass_uri' => null,
118 'link_validity' => 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
131 if ($replaces != null && is_array($replaces)) {
132 $this->setReplaces($replaces);
137 * Set replacements values
139 * @param array $replaces Replacements values
143 public function setReplaces($replaces)
145 //let's populate replacement array with values provided
146 foreach ($replaces as $k => $v) {
147 $this->replaces
[$k] = $v;
154 * @param string $ref Reference of text to get
155 * @param string $lang Language texts to get
157 * @return array of all text fields for one language.
159 public function getTexts($ref, $lang)
163 //check if language is set and exists
164 $langs = $i18n->getList();
166 foreach ($langs as $l) {
167 if ($lang === $l->getID()) {
173 if ($is_lang_ok !== true) {
175 'Language ' . $lang .
176 ' does not exists. Falling back to default Galette lang.',
179 $lang = $i18n->getID();
183 $select = $zdb->select(self
::TABLE
);
190 $results = $zdb->execute($select);
191 $result = $results->current();
193 $this->all_texts
= $result;
195 //hum... no result... That means text do not exist in the
196 //database, let's add it
198 $this->defaults
= $this->getAllDefaults(); //load defaults
199 foreach ($this->defaults
as $d) {
200 if ($d['tref'] == $ref && $d['tlang'] == $lang) {
205 if ($default !== null) {
207 'tref' => $default['tref'],
208 'tsubject' => $default['tsubject'],
209 'tbody' => $default['tbody'],
210 'tlang' => $default['tlang'],
211 'tcomment' => $default['tcomment']
215 $this->insert($zdb, [$values]);
216 return $this->getTexts($ref, $lang);
217 } catch (\Exception
$e) {
219 'Unable to add missing requested text "' . $ref .
220 ' (' . $lang . ') | ' . $e->getMessage(),
226 'Unable to find missing requested text "' . $ref .
232 return $this->all_texts
;
233 } catch (\Exception
$e) {
235 'Cannot get text `' . $ref . '` for lang `' . $lang . '` | ' .
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
251 * @return integer|false affected rows (0 if record did not change)
254 public function setTexts($ref, $lang, $subject, $body)
261 'tsubject' => $subject,
265 $update = $zdb->update(self
::TABLE
);
266 $update->set($values)->where(
272 $zdb->execute($update);
275 } catch (\Exception
$e) {
277 'An error has occurred while storing email text. | ' .
288 * @param string $lang Requested language
290 * @return array: list of references used for texts
292 public function getRefs($lang)
297 $select = $zdb->select(self
::TABLE
);
299 array('tref', 'tcomment')
300 )->where(array('tlang' => $lang));
303 $results = $zdb->execute($select);
304 foreach ($results as $result) {
308 } catch (\Exception
$e) {
310 'Cannot get refs for lang `' . $lang . '` | ' .
319 * Retrieve fields from database
321 * @deprecated Do not seem to be used as of 2013-07-16
325 public static function getDbFields()
328 $columns = $zdb->getColumns(self
::TABLE
);
330 foreach ($columns as $col) {
331 $fields[] = $col->getName();
337 * Initialize texts at install time
339 * @param boolean $check_first Check first if it seem initialized
341 * @return boolean|Exception false if no need to initialize, true if data
342 * has been initialized, Exception if error
344 public function installInit($check_first = true)
349 //first of all, let's check if data seem to have already
351 $this->defaults
= $this->getAllDefaults(); //load defaults
353 if ($check_first === true) {
354 $select = $zdb->select(self
::TABLE
);
357 'counter' => new Expression('COUNT(' . self
::PK
. ')')
361 $results = $zdb->execute($select);
362 $result = $results->current();
363 $count = $result->counter
;
365 //if we got no values in texts table, let's proceed
368 if ($count < count($this->defaults
)) {
369 return $this->checkUpdate();
377 if ($proceed === true) {
378 //first, we drop all values
379 $delete = $zdb->delete(self
::TABLE
);
380 $zdb->execute($delete);
382 $zdb->handleSequence(
384 count($this->defaults
)
387 $this->insert($zdb, $this->defaults
);
390 'Default texts were successfully stored into database.',
395 } catch (\Exception
$e) {
397 'Unable to initialize default texts.' . $e->getMessage(),
405 * Checks for missing texts in the database
409 private function checkUpdate()
414 $select = $zdb->select(self
::TABLE
);
415 $dblist = $zdb->execute($select);
418 foreach ($dblist as $dbentry) {
423 foreach ($this->defaults
as $default) {
425 foreach ($list as $text) {
427 $text->tref
== $default['tref']
428 && $text->tlang
== $default['tlang']
435 if ($exists === false) {
436 //text does not exists in database, insert it.
437 $missing[] = $default;
441 if (count($missing) > 0) {
442 $this->insert($zdb, $missing);
445 'Missing texts were successfully stored into database.',
450 } catch (\Exception
$e) {
452 'An error occurred checking missing texts.' . $e->getMessage(),
460 * Get the subject, with all replacements done
464 public function getSubject()
469 $this->all_texts
->tsubject
474 * Get the body, with all replacements done
478 public function getBody()
483 $this->all_texts
->tbody
488 * Insert values in database
490 * @param Db $zdb Database instance
491 * @param array $values Values to insert
495 private function insert($zdb, $values)
497 $insert = $zdb->insert(self
::TABLE
);
501 'tsubject' => ':tsubject',
504 'tcomment' => ':tcomment'
507 $stmt = $zdb->sql
->prepareStatementForSqlObject($insert);
509 foreach ($values as $value) {
510 $stmt->execute($value);
515 * Get default mail texts for all languages
519 public function getAllDefaults()
524 foreach (array_keys($i18n->getArrayList()) as $lang) {
525 $all = array_merge($all, $this->getDefaultTexts($lang));
532 * Get default texts for specified language
534 * @param string $lang Requested lang. Defaults to en_US
538 public function getDefaultTexts($lang = 'en_US')
542 $current_lang = $i18n->getID();
544 $i18n->changeLanguage($lang);
547 include GALETTE_ROOT
. 'includes/fields_defs/texts_fields.php';
550 foreach ($texts_fields as $text_field) {
551 unset($text_field['tid']);
552 $text_field['tlang'] = $lang;
553 $texts[] = $text_field;
556 //reset to current lang
557 $i18n->changeLanguage($current_lang);