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; 210e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 22fe8a65d1SGreg Roachuse Fisharebest\Webtrees\Tree; 23ec589cf2SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 24fe8a65d1SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse; 25fe8a65d1SGreg Roachuse Symfony\Component\HttpFoundation\Request; 26fe8a65d1SGreg Roachuse Symfony\Component\HttpFoundation\Response; 27fe8a65d1SGreg Roachuse Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; 288c2e8227SGreg Roach 298c2e8227SGreg Roach/** 308c2e8227SGreg Roach * Class UserJournalModule 318c2e8227SGreg Roach */ 3249a243cbSGreg Roachclass UserJournalModule extends AbstractModule implements ModuleInterface, ModuleBlockInterface 33c1010edaSGreg Roach{ 3449a243cbSGreg Roach use ModuleBlockTrait; 3549a243cbSGreg Roach 3676692c8bSGreg Roach /** 3776692c8bSGreg Roach * How should this module be labelled on tabs, menus, etc.? 3876692c8bSGreg Roach * 3976692c8bSGreg Roach * @return string 4076692c8bSGreg Roach */ 4149a243cbSGreg Roach public function title(): string 42c1010edaSGreg Roach { 43bbb76c12SGreg Roach /* I18N: Name of a module */ 44bbb76c12SGreg Roach return I18N::translate('Journal'); 458c2e8227SGreg Roach } 468c2e8227SGreg Roach 4776692c8bSGreg Roach /** 4876692c8bSGreg Roach * A sentence describing what this module does. 4976692c8bSGreg Roach * 5076692c8bSGreg Roach * @return string 5176692c8bSGreg Roach */ 5249a243cbSGreg Roach public function description(): string 53c1010edaSGreg Roach { 54bbb76c12SGreg Roach /* I18N: Description of the “Journal” module */ 55bbb76c12SGreg Roach return I18N::translate('A private area to record notes or keep a journal.'); 568c2e8227SGreg Roach } 578c2e8227SGreg Roach 5876692c8bSGreg Roach /** 5976692c8bSGreg Roach * Generate the HTML content of this block. 6076692c8bSGreg Roach * 61e490cd80SGreg Roach * @param Tree $tree 6276692c8bSGreg Roach * @param int $block_id 635f2ae573SGreg Roach * @param string $ctype 64727f238cSGreg Roach * @param string[] $cfg 6576692c8bSGreg Roach * 6676692c8bSGreg Roach * @return string 6776692c8bSGreg Roach */ 685f2ae573SGreg Roach public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 69c1010edaSGreg Roach { 70ec589cf2SGreg Roach $articles = DB::table('news') 71ec589cf2SGreg Roach ->where('user_id', '=', Auth::id()) 72ec589cf2SGreg Roach ->orderByDesc('updated') 73ec589cf2SGreg Roach ->select(['news_id', 'user_id', 'gedcom_id', DB::raw('UNIX_TIMESTAMP(updated) AS updated'), 'subject', 'body']) 74ec589cf2SGreg Roach ->get(); 75ec589cf2SGreg Roach 76147e99aaSGreg Roach $content = view('modules/user_blog/list', [ 77fe8a65d1SGreg Roach 'articles' => $articles, 78fe8a65d1SGreg Roach 'block_id' => $block_id, 79fe8a65d1SGreg Roach 'limit' => 5, 80fe8a65d1SGreg Roach ]); 818c2e8227SGreg Roach 826a8879feSGreg Roach if ($ctype !== '') { 83147e99aaSGreg Roach return view('modules/block-template', [ 84*26684e68SGreg Roach 'block' => str_replace('_', '-', $this->name()), 859c6524dcSGreg Roach 'id' => $block_id, 869c6524dcSGreg Roach 'config_url' => '', 8749a243cbSGreg Roach 'title' => $this->title(), 889c6524dcSGreg Roach 'content' => $content, 899c6524dcSGreg Roach ]); 908c2e8227SGreg Roach } 91b2ce94c6SRico Sonntag 92b2ce94c6SRico Sonntag return $content; 938c2e8227SGreg Roach } 948c2e8227SGreg Roach 958c2e8227SGreg Roach /** {@inheritdoc} */ 96c1010edaSGreg Roach public function loadAjax(): bool 97c1010edaSGreg Roach { 988c2e8227SGreg Roach return false; 998c2e8227SGreg Roach } 1008c2e8227SGreg Roach 1018c2e8227SGreg Roach /** {@inheritdoc} */ 102c1010edaSGreg Roach public function isUserBlock(): bool 103c1010edaSGreg Roach { 1048c2e8227SGreg Roach return true; 1058c2e8227SGreg Roach } 1068c2e8227SGreg Roach 1078c2e8227SGreg Roach /** {@inheritdoc} */ 108c1010edaSGreg Roach public function isGedcomBlock(): bool 109c1010edaSGreg Roach { 1108c2e8227SGreg Roach return false; 1118c2e8227SGreg Roach } 1128c2e8227SGreg Roach 11376692c8bSGreg Roach /** 114a45f9889SGreg Roach * Update the configuration for a block. 115a45f9889SGreg Roach * 116a45f9889SGreg Roach * @param Request $request 117a45f9889SGreg Roach * @param int $block_id 118a45f9889SGreg Roach * 119a45f9889SGreg Roach * @return void 120a45f9889SGreg Roach */ 121a45f9889SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id) 122a45f9889SGreg Roach { 123a45f9889SGreg Roach } 124a45f9889SGreg Roach 125a45f9889SGreg Roach /** 12676692c8bSGreg Roach * An HTML form to edit block settings 12776692c8bSGreg Roach * 128e490cd80SGreg Roach * @param Tree $tree 12976692c8bSGreg Roach * @param int $block_id 130a9430be8SGreg Roach * 131a9430be8SGreg Roach * @return void 13276692c8bSGreg Roach */ 133a45f9889SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id) 134c1010edaSGreg Roach { 1358c2e8227SGreg Roach } 136fe8a65d1SGreg Roach 137fe8a65d1SGreg Roach /** 138fe8a65d1SGreg Roach * @param Request $request 139fe8a65d1SGreg Roach * 140fe8a65d1SGreg Roach * @return Response 141fe8a65d1SGreg Roach */ 142c1010edaSGreg Roach public function getEditJournalAction(Request $request): Response 143c1010edaSGreg Roach { 144fe8a65d1SGreg Roach if (!Auth::check()) { 14559f2f229SGreg Roach throw new AccessDeniedHttpException(); 146fe8a65d1SGreg Roach } 147fe8a65d1SGreg Roach 148fe8a65d1SGreg Roach $news_id = $request->get('news_id'); 149fe8a65d1SGreg Roach 150fe8a65d1SGreg Roach if ($news_id > 0) { 151ec589cf2SGreg Roach $row = DB::table('news') 152ec589cf2SGreg Roach ->where('news_id', '=', $news_id) 153ec589cf2SGreg Roach ->where('user_id', '=', Auth::id()) 154ec589cf2SGreg Roach ->first(); 155fe8a65d1SGreg Roach } else { 156fe8a65d1SGreg Roach $row = (object) [ 157fe8a65d1SGreg Roach 'body' => '', 158fe8a65d1SGreg Roach 'subject' => '', 159fe8a65d1SGreg Roach ]; 160fe8a65d1SGreg Roach } 161fe8a65d1SGreg Roach 162fe8a65d1SGreg Roach $title = I18N::translate('Add/edit a journal/news entry'); 163fe8a65d1SGreg Roach 164147e99aaSGreg Roach return $this->viewResponse('modules/user_blog/edit', [ 165fe8a65d1SGreg Roach 'body' => $row->body, 166fe8a65d1SGreg Roach 'news_id' => $news_id, 167fe8a65d1SGreg Roach 'subject' => $row->subject, 168fe8a65d1SGreg Roach 'title' => $title, 169fe8a65d1SGreg Roach ]); 170fe8a65d1SGreg Roach } 171fe8a65d1SGreg Roach 172fe8a65d1SGreg Roach /** 173fe8a65d1SGreg Roach * @param Request $request 174b6db7c1fSGreg Roach * @param Tree $tree 175fe8a65d1SGreg Roach * 176b43958a0SGreg Roach * @return RedirectResponse 177fe8a65d1SGreg Roach */ 178b6db7c1fSGreg Roach public function postEditJournalAction(Request $request, Tree $tree): RedirectResponse 179c1010edaSGreg Roach { 180fe8a65d1SGreg Roach if (!Auth::check()) { 18159f2f229SGreg Roach throw new AccessDeniedHttpException(); 182fe8a65d1SGreg Roach } 183fe8a65d1SGreg Roach 184fe8a65d1SGreg Roach $news_id = $request->get('news_id'); 185fe8a65d1SGreg Roach $subject = $request->get('subject'); 186fe8a65d1SGreg Roach $body = $request->get('body'); 187fe8a65d1SGreg Roach 188fe8a65d1SGreg Roach if ($news_id > 0) { 189ec589cf2SGreg Roach DB::table('news') 190ec589cf2SGreg Roach ->where('news_id', '=', $news_id) 191ec589cf2SGreg Roach ->where('user_id', '=', Auth::id()) 192ec589cf2SGreg Roach ->update([ 193fe8a65d1SGreg Roach 'body' => $body, 194ec589cf2SGreg Roach 'subject' => $subject, 195fe8a65d1SGreg Roach ]); 196fe8a65d1SGreg Roach } else { 197ec589cf2SGreg Roach DB::table('news')->insert([ 198fe8a65d1SGreg Roach 'body' => $body, 199fe8a65d1SGreg Roach 'subject' => $subject, 200fe8a65d1SGreg Roach 'user_id' => Auth::id(), 201fe8a65d1SGreg Roach ]); 202fe8a65d1SGreg Roach } 203fe8a65d1SGreg Roach 204fe8a65d1SGreg Roach $url = route('user-page', [ 205aa6f03bbSGreg Roach 'ged' => $tree->name(), 206fe8a65d1SGreg Roach ]); 207fe8a65d1SGreg Roach 208fe8a65d1SGreg Roach return new RedirectResponse($url); 209fe8a65d1SGreg Roach } 210fe8a65d1SGreg Roach 211fe8a65d1SGreg Roach /** 212fe8a65d1SGreg Roach * @param Request $request 213b6db7c1fSGreg Roach * @param Tree $tree 214fe8a65d1SGreg Roach * 215b43958a0SGreg Roach * @return RedirectResponse 216fe8a65d1SGreg Roach */ 217b6db7c1fSGreg Roach public function postDeleteJournalAction(Request $request, Tree $tree): RedirectResponse 218c1010edaSGreg Roach { 219fe8a65d1SGreg Roach $news_id = $request->get('news_id'); 220fe8a65d1SGreg Roach 221ec589cf2SGreg Roach DB::table('news') 222ec589cf2SGreg Roach ->where('news_id', '=', $news_id) 223ec589cf2SGreg Roach ->where('user_id', '=', Auth::id()) 224ec589cf2SGreg Roach ->delete(); 225fe8a65d1SGreg Roach 226fe8a65d1SGreg Roach $url = route('user-page', [ 227aa6f03bbSGreg Roach 'ged' => $tree->name(), 228fe8a65d1SGreg Roach ]); 229fe8a65d1SGreg Roach 230fe8a65d1SGreg Roach return new RedirectResponse($url); 231fe8a65d1SGreg Roach } 2328c2e8227SGreg Roach} 233