18c2e8227SGreg Roach<?php 28c2e8227SGreg Roach/** 38c2e8227SGreg Roach * webtrees: online genealogy 4*8fcd0d32SGreg 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; 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 */ 32c1010edaSGreg Roachclass UserJournalModule extends AbstractModule implements ModuleBlockInterface 33c1010edaSGreg Roach{ 3476692c8bSGreg Roach /** 3576692c8bSGreg Roach * How should this module be labelled on tabs, menus, etc.? 3676692c8bSGreg Roach * 3776692c8bSGreg Roach * @return string 3876692c8bSGreg Roach */ 398f53f488SRico Sonntag public function getTitle(): string 40c1010edaSGreg Roach { 41bbb76c12SGreg Roach /* I18N: Name of a module */ 42bbb76c12SGreg Roach return I18N::translate('Journal'); 438c2e8227SGreg Roach } 448c2e8227SGreg Roach 4576692c8bSGreg Roach /** 4676692c8bSGreg Roach * A sentence describing what this module does. 4776692c8bSGreg Roach * 4876692c8bSGreg Roach * @return string 4976692c8bSGreg Roach */ 508f53f488SRico Sonntag public function getDescription(): string 51c1010edaSGreg Roach { 52bbb76c12SGreg Roach /* I18N: Description of the “Journal” module */ 53bbb76c12SGreg Roach return I18N::translate('A private area to record notes or keep a journal.'); 548c2e8227SGreg Roach } 558c2e8227SGreg Roach 5676692c8bSGreg Roach /** 5776692c8bSGreg Roach * Generate the HTML content of this block. 5876692c8bSGreg Roach * 59e490cd80SGreg Roach * @param Tree $tree 6076692c8bSGreg Roach * @param int $block_id 615f2ae573SGreg Roach * @param string $ctype 62727f238cSGreg Roach * @param string[] $cfg 6376692c8bSGreg Roach * 6476692c8bSGreg Roach * @return string 6576692c8bSGreg Roach */ 665f2ae573SGreg Roach public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 67c1010edaSGreg Roach { 68d004f62cSGreg Roach $articles = Database::prepare( 69e5588fb0SGreg Roach "SELECT news_id, user_id, gedcom_id, UNIX_TIMESTAMP(updated) + :offset AS updated, subject, body FROM `##news` WHERE user_id = :user_id ORDER BY updated DESC" 7013abd6f3SGreg Roach )->execute([ 7145d06cf3SGreg Roach 'offset' => WT_TIMESTAMP_OFFSET, 7245d06cf3SGreg Roach 'user_id' => Auth::id(), 7313abd6f3SGreg Roach ])->fetchAll(); 748c2e8227SGreg Roach 75147e99aaSGreg Roach $content = view('modules/user_blog/list', [ 76fe8a65d1SGreg Roach 'articles' => $articles, 77fe8a65d1SGreg Roach 'block_id' => $block_id, 78fe8a65d1SGreg Roach 'limit' => 5, 79fe8a65d1SGreg Roach ]); 808c2e8227SGreg Roach 816a8879feSGreg Roach if ($ctype !== '') { 82147e99aaSGreg Roach return view('modules/block-template', [ 839c6524dcSGreg Roach 'block' => str_replace('_', '-', $this->getName()), 849c6524dcSGreg Roach 'id' => $block_id, 859c6524dcSGreg Roach 'config_url' => '', 869c6524dcSGreg Roach 'title' => $this->getTitle(), 879c6524dcSGreg Roach 'content' => $content, 889c6524dcSGreg Roach ]); 898c2e8227SGreg Roach } 90b2ce94c6SRico Sonntag 91b2ce94c6SRico Sonntag return $content; 928c2e8227SGreg Roach } 938c2e8227SGreg Roach 948c2e8227SGreg Roach /** {@inheritdoc} */ 95c1010edaSGreg Roach public function loadAjax(): bool 96c1010edaSGreg Roach { 978c2e8227SGreg Roach return false; 988c2e8227SGreg Roach } 998c2e8227SGreg Roach 1008c2e8227SGreg Roach /** {@inheritdoc} */ 101c1010edaSGreg Roach public function isUserBlock(): bool 102c1010edaSGreg Roach { 1038c2e8227SGreg Roach return true; 1048c2e8227SGreg Roach } 1058c2e8227SGreg Roach 1068c2e8227SGreg Roach /** {@inheritdoc} */ 107c1010edaSGreg Roach public function isGedcomBlock(): bool 108c1010edaSGreg Roach { 1098c2e8227SGreg Roach return false; 1108c2e8227SGreg Roach } 1118c2e8227SGreg Roach 11276692c8bSGreg Roach /** 113a45f9889SGreg Roach * Update the configuration for a block. 114a45f9889SGreg Roach * 115a45f9889SGreg Roach * @param Request $request 116a45f9889SGreg Roach * @param int $block_id 117a45f9889SGreg Roach * 118a45f9889SGreg Roach * @return void 119a45f9889SGreg Roach */ 120a45f9889SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id) 121a45f9889SGreg Roach { 122a45f9889SGreg Roach } 123a45f9889SGreg Roach 124a45f9889SGreg Roach /** 12576692c8bSGreg Roach * An HTML form to edit block settings 12676692c8bSGreg Roach * 127e490cd80SGreg Roach * @param Tree $tree 12876692c8bSGreg Roach * @param int $block_id 129a9430be8SGreg Roach * 130a9430be8SGreg Roach * @return void 13176692c8bSGreg Roach */ 132a45f9889SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id) 133c1010edaSGreg Roach { 1348c2e8227SGreg Roach } 135fe8a65d1SGreg Roach 136fe8a65d1SGreg Roach /** 137fe8a65d1SGreg Roach * @param Request $request 138fe8a65d1SGreg Roach * 139fe8a65d1SGreg Roach * @return Response 140fe8a65d1SGreg Roach */ 141c1010edaSGreg Roach public function getEditJournalAction(Request $request): Response 142c1010edaSGreg Roach { 143fe8a65d1SGreg Roach if (!Auth::check()) { 14459f2f229SGreg Roach throw new AccessDeniedHttpException(); 145fe8a65d1SGreg Roach } 146fe8a65d1SGreg Roach 147fe8a65d1SGreg Roach $news_id = $request->get('news_id'); 148fe8a65d1SGreg Roach 149fe8a65d1SGreg Roach if ($news_id > 0) { 150fe8a65d1SGreg Roach $row = Database::prepare( 151fe8a65d1SGreg Roach "SELECT subject, body FROM `##news` WHERE news_id = :news_id AND user_id = :user_id" 152fe8a65d1SGreg Roach )->execute([ 153fe8a65d1SGreg Roach 'news_id' => $news_id, 154fe8a65d1SGreg Roach 'user_id' => Auth::id(), 155fe8a65d1SGreg Roach ])->fetchOneRow(); 156fe8a65d1SGreg Roach } else { 157fe8a65d1SGreg Roach $row = (object) [ 158fe8a65d1SGreg Roach 'body' => '', 159fe8a65d1SGreg Roach 'subject' => '', 160fe8a65d1SGreg Roach ]; 161fe8a65d1SGreg Roach } 162fe8a65d1SGreg Roach 163fe8a65d1SGreg Roach $title = I18N::translate('Add/edit a journal/news entry'); 164fe8a65d1SGreg Roach 165147e99aaSGreg Roach return $this->viewResponse('modules/user_blog/edit', [ 166fe8a65d1SGreg Roach 'body' => $row->body, 167fe8a65d1SGreg Roach 'news_id' => $news_id, 168fe8a65d1SGreg Roach 'subject' => $row->subject, 169fe8a65d1SGreg Roach 'title' => $title, 170fe8a65d1SGreg Roach ]); 171fe8a65d1SGreg Roach } 172fe8a65d1SGreg Roach 173fe8a65d1SGreg Roach /** 174fe8a65d1SGreg Roach * @param Request $request 175b6db7c1fSGreg Roach * @param Tree $tree 176fe8a65d1SGreg Roach * 177b43958a0SGreg Roach * @return RedirectResponse 178fe8a65d1SGreg Roach */ 179b6db7c1fSGreg Roach public function postEditJournalAction(Request $request, Tree $tree): RedirectResponse 180c1010edaSGreg Roach { 181fe8a65d1SGreg Roach if (!Auth::check()) { 18259f2f229SGreg Roach throw new AccessDeniedHttpException(); 183fe8a65d1SGreg Roach } 184fe8a65d1SGreg Roach 185fe8a65d1SGreg Roach $news_id = $request->get('news_id'); 186fe8a65d1SGreg Roach $subject = $request->get('subject'); 187fe8a65d1SGreg Roach $body = $request->get('body'); 188fe8a65d1SGreg Roach 189fe8a65d1SGreg Roach if ($news_id > 0) { 190fe8a65d1SGreg Roach Database::prepare( 191fe8a65d1SGreg Roach "UPDATE `##news` SET subject = :subject, body = :body, updated = CURRENT_TIMESTAMP" . 192fe8a65d1SGreg Roach " WHERE news_id = :news_id AND user_id = :user_id" 193fe8a65d1SGreg Roach )->execute([ 194fe8a65d1SGreg Roach 'subject' => $subject, 195fe8a65d1SGreg Roach 'body' => $body, 196fe8a65d1SGreg Roach 'news_id' => $news_id, 197fe8a65d1SGreg Roach 'user_id' => Auth::id(), 198fe8a65d1SGreg Roach ]); 199fe8a65d1SGreg Roach } else { 200fe8a65d1SGreg Roach Database::prepare( 201fe8a65d1SGreg Roach "INSERT INTO `##news` (user_id, subject, body, updated) VALUES (:user_id, :subject ,:body, CURRENT_TIMESTAMP)" 202fe8a65d1SGreg Roach )->execute([ 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 226fe8a65d1SGreg Roach if (!Auth::check()) { 22759f2f229SGreg Roach throw new AccessDeniedHttpException(); 228fe8a65d1SGreg Roach } 229fe8a65d1SGreg Roach 230fe8a65d1SGreg Roach Database::prepare( 231fe8a65d1SGreg Roach "DELETE FROM `##news` WHERE news_id = :news_id AND user_id = :user_id" 232fe8a65d1SGreg Roach )->execute([ 233fe8a65d1SGreg Roach 'news_id' => $news_id, 234fe8a65d1SGreg Roach 'user_id' => Auth::id(), 235fe8a65d1SGreg Roach ]); 236fe8a65d1SGreg Roach 237fe8a65d1SGreg Roach $url = route('user-page', [ 238aa6f03bbSGreg Roach 'ged' => $tree->name(), 239fe8a65d1SGreg Roach ]); 240fe8a65d1SGreg Roach 241fe8a65d1SGreg Roach return new RedirectResponse($url); 242fe8a65d1SGreg Roach } 2438c2e8227SGreg Roach} 244