1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2023 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Fisharebest\Webtrees\Auth; 23use Fisharebest\Webtrees\DB; 24use Fisharebest\Webtrees\Http\Exceptions\HttpAccessDeniedException; 25use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException; 26use Fisharebest\Webtrees\Http\RequestHandlers\UserPage; 27use Fisharebest\Webtrees\I18N; 28use Fisharebest\Webtrees\Registry; 29use Fisharebest\Webtrees\Services\HtmlService; 30use Fisharebest\Webtrees\Tree; 31use Fisharebest\Webtrees\Validator; 32use Illuminate\Database\Query\Expression; 33use Illuminate\Support\Str; 34use Psr\Http\Message\ResponseInterface; 35use Psr\Http\Message\ServerRequestInterface; 36 37use function redirect; 38 39/** 40 * Class UserJournalModule 41 */ 42class UserJournalModule extends AbstractModule implements ModuleBlockInterface 43{ 44 use ModuleBlockTrait; 45 46 private HtmlService $html_service; 47 48 /** 49 * @param HtmlService $html_service 50 */ 51 public function __construct(HtmlService $html_service) 52 { 53 $this->html_service = $html_service; 54 } 55 56 /** 57 * A sentence describing what this module does. 58 * 59 * @return string 60 */ 61 public function description(): string 62 { 63 /* I18N: Description of the “Journal” module */ 64 return I18N::translate('A private area to record notes or keep a journal.'); 65 } 66 67 /** 68 * Generate the HTML content of this block. 69 * 70 * @param Tree $tree 71 * @param int $block_id 72 * @param string $context 73 * @param array<string,string> $config 74 * 75 * @return string 76 */ 77 public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 78 { 79 $articles = DB::table('news') 80 ->where('user_id', '=', Auth::id()) 81 ->orderByDesc('updated') 82 ->get() 83 ->map(static function (object $row): object { 84 $row->updated = Registry::timestampFactory()->fromString($row->updated); 85 86 return $row; 87 }); 88 89 $content = view('modules/user_blog/list', [ 90 'articles' => $articles, 91 'block_id' => $block_id, 92 'limit' => 5, 93 'tree' => $tree, 94 ]); 95 96 if ($context !== self::CONTEXT_EMBED) { 97 return view('modules/block-template', [ 98 'block' => Str::kebab($this->name()), 99 'id' => $block_id, 100 'config_url' => '', 101 'title' => $this->title(), 102 'content' => $content, 103 ]); 104 } 105 106 return $content; 107 } 108 109 /** 110 * How should this module be identified in the control panel, etc.? 111 * 112 * @return string 113 */ 114 public function title(): string 115 { 116 /* I18N: Name of a module */ 117 return I18N::translate('Journal'); 118 } 119 120 /** 121 * Should this block load asynchronously using AJAX? 122 * 123 * Simple blocks are faster in-line, more complex ones can be loaded later. 124 * 125 * @return bool 126 */ 127 public function loadAjax(): bool 128 { 129 return false; 130 } 131 132 /** 133 * Can this block be shown on the user’s home page? 134 * 135 * @return bool 136 */ 137 public function isUserBlock(): bool 138 { 139 return true; 140 } 141 142 /** 143 * Can this block be shown on the tree’s home page? 144 * 145 * @return bool 146 */ 147 public function isTreeBlock(): bool 148 { 149 return false; 150 } 151 152 /** 153 * @param ServerRequestInterface $request 154 * 155 * @return ResponseInterface 156 */ 157 public function getEditJournalAction(ServerRequestInterface $request): ResponseInterface 158 { 159 $tree = Validator::attributes($request)->tree(); 160 161 if (!Auth::check()) { 162 throw new HttpAccessDeniedException(); 163 } 164 165 $news_id = Validator::queryParams($request)->integer('news_id', 0); 166 167 if ($news_id !== 0) { 168 $row = DB::table('news') 169 ->where('news_id', '=', $news_id) 170 ->where('user_id', '=', Auth::id()) 171 ->first(); 172 173 // Record was deleted before we could read it? 174 if ($row === null) { 175 throw new HttpNotFoundException(I18N::translate('%s does not exist.', 'news_id:' . $news_id)); 176 } 177 } else { 178 $row = (object)['body' => '', 'subject' => '']; 179 } 180 181 $title = I18N::translate('Add/edit a journal/news entry'); 182 183 return $this->viewResponse('modules/user_blog/edit', [ 184 'body' => $row->body, 185 'news_id' => $news_id, 186 'subject' => $row->subject, 187 'title' => $title, 188 'tree' => $tree, 189 ]); 190 } 191 192 /** 193 * @param ServerRequestInterface $request 194 * 195 * @return ResponseInterface 196 */ 197 public function postEditJournalAction(ServerRequestInterface $request): ResponseInterface 198 { 199 $tree = Validator::attributes($request)->tree(); 200 201 if (!Auth::check()) { 202 throw new HttpAccessDeniedException(); 203 } 204 205 $news_id = Validator::queryParams($request)->integer('news_id', 0); 206 $subject = Validator::parsedBody($request)->string('subject'); 207 $body = Validator::parsedBody($request)->string('body'); 208 209 $subject = $this->html_service->sanitize($subject); 210 $body = $this->html_service->sanitize($body); 211 212 if ($news_id !== 0) { 213 DB::table('news') 214 ->where('news_id', '=', $news_id) 215 ->where('user_id', '=', Auth::id()) 216 ->update([ 217 'body' => $body, 218 'subject' => $subject, 219 'updated' => new Expression('updated'), // See issue #3208 220 ]); 221 } else { 222 DB::table('news')->insert([ 223 'body' => $body, 224 'subject' => $subject, 225 'user_id' => Auth::id(), 226 ]); 227 } 228 229 $url = route(UserPage::class, ['tree' => $tree->name()]); 230 231 return redirect($url); 232 } 233 234 /** 235 * @param ServerRequestInterface $request 236 * 237 * @return ResponseInterface 238 */ 239 public function postDeleteJournalAction(ServerRequestInterface $request): ResponseInterface 240 { 241 $tree = Validator::attributes($request)->tree(); 242 $news_id = Validator::queryParams($request)->integer('news_id'); 243 244 DB::table('news') 245 ->where('news_id', '=', $news_id) 246 ->where('user_id', '=', Auth::id()) 247 ->delete(); 248 249 $url = route(UserPage::class, ['tree' => $tree->name()]); 250 251 return redirect($url); 252 } 253} 254