xref: /webtrees/app/Module/FamilyTreeNewsModule.php (revision e60d40369c3d7739ac89b1d48a13da904510d04e)
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   $ctype
69     * @param string[] $cfg
70     *
71     * @return string
72     */
73    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): 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 ($ctype !== '') {
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 comples ones
119     * 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     * Update the configuration for a block.
150     *
151     * @param ServerRequestInterface $request
152     * @param int     $block_id
153     *
154     * @return void
155     */
156    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
157    {
158    }
159
160    /**
161     * An HTML form to edit block settings
162     *
163     * @param Tree $tree
164     * @param int  $block_id
165     *
166     * @return void
167     */
168    public function editBlockConfiguration(Tree $tree, int $block_id): void
169    {
170    }
171
172    /**
173     * @param ServerRequestInterface $request
174     * @param Tree                   $tree
175     *
176     * @return ResponseInterface
177     */
178    public function getEditNewsAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
179    {
180        if (!Auth::isManager($tree)) {
181            throw new AccessDeniedHttpException();
182        }
183
184        $news_id = $request->getQueryParams()['news_id'] ?? '';
185
186        if ($news_id !== '') {
187            $row = DB::table('news')
188                ->where('news_id', '=', $news_id)
189                ->where('gedcom_id', '=', $tree->id())
190                ->first();
191        } else {
192            $row = (object) [
193                'body'    => '',
194                'subject' => '',
195            ];
196        }
197
198        $title = I18N::translate('Add/edit a journal/news entry');
199
200        return $this->viewResponse('modules/gedcom_news/edit', [
201            'body'    => $row->body,
202            'news_id' => $news_id,
203            'subject' => $row->subject,
204            'title'   => $title,
205        ]);
206    }
207
208    /**
209     * @param ServerRequestInterface $request
210     * @param Tree                   $tree
211     *
212     * @return ResponseInterface
213     */
214    public function postEditNewsAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
215    {
216        if (!Auth::isManager($tree)) {
217            throw new AccessDeniedHttpException();
218        }
219
220        $news_id = $request->getQueryParams()['news_id'] ?? '';
221        $subject = $request->getParsedBody()['subject'];
222        $body    = $request->getParsedBody()['body'];
223
224        $subject = $this->html_service->sanitize($subject);
225        $body    = $this->html_service->sanitize($body);
226
227        if ($news_id > 0) {
228            DB::table('news')
229                ->where('news_id', '=', $news_id)
230                ->where('gedcom_id', '=', $tree->id())
231                ->update([
232                    'body'    => $body,
233                    'subject' => $subject,
234                ]);
235        } else {
236            DB::table('news')->insert([
237                'body'      => $body,
238                'subject'   => $subject,
239                'gedcom_id' => $tree->id(),
240            ]);
241        }
242
243        $url = route('tree-page', [
244            'ged' => $tree->name(),
245        ]);
246
247        return redirect($url);
248    }
249
250    /**
251     * @param ServerRequestInterface $request
252     * @param Tree                   $tree
253     *
254     * @return ResponseInterface
255     */
256    public function postDeleteNewsAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
257    {
258        $news_id = $request->getQueryParams()['news_id'];
259
260        if (!Auth::isManager($tree)) {
261            throw new AccessDeniedHttpException();
262        }
263
264        DB::table('news')
265            ->where('news_id', '=', $news_id)
266            ->where('gedcom_id', '=', $tree->id())
267            ->delete();
268
269        $url = route('tree-page', [
270            'ged' => $tree->name(),
271        ]);
272
273        return redirect($url);
274    }
275}
276