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 */ 30e2a378d3SGreg Roachclass UserJournalModule extends AbstractModule implements ModuleBlockInterface { 3176692c8bSGreg Roach /** 3276692c8bSGreg Roach * Create a new module. 3376692c8bSGreg Roach * 3476692c8bSGreg Roach * @param string $directory Where is this module installed 3576692c8bSGreg Roach */ 363bfb94b0SGreg Roach public function __construct($directory) { 373bfb94b0SGreg Roach parent::__construct($directory); 383bfb94b0SGreg Roach 393bfb94b0SGreg Roach // Create/update the database tables. 403bfb94b0SGreg Roach Database::updateSchema('\Fisharebest\Webtrees\Module\FamilyTreeNews\Schema', 'NB_SCHEMA_VERSION', 3); 413bfb94b0SGreg Roach } 423bfb94b0SGreg Roach 4376692c8bSGreg Roach /** 4476692c8bSGreg Roach * How should this module be labelled on tabs, menus, etc.? 4576692c8bSGreg Roach * 4676692c8bSGreg Roach * @return string 4776692c8bSGreg Roach */ 488c2e8227SGreg Roach public function getTitle() { 498c2e8227SGreg Roach return /* I18N: Name of a module */ I18N::translate('Journal'); 508c2e8227SGreg Roach } 518c2e8227SGreg Roach 5276692c8bSGreg Roach /** 5376692c8bSGreg Roach * A sentence describing what this module does. 5476692c8bSGreg Roach * 5576692c8bSGreg Roach * @return string 5676692c8bSGreg Roach */ 578c2e8227SGreg Roach public function getDescription() { 588c2e8227SGreg Roach return /* I18N: Description of the “Journal” module */ I18N::translate('A private area to record notes or keep a journal.'); 598c2e8227SGreg Roach } 608c2e8227SGreg Roach 6176692c8bSGreg Roach /** 6276692c8bSGreg Roach * Generate the HTML content of this block. 6376692c8bSGreg Roach * 6476692c8bSGreg Roach * @param int $block_id 6576692c8bSGreg Roach * @param bool $template 66727f238cSGreg Roach * @param string[] $cfg 6776692c8bSGreg Roach * 6876692c8bSGreg Roach * @return string 6976692c8bSGreg Roach */ 70a9430be8SGreg Roach public function getBlock($block_id, $template = true, $cfg = []): string { 71fe8a65d1SGreg Roach global $WT_TREE; 728c2e8227SGreg Roach 73d004f62cSGreg Roach $articles = Database::prepare( 7445d06cf3SGreg Roach "SELECT SQL_CACHE 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" 7513abd6f3SGreg Roach )->execute([ 7645d06cf3SGreg Roach 'offset' => WT_TIMESTAMP_OFFSET, 7745d06cf3SGreg Roach 'user_id' => Auth::id(), 7813abd6f3SGreg Roach ])->fetchAll(); 798c2e8227SGreg Roach 80fe8a65d1SGreg Roach $content = view('blocks/journal', [ 81fe8a65d1SGreg Roach 'articles' => $articles, 82fe8a65d1SGreg Roach 'block_id' => $block_id, 83fe8a65d1SGreg Roach 'limit' => 5, 84fe8a65d1SGreg Roach ]); 858c2e8227SGreg Roach 868c2e8227SGreg Roach if ($template) { 87225e381fSGreg Roach return view('blocks/template', [ 889c6524dcSGreg Roach 'block' => str_replace('_', '-', $this->getName()), 899c6524dcSGreg Roach 'id' => $block_id, 909c6524dcSGreg Roach 'config_url' => '', 919c6524dcSGreg Roach 'title' => $this->getTitle(), 929c6524dcSGreg Roach 'content' => $content, 939c6524dcSGreg Roach ]); 948c2e8227SGreg Roach } else { 958c2e8227SGreg Roach return $content; 968c2e8227SGreg Roach } 978c2e8227SGreg Roach } 988c2e8227SGreg Roach 998c2e8227SGreg Roach /** {@inheritdoc} */ 100a9430be8SGreg Roach public function loadAjax(): bool { 1018c2e8227SGreg Roach return false; 1028c2e8227SGreg Roach } 1038c2e8227SGreg Roach 1048c2e8227SGreg Roach /** {@inheritdoc} */ 105a9430be8SGreg Roach public function isUserBlock(): bool { 1068c2e8227SGreg Roach return true; 1078c2e8227SGreg Roach } 1088c2e8227SGreg Roach 1098c2e8227SGreg Roach /** {@inheritdoc} */ 110a9430be8SGreg Roach public function isGedcomBlock(): bool { 1118c2e8227SGreg Roach return false; 1128c2e8227SGreg Roach } 1138c2e8227SGreg Roach 11476692c8bSGreg Roach /** 11576692c8bSGreg Roach * An HTML form to edit block settings 11676692c8bSGreg Roach * 11776692c8bSGreg Roach * @param int $block_id 118a9430be8SGreg Roach * 119a9430be8SGreg Roach * @return void 12076692c8bSGreg Roach */ 121be9a728cSGreg Roach public function configureBlock($block_id) { 1228c2e8227SGreg Roach } 123fe8a65d1SGreg Roach 124fe8a65d1SGreg Roach /** 125fe8a65d1SGreg Roach * @param Request $request 126fe8a65d1SGreg Roach * 127fe8a65d1SGreg Roach * @return Response 128fe8a65d1SGreg Roach */ 129fe8a65d1SGreg Roach public function getEditJournalAction(Request $request): Response { 130fe8a65d1SGreg Roach /** @var Tree $tree */ 131fe8a65d1SGreg Roach $tree = $request->attributes->get('tree'); 132fe8a65d1SGreg Roach 133fe8a65d1SGreg Roach if (!Auth::check()) { 134fe8a65d1SGreg Roach throw new AccessDeniedHttpException; 135fe8a65d1SGreg Roach } 136fe8a65d1SGreg Roach 137fe8a65d1SGreg Roach $news_id = $request->get('news_id'); 138fe8a65d1SGreg Roach 139fe8a65d1SGreg Roach if ($news_id > 0) { 140fe8a65d1SGreg Roach $row = Database::prepare( 141fe8a65d1SGreg Roach "SELECT subject, body FROM `##news` WHERE news_id = :news_id AND user_id = :user_id" 142fe8a65d1SGreg Roach )->execute([ 143fe8a65d1SGreg Roach 'news_id' => $news_id, 144fe8a65d1SGreg Roach 'user_id' => Auth::id(), 145fe8a65d1SGreg Roach ])->fetchOneRow(); 146fe8a65d1SGreg Roach } else { 147fe8a65d1SGreg Roach $row = (object) [ 148fe8a65d1SGreg Roach 'body' => '', 149fe8a65d1SGreg Roach 'subject' => '', 150fe8a65d1SGreg Roach ]; 151fe8a65d1SGreg Roach } 152fe8a65d1SGreg Roach 153fe8a65d1SGreg Roach $title = I18N::translate('Add/edit a journal/news entry'); 154fe8a65d1SGreg Roach 155fe8a65d1SGreg Roach return $this->viewResponse('blocks/journal-edit', [ 156fe8a65d1SGreg Roach 'body' => $row->body, 157fe8a65d1SGreg Roach 'news_id' => $news_id, 158fe8a65d1SGreg Roach 'subject' => $row->subject, 159fe8a65d1SGreg Roach 'title' => $title, 160fe8a65d1SGreg Roach ]); 161fe8a65d1SGreg Roach } 162fe8a65d1SGreg Roach 163fe8a65d1SGreg Roach /** 164fe8a65d1SGreg Roach * @param Request $request 165fe8a65d1SGreg Roach * 166*b43958a0SGreg Roach * @return RedirectResponse 167fe8a65d1SGreg Roach */ 168fe8a65d1SGreg Roach public function postEditJournalAction(Request $request): RedirectResponse { 169fe8a65d1SGreg Roach /** @var Tree $tree */ 170fe8a65d1SGreg Roach $tree = $request->attributes->get('tree'); 171fe8a65d1SGreg Roach 172fe8a65d1SGreg Roach if (!Auth::check()) { 173fe8a65d1SGreg Roach throw new AccessDeniedHttpException; 174fe8a65d1SGreg Roach } 175fe8a65d1SGreg Roach 176fe8a65d1SGreg Roach $news_id = $request->get('news_id'); 177fe8a65d1SGreg Roach $subject = $request->get('subject'); 178fe8a65d1SGreg Roach $body = $request->get('body'); 179fe8a65d1SGreg Roach 180fe8a65d1SGreg Roach if ($news_id > 0) { 181fe8a65d1SGreg Roach Database::prepare( 182fe8a65d1SGreg Roach "UPDATE `##news` SET subject = :subject, body = :body, updated = CURRENT_TIMESTAMP" . 183fe8a65d1SGreg Roach " WHERE news_id = :news_id AND user_id = :user_id" 184fe8a65d1SGreg Roach )->execute([ 185fe8a65d1SGreg Roach 'subject' => $subject, 186fe8a65d1SGreg Roach 'body' => $body, 187fe8a65d1SGreg Roach 'news_id' => $news_id, 188fe8a65d1SGreg Roach 'user_id' => Auth::id(), 189fe8a65d1SGreg Roach ]); 190fe8a65d1SGreg Roach } else { 191fe8a65d1SGreg Roach Database::prepare( 192fe8a65d1SGreg Roach "INSERT INTO `##news` (user_id, subject, body, updated) VALUES (:user_id, :subject ,:body, CURRENT_TIMESTAMP)" 193fe8a65d1SGreg Roach )->execute([ 194fe8a65d1SGreg Roach 'body' => $body, 195fe8a65d1SGreg Roach 'subject' => $subject, 196fe8a65d1SGreg Roach 'user_id' => Auth::id(), 197fe8a65d1SGreg Roach ]); 198fe8a65d1SGreg Roach } 199fe8a65d1SGreg Roach 200fe8a65d1SGreg Roach $url = route('user-page', [ 201fe8a65d1SGreg Roach 'ged' => $tree->getName(), 202fe8a65d1SGreg Roach ]); 203fe8a65d1SGreg Roach 204fe8a65d1SGreg Roach return new RedirectResponse($url); 205fe8a65d1SGreg Roach } 206fe8a65d1SGreg Roach 207fe8a65d1SGreg Roach /** 208fe8a65d1SGreg Roach * @param Request $request 209fe8a65d1SGreg Roach * 210*b43958a0SGreg Roach * @return RedirectResponse 211fe8a65d1SGreg Roach */ 212fe8a65d1SGreg Roach public function postDeleteJournalAction(Request $request): RedirectResponse { 213fe8a65d1SGreg Roach /** @var Tree $tree */ 214fe8a65d1SGreg Roach $tree = $request->attributes->get('tree'); 215fe8a65d1SGreg Roach 216fe8a65d1SGreg Roach $news_id = $request->get('news_id'); 217fe8a65d1SGreg Roach 218fe8a65d1SGreg Roach if (!Auth::check()) { 219fe8a65d1SGreg Roach throw new AccessDeniedHttpException; 220fe8a65d1SGreg Roach } 221fe8a65d1SGreg Roach 222fe8a65d1SGreg Roach Database::prepare( 223fe8a65d1SGreg Roach "DELETE FROM `##news` WHERE news_id = :news_id AND user_id = :user_id" 224fe8a65d1SGreg Roach )->execute([ 225fe8a65d1SGreg Roach 'news_id' => $news_id, 226fe8a65d1SGreg Roach 'user_id' => Auth::id(), 227fe8a65d1SGreg Roach ]); 228fe8a65d1SGreg Roach 229fe8a65d1SGreg Roach $url = route('user-page', [ 230fe8a65d1SGreg Roach 'ged' => $tree->getName(), 231fe8a65d1SGreg Roach ]); 232fe8a65d1SGreg Roach 233fe8a65d1SGreg Roach return new RedirectResponse($url); 234fe8a65d1SGreg Roach } 2358c2e8227SGreg Roach} 236