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