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