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