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