xref: /webtrees/app/Module/UserJournalModule.php (revision 2836aa05d87d478edc3ef1f1983e9d37862ded10)
1<?php
2namespace Fisharebest\Webtrees\Module;
3
4/**
5 * webtrees: online genealogy
6 * Copyright (C) 2015 webtrees development team
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18use Fisharebest\Webtrees\Auth;
19use Fisharebest\Webtrees\Database;
20use Fisharebest\Webtrees\Filter;
21use Fisharebest\Webtrees\I18N;
22use Fisharebest\Webtrees\Theme;
23
24/**
25 * Class UserJournalModule
26 */
27class UserJournalModule extends AbstractModule implements ModuleBlockInterface {
28	/** {@inheritdoc} */
29	public function __construct($directory) {
30		parent::__construct($directory);
31
32		// Create/update the database tables.
33		Database::updateSchema('\Fisharebest\Webtrees\Module\FamilyTreeNews\Schema', 'NB_SCHEMA_VERSION', 3);
34	}
35
36	/** {@inheritdoc} */
37	public function getTitle() {
38		return /* I18N: Name of a module */ I18N::translate('Journal');
39	}
40
41	/** {@inheritdoc} */
42	public function getDescription() {
43		return /* I18N: Description of the “Journal” module */ I18N::translate('A private area to record notes or keep a journal.');
44	}
45
46	/** {@inheritdoc} */
47	public function getBlock($block_id, $template = true, $cfg = null) {
48		global $ctype;
49
50		switch (Filter::get('action')) {
51		case 'deletenews':
52			$news_id = Filter::getInteger('news_id');
53			if ($news_id) {
54				Database::prepare("DELETE FROM `##news` WHERE news_id = ?")->execute(array($news_id));
55			}
56			break;
57		}
58		$block = $this->getBlockSetting($block_id, 'block', '1');
59		if ($cfg) {
60			foreach (array('block') as $name) {
61				if (array_key_exists($name, $cfg)) {
62					$$name = $cfg[$name];
63				}
64			}
65		}
66		$usernews = Database::prepare(
67			"SELECT SQL_CACHE news_id, user_id, gedcom_id, UNIX_TIMESTAMP(updated) AS updated, subject, body FROM `##news` WHERE user_id = ? ORDER BY updated DESC"
68		)->execute(array(Auth::id()))->fetchAll();
69
70		$id    = $this->getName() . $block_id;
71		$class = $this->getName() . '_block';
72		$title = '';
73		$title .= $this->getTitle();
74		$content = '';
75		if (!$usernews) {
76			$content .= I18N::translate('You have not created any journal items.');
77		}
78		foreach ($usernews as $news) {
79			$content .= '<div class="journal_box">';
80			$content .= '<div class="news_title">' . $news->subject . '</div>';
81			$content .= '<div class="news_date">' . format_timestamp($news->updated) . '</div>';
82			if ($news->body == strip_tags($news->body)) {
83				// No HTML?
84				$news->body = nl2br($news->body, false);
85			}
86			$content .= $news->body . '<br><br>';
87			$content .= '<a href="#" onclick="window.open(\'editnews.php?news_id=\'+' . $news->news_id . ', \'_blank\', indx_window_specs); return false;">' . I18N::translate('Edit') . '</a> | ';
88			$content .= '<a href="index.php?action=deletenews&amp;news_id=' . $news->news_id . '&amp;ctype=' . $ctype . '" onclick="return confirm(\'' . I18N::translate('Are you sure you want to delete this journal entry?') . "');\">" . I18N::translate('Delete') . '</a><br>';
89			$content .= "</div><br>";
90		}
91		$content .= '<br><a href="#" onclick="window.open(\'editnews.php?user_id=' . Auth::id() . '\', \'_blank\', indx_window_specs); return false;">' . I18N::translate('Add a new journal entry') . '</a>';
92
93		if ($template) {
94			if ($block) {
95				$class .= ' small_inner_block';
96			}
97
98			return Theme::theme()->formatBlock($id, $title, $class, $content);
99		} else {
100			return $content;
101		}
102	}
103
104	/** {@inheritdoc} */
105	public function loadAjax() {
106		return false;
107	}
108
109	/** {@inheritdoc} */
110	public function isUserBlock() {
111		return true;
112	}
113
114	/** {@inheritdoc} */
115	public function isGedcomBlock() {
116		return false;
117	}
118
119	/** {@inheritdoc} */
120	public function configureBlock($block_id) {
121	}
122}
123