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\Database; 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 23fe8a65d1SGreg Roachuse Fisharebest\Webtrees\Tree; 24*ec589cf2SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 25fe8a65d1SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse; 26fe8a65d1SGreg Roachuse Symfony\Component\HttpFoundation\Request; 27fe8a65d1SGreg Roachuse Symfony\Component\HttpFoundation\Response; 28fe8a65d1SGreg Roachuse Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; 298c2e8227SGreg Roach 308c2e8227SGreg Roach/** 318c2e8227SGreg Roach * Class UserJournalModule 328c2e8227SGreg Roach */ 33c1010edaSGreg Roachclass UserJournalModule extends AbstractModule implements ModuleBlockInterface 34c1010edaSGreg Roach{ 3576692c8bSGreg Roach /** 3676692c8bSGreg Roach * How should this module be labelled on tabs, menus, etc.? 3776692c8bSGreg Roach * 3876692c8bSGreg Roach * @return string 3976692c8bSGreg Roach */ 408f53f488SRico Sonntag public function getTitle(): string 41c1010edaSGreg Roach { 42bbb76c12SGreg Roach /* I18N: Name of a module */ 43bbb76c12SGreg Roach return I18N::translate('Journal'); 448c2e8227SGreg Roach } 458c2e8227SGreg Roach 4676692c8bSGreg Roach /** 4776692c8bSGreg Roach * A sentence describing what this module does. 4876692c8bSGreg Roach * 4976692c8bSGreg Roach * @return string 5076692c8bSGreg Roach */ 518f53f488SRico Sonntag public function getDescription(): string 52c1010edaSGreg Roach { 53bbb76c12SGreg Roach /* I18N: Description of the “Journal” module */ 54bbb76c12SGreg Roach return I18N::translate('A private area to record notes or keep a journal.'); 558c2e8227SGreg Roach } 568c2e8227SGreg Roach 5776692c8bSGreg Roach /** 5876692c8bSGreg Roach * Generate the HTML content of this block. 5976692c8bSGreg Roach * 60e490cd80SGreg Roach * @param Tree $tree 6176692c8bSGreg Roach * @param int $block_id 625f2ae573SGreg Roach * @param string $ctype 63727f238cSGreg Roach * @param string[] $cfg 6476692c8bSGreg Roach * 6576692c8bSGreg Roach * @return string 6676692c8bSGreg Roach */ 675f2ae573SGreg Roach public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 68c1010edaSGreg Roach { 69d004f62cSGreg Roach $articles = Database::prepare( 70*ec589cf2SGreg Roach "SELECT news_id, user_id, gedcom_id, UNIX_TIMESTAMP(updated) AS updated, subject, body FROM `##news` WHERE user_id = :user_id ORDER BY updated DESC" 7113abd6f3SGreg Roach )->execute([ 7245d06cf3SGreg Roach 'user_id' => Auth::id(), 7313abd6f3SGreg Roach ])->fetchAll(); 748c2e8227SGreg Roach 75*ec589cf2SGreg Roach $articles = DB::table('news') 76*ec589cf2SGreg Roach ->where('user_id', '=', Auth::id()) 77*ec589cf2SGreg Roach ->orderByDesc('updated') 78*ec589cf2SGreg Roach ->select(['news_id', 'user_id', 'gedcom_id', DB::raw('UNIX_TIMESTAMP(updated) AS updated'), 'subject', 'body']) 79*ec589cf2SGreg Roach ->get(); 80*ec589cf2SGreg Roach 81147e99aaSGreg Roach $content = view('modules/user_blog/list', [ 82fe8a65d1SGreg Roach 'articles' => $articles, 83fe8a65d1SGreg Roach 'block_id' => $block_id, 84fe8a65d1SGreg Roach 'limit' => 5, 85fe8a65d1SGreg Roach ]); 868c2e8227SGreg Roach 876a8879feSGreg Roach if ($ctype !== '') { 88147e99aaSGreg Roach return view('modules/block-template', [ 899c6524dcSGreg Roach 'block' => str_replace('_', '-', $this->getName()), 909c6524dcSGreg Roach 'id' => $block_id, 919c6524dcSGreg Roach 'config_url' => '', 929c6524dcSGreg Roach 'title' => $this->getTitle(), 939c6524dcSGreg Roach 'content' => $content, 949c6524dcSGreg Roach ]); 958c2e8227SGreg Roach } 96b2ce94c6SRico Sonntag 97b2ce94c6SRico Sonntag return $content; 988c2e8227SGreg Roach } 998c2e8227SGreg Roach 1008c2e8227SGreg Roach /** {@inheritdoc} */ 101c1010edaSGreg Roach public function loadAjax(): bool 102c1010edaSGreg Roach { 1038c2e8227SGreg Roach return false; 1048c2e8227SGreg Roach } 1058c2e8227SGreg Roach 1068c2e8227SGreg Roach /** {@inheritdoc} */ 107c1010edaSGreg Roach public function isUserBlock(): bool 108c1010edaSGreg Roach { 1098c2e8227SGreg Roach return true; 1108c2e8227SGreg Roach } 1118c2e8227SGreg Roach 1128c2e8227SGreg Roach /** {@inheritdoc} */ 113c1010edaSGreg Roach public function isGedcomBlock(): bool 114c1010edaSGreg Roach { 1158c2e8227SGreg Roach return false; 1168c2e8227SGreg Roach } 1178c2e8227SGreg Roach 11876692c8bSGreg Roach /** 119a45f9889SGreg Roach * Update the configuration for a block. 120a45f9889SGreg Roach * 121a45f9889SGreg Roach * @param Request $request 122a45f9889SGreg Roach * @param int $block_id 123a45f9889SGreg Roach * 124a45f9889SGreg Roach * @return void 125a45f9889SGreg Roach */ 126a45f9889SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id) 127a45f9889SGreg Roach { 128a45f9889SGreg Roach } 129a45f9889SGreg Roach 130a45f9889SGreg Roach /** 13176692c8bSGreg Roach * An HTML form to edit block settings 13276692c8bSGreg Roach * 133e490cd80SGreg Roach * @param Tree $tree 13476692c8bSGreg Roach * @param int $block_id 135a9430be8SGreg Roach * 136a9430be8SGreg Roach * @return void 13776692c8bSGreg Roach */ 138a45f9889SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id) 139c1010edaSGreg Roach { 1408c2e8227SGreg Roach } 141fe8a65d1SGreg Roach 142fe8a65d1SGreg Roach /** 143fe8a65d1SGreg Roach * @param Request $request 144fe8a65d1SGreg Roach * 145fe8a65d1SGreg Roach * @return Response 146fe8a65d1SGreg Roach */ 147c1010edaSGreg Roach public function getEditJournalAction(Request $request): Response 148c1010edaSGreg Roach { 149fe8a65d1SGreg Roach if (!Auth::check()) { 15059f2f229SGreg Roach throw new AccessDeniedHttpException(); 151fe8a65d1SGreg Roach } 152fe8a65d1SGreg Roach 153fe8a65d1SGreg Roach $news_id = $request->get('news_id'); 154fe8a65d1SGreg Roach 155fe8a65d1SGreg Roach if ($news_id > 0) { 156*ec589cf2SGreg Roach $row = DB::table('news') 157*ec589cf2SGreg Roach ->where('news_id', '=', $news_id) 158*ec589cf2SGreg Roach ->where('user_id', '=', Auth::id()) 159*ec589cf2SGreg Roach ->first(); 160fe8a65d1SGreg Roach } else { 161fe8a65d1SGreg Roach $row = (object) [ 162fe8a65d1SGreg Roach 'body' => '', 163fe8a65d1SGreg Roach 'subject' => '', 164fe8a65d1SGreg Roach ]; 165fe8a65d1SGreg Roach } 166fe8a65d1SGreg Roach 167fe8a65d1SGreg Roach $title = I18N::translate('Add/edit a journal/news entry'); 168fe8a65d1SGreg Roach 169147e99aaSGreg Roach return $this->viewResponse('modules/user_blog/edit', [ 170fe8a65d1SGreg Roach 'body' => $row->body, 171fe8a65d1SGreg Roach 'news_id' => $news_id, 172fe8a65d1SGreg Roach 'subject' => $row->subject, 173fe8a65d1SGreg Roach 'title' => $title, 174fe8a65d1SGreg Roach ]); 175fe8a65d1SGreg Roach } 176fe8a65d1SGreg Roach 177fe8a65d1SGreg Roach /** 178fe8a65d1SGreg Roach * @param Request $request 179b6db7c1fSGreg Roach * @param Tree $tree 180fe8a65d1SGreg Roach * 181b43958a0SGreg Roach * @return RedirectResponse 182fe8a65d1SGreg Roach */ 183b6db7c1fSGreg Roach public function postEditJournalAction(Request $request, Tree $tree): RedirectResponse 184c1010edaSGreg Roach { 185fe8a65d1SGreg Roach if (!Auth::check()) { 18659f2f229SGreg Roach throw new AccessDeniedHttpException(); 187fe8a65d1SGreg Roach } 188fe8a65d1SGreg Roach 189fe8a65d1SGreg Roach $news_id = $request->get('news_id'); 190fe8a65d1SGreg Roach $subject = $request->get('subject'); 191fe8a65d1SGreg Roach $body = $request->get('body'); 192fe8a65d1SGreg Roach 193fe8a65d1SGreg Roach if ($news_id > 0) { 194*ec589cf2SGreg Roach DB::table('news') 195*ec589cf2SGreg Roach ->where('news_id', '=', $news_id) 196*ec589cf2SGreg Roach ->where('user_id', '=', Auth::id()) 197*ec589cf2SGreg Roach ->update([ 198fe8a65d1SGreg Roach 'body' => $body, 199*ec589cf2SGreg Roach 'subject' => $subject, 200fe8a65d1SGreg Roach ]); 201fe8a65d1SGreg Roach } else { 202*ec589cf2SGreg Roach DB::table('news')->insert([ 203fe8a65d1SGreg Roach 'body' => $body, 204fe8a65d1SGreg Roach 'subject' => $subject, 205fe8a65d1SGreg Roach 'user_id' => Auth::id(), 206fe8a65d1SGreg Roach ]); 207fe8a65d1SGreg Roach } 208fe8a65d1SGreg Roach 209fe8a65d1SGreg Roach $url = route('user-page', [ 210aa6f03bbSGreg Roach 'ged' => $tree->name(), 211fe8a65d1SGreg Roach ]); 212fe8a65d1SGreg Roach 213fe8a65d1SGreg Roach return new RedirectResponse($url); 214fe8a65d1SGreg Roach } 215fe8a65d1SGreg Roach 216fe8a65d1SGreg Roach /** 217fe8a65d1SGreg Roach * @param Request $request 218b6db7c1fSGreg Roach * @param Tree $tree 219fe8a65d1SGreg Roach * 220b43958a0SGreg Roach * @return RedirectResponse 221fe8a65d1SGreg Roach */ 222b6db7c1fSGreg Roach public function postDeleteJournalAction(Request $request, Tree $tree): RedirectResponse 223c1010edaSGreg Roach { 224fe8a65d1SGreg Roach $news_id = $request->get('news_id'); 225fe8a65d1SGreg Roach 226*ec589cf2SGreg Roach DB::table('news') 227*ec589cf2SGreg Roach ->where('news_id', '=', $news_id) 228*ec589cf2SGreg Roach ->where('user_id', '=', Auth::id()) 229*ec589cf2SGreg Roach ->delete(); 230fe8a65d1SGreg Roach 231fe8a65d1SGreg Roach $url = route('user-page', [ 232aa6f03bbSGreg Roach 'ged' => $tree->name(), 233fe8a65d1SGreg Roach ]); 234fe8a65d1SGreg Roach 235fe8a65d1SGreg Roach return new RedirectResponse($url); 236fe8a65d1SGreg Roach } 2378c2e8227SGreg Roach} 238