xref: /webtrees/app/Module/FamilyTreeNewsModule.php (revision 6b1381a306f9b3ddbe167a71b6a607d5ab7a609d)
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\Tree;
24use Illuminate\Database\Capsule\Manager as DB;
25use Illuminate\Support\Str;
26use Psr\Http\Message\ResponseInterface;
27use Psr\Http\Message\ServerRequestInterface;
28use stdClass;
29use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
30
31/**
32 * Class FamilyTreeNewsModule
33 */
34class FamilyTreeNewsModule extends AbstractModule implements ModuleBlockInterface
35{
36    use ModuleBlockTrait;
37
38    /**
39     * A sentence describing what this module does.
40     *
41     * @return string
42     */
43    public function description(): string
44    {
45        /* I18N: Description of the “News” module */
46        return I18N::translate('Family news and site announcements.');
47    }
48
49    /**
50     * Generate the HTML content of this block.
51     *
52     * @param Tree     $tree
53     * @param int      $block_id
54     * @param string   $ctype
55     * @param string[] $cfg
56     *
57     * @return string
58     */
59    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
60    {
61        $articles = DB::table('news')
62            ->where('gedcom_id', '=', $tree->id())
63            ->orderByDesc('updated')
64            ->get()
65            ->map(static function (stdClass $row): stdClass {
66                $row->updated = Carbon::make($row->updated);
67
68                return $row;
69            });
70
71        $content = view('modules/gedcom_news/list', [
72            'articles' => $articles,
73            'block_id' => $block_id,
74            'limit'    => 5,
75        ]);
76
77        if ($ctype !== '') {
78            return view('modules/block-template', [
79                'block'      => Str::kebab($this->name()),
80                'id'         => $block_id,
81                'config_url' => '',
82                'title'      => $this->title(),
83                'content'    => $content,
84            ]);
85        }
86
87        return $content;
88    }
89
90    /**
91     * How should this module be identified in the control panel, etc.?
92     *
93     * @return string
94     */
95    public function title(): string
96    {
97        /* I18N: Name of a module */
98        return I18N::translate('News');
99    }
100
101    /** {@inheritdoc} */
102    public function loadAjax(): bool
103    {
104        return false;
105    }
106
107    /** {@inheritdoc} */
108    public function isUserBlock(): bool
109    {
110        return false;
111    }
112
113    /** {@inheritdoc} */
114    public function isTreeBlock(): bool
115    {
116        return true;
117    }
118
119    /**
120     * Update the configuration for a block.
121     *
122     * @param ServerRequestInterface $request
123     * @param int     $block_id
124     *
125     * @return void
126     */
127    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
128    {
129    }
130
131    /**
132     * An HTML form to edit block settings
133     *
134     * @param Tree $tree
135     * @param int  $block_id
136     *
137     * @return void
138     */
139    public function editBlockConfiguration(Tree $tree, int $block_id): void
140    {
141    }
142
143    /**
144     * @param ServerRequestInterface $request
145     * @param Tree                   $tree
146     *
147     * @return ResponseInterface
148     */
149    public function getEditNewsAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
150    {
151        if (!Auth::isManager($tree)) {
152            throw new AccessDeniedHttpException();
153        }
154
155        $news_id = $request->getQueryParams()['news_id'] ?? '';
156
157        if ($news_id !== '') {
158            $row = DB::table('news')
159                ->where('news_id', '=', $news_id)
160                ->where('gedcom_id', '=', $tree->id())
161                ->first();
162        } else {
163            $row = (object) [
164                'body'    => '',
165                'subject' => '',
166            ];
167        }
168
169        $title = I18N::translate('Add/edit a journal/news entry');
170
171        return $this->viewResponse('modules/gedcom_news/edit', [
172            'body'    => $row->body,
173            'news_id' => $news_id,
174            'subject' => $row->subject,
175            'title'   => $title,
176        ]);
177    }
178
179    /**
180     * @param ServerRequestInterface $request
181     * @param Tree                   $tree
182     *
183     * @return ResponseInterface
184     */
185    public function postEditNewsAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
186    {
187        if (!Auth::isManager($tree)) {
188            throw new AccessDeniedHttpException();
189        }
190
191        $news_id = $request->getQueryParams()['news_id'] ?? '';
192        $subject = $request->getParsedBody()['subject'];
193        $body    = $request->getParsedBody()['body'];
194
195        if ($news_id > 0) {
196            DB::table('news')
197                ->where('news_id', '=', $news_id)
198                ->where('gedcom_id', '=', $tree->id())
199                ->update([
200                    'body'    => $body,
201                    'subject' => $subject,
202                ]);
203        } else {
204            DB::table('news')->insert([
205                'body'      => $body,
206                'subject'   => $subject,
207                'gedcom_id' => $tree->id(),
208            ]);
209        }
210
211        $url = route('tree-page', [
212            'ged' => $tree->name(),
213        ]);
214
215        return redirect($url);
216    }
217
218    /**
219     * @param ServerRequestInterface $request
220     * @param Tree                   $tree
221     *
222     * @return ResponseInterface
223     */
224    public function postDeleteNewsAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
225    {
226        $news_id = $request->getQueryParams()['news_id'];
227
228        if (!Auth::isManager($tree)) {
229            throw new AccessDeniedHttpException();
230        }
231
232        DB::table('news')
233            ->where('news_id', '=', $news_id)
234            ->where('gedcom_id', '=', $tree->id())
235            ->delete();
236
237        $url = route('tree-page', [
238            'ged' => $tree->name(),
239        ]);
240
241        return redirect($url);
242    }
243}
244