xref: /webtrees/app/Module/UserJournalModule.php (revision 600daa5d4fa729dbec2d21fb3adffed0ae6efda9)
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 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\Carbon;
24use Fisharebest\Webtrees\Exceptions\HttpAccessDeniedException;
25use Fisharebest\Webtrees\Http\RequestHandlers\UserPage;
26use Fisharebest\Webtrees\I18N;
27use Fisharebest\Webtrees\Services\HtmlService;
28use Fisharebest\Webtrees\Tree;
29use Illuminate\Database\Capsule\Manager as DB;
30use Illuminate\Support\Str;
31use Psr\Http\Message\ResponseInterface;
32use Psr\Http\Message\ServerRequestInterface;
33use stdClass;
34
35use function assert;
36
37/**
38 * Class UserJournalModule
39 */
40class UserJournalModule extends AbstractModule implements ModuleBlockInterface
41{
42    use ModuleBlockTrait;
43
44    /** @var HtmlService */
45    private $html_service;
46
47    /**
48     * HtmlBlockModule constructor.
49     *
50     * @param HtmlService $html_service
51     */
52    public function __construct(HtmlService $html_service)
53    {
54        $this->html_service = $html_service;
55    }
56
57    /**
58     * A sentence describing what this module does.
59     *
60     * @return string
61     */
62    public function description(): string
63    {
64        /* I18N: Description of the “Journal” module */
65        return I18N::translate('A private area to record notes or keep a journal.');
66    }
67
68    /**
69     * Generate the HTML content of this block.
70     *
71     * @param Tree     $tree
72     * @param int      $block_id
73     * @param string   $context
74     * @param string[] $config
75     *
76     * @return string
77     */
78    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
79    {
80        $articles = DB::table('news')
81            ->where('user_id', '=', Auth::id())
82            ->orderByDesc('updated')
83            ->get()
84            ->map(static function (stdClass $row): stdClass {
85                $row->updated = Carbon::make($row->updated);
86
87                return $row;
88            });
89
90        $content = view('modules/user_blog/list', [
91            'articles' => $articles,
92            'block_id' => $block_id,
93            'limit'    => 5,
94            'tree'     => $tree,
95        ]);
96
97        if ($context !== self::CONTEXT_EMBED) {
98            return view('modules/block-template', [
99                'block'      => Str::kebab($this->name()),
100                'id'         => $block_id,
101                'config_url' => '',
102                'title'      => $this->title(),
103                'content'    => $content,
104            ]);
105        }
106
107        return $content;
108    }
109
110    /**
111     * How should this module be identified in the control panel, etc.?
112     *
113     * @return string
114     */
115    public function title(): string
116    {
117        /* I18N: Name of a module */
118        return I18N::translate('Journal');
119    }
120
121    /**
122     * Should this block load asynchronously using AJAX?
123     *
124     * Simple blocks are faster in-line, more complex ones can be loaded later.
125     *
126     * @return bool
127     */
128    public function loadAjax(): bool
129    {
130        return false;
131    }
132
133    /**
134     * Can this block be shown on the user’s home page?
135     *
136     * @return bool
137     */
138    public function isUserBlock(): bool
139    {
140        return true;
141    }
142
143    /**
144     * Can this block be shown on the tree’s home page?
145     *
146     * @return bool
147     */
148    public function isTreeBlock(): bool
149    {
150        return false;
151    }
152
153    /**
154     * @param ServerRequestInterface $request
155     *
156     * @return ResponseInterface
157     */
158    public function getEditJournalAction(ServerRequestInterface $request): ResponseInterface
159    {
160        $tree = $request->getAttribute('tree');
161        assert($tree instanceof Tree);
162
163        if (!Auth::check()) {
164            throw new HttpAccessDeniedException();
165        }
166
167        $news_id = $request->getQueryParams()['news_id'] ?? '';
168
169        if ($news_id !== '') {
170            $row = DB::table('news')
171                ->where('news_id', '=', $news_id)
172                ->where('user_id', '=', Auth::id())
173                ->first();
174        } else {
175            $row = (object) [
176                'body'    => '',
177                'subject' => '',
178            ];
179        }
180
181        $title = I18N::translate('Add/edit a journal/news entry');
182
183        return $this->viewResponse('modules/user_blog/edit', [
184            'body'    => $row->body,
185            'news_id' => $news_id,
186            'subject' => $row->subject,
187            'title'   => $title,
188            'tree'    => $tree,
189        ]);
190    }
191
192    /**
193     * @param ServerRequestInterface $request
194     *
195     * @return ResponseInterface
196     */
197    public function postEditJournalAction(ServerRequestInterface $request): ResponseInterface
198    {
199        $tree = $request->getAttribute('tree');
200        assert($tree instanceof Tree);
201
202        if (!Auth::check()) {
203            throw new HttpAccessDeniedException();
204        }
205
206        $params = (array) $request->getParsedBody();
207
208        $news_id = $request->getQueryParams()['news_id'] ?? '';
209        $subject = $params['subject'];
210        $body    = $params['body'];
211
212        $subject = $this->html_service->sanitize($subject);
213        $body    = $this->html_service->sanitize($body);
214
215        if ($news_id !== '') {
216            DB::table('news')
217                ->where('news_id', '=', $news_id)
218                ->where('user_id', '=', Auth::id())
219                ->update([
220                    'body'    => $body,
221                    'subject' => $subject,
222                ]);
223        } else {
224            DB::table('news')->insert([
225                'body'    => $body,
226                'subject' => $subject,
227                'user_id' => Auth::id(),
228            ]);
229        }
230
231        $url = route(UserPage::class, ['tree' => $tree->name()]);
232
233        return redirect($url);
234    }
235
236    /**
237     * @param ServerRequestInterface $request
238     *
239     * @return ResponseInterface
240     */
241    public function postDeleteJournalAction(ServerRequestInterface $request): ResponseInterface
242    {
243        $tree = $request->getAttribute('tree');
244        assert($tree instanceof Tree);
245
246        $news_id = $request->getQueryParams()['news_id'];
247
248        DB::table('news')
249            ->where('news_id', '=', $news_id)
250            ->where('user_id', '=', Auth::id())
251            ->delete();
252
253        $url = route(UserPage::class, ['tree' => $tree->name()]);
254
255        return redirect($url);
256    }
257}
258