xref: /webtrees/app/Module/UserJournalModule.php (revision ecf66805cdf435857ce4ed0eb577cfd1296f8989)
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\Functions\FunctionsDate;
21use Fisharebest\Webtrees\I18N;
22use Fisharebest\Webtrees\View;
23
24/**
25 * Class UserJournalModule
26 */
27class UserJournalModule extends AbstractModule implements ModuleBlockInterface {
28	/**
29	 * Create a new module.
30	 *
31	 * @param string $directory Where is this module installed
32	 */
33	public function __construct($directory) {
34		parent::__construct($directory);
35
36		// Create/update the database tables.
37		Database::updateSchema('\Fisharebest\Webtrees\Module\FamilyTreeNews\Schema', 'NB_SCHEMA_VERSION', 3);
38	}
39
40	/**
41	 * How should this module be labelled on tabs, menus, etc.?
42	 *
43	 * @return string
44	 */
45	public function getTitle() {
46		return /* I18N: Name of a module */ I18N::translate('Journal');
47	}
48
49	/**
50	 * A sentence describing what this module does.
51	 *
52	 * @return string
53	 */
54	public function getDescription() {
55		return /* I18N: Description of the “Journal” module */ I18N::translate('A private area to record notes or keep a journal.');
56	}
57
58	/**
59	 * Generate the HTML content of this block.
60	 *
61	 * @param int      $block_id
62	 * @param bool     $template
63	 * @param string[] $cfg
64	 *
65	 * @return string
66	 */
67	public function getBlock($block_id, $template = true, $cfg = []): string {
68		global $ctype, $WT_TREE;
69
70		$articles = Database::prepare(
71			"SELECT SQL_CACHE 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"
72		)->execute([
73			'offset'  => WT_TIMESTAMP_OFFSET,
74			'user_id' => Auth::id(),
75		])->fetchAll();
76
77		$content = '';
78
79		if (empty($articles)) {
80			$content .= '<p>' . I18N::translate('You have not created any journal items.') . '</p>';
81		}
82
83		foreach ($articles as $article) {
84			$content .= '<div class="journal_box">';
85			$content .= '<div class="news_title">' . e($article->subject) . '</div>';
86			$content .= '<div class="news_date">' . FunctionsDate::formatTimestamp($article->updated) . '</div>';
87			if ($article->body == strip_tags($article->body)) {
88				$article->body = nl2br($article->body, false);
89			}
90			$content .= $article->body;
91			$content .= '<a href="editnews.php?news_id=' . $article->news_id . '&amp;ctype=user&amp;ged=' . $WT_TREE->getNameHtml() . '">' . I18N::translate('Edit') . '</a>';
92			$content .= ' | ';
93			$content .= '<a href="editnews.php?action=delete&amp;news_id=' . $article->news_id . '&amp;ctype=user&amp;ged=' . $WT_TREE->getNameHtml() . '" onclick="return confirm(\'' . I18N::translate('Are you sure you want to delete “%s”?', e($article->subject)) . "');\">" . I18N::translate('Delete') . '</a><br>';
94			$content .= '</div><br>';
95		}
96
97		$content .= '<p><a href="editnews.php?ctype=user&amp;ged=' . $WT_TREE->getNameUrl() . '">' . I18N::translate('Add a journal entry') . '</a></p>';
98
99		if ($template) {
100			return View::make('blocks/template', [
101				'block'      => str_replace('_', '-', $this->getName()),
102				'id'         => $block_id,
103				'config_url' => '',
104				'title'      => $this->getTitle(),
105				'content'    => $content,
106			]);
107		} else {
108			return $content;
109		}
110	}
111
112	/** {@inheritdoc} */
113	public function loadAjax(): bool {
114		return false;
115	}
116
117	/** {@inheritdoc} */
118	public function isUserBlock(): bool {
119		return true;
120	}
121
122	/** {@inheritdoc} */
123	public function isGedcomBlock(): bool {
124		return false;
125	}
126
127	/**
128	 * An HTML form to edit block settings
129	 *
130	 * @param int $block_id
131	 *
132	 * @return void
133	 */
134	public function configureBlock($block_id) {
135	}
136}
137