]>
Commit | Line | Data |
---|---|---|
1b8620b1 A |
1 | <?php |
2 | ||
3 | declare(strict_types=1); | |
4 | ||
5 | namespace Shaarli\Front\Controller\Admin; | |
6 | ||
7 | use Exception; | |
8 | use Slim\Http\Request; | |
9 | use Slim\Http\Response; | |
10 | ||
11 | /** | |
12 | * Class PluginsController | |
13 | * | |
14 | * Slim controller used to handle Shaarli plugins configuration page (display + save new config). | |
15 | */ | |
16 | class PluginsController extends ShaarliAdminController | |
17 | { | |
18 | /** | |
19 | * GET /admin/plugins - Displays the configuration page | |
20 | */ | |
21 | public function index(Request $request, Response $response): Response | |
22 | { | |
23 | $pluginMeta = $this->container->pluginManager->getPluginsMeta(); | |
24 | ||
25 | // Split plugins into 2 arrays: ordered enabled plugins and disabled. | |
26 | $enabledPlugins = array_filter($pluginMeta, function ($v) { | |
27 | return ($v['order'] ?? false) !== false; | |
28 | }); | |
29 | $enabledPlugins = load_plugin_parameter_values($enabledPlugins, $this->container->conf->get('plugins', [])); | |
30 | uasort( | |
31 | $enabledPlugins, | |
32 | function ($a, $b) { | |
33 | return $a['order'] - $b['order']; | |
34 | } | |
35 | ); | |
36 | $disabledPlugins = array_filter($pluginMeta, function ($v) { | |
37 | return ($v['order'] ?? false) === false; | |
38 | }); | |
39 | ||
40 | $this->assignView('enabledPlugins', $enabledPlugins); | |
41 | $this->assignView('disabledPlugins', $disabledPlugins); | |
42 | $this->assignView( | |
43 | 'pagetitle', | |
44 | t('Plugin Administration') .' - '. $this->container->conf->get('general.title', 'Shaarli') | |
45 | ); | |
46 | ||
47 | return $response->write($this->render('pluginsadmin')); | |
48 | } | |
49 | ||
50 | /** | |
51 | * POST /admin/plugins - Update Shaarli's configuration | |
52 | */ | |
53 | public function save(Request $request, Response $response): Response | |
54 | { | |
55 | $this->checkToken($request); | |
56 | ||
57 | try { | |
58 | $parameters = $request->getParams() ?? []; | |
59 | ||
60 | $this->executeHooks($parameters); | |
61 | ||
62 | if (isset($parameters['parameters_form'])) { | |
63 | unset($parameters['parameters_form']); | |
64 | foreach ($parameters as $param => $value) { | |
65 | $this->container->conf->set('plugins.'. $param, escape($value)); | |
66 | } | |
67 | } else { | |
68 | $this->container->conf->set('general.enabled_plugins', save_plugin_config($parameters)); | |
69 | } | |
70 | ||
71 | $this->container->conf->write($this->container->loginManager->isLoggedIn()); | |
72 | $this->container->history->updateSettings(); | |
73 | ||
74 | $this->saveSuccessMessage(t('Setting successfully saved.')); | |
75 | } catch (Exception $e) { | |
76 | $this->saveErrorMessage( | |
77 | t('ERROR while saving plugin configuration: ') . PHP_EOL . $e->getMessage() | |
78 | ); | |
79 | } | |
80 | ||
81 | return $this->redirect($response, '/admin/plugins'); | |
82 | } | |
83 | ||
84 | /** | |
85 | * @param mixed[] $data Variables passed to the template engine | |
86 | * | |
87 | * @return mixed[] Template data after active plugins render_picwall hook execution. | |
88 | */ | |
89 | protected function executeHooks(array $data): array | |
90 | { | |
91 | $this->container->pluginManager->executeHooks( | |
92 | 'save_plugin_parameters', | |
93 | $data | |
94 | ); | |
95 | ||
96 | return $data; | |
97 | } | |
98 | } |