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