aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/front/controller
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-06-20 15:14:24 +0200
committerArthurHoaro <arthur@hoa.ro>2020-07-23 21:19:21 +0200
commit1b8620b1ad4e2c647ff2d032c8e7c6687b6647a1 (patch)
tree98a76bf93f9ed84680daa06050680a7c0425e535 /application/front/controller
parent78657347c5b463d7c22bfc8c87b7db39fe058833 (diff)
downloadShaarli-1b8620b1ad4e2c647ff2d032c8e7c6687b6647a1.tar.gz
Shaarli-1b8620b1ad4e2c647ff2d032c8e7c6687b6647a1.tar.zst
Shaarli-1b8620b1ad4e2c647ff2d032c8e7c6687b6647a1.zip
Process plugins administration page through Slim controllers
Diffstat (limited to 'application/front/controller')
-rw-r--r--application/front/controller/admin/PluginsController.php98
1 files changed, 98 insertions, 0 deletions
diff --git a/application/front/controller/admin/PluginsController.php b/application/front/controller/admin/PluginsController.php
new file mode 100644
index 00000000..d5ec91f0
--- /dev/null
+++ b/application/front/controller/admin/PluginsController.php
@@ -0,0 +1,98 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller\Admin;
6
7use Exception;
8use Slim\Http\Request;
9use Slim\Http\Response;
10
11/**
12 * Class PluginsController
13 *
14 * Slim controller used to handle Shaarli plugins configuration page (display + save new config).
15 */
16class 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}