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