]>
Commit | Line | Data |
---|---|---|
c70ff64a A |
1 | <?php |
2 | ||
3 | declare(strict_types=1); | |
4 | ||
5 | namespace Shaarli\Front\Controller\Admin; | |
6 | ||
7 | use DateTime; | |
8 | use Shaarli\Bookmark\Bookmark; | |
9 | use Slim\Http\Request; | |
10 | use Slim\Http\Response; | |
11 | ||
12 | /** | |
13 | * Class ExportController | |
14 | * | |
15 | * Slim controller used to display Shaarli data export page, | |
16 | * and process the bookmarks export as a Netscape Bookmarks file. | |
17 | */ | |
18 | class ExportController extends ShaarliAdminController | |
19 | { | |
20 | /** | |
21 | * GET /admin/export - Display export page | |
22 | */ | |
23 | public function index(Request $request, Response $response): Response | |
24 | { | |
25 | $this->assignView('pagetitle', t('Export') .' - '. $this->container->conf->get('general.title', 'Shaarli')); | |
26 | ||
27 | return $response->write($this->render('export')); | |
28 | } | |
29 | ||
30 | /** | |
31 | * POST /admin/export - Process export, and serve download file named | |
32 | * bookmarks_(all|private|public)_datetime.html | |
33 | */ | |
34 | public function export(Request $request, Response $response): Response | |
35 | { | |
36 | $selection = $request->getParam('selection'); | |
37 | ||
38 | if (empty($selection)) { | |
39 | $this->saveErrorMessage(t('Please select an export mode.')); | |
40 | ||
41 | return $this->redirect($response, '/admin/export'); | |
42 | } | |
43 | ||
44 | $prependNoteUrl = filter_var($request->getParam('prepend_note_url') ?? false, FILTER_VALIDATE_BOOLEAN); | |
45 | ||
46 | try { | |
47 | $formatter = $this->container->formatterFactory->getFormatter('raw'); | |
48 | ||
49 | $this->assignView( | |
50 | 'links', | |
51 | $this->container->netscapeBookmarkUtils->filterAndFormat( | |
52 | $formatter, | |
53 | $selection, | |
54 | $prependNoteUrl, | |
55 | index_url($this->container->environment) | |
56 | ) | |
57 | ); | |
58 | } catch (\Exception $exc) { | |
59 | $this->saveErrorMessage($exc->getMessage()); | |
60 | ||
61 | return $this->redirect($response, '/admin/export'); | |
62 | } | |
63 | ||
64 | $now = new DateTime(); | |
65 | $response = $response->withHeader('Content-Type', 'text/html; charset=utf-8'); | |
66 | $response = $response->withHeader( | |
67 | 'Content-disposition', | |
68 | 'attachment; filename=bookmarks_'.$selection.'_'.$now->format(Bookmark::LINK_DATE_FORMAT).'.html' | |
69 | ); | |
70 | ||
71 | $this->assignView('date', $now->format(DateTime::RFC822)); | |
72 | $this->assignView('eol', PHP_EOL); | |
73 | $this->assignView('selection', $selection); | |
74 | ||
75 | return $response->write($this->render('export.bookmarks')); | |
76 | } | |
77 | ||
78 | /** | |
79 | * @param mixed[] $data Variables passed to the template engine | |
80 | * | |
81 | * @return mixed[] Template data after active plugins render_picwall hook execution. | |
82 | */ | |
83 | protected function executeHooks(array $data): array | |
84 | { | |
85 | $this->container->pluginManager->executeHooks( | |
86 | 'render_tools', | |
87 | $data | |
88 | ); | |
89 | ||
90 | return $data; | |
91 | } | |
92 | } |