xref: /webtrees/app/Module/FamilyTreeNewsModule.php (revision 26684e686fb5ab50ecb57e7e6c6a0a55852d2203)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 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\I18N;
22fe8a65d1SGreg Roachuse Fisharebest\Webtrees\Tree;
23cef665d9SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
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 */
3249a243cbSGreg Roachclass FamilyTreeNewsModule extends AbstractModule implements ModuleInterface, ModuleBlockInterface
33c1010edaSGreg Roach{
3449a243cbSGreg Roach    use ModuleBlockTrait;
3549a243cbSGreg Roach
3676692c8bSGreg Roach    /**
3776692c8bSGreg Roach     * How should this module be labelled on tabs, menus, etc.?
3876692c8bSGreg Roach     *
3976692c8bSGreg Roach     * @return string
4076692c8bSGreg Roach     */
4149a243cbSGreg Roach    public function title(): string
42c1010edaSGreg Roach    {
43bbb76c12SGreg Roach        /* I18N: Name of a module */
44bbb76c12SGreg Roach        return I18N::translate('News');
458c2e8227SGreg Roach    }
468c2e8227SGreg Roach
4776692c8bSGreg Roach    /**
4876692c8bSGreg Roach     * A sentence describing what this module does.
4976692c8bSGreg Roach     *
5076692c8bSGreg Roach     * @return string
5176692c8bSGreg Roach     */
5249a243cbSGreg Roach    public function description(): string
53c1010edaSGreg Roach    {
54bbb76c12SGreg Roach        /* I18N: Description of the “News” module */
55bbb76c12SGreg Roach        return I18N::translate('Family news and site announcements.');
568c2e8227SGreg Roach    }
578c2e8227SGreg Roach
5876692c8bSGreg Roach    /**
5976692c8bSGreg Roach     * Generate the HTML content of this block.
6076692c8bSGreg Roach     *
61e490cd80SGreg Roach     * @param Tree     $tree
6276692c8bSGreg Roach     * @param int      $block_id
635f2ae573SGreg Roach     * @param string   $ctype
64727f238cSGreg Roach     * @param string[] $cfg
6576692c8bSGreg Roach     *
6676692c8bSGreg Roach     * @return string
6776692c8bSGreg Roach     */
685f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
69c1010edaSGreg Roach    {
70cef665d9SGreg Roach        $articles = DB::table('news')
71cef665d9SGreg Roach            ->where('gedcom_id', '=', $tree->id())
72cef665d9SGreg Roach            ->orderByDesc('updated')
73cef665d9SGreg Roach            ->select(['news_id', 'user_id', 'gedcom_id', DB::raw('UNIX_TIMESTAMP(updated) AS updated'), 'subject', 'body'])
74cef665d9SGreg Roach            ->get();
75d004f62cSGreg Roach
76147e99aaSGreg Roach        $content = view('modules/gedcom_news/list', [
77fe8a65d1SGreg Roach            'articles' => $articles,
78fe8a65d1SGreg Roach            'block_id' => $block_id,
79fe8a65d1SGreg Roach            'limit'    => 5,
80fe8a65d1SGreg Roach        ]);
818c2e8227SGreg Roach
826a8879feSGreg Roach        if ($ctype !== '') {
83147e99aaSGreg Roach            return view('modules/block-template', [
84*26684e68SGreg Roach                'block'      => str_replace('_', '-', $this->name()),
859c6524dcSGreg Roach                'id'         => $block_id,
869c6524dcSGreg Roach                'config_url' => '',
8749a243cbSGreg Roach                'title'      => $this->title(),
889c6524dcSGreg Roach                'content'    => $content,
899c6524dcSGreg Roach            ]);
908c2e8227SGreg Roach        }
91b2ce94c6SRico Sonntag
92b2ce94c6SRico Sonntag        return $content;
938c2e8227SGreg Roach    }
948c2e8227SGreg Roach
958c2e8227SGreg Roach    /** {@inheritdoc} */
96c1010edaSGreg Roach    public function loadAjax(): bool
97c1010edaSGreg Roach    {
988c2e8227SGreg Roach        return false;
998c2e8227SGreg Roach    }
1008c2e8227SGreg Roach
1018c2e8227SGreg Roach    /** {@inheritdoc} */
102c1010edaSGreg Roach    public function isUserBlock(): bool
103c1010edaSGreg Roach    {
1048c2e8227SGreg Roach        return false;
1058c2e8227SGreg Roach    }
1068c2e8227SGreg Roach
1078c2e8227SGreg Roach    /** {@inheritdoc} */
108c1010edaSGreg Roach    public function isGedcomBlock(): bool
109c1010edaSGreg Roach    {
1108c2e8227SGreg Roach        return true;
1118c2e8227SGreg Roach    }
1128c2e8227SGreg Roach
11376692c8bSGreg Roach    /**
114a45f9889SGreg Roach     * Update the configuration for a block.
115a45f9889SGreg Roach     *
116a45f9889SGreg Roach     * @param Request $request
117a45f9889SGreg Roach     * @param int     $block_id
118a45f9889SGreg Roach     *
119a45f9889SGreg Roach     * @return void
120a45f9889SGreg Roach     */
121a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
122a45f9889SGreg Roach    {
123a45f9889SGreg Roach    }
124a45f9889SGreg Roach
125a45f9889SGreg Roach    /**
12676692c8bSGreg Roach     * An HTML form to edit block settings
12776692c8bSGreg Roach     *
128e490cd80SGreg Roach     * @param Tree $tree
12976692c8bSGreg Roach     * @param int  $block_id
130a9430be8SGreg Roach     *
131a9430be8SGreg Roach     * @return void
13276692c8bSGreg Roach     */
133a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
134c1010edaSGreg Roach    {
1358c2e8227SGreg Roach    }
136fe8a65d1SGreg Roach
137fe8a65d1SGreg Roach    /**
138fe8a65d1SGreg Roach     * @param Request $request
139b6db7c1fSGreg Roach     * @param Tree    $tree
140fe8a65d1SGreg Roach     *
141fe8a65d1SGreg Roach     * @return Response
142fe8a65d1SGreg Roach     */
143b6db7c1fSGreg Roach    public function getEditNewsAction(Request $request, Tree $tree): Response
144c1010edaSGreg Roach    {
145fe8a65d1SGreg Roach        if (!Auth::isManager($tree)) {
14659f2f229SGreg Roach            throw new AccessDeniedHttpException();
147fe8a65d1SGreg Roach        }
148fe8a65d1SGreg Roach
149fe8a65d1SGreg Roach        $news_id = $request->get('news_id');
150fe8a65d1SGreg Roach
151fe8a65d1SGreg Roach        if ($news_id > 0) {
152cef665d9SGreg Roach            $row = DB::table('news')
153cef665d9SGreg Roach                ->where('news_id', '=', $news_id)
154cef665d9SGreg Roach                ->where('gedcom_id', '=', $tree->id())
155cef665d9SGreg Roach                ->first();
156fe8a65d1SGreg Roach        } else {
157fe8a65d1SGreg Roach            $row = (object) [
158fe8a65d1SGreg Roach                'body'    => '',
159fe8a65d1SGreg Roach                'subject' => '',
160fe8a65d1SGreg Roach            ];
161fe8a65d1SGreg Roach        }
162fe8a65d1SGreg Roach
163fe8a65d1SGreg Roach        $title = I18N::translate('Add/edit a journal/news entry');
164fe8a65d1SGreg Roach
165147e99aaSGreg Roach        return $this->viewResponse('modules/gedcom_news/edit', [
166fe8a65d1SGreg Roach            'body'    => $row->body,
167fe8a65d1SGreg Roach            'news_id' => $news_id,
168fe8a65d1SGreg Roach            'subject' => $row->subject,
169fe8a65d1SGreg Roach            'title'   => $title,
170fe8a65d1SGreg Roach        ]);
171fe8a65d1SGreg Roach    }
172fe8a65d1SGreg Roach
173fe8a65d1SGreg Roach    /**
174fe8a65d1SGreg Roach     * @param Request $request
175b6db7c1fSGreg Roach     * @param Tree    $tree
176fe8a65d1SGreg Roach     *
177b43958a0SGreg Roach     * @return RedirectResponse
178fe8a65d1SGreg Roach     */
179b6db7c1fSGreg Roach    public function postEditNewsAction(Request $request, Tree $tree): RedirectResponse
180c1010edaSGreg Roach    {
181fe8a65d1SGreg Roach        if (!Auth::isManager($tree)) {
18259f2f229SGreg Roach            throw new AccessDeniedHttpException();
183fe8a65d1SGreg Roach        }
184fe8a65d1SGreg Roach
185fe8a65d1SGreg Roach        $news_id = $request->get('news_id');
186fe8a65d1SGreg Roach        $subject = $request->get('subject');
187fe8a65d1SGreg Roach        $body    = $request->get('body');
188fe8a65d1SGreg Roach
189fe8a65d1SGreg Roach        if ($news_id > 0) {
190cef665d9SGreg Roach            DB::table('news')
191cef665d9SGreg Roach                ->where('news_id', '=', $news_id)
192cef665d9SGreg Roach                ->where('gedcom_id', '=', $tree->id())
193cef665d9SGreg Roach                ->update([
194fe8a65d1SGreg Roach                    'body'    => $body,
195cef665d9SGreg Roach                    'subject' => $subject,
196fe8a65d1SGreg Roach                ]);
197fe8a65d1SGreg Roach        } else {
198cef665d9SGreg Roach            DB::table('news')->insert([
199fe8a65d1SGreg Roach                'body'      => $body,
200fe8a65d1SGreg Roach                'subject'   => $subject,
201cef665d9SGreg Roach                'gedcom_id' => $tree->id(),
202fe8a65d1SGreg Roach            ]);
203fe8a65d1SGreg Roach        }
204fe8a65d1SGreg Roach
20504ac9f0fSGreg Roach        $url = route('tree-page', [
206aa6f03bbSGreg Roach            'ged' => $tree->name(),
207fe8a65d1SGreg Roach        ]);
208fe8a65d1SGreg Roach
209fe8a65d1SGreg Roach        return new RedirectResponse($url);
210fe8a65d1SGreg Roach    }
211fe8a65d1SGreg Roach
212fe8a65d1SGreg Roach    /**
213fe8a65d1SGreg Roach     * @param Request $request
214b6db7c1fSGreg Roach     * @param Tree    $tree
215fe8a65d1SGreg Roach     *
216b43958a0SGreg Roach     * @return RedirectResponse
217fe8a65d1SGreg Roach     */
218b6db7c1fSGreg Roach    public function postDeleteNewsAction(Request $request, Tree $tree): RedirectResponse
219c1010edaSGreg Roach    {
220fe8a65d1SGreg Roach        $news_id = $request->get('news_id');
221fe8a65d1SGreg Roach
222fe8a65d1SGreg Roach        if (!Auth::isManager($tree)) {
22359f2f229SGreg Roach            throw new AccessDeniedHttpException();
224fe8a65d1SGreg Roach        }
225fe8a65d1SGreg Roach
226cef665d9SGreg Roach        DB::table('news')
227cef665d9SGreg Roach            ->where('news_id', '=', $news_id)
228cef665d9SGreg Roach            ->where('gedcom_id', '=', $tree->id())
229cef665d9SGreg Roach            ->delete();
230fe8a65d1SGreg Roach
23104ac9f0fSGreg Roach        $url = route('tree-page', [
232aa6f03bbSGreg Roach            'ged' => $tree->name(),
233fe8a65d1SGreg Roach        ]);
234fe8a65d1SGreg Roach
235fe8a65d1SGreg Roach        return new RedirectResponse($url);
236fe8a65d1SGreg Roach    }
2378c2e8227SGreg Roach}
238