xref: /webtrees/app/Module/UserJournalModule.php (revision 8c2e82270a639a3acf607b432e54721116dae723)
1<?php
2namespace Fisharebest\Webtrees;
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 */
18
19/**
20 * Class UserJournalModule
21 */
22class UserJournalModule extends Module implements ModuleBlockInterface {
23	/** {@inheritdoc} */
24	public function getTitle() {
25		return /* I18N: Name of a module */ I18N::translate('Journal');
26	}
27
28	/** {@inheritdoc} */
29	public function getDescription() {
30		return /* I18N: Description of the “Journal” module */ I18N::translate('A private area to record notes or keep a journal.');
31	}
32
33	/** {@inheritdoc} */
34	public function getBlock($block_id, $template = true, $cfg = null) {
35		global $ctype;
36
37		switch (Filter::get('action')) {
38		case 'deletenews':
39			$news_id = Filter::getInteger('news_id');
40			if ($news_id) {
41				Database::prepare("DELETE FROM `##news` WHERE news_id = ?")->execute(array($news_id));
42			}
43			break;
44		}
45		$block = get_block_setting($block_id, 'block', '1');
46		if ($cfg) {
47			foreach (array('block') as $name) {
48				if (array_key_exists($name, $cfg)) {
49					$$name = $cfg[$name];
50				}
51			}
52		}
53		$usernews = Database::prepare(
54			"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"
55		)->execute(array(Auth::id()))->fetchAll();
56
57		$id    = $this->getName() . $block_id;
58		$class = $this->getName() . '_block';
59		$title = '';
60		$title .= $this->getTitle();
61		$content = '';
62		if (!$usernews) {
63			$content .= I18N::translate('You have not created any journal items.');
64		}
65		foreach ($usernews as $news) {
66			$content .= '<div class="journal_box">';
67			$content .= '<div class="news_title">' . $news->subject . '</div>';
68			$content .= '<div class="news_date">' . format_timestamp($news->updated) . '</div>';
69			if ($news->body == strip_tags($news->body)) {
70				// No HTML?
71				$news->body = nl2br($news->body, false);
72			}
73			$content .= $news->body . '<br><br>';
74			$content .= '<a href="#" onclick="window.open(\'editnews.php?news_id=\'+' . $news->news_id . ', \'_blank\', indx_window_specs); return false;">' . I18N::translate('Edit') . '</a> | ';
75			$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>';
76			$content .= "</div><br>";
77		}
78		$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>';
79
80		if ($template) {
81			if ($block) {
82				$class .= ' small_inner_block';
83			}
84			return Theme::theme()->formatBlock($id, $title, $class, $content);
85		} else {
86			return $content;
87		}
88	}
89
90	/** {@inheritdoc} */
91	public function loadAjax() {
92		return false;
93	}
94
95	/** {@inheritdoc} */
96	public function isUserBlock() {
97		return true;
98	}
99
100	/** {@inheritdoc} */
101	public function isGedcomBlock() {
102		return false;
103	}
104
105	/** {@inheritdoc} */
106	public function configureBlock($block_id) {
107	}
108}
109