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 */ 17e7f56f2aSGreg Roachdeclare(strict_types=1); 18e7f56f2aSGreg Roach 1976692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 2076692c8bSGreg Roach 210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 224459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 2450d6f48cSGreg Roachuse Fisharebest\Webtrees\Services\HtmlService; 25fe8a65d1SGreg Roachuse Fisharebest\Webtrees\Tree; 26ec589cf2SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 271e7a7a28SGreg Roachuse Illuminate\Support\Str; 28*5229eadeSGreg Roachuse InvalidArgumentException; 296ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 306ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 31ca52a408SGreg Roachuse stdClass; 32fe8a65d1SGreg Roachuse Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; 33*5229eadeSGreg Roachuse function assert; 348c2e8227SGreg Roach 358c2e8227SGreg Roach/** 368c2e8227SGreg Roach * Class UserJournalModule 378c2e8227SGreg Roach */ 3837eb8894SGreg Roachclass UserJournalModule extends AbstractModule implements ModuleBlockInterface 39c1010edaSGreg Roach{ 4049a243cbSGreg Roach use ModuleBlockTrait; 4149a243cbSGreg Roach 4250d6f48cSGreg Roach /** @var HtmlService */ 4350d6f48cSGreg Roach private $html_service; 4450d6f48cSGreg Roach 4550d6f48cSGreg Roach /** 4650d6f48cSGreg Roach * HtmlBlockModule bootstrap. 4750d6f48cSGreg Roach * 4850d6f48cSGreg Roach * @param HtmlService $html_service 4950d6f48cSGreg Roach */ 5050d6f48cSGreg Roach public function boot(HtmlService $html_service) 5150d6f48cSGreg Roach { 5250d6f48cSGreg Roach $this->html_service = $html_service; 5350d6f48cSGreg Roach } 5450d6f48cSGreg Roach 5576692c8bSGreg Roach /** 5676692c8bSGreg Roach * A sentence describing what this module does. 5776692c8bSGreg Roach * 5876692c8bSGreg Roach * @return string 5976692c8bSGreg Roach */ 6049a243cbSGreg Roach public function description(): string 61c1010edaSGreg Roach { 62bbb76c12SGreg Roach /* I18N: Description of the “Journal” module */ 63bbb76c12SGreg Roach return I18N::translate('A private area to record notes or keep a journal.'); 648c2e8227SGreg Roach } 658c2e8227SGreg Roach 6676692c8bSGreg Roach /** 6776692c8bSGreg Roach * Generate the HTML content of this block. 6876692c8bSGreg Roach * 69e490cd80SGreg Roach * @param Tree $tree 7076692c8bSGreg Roach * @param int $block_id 713caaa4d2SGreg Roach * @param string $context 723caaa4d2SGreg Roach * @param string[] $config 7376692c8bSGreg Roach * 7476692c8bSGreg Roach * @return string 7576692c8bSGreg Roach */ 763caaa4d2SGreg Roach public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 77c1010edaSGreg Roach { 78ec589cf2SGreg Roach $articles = DB::table('news') 79ec589cf2SGreg Roach ->where('user_id', '=', Auth::id()) 80ec589cf2SGreg Roach ->orderByDesc('updated') 81ca52a408SGreg Roach ->get() 820b5fd0a6SGreg Roach ->map(static function (stdClass $row): stdClass { 834459dc9aSGreg Roach $row->updated = Carbon::make($row->updated); 84ca52a408SGreg Roach 85ca52a408SGreg Roach return $row; 86ca52a408SGreg Roach }); 87ec589cf2SGreg Roach 88147e99aaSGreg Roach $content = view('modules/user_blog/list', [ 89fe8a65d1SGreg Roach 'articles' => $articles, 90fe8a65d1SGreg Roach 'block_id' => $block_id, 91fe8a65d1SGreg Roach 'limit' => 5, 92fe8a65d1SGreg Roach ]); 938c2e8227SGreg Roach 943caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 95147e99aaSGreg Roach return view('modules/block-template', [ 961e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 979c6524dcSGreg Roach 'id' => $block_id, 989c6524dcSGreg Roach 'config_url' => '', 9949a243cbSGreg Roach 'title' => $this->title(), 1009c6524dcSGreg Roach 'content' => $content, 1019c6524dcSGreg Roach ]); 1028c2e8227SGreg Roach } 103b2ce94c6SRico Sonntag 104b2ce94c6SRico Sonntag return $content; 1058c2e8227SGreg Roach } 1068c2e8227SGreg Roach 1076ccdf4f0SGreg Roach /** 1086ccdf4f0SGreg Roach * How should this module be identified in the control panel, etc.? 1096ccdf4f0SGreg Roach * 1106ccdf4f0SGreg Roach * @return string 1116ccdf4f0SGreg Roach */ 1126ccdf4f0SGreg Roach public function title(): string 1136ccdf4f0SGreg Roach { 1146ccdf4f0SGreg Roach /* I18N: Name of a module */ 1156ccdf4f0SGreg Roach return I18N::translate('Journal'); 1166ccdf4f0SGreg Roach } 1176ccdf4f0SGreg Roach 11850d6f48cSGreg Roach /** 11950d6f48cSGreg Roach * Should this block load asynchronously using AJAX? 12050d6f48cSGreg Roach * 1213caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 12250d6f48cSGreg Roach * 12350d6f48cSGreg Roach * @return bool 12450d6f48cSGreg Roach */ 125c1010edaSGreg Roach public function loadAjax(): bool 126c1010edaSGreg Roach { 1278c2e8227SGreg Roach return false; 1288c2e8227SGreg Roach } 1298c2e8227SGreg Roach 13050d6f48cSGreg Roach /** 13150d6f48cSGreg Roach * Can this block be shown on the user’s home page? 13250d6f48cSGreg Roach * 13350d6f48cSGreg Roach * @return bool 13450d6f48cSGreg Roach */ 135c1010edaSGreg Roach public function isUserBlock(): bool 136c1010edaSGreg Roach { 1378c2e8227SGreg Roach return true; 1388c2e8227SGreg Roach } 1398c2e8227SGreg Roach 14050d6f48cSGreg Roach /** 14150d6f48cSGreg Roach * Can this block be shown on the tree’s home page? 14250d6f48cSGreg Roach * 14350d6f48cSGreg Roach * @return bool 14450d6f48cSGreg Roach */ 14563276d8fSGreg Roach public function isTreeBlock(): bool 146c1010edaSGreg Roach { 1478c2e8227SGreg Roach return false; 1488c2e8227SGreg Roach } 1498c2e8227SGreg Roach 15076692c8bSGreg Roach /** 1516ccdf4f0SGreg Roach * @param ServerRequestInterface $request 152fe8a65d1SGreg Roach * 1536ccdf4f0SGreg Roach * @return ResponseInterface 154fe8a65d1SGreg Roach */ 1556ccdf4f0SGreg Roach public function getEditJournalAction(ServerRequestInterface $request): ResponseInterface 156c1010edaSGreg Roach { 157fe8a65d1SGreg Roach if (!Auth::check()) { 15859f2f229SGreg Roach throw new AccessDeniedHttpException(); 159fe8a65d1SGreg Roach } 160fe8a65d1SGreg Roach 161c9e6b699SGreg Roach $news_id = $request->getQueryParams()['news_id'] ?? ''; 162fe8a65d1SGreg Roach 163c9e6b699SGreg Roach if ($news_id !== '') { 164ec589cf2SGreg Roach $row = DB::table('news') 165ec589cf2SGreg Roach ->where('news_id', '=', $news_id) 166ec589cf2SGreg Roach ->where('user_id', '=', Auth::id()) 167ec589cf2SGreg Roach ->first(); 168fe8a65d1SGreg Roach } else { 169fe8a65d1SGreg Roach $row = (object) [ 170fe8a65d1SGreg Roach 'body' => '', 171fe8a65d1SGreg Roach 'subject' => '', 172fe8a65d1SGreg Roach ]; 173fe8a65d1SGreg Roach } 174fe8a65d1SGreg Roach 175fe8a65d1SGreg Roach $title = I18N::translate('Add/edit a journal/news entry'); 176fe8a65d1SGreg Roach 177147e99aaSGreg Roach return $this->viewResponse('modules/user_blog/edit', [ 178fe8a65d1SGreg Roach 'body' => $row->body, 179fe8a65d1SGreg Roach 'news_id' => $news_id, 180fe8a65d1SGreg Roach 'subject' => $row->subject, 181fe8a65d1SGreg Roach 'title' => $title, 182fe8a65d1SGreg Roach ]); 183fe8a65d1SGreg Roach } 184fe8a65d1SGreg Roach 185fe8a65d1SGreg Roach /** 1866ccdf4f0SGreg Roach * @param ServerRequestInterface $request 187fe8a65d1SGreg Roach * 1886ccdf4f0SGreg Roach * @return ResponseInterface 189fe8a65d1SGreg Roach */ 19057ab2231SGreg Roach public function postEditJournalAction(ServerRequestInterface $request): ResponseInterface 191c1010edaSGreg Roach { 19257ab2231SGreg Roach $tree = $request->getAttribute('tree'); 193*5229eadeSGreg Roach assert($tree instanceof Tree, new InvalidArgumentException()); 19457ab2231SGreg Roach 195fe8a65d1SGreg Roach if (!Auth::check()) { 19659f2f229SGreg Roach throw new AccessDeniedHttpException(); 197fe8a65d1SGreg Roach } 198fe8a65d1SGreg Roach 199c9e6b699SGreg Roach $news_id = $request->getQueryParams()['news_id'] ?? ''; 200c9e6b699SGreg Roach $subject = $request->getParsedBody()['subject']; 201c9e6b699SGreg Roach $body = $request->getParsedBody()['body']; 202fe8a65d1SGreg Roach 20350d6f48cSGreg Roach $subject = $this->html_service->sanitize($subject); 20450d6f48cSGreg Roach $body = $this->html_service->sanitize($body); 20550d6f48cSGreg Roach 206c9e6b699SGreg Roach if ($news_id !== '') { 207ec589cf2SGreg Roach DB::table('news') 208ec589cf2SGreg Roach ->where('news_id', '=', $news_id) 209ec589cf2SGreg Roach ->where('user_id', '=', Auth::id()) 210ec589cf2SGreg Roach ->update([ 211fe8a65d1SGreg Roach 'body' => $body, 212ec589cf2SGreg Roach 'subject' => $subject, 213fe8a65d1SGreg Roach ]); 214fe8a65d1SGreg Roach } else { 215ec589cf2SGreg Roach DB::table('news')->insert([ 216fe8a65d1SGreg Roach 'body' => $body, 217fe8a65d1SGreg Roach 'subject' => $subject, 218fe8a65d1SGreg Roach 'user_id' => Auth::id(), 219fe8a65d1SGreg Roach ]); 220fe8a65d1SGreg Roach } 221fe8a65d1SGreg Roach 222d72b284aSGreg Roach $url = route('user-page', ['tree' => $tree->name()]); 223fe8a65d1SGreg Roach 2246ccdf4f0SGreg Roach return redirect($url); 225fe8a65d1SGreg Roach } 226fe8a65d1SGreg Roach 227fe8a65d1SGreg Roach /** 2286ccdf4f0SGreg Roach * @param ServerRequestInterface $request 229fe8a65d1SGreg Roach * 2306ccdf4f0SGreg Roach * @return ResponseInterface 231fe8a65d1SGreg Roach */ 23257ab2231SGreg Roach public function postDeleteJournalAction(ServerRequestInterface $request): ResponseInterface 233c1010edaSGreg Roach { 23457ab2231SGreg Roach $tree = $request->getAttribute('tree'); 235c9e6b699SGreg Roach $news_id = $request->getQueryParams()['news_id']; 236fe8a65d1SGreg Roach 237ec589cf2SGreg Roach DB::table('news') 238ec589cf2SGreg Roach ->where('news_id', '=', $news_id) 239ec589cf2SGreg Roach ->where('user_id', '=', Auth::id()) 240ec589cf2SGreg Roach ->delete(); 241fe8a65d1SGreg Roach 242d72b284aSGreg Roach $url = route('user-page', ['tree' => $tree->name()]); 243fe8a65d1SGreg Roach 2446ccdf4f0SGreg Roach return redirect($url); 245fe8a65d1SGreg Roach } 2468c2e8227SGreg Roach} 247