xref: /webtrees/app/Module/FamilyTreeNewsModule.php (revision 90a2f71801fb87cd8a5109b7c84d162b5549b38c)
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\I18N;
25use Fisharebest\Webtrees\Services\HtmlService;
26use Fisharebest\Webtrees\Tree;
27use Illuminate\Database\Capsule\Manager as DB;
28use Illuminate\Support\Str;
29use Psr\Http\Message\ResponseInterface;
30use Psr\Http\Message\ServerRequestInterface;
31use stdClass;
32use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
33
34use function assert;
35
36/**
37 * Class FamilyTreeNewsModule
38 */
39class FamilyTreeNewsModule extends AbstractModule implements ModuleBlockInterface
40{
41    use ModuleBlockTrait;
42
43    /** @var HtmlService */
44    private $html_service;
45
46    /**
47     * HtmlBlockModule bootstrap.
48     *
49     * @param HtmlService $html_service
50     */
51    public function boot(HtmlService $html_service): void
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 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 (stdClass $row): stdClass {
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        ]);
94
95        if ($context !== self::CONTEXT_EMBED) {
96            return view('modules/block-template', [
97                'block'      => Str::kebab($this->name()),
98                'id'         => $block_id,
99                'config_url' => '',
100                'title'      => $this->title(),
101                'content'    => $content,
102            ]);
103        }
104
105        return $content;
106    }
107
108    /**
109     * How should this module be identified in the control panel, etc.?
110     *
111     * @return string
112     */
113    public function title(): string
114    {
115        /* I18N: Name of a module */
116        return I18N::translate('News');
117    }
118
119    /**
120     * Should this block load asynchronously using AJAX?
121     *
122     * Simple blocks are faster in-line, more complex ones can be loaded later.
123     *
124     * @return bool
125     */
126    public function loadAjax(): bool
127    {
128        return false;
129    }
130
131    /**
132     * Can this block be shown on the user’s home page?
133     *
134     * @return bool
135     */
136    public function isUserBlock(): bool
137    {
138        return false;
139    }
140
141    /**
142     * Can this block be shown on the tree’s home page?
143     *
144     * @return bool
145     */
146    public function isTreeBlock(): bool
147    {
148        return true;
149    }
150
151    /**
152     * @param ServerRequestInterface $request
153     *
154     * @return ResponseInterface
155     */
156    public function getEditNewsAction(ServerRequestInterface $request): ResponseInterface
157    {
158        $tree = $request->getAttribute('tree');
159        assert($tree instanceof Tree);
160
161        if (!Auth::isManager($tree)) {
162            throw new AccessDeniedHttpException();
163        }
164
165        $news_id = $request->getQueryParams()['news_id'] ?? '';
166
167        if ($news_id !== '') {
168            $row = DB::table('news')
169                ->where('news_id', '=', $news_id)
170                ->where('gedcom_id', '=', $tree->id())
171                ->first();
172        } else {
173            $row = (object) [
174                'body'    => '',
175                'subject' => '',
176            ];
177        }
178
179        $title = I18N::translate('Add/edit a journal/news entry');
180
181        return $this->viewResponse('modules/gedcom_news/edit', [
182            'body'    => $row->body,
183            'news_id' => $news_id,
184            'subject' => $row->subject,
185            'title'   => $title,
186        ]);
187    }
188
189    /**
190     * @param ServerRequestInterface $request
191     *
192     * @return ResponseInterface
193     */
194    public function postEditNewsAction(ServerRequestInterface $request): ResponseInterface
195    {
196        $tree = $request->getAttribute('tree');
197        assert($tree instanceof Tree);
198
199        if (!Auth::isManager($tree)) {
200            throw new AccessDeniedHttpException();
201        }
202
203        $news_id = $request->getQueryParams()['news_id'] ?? '';
204        $subject = $request->getParsedBody()['subject'];
205        $body    = $request->getParsedBody()['body'];
206
207        $subject = $this->html_service->sanitize($subject);
208        $body    = $this->html_service->sanitize($body);
209
210        if ($news_id > 0) {
211            DB::table('news')
212                ->where('news_id', '=', $news_id)
213                ->where('gedcom_id', '=', $tree->id())
214                ->update([
215                    'body'    => $body,
216                    'subject' => $subject,
217                ]);
218        } else {
219            DB::table('news')->insert([
220                'body'      => $body,
221                'subject'   => $subject,
222                'gedcom_id' => $tree->id(),
223            ]);
224        }
225
226        $url = route('tree-page', ['tree' => $tree->name()]);
227
228        return redirect($url);
229    }
230
231    /**
232     * @param ServerRequestInterface $request
233     *
234     * @return ResponseInterface
235     */
236    public function postDeleteNewsAction(ServerRequestInterface $request): ResponseInterface
237    {
238        $tree    = $request->getAttribute('tree');
239        $news_id = $request->getQueryParams()['news_id'];
240
241        if (!Auth::isManager($tree)) {
242            throw new AccessDeniedHttpException();
243        }
244
245        DB::table('news')
246            ->where('news_id', '=', $news_id)
247            ->where('gedcom_id', '=', $tree->id())
248            ->delete();
249
250        $url = route('tree-page', ['tree' => $tree->name()]);
251
252        return redirect($url);
253    }
254}
255