]>
Commit | Line | Data |
---|---|---|
6c50a6cc A |
1 | <?php |
2 | ||
3 | declare(strict_types=1); | |
4 | ||
5 | namespace Shaarli\Front\Controller; | |
6 | ||
0498b209 | 7 | use Shaarli\Bookmark\BookmarkFilter; |
6c50a6cc A |
8 | use Shaarli\Container\ShaarliContainer; |
9 | ||
10 | abstract class ShaarliController | |
11 | { | |
12 | /** @var ShaarliContainer */ | |
27ceea2a | 13 | protected $container; |
6c50a6cc | 14 | |
27ceea2a A |
15 | /** @param ShaarliContainer $container Slim container (extended for attribute completion). */ |
16 | public function __construct(ShaarliContainer $container) | |
6c50a6cc | 17 | { |
27ceea2a | 18 | $this->container = $container; |
6c50a6cc A |
19 | } |
20 | ||
21 | /** | |
22 | * Assign variables to RainTPL template through the PageBuilder. | |
23 | * | |
24 | * @param mixed $value Value to assign to the template | |
25 | */ | |
26 | protected function assignView(string $name, $value): self | |
27 | { | |
27ceea2a | 28 | $this->container->pageBuilder->assign($name, $value); |
6c50a6cc A |
29 | |
30 | return $this; | |
31 | } | |
0498b209 | 32 | |
7b2ba6ef A |
33 | /** |
34 | * Assign variables to RainTPL template through the PageBuilder. | |
35 | * | |
36 | * @param mixed $data Values to assign to the template and their keys | |
37 | */ | |
38 | protected function assignAllView(array $data): self | |
39 | { | |
40 | foreach ($data as $key => $value) { | |
41 | $this->assignView($key, $value); | |
42 | } | |
43 | ||
44 | return $this; | |
45 | } | |
46 | ||
0498b209 A |
47 | protected function render(string $template): string |
48 | { | |
27ceea2a A |
49 | $this->assignView('linkcount', $this->container->bookmarkService->count(BookmarkFilter::$ALL)); |
50 | $this->assignView('privateLinkcount', $this->container->bookmarkService->count(BookmarkFilter::$PRIVATE)); | |
51 | $this->assignView('plugin_errors', $this->container->pluginManager->getErrors()); | |
0498b209 A |
52 | |
53 | $this->executeDefaultHooks($template); | |
54 | ||
27ceea2a | 55 | return $this->container->pageBuilder->render($template); |
0498b209 A |
56 | } |
57 | ||
58 | /** | |
59 | * Call plugin hooks for header, footer and includes, specifying which page will be rendered. | |
60 | * Then assign generated data to RainTPL. | |
61 | */ | |
62 | protected function executeDefaultHooks(string $template): void | |
63 | { | |
64 | $common_hooks = [ | |
65 | 'includes', | |
66 | 'header', | |
67 | 'footer', | |
68 | ]; | |
69 | ||
70 | foreach ($common_hooks as $name) { | |
71 | $plugin_data = []; | |
27ceea2a | 72 | $this->container->pluginManager->executeHooks( |
0498b209 A |
73 | 'render_' . $name, |
74 | $plugin_data, | |
75 | [ | |
76 | 'target' => $template, | |
27ceea2a | 77 | 'loggedin' => $this->container->loginManager->isLoggedIn() |
0498b209 A |
78 | ] |
79 | ); | |
80 | $this->assignView('plugins_' . $name, $plugin_data); | |
81 | } | |
82 | } | |
6c50a6cc | 83 | } |