1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2019 webtrees development team 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16declare(strict_types=1); 17 18namespace Fisharebest\Webtrees\Module; 19 20use Fisharebest\Webtrees\Auth; 21use Fisharebest\Webtrees\I18N; 22use Fisharebest\Webtrees\Tree; 23use Illuminate\Database\Capsule\Manager as DB; 24use Symfony\Component\HttpFoundation\RedirectResponse; 25use Symfony\Component\HttpFoundation\Request; 26use Symfony\Component\HttpFoundation\Response; 27use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; 28 29/** 30 * Class FamilyTreeNewsModule 31 */ 32class FamilyTreeNewsModule extends AbstractModule implements ModuleBlockInterface 33{ 34 use ModuleBlockTrait; 35 36 /** 37 * How should this module be labelled on tabs, menus, etc.? 38 * 39 * @return string 40 */ 41 public function title(): string 42 { 43 /* I18N: Name of a module */ 44 return I18N::translate('News'); 45 } 46 47 /** 48 * A sentence describing what this module does. 49 * 50 * @return string 51 */ 52 public function description(): string 53 { 54 /* I18N: Description of the “News” module */ 55 return I18N::translate('Family news and site announcements.'); 56 } 57 58 /** 59 * Generate the HTML content of this block. 60 * 61 * @param Tree $tree 62 * @param int $block_id 63 * @param string $ctype 64 * @param string[] $cfg 65 * 66 * @return string 67 */ 68 public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 69 { 70 $articles = DB::table('news') 71 ->where('gedcom_id', '=', $tree->id()) 72 ->orderByDesc('updated') 73 ->select(['news_id', 'user_id', 'gedcom_id', DB::raw('UNIX_TIMESTAMP(updated) AS updated'), 'subject', 'body']) 74 ->get(); 75 76 $content = view('modules/gedcom_news/list', [ 77 'articles' => $articles, 78 'block_id' => $block_id, 79 'limit' => 5, 80 ]); 81 82 if ($ctype !== '') { 83 return view('modules/block-template', [ 84 'block' => str_replace('_', '-', $this->name()), 85 'id' => $block_id, 86 'config_url' => '', 87 'title' => $this->title(), 88 'content' => $content, 89 ]); 90 } 91 92 return $content; 93 } 94 95 /** {@inheritdoc} */ 96 public function loadAjax(): bool 97 { 98 return false; 99 } 100 101 /** {@inheritdoc} */ 102 public function isUserBlock(): bool 103 { 104 return false; 105 } 106 107 /** {@inheritdoc} */ 108 public function isTreeBlock(): bool 109 { 110 return true; 111 } 112 113 /** 114 * Update the configuration for a block. 115 * 116 * @param Request $request 117 * @param int $block_id 118 * 119 * @return void 120 */ 121 public function saveBlockConfiguration(Request $request, int $block_id) 122 { 123 } 124 125 /** 126 * An HTML form to edit block settings 127 * 128 * @param Tree $tree 129 * @param int $block_id 130 * 131 * @return void 132 */ 133 public function editBlockConfiguration(Tree $tree, int $block_id) 134 { 135 } 136 137 /** 138 * @param Request $request 139 * @param Tree $tree 140 * 141 * @return Response 142 */ 143 public function getEditNewsAction(Request $request, Tree $tree): Response 144 { 145 if (!Auth::isManager($tree)) { 146 throw new AccessDeniedHttpException(); 147 } 148 149 $news_id = $request->get('news_id'); 150 151 if ($news_id > 0) { 152 $row = DB::table('news') 153 ->where('news_id', '=', $news_id) 154 ->where('gedcom_id', '=', $tree->id()) 155 ->first(); 156 } else { 157 $row = (object) [ 158 'body' => '', 159 'subject' => '', 160 ]; 161 } 162 163 $title = I18N::translate('Add/edit a journal/news entry'); 164 165 return $this->viewResponse('modules/gedcom_news/edit', [ 166 'body' => $row->body, 167 'news_id' => $news_id, 168 'subject' => $row->subject, 169 'title' => $title, 170 ]); 171 } 172 173 /** 174 * @param Request $request 175 * @param Tree $tree 176 * 177 * @return RedirectResponse 178 */ 179 public function postEditNewsAction(Request $request, Tree $tree): RedirectResponse 180 { 181 if (!Auth::isManager($tree)) { 182 throw new AccessDeniedHttpException(); 183 } 184 185 $news_id = $request->get('news_id'); 186 $subject = $request->get('subject'); 187 $body = $request->get('body'); 188 189 if ($news_id > 0) { 190 DB::table('news') 191 ->where('news_id', '=', $news_id) 192 ->where('gedcom_id', '=', $tree->id()) 193 ->update([ 194 'body' => $body, 195 'subject' => $subject, 196 ]); 197 } else { 198 DB::table('news')->insert([ 199 'body' => $body, 200 'subject' => $subject, 201 'gedcom_id' => $tree->id(), 202 ]); 203 } 204 205 $url = route('tree-page', [ 206 'ged' => $tree->name(), 207 ]); 208 209 return new RedirectResponse($url); 210 } 211 212 /** 213 * @param Request $request 214 * @param Tree $tree 215 * 216 * @return RedirectResponse 217 */ 218 public function postDeleteNewsAction(Request $request, Tree $tree): RedirectResponse 219 { 220 $news_id = $request->get('news_id'); 221 222 if (!Auth::isManager($tree)) { 223 throw new AccessDeniedHttpException(); 224 } 225 226 DB::table('news') 227 ->where('news_id', '=', $news_id) 228 ->where('gedcom_id', '=', $tree->id()) 229 ->delete(); 230 231 $url = route('tree-page', [ 232 'ged' => $tree->name(), 233 ]); 234 235 return new RedirectResponse($url); 236 } 237} 238