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