xref: /webtrees/app/Module/UserJournalModule.php (revision 4874f72da8279544d9c0a459e2920a9986acfaa0)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17declare(strict_types=1);
18
19namespace Fisharebest\Webtrees\Module;
20
21use Fisharebest\Webtrees\Auth;
22use Fisharebest\Webtrees\Carbon;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Services\HtmlService;
25use Fisharebest\Webtrees\Tree;
26use Illuminate\Database\Capsule\Manager as DB;
27use Illuminate\Support\Str;
28use Psr\Http\Message\ResponseInterface;
29use Psr\Http\Message\ServerRequestInterface;
30use stdClass;
31use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
32
33/**
34 * Class UserJournalModule
35 */
36class UserJournalModule extends AbstractModule implements ModuleBlockInterface
37{
38    use ModuleBlockTrait;
39
40    /** @var HtmlService */
41    private $html_service;
42
43    /**
44     * HtmlBlockModule bootstrap.
45     *
46     * @param HtmlService $html_service
47     */
48    public function boot(HtmlService $html_service)
49    {
50        $this->html_service = $html_service;
51    }
52
53    /**
54     * A sentence describing what this module does.
55     *
56     * @return string
57     */
58    public function description(): string
59    {
60        /* I18N: Description of the “Journal” module */
61        return I18N::translate('A private area to record notes or keep a journal.');
62    }
63
64    /**
65     * Generate the HTML content of this block.
66     *
67     * @param Tree     $tree
68     * @param int      $block_id
69     * @param string   $context
70     * @param string[] $config
71     *
72     * @return string
73     */
74    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
75    {
76        $articles = DB::table('news')
77            ->where('user_id', '=', Auth::id())
78            ->orderByDesc('updated')
79            ->get()
80            ->map(static function (stdClass $row): stdClass {
81                $row->updated = Carbon::make($row->updated);
82
83                return $row;
84            });
85
86        $content = view('modules/user_blog/list', [
87            'articles' => $articles,
88            'block_id' => $block_id,
89            'limit'    => 5,
90        ]);
91
92        if ($context !== self::CONTEXT_EMBED) {
93            return view('modules/block-template', [
94                'block'      => Str::kebab($this->name()),
95                'id'         => $block_id,
96                'config_url' => '',
97                'title'      => $this->title(),
98                'content'    => $content,
99            ]);
100        }
101
102        return $content;
103    }
104
105    /**
106     * How should this module be identified in the control panel, etc.?
107     *
108     * @return string
109     */
110    public function title(): string
111    {
112        /* I18N: Name of a module */
113        return I18N::translate('Journal');
114    }
115
116    /**
117     * Should this block load asynchronously using AJAX?
118     *
119     * Simple blocks are faster in-line, more complex ones can be loaded later.
120     *
121     * @return bool
122     */
123    public function loadAjax(): bool
124    {
125        return false;
126    }
127
128    /**
129     * Can this block be shown on the user’s home page?
130     *
131     * @return bool
132     */
133    public function isUserBlock(): bool
134    {
135        return true;
136    }
137
138    /**
139     * Can this block be shown on the tree’s home page?
140     *
141     * @return bool
142     */
143    public function isTreeBlock(): bool
144    {
145        return false;
146    }
147
148    /**
149     * @param ServerRequestInterface $request
150     *
151     * @return ResponseInterface
152     */
153    public function getEditJournalAction(ServerRequestInterface $request): ResponseInterface
154    {
155        if (!Auth::check()) {
156            throw new AccessDeniedHttpException();
157        }
158
159        $news_id = $request->getQueryParams()['news_id'] ?? '';
160
161        if ($news_id !== '') {
162            $row = DB::table('news')
163                ->where('news_id', '=', $news_id)
164                ->where('user_id', '=', Auth::id())
165                ->first();
166        } else {
167            $row = (object) [
168                'body'    => '',
169                'subject' => '',
170            ];
171        }
172
173        $title = I18N::translate('Add/edit a journal/news entry');
174
175        return $this->viewResponse('modules/user_blog/edit', [
176            'body'    => $row->body,
177            'news_id' => $news_id,
178            'subject' => $row->subject,
179            'title'   => $title,
180        ]);
181    }
182
183    /**
184     * @param ServerRequestInterface $request
185     *
186     * @return ResponseInterface
187     */
188    public function postEditJournalAction(ServerRequestInterface $request): ResponseInterface
189    {
190        $tree = $request->getAttribute('tree');
191
192        if (!Auth::check()) {
193            throw new AccessDeniedHttpException();
194        }
195
196        $news_id = $request->getQueryParams()['news_id'] ?? '';
197        $subject = $request->getParsedBody()['subject'];
198        $body    = $request->getParsedBody()['body'];
199
200        $subject = $this->html_service->sanitize($subject);
201        $body    = $this->html_service->sanitize($body);
202
203        if ($news_id !== '') {
204            DB::table('news')
205                ->where('news_id', '=', $news_id)
206                ->where('user_id', '=', Auth::id())
207                ->update([
208                    'body'    => $body,
209                    'subject' => $subject,
210                ]);
211        } else {
212            DB::table('news')->insert([
213                'body'    => $body,
214                'subject' => $subject,
215                'user_id' => Auth::id(),
216            ]);
217        }
218
219        $url = route('user-page', ['tree' => $tree->name()]);
220
221        return redirect($url);
222    }
223
224    /**
225     * @param ServerRequestInterface $request
226     *
227     * @return ResponseInterface
228     */
229    public function postDeleteJournalAction(ServerRequestInterface $request): ResponseInterface
230    {
231        $tree    = $request->getAttribute('tree');
232        $news_id = $request->getQueryParams()['news_id'];
233
234        DB::table('news')
235            ->where('news_id', '=', $news_id)
236            ->where('user_id', '=', Auth::id())
237            ->delete();
238
239        $url = route('user-page', ['tree' => $tree->name()]);
240
241        return redirect($url);
242    }
243}
244