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