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\Carbon; 22use Fisharebest\Webtrees\I18N; 23use Fisharebest\Webtrees\Services\HtmlService; 24use Fisharebest\Webtrees\Tree; 25use Illuminate\Database\Capsule\Manager as DB; 26use Illuminate\Support\Str; 27use Psr\Http\Message\ResponseInterface; 28use Psr\Http\Message\ServerRequestInterface; 29use stdClass; 30use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; 31 32/** 33 * Class FamilyTreeNewsModule 34 */ 35class FamilyTreeNewsModule extends AbstractModule implements ModuleBlockInterface 36{ 37 use ModuleBlockTrait; 38 39 /** @var HtmlService */ 40 private $html_service; 41 42 /** 43 * HtmlBlockModule bootstrap. 44 * 45 * @param HtmlService $html_service 46 */ 47 public function boot(HtmlService $html_service) 48 { 49 $this->html_service = $html_service; 50 } 51 52 /** 53 * A sentence describing what this module does. 54 * 55 * @return string 56 */ 57 public function description(): string 58 { 59 /* I18N: Description of the “News” module */ 60 return I18N::translate('Family news and site announcements.'); 61 } 62 63 /** 64 * Generate the HTML content of this block. 65 * 66 * @param Tree $tree 67 * @param int $block_id 68 * @param string $context 69 * @param string[] $config 70 * 71 * @return string 72 */ 73 public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 74 { 75 $articles = DB::table('news') 76 ->where('gedcom_id', '=', $tree->id()) 77 ->orderByDesc('updated') 78 ->get() 79 ->map(static function (stdClass $row): stdClass { 80 $row->updated = Carbon::make($row->updated); 81 82 return $row; 83 }); 84 85 $content = view('modules/gedcom_news/list', [ 86 'articles' => $articles, 87 'block_id' => $block_id, 88 'limit' => 5, 89 ]); 90 91 if ($context !== self::CONTEXT_EMBED) { 92 return view('modules/block-template', [ 93 'block' => Str::kebab($this->name()), 94 'id' => $block_id, 95 'config_url' => '', 96 'title' => $this->title(), 97 'content' => $content, 98 ]); 99 } 100 101 return $content; 102 } 103 104 /** 105 * How should this module be identified in the control panel, etc.? 106 * 107 * @return string 108 */ 109 public function title(): string 110 { 111 /* I18N: Name of a module */ 112 return I18N::translate('News'); 113 } 114 115 /** 116 * Should this block load asynchronously using AJAX? 117 * 118 * Simple blocks are faster in-line, more complex ones can be loaded later. 119 * 120 * @return bool 121 */ 122 public function loadAjax(): bool 123 { 124 return false; 125 } 126 127 /** 128 * Can this block be shown on the user’s home page? 129 * 130 * @return bool 131 */ 132 public function isUserBlock(): bool 133 { 134 return false; 135 } 136 137 /** 138 * Can this block be shown on the tree’s home page? 139 * 140 * @return bool 141 */ 142 public function isTreeBlock(): bool 143 { 144 return true; 145 } 146 147 /** 148 * @param ServerRequestInterface $request 149 * 150 * @return ResponseInterface 151 */ 152 public function getEditNewsAction(ServerRequestInterface $request): ResponseInterface 153 { 154 $tree = $request->getAttribute('tree'); 155 156 if (!Auth::isManager($tree)) { 157 throw new AccessDeniedHttpException(); 158 } 159 160 $news_id = $request->getQueryParams()['news_id'] ?? ''; 161 162 if ($news_id !== '') { 163 $row = DB::table('news') 164 ->where('news_id', '=', $news_id) 165 ->where('gedcom_id', '=', $tree->id()) 166 ->first(); 167 } else { 168 $row = (object) [ 169 'body' => '', 170 'subject' => '', 171 ]; 172 } 173 174 $title = I18N::translate('Add/edit a journal/news entry'); 175 176 return $this->viewResponse('modules/gedcom_news/edit', [ 177 'body' => $row->body, 178 'news_id' => $news_id, 179 'subject' => $row->subject, 180 'title' => $title, 181 ]); 182 } 183 184 /** 185 * @param ServerRequestInterface $request 186 * 187 * @return ResponseInterface 188 */ 189 public function postEditNewsAction(ServerRequestInterface $request): ResponseInterface 190 { 191 $tree = $request->getAttribute('tree'); 192 193 if (!Auth::isManager($tree)) { 194 throw new AccessDeniedHttpException(); 195 } 196 197 $news_id = $request->getQueryParams()['news_id'] ?? ''; 198 $subject = $request->getParsedBody()['subject']; 199 $body = $request->getParsedBody()['body']; 200 201 $subject = $this->html_service->sanitize($subject); 202 $body = $this->html_service->sanitize($body); 203 204 if ($news_id > 0) { 205 DB::table('news') 206 ->where('news_id', '=', $news_id) 207 ->where('gedcom_id', '=', $tree->id()) 208 ->update([ 209 'body' => $body, 210 'subject' => $subject, 211 ]); 212 } else { 213 DB::table('news')->insert([ 214 'body' => $body, 215 'subject' => $subject, 216 'gedcom_id' => $tree->id(), 217 ]); 218 } 219 220 $url = route('tree-page', [ 221 'ged' => $tree->name(), 222 ]); 223 224 return redirect($url); 225 } 226 227 /** 228 * @param ServerRequestInterface $request 229 * 230 * @return ResponseInterface 231 */ 232 public function postDeleteNewsAction(ServerRequestInterface $request): ResponseInterface 233 { 234 $tree = $request->getAttribute('tree'); 235 $news_id = $request->getQueryParams()['news_id']; 236 237 if (!Auth::isManager($tree)) { 238 throw new AccessDeniedHttpException(); 239 } 240 241 DB::table('news') 242 ->where('news_id', '=', $news_id) 243 ->where('gedcom_id', '=', $tree->id()) 244 ->delete(); 245 246 $url = route('tree-page', [ 247 'ged' => $tree->name(), 248 ]); 249 250 return redirect($url); 251 } 252} 253