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 FamilyTreeNewsModule 298c2e8227SGreg Roach */ 30c1010edaSGreg Roachclass FamilyTreeNewsModule extends AbstractModule implements ModuleBlockInterface 31c1010edaSGreg Roach{ 327fafef87SGreg Roach // How to update the database schema for this module 337fafef87SGreg Roach const SCHEMA_TARGET_VERSION = 3; 347fafef87SGreg Roach const SCHEMA_SETTING_NAME = 'NB_SCHEMA_VERSION'; 357fafef87SGreg Roach const SCHEMA_MIGRATION_PREFIX = '\Fisharebest\Webtrees\Module\FamilyTreeNews\Schema'; 367fafef87SGreg Roach 3776692c8bSGreg Roach /** 3876692c8bSGreg Roach * Create a new module. 3976692c8bSGreg Roach * 4076692c8bSGreg Roach * @param string $directory Where is this module installed 4176692c8bSGreg Roach */ 42c1010edaSGreg Roach public function __construct($directory) 43c1010edaSGreg Roach { 443bfb94b0SGreg Roach parent::__construct($directory); 453bfb94b0SGreg Roach 463bfb94b0SGreg Roach // Create/update the database tables. 477fafef87SGreg Roach Database::updateSchema(self::SCHEMA_MIGRATION_PREFIX, self::SCHEMA_SETTING_NAME, self::SCHEMA_TARGET_VERSION); 483bfb94b0SGreg Roach } 493bfb94b0SGreg Roach 5076692c8bSGreg Roach /** 5176692c8bSGreg Roach * How should this module be labelled on tabs, menus, etc.? 5276692c8bSGreg Roach * 5376692c8bSGreg Roach * @return string 5476692c8bSGreg Roach */ 55*8f53f488SRico Sonntag public function getTitle(): string 56c1010edaSGreg Roach { 57bbb76c12SGreg Roach /* I18N: Name of a module */ 58bbb76c12SGreg Roach return I18N::translate('News'); 598c2e8227SGreg Roach } 608c2e8227SGreg Roach 6176692c8bSGreg Roach /** 6276692c8bSGreg Roach * A sentence describing what this module does. 6376692c8bSGreg Roach * 6476692c8bSGreg Roach * @return string 6576692c8bSGreg Roach */ 66*8f53f488SRico Sonntag public function getDescription(): string 67c1010edaSGreg Roach { 68bbb76c12SGreg Roach /* I18N: Description of the “News” module */ 69bbb76c12SGreg Roach return I18N::translate('Family news and site announcements.'); 708c2e8227SGreg Roach } 718c2e8227SGreg Roach 7276692c8bSGreg Roach /** 7376692c8bSGreg Roach * Generate the HTML content of this block. 7476692c8bSGreg Roach * 75e490cd80SGreg Roach * @param Tree $tree 7676692c8bSGreg Roach * @param int $block_id 7776692c8bSGreg Roach * @param bool $template 78727f238cSGreg Roach * @param string[] $cfg 7976692c8bSGreg Roach * 8076692c8bSGreg Roach * @return string 8176692c8bSGreg Roach */ 82c1010edaSGreg Roach public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string 83c1010edaSGreg Roach { 84d004f62cSGreg Roach $articles = Database::prepare( 85e5588fb0SGreg Roach "SELECT news_id, user_id, gedcom_id, UNIX_TIMESTAMP(updated) + :offset AS updated, subject, body FROM `##news` WHERE gedcom_id = :tree_id ORDER BY updated DESC" 8613abd6f3SGreg Roach )->execute([ 8745d06cf3SGreg Roach 'offset' => WT_TIMESTAMP_OFFSET, 88e490cd80SGreg Roach 'tree_id' => $tree->getTreeId(), 8913abd6f3SGreg Roach ])->fetchAll(); 90d004f62cSGreg Roach 91147e99aaSGreg Roach $content = view('modules/gedcom_news/list', [ 92fe8a65d1SGreg Roach 'articles' => $articles, 93fe8a65d1SGreg Roach 'block_id' => $block_id, 94fe8a65d1SGreg Roach 'limit' => 5, 95fe8a65d1SGreg Roach ]); 968c2e8227SGreg Roach 978c2e8227SGreg Roach if ($template) { 98147e99aaSGreg Roach return view('modules/block-template', [ 999c6524dcSGreg Roach 'block' => str_replace('_', '-', $this->getName()), 1009c6524dcSGreg Roach 'id' => $block_id, 1019c6524dcSGreg Roach 'config_url' => '', 1029c6524dcSGreg Roach 'title' => $this->getTitle(), 1039c6524dcSGreg Roach 'content' => $content, 1049c6524dcSGreg Roach ]); 1058c2e8227SGreg Roach } else { 1068c2e8227SGreg Roach return $content; 1078c2e8227SGreg Roach } 1088c2e8227SGreg Roach } 1098c2e8227SGreg Roach 1108c2e8227SGreg Roach /** {@inheritdoc} */ 111c1010edaSGreg Roach public function loadAjax(): bool 112c1010edaSGreg Roach { 1138c2e8227SGreg Roach return false; 1148c2e8227SGreg Roach } 1158c2e8227SGreg Roach 1168c2e8227SGreg Roach /** {@inheritdoc} */ 117c1010edaSGreg Roach public function isUserBlock(): bool 118c1010edaSGreg Roach { 1198c2e8227SGreg Roach return false; 1208c2e8227SGreg Roach } 1218c2e8227SGreg Roach 1228c2e8227SGreg Roach /** {@inheritdoc} */ 123c1010edaSGreg Roach public function isGedcomBlock(): bool 124c1010edaSGreg Roach { 1258c2e8227SGreg Roach return true; 1268c2e8227SGreg Roach } 1278c2e8227SGreg Roach 12876692c8bSGreg Roach /** 129a45f9889SGreg Roach * Update the configuration for a block. 130a45f9889SGreg Roach * 131a45f9889SGreg Roach * @param Request $request 132a45f9889SGreg Roach * @param int $block_id 133a45f9889SGreg Roach * 134a45f9889SGreg Roach * @return void 135a45f9889SGreg Roach */ 136a45f9889SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id) 137a45f9889SGreg Roach { 138a45f9889SGreg Roach } 139a45f9889SGreg Roach 140a45f9889SGreg Roach /** 14176692c8bSGreg Roach * An HTML form to edit block settings 14276692c8bSGreg Roach * 143e490cd80SGreg Roach * @param Tree $tree 14476692c8bSGreg Roach * @param int $block_id 145a9430be8SGreg Roach * 146a9430be8SGreg Roach * @return void 14776692c8bSGreg Roach */ 148a45f9889SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id) 149c1010edaSGreg Roach { 1508c2e8227SGreg Roach } 151fe8a65d1SGreg Roach 152fe8a65d1SGreg Roach /** 153fe8a65d1SGreg Roach * @param Request $request 154b6db7c1fSGreg Roach * @param Tree $tree 155fe8a65d1SGreg Roach * 156fe8a65d1SGreg Roach * @return Response 157fe8a65d1SGreg Roach */ 158b6db7c1fSGreg Roach public function getEditNewsAction(Request $request, Tree $tree): Response 159c1010edaSGreg Roach { 160fe8a65d1SGreg Roach if (!Auth::isManager($tree)) { 16159f2f229SGreg Roach throw new AccessDeniedHttpException(); 162fe8a65d1SGreg Roach } 163fe8a65d1SGreg Roach 164fe8a65d1SGreg Roach $news_id = $request->get('news_id'); 165fe8a65d1SGreg Roach 166fe8a65d1SGreg Roach if ($news_id > 0) { 167fe8a65d1SGreg Roach $row = Database::prepare( 168fe8a65d1SGreg Roach "SELECT subject, body FROM `##news` WHERE news_id = :news_id AND gedcom_id = :tree_id" 169fe8a65d1SGreg Roach )->execute([ 170fe8a65d1SGreg Roach 'news_id' => $news_id, 171fe8a65d1SGreg Roach 'tree_id' => $tree->getTreeId(), 172fe8a65d1SGreg Roach ])->fetchOneRow(); 173fe8a65d1SGreg Roach } else { 174fe8a65d1SGreg Roach $row = (object)[ 175fe8a65d1SGreg Roach 'body' => '', 176fe8a65d1SGreg Roach 'subject' => '', 177fe8a65d1SGreg Roach ]; 178fe8a65d1SGreg Roach } 179fe8a65d1SGreg Roach 180fe8a65d1SGreg Roach $title = I18N::translate('Add/edit a journal/news entry'); 181fe8a65d1SGreg Roach 182147e99aaSGreg Roach return $this->viewResponse('modules/gedcom_news/edit', [ 183fe8a65d1SGreg Roach 'body' => $row->body, 184fe8a65d1SGreg Roach 'news_id' => $news_id, 185fe8a65d1SGreg Roach 'subject' => $row->subject, 186fe8a65d1SGreg Roach 'title' => $title, 187fe8a65d1SGreg Roach ]); 188fe8a65d1SGreg Roach } 189fe8a65d1SGreg Roach 190fe8a65d1SGreg Roach /** 191fe8a65d1SGreg Roach * @param Request $request 192b6db7c1fSGreg Roach * @param Tree $tree 193fe8a65d1SGreg Roach * 194b43958a0SGreg Roach * @return RedirectResponse 195fe8a65d1SGreg Roach */ 196b6db7c1fSGreg Roach public function postEditNewsAction(Request $request, Tree $tree): RedirectResponse 197c1010edaSGreg Roach { 198fe8a65d1SGreg Roach if (!Auth::isManager($tree)) { 19959f2f229SGreg Roach throw new AccessDeniedHttpException(); 200fe8a65d1SGreg Roach } 201fe8a65d1SGreg Roach 202fe8a65d1SGreg Roach $news_id = $request->get('news_id'); 203fe8a65d1SGreg Roach $subject = $request->get('subject'); 204fe8a65d1SGreg Roach $body = $request->get('body'); 205fe8a65d1SGreg Roach 206fe8a65d1SGreg Roach if ($news_id > 0) { 207fe8a65d1SGreg Roach Database::prepare( 208fe8a65d1SGreg Roach "UPDATE `##news` SET subject = :subject, body = :body, updated = CURRENT_TIMESTAMP" . 209fe8a65d1SGreg Roach " WHERE news_id = :news_id AND gedcom_id = :tree_id" 210fe8a65d1SGreg Roach )->execute([ 211fe8a65d1SGreg Roach 'subject' => $subject, 212fe8a65d1SGreg Roach 'body' => $body, 213fe8a65d1SGreg Roach 'news_id' => $news_id, 214fe8a65d1SGreg Roach 'tree_id' => $tree->getTreeId(), 215fe8a65d1SGreg Roach ]); 216fe8a65d1SGreg Roach } else { 217fe8a65d1SGreg Roach Database::prepare( 218fe8a65d1SGreg Roach "INSERT INTO `##news` (gedcom_id, subject, body, updated) VALUES (:tree_id, :subject ,:body, CURRENT_TIMESTAMP)" 219fe8a65d1SGreg Roach )->execute([ 220fe8a65d1SGreg Roach 'body' => $body, 221fe8a65d1SGreg Roach 'subject' => $subject, 222fe8a65d1SGreg Roach 'tree_id' => $tree->getTreeId(), 223fe8a65d1SGreg Roach ]); 224fe8a65d1SGreg Roach } 225fe8a65d1SGreg Roach 22604ac9f0fSGreg Roach $url = route('tree-page', [ 227fe8a65d1SGreg Roach 'ged' => $tree->getName(), 228fe8a65d1SGreg Roach ]); 229fe8a65d1SGreg Roach 230fe8a65d1SGreg Roach return new RedirectResponse($url); 231fe8a65d1SGreg Roach } 232fe8a65d1SGreg Roach 233fe8a65d1SGreg Roach /** 234fe8a65d1SGreg Roach * @param Request $request 235b6db7c1fSGreg Roach * @param Tree $tree 236fe8a65d1SGreg Roach * 237b43958a0SGreg Roach * @return RedirectResponse 238fe8a65d1SGreg Roach */ 239b6db7c1fSGreg Roach public function postDeleteNewsAction(Request $request, Tree $tree): RedirectResponse 240c1010edaSGreg Roach { 241fe8a65d1SGreg Roach $news_id = $request->get('news_id'); 242fe8a65d1SGreg Roach 243fe8a65d1SGreg Roach if (!Auth::isManager($tree)) { 24459f2f229SGreg Roach throw new AccessDeniedHttpException(); 245fe8a65d1SGreg Roach } 246fe8a65d1SGreg Roach 247fe8a65d1SGreg Roach Database::prepare( 248fe8a65d1SGreg Roach "DELETE FROM `##news` WHERE news_id = :news_id AND gedcom_id = :tree_id" 249fe8a65d1SGreg Roach )->execute([ 250fe8a65d1SGreg Roach 'news_id' => $news_id, 251fe8a65d1SGreg Roach 'tree_id' => $tree->getTreeId(), 252fe8a65d1SGreg Roach ]); 253fe8a65d1SGreg Roach 25404ac9f0fSGreg Roach $url = route('tree-page', [ 255fe8a65d1SGreg Roach 'ged' => $tree->getName(), 256fe8a65d1SGreg Roach ]); 257fe8a65d1SGreg Roach 258fe8a65d1SGreg Roach return new RedirectResponse($url); 259fe8a65d1SGreg Roach } 2608c2e8227SGreg Roach} 261