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 UserJournalModule 34 */ 35class UserJournalModule 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('Journal'); 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 “Journal” module */ 58 return I18N::translate('A private area to record notes or keep a journal.'); 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('user_id', '=', Auth::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/user_blog/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 true; 112 } 113 114 /** {@inheritdoc} */ 115 public function isTreeBlock(): bool 116 { 117 return false; 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 * 147 * @return Response 148 */ 149 public function getEditJournalAction(Request $request): Response 150 { 151 if (!Auth::check()) { 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('user_id', '=', Auth::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/user_blog/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 postEditJournalAction(Request $request, Tree $tree): RedirectResponse 186 { 187 if (!Auth::check()) { 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('user_id', '=', Auth::id()) 199 ->update([ 200 'body' => $body, 201 'subject' => $subject, 202 ]); 203 } else { 204 DB::table('news')->insert([ 205 'body' => $body, 206 'subject' => $subject, 207 'user_id' => Auth::id(), 208 ]); 209 } 210 211 $url = route('user-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 postDeleteJournalAction(Request $request, Tree $tree): RedirectResponse 225 { 226 $news_id = $request->get('news_id'); 227 228 DB::table('news') 229 ->where('news_id', '=', $news_id) 230 ->where('user_id', '=', Auth::id()) 231 ->delete(); 232 233 $url = route('user-page', [ 234 'ged' => $tree->name(), 235 ]); 236 237 return new RedirectResponse($url); 238 } 239} 240