1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2018 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\Database; 22use Fisharebest\Webtrees\I18N; 23use Fisharebest\Webtrees\Tree; 24use Symfony\Component\HttpFoundation\RedirectResponse; 25use Symfony\Component\HttpFoundation\Request; 26use Symfony\Component\HttpFoundation\Response; 27use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; 28 29/** 30 * Class UserJournalModule 31 */ 32class UserJournalModule extends AbstractModule implements ModuleBlockInterface 33{ 34 /** 35 * Create a new module. 36 * 37 * @param string $directory Where is this module installed 38 */ 39 public function __construct($directory) 40 { 41 parent::__construct($directory); 42 43 // Create/update the database tables. 44 Database::updateSchema('\Fisharebest\Webtrees\Module\FamilyTreeNews\Schema', 'NB_SCHEMA_VERSION', 3); 45 } 46 47 /** 48 * How should this module be labelled on tabs, menus, etc.? 49 * 50 * @return string 51 */ 52 public function getTitle(): string 53 { 54 /* I18N: Name of a module */ 55 return I18N::translate('Journal'); 56 } 57 58 /** 59 * A sentence describing what this module does. 60 * 61 * @return string 62 */ 63 public function getDescription(): string 64 { 65 /* I18N: Description of the “Journal” module */ 66 return I18N::translate('A private area to record notes or keep a journal.'); 67 } 68 69 /** 70 * Generate the HTML content of this block. 71 * 72 * @param Tree $tree 73 * @param int $block_id 74 * @param bool $template 75 * @param string[] $cfg 76 * 77 * @return string 78 */ 79 public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string 80 { 81 $articles = Database::prepare( 82 "SELECT news_id, user_id, gedcom_id, UNIX_TIMESTAMP(updated) + :offset AS updated, subject, body FROM `##news` WHERE user_id = :user_id ORDER BY updated DESC" 83 )->execute([ 84 'offset' => WT_TIMESTAMP_OFFSET, 85 'user_id' => Auth::id(), 86 ])->fetchAll(); 87 88 $content = view('modules/user_blog/list', [ 89 'articles' => $articles, 90 'block_id' => $block_id, 91 'limit' => 5, 92 ]); 93 94 if ($template) { 95 return view('modules/block-template', [ 96 'block' => str_replace('_', '-', $this->getName()), 97 'id' => $block_id, 98 'config_url' => '', 99 'title' => $this->getTitle(), 100 'content' => $content, 101 ]); 102 } 103 104 return $content; 105 } 106 107 /** {@inheritdoc} */ 108 public function loadAjax(): bool 109 { 110 return false; 111 } 112 113 /** {@inheritdoc} */ 114 public function isUserBlock(): bool 115 { 116 return true; 117 } 118 119 /** {@inheritdoc} */ 120 public function isGedcomBlock(): bool 121 { 122 return false; 123 } 124 125 /** 126 * Update the configuration for a block. 127 * 128 * @param Request $request 129 * @param int $block_id 130 * 131 * @return void 132 */ 133 public function saveBlockConfiguration(Request $request, int $block_id) 134 { 135 } 136 137 /** 138 * An HTML form to edit block settings 139 * 140 * @param Tree $tree 141 * @param int $block_id 142 * 143 * @return void 144 */ 145 public function editBlockConfiguration(Tree $tree, int $block_id) 146 { 147 } 148 149 /** 150 * @param Request $request 151 * 152 * @return Response 153 */ 154 public function getEditJournalAction(Request $request): Response 155 { 156 if (!Auth::check()) { 157 throw new AccessDeniedHttpException(); 158 } 159 160 $news_id = $request->get('news_id'); 161 162 if ($news_id > 0) { 163 $row = Database::prepare( 164 "SELECT subject, body FROM `##news` WHERE news_id = :news_id AND user_id = :user_id" 165 )->execute([ 166 'news_id' => $news_id, 167 'user_id' => Auth::id(), 168 ])->fetchOneRow(); 169 } else { 170 $row = (object) [ 171 'body' => '', 172 'subject' => '', 173 ]; 174 } 175 176 $title = I18N::translate('Add/edit a journal/news entry'); 177 178 return $this->viewResponse('modules/user_blog/edit', [ 179 'body' => $row->body, 180 'news_id' => $news_id, 181 'subject' => $row->subject, 182 'title' => $title, 183 ]); 184 } 185 186 /** 187 * @param Request $request 188 * @param Tree $tree 189 * 190 * @return RedirectResponse 191 */ 192 public function postEditJournalAction(Request $request, Tree $tree): RedirectResponse 193 { 194 if (!Auth::check()) { 195 throw new AccessDeniedHttpException(); 196 } 197 198 $news_id = $request->get('news_id'); 199 $subject = $request->get('subject'); 200 $body = $request->get('body'); 201 202 if ($news_id > 0) { 203 Database::prepare( 204 "UPDATE `##news` SET subject = :subject, body = :body, updated = CURRENT_TIMESTAMP" . 205 " WHERE news_id = :news_id AND user_id = :user_id" 206 )->execute([ 207 'subject' => $subject, 208 'body' => $body, 209 'news_id' => $news_id, 210 'user_id' => Auth::id(), 211 ]); 212 } else { 213 Database::prepare( 214 "INSERT INTO `##news` (user_id, subject, body, updated) VALUES (:user_id, :subject ,:body, CURRENT_TIMESTAMP)" 215 )->execute([ 216 'body' => $body, 217 'subject' => $subject, 218 'user_id' => Auth::id(), 219 ]); 220 } 221 222 $url = route('user-page', [ 223 'ged' => $tree->getName(), 224 ]); 225 226 return new RedirectResponse($url); 227 } 228 229 /** 230 * @param Request $request 231 * @param Tree $tree 232 * 233 * @return RedirectResponse 234 */ 235 public function postDeleteJournalAction(Request $request, Tree $tree): RedirectResponse 236 { 237 $news_id = $request->get('news_id'); 238 239 if (!Auth::check()) { 240 throw new AccessDeniedHttpException(); 241 } 242 243 Database::prepare( 244 "DELETE FROM `##news` WHERE news_id = :news_id AND user_id = :user_id" 245 )->execute([ 246 'news_id' => $news_id, 247 'user_id' => Auth::id(), 248 ]); 249 250 $url = route('user-page', [ 251 'ged' => $tree->getName(), 252 ]); 253 254 return new RedirectResponse($url); 255 } 256} 257