xref: /webtrees/app/Module/FamilyTreeNewsModule.php (revision b37c98e1e5caf7a6c6899a570b7ab9f7b523fc51)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Module;
19
20use Fisharebest\Webtrees\Auth;
21use Fisharebest\Webtrees\Carbon;
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Services\HtmlService;
24use Fisharebest\Webtrees\Tree;
25use Illuminate\Database\Capsule\Manager as DB;
26use Illuminate\Support\Str;
27use Psr\Http\Message\ResponseInterface;
28use Psr\Http\Message\ServerRequestInterface;
29use stdClass;
30use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
31
32/**
33 * Class FamilyTreeNewsModule
34 */
35class FamilyTreeNewsModule extends AbstractModule implements ModuleBlockInterface
36{
37    use ModuleBlockTrait;
38
39    /** @var HtmlService */
40    private $html_service;
41
42    /**
43     * HtmlBlockModule bootstrap.
44     *
45     * @param HtmlService $html_service
46     */
47    public function boot(HtmlService $html_service)
48    {
49        $this->html_service = $html_service;
50    }
51
52    /**
53     * A sentence describing what this module does.
54     *
55     * @return string
56     */
57    public function description(): string
58    {
59        /* I18N: Description of the “News” module */
60        return I18N::translate('Family news and site announcements.');
61    }
62
63    /**
64     * Generate the HTML content of this block.
65     *
66     * @param Tree     $tree
67     * @param int      $block_id
68     * @param string   $context
69     * @param string[] $config
70     *
71     * @return string
72     */
73    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
74    {
75        $articles = DB::table('news')
76            ->where('gedcom_id', '=', $tree->id())
77            ->orderByDesc('updated')
78            ->get()
79            ->map(static function (stdClass $row): stdClass {
80                $row->updated = Carbon::make($row->updated);
81
82                return $row;
83            });
84
85        $content = view('modules/gedcom_news/list', [
86            'articles' => $articles,
87            'block_id' => $block_id,
88            'limit'    => 5,
89        ]);
90
91        if ($context !== self::CONTEXT_EMBED) {
92            return view('modules/block-template', [
93                'block'      => Str::kebab($this->name()),
94                'id'         => $block_id,
95                'config_url' => '',
96                'title'      => $this->title(),
97                'content'    => $content,
98            ]);
99        }
100
101        return $content;
102    }
103
104    /**
105     * How should this module be identified in the control panel, etc.?
106     *
107     * @return string
108     */
109    public function title(): string
110    {
111        /* I18N: Name of a module */
112        return I18N::translate('News');
113    }
114
115    /**
116     * Should this block load asynchronously using AJAX?
117     *
118     * Simple blocks are faster in-line, more complex ones can be loaded later.
119     *
120     * @return bool
121     */
122    public function loadAjax(): bool
123    {
124        return false;
125    }
126
127    /**
128     * Can this block be shown on the user’s home page?
129     *
130     * @return bool
131     */
132    public function isUserBlock(): bool
133    {
134        return false;
135    }
136
137    /**
138     * Can this block be shown on the tree’s home page?
139     *
140     * @return bool
141     */
142    public function isTreeBlock(): bool
143    {
144        return true;
145    }
146
147    /**
148     * @param ServerRequestInterface $request
149     * @param Tree                   $tree
150     *
151     * @return ResponseInterface
152     */
153    public function getEditNewsAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
154    {
155        if (!Auth::isManager($tree)) {
156            throw new AccessDeniedHttpException();
157        }
158
159        $news_id = $request->getQueryParams()['news_id'] ?? '';
160
161        if ($news_id !== '') {
162            $row = DB::table('news')
163                ->where('news_id', '=', $news_id)
164                ->where('gedcom_id', '=', $tree->id())
165                ->first();
166        } else {
167            $row = (object) [
168                'body'    => '',
169                'subject' => '',
170            ];
171        }
172
173        $title = I18N::translate('Add/edit a journal/news entry');
174
175        return $this->viewResponse('modules/gedcom_news/edit', [
176            'body'    => $row->body,
177            'news_id' => $news_id,
178            'subject' => $row->subject,
179            'title'   => $title,
180        ]);
181    }
182
183    /**
184     * @param ServerRequestInterface $request
185     * @param Tree                   $tree
186     *
187     * @return ResponseInterface
188     */
189    public function postEditNewsAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
190    {
191        if (!Auth::isManager($tree)) {
192            throw new AccessDeniedHttpException();
193        }
194
195        $news_id = $request->getQueryParams()['news_id'] ?? '';
196        $subject = $request->getParsedBody()['subject'];
197        $body    = $request->getParsedBody()['body'];
198
199        $subject = $this->html_service->sanitize($subject);
200        $body    = $this->html_service->sanitize($body);
201
202        if ($news_id > 0) {
203            DB::table('news')
204                ->where('news_id', '=', $news_id)
205                ->where('gedcom_id', '=', $tree->id())
206                ->update([
207                    'body'    => $body,
208                    'subject' => $subject,
209                ]);
210        } else {
211            DB::table('news')->insert([
212                'body'      => $body,
213                'subject'   => $subject,
214                'gedcom_id' => $tree->id(),
215            ]);
216        }
217
218        $url = route('tree-page', [
219            'ged' => $tree->name(),
220        ]);
221
222        return redirect($url);
223    }
224
225    /**
226     * @param ServerRequestInterface $request
227     * @param Tree                   $tree
228     *
229     * @return ResponseInterface
230     */
231    public function postDeleteNewsAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
232    {
233        $news_id = $request->getQueryParams()['news_id'];
234
235        if (!Auth::isManager($tree)) {
236            throw new AccessDeniedHttpException();
237        }
238
239        DB::table('news')
240            ->where('news_id', '=', $news_id)
241            ->where('gedcom_id', '=', $tree->id())
242            ->delete();
243
244        $url = route('tree-page', [
245            'ged' => $tree->name(),
246        ]);
247
248        return redirect($url);
249    }
250}
251