xref: /webtrees/app/Module/FamilyTreeNewsModule.php (revision b6db7c1f82484c632c75d5225ce767d398ddc27f)
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 */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1776692c8bSGreg Roach
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Database;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
21fe8a65d1SGreg Roachuse Fisharebest\Webtrees\Tree;
22fe8a65d1SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse;
23fe8a65d1SGreg Roachuse Symfony\Component\HttpFoundation\Request;
24fe8a65d1SGreg Roachuse Symfony\Component\HttpFoundation\Response;
25fe8a65d1SGreg Roachuse Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
268c2e8227SGreg Roach
278c2e8227SGreg Roach/**
288c2e8227SGreg Roach * Class FamilyTreeNewsModule
298c2e8227SGreg Roach */
30c1010edaSGreg Roachclass FamilyTreeNewsModule extends AbstractModule implements ModuleBlockInterface
31c1010edaSGreg Roach{
327fafef87SGreg Roach    // How to update the database schema for this module
337fafef87SGreg Roach    const SCHEMA_TARGET_VERSION   = 3;
347fafef87SGreg Roach    const SCHEMA_SETTING_NAME     = 'NB_SCHEMA_VERSION';
357fafef87SGreg Roach    const SCHEMA_MIGRATION_PREFIX = '\Fisharebest\Webtrees\Module\FamilyTreeNews\Schema';
367fafef87SGreg Roach
3776692c8bSGreg Roach    /**
3876692c8bSGreg Roach     * Create a new module.
3976692c8bSGreg Roach     *
4076692c8bSGreg Roach     * @param string $directory Where is this module installed
4176692c8bSGreg Roach     */
42c1010edaSGreg Roach    public function __construct($directory)
43c1010edaSGreg Roach    {
443bfb94b0SGreg Roach        parent::__construct($directory);
453bfb94b0SGreg Roach
463bfb94b0SGreg Roach        // Create/update the database tables.
477fafef87SGreg Roach        Database::updateSchema(self::SCHEMA_MIGRATION_PREFIX, self::SCHEMA_SETTING_NAME, self::SCHEMA_TARGET_VERSION);
483bfb94b0SGreg Roach    }
493bfb94b0SGreg Roach
5076692c8bSGreg Roach    /**
5176692c8bSGreg Roach     * How should this module be labelled on tabs, menus, etc.?
5276692c8bSGreg Roach     *
5376692c8bSGreg Roach     * @return string
5476692c8bSGreg Roach     */
55c1010edaSGreg Roach    public function getTitle()
56c1010edaSGreg Roach    {
57c1010edaSGreg Roach        return /* I18N: Name of a module */
58c1010edaSGreg Roach            I18N::translate('News');
598c2e8227SGreg Roach    }
608c2e8227SGreg Roach
6176692c8bSGreg Roach    /**
6276692c8bSGreg Roach     * A sentence describing what this module does.
6376692c8bSGreg Roach     *
6476692c8bSGreg Roach     * @return string
6576692c8bSGreg Roach     */
66c1010edaSGreg Roach    public function getDescription()
67c1010edaSGreg Roach    {
68c1010edaSGreg Roach        return /* I18N: Description of the “News” module */
69c1010edaSGreg Roach            I18N::translate('Family news and site announcements.');
708c2e8227SGreg Roach    }
718c2e8227SGreg Roach
7276692c8bSGreg Roach    /**
7376692c8bSGreg Roach     * Generate the HTML content of this block.
7476692c8bSGreg Roach     *
75e490cd80SGreg Roach     * @param Tree     $tree
7676692c8bSGreg Roach     * @param int      $block_id
7776692c8bSGreg Roach     * @param bool     $template
78727f238cSGreg Roach     * @param string[] $cfg
7976692c8bSGreg Roach     *
8076692c8bSGreg Roach     * @return string
8176692c8bSGreg Roach     */
82c1010edaSGreg Roach    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
83c1010edaSGreg Roach    {
84d004f62cSGreg Roach        $articles = Database::prepare(
85e5588fb0SGreg 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"
8613abd6f3SGreg Roach        )->execute([
8745d06cf3SGreg Roach            'offset'  => WT_TIMESTAMP_OFFSET,
88e490cd80SGreg Roach            'tree_id' => $tree->getTreeId(),
8913abd6f3SGreg Roach        ])->fetchAll();
90d004f62cSGreg Roach
91147e99aaSGreg Roach        $content = view('modules/gedcom_news/list', [
92fe8a65d1SGreg Roach            'articles' => $articles,
93fe8a65d1SGreg Roach            'block_id' => $block_id,
94fe8a65d1SGreg Roach            'limit'    => 5,
95fe8a65d1SGreg Roach        ]);
968c2e8227SGreg Roach
978c2e8227SGreg Roach        if ($template) {
98147e99aaSGreg Roach            return view('modules/block-template', [
999c6524dcSGreg Roach                'block'      => str_replace('_', '-', $this->getName()),
1009c6524dcSGreg Roach                'id'         => $block_id,
1019c6524dcSGreg Roach                'config_url' => '',
1029c6524dcSGreg Roach                'title'      => $this->getTitle(),
1039c6524dcSGreg Roach                'content'    => $content,
1049c6524dcSGreg Roach            ]);
1058c2e8227SGreg Roach        } else {
1068c2e8227SGreg Roach            return $content;
1078c2e8227SGreg Roach        }
1088c2e8227SGreg Roach    }
1098c2e8227SGreg Roach
1108c2e8227SGreg Roach    /** {@inheritdoc} */
111c1010edaSGreg Roach    public function loadAjax(): bool
112c1010edaSGreg Roach    {
1138c2e8227SGreg Roach        return false;
1148c2e8227SGreg Roach    }
1158c2e8227SGreg Roach
1168c2e8227SGreg Roach    /** {@inheritdoc} */
117c1010edaSGreg Roach    public function isUserBlock(): bool
118c1010edaSGreg Roach    {
1198c2e8227SGreg Roach        return false;
1208c2e8227SGreg Roach    }
1218c2e8227SGreg Roach
1228c2e8227SGreg Roach    /** {@inheritdoc} */
123c1010edaSGreg Roach    public function isGedcomBlock(): bool
124c1010edaSGreg Roach    {
1258c2e8227SGreg Roach        return true;
1268c2e8227SGreg Roach    }
1278c2e8227SGreg Roach
12876692c8bSGreg Roach    /**
12976692c8bSGreg Roach     * An HTML form to edit block settings
13076692c8bSGreg Roach     *
131e490cd80SGreg Roach     * @param Tree $tree
13276692c8bSGreg Roach     * @param int  $block_id
133a9430be8SGreg Roach     *
134a9430be8SGreg Roach     * @return void
13576692c8bSGreg Roach     */
136c1010edaSGreg Roach    public function configureBlock(Tree $tree, int $block_id)
137c1010edaSGreg Roach    {
1388c2e8227SGreg Roach    }
139fe8a65d1SGreg Roach
140fe8a65d1SGreg Roach    /**
141fe8a65d1SGreg Roach     * @param Request $request
142*b6db7c1fSGreg Roach     * @param Tree    $tree
143fe8a65d1SGreg Roach     *
144fe8a65d1SGreg Roach     * @return Response
145fe8a65d1SGreg Roach     */
146*b6db7c1fSGreg Roach    public function getEditNewsAction(Request $request, Tree $tree): Response
147c1010edaSGreg Roach    {
148fe8a65d1SGreg Roach        if (!Auth::isManager($tree)) {
149fe8a65d1SGreg Roach            throw new AccessDeniedHttpException;
150fe8a65d1SGreg Roach        }
151fe8a65d1SGreg Roach
152fe8a65d1SGreg Roach        $news_id = $request->get('news_id');
153fe8a65d1SGreg Roach
154fe8a65d1SGreg Roach        if ($news_id > 0) {
155fe8a65d1SGreg Roach            $row = Database::prepare(
156fe8a65d1SGreg Roach                "SELECT subject, body FROM `##news` WHERE news_id = :news_id AND gedcom_id = :tree_id"
157fe8a65d1SGreg Roach            )->execute([
158fe8a65d1SGreg Roach                'news_id' => $news_id,
159fe8a65d1SGreg Roach                'tree_id' => $tree->getTreeId(),
160fe8a65d1SGreg Roach            ])->fetchOneRow();
161fe8a65d1SGreg Roach        } else {
162fe8a65d1SGreg Roach            $row = (object)[
163fe8a65d1SGreg Roach                'body'    => '',
164fe8a65d1SGreg Roach                'subject' => '',
165fe8a65d1SGreg Roach            ];
166fe8a65d1SGreg Roach        }
167fe8a65d1SGreg Roach
168fe8a65d1SGreg Roach        $title = I18N::translate('Add/edit a journal/news entry');
169fe8a65d1SGreg Roach
170147e99aaSGreg Roach        return $this->viewResponse('modules/gedcom_news/edit', [
171fe8a65d1SGreg Roach            'body'    => $row->body,
172fe8a65d1SGreg Roach            'news_id' => $news_id,
173fe8a65d1SGreg Roach            'subject' => $row->subject,
174fe8a65d1SGreg Roach            'title'   => $title,
175fe8a65d1SGreg Roach        ]);
176fe8a65d1SGreg Roach    }
177fe8a65d1SGreg Roach
178fe8a65d1SGreg Roach    /**
179fe8a65d1SGreg Roach     * @param Request $request
180*b6db7c1fSGreg Roach     * @param Tree    $tree
181fe8a65d1SGreg Roach     *
182b43958a0SGreg Roach     * @return RedirectResponse
183fe8a65d1SGreg Roach     */
184*b6db7c1fSGreg Roach    public function postEditNewsAction(Request $request, Tree $tree): RedirectResponse
185c1010edaSGreg Roach    {
186fe8a65d1SGreg Roach        if (!Auth::isManager($tree)) {
187fe8a65d1SGreg Roach            throw new AccessDeniedHttpException;
188fe8a65d1SGreg Roach        }
189fe8a65d1SGreg Roach
190fe8a65d1SGreg Roach        $news_id = $request->get('news_id');
191fe8a65d1SGreg Roach        $subject = $request->get('subject');
192fe8a65d1SGreg Roach        $body    = $request->get('body');
193fe8a65d1SGreg Roach
194fe8a65d1SGreg Roach        if ($news_id > 0) {
195fe8a65d1SGreg Roach            Database::prepare(
196fe8a65d1SGreg Roach                "UPDATE `##news` SET subject = :subject, body = :body, updated = CURRENT_TIMESTAMP" .
197fe8a65d1SGreg Roach                " WHERE news_id = :news_id AND gedcom_id = :tree_id"
198fe8a65d1SGreg Roach            )->execute([
199fe8a65d1SGreg Roach                'subject' => $subject,
200fe8a65d1SGreg Roach                'body'    => $body,
201fe8a65d1SGreg Roach                'news_id' => $news_id,
202fe8a65d1SGreg Roach                'tree_id' => $tree->getTreeId(),
203fe8a65d1SGreg Roach            ]);
204fe8a65d1SGreg Roach        } else {
205fe8a65d1SGreg Roach            Database::prepare(
206fe8a65d1SGreg Roach                "INSERT INTO `##news` (gedcom_id, subject, body, updated) VALUES (:tree_id, :subject ,:body, CURRENT_TIMESTAMP)"
207fe8a65d1SGreg Roach            )->execute([
208fe8a65d1SGreg Roach                'body'    => $body,
209fe8a65d1SGreg Roach                'subject' => $subject,
210fe8a65d1SGreg Roach                'tree_id' => $tree->getTreeId(),
211fe8a65d1SGreg Roach            ]);
212fe8a65d1SGreg Roach        }
213fe8a65d1SGreg Roach
21404ac9f0fSGreg Roach        $url = route('tree-page', [
215fe8a65d1SGreg Roach            'ged' => $tree->getName(),
216fe8a65d1SGreg Roach        ]);
217fe8a65d1SGreg Roach
218fe8a65d1SGreg Roach        return new RedirectResponse($url);
219fe8a65d1SGreg Roach    }
220fe8a65d1SGreg Roach
221fe8a65d1SGreg Roach    /**
222fe8a65d1SGreg Roach     * @param Request $request
223*b6db7c1fSGreg Roach     * @param Tree    $tree
224fe8a65d1SGreg Roach     *
225b43958a0SGreg Roach     * @return RedirectResponse
226fe8a65d1SGreg Roach     */
227*b6db7c1fSGreg Roach    public function postDeleteNewsAction(Request $request, Tree $tree): RedirectResponse
228c1010edaSGreg Roach    {
229fe8a65d1SGreg Roach        $news_id = $request->get('news_id');
230fe8a65d1SGreg Roach
231fe8a65d1SGreg Roach        if (!Auth::isManager($tree)) {
232fe8a65d1SGreg Roach            throw new AccessDeniedHttpException;
233fe8a65d1SGreg Roach        }
234fe8a65d1SGreg Roach
235fe8a65d1SGreg Roach        Database::prepare(
236fe8a65d1SGreg Roach            "DELETE FROM `##news` WHERE news_id = :news_id AND gedcom_id = :tree_id"
237fe8a65d1SGreg Roach        )->execute([
238fe8a65d1SGreg Roach            'news_id' => $news_id,
239fe8a65d1SGreg Roach            'tree_id' => $tree->getTreeId(),
240fe8a65d1SGreg Roach        ]);
241fe8a65d1SGreg Roach
24204ac9f0fSGreg Roach        $url = route('tree-page', [
243fe8a65d1SGreg Roach            'ged' => $tree->getName(),
244fe8a65d1SGreg Roach        ]);
245fe8a65d1SGreg Roach
246fe8a65d1SGreg Roach        return new RedirectResponse($url);
247fe8a65d1SGreg Roach    }
2488c2e8227SGreg Roach}
249