xref: /webtrees/app/Module/FamilyTreeNewsModule.php (revision 75964c75fd01f8e12e2a313cc89fc2222413a03f)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
98c2e8227SGreg Roach * (at your option) any later version.
108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138c2e8227SGreg Roach * GNU General Public License for more details.
148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
158c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
168c2e8227SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2176692c8bSGreg Roach
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
234459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
2550d6f48cSGreg Roachuse Fisharebest\Webtrees\Services\HtmlService;
26fe8a65d1SGreg Roachuse Fisharebest\Webtrees\Tree;
27cef665d9SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
281e7a7a28SGreg Roachuse Illuminate\Support\Str;
296ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
306ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
31ca52a408SGreg Roachuse stdClass;
32fe8a65d1SGreg Roachuse Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
33f3874e19SGreg Roach
345229eadeSGreg Roachuse function assert;
358c2e8227SGreg Roach
368c2e8227SGreg Roach/**
378c2e8227SGreg Roach * Class FamilyTreeNewsModule
388c2e8227SGreg Roach */
3937eb8894SGreg Roachclass FamilyTreeNewsModule extends AbstractModule implements ModuleBlockInterface
40c1010edaSGreg Roach{
4149a243cbSGreg Roach    use ModuleBlockTrait;
4249a243cbSGreg Roach
4350d6f48cSGreg Roach    /** @var HtmlService */
4450d6f48cSGreg Roach    private $html_service;
4550d6f48cSGreg Roach
4650d6f48cSGreg Roach    /**
4750d6f48cSGreg Roach     * HtmlBlockModule bootstrap.
4850d6f48cSGreg Roach     *
4950d6f48cSGreg Roach     * @param HtmlService $html_service
5050d6f48cSGreg Roach     */
51982e6c55SGreg Roach    public function boot(HtmlService $html_service): void
5250d6f48cSGreg Roach    {
5350d6f48cSGreg Roach        $this->html_service = $html_service;
5450d6f48cSGreg Roach    }
5550d6f48cSGreg Roach
5676692c8bSGreg Roach    /**
5776692c8bSGreg Roach     * A sentence describing what this module does.
5876692c8bSGreg Roach     *
5976692c8bSGreg Roach     * @return string
6076692c8bSGreg Roach     */
6149a243cbSGreg Roach    public function description(): string
62c1010edaSGreg Roach    {
63bbb76c12SGreg Roach        /* I18N: Description of the “News” module */
64bbb76c12SGreg Roach        return I18N::translate('Family news and site announcements.');
658c2e8227SGreg Roach    }
668c2e8227SGreg Roach
6776692c8bSGreg Roach    /**
6876692c8bSGreg Roach     * Generate the HTML content of this block.
6976692c8bSGreg Roach     *
70e490cd80SGreg Roach     * @param Tree     $tree
7176692c8bSGreg Roach     * @param int      $block_id
723caaa4d2SGreg Roach     * @param string   $context
733caaa4d2SGreg Roach     * @param string[] $config
7476692c8bSGreg Roach     *
7576692c8bSGreg Roach     * @return string
7676692c8bSGreg Roach     */
773caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
78c1010edaSGreg Roach    {
79cef665d9SGreg Roach        $articles = DB::table('news')
80cef665d9SGreg Roach            ->where('gedcom_id', '=', $tree->id())
81cef665d9SGreg Roach            ->orderByDesc('updated')
82ca52a408SGreg Roach            ->get()
830b5fd0a6SGreg Roach            ->map(static function (stdClass $row): stdClass {
844459dc9aSGreg Roach                $row->updated = Carbon::make($row->updated);
85ca52a408SGreg Roach
86ca52a408SGreg Roach                return $row;
87ca52a408SGreg Roach            });
88d004f62cSGreg Roach
89147e99aaSGreg Roach        $content = view('modules/gedcom_news/list', [
90fe8a65d1SGreg Roach            'articles' => $articles,
91fe8a65d1SGreg Roach            'block_id' => $block_id,
92fe8a65d1SGreg Roach            'limit'    => 5,
93fe8a65d1SGreg Roach        ]);
948c2e8227SGreg Roach
953caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
96147e99aaSGreg Roach            return view('modules/block-template', [
971e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
989c6524dcSGreg Roach                'id'         => $block_id,
999c6524dcSGreg Roach                'config_url' => '',
10049a243cbSGreg Roach                'title'      => $this->title(),
1019c6524dcSGreg Roach                'content'    => $content,
1029c6524dcSGreg Roach            ]);
1038c2e8227SGreg Roach        }
104b2ce94c6SRico Sonntag
105b2ce94c6SRico Sonntag        return $content;
1068c2e8227SGreg Roach    }
1078c2e8227SGreg Roach
1086ccdf4f0SGreg Roach    /**
1096ccdf4f0SGreg Roach     * How should this module be identified in the control panel, etc.?
1106ccdf4f0SGreg Roach     *
1116ccdf4f0SGreg Roach     * @return string
1126ccdf4f0SGreg Roach     */
1136ccdf4f0SGreg Roach    public function title(): string
1146ccdf4f0SGreg Roach    {
1156ccdf4f0SGreg Roach        /* I18N: Name of a module */
1166ccdf4f0SGreg Roach        return I18N::translate('News');
1176ccdf4f0SGreg Roach    }
1186ccdf4f0SGreg Roach
11950d6f48cSGreg Roach    /**
12050d6f48cSGreg Roach     * Should this block load asynchronously using AJAX?
12150d6f48cSGreg Roach     *
1223caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
12350d6f48cSGreg Roach     *
12450d6f48cSGreg Roach     * @return bool
12550d6f48cSGreg Roach     */
126c1010edaSGreg Roach    public function loadAjax(): bool
127c1010edaSGreg Roach    {
1288c2e8227SGreg Roach        return false;
1298c2e8227SGreg Roach    }
1308c2e8227SGreg Roach
13150d6f48cSGreg Roach    /**
13250d6f48cSGreg Roach     * Can this block be shown on the user’s home page?
13350d6f48cSGreg Roach     *
13450d6f48cSGreg Roach     * @return bool
13550d6f48cSGreg Roach     */
136c1010edaSGreg Roach    public function isUserBlock(): bool
137c1010edaSGreg Roach    {
1388c2e8227SGreg Roach        return false;
1398c2e8227SGreg Roach    }
1408c2e8227SGreg Roach
14150d6f48cSGreg Roach    /**
14250d6f48cSGreg Roach     * Can this block be shown on the tree’s home page?
14350d6f48cSGreg Roach     *
14450d6f48cSGreg Roach     * @return bool
14550d6f48cSGreg Roach     */
14663276d8fSGreg Roach    public function isTreeBlock(): bool
147c1010edaSGreg Roach    {
1488c2e8227SGreg Roach        return true;
1498c2e8227SGreg Roach    }
1508c2e8227SGreg Roach
15176692c8bSGreg Roach    /**
1526ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
153fe8a65d1SGreg Roach     *
1546ccdf4f0SGreg Roach     * @return ResponseInterface
155fe8a65d1SGreg Roach     */
15657ab2231SGreg Roach    public function getEditNewsAction(ServerRequestInterface $request): ResponseInterface
157c1010edaSGreg Roach    {
15857ab2231SGreg Roach        $tree = $request->getAttribute('tree');
159*75964c75SGreg Roach        assert($tree instanceof Tree);
16057ab2231SGreg Roach
161fe8a65d1SGreg Roach        if (!Auth::isManager($tree)) {
16259f2f229SGreg Roach            throw new AccessDeniedHttpException();
163fe8a65d1SGreg Roach        }
164fe8a65d1SGreg Roach
165ac5f8ed1SGreg Roach        $news_id = $request->getQueryParams()['news_id'] ?? '';
166fe8a65d1SGreg Roach
167ac5f8ed1SGreg Roach        if ($news_id !== '') {
168cef665d9SGreg Roach            $row = DB::table('news')
169cef665d9SGreg Roach                ->where('news_id', '=', $news_id)
170cef665d9SGreg Roach                ->where('gedcom_id', '=', $tree->id())
171cef665d9SGreg Roach                ->first();
172fe8a65d1SGreg Roach        } else {
173fe8a65d1SGreg Roach            $row = (object) [
174fe8a65d1SGreg Roach                'body'    => '',
175fe8a65d1SGreg Roach                'subject' => '',
176fe8a65d1SGreg Roach            ];
177fe8a65d1SGreg Roach        }
178fe8a65d1SGreg Roach
179fe8a65d1SGreg Roach        $title = I18N::translate('Add/edit a journal/news entry');
180fe8a65d1SGreg Roach
181147e99aaSGreg Roach        return $this->viewResponse('modules/gedcom_news/edit', [
182fe8a65d1SGreg Roach            'body'    => $row->body,
183fe8a65d1SGreg Roach            'news_id' => $news_id,
184fe8a65d1SGreg Roach            'subject' => $row->subject,
185fe8a65d1SGreg Roach            'title'   => $title,
186fe8a65d1SGreg Roach        ]);
187fe8a65d1SGreg Roach    }
188fe8a65d1SGreg Roach
189fe8a65d1SGreg Roach    /**
1906ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
191fe8a65d1SGreg Roach     *
1926ccdf4f0SGreg Roach     * @return ResponseInterface
193fe8a65d1SGreg Roach     */
19457ab2231SGreg Roach    public function postEditNewsAction(ServerRequestInterface $request): ResponseInterface
195c1010edaSGreg Roach    {
19657ab2231SGreg Roach        $tree = $request->getAttribute('tree');
197*75964c75SGreg Roach        assert($tree instanceof Tree);
19857ab2231SGreg Roach
199fe8a65d1SGreg Roach        if (!Auth::isManager($tree)) {
20059f2f229SGreg Roach            throw new AccessDeniedHttpException();
201fe8a65d1SGreg Roach        }
202fe8a65d1SGreg Roach
203ac5f8ed1SGreg Roach        $news_id = $request->getQueryParams()['news_id'] ?? '';
204ac5f8ed1SGreg Roach        $subject = $request->getParsedBody()['subject'];
205ac5f8ed1SGreg Roach        $body    = $request->getParsedBody()['body'];
206fe8a65d1SGreg Roach
20750d6f48cSGreg Roach        $subject = $this->html_service->sanitize($subject);
20850d6f48cSGreg Roach        $body    = $this->html_service->sanitize($body);
20950d6f48cSGreg Roach
210fe8a65d1SGreg Roach        if ($news_id > 0) {
211cef665d9SGreg Roach            DB::table('news')
212cef665d9SGreg Roach                ->where('news_id', '=', $news_id)
213cef665d9SGreg Roach                ->where('gedcom_id', '=', $tree->id())
214cef665d9SGreg Roach                ->update([
215fe8a65d1SGreg Roach                    'body'    => $body,
216cef665d9SGreg Roach                    'subject' => $subject,
217fe8a65d1SGreg Roach                ]);
218fe8a65d1SGreg Roach        } else {
219cef665d9SGreg Roach            DB::table('news')->insert([
220fe8a65d1SGreg Roach                'body'      => $body,
221fe8a65d1SGreg Roach                'subject'   => $subject,
222cef665d9SGreg Roach                'gedcom_id' => $tree->id(),
223fe8a65d1SGreg Roach            ]);
224fe8a65d1SGreg Roach        }
225fe8a65d1SGreg Roach
226d72b284aSGreg Roach        $url = route('tree-page', ['tree' => $tree->name()]);
227fe8a65d1SGreg Roach
2286ccdf4f0SGreg Roach        return redirect($url);
229fe8a65d1SGreg Roach    }
230fe8a65d1SGreg Roach
231fe8a65d1SGreg Roach    /**
2326ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
233fe8a65d1SGreg Roach     *
2346ccdf4f0SGreg Roach     * @return ResponseInterface
235fe8a65d1SGreg Roach     */
23657ab2231SGreg Roach    public function postDeleteNewsAction(ServerRequestInterface $request): ResponseInterface
237c1010edaSGreg Roach    {
23857ab2231SGreg Roach        $tree    = $request->getAttribute('tree');
239ac5f8ed1SGreg Roach        $news_id = $request->getQueryParams()['news_id'];
240fe8a65d1SGreg Roach
241fe8a65d1SGreg Roach        if (!Auth::isManager($tree)) {
24259f2f229SGreg Roach            throw new AccessDeniedHttpException();
243fe8a65d1SGreg Roach        }
244fe8a65d1SGreg Roach
245cef665d9SGreg Roach        DB::table('news')
246cef665d9SGreg Roach            ->where('news_id', '=', $news_id)
247cef665d9SGreg Roach            ->where('gedcom_id', '=', $tree->id())
248cef665d9SGreg Roach            ->delete();
249fe8a65d1SGreg Roach
250d72b284aSGreg Roach        $url = route('tree-page', ['tree' => $tree->name()]);
251fe8a65d1SGreg Roach
2526ccdf4f0SGreg Roach        return redirect($url);
253fe8a65d1SGreg Roach    }
2548c2e8227SGreg Roach}
255