18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 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 1589f7189bSGreg Roach * along with this program. If not, see <https://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; 23*6f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB; 2481b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpAccessDeniedException; 254348fc02SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException; 268e0e1b25SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\TreePage; 270e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 28d97083feSGreg Roachuse Fisharebest\Webtrees\Registry; 2950d6f48cSGreg Roachuse Fisharebest\Webtrees\Services\HtmlService; 30fe8a65d1SGreg Roachuse Fisharebest\Webtrees\Tree; 31b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 320dc38e08SGreg Roachuse Illuminate\Database\Query\Expression; 331e7a7a28SGreg Roachuse Illuminate\Support\Str; 346ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 356ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 36f3874e19SGreg Roach 378c2e8227SGreg Roach/** 388c2e8227SGreg Roach * Class FamilyTreeNewsModule 398c2e8227SGreg Roach */ 4037eb8894SGreg Roachclass FamilyTreeNewsModule extends AbstractModule implements ModuleBlockInterface 41c1010edaSGreg Roach{ 4249a243cbSGreg Roach use ModuleBlockTrait; 4349a243cbSGreg Roach 4443f2f523SGreg Roach private HtmlService $html_service; 4550d6f48cSGreg Roach 4650d6f48cSGreg Roach /** 4750d6f48cSGreg Roach * @param HtmlService $html_service 4850d6f48cSGreg Roach */ 499e18e23bSGreg Roach public function __construct(HtmlService $html_service) 5050d6f48cSGreg Roach { 5150d6f48cSGreg Roach $this->html_service = $html_service; 5250d6f48cSGreg Roach } 5350d6f48cSGreg Roach 5449a243cbSGreg Roach public function description(): string 55c1010edaSGreg Roach { 56bbb76c12SGreg Roach /* I18N: Description of the “News” module */ 57bbb76c12SGreg Roach return I18N::translate('Family news and site announcements.'); 588c2e8227SGreg Roach } 598c2e8227SGreg Roach 6076692c8bSGreg Roach /** 6176692c8bSGreg Roach * Generate the HTML content of this block. 6276692c8bSGreg Roach * 63e490cd80SGreg Roach * @param Tree $tree 6476692c8bSGreg Roach * @param int $block_id 653caaa4d2SGreg Roach * @param string $context 6676d39c55SGreg Roach * @param array<string,string> $config 6776692c8bSGreg Roach * 6876692c8bSGreg Roach * @return string 6976692c8bSGreg Roach */ 703caaa4d2SGreg Roach public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 71c1010edaSGreg Roach { 72cef665d9SGreg Roach $articles = DB::table('news') 73cef665d9SGreg Roach ->where('gedcom_id', '=', $tree->id()) 74cef665d9SGreg Roach ->orderByDesc('updated') 75ca52a408SGreg Roach ->get() 76f70bcff5SGreg Roach ->map(static function (object $row): object { 77d97083feSGreg Roach $row->updated = Registry::timestampFactory()->fromString($row->updated); 78ca52a408SGreg Roach 79ca52a408SGreg Roach return $row; 80ca52a408SGreg Roach }); 81d004f62cSGreg Roach 82147e99aaSGreg Roach $content = view('modules/gedcom_news/list', [ 83fe8a65d1SGreg Roach 'articles' => $articles, 84fe8a65d1SGreg Roach 'block_id' => $block_id, 85fe8a65d1SGreg Roach 'limit' => 5, 86fd303cbfSGreg Roach 'tree' => $tree, 87fe8a65d1SGreg Roach ]); 888c2e8227SGreg Roach 893caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 90147e99aaSGreg Roach return view('modules/block-template', [ 911e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 929c6524dcSGreg Roach 'id' => $block_id, 939c6524dcSGreg Roach 'config_url' => '', 9449a243cbSGreg Roach 'title' => $this->title(), 959c6524dcSGreg Roach 'content' => $content, 969c6524dcSGreg Roach ]); 978c2e8227SGreg Roach } 98b2ce94c6SRico Sonntag 99b2ce94c6SRico Sonntag return $content; 1008c2e8227SGreg Roach } 1018c2e8227SGreg Roach 1026ccdf4f0SGreg Roach /** 1036ccdf4f0SGreg Roach * How should this module be identified in the control panel, etc.? 1046ccdf4f0SGreg Roach * 1056ccdf4f0SGreg Roach * @return string 1066ccdf4f0SGreg Roach */ 1076ccdf4f0SGreg Roach public function title(): string 1086ccdf4f0SGreg Roach { 1096ccdf4f0SGreg Roach /* I18N: Name of a module */ 1106ccdf4f0SGreg Roach return I18N::translate('News'); 1116ccdf4f0SGreg Roach } 1126ccdf4f0SGreg Roach 11350d6f48cSGreg Roach /** 11450d6f48cSGreg Roach * Should this block load asynchronously using AJAX? 11550d6f48cSGreg Roach * 1163caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 11750d6f48cSGreg Roach * 11850d6f48cSGreg Roach * @return bool 11950d6f48cSGreg Roach */ 120c1010edaSGreg Roach public function loadAjax(): bool 121c1010edaSGreg Roach { 1228c2e8227SGreg Roach return false; 1238c2e8227SGreg Roach } 1248c2e8227SGreg Roach 12550d6f48cSGreg Roach /** 12650d6f48cSGreg Roach * Can this block be shown on the user’s home page? 12750d6f48cSGreg Roach * 12850d6f48cSGreg Roach * @return bool 12950d6f48cSGreg Roach */ 130c1010edaSGreg Roach public function isUserBlock(): bool 131c1010edaSGreg Roach { 1328c2e8227SGreg Roach return false; 1338c2e8227SGreg Roach } 1348c2e8227SGreg Roach 13550d6f48cSGreg Roach /** 13650d6f48cSGreg Roach * Can this block be shown on the tree’s home page? 13750d6f48cSGreg Roach * 13850d6f48cSGreg Roach * @return bool 13950d6f48cSGreg Roach */ 14063276d8fSGreg Roach public function isTreeBlock(): bool 141c1010edaSGreg Roach { 1428c2e8227SGreg Roach return true; 1438c2e8227SGreg Roach } 1448c2e8227SGreg Roach 14576692c8bSGreg Roach /** 1466ccdf4f0SGreg Roach * @param ServerRequestInterface $request 147fe8a65d1SGreg Roach * 1486ccdf4f0SGreg Roach * @return ResponseInterface 149fe8a65d1SGreg Roach */ 15057ab2231SGreg Roach public function getEditNewsAction(ServerRequestInterface $request): ResponseInterface 151c1010edaSGreg Roach { 152b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 15357ab2231SGreg Roach 154fe8a65d1SGreg Roach if (!Auth::isManager($tree)) { 155d501c45dSGreg Roach throw new HttpAccessDeniedException(); 156fe8a65d1SGreg Roach } 157fe8a65d1SGreg Roach 158748dbe15SGreg Roach $news_id = Validator::queryParams($request)->integer('news_id', 0); 159fe8a65d1SGreg Roach 160748dbe15SGreg Roach if ($news_id !== 0) { 161cef665d9SGreg Roach $row = DB::table('news') 162cef665d9SGreg Roach ->where('news_id', '=', $news_id) 163cef665d9SGreg Roach ->where('gedcom_id', '=', $tree->id()) 164cef665d9SGreg Roach ->first(); 1654348fc02SGreg Roach 1664348fc02SGreg Roach // Record was deleted before we could read it? 167e61121aaSGreg Roach if ($row === null) { 1684566681eSGreg Roach throw new HttpNotFoundException(I18N::translate('%s does not exist.', 'news_id:' . $news_id)); 1694348fc02SGreg Roach } 170fe8a65d1SGreg Roach } else { 171fe8a65d1SGreg Roach $row = (object) [ 172fe8a65d1SGreg Roach 'body' => '', 173fe8a65d1SGreg Roach 'subject' => '', 174fe8a65d1SGreg Roach ]; 175fe8a65d1SGreg Roach } 176fe8a65d1SGreg Roach 177fe8a65d1SGreg Roach $title = I18N::translate('Add/edit a journal/news entry'); 178fe8a65d1SGreg Roach 179147e99aaSGreg Roach return $this->viewResponse('modules/gedcom_news/edit', [ 180fe8a65d1SGreg Roach 'body' => $row->body, 181fe8a65d1SGreg Roach 'news_id' => $news_id, 182fe8a65d1SGreg Roach 'subject' => $row->subject, 183fe8a65d1SGreg Roach 'title' => $title, 184b3f0586eSGreg Roach 'tree' => $tree, 185fe8a65d1SGreg Roach ]); 186fe8a65d1SGreg Roach } 187fe8a65d1SGreg Roach 188fe8a65d1SGreg Roach /** 1896ccdf4f0SGreg Roach * @param ServerRequestInterface $request 190fe8a65d1SGreg Roach * 1916ccdf4f0SGreg Roach * @return ResponseInterface 192fe8a65d1SGreg Roach */ 19357ab2231SGreg Roach public function postEditNewsAction(ServerRequestInterface $request): ResponseInterface 194c1010edaSGreg Roach { 195b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 19657ab2231SGreg Roach 197fe8a65d1SGreg Roach if (!Auth::isManager($tree)) { 198d501c45dSGreg Roach throw new HttpAccessDeniedException(); 199fe8a65d1SGreg Roach } 200fe8a65d1SGreg Roach 201748dbe15SGreg Roach $news_id = Validator::queryParams($request)->integer('news_id', 0); 202748dbe15SGreg Roach $subject = Validator::parsedBody($request)->string('subject'); 203748dbe15SGreg Roach $body = Validator::parsedBody($request)->string('body'); 204fe8a65d1SGreg Roach 20550d6f48cSGreg Roach $subject = $this->html_service->sanitize($subject); 20650d6f48cSGreg Roach $body = $this->html_service->sanitize($body); 20750d6f48cSGreg Roach 208748dbe15SGreg Roach if ($news_id !== 0) { 209cef665d9SGreg Roach DB::table('news') 210cef665d9SGreg Roach ->where('news_id', '=', $news_id) 211cef665d9SGreg Roach ->where('gedcom_id', '=', $tree->id()) 212cef665d9SGreg Roach ->update([ 213fe8a65d1SGreg Roach 'body' => $body, 214cef665d9SGreg Roach 'subject' => $subject, 2150dc38e08SGreg Roach 'updated' => new Expression('updated'), // See issue #3208 216fe8a65d1SGreg Roach ]); 217fe8a65d1SGreg Roach } else { 218cef665d9SGreg Roach DB::table('news')->insert([ 219fe8a65d1SGreg Roach 'body' => $body, 220fe8a65d1SGreg Roach 'subject' => $subject, 221cef665d9SGreg Roach 'gedcom_id' => $tree->id(), 222fe8a65d1SGreg Roach ]); 223fe8a65d1SGreg Roach } 224fe8a65d1SGreg Roach 2258e0e1b25SGreg Roach $url = route(TreePage::class, ['tree' => $tree->name()]); 226fe8a65d1SGreg Roach 2276ccdf4f0SGreg Roach return redirect($url); 228fe8a65d1SGreg Roach } 229fe8a65d1SGreg Roach 230fe8a65d1SGreg Roach /** 2316ccdf4f0SGreg Roach * @param ServerRequestInterface $request 232fe8a65d1SGreg Roach * 2336ccdf4f0SGreg Roach * @return ResponseInterface 234fe8a65d1SGreg Roach */ 23557ab2231SGreg Roach public function postDeleteNewsAction(ServerRequestInterface $request): ResponseInterface 236c1010edaSGreg Roach { 237b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 238748dbe15SGreg Roach $news_id = Validator::queryParams($request)->integer('news_id'); 239fe8a65d1SGreg Roach 240fe8a65d1SGreg Roach if (!Auth::isManager($tree)) { 241d501c45dSGreg Roach throw new HttpAccessDeniedException(); 242fe8a65d1SGreg Roach } 243fe8a65d1SGreg Roach 244cef665d9SGreg Roach DB::table('news') 245cef665d9SGreg Roach ->where('news_id', '=', $news_id) 246cef665d9SGreg Roach ->where('gedcom_id', '=', $tree->id()) 247cef665d9SGreg Roach ->delete(); 248fe8a65d1SGreg Roach 2498e0e1b25SGreg Roach $url = route(TreePage::class, ['tree' => $tree->name()]); 250fe8a65d1SGreg Roach 2516ccdf4f0SGreg Roach return redirect($url); 252fe8a65d1SGreg Roach } 2538c2e8227SGreg Roach} 254