18c2e8227SGreg Roach<?php 28c2e8227SGreg Roach/** 38c2e8227SGreg Roach * webtrees: online genealogy 41062a142SGreg Roach * Copyright (C) 2018 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 */ 1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 1776692c8bSGreg Roach 180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Database; 200e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 21fe8a65d1SGreg Roachuse Fisharebest\Webtrees\Tree; 22fe8a65d1SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse; 23fe8a65d1SGreg Roachuse Symfony\Component\HttpFoundation\Request; 24fe8a65d1SGreg Roachuse Symfony\Component\HttpFoundation\Response; 25fe8a65d1SGreg Roachuse Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; 268c2e8227SGreg Roach 278c2e8227SGreg Roach/** 288c2e8227SGreg Roach * Class UserJournalModule 298c2e8227SGreg Roach */ 30c1010edaSGreg Roachclass UserJournalModule extends AbstractModule implements ModuleBlockInterface 31c1010edaSGreg Roach{ 3276692c8bSGreg Roach /** 3376692c8bSGreg Roach * Create a new module. 3476692c8bSGreg Roach * 3576692c8bSGreg Roach * @param string $directory Where is this module installed 3676692c8bSGreg Roach */ 37c1010edaSGreg Roach public function __construct($directory) 38c1010edaSGreg Roach { 393bfb94b0SGreg Roach parent::__construct($directory); 403bfb94b0SGreg Roach 413bfb94b0SGreg Roach // Create/update the database tables. 423bfb94b0SGreg Roach Database::updateSchema('\Fisharebest\Webtrees\Module\FamilyTreeNews\Schema', 'NB_SCHEMA_VERSION', 3); 433bfb94b0SGreg Roach } 443bfb94b0SGreg Roach 4576692c8bSGreg Roach /** 4676692c8bSGreg Roach * How should this module be labelled on tabs, menus, etc.? 4776692c8bSGreg Roach * 4876692c8bSGreg Roach * @return string 4976692c8bSGreg Roach */ 50c1010edaSGreg Roach public function getTitle() 51c1010edaSGreg Roach { 52c1010edaSGreg Roach return /* I18N: Name of a module */ 53c1010edaSGreg Roach I18N::translate('Journal'); 548c2e8227SGreg Roach } 558c2e8227SGreg Roach 5676692c8bSGreg Roach /** 5776692c8bSGreg Roach * A sentence describing what this module does. 5876692c8bSGreg Roach * 5976692c8bSGreg Roach * @return string 6076692c8bSGreg Roach */ 61c1010edaSGreg Roach public function getDescription() 62c1010edaSGreg Roach { 63c1010edaSGreg Roach return /* I18N: Description of the “Journal” module */ 64c1010edaSGreg Roach I18N::translate('A private area to record notes or keep a journal.'); 658c2e8227SGreg Roach } 668c2e8227SGreg Roach 6776692c8bSGreg Roach /** 6876692c8bSGreg Roach * Generate the HTML content of this block. 6976692c8bSGreg Roach * 70e490cd80SGreg Roach * @param Tree $tree 7176692c8bSGreg Roach * @param int $block_id 7276692c8bSGreg Roach * @param bool $template 73727f238cSGreg Roach * @param string[] $cfg 7476692c8bSGreg Roach * 7576692c8bSGreg Roach * @return string 7676692c8bSGreg Roach */ 77c1010edaSGreg Roach public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string 78c1010edaSGreg Roach { 79d004f62cSGreg Roach $articles = Database::prepare( 80e5588fb0SGreg 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" 8113abd6f3SGreg Roach )->execute([ 8245d06cf3SGreg Roach 'offset' => WT_TIMESTAMP_OFFSET, 8345d06cf3SGreg Roach 'user_id' => Auth::id(), 8413abd6f3SGreg Roach ])->fetchAll(); 858c2e8227SGreg Roach 86147e99aaSGreg Roach $content = view('modules/user_blog/list', [ 87fe8a65d1SGreg Roach 'articles' => $articles, 88fe8a65d1SGreg Roach 'block_id' => $block_id, 89fe8a65d1SGreg Roach 'limit' => 5, 90fe8a65d1SGreg Roach ]); 918c2e8227SGreg Roach 928c2e8227SGreg Roach if ($template) { 93147e99aaSGreg Roach return view('modules/block-template', [ 949c6524dcSGreg Roach 'block' => str_replace('_', '-', $this->getName()), 959c6524dcSGreg Roach 'id' => $block_id, 969c6524dcSGreg Roach 'config_url' => '', 979c6524dcSGreg Roach 'title' => $this->getTitle(), 989c6524dcSGreg Roach 'content' => $content, 999c6524dcSGreg Roach ]); 1008c2e8227SGreg Roach } else { 1018c2e8227SGreg Roach return $content; 1028c2e8227SGreg Roach } 1038c2e8227SGreg Roach } 1048c2e8227SGreg Roach 1058c2e8227SGreg Roach /** {@inheritdoc} */ 106c1010edaSGreg Roach public function loadAjax(): bool 107c1010edaSGreg Roach { 1088c2e8227SGreg Roach return false; 1098c2e8227SGreg Roach } 1108c2e8227SGreg Roach 1118c2e8227SGreg Roach /** {@inheritdoc} */ 112c1010edaSGreg Roach public function isUserBlock(): bool 113c1010edaSGreg Roach { 1148c2e8227SGreg Roach return true; 1158c2e8227SGreg Roach } 1168c2e8227SGreg Roach 1178c2e8227SGreg Roach /** {@inheritdoc} */ 118c1010edaSGreg Roach public function isGedcomBlock(): bool 119c1010edaSGreg Roach { 1208c2e8227SGreg Roach return false; 1218c2e8227SGreg Roach } 1228c2e8227SGreg Roach 12376692c8bSGreg Roach /** 12476692c8bSGreg Roach * An HTML form to edit block settings 12576692c8bSGreg Roach * 126e490cd80SGreg Roach * @param Tree $tree 12776692c8bSGreg Roach * @param int $block_id 128a9430be8SGreg Roach * 129a9430be8SGreg Roach * @return void 13076692c8bSGreg Roach */ 131c1010edaSGreg Roach public function configureBlock(Tree $tree, int $block_id) 132c1010edaSGreg Roach { 1338c2e8227SGreg Roach } 134fe8a65d1SGreg Roach 135fe8a65d1SGreg Roach /** 136fe8a65d1SGreg Roach * @param Request $request 137fe8a65d1SGreg Roach * 138fe8a65d1SGreg Roach * @return Response 139fe8a65d1SGreg Roach */ 140c1010edaSGreg Roach public function getEditJournalAction(Request $request): Response 141c1010edaSGreg Roach { 142fe8a65d1SGreg Roach if (!Auth::check()) { 143fe8a65d1SGreg Roach throw new AccessDeniedHttpException; 144fe8a65d1SGreg Roach } 145fe8a65d1SGreg Roach 146fe8a65d1SGreg Roach $news_id = $request->get('news_id'); 147fe8a65d1SGreg Roach 148fe8a65d1SGreg Roach if ($news_id > 0) { 149fe8a65d1SGreg Roach $row = Database::prepare( 150fe8a65d1SGreg Roach "SELECT subject, body FROM `##news` WHERE news_id = :news_id AND user_id = :user_id" 151fe8a65d1SGreg Roach )->execute([ 152fe8a65d1SGreg Roach 'news_id' => $news_id, 153fe8a65d1SGreg Roach 'user_id' => Auth::id(), 154fe8a65d1SGreg Roach ])->fetchOneRow(); 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 174*b6db7c1fSGreg Roach * @param Tree $tree 175fe8a65d1SGreg Roach * 176b43958a0SGreg Roach * @return RedirectResponse 177fe8a65d1SGreg Roach */ 178*b6db7c1fSGreg Roach public function postEditJournalAction(Request $request, Tree $tree): RedirectResponse 179c1010edaSGreg Roach { 180fe8a65d1SGreg Roach if (!Auth::check()) { 181fe8a65d1SGreg 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) { 189fe8a65d1SGreg Roach Database::prepare( 190fe8a65d1SGreg Roach "UPDATE `##news` SET subject = :subject, body = :body, updated = CURRENT_TIMESTAMP" . 191fe8a65d1SGreg Roach " WHERE news_id = :news_id AND user_id = :user_id" 192fe8a65d1SGreg Roach )->execute([ 193fe8a65d1SGreg Roach 'subject' => $subject, 194fe8a65d1SGreg Roach 'body' => $body, 195fe8a65d1SGreg Roach 'news_id' => $news_id, 196fe8a65d1SGreg Roach 'user_id' => Auth::id(), 197fe8a65d1SGreg Roach ]); 198fe8a65d1SGreg Roach } else { 199fe8a65d1SGreg Roach Database::prepare( 200fe8a65d1SGreg Roach "INSERT INTO `##news` (user_id, subject, body, updated) VALUES (:user_id, :subject ,:body, CURRENT_TIMESTAMP)" 201fe8a65d1SGreg Roach )->execute([ 202fe8a65d1SGreg Roach 'body' => $body, 203fe8a65d1SGreg Roach 'subject' => $subject, 204fe8a65d1SGreg Roach 'user_id' => Auth::id(), 205fe8a65d1SGreg Roach ]); 206fe8a65d1SGreg Roach } 207fe8a65d1SGreg Roach 208fe8a65d1SGreg Roach $url = route('user-page', [ 209fe8a65d1SGreg Roach 'ged' => $tree->getName(), 210fe8a65d1SGreg Roach ]); 211fe8a65d1SGreg Roach 212fe8a65d1SGreg Roach return new RedirectResponse($url); 213fe8a65d1SGreg Roach } 214fe8a65d1SGreg Roach 215fe8a65d1SGreg Roach /** 216fe8a65d1SGreg Roach * @param Request $request 217*b6db7c1fSGreg Roach * @param Tree $tree 218fe8a65d1SGreg Roach * 219b43958a0SGreg Roach * @return RedirectResponse 220fe8a65d1SGreg Roach */ 221*b6db7c1fSGreg Roach public function postDeleteJournalAction(Request $request, Tree $tree): RedirectResponse 222c1010edaSGreg Roach { 223fe8a65d1SGreg Roach $news_id = $request->get('news_id'); 224fe8a65d1SGreg Roach 225fe8a65d1SGreg Roach if (!Auth::check()) { 226fe8a65d1SGreg Roach throw new AccessDeniedHttpException; 227fe8a65d1SGreg Roach } 228fe8a65d1SGreg Roach 229fe8a65d1SGreg Roach Database::prepare( 230fe8a65d1SGreg Roach "DELETE FROM `##news` WHERE news_id = :news_id AND user_id = :user_id" 231fe8a65d1SGreg Roach )->execute([ 232fe8a65d1SGreg Roach 'news_id' => $news_id, 233fe8a65d1SGreg Roach 'user_id' => Auth::id(), 234fe8a65d1SGreg Roach ]); 235fe8a65d1SGreg Roach 236fe8a65d1SGreg Roach $url = route('user-page', [ 237fe8a65d1SGreg Roach 'ged' => $tree->getName(), 238fe8a65d1SGreg Roach ]); 239fe8a65d1SGreg Roach 240fe8a65d1SGreg Roach return new RedirectResponse($url); 241fe8a65d1SGreg Roach } 2428c2e8227SGreg Roach} 243