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