]> git.agnieray.net Git - galette.git/blob - galette/includes/dependencies.php
Fix name (sorry)
[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['zdb'] = function ($c) {
188 $zdb = new Galette\Core\Db();
189 return $zdb;
190 };
191
192 $container['preferences'] = function ($c) {
193 return new Galette\Core\Preferences($c->get('zdb'));
194 };
195
196 $container['login'] = function ($c) {
197 $login = $c->get('session')->login;
198 if (!$login) {
199 $login = new Galette\Core\Login(
200 $c->get('zdb'),
201 $c->get('i18n'),
202 $c->get('session')
203 );
204 }
205 return $login;
206 };
207
208 $container['session'] = function ($c) {
209 $session = new \RKA\Session();
210 return $session;
211 };
212
213 $container['logo'] = function ($c) {
214 return new Galette\Core\Logo();
215 };
216
217 $container['print_logo'] = function ($c) {
218 return new Galette\Core\PrintLogo();
219 };
220
221
222 $container['history'] = function ($c) {
223 return new Galette\Core\History($c->get('zdb'), $c->get('login'));
224 };
225
226 $container['acls'] = function ($c) {
227 include_once GALETTE_ROOT . 'includes/core_acls.php';
228 $acls = $core_acls;
229
230 foreach ($c->get('plugins')->getModules() as $plugin) {
231 $acls[$plugin['route'] . 'Info'] = 'member';
232 }
233
234 //use + to be sure core ACLs are not overrided by plugins ones.
235 $acls = $acls + $c->get('plugins')->getAcls();
236
237 //load user defined ACLs
238 if (file_exists(GALETTE_CONFIG_PATH . 'local_acls.inc.php')) {
239 //use array_merge here, we want $local_acls to override core ones.
240 $acls = array_merge($acls, $local_acls);
241 }
242
243 return $acls;
244 };
245
246 $container['members_fields'] = function ($c) {
247 include_once GALETTE_ROOT . 'includes/fields_defs/members_fields.php';
248 return $members_fields;
249 };
250
251 $container['members_form_fields'] = function ($c) {
252 $fields = $c->get('members_fields');
253 foreach ($fields as $k => $field) {
254 if ($field['position'] == -1) {
255 unset($fields[$k]);
256 }
257 }
258 return $fields;
259 };
260
261
262 $container['members_fields_cats'] = function ($c) {
263 include_once GALETTE_ROOT . 'includes/fields_defs/members_fields_cats.php';
264 return $members_fields_cats;
265 };
266
267 $container['pdfmodels_fields'] = function ($c) {
268 //include_once GALETTE_ROOT . 'includes/fields_defs/pdfmodels_fields.php';
269 $pdfmodels_fields = array(
270 array(
271 'model_id' => PdfModel::MAIN_MODEL,
272 'model_name' => '_T("Main")',
273 'model_title' => null,
274 'model_type' => PdfModel::MAIN_MODEL,
275 'model_header' => '<table>
276 <tr>
277 <td id="pdf_assoname"><strong id="asso_name">{ASSO_NAME}</strong><br/>{ASSO_SLOGAN}</td>
278 <td id="pdf_logo">{ASSO_LOGO}</td>
279 </tr>
280 </table>',
281 'model_footer' => '<div id="pdf_footer">
282 _T("Association") {ASSO_NAME} - {ASSO_ADDRESS}<br/>
283 {ASSO_WEBSITE}
284 </div>',
285 'model_body' => null,
286 'model_styles' => 'div#pdf_title {
287 font-size: 1.4em;
288 font-wieght:bold;
289 text-align: center;
290 }
291
292 div#pdf_subtitle {
293 text-align: center;
294 }
295
296 div#pdf_footer {
297 text-align: center;
298 font-size: 0.7em;
299 }
300
301 td#pdf_assoname {
302 width: 75%;
303 font-size: 1.1em;
304 }
305
306 strong#asso_name {
307 font-size: 1.6em;
308 }
309
310 td#pdf_logo {
311 text-align: right;
312 width: 25%;
313 }',
314 'model_parent' => null
315 ),
316 array(
317 'model_id' => PdfModel::INVOICE_MODEL,
318 'model_name' => '_T("Invoice")',
319 'model_title' => '_T("Invoice") {CONTRIBUTION_YEAR}-{CONTRIBUTION_ID}',
320 'model_type' => PdfModel::INVOICE_MODEL,
321 'model_header' => null,
322 'model_footer' => null,
323 'model_body' => '<table>
324 <tr>
325 <td width="300"></td>
326 <td><strong>{NAME_ADH}</strong><br/>
327 {ADDRESS_ADH}<br/>
328 <strong>{ZIP_ADH} {TOWN_ADH}</strong>
329 </td>
330 </tr>
331 <tr>
332 <td height="100"></td>
333 </tr>
334 <tr>
335 <td colspan="2">
336 <table>
337 <thead>
338 <tr>
339 <th>_T("Label")</th>
340 <th>_T("Amount")</th>
341 </tr>
342 </thead>
343 <tbody>
344 <tr>
345 <td>
346 {CONTRIBUTION_LABEL} (_T("on") {CONTRIBUTION_DATE})<br/>
347 _T("from") {CONTRIBUTION_BEGIN_DATE} _T("to") {CONTRIBUTION_END_DATE}<br/>
348 {CONTRIBUTION_PAYMENT_TYPE}<br/>
349 {CONTRIBUTION_COMMENT}
350 </td>
351 <td>{CONTRIBUTION_AMOUNT}</td>
352 </tr>
353 </tbody>
354 </table>
355 </td>
356 </tr>
357 </table>',
358 'model_styles' => null,
359 'model_parent' => PdfModel::MAIN_MODEL
360 ),
361 array(
362 'model_id' => PdfModel::RECEIPT_MODEL,
363 'model_name' => '_T("Receipt")',
364 'model_title' => '_T("Receipt") {CONTRIBUTION_YEAR}-{CONTRIBUTION_ID}',
365 'model_type' => PdfModel::RECEIPT_MODEL,
366 'model_header' => null,
367 'model_footer' => null,
368 'model_body' => '<table>
369 <tr>
370 <td width="300"></td>
371 <td><strong>{NAME_ADH}</strong><br/>
372 {ADDRESS_ADH}<br/>
373 <strong>{ZIP_ADH} {TOWN_ADH}</strong>
374 </td>
375 </tr>
376 <tr>
377 <td height="100"></td>
378 </tr>
379 <tr>
380 <td colspan="2">
381 <table>
382 <thead>
383 <tr>
384 <th>_T("Label")</th>
385 <th>_T("Amount")</th>
386 </tr>
387 </thead>
388 <tbody>
389 <tr>
390 <td>
391 {CONTRIBUTION_LABEL} (_T("on") {CONTRIBUTION_DATE})<br/>
392 _T("from") {CONTRIBUTION_BEGIN_DATE} _T("to") {CONTRIBUTION_END_DATE}<br/>
393 {CONTRIBUTION_PAYMENT_TYPE}<br/>
394 {CONTRIBUTION_COMMENT}
395 </td>
396 <td>{CONTRIBUTION_AMOUNT}</td>
397 </tr>
398 </tbody>
399 </table>
400 </td>
401 </tr>
402 </table>',
403 'model_styles' => null,
404 'model_parent' => PdfModel::MAIN_MODEL
405 ),
406 array(
407 'model_id' => PdfModel::ADHESION_FORM_MODEL,
408 'model_name' => '_T("Adhesion form")',
409 'model_title' => '_T("Adhesion form")',
410 'model_type' => PdfModel::ADHESION_FORM_MODEL,
411 'model_header' => null,
412 'model_footer' => null,
413 'model_body' => '<hr/>
414 <div class="infos">_T("Complete the following form and send it with your funds, in order to complete your subscription.")</div>
415 <table>
416 <tr>
417 <td width="50%"></td>
418 <td width="50%">{ASSO_ADDRESS_MULTI}</td>
419 </tr>
420 </table>
421 <hr/>
422 <table>
423 <tr>
424 <td height="30"></td>
425 </tr>
426 <tr>
427 <td>_T("Required membership:")
428 <form action="none">
429 <input type="radio" class="box" name="cotisation" value="none1">_T("Active member")
430 <input type="radio" class="box" name="cotisation" value="none2">_T("Benefactor member")
431 <input type="radio" class="box" name="cotisation" value="none3">_T("Donation")
432 <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>
433 </form>
434 </td>
435 </tr>
436 <tr>
437 <td height="30"></td>
438 </tr>
439 </table>
440 <table class="member">
441 <tr>
442 <td class="label">_T("Politeness")</td>
443 <td class="input">{TITLE_ADH}</td>
444 </tr>
445 <tr>
446 <td class="label">_T("Name")</td>
447 <td class="input">{LAST_NAME_ADH}</td>
448 </tr>
449 <tr>
450 <td class="label">_T("First name")</td>
451 <td class="input">{FIRST_NAME_ADH}</td>
452 </tr>
453 <tr>
454 <td class="label">_T("Company name") *</td>
455 <td class="input">{COMPANY_ADH}</td>
456 </tr>
457 <tr>
458 <td class="label">_T("Address")</td>
459 <td class="input">{ADDRESS_ADH}</td>
460 </tr>
461 <tr>
462 <td class="label"></td>
463 <td class="input"></td>
464 </tr>
465 <tr>
466 <td class="label"></td>
467 <td class="input"></td>
468 </tr>
469 <tr>
470 <td class="label">_T("Zip Code")</td>
471 <td class="cpinput">{ZIP_ADH}</td>
472 <td class="label">_T("City")</td>
473 <td class="towninput">{TOWN_ADH}</td>
474 </tr>
475 <tr>
476 <td class="label">_T("Country")</td>
477 <td class="input">{COUNTRY_ADH}</td>
478 </tr>
479 <tr>
480 <td class="label">_T("Email address")</td>
481 <td class="input">{EMAIL_ADH}</td>
482 </tr>
483 <tr>
484 <td class="label">_T("Username") **</td>
485 <td class="input">{LOGIN_ADH}</td>
486 </tr>
487 <tr>
488 <td colspan="2" height="10"></td>
489 </tr>
490 <tr>
491 <td class="label">_T("Amount")</td>
492 <td class="input"></td>
493 </tr>
494 </table>
495 <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>
496 <p class="notes">_T("* Only for compagnies")<br/>_T("** Galette identifier, if applicable")</p>',
497 'model_styles' => 'td.label {
498 width: 20%;
499 font-weight: bold;
500 }
501 td.input {
502 width: 80%;
503 border-bottom: 1px dotted black;
504 }
505
506 td.cpinput {
507 width: 10%;
508 border-bottom: 1px dotted black;
509 }
510
511 td.towninput {
512 width: 50%;
513 border-bottom: 1px dotted black;
514 }
515
516 div.infos {
517 font-size: .8em;
518 }
519
520 p.notes {
521 font-size: 0.6em;
522 text-align: right;
523 }
524
525 .member td {
526 line-height: 20px;
527 height: 20px;
528 }',
529 'model_parent' => PdfModel::MAIN_MODEL
530 )
531 );
532 return $pdfmodels_fields;
533 };
534
535 // -----------------------------------------------------------------------------
536 // Service factories
537 // -----------------------------------------------------------------------------
538
539 // monolog
540 $container['logger'] = function ($c) {
541 $settings = $c->get('settings');
542 $logger = new \Monolog\Logger($settings['logger']['name']);
543 $logger->pushProcessor(new \Monolog\Processor\UidProcessor());
544 $logger->pushHandler(new \Monolog\Handler\StreamHandler($settings['logger']['path'], $settings['logger']['level']));
545 return $logger;
546 };
547
548 $container['fields_config'] = function ($c) {
549 $fc = new Galette\Entity\FieldsConfig(
550 $c->get('zdb'),
551 Galette\Entity\Adherent::TABLE,
552 $c->get('members_fields'),
553 $c->get('members_fields_cats')
554 );
555 return $fc;
556 };
557
558 $container['lists_config'] = function ($c) {
559 $fc = new Galette\Entity\ListsConfig(
560 $c->get('zdb'),
561 Galette\Entity\Adherent::TABLE,
562 $c->get('members_fields'),
563 $c->get('members_fields_cats')
564 );
565 return $fc;
566 };
567
568 $container['cache'] = function ($c) {
569 $adapter = null;
570 if (function_exists('wincache_ucache_add')) {
571 //since APCu is not known to work on windows
572 $adapter = 'wincache';
573 } elseif (function_exists('apcu_fetch')) {
574 $adapter = 'apcu';
575 }
576 if ($adapter !== null) {
577 $uuid = $c->get('mode') !== 'INSTALL' ? $c->get('preferences')->pref_instance_uuid : '_install';
578 $cache = Laminas\Cache\StorageFactory::factory([
579 'adapter' => $adapter,
580 'options' => [
581 'namespace' => str_replace(
582 ['%version', '%uuid'],
583 [GALETTE_VERSION, $uuid],
584 'galette_%version_%uuid'
585 )
586 ]
587 ]);
588 return $cache;
589 }
590 return null;
591 };
592
593 $container['translator'] = function ($c) {
594 $translator = new Galette\Core\Translator();
595
596 $domains = ['galette'];
597 foreach ($domains as $domain) {
598 //load translation file for domain
599 $translator->addTranslationFilePattern(
600 'gettext',
601 GALETTE_ROOT . '/lang/',
602 '/%s/LC_MESSAGES/' . $domain . '.mo',
603 $domain
604 );
605
606 //check if a local lang file exists and load it
607 $translator->addTranslationFilePattern(
608 'phparray',
609 GALETTE_ROOT . '/lang/',
610 $domain . '_%s_local_lang.php',
611 $domain
612 );
613 }
614
615 $translator->setLocale($c->get('i18n')->getLongID());
616 if (!isset($container['mode']) || $c->get('mode') !== 'INSTALL' && $c->get('mode') !== 'NEED_UPDATE') {
617 $translator->setCache($c->get('cache'));
618 }
619 return $translator;
620 };
621
622 // Add Event manager to dependency.
623 $container['event_manager'] = function ($c) {
624 $emitter = new SlimEventManager();
625 return $emitter;
626 };
627
628 //For bad existing globals can be used...
629 if (!isset($container['mode']) || $container['mode'] !== 'INSTALL' && $container['mode'] !== 'NEED_UPDATE') {
630 $zdb = $container->get('zdb');
631 $preferences = $container->get('preferences');
632 $login = $container->get('login');
633 $hist = $container->get('history');
634 }
635 $i18n = $container->get('i18n');
636 $translator = $container->get('translator');
637 $emitter = $container->get('event_manager');
638
639 require_once GALETTE_ROOT . 'includes/i18n.inc.php';