18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 589f7189bSGreg Roach * Copyright (C) 2021 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; 234459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon; 2481b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpAccessDeniedException; 258e0e1b25SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\UserPage; 260e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 2750d6f48cSGreg Roachuse Fisharebest\Webtrees\Services\HtmlService; 28fe8a65d1SGreg Roachuse Fisharebest\Webtrees\Tree; 29ec589cf2SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 300dc38e08SGreg Roachuse Illuminate\Database\Query\Expression; 311e7a7a28SGreg Roachuse Illuminate\Support\Str; 326ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 336ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 34ca52a408SGreg Roachuse stdClass; 35f3874e19SGreg Roach 365229eadeSGreg Roachuse function assert; 378c2e8227SGreg Roach 388c2e8227SGreg Roach/** 398c2e8227SGreg Roach * Class UserJournalModule 408c2e8227SGreg Roach */ 4137eb8894SGreg Roachclass UserJournalModule extends AbstractModule implements ModuleBlockInterface 42c1010edaSGreg Roach{ 4349a243cbSGreg Roach use ModuleBlockTrait; 4449a243cbSGreg Roach 4550d6f48cSGreg Roach /** @var HtmlService */ 4650d6f48cSGreg Roach private $html_service; 4750d6f48cSGreg Roach 4850d6f48cSGreg Roach /** 499e18e23bSGreg Roach * HtmlBlockModule constructor. 5050d6f48cSGreg Roach * 5150d6f48cSGreg Roach * @param HtmlService $html_service 5250d6f48cSGreg Roach */ 539e18e23bSGreg Roach public function __construct(HtmlService $html_service) 5450d6f48cSGreg Roach { 5550d6f48cSGreg Roach $this->html_service = $html_service; 5650d6f48cSGreg Roach } 5750d6f48cSGreg Roach 5876692c8bSGreg Roach /** 5976692c8bSGreg Roach * A sentence describing what this module does. 6076692c8bSGreg Roach * 6176692c8bSGreg Roach * @return string 6276692c8bSGreg Roach */ 6349a243cbSGreg Roach public function description(): string 64c1010edaSGreg Roach { 65bbb76c12SGreg Roach /* I18N: Description of the “Journal” module */ 66bbb76c12SGreg Roach return I18N::translate('A private area to record notes or keep a journal.'); 678c2e8227SGreg Roach } 688c2e8227SGreg Roach 6976692c8bSGreg Roach /** 7076692c8bSGreg Roach * Generate the HTML content of this block. 7176692c8bSGreg Roach * 72e490cd80SGreg Roach * @param Tree $tree 7376692c8bSGreg Roach * @param int $block_id 743caaa4d2SGreg Roach * @param string $context 75*09482a55SGreg Roach * @param array<string> $config 7676692c8bSGreg Roach * 7776692c8bSGreg Roach * @return string 7876692c8bSGreg Roach */ 793caaa4d2SGreg Roach public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 80c1010edaSGreg Roach { 81ec589cf2SGreg Roach $articles = DB::table('news') 82ec589cf2SGreg Roach ->where('user_id', '=', Auth::id()) 83ec589cf2SGreg Roach ->orderByDesc('updated') 84ca52a408SGreg Roach ->get() 850b5fd0a6SGreg Roach ->map(static function (stdClass $row): stdClass { 864459dc9aSGreg Roach $row->updated = Carbon::make($row->updated); 87ca52a408SGreg Roach 88ca52a408SGreg Roach return $row; 89ca52a408SGreg Roach }); 90ec589cf2SGreg Roach 91147e99aaSGreg Roach $content = view('modules/user_blog/list', [ 92fe8a65d1SGreg Roach 'articles' => $articles, 93fe8a65d1SGreg Roach 'block_id' => $block_id, 94fe8a65d1SGreg Roach 'limit' => 5, 955f1b387cSGreg Roach 'tree' => $tree, 96fe8a65d1SGreg Roach ]); 978c2e8227SGreg Roach 983caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 99147e99aaSGreg Roach return view('modules/block-template', [ 1001e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 1019c6524dcSGreg Roach 'id' => $block_id, 1029c6524dcSGreg Roach 'config_url' => '', 10349a243cbSGreg Roach 'title' => $this->title(), 1049c6524dcSGreg Roach 'content' => $content, 1059c6524dcSGreg Roach ]); 1068c2e8227SGreg Roach } 107b2ce94c6SRico Sonntag 108b2ce94c6SRico Sonntag return $content; 1098c2e8227SGreg Roach } 1108c2e8227SGreg Roach 1116ccdf4f0SGreg Roach /** 1126ccdf4f0SGreg Roach * How should this module be identified in the control panel, etc.? 1136ccdf4f0SGreg Roach * 1146ccdf4f0SGreg Roach * @return string 1156ccdf4f0SGreg Roach */ 1166ccdf4f0SGreg Roach public function title(): string 1176ccdf4f0SGreg Roach { 1186ccdf4f0SGreg Roach /* I18N: Name of a module */ 1196ccdf4f0SGreg Roach return I18N::translate('Journal'); 1206ccdf4f0SGreg Roach } 1216ccdf4f0SGreg Roach 12250d6f48cSGreg Roach /** 12350d6f48cSGreg Roach * Should this block load asynchronously using AJAX? 12450d6f48cSGreg Roach * 1253caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 12650d6f48cSGreg Roach * 12750d6f48cSGreg Roach * @return bool 12850d6f48cSGreg Roach */ 129c1010edaSGreg Roach public function loadAjax(): bool 130c1010edaSGreg Roach { 1318c2e8227SGreg Roach return false; 1328c2e8227SGreg Roach } 1338c2e8227SGreg Roach 13450d6f48cSGreg Roach /** 13550d6f48cSGreg Roach * Can this block be shown on the user’s home page? 13650d6f48cSGreg Roach * 13750d6f48cSGreg Roach * @return bool 13850d6f48cSGreg Roach */ 139c1010edaSGreg Roach public function isUserBlock(): bool 140c1010edaSGreg Roach { 1418c2e8227SGreg Roach return true; 1428c2e8227SGreg Roach } 1438c2e8227SGreg Roach 14450d6f48cSGreg Roach /** 14550d6f48cSGreg Roach * Can this block be shown on the tree’s home page? 14650d6f48cSGreg Roach * 14750d6f48cSGreg Roach * @return bool 14850d6f48cSGreg Roach */ 14963276d8fSGreg Roach public function isTreeBlock(): bool 150c1010edaSGreg Roach { 1518c2e8227SGreg Roach return false; 1528c2e8227SGreg Roach } 1538c2e8227SGreg Roach 15476692c8bSGreg Roach /** 1556ccdf4f0SGreg Roach * @param ServerRequestInterface $request 156fe8a65d1SGreg Roach * 1576ccdf4f0SGreg Roach * @return ResponseInterface 158fe8a65d1SGreg Roach */ 1596ccdf4f0SGreg Roach public function getEditJournalAction(ServerRequestInterface $request): ResponseInterface 160c1010edaSGreg Roach { 1615f1b387cSGreg Roach $tree = $request->getAttribute('tree'); 1625f1b387cSGreg Roach assert($tree instanceof Tree); 1635f1b387cSGreg Roach 164fe8a65d1SGreg Roach if (!Auth::check()) { 165d501c45dSGreg Roach throw new HttpAccessDeniedException(); 166fe8a65d1SGreg Roach } 167fe8a65d1SGreg Roach 168c9e6b699SGreg Roach $news_id = $request->getQueryParams()['news_id'] ?? ''; 169fe8a65d1SGreg Roach 170c9e6b699SGreg Roach if ($news_id !== '') { 171ec589cf2SGreg Roach $row = DB::table('news') 172ec589cf2SGreg Roach ->where('news_id', '=', $news_id) 173ec589cf2SGreg Roach ->where('user_id', '=', Auth::id()) 174ec589cf2SGreg Roach ->first(); 175fe8a65d1SGreg Roach } else { 176fe8a65d1SGreg Roach $row = (object) [ 177fe8a65d1SGreg Roach 'body' => '', 178fe8a65d1SGreg Roach 'subject' => '', 179fe8a65d1SGreg Roach ]; 180fe8a65d1SGreg Roach } 181fe8a65d1SGreg Roach 182fe8a65d1SGreg Roach $title = I18N::translate('Add/edit a journal/news entry'); 183fe8a65d1SGreg Roach 184147e99aaSGreg Roach return $this->viewResponse('modules/user_blog/edit', [ 185fe8a65d1SGreg Roach 'body' => $row->body, 186fe8a65d1SGreg Roach 'news_id' => $news_id, 187fe8a65d1SGreg Roach 'subject' => $row->subject, 188fe8a65d1SGreg Roach 'title' => $title, 1895f1b387cSGreg Roach 'tree' => $tree, 190fe8a65d1SGreg Roach ]); 191fe8a65d1SGreg Roach } 192fe8a65d1SGreg Roach 193fe8a65d1SGreg Roach /** 1946ccdf4f0SGreg Roach * @param ServerRequestInterface $request 195fe8a65d1SGreg Roach * 1966ccdf4f0SGreg Roach * @return ResponseInterface 197fe8a65d1SGreg Roach */ 19857ab2231SGreg Roach public function postEditJournalAction(ServerRequestInterface $request): ResponseInterface 199c1010edaSGreg Roach { 20057ab2231SGreg Roach $tree = $request->getAttribute('tree'); 20175964c75SGreg Roach assert($tree instanceof Tree); 20257ab2231SGreg Roach 203fe8a65d1SGreg Roach if (!Auth::check()) { 204d501c45dSGreg Roach throw new HttpAccessDeniedException(); 205fe8a65d1SGreg Roach } 206fe8a65d1SGreg Roach 207b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 208b46c87bdSGreg Roach 209c9e6b699SGreg Roach $news_id = $request->getQueryParams()['news_id'] ?? ''; 210b46c87bdSGreg Roach $subject = $params['subject']; 211b46c87bdSGreg Roach $body = $params['body']; 212fe8a65d1SGreg Roach 21350d6f48cSGreg Roach $subject = $this->html_service->sanitize($subject); 21450d6f48cSGreg Roach $body = $this->html_service->sanitize($body); 21550d6f48cSGreg Roach 216c9e6b699SGreg Roach if ($news_id !== '') { 217ec589cf2SGreg Roach DB::table('news') 218ec589cf2SGreg Roach ->where('news_id', '=', $news_id) 219ec589cf2SGreg Roach ->where('user_id', '=', Auth::id()) 220ec589cf2SGreg Roach ->update([ 221fe8a65d1SGreg Roach 'body' => $body, 222ec589cf2SGreg Roach 'subject' => $subject, 2230dc38e08SGreg Roach 'updated' => new Expression('updated'), // See issue #3208 224fe8a65d1SGreg Roach ]); 225fe8a65d1SGreg Roach } else { 226ec589cf2SGreg Roach DB::table('news')->insert([ 227fe8a65d1SGreg Roach 'body' => $body, 228fe8a65d1SGreg Roach 'subject' => $subject, 229fe8a65d1SGreg Roach 'user_id' => Auth::id(), 230fe8a65d1SGreg Roach ]); 231fe8a65d1SGreg Roach } 232fe8a65d1SGreg Roach 2338e0e1b25SGreg Roach $url = route(UserPage::class, ['tree' => $tree->name()]); 234fe8a65d1SGreg Roach 2356ccdf4f0SGreg Roach return redirect($url); 236fe8a65d1SGreg Roach } 237fe8a65d1SGreg Roach 238fe8a65d1SGreg Roach /** 2396ccdf4f0SGreg Roach * @param ServerRequestInterface $request 240fe8a65d1SGreg Roach * 2416ccdf4f0SGreg Roach * @return ResponseInterface 242fe8a65d1SGreg Roach */ 24357ab2231SGreg Roach public function postDeleteJournalAction(ServerRequestInterface $request): ResponseInterface 244c1010edaSGreg Roach { 24557ab2231SGreg Roach $tree = $request->getAttribute('tree'); 2464ea62551SGreg Roach assert($tree instanceof Tree); 2474ea62551SGreg Roach 248c9e6b699SGreg Roach $news_id = $request->getQueryParams()['news_id']; 249fe8a65d1SGreg Roach 250ec589cf2SGreg Roach DB::table('news') 251ec589cf2SGreg Roach ->where('news_id', '=', $news_id) 252ec589cf2SGreg Roach ->where('user_id', '=', Auth::id()) 253ec589cf2SGreg Roach ->delete(); 254fe8a65d1SGreg Roach 2558e0e1b25SGreg Roach $url = route(UserPage::class, ['tree' => $tree->name()]); 256fe8a65d1SGreg Roach 2576ccdf4f0SGreg Roach return redirect($url); 258fe8a65d1SGreg Roach } 2598c2e8227SGreg Roach} 260