]> git.agnieray.net Git - galette.git/blob - galette/includes/dependencies.php
e56259491a99359dbabf030778ee43b996ec180d
[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 // -----------------------------------------------------------------------------
276 // Service factories
277 // -----------------------------------------------------------------------------
278
279 // monolog
280 $container['logger'] = function ($c) {
281 $settings = $c->get('settings');
282 $logger = new \Monolog\Logger($settings['logger']['name']);
283 $logger->pushProcessor(new \Monolog\Processor\UidProcessor());
284 $logger->pushHandler(new \Monolog\Handler\StreamHandler($settings['logger']['path'], $settings['logger']['level']));
285 return $logger;
286 };
287
288 $container['fields_config'] = function ($c) {
289 $fc = new Galette\Entity\FieldsConfig(
290 $c->get('zdb'),
291 Galette\Entity\Adherent::TABLE,
292 $c->get('members_fields'),
293 $c->get('members_fields_cats')
294 );
295 return $fc;
296 };
297
298 $container['lists_config'] = function ($c) {
299 $fc = new Galette\Entity\ListsConfig(
300 $c->get('zdb'),
301 Galette\Entity\Adherent::TABLE,
302 $c->get('members_fields'),
303 $c->get('members_fields_cats')
304 );
305 return $fc;
306 };
307
308 $container['cache'] = function ($c) {
309 $adapter = null;
310 if (function_exists('wincache_ucache_add')) {
311 //since APCu is not known to work on windows
312 $adapter = 'wincache';
313 } elseif (function_exists('apcu_fetch')) {
314 $adapter = 'apcu';
315 }
316 if ($adapter !== null) {
317 $uuid = $c->get('mode') !== 'INSTALL' ? $c->get('preferences')->pref_instance_uuid : '_install';
318 $cache = Laminas\Cache\StorageFactory::factory([
319 'adapter' => $adapter,
320 'options' => [
321 'namespace' => str_replace(
322 ['%version', '%uuid'],
323 [GALETTE_VERSION, $uuid],
324 'galette_%version_%uuid'
325 )
326 ]
327 ]);
328 return $cache;
329 }
330 return null;
331 };
332
333 $container['translator'] = function ($c) {
334 $translator = new Galette\Core\Translator();
335
336 $domains = ['galette'];
337 foreach ($domains as $domain) {
338 //load translation file for domain
339 $translator->addTranslationFilePattern(
340 'gettext',
341 GALETTE_ROOT . '/lang/',
342 '/%s/LC_MESSAGES/' . $domain . '.mo',
343 $domain
344 );
345
346 //check if a local lang file exists and load it
347 $translator->addTranslationFilePattern(
348 'phparray',
349 GALETTE_ROOT . '/lang/',
350 $domain . '_%s_local_lang.php',
351 $domain
352 );
353 }
354
355 $translator->setLocale($c->get('i18n')->getLongID());
356 if (!isset($container['mode']) || $c->get('mode') !== 'INSTALL' && $c->get('mode') !== 'NEED_UPDATE') {
357 $translator->setCache($c->get('cache'));
358 }
359 return $translator;
360 };
361
362 // Add Event manager to dependency.
363 $container['event_manager'] = function ($c) {
364 $emitter = new SlimEventManager();
365
366 $emitter->useListenerProvider(
367 new Galette\Events\MemberListener(
368 $c->get('preferences'),
369 $c->get('router'),
370 $c->get('history'),
371 $c->get('flash'),
372 $c->get('login'),
373 $c->get('zdb')
374 )
375 );
376
377 $emitter->useListenerProvider(
378 new Galette\Events\ContribListener(
379 $c->get('preferences'),
380 $c->get('router'),
381 $c->get('history'),
382 $c->get('flash'),
383 $c->get('login'),
384 $c->get('zdb')
385 )
386 );
387
388
389 return $emitter;
390 };
391
392 //For bad existing globals can be used...
393 if (!isset($container['mode']) || ($container['mode'] !== 'INSTALL' && $container['mode'] !== 'NEED_UPDATE')) {
394 $zdb = $container->get('zdb');
395 $preferences = $container->get('preferences');
396 $login = $container->get('login');
397 $hist = $container->get('history');
398 global $l10n;
399 $l10n = $container->get('l10n');
400 $emitter = $container->get('event_manager');
401 }
402 global $translator, $i18n;
403 $i18n = $container->get('i18n');
404 $translator = $container->get('translator');
405
406 require_once GALETTE_ROOT . 'includes/i18n.inc.php';