xref: /webtrees/app/Module/UserJournalModule.php (revision 6bdf767435631ad1dc27ec1ffd855d43dbdce907)
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		switch (Filter::get('action')) {
72		case 'deletenews':
73			$news_id = Filter::getInteger('news_id');
74			if ($news_id) {
75				Database::prepare("DELETE FROM `##news` WHERE news_id = ?")->execute([$news_id]);
76			}
77			break;
78		}
79
80		$articles = Database::prepare(
81			"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"
82		)->execute([
83			'offset'  => WT_TIMESTAMP_OFFSET,
84			'user_id' => Auth::id(),
85		])->fetchAll();
86
87		$id      = $this->getName() . $block_id;
88		$class   = $this->getName() . '_block';
89		$title   = $this->getTitle();
90		$content = '';
91
92		if (empty($articles)) {
93			$content .= '<p>' . I18N::translate('You have not created any journal items.') . '</p>';
94		}
95
96		foreach ($articles as $article) {
97			$content .= '<div class="journal_box">';
98			$content .= '<div class="news_title">' . Filter::escapeHtml($article->subject) . '</div>';
99			$content .= '<div class="news_date">' . FunctionsDate::formatTimestamp($article->updated) . '</div>';
100			if ($article->body == strip_tags($article->body)) {
101				$article->body = nl2br($article->body, false);
102			}
103			$content .= $article->body;
104			$content .= '<a href="#" onclick="window.open(\'editnews.php?news_id=\'+' . $article->news_id . ', \'_blank\', indx_window_specs); return false;">' . I18N::translate('Edit') . '</a>';
105			$content .= ' | ';
106			$content .= '<a href="index.php?action=deletenews&amp;news_id=' . $article->news_id . '&amp;ctype=' . $ctype . '&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>';
107			$content .= '</div><br>';
108		}
109
110		$content .= '<p><a href="#" onclick="window.open(\'editnews.php?user_id=' . Auth::id() . '\', \'_blank\', indx_window_specs); return false;">' . I18N::translate('Add a journal entry') . '</a></p>';
111
112		if ($template) {
113			return Theme::theme()->formatBlock($id, $title, $class, $content);
114		} else {
115			return $content;
116		}
117	}
118
119	/** {@inheritdoc} */
120	public function loadAjax() {
121		return false;
122	}
123
124	/** {@inheritdoc} */
125	public function isUserBlock() {
126		return true;
127	}
128
129	/** {@inheritdoc} */
130	public function isGedcomBlock() {
131		return false;
132	}
133
134	/**
135	 * An HTML form to edit block settings
136	 *
137	 * @param int $block_id
138	 */
139	public function configureBlock($block_id) {
140	}
141}
142