xref: /webtrees/app/Module/FamilyTreeNewsModule.php (revision e2a378d30d9bd3fff591da7a11c7cb5ead502323)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roachnamespace Fisharebest\Webtrees;
38c2e8227SGreg Roach
48c2e8227SGreg Roach/**
58c2e8227SGreg Roach * webtrees: online genealogy
68c2e8227SGreg Roach * Copyright (C) 2015 webtrees development team
78c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
88c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
98c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
108c2e8227SGreg Roach * (at your option) any later version.
118c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
128c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
138c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
148c2e8227SGreg Roach * GNU General Public License for more details.
158c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
168c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
178c2e8227SGreg Roach */
188c2e8227SGreg Roach
198c2e8227SGreg Roach/**
208c2e8227SGreg Roach * Class FamilyTreeNewsModule
218c2e8227SGreg Roach */
22*e2a378d3SGreg Roachclass FamilyTreeNewsModule extends AbstractModule implements ModuleBlockInterface {
238c2e8227SGreg Roach	/** {@inheritdoc} */
248c2e8227SGreg Roach	public function getTitle() {
258c2e8227SGreg Roach		return /* I18N: Name of a module */ I18N::translate('News');
268c2e8227SGreg Roach	}
278c2e8227SGreg Roach
288c2e8227SGreg Roach	/** {@inheritdoc} */
298c2e8227SGreg Roach	public function getDescription() {
308c2e8227SGreg Roach		return /* I18N: Description of the “GEDCOM News” module */ I18N::translate('Family news and site announcements.');
318c2e8227SGreg Roach	}
328c2e8227SGreg Roach
338c2e8227SGreg Roach	/** {@inheritdoc} */
348c2e8227SGreg Roach	public function getBlock($block_id, $template = true, $cfg = null) {
354b9ff166SGreg Roach		global $ctype, $WT_TREE;
368c2e8227SGreg Roach
378c2e8227SGreg Roach		switch (Filter::get('action')) {
388c2e8227SGreg Roach		case 'deletenews':
398c2e8227SGreg Roach			$news_id = Filter::get('news_id');
408c2e8227SGreg Roach			if ($news_id) {
418c2e8227SGreg Roach				Database::prepare("DELETE FROM `##news` WHERE news_id = ?")->execute(array($news_id));
428c2e8227SGreg Roach			}
438c2e8227SGreg Roach			break;
448c2e8227SGreg Roach		}
458c2e8227SGreg Roach
468c2e8227SGreg Roach		if (isset($_REQUEST['gedcom_news_archive'])) {
478c2e8227SGreg Roach			$limit = 'nolimit';
488c2e8227SGreg Roach			$flag  = '0';
498c2e8227SGreg Roach		} else {
50*e2a378d3SGreg Roach			$flag = $this->getBlockSetting($block_id, 'flag', 0);
518c2e8227SGreg Roach			if ($flag === '0') {
528c2e8227SGreg Roach				$limit = 'nolimit';
538c2e8227SGreg Roach			} else {
54*e2a378d3SGreg Roach				$limit = $this->getBlockSetting($block_id, 'limit', 'nolimit');
558c2e8227SGreg Roach			}
568c2e8227SGreg Roach		}
578c2e8227SGreg Roach		if ($cfg) {
588c2e8227SGreg Roach			foreach (array('limit', 'flag') as $name) {
598c2e8227SGreg Roach				if (array_key_exists($name, $cfg)) {
608c2e8227SGreg Roach					$$name = $cfg[$name];
618c2e8227SGreg Roach				}
628c2e8227SGreg Roach			}
638c2e8227SGreg Roach		}
648c2e8227SGreg Roach		$usernews = Database::prepare(
658c2e8227SGreg Roach			"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"
6624ec66ceSGreg Roach		)->execute(array($WT_TREE->getTreeId()))->fetchAll();
678c2e8227SGreg Roach
688c2e8227SGreg Roach		$id    = $this->getName() . $block_id;
698c2e8227SGreg Roach		$class = $this->getName() . '_block';
704b9ff166SGreg Roach		if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
718c2e8227SGreg Roach			$title = '<i class="icon-admin" title="' . I18N::translate('Configure') . '" onclick="modalDialog(\'block_edit.php?block_id=' . $block_id . '\', \'' . $this->getTitle() . '\');"></i>';
728c2e8227SGreg Roach		} else {
738c2e8227SGreg Roach			$title = '';
748c2e8227SGreg Roach		}
758c2e8227SGreg Roach		$title .= $this->getTitle();
768c2e8227SGreg Roach
778c2e8227SGreg Roach		$content = '';
788c2e8227SGreg Roach		if (count($usernews) == 0) {
798c2e8227SGreg Roach			$content .= I18N::translate('No news articles have been submitted.') . '<br>';
808c2e8227SGreg Roach		}
818c2e8227SGreg Roach		$c = 0;
828c2e8227SGreg Roach		foreach ($usernews as $news) {
838c2e8227SGreg Roach			if ($limit == 'count') {
848c2e8227SGreg Roach				if ($c >= $flag) {
858c2e8227SGreg Roach					break;
868c2e8227SGreg Roach				}
878c2e8227SGreg Roach				$c++;
888c2e8227SGreg Roach			}
898c2e8227SGreg Roach			if ($limit == 'date') {
908c2e8227SGreg Roach				if ((int) ((WT_TIMESTAMP - $news->updated) / 86400) > $flag) {
918c2e8227SGreg Roach					break;
928c2e8227SGreg Roach				}
938c2e8227SGreg Roach			}
948c2e8227SGreg Roach			$content .= '<div class="news_box" id="article' . $news->news_id . '">';
958c2e8227SGreg Roach			$content .= '<div class="news_title">' . Filter::escapeHtml($news->subject) . '</div>';
968c2e8227SGreg Roach			$content .= '<div class="news_date">' . format_timestamp($news->updated) . '</div>';
978c2e8227SGreg Roach			if ($news->body == strip_tags($news->body)) {
988c2e8227SGreg Roach				$news->body = nl2br($news->body, false);
998c2e8227SGreg Roach			}
1008c2e8227SGreg Roach			$content .= $news->body;
1018c2e8227SGreg Roach			// Print Admin options for this News item
1024b9ff166SGreg Roach			if (Auth::isManager($WT_TREE)) {
1038c2e8227SGreg Roach				$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>';
1048c2e8227SGreg Roach			}
1058c2e8227SGreg Roach			$content .= '</div>';
1068c2e8227SGreg Roach		}
1078c2e8227SGreg Roach		$printedAddLink = false;
1084b9ff166SGreg Roach		if (Auth::isManager($WT_TREE)) {
10924ec66ceSGreg Roach			$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>";
1108c2e8227SGreg Roach			$printedAddLink = true;
1118c2e8227SGreg Roach		}
1128c2e8227SGreg Roach		if ($limit == 'date' || $limit == 'count') {
1138c2e8227SGreg Roach			if ($printedAddLink) {
1148c2e8227SGreg Roach				$content .= '&nbsp;&nbsp;|&nbsp;&nbsp;';
1158c2e8227SGreg Roach			}
1168c2e8227SGreg Roach			$content .= '<a href="index.php?gedcom_news_archive=yes&amp;ctype=' . $ctype . '">' . I18N::translate('View archive') . "</a>";
1178c2e8227SGreg Roach			$content .= help_link('gedcom_news_archive') . '<br>';
1188c2e8227SGreg Roach		}
1198c2e8227SGreg Roach
1208c2e8227SGreg Roach		if ($template) {
1218c2e8227SGreg Roach			return Theme::theme()->formatBlock($id, $title, $class, $content);
1228c2e8227SGreg Roach		} else {
1238c2e8227SGreg Roach			return $content;
1248c2e8227SGreg Roach		}
1258c2e8227SGreg Roach	}
1268c2e8227SGreg Roach
1278c2e8227SGreg Roach	/** {@inheritdoc} */
1288c2e8227SGreg Roach	public function loadAjax() {
1298c2e8227SGreg Roach		return false;
1308c2e8227SGreg Roach	}
1318c2e8227SGreg Roach
1328c2e8227SGreg Roach	/** {@inheritdoc} */
1338c2e8227SGreg Roach	public function isUserBlock() {
1348c2e8227SGreg Roach		return false;
1358c2e8227SGreg Roach	}
1368c2e8227SGreg Roach
1378c2e8227SGreg Roach	/** {@inheritdoc} */
1388c2e8227SGreg Roach	public function isGedcomBlock() {
1398c2e8227SGreg Roach		return true;
1408c2e8227SGreg Roach	}
1418c2e8227SGreg Roach
1428c2e8227SGreg Roach	/** {@inheritdoc} */
1438c2e8227SGreg Roach	public function configureBlock($block_id) {
1448c2e8227SGreg Roach		if (Filter::postBool('save') && Filter::checkCsrf()) {
145*e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'limit', Filter::post('limit'));
146*e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'flag', Filter::post('flag'));
1478c2e8227SGreg Roach		}
1488c2e8227SGreg Roach
149*e2a378d3SGreg Roach		$limit = $this->getBlockSetting($block_id, 'limit', 'nolimit');
150*e2a378d3SGreg Roach		$flag  = $this->getBlockSetting($block_id, 'flag', 0);
1518c2e8227SGreg Roach
1528c2e8227SGreg Roach		echo
1538c2e8227SGreg Roach			'<tr><td class="descriptionbox wrap width33">',
154e284b8e1SGreg Roach			/* I18N: Limit display by [age/number] */ I18N::translate('Limit display by'),
1558c2e8227SGreg Roach			'</td><td class="optionbox"><select name="limit"><option value="nolimit" ',
1568c2e8227SGreg Roach			($limit == 'nolimit' ? 'selected' : '') . ">",
1578c2e8227SGreg Roach			I18N::translate('No limit') . "</option>",
1588c2e8227SGreg Roach			'<option value="date" ' . ($limit == 'date' ? 'selected' : '') . ">" . I18N::translate('Age of item') . "</option>",
1598c2e8227SGreg Roach			'<option value="count" ' . ($limit == 'count' ? 'selected' : '') . ">" . I18N::translate('Number of items') . "</option>",
1608c2e8227SGreg Roach			'</select></td></tr>';
1618c2e8227SGreg Roach
1628c2e8227SGreg Roach		echo '<tr><td class="descriptionbox wrap width33">';
163e284b8e1SGreg Roach		echo I18N::translate('Limit');
1648c2e8227SGreg Roach		echo '</td><td class="optionbox"><input type="text" name="flag" size="4" maxlength="4" value="' . $flag . '"></td></tr>';
1658c2e8227SGreg Roach	}
1668c2e8227SGreg Roach}
167