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 { 52bbb76c12SGreg Roach /* I18N: Name of a module */ 53bbb76c12SGreg Roach return 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 { 63bbb76c12SGreg Roach /* I18N: Description of the “Journal” module */ 64bbb76c12SGreg Roach return 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 /** 124*a45f9889SGreg Roach * Update the configuration for a block. 125*a45f9889SGreg Roach * 126*a45f9889SGreg Roach * @param Request $request 127*a45f9889SGreg Roach * @param int $block_id 128*a45f9889SGreg Roach * 129*a45f9889SGreg Roach * @return void 130*a45f9889SGreg Roach */ 131*a45f9889SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id) 132*a45f9889SGreg Roach { 133*a45f9889SGreg Roach } 134*a45f9889SGreg Roach 135*a45f9889SGreg Roach /** 13676692c8bSGreg Roach * An HTML form to edit block settings 13776692c8bSGreg Roach * 138e490cd80SGreg Roach * @param Tree $tree 13976692c8bSGreg Roach * @param int $block_id 140a9430be8SGreg Roach * 141a9430be8SGreg Roach * @return void 14276692c8bSGreg Roach */ 143*a45f9889SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id) 144c1010edaSGreg Roach { 1458c2e8227SGreg Roach } 146fe8a65d1SGreg Roach 147fe8a65d1SGreg Roach /** 148fe8a65d1SGreg Roach * @param Request $request 149fe8a65d1SGreg Roach * 150fe8a65d1SGreg Roach * @return Response 151fe8a65d1SGreg Roach */ 152c1010edaSGreg Roach public function getEditJournalAction(Request $request): Response 153c1010edaSGreg Roach { 154fe8a65d1SGreg Roach if (!Auth::check()) { 15559f2f229SGreg Roach throw new AccessDeniedHttpException(); 156fe8a65d1SGreg Roach } 157fe8a65d1SGreg Roach 158fe8a65d1SGreg Roach $news_id = $request->get('news_id'); 159fe8a65d1SGreg Roach 160fe8a65d1SGreg Roach if ($news_id > 0) { 161fe8a65d1SGreg Roach $row = Database::prepare( 162fe8a65d1SGreg Roach "SELECT subject, body FROM `##news` WHERE news_id = :news_id AND user_id = :user_id" 163fe8a65d1SGreg Roach )->execute([ 164fe8a65d1SGreg Roach 'news_id' => $news_id, 165fe8a65d1SGreg Roach 'user_id' => Auth::id(), 166fe8a65d1SGreg Roach ])->fetchOneRow(); 167fe8a65d1SGreg Roach } else { 168fe8a65d1SGreg Roach $row = (object)[ 169fe8a65d1SGreg Roach 'body' => '', 170fe8a65d1SGreg Roach 'subject' => '', 171fe8a65d1SGreg Roach ]; 172fe8a65d1SGreg Roach } 173fe8a65d1SGreg Roach 174fe8a65d1SGreg Roach $title = I18N::translate('Add/edit a journal/news entry'); 175fe8a65d1SGreg Roach 176147e99aaSGreg Roach return $this->viewResponse('modules/user_blog/edit', [ 177fe8a65d1SGreg Roach 'body' => $row->body, 178fe8a65d1SGreg Roach 'news_id' => $news_id, 179fe8a65d1SGreg Roach 'subject' => $row->subject, 180fe8a65d1SGreg Roach 'title' => $title, 181fe8a65d1SGreg Roach ]); 182fe8a65d1SGreg Roach } 183fe8a65d1SGreg Roach 184fe8a65d1SGreg Roach /** 185fe8a65d1SGreg Roach * @param Request $request 186b6db7c1fSGreg Roach * @param Tree $tree 187fe8a65d1SGreg Roach * 188b43958a0SGreg Roach * @return RedirectResponse 189fe8a65d1SGreg Roach */ 190b6db7c1fSGreg Roach public function postEditJournalAction(Request $request, Tree $tree): RedirectResponse 191c1010edaSGreg Roach { 192fe8a65d1SGreg Roach if (!Auth::check()) { 19359f2f229SGreg Roach throw new AccessDeniedHttpException(); 194fe8a65d1SGreg Roach } 195fe8a65d1SGreg Roach 196fe8a65d1SGreg Roach $news_id = $request->get('news_id'); 197fe8a65d1SGreg Roach $subject = $request->get('subject'); 198fe8a65d1SGreg Roach $body = $request->get('body'); 199fe8a65d1SGreg Roach 200fe8a65d1SGreg Roach if ($news_id > 0) { 201fe8a65d1SGreg Roach Database::prepare( 202fe8a65d1SGreg Roach "UPDATE `##news` SET subject = :subject, body = :body, updated = CURRENT_TIMESTAMP" . 203fe8a65d1SGreg Roach " WHERE news_id = :news_id AND user_id = :user_id" 204fe8a65d1SGreg Roach )->execute([ 205fe8a65d1SGreg Roach 'subject' => $subject, 206fe8a65d1SGreg Roach 'body' => $body, 207fe8a65d1SGreg Roach 'news_id' => $news_id, 208fe8a65d1SGreg Roach 'user_id' => Auth::id(), 209fe8a65d1SGreg Roach ]); 210fe8a65d1SGreg Roach } else { 211fe8a65d1SGreg Roach Database::prepare( 212fe8a65d1SGreg Roach "INSERT INTO `##news` (user_id, subject, body, updated) VALUES (:user_id, :subject ,:body, CURRENT_TIMESTAMP)" 213fe8a65d1SGreg Roach )->execute([ 214fe8a65d1SGreg Roach 'body' => $body, 215fe8a65d1SGreg Roach 'subject' => $subject, 216fe8a65d1SGreg Roach 'user_id' => Auth::id(), 217fe8a65d1SGreg Roach ]); 218fe8a65d1SGreg Roach } 219fe8a65d1SGreg Roach 220fe8a65d1SGreg Roach $url = route('user-page', [ 221fe8a65d1SGreg Roach 'ged' => $tree->getName(), 222fe8a65d1SGreg Roach ]); 223fe8a65d1SGreg Roach 224fe8a65d1SGreg Roach return new RedirectResponse($url); 225fe8a65d1SGreg Roach } 226fe8a65d1SGreg Roach 227fe8a65d1SGreg Roach /** 228fe8a65d1SGreg Roach * @param Request $request 229b6db7c1fSGreg Roach * @param Tree $tree 230fe8a65d1SGreg Roach * 231b43958a0SGreg Roach * @return RedirectResponse 232fe8a65d1SGreg Roach */ 233b6db7c1fSGreg Roach public function postDeleteJournalAction(Request $request, Tree $tree): RedirectResponse 234c1010edaSGreg Roach { 235fe8a65d1SGreg Roach $news_id = $request->get('news_id'); 236fe8a65d1SGreg Roach 237fe8a65d1SGreg Roach if (!Auth::check()) { 23859f2f229SGreg Roach throw new AccessDeniedHttpException(); 239fe8a65d1SGreg Roach } 240fe8a65d1SGreg Roach 241fe8a65d1SGreg Roach Database::prepare( 242fe8a65d1SGreg Roach "DELETE FROM `##news` WHERE news_id = :news_id AND user_id = :user_id" 243fe8a65d1SGreg Roach )->execute([ 244fe8a65d1SGreg Roach 'news_id' => $news_id, 245fe8a65d1SGreg Roach 'user_id' => Auth::id(), 246fe8a65d1SGreg Roach ]); 247fe8a65d1SGreg Roach 248fe8a65d1SGreg Roach $url = route('user-page', [ 249fe8a65d1SGreg Roach 'ged' => $tree->getName(), 250fe8a65d1SGreg Roach ]); 251fe8a65d1SGreg Roach 252fe8a65d1SGreg Roach return new RedirectResponse($url); 253fe8a65d1SGreg Roach } 2548c2e8227SGreg Roach} 255