xref: /webtrees/app/Module/FamilyTreeNewsModule.php (revision 6a8879fe9c08e13848197f33eb77fde0b2371399)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1976692c8bSGreg Roach
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Database;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
23fe8a65d1SGreg Roachuse Fisharebest\Webtrees\Tree;
24fe8a65d1SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse;
25fe8a65d1SGreg Roachuse Symfony\Component\HttpFoundation\Request;
26fe8a65d1SGreg Roachuse Symfony\Component\HttpFoundation\Response;
27fe8a65d1SGreg Roachuse Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
288c2e8227SGreg Roach
298c2e8227SGreg Roach/**
308c2e8227SGreg Roach * Class FamilyTreeNewsModule
318c2e8227SGreg Roach */
32c1010edaSGreg Roachclass FamilyTreeNewsModule extends AbstractModule implements ModuleBlockInterface
33c1010edaSGreg Roach{
347fafef87SGreg Roach    // How to update the database schema for this module
357fafef87SGreg Roach    const SCHEMA_TARGET_VERSION   = 3;
367fafef87SGreg Roach    const SCHEMA_SETTING_NAME     = 'NB_SCHEMA_VERSION';
377fafef87SGreg Roach    const SCHEMA_MIGRATION_PREFIX = '\Fisharebest\Webtrees\Module\FamilyTreeNews\Schema';
387fafef87SGreg Roach
3976692c8bSGreg Roach    /**
4076692c8bSGreg Roach     * Create a new module.
4176692c8bSGreg Roach     *
4276692c8bSGreg Roach     * @param string $directory Where is this module installed
4376692c8bSGreg Roach     */
448df859b1SGreg Roach    public function __construct(string $directory)
45c1010edaSGreg Roach    {
463bfb94b0SGreg Roach        parent::__construct($directory);
473bfb94b0SGreg Roach
483bfb94b0SGreg Roach        // Create/update the database tables.
497fafef87SGreg Roach        Database::updateSchema(self::SCHEMA_MIGRATION_PREFIX, self::SCHEMA_SETTING_NAME, self::SCHEMA_TARGET_VERSION);
503bfb94b0SGreg Roach    }
513bfb94b0SGreg Roach
5276692c8bSGreg Roach    /**
5376692c8bSGreg Roach     * How should this module be labelled on tabs, menus, etc.?
5476692c8bSGreg Roach     *
5576692c8bSGreg Roach     * @return string
5676692c8bSGreg Roach     */
578f53f488SRico Sonntag    public function getTitle(): string
58c1010edaSGreg Roach    {
59bbb76c12SGreg Roach        /* I18N: Name of a module */
60bbb76c12SGreg Roach        return I18N::translate('News');
618c2e8227SGreg Roach    }
628c2e8227SGreg Roach
6376692c8bSGreg Roach    /**
6476692c8bSGreg Roach     * A sentence describing what this module does.
6576692c8bSGreg Roach     *
6676692c8bSGreg Roach     * @return string
6776692c8bSGreg Roach     */
688f53f488SRico Sonntag    public function getDescription(): string
69c1010edaSGreg Roach    {
70bbb76c12SGreg Roach        /* I18N: Description of the “News” module */
71bbb76c12SGreg Roach        return I18N::translate('Family news and site announcements.');
728c2e8227SGreg Roach    }
738c2e8227SGreg Roach
7476692c8bSGreg Roach    /**
7576692c8bSGreg Roach     * Generate the HTML content of this block.
7676692c8bSGreg Roach     *
77e490cd80SGreg Roach     * @param Tree     $tree
7876692c8bSGreg Roach     * @param int      $block_id
795f2ae573SGreg Roach     * @param string   $ctype
80727f238cSGreg Roach     * @param string[] $cfg
8176692c8bSGreg Roach     *
8276692c8bSGreg Roach     * @return string
8376692c8bSGreg Roach     */
845f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
85c1010edaSGreg Roach    {
86d004f62cSGreg Roach        $articles = Database::prepare(
87e5588fb0SGreg Roach            "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"
8813abd6f3SGreg Roach        )->execute([
8945d06cf3SGreg Roach            'offset'  => WT_TIMESTAMP_OFFSET,
9072cf66d4SGreg Roach            'tree_id' => $tree->id(),
9113abd6f3SGreg Roach        ])->fetchAll();
92d004f62cSGreg Roach
93147e99aaSGreg Roach        $content = view('modules/gedcom_news/list', [
94fe8a65d1SGreg Roach            'articles' => $articles,
95fe8a65d1SGreg Roach            'block_id' => $block_id,
96fe8a65d1SGreg Roach            'limit'    => 5,
97fe8a65d1SGreg Roach        ]);
988c2e8227SGreg Roach
99*6a8879feSGreg Roach        if ($ctype !== '') {
100147e99aaSGreg Roach            return view('modules/block-template', [
1019c6524dcSGreg Roach                'block'      => str_replace('_', '-', $this->getName()),
1029c6524dcSGreg Roach                'id'         => $block_id,
1039c6524dcSGreg Roach                'config_url' => '',
1049c6524dcSGreg Roach                'title'      => $this->getTitle(),
1059c6524dcSGreg Roach                'content'    => $content,
1069c6524dcSGreg Roach            ]);
1078c2e8227SGreg Roach        }
108b2ce94c6SRico Sonntag
109b2ce94c6SRico Sonntag        return $content;
1108c2e8227SGreg Roach    }
1118c2e8227SGreg Roach
1128c2e8227SGreg Roach    /** {@inheritdoc} */
113c1010edaSGreg Roach    public function loadAjax(): bool
114c1010edaSGreg Roach    {
1158c2e8227SGreg Roach        return false;
1168c2e8227SGreg Roach    }
1178c2e8227SGreg Roach
1188c2e8227SGreg Roach    /** {@inheritdoc} */
119c1010edaSGreg Roach    public function isUserBlock(): bool
120c1010edaSGreg Roach    {
1218c2e8227SGreg Roach        return false;
1228c2e8227SGreg Roach    }
1238c2e8227SGreg Roach
1248c2e8227SGreg Roach    /** {@inheritdoc} */
125c1010edaSGreg Roach    public function isGedcomBlock(): bool
126c1010edaSGreg Roach    {
1278c2e8227SGreg Roach        return true;
1288c2e8227SGreg Roach    }
1298c2e8227SGreg Roach
13076692c8bSGreg Roach    /**
131a45f9889SGreg Roach     * Update the configuration for a block.
132a45f9889SGreg Roach     *
133a45f9889SGreg Roach     * @param Request $request
134a45f9889SGreg Roach     * @param int     $block_id
135a45f9889SGreg Roach     *
136a45f9889SGreg Roach     * @return void
137a45f9889SGreg Roach     */
138a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
139a45f9889SGreg Roach    {
140a45f9889SGreg Roach    }
141a45f9889SGreg Roach
142a45f9889SGreg Roach    /**
14376692c8bSGreg Roach     * An HTML form to edit block settings
14476692c8bSGreg Roach     *
145e490cd80SGreg Roach     * @param Tree $tree
14676692c8bSGreg Roach     * @param int  $block_id
147a9430be8SGreg Roach     *
148a9430be8SGreg Roach     * @return void
14976692c8bSGreg Roach     */
150a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
151c1010edaSGreg Roach    {
1528c2e8227SGreg Roach    }
153fe8a65d1SGreg Roach
154fe8a65d1SGreg Roach    /**
155fe8a65d1SGreg Roach     * @param Request $request
156b6db7c1fSGreg Roach     * @param Tree    $tree
157fe8a65d1SGreg Roach     *
158fe8a65d1SGreg Roach     * @return Response
159fe8a65d1SGreg Roach     */
160b6db7c1fSGreg Roach    public function getEditNewsAction(Request $request, Tree $tree): Response
161c1010edaSGreg Roach    {
162fe8a65d1SGreg Roach        if (!Auth::isManager($tree)) {
16359f2f229SGreg Roach            throw new AccessDeniedHttpException();
164fe8a65d1SGreg Roach        }
165fe8a65d1SGreg Roach
166fe8a65d1SGreg Roach        $news_id = $request->get('news_id');
167fe8a65d1SGreg Roach
168fe8a65d1SGreg Roach        if ($news_id > 0) {
169fe8a65d1SGreg Roach            $row = Database::prepare(
170fe8a65d1SGreg Roach                "SELECT subject, body FROM `##news` WHERE news_id = :news_id AND gedcom_id = :tree_id"
171fe8a65d1SGreg Roach            )->execute([
172fe8a65d1SGreg Roach                'news_id' => $news_id,
17372cf66d4SGreg Roach                'tree_id' => $tree->id(),
174fe8a65d1SGreg Roach            ])->fetchOneRow();
175fe8a65d1SGreg Roach        } else {
176fe8a65d1SGreg Roach            $row = (object) [
177fe8a65d1SGreg Roach                'body'    => '',
178fe8a65d1SGreg Roach                'subject' => '',
179fe8a65d1SGreg Roach            ];
180fe8a65d1SGreg Roach        }
181fe8a65d1SGreg Roach
182fe8a65d1SGreg Roach        $title = I18N::translate('Add/edit a journal/news entry');
183fe8a65d1SGreg Roach
184147e99aaSGreg Roach        return $this->viewResponse('modules/gedcom_news/edit', [
185fe8a65d1SGreg Roach            'body'    => $row->body,
186fe8a65d1SGreg Roach            'news_id' => $news_id,
187fe8a65d1SGreg Roach            'subject' => $row->subject,
188fe8a65d1SGreg Roach            'title'   => $title,
189fe8a65d1SGreg Roach        ]);
190fe8a65d1SGreg Roach    }
191fe8a65d1SGreg Roach
192fe8a65d1SGreg Roach    /**
193fe8a65d1SGreg Roach     * @param Request $request
194b6db7c1fSGreg Roach     * @param Tree    $tree
195fe8a65d1SGreg Roach     *
196b43958a0SGreg Roach     * @return RedirectResponse
197fe8a65d1SGreg Roach     */
198b6db7c1fSGreg Roach    public function postEditNewsAction(Request $request, Tree $tree): RedirectResponse
199c1010edaSGreg Roach    {
200fe8a65d1SGreg Roach        if (!Auth::isManager($tree)) {
20159f2f229SGreg Roach            throw new AccessDeniedHttpException();
202fe8a65d1SGreg Roach        }
203fe8a65d1SGreg Roach
204fe8a65d1SGreg Roach        $news_id = $request->get('news_id');
205fe8a65d1SGreg Roach        $subject = $request->get('subject');
206fe8a65d1SGreg Roach        $body    = $request->get('body');
207fe8a65d1SGreg Roach
208fe8a65d1SGreg Roach        if ($news_id > 0) {
209fe8a65d1SGreg Roach            Database::prepare(
210fe8a65d1SGreg Roach                "UPDATE `##news` SET subject = :subject, body = :body, updated = CURRENT_TIMESTAMP" .
211fe8a65d1SGreg Roach                " WHERE news_id = :news_id AND gedcom_id = :tree_id"
212fe8a65d1SGreg Roach            )->execute([
213fe8a65d1SGreg Roach                'subject' => $subject,
214fe8a65d1SGreg Roach                'body'    => $body,
215fe8a65d1SGreg Roach                'news_id' => $news_id,
21672cf66d4SGreg Roach                'tree_id' => $tree->id(),
217fe8a65d1SGreg Roach            ]);
218fe8a65d1SGreg Roach        } else {
219fe8a65d1SGreg Roach            Database::prepare(
220fe8a65d1SGreg Roach                "INSERT INTO `##news` (gedcom_id, subject, body, updated) VALUES (:tree_id, :subject ,:body, CURRENT_TIMESTAMP)"
221fe8a65d1SGreg Roach            )->execute([
222fe8a65d1SGreg Roach                'body'    => $body,
223fe8a65d1SGreg Roach                'subject' => $subject,
22472cf66d4SGreg Roach                'tree_id' => $tree->id(),
225fe8a65d1SGreg Roach            ]);
226fe8a65d1SGreg Roach        }
227fe8a65d1SGreg Roach
22804ac9f0fSGreg Roach        $url = route('tree-page', [
229aa6f03bbSGreg Roach            'ged' => $tree->name(),
230fe8a65d1SGreg Roach        ]);
231fe8a65d1SGreg Roach
232fe8a65d1SGreg Roach        return new RedirectResponse($url);
233fe8a65d1SGreg Roach    }
234fe8a65d1SGreg Roach
235fe8a65d1SGreg Roach    /**
236fe8a65d1SGreg Roach     * @param Request $request
237b6db7c1fSGreg Roach     * @param Tree    $tree
238fe8a65d1SGreg Roach     *
239b43958a0SGreg Roach     * @return RedirectResponse
240fe8a65d1SGreg Roach     */
241b6db7c1fSGreg Roach    public function postDeleteNewsAction(Request $request, Tree $tree): RedirectResponse
242c1010edaSGreg Roach    {
243fe8a65d1SGreg Roach        $news_id = $request->get('news_id');
244fe8a65d1SGreg Roach
245fe8a65d1SGreg Roach        if (!Auth::isManager($tree)) {
24659f2f229SGreg Roach            throw new AccessDeniedHttpException();
247fe8a65d1SGreg Roach        }
248fe8a65d1SGreg Roach
249fe8a65d1SGreg Roach        Database::prepare(
250fe8a65d1SGreg Roach            "DELETE FROM `##news` WHERE news_id = :news_id AND gedcom_id = :tree_id"
251fe8a65d1SGreg Roach        )->execute([
252fe8a65d1SGreg Roach            'news_id' => $news_id,
25372cf66d4SGreg Roach            'tree_id' => $tree->id(),
254fe8a65d1SGreg Roach        ]);
255fe8a65d1SGreg Roach
25604ac9f0fSGreg Roach        $url = route('tree-page', [
257aa6f03bbSGreg Roach            'ged' => $tree->name(),
258fe8a65d1SGreg Roach        ]);
259fe8a65d1SGreg Roach
260fe8a65d1SGreg Roach        return new RedirectResponse($url);
261fe8a65d1SGreg Roach    }
2628c2e8227SGreg Roach}
263