xref: /webtrees/app/Module/FamilyTreeNewsModule.php (revision 76ab908c83a7ad14130d09d30c1d81543922dff3)
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 FamilyTreeNewsModule
21 */
22class FamilyTreeNewsModule extends Module implements ModuleBlockInterface {
23	/** {@inheritdoc} */
24	public function getTitle() {
25		return /* I18N: Name of a module */ I18N::translate('News');
26	}
27
28	/** {@inheritdoc} */
29	public function getDescription() {
30		return /* I18N: Description of the “GEDCOM News” module */ I18N::translate('Family news and site announcements.');
31	}
32
33	/** {@inheritdoc} */
34	public function getBlock($block_id, $template = true, $cfg = null) {
35		global $ctype, $WT_TREE;
36
37		switch (Filter::get('action')) {
38		case 'deletenews':
39			$news_id = Filter::get('news_id');
40			if ($news_id) {
41				Database::prepare("DELETE FROM `##news` WHERE news_id = ?")->execute(array($news_id));
42			}
43			break;
44		}
45
46		if (isset($_REQUEST['gedcom_news_archive'])) {
47			$limit = 'nolimit';
48			$flag  = '0';
49		} else {
50			$flag = get_block_setting($block_id, 'flag', 0);
51			if ($flag === '0') {
52				$limit = 'nolimit';
53			} else {
54				$limit = get_block_setting($block_id, 'limit', 'nolimit');
55			}
56		}
57		if ($cfg) {
58			foreach (array('limit', 'flag') as $name) {
59				if (array_key_exists($name, $cfg)) {
60					$$name = $cfg[$name];
61				}
62			}
63		}
64		$usernews = Database::prepare(
65			"SELECT SQL_CACHE news_id, user_id, gedcom_id, UNIX_TIMESTAMP(updated) AS updated, subject, body FROM `##news` WHERE gedcom_id=? ORDER BY updated DESC"
66		)->execute(array($WT_TREE->getTreeId()))->fetchAll();
67
68		$id    = $this->getName() . $block_id;
69		$class = $this->getName() . '_block';
70		if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
71			$title = '<i class="icon-admin" title="' . I18N::translate('Configure') . '" onclick="modalDialog(\'block_edit.php?block_id=' . $block_id . '\', \'' . $this->getTitle() . '\');"></i>';
72		} else {
73			$title = '';
74		}
75		$title .= $this->getTitle();
76
77		$content = '';
78		if (count($usernews) == 0) {
79			$content .= I18N::translate('No news articles have been submitted.') . '<br>';
80		}
81		$c = 0;
82		foreach ($usernews as $news) {
83			if ($limit == 'count') {
84				if ($c >= $flag) {
85					break;
86				}
87				$c++;
88			}
89			if ($limit == 'date') {
90				if ((int) ((WT_TIMESTAMP - $news->updated) / 86400) > $flag) {
91					break;
92				}
93			}
94			$content .= '<div class="news_box" id="article' . $news->news_id . '">';
95			$content .= '<div class="news_title">' . Filter::escapeHtml($news->subject) . '</div>';
96			$content .= '<div class="news_date">' . format_timestamp($news->updated) . '</div>';
97			if ($news->body == strip_tags($news->body)) {
98				$news->body = nl2br($news->body, false);
99			}
100			$content .= $news->body;
101			// Print Admin options for this News item
102			if (Auth::isManager($WT_TREE)) {
103				$content .= '<hr>' . '<a href="#" onclick="window.open(\'editnews.php?news_id=\'+' . $news->news_id . ', \'_blank\', news_window_specs); return false;">' . I18N::translate('Edit') . '</a> | ' . '<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 news article?') . "');\">" . I18N::translate('Delete') . '</a><br>';
104			}
105			$content .= '</div>';
106		}
107		$printedAddLink = false;
108		if (Auth::isManager($WT_TREE)) {
109			$content .= "<a href=\"#\" onclick=\"window.open('editnews.php?gedcom_id=" . $WT_TREE->getTreeId() . "', '_blank', news_window_specs); return false;\">" . I18N::translate('Add a news article') . "</a>";
110			$printedAddLink = true;
111		}
112		if ($limit == 'date' || $limit == 'count') {
113			if ($printedAddLink) {
114				$content .= '&nbsp;&nbsp;|&nbsp;&nbsp;';
115			}
116			$content .= '<a href="index.php?gedcom_news_archive=yes&amp;ctype=' . $ctype . '">' . I18N::translate('View archive') . "</a>";
117			$content .= help_link('gedcom_news_archive') . '<br>';
118		}
119
120		if ($template) {
121			return Theme::theme()->formatBlock($id, $title, $class, $content);
122		} else {
123			return $content;
124		}
125	}
126
127	/** {@inheritdoc} */
128	public function loadAjax() {
129		return false;
130	}
131
132	/** {@inheritdoc} */
133	public function isUserBlock() {
134		return false;
135	}
136
137	/** {@inheritdoc} */
138	public function isGedcomBlock() {
139		return true;
140	}
141
142	/** {@inheritdoc} */
143	public function configureBlock($block_id) {
144		if (Filter::postBool('save') && Filter::checkCsrf()) {
145			set_block_setting($block_id, 'limit', Filter::post('limit'));
146			set_block_setting($block_id, 'flag', Filter::post('flag'));
147		}
148
149		$limit = get_block_setting($block_id, 'limit', 'nolimit');
150		$flag  = get_block_setting($block_id, 'flag', 0);
151
152		echo
153			'<tr><td class="descriptionbox wrap width33">',
154			/* I18N: Limit display by [age/number] */ I18N::translate('Limit display by'),
155			'</td><td class="optionbox"><select name="limit"><option value="nolimit" ',
156			($limit == 'nolimit' ? 'selected' : '') . ">",
157			I18N::translate('No limit') . "</option>",
158			'<option value="date" ' . ($limit == 'date' ? 'selected' : '') . ">" . I18N::translate('Age of item') . "</option>",
159			'<option value="count" ' . ($limit == 'count' ? 'selected' : '') . ">" . I18N::translate('Number of items') . "</option>",
160			'</select></td></tr>';
161
162		echo '<tr><td class="descriptionbox wrap width33">';
163		echo I18N::translate('Limit');
164		echo '</td><td class="optionbox"><input type="text" name="flag" size="4" maxlength="4" value="' . $flag . '"></td></tr>';
165	}
166}
167