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