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 $articles = Database::prepare( 72 "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" 73 )->execute([ 74 'offset' => WT_TIMESTAMP_OFFSET, 75 'user_id' => Auth::id(), 76 ])->fetchAll(); 77 78 $content = view('blocks/journal', [ 79 'articles' => $articles, 80 'block_id' => $block_id, 81 'limit' => 5, 82 ]); 83 84 if ($template) { 85 return view('blocks/template', [ 86 'block' => str_replace('_', '-', $this->getName()), 87 'id' => $block_id, 88 'config_url' => '', 89 'title' => $this->getTitle(), 90 'content' => $content, 91 ]); 92 } else { 93 return $content; 94 } 95 } 96 97 /** {@inheritdoc} */ 98 public function loadAjax(): bool { 99 return false; 100 } 101 102 /** {@inheritdoc} */ 103 public function isUserBlock(): bool { 104 return true; 105 } 106 107 /** {@inheritdoc} */ 108 public function isGedcomBlock(): bool { 109 return false; 110 } 111 112 /** 113 * An HTML form to edit block settings 114 * 115 * @param int $block_id 116 * 117 * @return void 118 */ 119 public function configureBlock($block_id) { 120 } 121 122 /** 123 * @param Request $request 124 * 125 * @return Response 126 */ 127 public function getEditJournalAction(Request $request): Response { 128 if (!Auth::check()) { 129 throw new AccessDeniedHttpException; 130 } 131 132 $news_id = $request->get('news_id'); 133 134 if ($news_id > 0) { 135 $row = Database::prepare( 136 "SELECT subject, body FROM `##news` WHERE news_id = :news_id AND user_id = :user_id" 137 )->execute([ 138 'news_id' => $news_id, 139 'user_id' => Auth::id(), 140 ])->fetchOneRow(); 141 } else { 142 $row = (object) [ 143 'body' => '', 144 'subject' => '', 145 ]; 146 } 147 148 $title = I18N::translate('Add/edit a journal/news entry'); 149 150 return $this->viewResponse('blocks/journal-edit', [ 151 'body' => $row->body, 152 'news_id' => $news_id, 153 'subject' => $row->subject, 154 'title' => $title, 155 ]); 156 } 157 158 /** 159 * @param Request $request 160 * 161 * @return RedirectResponse 162 */ 163 public function postEditJournalAction(Request $request): RedirectResponse { 164 /** @var Tree $tree */ 165 $tree = $request->attributes->get('tree'); 166 167 if (!Auth::check()) { 168 throw new AccessDeniedHttpException; 169 } 170 171 $news_id = $request->get('news_id'); 172 $subject = $request->get('subject'); 173 $body = $request->get('body'); 174 175 if ($news_id > 0) { 176 Database::prepare( 177 "UPDATE `##news` SET subject = :subject, body = :body, updated = CURRENT_TIMESTAMP" . 178 " WHERE news_id = :news_id AND user_id = :user_id" 179 )->execute([ 180 'subject' => $subject, 181 'body' => $body, 182 'news_id' => $news_id, 183 'user_id' => Auth::id(), 184 ]); 185 } else { 186 Database::prepare( 187 "INSERT INTO `##news` (user_id, subject, body, updated) VALUES (:user_id, :subject ,:body, CURRENT_TIMESTAMP)" 188 )->execute([ 189 'body' => $body, 190 'subject' => $subject, 191 'user_id' => Auth::id(), 192 ]); 193 } 194 195 $url = route('user-page', [ 196 'ged' => $tree->getName(), 197 ]); 198 199 return new RedirectResponse($url); 200 } 201 202 /** 203 * @param Request $request 204 * 205 * @return RedirectResponse 206 */ 207 public function postDeleteJournalAction(Request $request): RedirectResponse { 208 /** @var Tree $tree */ 209 $tree = $request->attributes->get('tree'); 210 211 $news_id = $request->get('news_id'); 212 213 if (!Auth::check()) { 214 throw new AccessDeniedHttpException; 215 } 216 217 Database::prepare( 218 "DELETE FROM `##news` WHERE news_id = :news_id AND user_id = :user_id" 219 )->execute([ 220 'news_id' => $news_id, 221 'user_id' => Auth::id(), 222 ]); 223 224 $url = route('user-page', [ 225 'ged' => $tree->getName(), 226 ]); 227 228 return new RedirectResponse($url); 229 } 230} 231