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