xref: /webtrees/app/Module/UserJournalModule.php (revision 76692c8b291f16d9251d67f27078779f6737fe7e)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2015 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 array $cfg
65	 *
66	 * @return string
67	 */
68	public function getBlock($block_id, $template = true, $cfg = array()) {
69		global $ctype;
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(array($news_id));
76			}
77			break;
78		}
79		$block = $this->getBlockSetting($block_id, 'block', '1');
80		foreach (array('block') as $name) {
81			if (array_key_exists($name, $cfg)) {
82				$$name = $cfg[$name];
83			}
84		}
85		$usernews = Database::prepare(
86			"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"
87		)->execute(array(Auth::id()))->fetchAll();
88
89		$id    = $this->getName() . $block_id;
90		$class = $this->getName() . '_block';
91		$title = '';
92		$title .= $this->getTitle();
93		$content = '';
94		if (!$usernews) {
95			$content .= I18N::translate('You have not created any journal items.');
96		}
97		foreach ($usernews as $news) {
98			$content .= '<div class="journal_box">';
99			$content .= '<div class="news_title">' . $news->subject . '</div>';
100			$content .= '<div class="news_date">' . FunctionsDate::formatTimestamp($news->updated) . '</div>';
101			if ($news->body == strip_tags($news->body)) {
102				// No HTML?
103				$news->body = nl2br($news->body, false);
104			}
105			$content .= $news->body . '<br><br>';
106			$content .= '<a href="#" onclick="window.open(\'editnews.php?news_id=\'+' . $news->news_id . ', \'_blank\', indx_window_specs); return false;">' . I18N::translate('Edit') . '</a> | ';
107			$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>';
108			$content .= "</div><br>";
109		}
110		$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>';
111
112		if ($template) {
113			if ($block) {
114				$class .= ' small_inner_block';
115			}
116
117			return Theme::theme()->formatBlock($id, $title, $class, $content);
118		} else {
119			return $content;
120		}
121	}
122
123	/** {@inheritdoc} */
124	public function loadAjax() {
125		return false;
126	}
127
128	/** {@inheritdoc} */
129	public function isUserBlock() {
130		return true;
131	}
132
133	/** {@inheritdoc} */
134	public function isGedcomBlock() {
135		return false;
136	}
137
138	/**
139	 * An HTML form to edit block settings
140	 *
141	 * @param int $block_id
142	 */
143	public function configureBlock($block_id) {
144	}
145}
146