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