xref: /webtrees/app/Module/UserJournalModule.php (revision 9b93b7c342a543e7bd55b5a09d97ba0c39fc32b0)
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   $ctype
69     * @param string[] $cfg
70     *
71     * @return string
72     */
73    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): 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 ($ctype !== '') {
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 comples ones
119     * 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     * Update the configuration for a block.
150     *
151     * @param ServerRequestInterface $request
152     * @param int     $block_id
153     *
154     * @return void
155     */
156    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
157    {
158    }
159
160    /**
161     * An HTML form to edit block settings
162     *
163     * @param Tree $tree
164     * @param int  $block_id
165     *
166     * @return void
167     */
168    public function editBlockConfiguration(Tree $tree, int $block_id): void
169    {
170    }
171
172    /**
173     * @param ServerRequestInterface $request
174     *
175     * @return ResponseInterface
176     */
177    public function getEditJournalAction(ServerRequestInterface $request): ResponseInterface
178    {
179        if (!Auth::check()) {
180            throw new AccessDeniedHttpException();
181        }
182
183        $news_id = $request->getQueryParams()['news_id'] ?? '';
184
185        if ($news_id !== '') {
186            $row = DB::table('news')
187                ->where('news_id', '=', $news_id)
188                ->where('user_id', '=', Auth::id())
189                ->first();
190        } else {
191            $row = (object) [
192                'body'    => '',
193                'subject' => '',
194            ];
195        }
196
197        $title = I18N::translate('Add/edit a journal/news entry');
198
199        return $this->viewResponse('modules/user_blog/edit', [
200            'body'    => $row->body,
201            'news_id' => $news_id,
202            'subject' => $row->subject,
203            'title'   => $title,
204        ]);
205    }
206
207    /**
208     * @param ServerRequestInterface $request
209     * @param Tree                   $tree
210     *
211     * @return ResponseInterface
212     */
213    public function postEditJournalAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
214    {
215        if (!Auth::check()) {
216            throw new AccessDeniedHttpException();
217        }
218
219        $news_id = $request->getQueryParams()['news_id'] ?? '';
220        $subject = $request->getParsedBody()['subject'];
221        $body    = $request->getParsedBody()['body'];
222
223        $subject = $this->html_service->sanitize($subject);
224        $body    = $this->html_service->sanitize($body);
225
226        if ($news_id !== '') {
227            DB::table('news')
228                ->where('news_id', '=', $news_id)
229                ->where('user_id', '=', Auth::id())
230                ->update([
231                    'body'    => $body,
232                    'subject' => $subject,
233                ]);
234        } else {
235            DB::table('news')->insert([
236                'body'    => $body,
237                'subject' => $subject,
238                'user_id' => Auth::id(),
239            ]);
240        }
241
242        $url = route('user-page', [
243            'ged' => $tree->name(),
244        ]);
245
246        return redirect($url);
247    }
248
249    /**
250     * @param ServerRequestInterface $request
251     * @param Tree                   $tree
252     *
253     * @return ResponseInterface
254     */
255    public function postDeleteJournalAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
256    {
257        $news_id = $request->getQueryParams()['news_id'];
258
259        DB::table('news')
260            ->where('news_id', '=', $news_id)
261            ->where('user_id', '=', Auth::id())
262            ->delete();
263
264        $url = route('user-page', [
265            'ged' => $tree->name(),
266        ]);
267
268        return redirect($url);
269    }
270}
271