xref: /webtrees/app/Module/UserJournalModule.php (revision f7f4b984688fda35ae0a943c461ab9e8bb98e19b)
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\Functions\FunctionsDate;
21use Fisharebest\Webtrees\Html;
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\View;
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		$content = '';
79
80		if (empty($articles)) {
81			$content .= '<p>' . I18N::translate('You have not created any journal items.') . '</p>';
82		}
83
84		foreach ($articles as $article) {
85			$content .= '<div class="journal_box">';
86			$content .= '<div class="news_title">' . Html::escape($article->subject) . '</div>';
87			$content .= '<div class="news_date">' . FunctionsDate::formatTimestamp($article->updated) . '</div>';
88			if ($article->body == strip_tags($article->body)) {
89				$article->body = nl2br($article->body, false);
90			}
91			$content .= $article->body;
92			$content .= '<a href="editnews.php?news_id=' . $article->news_id . '&amp;ctype=user&amp;ged=' . $WT_TREE->getNameHtml() . '">' . I18N::translate('Edit') . '</a>';
93			$content .= ' | ';
94			$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”?', Html::escape($article->subject)) . "');\">" . I18N::translate('Delete') . '</a><br>';
95			$content .= '</div><br>';
96		}
97
98		$content .= '<p><a href="editnews.php?ctype=user&amp;ged=' . $WT_TREE->getNameUrl() . '">' . I18N::translate('Add a journal entry') . '</a></p>';
99
100		if ($template) {
101			return View::make('blocks/template', [
102				'block'      => str_replace('_', '-', $this->getName()),
103				'id'         => $block_id,
104				'config_url' => '',
105				'title'      => $this->getTitle(),
106				'content'    => $content,
107			]);
108		} else {
109			return $content;
110		}
111	}
112
113	/** {@inheritdoc} */
114	public function loadAjax() {
115		return false;
116	}
117
118	/** {@inheritdoc} */
119	public function isUserBlock() {
120		return true;
121	}
122
123	/** {@inheritdoc} */
124	public function isGedcomBlock() {
125		return false;
126	}
127
128	/**
129	 * An HTML form to edit block settings
130	 *
131	 * @param int $block_id
132	 */
133	public function configureBlock($block_id) {
134	}
135}
136