]> git.agnieray.net Git - galette.git/blob - galette/includes/dependencies.php
696c794eccd981a94c9203e3962cb946f639eefa
[galette.git] / galette / includes / dependencies.php
1 <?php
2 /**
3 * Dependency injection configuration
4 *
5 * PHP version 5
6 *
7 * Copyright © 2003-2018 The Galette Team
8 *
9 * This file is part of Galette (http://galette.tuxfamily.org).
10 *
11 * Galette is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * Galette is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
23 *
24 * @category Functions
25 * @package Galette
26 *
27 * @author Frédéric Jaqcuot <unknown@unknow.com>
28 * @author Georges Khaznadar (password encryption, images) <unknown@unknow.com>
29 * @author Johan Cwiklinski <johan@x-tnd.be>
30 * @copyright 2003-2018 The Galette Team
31 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
32 * @link http://galette.tuxfamily.org
33 */
34
35 use Galette\Entity\PdfModel;
36 use Slim\Event\SlimEventManager;
37
38 $container = $app->getContainer();
39
40 // -----------------------------------------------------------------------------
41 // Error handling
42 // -----------------------------------------------------------------------------
43
44 $container['errorHandler'] = function ($c) {
45 return new Galette\Handlers\Error($c['view'], true);
46 };
47
48 $container['phpErrorHandler'] = function ($c) {
49 return new Galette\Handlers\PhpError($c['view'], true);
50 };
51
52 $container['notFoundHandler'] = function ($c) {
53 return new Galette\Handlers\NotFound($c['view']);
54 };
55
56 // -----------------------------------------------------------------------------
57 // Service providers
58 // -----------------------------------------------------------------------------
59
60 // Register Smarty View helper
61 $container['view'] = function ($c) {
62 $view = new \Slim\Views\Smarty(
63 rtrim(GALETTE_ROOT . GALETTE_TPL_SUBDIR, DIRECTORY_SEPARATOR),
64 [
65 'cacheDir' => rtrim(GALETTE_CACHE_DIR, DIRECTORY_SEPARATOR),
66 'compileDir' => rtrim(GALETTE_COMPILE_DIR, DIRECTORY_SEPARATOR),
67 'pluginsDir' => [
68 GALETTE_ROOT . 'includes/smarty_plugins'
69 ]
70 ]
71 );
72
73 // Add Slim specific plugins
74 $basepath = str_replace(
75 'index.php',
76 '',
77 $c->get('request')->getUri()->getBasePath()
78 );
79 $view->addSlimPlugins($c->get('router'), $basepath);
80
81 $smarty = $view->getSmarty();
82 $smarty->inheritance_merge_compiled_includes = false;
83
84 $smarty->assign('flash', $c->get('flash'));
85
86 $smarty->assign('login', $c->get('login'));
87 $smarty->assign('logo', $c->get('logo'));
88 $smarty->assign('tpl', $smarty);
89 $smarty->assign('headers', $c->get('plugins')->getTplHeaders());
90 $smarty->assign('plugin_actions', $c->get('plugins')->getTplAdhActions());
91 $smarty->assign(
92 'plugin_batch_actions',
93 $c->get('plugins')->getTplAdhBatchActions()
94 );
95 $smarty->assign(
96 'plugin_detailled_actions',
97 $c->get('plugins')->getTplAdhDetailledActions()
98 );
99 $smarty->assign('scripts_dir', 'js/');
100 $smarty->assign('jquery_dir', 'js/jquery/');
101 $smarty->assign('jquery_markitup_version', JQUERY_MARKITUP_VERSION);
102 $smarty->assign('PAGENAME', basename($_SERVER['SCRIPT_NAME']));
103 $smarty->assign('galette_base_path', './');
104 $smarty->assign('GALETTE_VERSION', GALETTE_VERSION);
105 $smarty->assign('GALETTE_MODE', GALETTE_MODE);
106 $smarty->assign('GALETTE_DISPLAY_ERRORS', GALETTE_DISPLAY_ERRORS);
107 $smarty->assign('_CURRENT_THEME_PATH', _CURRENT_THEME_PATH);
108
109 /*if ($this->parserConfigDir) {
110 $instance->setConfigDir($this->parserConfigDir);
111 }*/
112
113 $smarty->assign('template_subdir', GALETTE_THEME);
114 foreach ($c->get('plugins')->getTplAssignments() as $k => $v) {
115 $smarty->assign($k, $v);
116 }
117 /** galette_lang should be removed and languages used instead */
118 $smarty->assign('galette_lang', $c->get('i18n')->getAbbrev());
119 $smarty->assign('galette_lang_name', $c->get('i18n')->getName());
120 $smarty->assign('languages', $c->get('i18n')->getList());
121 $smarty->assign('i18n', $c->get('i18n'));
122 $smarty->assign('plugins', $c->get('plugins'));
123 $smarty->assign('preferences', $c->get('preferences'));
124 $smarty->assign('pref_slogan', $c->get('preferences')->pref_slogan);
125 $smarty->assign('pref_theme', $c->get('preferences')->pref_theme);
126 $smarty->assign('pref_statut', $c->get('preferences')->pref_statut);
127 $smarty->assign(
128 'pref_editor_enabled',
129 $c->preferences->pref_editor_enabled
130 );
131 $smarty->assign('pref_mail_method', $c->get('preferences')->pref_mail_method);
132 $smarty->assign('existing_mailing', $c->get('session')->mailing !== null);
133 $smarty->assign('contentcls', null);
134 $smarty->assign('additionnal_html_class', null);
135 $smarty->assign('head_redirect', null);
136 $smarty->assign('error_detected', null);
137 $smarty->assign('warning_detected', null);
138 $smarty->assign('success_detected', null);
139 $smarty->assign('require_tree', null);
140 $smarty->assign('html_editor', null);
141 $smarty->assign('require_charts', null);
142 $smarty->assign('require_mass', null);
143 $smarty->assign('autocomplete', null);
144 if ($c->get('login')->isAdmin() && $c->get('preferences')->pref_telemetry_date) {
145 $now = new \DateTime();
146 $sent = new \DateTime($c->get('preferences')->pref_telemetry_date);
147 $sent->add(new \DateInterval('P1Y'));// ask to resend telemetry after one year
148 if ($now > $sent && !$_COOKIE['renew_telemetry']) {
149 $smarty->assign('renew_telemetry', true);
150 }
151 }
152
153 foreach ($c->get('plugins')->getModules() as $module_id => $module) {
154 $smarty->addTemplateDir(
155 $module['root'] . '/templates/' . $c->get('preferences')->pref_theme,
156 $module['route']
157 );
158 }
159 return $view;
160 };
161
162 // Flash messages
163 $container['flash'] = function ($c) {
164 return new \Slim\Flash\Messages;
165 };
166
167 $container['plugins'] = function ($c) use ($app) {
168 $plugins = new Galette\Core\Plugins();
169 $i18n = $c->get('i18n');
170 $plugins->loadModules($c->get('preferences'), GALETTE_PLUGINS_PATH, $i18n->getLongID());
171 return $plugins;
172 };
173
174 $container['i18n'] = function ($c) {
175 $i18n = $c->get('session')->i18n;
176 if (!$i18n || !$i18n->getId() || isset($_GET['ui_pref_lang']) && $_GET['ui_pref_lang']) {
177 $i18n = new Galette\Core\I18n($_GET['ui_pref_lang'] ?? false);
178 $c->get('session')->i18n = $i18n;
179 }
180 return $i18n;
181 };
182
183 $container['zdb'] = function ($c) {
184 $zdb = new Galette\Core\Db();
185 return $zdb;
186 };
187
188 $container['preferences'] = function ($c) {
189 return new Galette\Core\Preferences($c->get('zdb'));
190 };
191
192 $container['login'] = function ($c) {
193 $login = $c->get('session')->login;
194 if (!$login) {
195 $login = new Galette\Core\Login(
196 $c->get('zdb'),
197 $c->get('i18n'),
198 $c->get('session')
199 );
200 }
201 return $login;
202 };
203
204 $container['session'] = function ($c) {
205 $session = new \RKA\Session();
206 return $session;
207 };
208
209 $container['logo'] = function ($c) {
210 return new Galette\Core\Logo();
211 };
212
213 $container['print_logo'] = function ($c) {
214 return new Galette\Core\PrintLogo();
215 };
216
217
218 $container['history'] = function ($c) {
219 return new Galette\Core\History($c->get('zdb'), $c->get('login'));
220 };
221
222 $container['acls'] = function ($c) {
223 include_once GALETTE_ROOT . 'includes/core_acls.php';
224 $acls = $core_acls;
225
226 foreach ($c->get('plugins')->getModules() as $plugin) {
227 $acls[$plugin['route'] . 'Info'] = 'member';
228 }
229
230 //use + to be sure core ACLs are not overrided by plugins ones.
231 $acls = $acls + $c->get('plugins')->getAcls();
232
233 //load user defined ACLs
234 if (file_exists(GALETTE_CONFIG_PATH . 'local_acls.inc.php')) {
235 //use array_merge here, we want $local_acls to override core ones.
236 $acls = array_merge($acls, $local_acls);
237 }
238
239 return $acls;
240 };
241
242 $container['members_fields'] = function ($c) {
243 include_once GALETTE_ROOT . 'includes/fields_defs/members_fields.php';
244 return $members_fields;
245 };
246
247 $container['members_form_fields'] = function ($c) {
248 $fields = $c->get('members_fields');
249 foreach ($fields as $k => $field) {
250 if ($field['position'] == -1) {
251 unset($fields[$k]);
252 }
253 }
254 return $fields;
255 };
256
257
258 $container['members_fields_cats'] = function ($c) {
259 include_once GALETTE_ROOT . 'includes/fields_defs/members_fields_cats.php';
260 return $members_fields_cats;
261 };
262
263 $container['pdfmodels_fields'] = function ($c) {
264 //include_once GALETTE_ROOT . 'includes/fields_defs/pdfmodels_fields.php';
265 $pdfmodels_fields = array(
266 array(
267 'model_id' => PdfModel::MAIN_MODEL,
268 'model_name' => '_T("Main")',
269 'model_title' => null,
270 'model_type' => PdfModel::MAIN_MODEL,
271 'model_header' => '<table>
272 <tr>
273 <td id="pdf_assoname"><strong id="asso_name">{ASSO_NAME}</strong><br/>{ASSO_SLOGAN}</td>
274 <td id="pdf_logo">{ASSO_LOGO}</td>
275 </tr>
276 </table>',
277 'model_footer' => '<div id="pdf_footer">
278 _T("Association") {ASSO_NAME} - {ASSO_ADDRESS}<br/>
279 {ASSO_WEBSITE}
280 </div>',
281 'model_body' => null,
282 'model_styles' => 'div#pdf_title {
283 font-size: 1.4em;
284 font-wieght:bold;
285 text-align: center;
286 }
287
288 div#pdf_subtitle {
289 text-align: center;
290 }
291
292 div#pdf_footer {
293 text-align: center;
294 font-size: 0.7em;
295 }
296
297 td#pdf_assoname {
298 width: 75%;
299 font-size: 1.1em;
300 }
301
302 strong#asso_name {
303 font-size: 1.6em;
304 }
305
306 td#pdf_logo {
307 text-align: right;
308 width: 25%;
309 }',
310 'model_parent' => null
311 ),
312 array(
313 'model_id' => PdfModel::INVOICE_MODEL,
314 'model_name' => '_T("Invoice")',
315 'model_title' => '_T("Invoice") {CONTRIBUTION_YEAR}-{CONTRIBUTION_ID}',
316 'model_type' => PdfModel::INVOICE_MODEL,
317 'model_header' => null,
318 'model_footer' => null,
319 'model_body' => '<table>
320 <tr>
321 <td width="300"></td>
322 <td><strong>{NAME_ADH}</strong><br/>
323 {ADDRESS_ADH}<br/>
324 <strong>{ZIP_ADH} {TOWN_ADH}</strong>
325 </td>
326 </tr>
327 <tr>
328 <td height="100"></td>
329 </tr>
330 <tr>
331 <td colspan="2">
332 <table>
333 <thead>
334 <tr>
335 <th>_T("Label")</th>
336 <th>_T("Amount")</th>
337 </tr>
338 </thead>
339 <tbody>
340 <tr>
341 <td>
342 {CONTRIBUTION_LABEL} (_T("on") {CONTRIBUTION_DATE})<br/>
343 _T("from") {CONTRIBUTION_BEGIN_DATE} _T("to") {CONTRIBUTION_END_DATE}<br/>
344 {CONTRIBUTION_PAYMENT_TYPE}<br/>
345 {CONTRIBUTION_COMMENT}
346 </td>
347 <td>{CONTRIBUTION_AMOUNT}</td>
348 </tr>
349 </tbody>
350 </table>
351 </td>
352 </tr>
353 </table>',
354 'model_styles' => null,
355 'model_parent' => PdfModel::MAIN_MODEL
356 ),
357 array(
358 'model_id' => PdfModel::RECEIPT_MODEL,
359 'model_name' => '_T("Receipt")',
360 'model_title' => '_T("Receipt") {CONTRIBUTION_YEAR}-{CONTRIBUTION_ID}',
361 'model_type' => PdfModel::RECEIPT_MODEL,
362 'model_header' => null,
363 'model_footer' => null,
364 'model_body' => '<table>
365 <tr>
366 <td width="300"></td>
367 <td><strong>{NAME_ADH}</strong><br/>
368 {ADDRESS_ADH}<br/>
369 <strong>{ZIP_ADH} {TOWN_ADH}</strong>
370 </td>
371 </tr>
372 <tr>
373 <td height="100"></td>
374 </tr>
375 <tr>
376 <td colspan="2">
377 <table>
378 <thead>
379 <tr>
380 <th>_T("Label")</th>
381 <th>_T("Amount")</th>
382 </tr>
383 </thead>
384 <tbody>
385 <tr>
386 <td>
387 {CONTRIBUTION_LABEL} (_T("on") {CONTRIBUTION_DATE})<br/>
388 _T("from") {CONTRIBUTION_BEGIN_DATE} _T("to") {CONTRIBUTION_END_DATE}<br/>
389 {CONTRIBUTION_PAYMENT_TYPE}<br/>
390 {CONTRIBUTION_COMMENT}
391 </td>
392 <td>{CONTRIBUTION_AMOUNT}</td>
393 </tr>
394 </tbody>
395 </table>
396 </td>
397 </tr>
398 </table>',
399 'model_styles' => null,
400 'model_parent' => PdfModel::MAIN_MODEL
401 ),
402 array(
403 'model_id' => PdfModel::ADHESION_FORM_MODEL,
404 'model_name' => '_T("Adhesion form")',
405 'model_title' => '_T("Adhesion form")',
406 'model_type' => PdfModel::ADHESION_FORM_MODEL,
407 'model_header' => null,
408 'model_footer' => null,
409 'model_body' => '<hr/>
410 <div class="infos">_T("Complete the following form and send it with your funds, in order to complete your subscription.")</div>
411 <table>
412 <tr>
413 <td width="50%"></td>
414 <td width="50%">{ASSO_ADDRESS_MULTI}</td>
415 </tr>
416 </table>
417 <hr/>
418 <table>
419 <tr>
420 <td height="30"></td>
421 </tr>
422 <tr>
423 <td>_T("Required membership:")
424 <form action="none">
425 <input type="radio" class="box" name="cotisation" value="none1">_T("Active member")
426 <input type="radio" class="box" name="cotisation" value="none2">_T("Benefactor member")
427 <input type="radio" class="box" name="cotisation" value="none3">_T("Donation")
428 <div class="infos">_T("The minimum contribution for each type of membership are defined on the website of the association. The amount of donations are free to be decided by the generous donor.") </div>
429 </form>
430 </td>
431 </tr>
432 <tr>
433 <td height="30"></td>
434 </tr>
435 </table>
436 <table class="member">
437 <tr>
438 <td class="label">_T("Politeness")</td>
439 <td class="input">{TITLE_ADH}</td>
440 </tr>
441 <tr>
442 <td class="label">_T("Name")</td>
443 <td class="input">{LAST_NAME_ADH}</td>
444 </tr>
445 <tr>
446 <td class="label">_T("First name")</td>
447 <td class="input">{FIRST_NAME_ADH}</td>
448 </tr>
449 <tr>
450 <td class="label">_T("Company name") *</td>
451 <td class="input">{COMPANY_ADH}</td>
452 </tr>
453 <tr>
454 <td class="label">_T("Address")</td>
455 <td class="input">{ADDRESS_ADH}</td>
456 </tr>
457 <tr>
458 <td class="label"></td>
459 <td class="input"></td>
460 </tr>
461 <tr>
462 <td class="label"></td>
463 <td class="input"></td>
464 </tr>
465 <tr>
466 <td class="label">_T("Zip Code")</td>
467 <td class="cpinput">{ZIP_ADH}</td>
468 <td class="label">_T("City")</td>
469 <td class="towninput">{TOWN_ADH}</td>
470 </tr>
471 <tr>
472 <td class="label">_T("Country")</td>
473 <td class="input">{COUNTRY_ADH}</td>
474 </tr>
475 <tr>
476 <td class="label">_T("Email address")</td>
477 <td class="input">{EMAIL_ADH}</td>
478 </tr>
479 <tr>
480 <td class="label">_T("Username") **</td>
481 <td class="input">{LOGIN_ADH}</td>
482 </tr>
483 <tr>
484 <td colspan="2" height="10"></td>
485 </tr>
486 <tr>
487 <td class="label">_T("Amount")</td>
488 <td class="input"></td>
489 </tr>
490 </table>
491 <p>str_replace(\'%s\', \'{ASSO_NAME}\', \'_T("Hereby, I agree to comply to %s association statutes and its rules.")\')</p><p>_T("At ................................................")</p><p>_T("On .......... / .......... / .......... ")</p><p>_T("Signature")</p>
492 <p class="notes">_T("* Only for compagnies")<br/>_T("** Galette identifier, if applicable")</p>',
493 'model_styles' => 'td.label {
494 width: 20%;
495 font-weight: bold;
496 }
497 td.input {
498 width: 80%;
499 border-bottom: 1px dotted black;
500 }
501
502 td.cpinput {
503 width: 10%;
504 border-bottom: 1px dotted black;
505 }
506
507 td.towninput {
508 width: 50%;
509 border-bottom: 1px dotted black;
510 }
511
512 div.infos {
513 font-size: .8em;
514 }
515
516 p.notes {
517 font-size: 0.6em;
518 text-align: right;
519 }
520
521 .member td {
522 line-height: 20px;
523 height: 20px;
524 }',
525 'model_parent' => PdfModel::MAIN_MODEL
526 )
527 );
528 return $pdfmodels_fields;
529 };
530
531 // -----------------------------------------------------------------------------
532 // Service factories
533 // -----------------------------------------------------------------------------
534
535 // monolog
536 $container['logger'] = function ($c) {
537 $settings = $c->get('settings');
538 $logger = new \Monolog\Logger($settings['logger']['name']);
539 $logger->pushProcessor(new \Monolog\Processor\UidProcessor());
540 $logger->pushHandler(new \Monolog\Handler\StreamHandler($settings['logger']['path'], $setting['logger']['level']));
541 return $logger;
542 };
543
544 $container['fields_config'] = function ($c) {
545 $fc = new Galette\Entity\FieldsConfig(
546 $c->get('zdb'),
547 Galette\Entity\Adherent::TABLE,
548 $c->get('members_fields'),
549 $c->get('members_fields_cats')
550 );
551 return $fc;
552 };
553
554 $container['lists_config'] = function ($c) {
555 $fc = new Galette\Entity\ListsConfig(
556 $c->get('zdb'),
557 Galette\Entity\Adherent::TABLE,
558 $c->get('members_fields'),
559 $c->get('members_fields_cats')
560 );
561 return $fc;
562 };
563
564 $container['cache'] = function ($c) {
565 $adapter = null;
566 if (function_exists('wincache_ucache_add')) {
567 //since APCu is not known to work on windows
568 $adapter = 'wincache';
569 } elseif (function_exists('apcu_fetch')) {
570 $adapter = 'apcu';
571 }
572 if ($adapter !== null) {
573 $uuid = $c->get('mode') !== 'INSTALL' ? $c->get('preferences')->pref_instance_uuid : '_install';
574 $cache = Laminas\Cache\StorageFactory::factory([
575 'adapter' => $adapter,
576 'options' => [
577 'namespace' => str_replace(
578 ['%version', '%uuid'],
579 [GALETTE_VERSION, $uuid],
580 'galette_%version_%uuid'
581 )
582 ]
583 ]);
584 return $cache;
585 }
586 return null;
587 };
588
589 $container['translator'] = function ($c) {
590 $translator = new Galette\Core\Translator();
591
592 $domains = ['galette'];
593 foreach ($domains as $domain) {
594 //load translation file for domain
595 $translator->addTranslationFilePattern(
596 'gettext',
597 GALETTE_ROOT . '/lang/',
598 '/%s/LC_MESSAGES/' . $domain . '.mo',
599 $domain
600 );
601
602 //check if a local lang file exists and load it
603 $translator->addTranslationFilePattern(
604 'phparray',
605 GALETTE_ROOT . '/lang/',
606 $domain . '_%s_local_lang.php',
607 $domain
608 );
609 }
610
611 $translator->setLocale($c->get('i18n')->getLongID());
612 if (!isset($container['mode']) || $c->get('mode') !== 'INSTALL' && $c->get('mode') !== 'NEED_UPDATE') {
613 $translator->setCache($c->get('cache'));
614 }
615 return $translator;
616 };
617
618 // Add Event manager to dependency.
619 $container['event_manager'] = function ($c) {
620 $emitter = new SlimEventManager();
621 return $emitter;
622 };
623
624 //For bad existing globals can be used...
625 if (!isset($container['mode']) || $container['mode'] !== 'INSTALL' && $container['mode'] !== 'NEED_UPDATE') {
626 $zdb = $container->get('zdb');
627 $preferences = $container->get('preferences');
628 $login = $container->get('login');
629 $hist = $container->get('history');
630 }
631 $i18n = $container->get('i18n');
632 $translator = $container->get('translator');
633 $emitter = $container->get('event_manager');
634
635 require_once GALETTE_ROOT . 'includes/i18n.inc.php';