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