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; 214459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon; 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 2350d6f48cSGreg Roachuse Fisharebest\Webtrees\Services\HtmlService; 24fe8a65d1SGreg Roachuse Fisharebest\Webtrees\Tree; 25cef665d9SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 261e7a7a28SGreg Roachuse Illuminate\Support\Str; 276ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 286ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 29ca52a408SGreg Roachuse stdClass; 30fe8a65d1SGreg Roachuse Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; 318c2e8227SGreg Roach 328c2e8227SGreg Roach/** 338c2e8227SGreg Roach * Class FamilyTreeNewsModule 348c2e8227SGreg Roach */ 3537eb8894SGreg Roachclass FamilyTreeNewsModule extends AbstractModule implements ModuleBlockInterface 36c1010edaSGreg Roach{ 3749a243cbSGreg Roach use ModuleBlockTrait; 3849a243cbSGreg Roach 3950d6f48cSGreg Roach /** @var HtmlService */ 4050d6f48cSGreg Roach private $html_service; 4150d6f48cSGreg Roach 4250d6f48cSGreg Roach /** 4350d6f48cSGreg Roach * HtmlBlockModule bootstrap. 4450d6f48cSGreg Roach * 4550d6f48cSGreg Roach * @param HtmlService $html_service 4650d6f48cSGreg Roach */ 4750d6f48cSGreg Roach public function boot(HtmlService $html_service) 4850d6f48cSGreg Roach { 4950d6f48cSGreg Roach $this->html_service = $html_service; 5050d6f48cSGreg Roach } 5150d6f48cSGreg Roach 5276692c8bSGreg Roach /** 5376692c8bSGreg Roach * A sentence describing what this module does. 5476692c8bSGreg Roach * 5576692c8bSGreg Roach * @return string 5676692c8bSGreg Roach */ 5749a243cbSGreg Roach public function description(): string 58c1010edaSGreg Roach { 59bbb76c12SGreg Roach /* I18N: Description of the “News” module */ 60bbb76c12SGreg Roach return I18N::translate('Family news and site announcements.'); 618c2e8227SGreg Roach } 628c2e8227SGreg Roach 6376692c8bSGreg Roach /** 6476692c8bSGreg Roach * Generate the HTML content of this block. 6576692c8bSGreg Roach * 66e490cd80SGreg Roach * @param Tree $tree 6776692c8bSGreg Roach * @param int $block_id 68*3caaa4d2SGreg Roach * @param string $context 69*3caaa4d2SGreg Roach * @param string[] $config 7076692c8bSGreg Roach * 7176692c8bSGreg Roach * @return string 7276692c8bSGreg Roach */ 73*3caaa4d2SGreg Roach public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 74c1010edaSGreg Roach { 75cef665d9SGreg Roach $articles = DB::table('news') 76cef665d9SGreg Roach ->where('gedcom_id', '=', $tree->id()) 77cef665d9SGreg Roach ->orderByDesc('updated') 78ca52a408SGreg Roach ->get() 790b5fd0a6SGreg Roach ->map(static function (stdClass $row): stdClass { 804459dc9aSGreg Roach $row->updated = Carbon::make($row->updated); 81ca52a408SGreg Roach 82ca52a408SGreg Roach return $row; 83ca52a408SGreg Roach }); 84d004f62cSGreg Roach 85147e99aaSGreg Roach $content = view('modules/gedcom_news/list', [ 86fe8a65d1SGreg Roach 'articles' => $articles, 87fe8a65d1SGreg Roach 'block_id' => $block_id, 88fe8a65d1SGreg Roach 'limit' => 5, 89fe8a65d1SGreg Roach ]); 908c2e8227SGreg Roach 91*3caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 92147e99aaSGreg Roach return view('modules/block-template', [ 931e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 949c6524dcSGreg Roach 'id' => $block_id, 959c6524dcSGreg Roach 'config_url' => '', 9649a243cbSGreg Roach 'title' => $this->title(), 979c6524dcSGreg Roach 'content' => $content, 989c6524dcSGreg Roach ]); 998c2e8227SGreg Roach } 100b2ce94c6SRico Sonntag 101b2ce94c6SRico Sonntag return $content; 1028c2e8227SGreg Roach } 1038c2e8227SGreg Roach 1046ccdf4f0SGreg Roach /** 1056ccdf4f0SGreg Roach * How should this module be identified in the control panel, etc.? 1066ccdf4f0SGreg Roach * 1076ccdf4f0SGreg Roach * @return string 1086ccdf4f0SGreg Roach */ 1096ccdf4f0SGreg Roach public function title(): string 1106ccdf4f0SGreg Roach { 1116ccdf4f0SGreg Roach /* I18N: Name of a module */ 1126ccdf4f0SGreg Roach return I18N::translate('News'); 1136ccdf4f0SGreg Roach } 1146ccdf4f0SGreg Roach 11550d6f48cSGreg Roach /** 11650d6f48cSGreg Roach * Should this block load asynchronously using AJAX? 11750d6f48cSGreg Roach * 118*3caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 11950d6f48cSGreg Roach * 12050d6f48cSGreg Roach * @return bool 12150d6f48cSGreg Roach */ 122c1010edaSGreg Roach public function loadAjax(): bool 123c1010edaSGreg Roach { 1248c2e8227SGreg Roach return false; 1258c2e8227SGreg Roach } 1268c2e8227SGreg Roach 12750d6f48cSGreg Roach /** 12850d6f48cSGreg Roach * Can this block be shown on the user’s home page? 12950d6f48cSGreg Roach * 13050d6f48cSGreg Roach * @return bool 13150d6f48cSGreg Roach */ 132c1010edaSGreg Roach public function isUserBlock(): bool 133c1010edaSGreg Roach { 1348c2e8227SGreg Roach return false; 1358c2e8227SGreg Roach } 1368c2e8227SGreg Roach 13750d6f48cSGreg Roach /** 13850d6f48cSGreg Roach * Can this block be shown on the tree’s home page? 13950d6f48cSGreg Roach * 14050d6f48cSGreg Roach * @return bool 14150d6f48cSGreg Roach */ 14263276d8fSGreg Roach public function isTreeBlock(): bool 143c1010edaSGreg Roach { 1448c2e8227SGreg Roach return true; 1458c2e8227SGreg Roach } 1468c2e8227SGreg Roach 14776692c8bSGreg Roach /** 1486ccdf4f0SGreg Roach * @param ServerRequestInterface $request 149b6db7c1fSGreg Roach * @param Tree $tree 150fe8a65d1SGreg Roach * 1516ccdf4f0SGreg Roach * @return ResponseInterface 152fe8a65d1SGreg Roach */ 1536ccdf4f0SGreg Roach public function getEditNewsAction(ServerRequestInterface $request, Tree $tree): ResponseInterface 154c1010edaSGreg Roach { 155fe8a65d1SGreg Roach if (!Auth::isManager($tree)) { 15659f2f229SGreg Roach throw new AccessDeniedHttpException(); 157fe8a65d1SGreg Roach } 158fe8a65d1SGreg Roach 159ac5f8ed1SGreg Roach $news_id = $request->getQueryParams()['news_id'] ?? ''; 160fe8a65d1SGreg Roach 161ac5f8ed1SGreg Roach if ($news_id !== '') { 162cef665d9SGreg Roach $row = DB::table('news') 163cef665d9SGreg Roach ->where('news_id', '=', $news_id) 164cef665d9SGreg Roach ->where('gedcom_id', '=', $tree->id()) 165cef665d9SGreg Roach ->first(); 166fe8a65d1SGreg Roach } else { 167fe8a65d1SGreg Roach $row = (object) [ 168fe8a65d1SGreg Roach 'body' => '', 169fe8a65d1SGreg Roach 'subject' => '', 170fe8a65d1SGreg Roach ]; 171fe8a65d1SGreg Roach } 172fe8a65d1SGreg Roach 173fe8a65d1SGreg Roach $title = I18N::translate('Add/edit a journal/news entry'); 174fe8a65d1SGreg Roach 175147e99aaSGreg Roach return $this->viewResponse('modules/gedcom_news/edit', [ 176fe8a65d1SGreg Roach 'body' => $row->body, 177fe8a65d1SGreg Roach 'news_id' => $news_id, 178fe8a65d1SGreg Roach 'subject' => $row->subject, 179fe8a65d1SGreg Roach 'title' => $title, 180fe8a65d1SGreg Roach ]); 181fe8a65d1SGreg Roach } 182fe8a65d1SGreg Roach 183fe8a65d1SGreg Roach /** 1846ccdf4f0SGreg Roach * @param ServerRequestInterface $request 185b6db7c1fSGreg Roach * @param Tree $tree 186fe8a65d1SGreg Roach * 1876ccdf4f0SGreg Roach * @return ResponseInterface 188fe8a65d1SGreg Roach */ 1896ccdf4f0SGreg Roach public function postEditNewsAction(ServerRequestInterface $request, Tree $tree): ResponseInterface 190c1010edaSGreg Roach { 191fe8a65d1SGreg Roach if (!Auth::isManager($tree)) { 19259f2f229SGreg Roach throw new AccessDeniedHttpException(); 193fe8a65d1SGreg Roach } 194fe8a65d1SGreg Roach 195ac5f8ed1SGreg Roach $news_id = $request->getQueryParams()['news_id'] ?? ''; 196ac5f8ed1SGreg Roach $subject = $request->getParsedBody()['subject']; 197ac5f8ed1SGreg Roach $body = $request->getParsedBody()['body']; 198fe8a65d1SGreg Roach 19950d6f48cSGreg Roach $subject = $this->html_service->sanitize($subject); 20050d6f48cSGreg Roach $body = $this->html_service->sanitize($body); 20150d6f48cSGreg Roach 202fe8a65d1SGreg Roach if ($news_id > 0) { 203cef665d9SGreg Roach DB::table('news') 204cef665d9SGreg Roach ->where('news_id', '=', $news_id) 205cef665d9SGreg Roach ->where('gedcom_id', '=', $tree->id()) 206cef665d9SGreg Roach ->update([ 207fe8a65d1SGreg Roach 'body' => $body, 208cef665d9SGreg Roach 'subject' => $subject, 209fe8a65d1SGreg Roach ]); 210fe8a65d1SGreg Roach } else { 211cef665d9SGreg Roach DB::table('news')->insert([ 212fe8a65d1SGreg Roach 'body' => $body, 213fe8a65d1SGreg Roach 'subject' => $subject, 214cef665d9SGreg Roach 'gedcom_id' => $tree->id(), 215fe8a65d1SGreg Roach ]); 216fe8a65d1SGreg Roach } 217fe8a65d1SGreg Roach 21804ac9f0fSGreg Roach $url = route('tree-page', [ 219aa6f03bbSGreg Roach 'ged' => $tree->name(), 220fe8a65d1SGreg Roach ]); 221fe8a65d1SGreg Roach 2226ccdf4f0SGreg Roach return redirect($url); 223fe8a65d1SGreg Roach } 224fe8a65d1SGreg Roach 225fe8a65d1SGreg Roach /** 2266ccdf4f0SGreg Roach * @param ServerRequestInterface $request 227b6db7c1fSGreg Roach * @param Tree $tree 228fe8a65d1SGreg Roach * 2296ccdf4f0SGreg Roach * @return ResponseInterface 230fe8a65d1SGreg Roach */ 2316ccdf4f0SGreg Roach public function postDeleteNewsAction(ServerRequestInterface $request, Tree $tree): ResponseInterface 232c1010edaSGreg Roach { 233ac5f8ed1SGreg Roach $news_id = $request->getQueryParams()['news_id']; 234fe8a65d1SGreg Roach 235fe8a65d1SGreg Roach if (!Auth::isManager($tree)) { 23659f2f229SGreg Roach throw new AccessDeniedHttpException(); 237fe8a65d1SGreg Roach } 238fe8a65d1SGreg Roach 239cef665d9SGreg Roach DB::table('news') 240cef665d9SGreg Roach ->where('news_id', '=', $news_id) 241cef665d9SGreg Roach ->where('gedcom_id', '=', $tree->id()) 242cef665d9SGreg Roach ->delete(); 243fe8a65d1SGreg Roach 24404ac9f0fSGreg Roach $url = route('tree-page', [ 245aa6f03bbSGreg Roach 'ged' => $tree->name(), 246fe8a65d1SGreg Roach ]); 247fe8a65d1SGreg Roach 2486ccdf4f0SGreg Roach return redirect($url); 249fe8a65d1SGreg Roach } 2508c2e8227SGreg Roach} 251