18c2e8227SGreg Roach<?php 28c2e8227SGreg Roach/** 38c2e8227SGreg Roach * webtrees: online genealogy 48fcd0d32SGreg Roach * Copyright (C) 2019 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; 214459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon; 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 23fe8a65d1SGreg Roachuse Fisharebest\Webtrees\Tree; 24cef665d9SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 251e7a7a28SGreg Roachuse Illuminate\Support\Str; 26ca52a408SGreg Roachuse stdClass; 27fe8a65d1SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse; 28fe8a65d1SGreg Roachuse Symfony\Component\HttpFoundation\Request; 29fe8a65d1SGreg Roachuse Symfony\Component\HttpFoundation\Response; 30fe8a65d1SGreg Roachuse Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; 318c2e8227SGreg Roach 328c2e8227SGreg Roach/** 338c2e8227SGreg Roach * Class FamilyTreeNewsModule 348c2e8227SGreg Roach */ 3537eb8894SGreg Roachclass FamilyTreeNewsModule extends AbstractModule implements ModuleBlockInterface 36c1010edaSGreg Roach{ 3749a243cbSGreg Roach use ModuleBlockTrait; 3849a243cbSGreg Roach 3976692c8bSGreg Roach /** 400cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 4176692c8bSGreg Roach * 4276692c8bSGreg Roach * @return string 4376692c8bSGreg Roach */ 4449a243cbSGreg Roach public function title(): string 45c1010edaSGreg Roach { 46bbb76c12SGreg Roach /* I18N: Name of a module */ 47bbb76c12SGreg Roach return I18N::translate('News'); 488c2e8227SGreg Roach } 498c2e8227SGreg Roach 5076692c8bSGreg Roach /** 5176692c8bSGreg Roach * A sentence describing what this module does. 5276692c8bSGreg Roach * 5376692c8bSGreg Roach * @return string 5476692c8bSGreg Roach */ 5549a243cbSGreg Roach public function description(): string 56c1010edaSGreg Roach { 57bbb76c12SGreg Roach /* I18N: Description of the “News” module */ 58bbb76c12SGreg Roach return I18N::translate('Family news and site announcements.'); 598c2e8227SGreg Roach } 608c2e8227SGreg Roach 6176692c8bSGreg Roach /** 6276692c8bSGreg Roach * Generate the HTML content of this block. 6376692c8bSGreg Roach * 64e490cd80SGreg Roach * @param Tree $tree 6576692c8bSGreg Roach * @param int $block_id 665f2ae573SGreg Roach * @param string $ctype 67727f238cSGreg Roach * @param string[] $cfg 6876692c8bSGreg Roach * 6976692c8bSGreg Roach * @return string 7076692c8bSGreg Roach */ 715f2ae573SGreg Roach public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 72c1010edaSGreg Roach { 73cef665d9SGreg Roach $articles = DB::table('news') 74cef665d9SGreg Roach ->where('gedcom_id', '=', $tree->id()) 75cef665d9SGreg Roach ->orderByDesc('updated') 76ca52a408SGreg Roach ->get() 77*0b5fd0a6SGreg Roach ->map(static function (stdClass $row): stdClass { 784459dc9aSGreg Roach $row->updated = Carbon::make($row->updated); 79ca52a408SGreg Roach 80ca52a408SGreg Roach return $row; 81ca52a408SGreg Roach }); 82d004f62cSGreg Roach 83147e99aaSGreg Roach $content = view('modules/gedcom_news/list', [ 84fe8a65d1SGreg Roach 'articles' => $articles, 85fe8a65d1SGreg Roach 'block_id' => $block_id, 86fe8a65d1SGreg Roach 'limit' => 5, 87fe8a65d1SGreg Roach ]); 888c2e8227SGreg Roach 896a8879feSGreg Roach if ($ctype !== '') { 90147e99aaSGreg Roach return view('modules/block-template', [ 911e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 929c6524dcSGreg Roach 'id' => $block_id, 939c6524dcSGreg Roach 'config_url' => '', 9449a243cbSGreg Roach 'title' => $this->title(), 959c6524dcSGreg Roach 'content' => $content, 969c6524dcSGreg Roach ]); 978c2e8227SGreg Roach } 98b2ce94c6SRico Sonntag 99b2ce94c6SRico Sonntag return $content; 1008c2e8227SGreg Roach } 1018c2e8227SGreg Roach 1028c2e8227SGreg Roach /** {@inheritdoc} */ 103c1010edaSGreg Roach public function loadAjax(): bool 104c1010edaSGreg Roach { 1058c2e8227SGreg Roach return false; 1068c2e8227SGreg Roach } 1078c2e8227SGreg Roach 1088c2e8227SGreg Roach /** {@inheritdoc} */ 109c1010edaSGreg Roach public function isUserBlock(): bool 110c1010edaSGreg Roach { 1118c2e8227SGreg Roach return false; 1128c2e8227SGreg Roach } 1138c2e8227SGreg Roach 1148c2e8227SGreg Roach /** {@inheritdoc} */ 11563276d8fSGreg Roach public function isTreeBlock(): bool 116c1010edaSGreg Roach { 1178c2e8227SGreg Roach return true; 1188c2e8227SGreg Roach } 1198c2e8227SGreg Roach 12076692c8bSGreg Roach /** 121a45f9889SGreg Roach * Update the configuration for a block. 122a45f9889SGreg Roach * 123a45f9889SGreg Roach * @param Request $request 124a45f9889SGreg Roach * @param int $block_id 125a45f9889SGreg Roach * 126a45f9889SGreg Roach * @return void 127a45f9889SGreg Roach */ 128e364afe4SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id): void 129a45f9889SGreg Roach { 130a45f9889SGreg Roach } 131a45f9889SGreg Roach 132a45f9889SGreg Roach /** 13376692c8bSGreg Roach * An HTML form to edit block settings 13476692c8bSGreg Roach * 135e490cd80SGreg Roach * @param Tree $tree 13676692c8bSGreg Roach * @param int $block_id 137a9430be8SGreg Roach * 138a9430be8SGreg Roach * @return void 13976692c8bSGreg Roach */ 140e364afe4SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id): void 141c1010edaSGreg Roach { 1428c2e8227SGreg Roach } 143fe8a65d1SGreg Roach 144fe8a65d1SGreg Roach /** 145fe8a65d1SGreg Roach * @param Request $request 146b6db7c1fSGreg Roach * @param Tree $tree 147fe8a65d1SGreg Roach * 148fe8a65d1SGreg Roach * @return Response 149fe8a65d1SGreg Roach */ 150b6db7c1fSGreg Roach public function getEditNewsAction(Request $request, Tree $tree): Response 151c1010edaSGreg Roach { 152fe8a65d1SGreg Roach if (!Auth::isManager($tree)) { 15359f2f229SGreg Roach throw new AccessDeniedHttpException(); 154fe8a65d1SGreg Roach } 155fe8a65d1SGreg Roach 156fe8a65d1SGreg Roach $news_id = $request->get('news_id'); 157fe8a65d1SGreg Roach 158fe8a65d1SGreg Roach if ($news_id > 0) { 159cef665d9SGreg Roach $row = DB::table('news') 160cef665d9SGreg Roach ->where('news_id', '=', $news_id) 161cef665d9SGreg Roach ->where('gedcom_id', '=', $tree->id()) 162cef665d9SGreg Roach ->first(); 163fe8a65d1SGreg Roach } else { 164fe8a65d1SGreg Roach $row = (object) [ 165fe8a65d1SGreg Roach 'body' => '', 166fe8a65d1SGreg Roach 'subject' => '', 167fe8a65d1SGreg Roach ]; 168fe8a65d1SGreg Roach } 169fe8a65d1SGreg Roach 170fe8a65d1SGreg Roach $title = I18N::translate('Add/edit a journal/news entry'); 171fe8a65d1SGreg Roach 172147e99aaSGreg Roach return $this->viewResponse('modules/gedcom_news/edit', [ 173fe8a65d1SGreg Roach 'body' => $row->body, 174fe8a65d1SGreg Roach 'news_id' => $news_id, 175fe8a65d1SGreg Roach 'subject' => $row->subject, 176fe8a65d1SGreg Roach 'title' => $title, 177fe8a65d1SGreg Roach ]); 178fe8a65d1SGreg Roach } 179fe8a65d1SGreg Roach 180fe8a65d1SGreg Roach /** 181fe8a65d1SGreg Roach * @param Request $request 182b6db7c1fSGreg Roach * @param Tree $tree 183fe8a65d1SGreg Roach * 184b43958a0SGreg Roach * @return RedirectResponse 185fe8a65d1SGreg Roach */ 186b6db7c1fSGreg Roach public function postEditNewsAction(Request $request, Tree $tree): RedirectResponse 187c1010edaSGreg Roach { 188fe8a65d1SGreg Roach if (!Auth::isManager($tree)) { 18959f2f229SGreg Roach throw new AccessDeniedHttpException(); 190fe8a65d1SGreg Roach } 191fe8a65d1SGreg Roach 192fe8a65d1SGreg Roach $news_id = $request->get('news_id'); 193fe8a65d1SGreg Roach $subject = $request->get('subject'); 194fe8a65d1SGreg Roach $body = $request->get('body'); 195fe8a65d1SGreg Roach 196fe8a65d1SGreg Roach if ($news_id > 0) { 197cef665d9SGreg Roach DB::table('news') 198cef665d9SGreg Roach ->where('news_id', '=', $news_id) 199cef665d9SGreg Roach ->where('gedcom_id', '=', $tree->id()) 200cef665d9SGreg Roach ->update([ 201fe8a65d1SGreg Roach 'body' => $body, 202cef665d9SGreg Roach 'subject' => $subject, 203fe8a65d1SGreg Roach ]); 204fe8a65d1SGreg Roach } else { 205cef665d9SGreg Roach DB::table('news')->insert([ 206fe8a65d1SGreg Roach 'body' => $body, 207fe8a65d1SGreg Roach 'subject' => $subject, 208cef665d9SGreg Roach 'gedcom_id' => $tree->id(), 209fe8a65d1SGreg Roach ]); 210fe8a65d1SGreg Roach } 211fe8a65d1SGreg Roach 21204ac9f0fSGreg Roach $url = route('tree-page', [ 213aa6f03bbSGreg Roach 'ged' => $tree->name(), 214fe8a65d1SGreg Roach ]); 215fe8a65d1SGreg Roach 216fe8a65d1SGreg Roach return new RedirectResponse($url); 217fe8a65d1SGreg Roach } 218fe8a65d1SGreg Roach 219fe8a65d1SGreg Roach /** 220fe8a65d1SGreg Roach * @param Request $request 221b6db7c1fSGreg Roach * @param Tree $tree 222fe8a65d1SGreg Roach * 223b43958a0SGreg Roach * @return RedirectResponse 224fe8a65d1SGreg Roach */ 225b6db7c1fSGreg Roach public function postDeleteNewsAction(Request $request, Tree $tree): RedirectResponse 226c1010edaSGreg Roach { 227fe8a65d1SGreg Roach $news_id = $request->get('news_id'); 228fe8a65d1SGreg Roach 229fe8a65d1SGreg Roach if (!Auth::isManager($tree)) { 23059f2f229SGreg Roach throw new AccessDeniedHttpException(); 231fe8a65d1SGreg Roach } 232fe8a65d1SGreg Roach 233cef665d9SGreg Roach DB::table('news') 234cef665d9SGreg Roach ->where('news_id', '=', $news_id) 235cef665d9SGreg Roach ->where('gedcom_id', '=', $tree->id()) 236cef665d9SGreg Roach ->delete(); 237fe8a65d1SGreg Roach 23804ac9f0fSGreg Roach $url = route('tree-page', [ 239aa6f03bbSGreg Roach 'ged' => $tree->name(), 240fe8a65d1SGreg Roach ]); 241fe8a65d1SGreg Roach 242fe8a65d1SGreg Roach return new RedirectResponse($url); 243fe8a65d1SGreg Roach } 2448c2e8227SGreg Roach} 245