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