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 */ 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 * Create a new module. 3676692c8bSGreg Roach * 3776692c8bSGreg Roach * @param string $directory Where is this module installed 3876692c8bSGreg Roach */ 39*8df859b1SGreg Roach public function __construct(string $directory) 40c1010edaSGreg Roach { 413bfb94b0SGreg Roach parent::__construct($directory); 423bfb94b0SGreg Roach 433bfb94b0SGreg Roach // Create/update the database tables. 443bfb94b0SGreg Roach Database::updateSchema('\Fisharebest\Webtrees\Module\FamilyTreeNews\Schema', 'NB_SCHEMA_VERSION', 3); 453bfb94b0SGreg Roach } 463bfb94b0SGreg Roach 4776692c8bSGreg Roach /** 4876692c8bSGreg Roach * How should this module be labelled on tabs, menus, etc.? 4976692c8bSGreg Roach * 5076692c8bSGreg Roach * @return string 5176692c8bSGreg Roach */ 528f53f488SRico Sonntag public function getTitle(): string 53c1010edaSGreg Roach { 54bbb76c12SGreg Roach /* I18N: Name of a module */ 55bbb76c12SGreg Roach return I18N::translate('Journal'); 568c2e8227SGreg Roach } 578c2e8227SGreg Roach 5876692c8bSGreg Roach /** 5976692c8bSGreg Roach * A sentence describing what this module does. 6076692c8bSGreg Roach * 6176692c8bSGreg Roach * @return string 6276692c8bSGreg Roach */ 638f53f488SRico Sonntag public function getDescription(): string 64c1010edaSGreg Roach { 65bbb76c12SGreg Roach /* I18N: Description of the “Journal” module */ 66bbb76c12SGreg Roach return I18N::translate('A private area to record notes or keep a journal.'); 678c2e8227SGreg Roach } 688c2e8227SGreg Roach 6976692c8bSGreg Roach /** 7076692c8bSGreg Roach * Generate the HTML content of this block. 7176692c8bSGreg Roach * 72e490cd80SGreg Roach * @param Tree $tree 7376692c8bSGreg Roach * @param int $block_id 7476692c8bSGreg Roach * @param bool $template 75727f238cSGreg Roach * @param string[] $cfg 7676692c8bSGreg Roach * 7776692c8bSGreg Roach * @return string 7876692c8bSGreg Roach */ 79c1010edaSGreg Roach public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string 80c1010edaSGreg Roach { 81d004f62cSGreg Roach $articles = Database::prepare( 82e5588fb0SGreg 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" 8313abd6f3SGreg Roach )->execute([ 8445d06cf3SGreg Roach 'offset' => WT_TIMESTAMP_OFFSET, 8545d06cf3SGreg Roach 'user_id' => Auth::id(), 8613abd6f3SGreg Roach ])->fetchAll(); 878c2e8227SGreg Roach 88147e99aaSGreg Roach $content = view('modules/user_blog/list', [ 89fe8a65d1SGreg Roach 'articles' => $articles, 90fe8a65d1SGreg Roach 'block_id' => $block_id, 91fe8a65d1SGreg Roach 'limit' => 5, 92fe8a65d1SGreg Roach ]); 938c2e8227SGreg Roach 948c2e8227SGreg Roach if ($template) { 95147e99aaSGreg Roach return view('modules/block-template', [ 969c6524dcSGreg Roach 'block' => str_replace('_', '-', $this->getName()), 979c6524dcSGreg Roach 'id' => $block_id, 989c6524dcSGreg Roach 'config_url' => '', 999c6524dcSGreg Roach 'title' => $this->getTitle(), 1009c6524dcSGreg Roach 'content' => $content, 1019c6524dcSGreg Roach ]); 1028c2e8227SGreg Roach } 103b2ce94c6SRico Sonntag 104b2ce94c6SRico Sonntag return $content; 1058c2e8227SGreg Roach } 1068c2e8227SGreg Roach 1078c2e8227SGreg Roach /** {@inheritdoc} */ 108c1010edaSGreg Roach public function loadAjax(): bool 109c1010edaSGreg Roach { 1108c2e8227SGreg Roach return false; 1118c2e8227SGreg Roach } 1128c2e8227SGreg Roach 1138c2e8227SGreg Roach /** {@inheritdoc} */ 114c1010edaSGreg Roach public function isUserBlock(): bool 115c1010edaSGreg Roach { 1168c2e8227SGreg Roach return true; 1178c2e8227SGreg Roach } 1188c2e8227SGreg Roach 1198c2e8227SGreg Roach /** {@inheritdoc} */ 120c1010edaSGreg Roach public function isGedcomBlock(): bool 121c1010edaSGreg Roach { 1228c2e8227SGreg Roach return false; 1238c2e8227SGreg Roach } 1248c2e8227SGreg Roach 12576692c8bSGreg Roach /** 126a45f9889SGreg Roach * Update the configuration for a block. 127a45f9889SGreg Roach * 128a45f9889SGreg Roach * @param Request $request 129a45f9889SGreg Roach * @param int $block_id 130a45f9889SGreg Roach * 131a45f9889SGreg Roach * @return void 132a45f9889SGreg Roach */ 133a45f9889SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id) 134a45f9889SGreg Roach { 135a45f9889SGreg Roach } 136a45f9889SGreg Roach 137a45f9889SGreg Roach /** 13876692c8bSGreg Roach * An HTML form to edit block settings 13976692c8bSGreg Roach * 140e490cd80SGreg Roach * @param Tree $tree 14176692c8bSGreg Roach * @param int $block_id 142a9430be8SGreg Roach * 143a9430be8SGreg Roach * @return void 14476692c8bSGreg Roach */ 145a45f9889SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id) 146c1010edaSGreg Roach { 1478c2e8227SGreg Roach } 148fe8a65d1SGreg Roach 149fe8a65d1SGreg Roach /** 150fe8a65d1SGreg Roach * @param Request $request 151fe8a65d1SGreg Roach * 152fe8a65d1SGreg Roach * @return Response 153fe8a65d1SGreg Roach */ 154c1010edaSGreg Roach public function getEditJournalAction(Request $request): Response 155c1010edaSGreg Roach { 156fe8a65d1SGreg Roach if (!Auth::check()) { 15759f2f229SGreg Roach throw new AccessDeniedHttpException(); 158fe8a65d1SGreg Roach } 159fe8a65d1SGreg Roach 160fe8a65d1SGreg Roach $news_id = $request->get('news_id'); 161fe8a65d1SGreg Roach 162fe8a65d1SGreg Roach if ($news_id > 0) { 163fe8a65d1SGreg Roach $row = Database::prepare( 164fe8a65d1SGreg Roach "SELECT subject, body FROM `##news` WHERE news_id = :news_id AND user_id = :user_id" 165fe8a65d1SGreg Roach )->execute([ 166fe8a65d1SGreg Roach 'news_id' => $news_id, 167fe8a65d1SGreg Roach 'user_id' => Auth::id(), 168fe8a65d1SGreg Roach ])->fetchOneRow(); 169fe8a65d1SGreg Roach } else { 170fe8a65d1SGreg Roach $row = (object) [ 171fe8a65d1SGreg Roach 'body' => '', 172fe8a65d1SGreg Roach 'subject' => '', 173fe8a65d1SGreg Roach ]; 174fe8a65d1SGreg Roach } 175fe8a65d1SGreg Roach 176fe8a65d1SGreg Roach $title = I18N::translate('Add/edit a journal/news entry'); 177fe8a65d1SGreg Roach 178147e99aaSGreg Roach return $this->viewResponse('modules/user_blog/edit', [ 179fe8a65d1SGreg Roach 'body' => $row->body, 180fe8a65d1SGreg Roach 'news_id' => $news_id, 181fe8a65d1SGreg Roach 'subject' => $row->subject, 182fe8a65d1SGreg Roach 'title' => $title, 183fe8a65d1SGreg Roach ]); 184fe8a65d1SGreg Roach } 185fe8a65d1SGreg Roach 186fe8a65d1SGreg Roach /** 187fe8a65d1SGreg Roach * @param Request $request 188b6db7c1fSGreg Roach * @param Tree $tree 189fe8a65d1SGreg Roach * 190b43958a0SGreg Roach * @return RedirectResponse 191fe8a65d1SGreg Roach */ 192b6db7c1fSGreg Roach public function postEditJournalAction(Request $request, Tree $tree): RedirectResponse 193c1010edaSGreg Roach { 194fe8a65d1SGreg Roach if (!Auth::check()) { 19559f2f229SGreg Roach throw new AccessDeniedHttpException(); 196fe8a65d1SGreg Roach } 197fe8a65d1SGreg Roach 198fe8a65d1SGreg Roach $news_id = $request->get('news_id'); 199fe8a65d1SGreg Roach $subject = $request->get('subject'); 200fe8a65d1SGreg Roach $body = $request->get('body'); 201fe8a65d1SGreg Roach 202fe8a65d1SGreg Roach if ($news_id > 0) { 203fe8a65d1SGreg Roach Database::prepare( 204fe8a65d1SGreg Roach "UPDATE `##news` SET subject = :subject, body = :body, updated = CURRENT_TIMESTAMP" . 205fe8a65d1SGreg Roach " WHERE news_id = :news_id AND user_id = :user_id" 206fe8a65d1SGreg Roach )->execute([ 207fe8a65d1SGreg Roach 'subject' => $subject, 208fe8a65d1SGreg Roach 'body' => $body, 209fe8a65d1SGreg Roach 'news_id' => $news_id, 210fe8a65d1SGreg Roach 'user_id' => Auth::id(), 211fe8a65d1SGreg Roach ]); 212fe8a65d1SGreg Roach } else { 213fe8a65d1SGreg Roach Database::prepare( 214fe8a65d1SGreg Roach "INSERT INTO `##news` (user_id, subject, body, updated) VALUES (:user_id, :subject ,:body, CURRENT_TIMESTAMP)" 215fe8a65d1SGreg Roach )->execute([ 216fe8a65d1SGreg Roach 'body' => $body, 217fe8a65d1SGreg Roach 'subject' => $subject, 218fe8a65d1SGreg Roach 'user_id' => Auth::id(), 219fe8a65d1SGreg Roach ]); 220fe8a65d1SGreg Roach } 221fe8a65d1SGreg Roach 222fe8a65d1SGreg Roach $url = route('user-page', [ 223fe8a65d1SGreg Roach 'ged' => $tree->getName(), 224fe8a65d1SGreg Roach ]); 225fe8a65d1SGreg Roach 226fe8a65d1SGreg Roach return new RedirectResponse($url); 227fe8a65d1SGreg Roach } 228fe8a65d1SGreg Roach 229fe8a65d1SGreg Roach /** 230fe8a65d1SGreg Roach * @param Request $request 231b6db7c1fSGreg Roach * @param Tree $tree 232fe8a65d1SGreg Roach * 233b43958a0SGreg Roach * @return RedirectResponse 234fe8a65d1SGreg Roach */ 235b6db7c1fSGreg Roach public function postDeleteJournalAction(Request $request, Tree $tree): RedirectResponse 236c1010edaSGreg Roach { 237fe8a65d1SGreg Roach $news_id = $request->get('news_id'); 238fe8a65d1SGreg Roach 239fe8a65d1SGreg Roach if (!Auth::check()) { 24059f2f229SGreg Roach throw new AccessDeniedHttpException(); 241fe8a65d1SGreg Roach } 242fe8a65d1SGreg Roach 243fe8a65d1SGreg Roach Database::prepare( 244fe8a65d1SGreg Roach "DELETE FROM `##news` WHERE news_id = :news_id AND user_id = :user_id" 245fe8a65d1SGreg Roach )->execute([ 246fe8a65d1SGreg Roach 'news_id' => $news_id, 247fe8a65d1SGreg Roach 'user_id' => Auth::id(), 248fe8a65d1SGreg Roach ]); 249fe8a65d1SGreg Roach 250fe8a65d1SGreg Roach $url = route('user-page', [ 251fe8a65d1SGreg Roach 'ged' => $tree->getName(), 252fe8a65d1SGreg Roach ]); 253fe8a65d1SGreg Roach 254fe8a65d1SGreg Roach return new RedirectResponse($url); 255fe8a65d1SGreg Roach } 2568c2e8227SGreg Roach} 257