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