xref: /webtrees/app/Module/FamilyTreeNewsModule.php (revision 369c0ce6d43eee62858778711fa4744ed347814a)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
4*369c0ce6SGreg Roach * Copyright (C) 2016 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1776692c8bSGreg Roach
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Database;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
213d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsDate;
223d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrint;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Theme;
258c2e8227SGreg Roach
268c2e8227SGreg Roach/**
278c2e8227SGreg Roach * Class FamilyTreeNewsModule
288c2e8227SGreg Roach */
29e2a378d3SGreg Roachclass FamilyTreeNewsModule extends AbstractModule implements ModuleBlockInterface {
307fafef87SGreg Roach	// How to update the database schema for this module
317fafef87SGreg Roach	const SCHEMA_TARGET_VERSION   = 3;
327fafef87SGreg Roach	const SCHEMA_SETTING_NAME     = 'NB_SCHEMA_VERSION';
337fafef87SGreg Roach	const SCHEMA_MIGRATION_PREFIX = '\Fisharebest\Webtrees\Module\FamilyTreeNews\Schema';
347fafef87SGreg Roach
3576692c8bSGreg Roach	/**
3676692c8bSGreg Roach	 * Create a new module.
3776692c8bSGreg Roach	 *
3876692c8bSGreg Roach	 * @param string $directory Where is this module installed
3976692c8bSGreg Roach	 */
403bfb94b0SGreg Roach	public function __construct($directory) {
413bfb94b0SGreg Roach		parent::__construct($directory);
423bfb94b0SGreg Roach
433bfb94b0SGreg Roach		// Create/update the database tables.
447fafef87SGreg Roach		// NOTE: if we want to set any module-settings, we'll need to move this.
457fafef87SGreg Roach		Database::updateSchema(self::SCHEMA_MIGRATION_PREFIX, self::SCHEMA_SETTING_NAME, self::SCHEMA_TARGET_VERSION);
463bfb94b0SGreg Roach	}
473bfb94b0SGreg Roach
4876692c8bSGreg Roach	/**
4976692c8bSGreg Roach	 * How should this module be labelled on tabs, menus, etc.?
5076692c8bSGreg Roach	 *
5176692c8bSGreg Roach	 * @return string
5276692c8bSGreg Roach	 */
538c2e8227SGreg Roach	public function getTitle() {
548c2e8227SGreg Roach		return /* I18N: Name of a module */ I18N::translate('News');
558c2e8227SGreg Roach	}
568c2e8227SGreg Roach
5776692c8bSGreg Roach	/**
5876692c8bSGreg Roach	 * A sentence describing what this module does.
5976692c8bSGreg Roach	 *
6076692c8bSGreg Roach	 * @return string
6176692c8bSGreg Roach	 */
628c2e8227SGreg Roach	public function getDescription() {
638c2e8227SGreg Roach		return /* I18N: Description of the “GEDCOM News” module */ I18N::translate('Family news and site announcements.');
648c2e8227SGreg Roach	}
658c2e8227SGreg Roach
6676692c8bSGreg Roach	/**
6776692c8bSGreg Roach	 * Generate the HTML content of this block.
6876692c8bSGreg Roach	 *
6976692c8bSGreg Roach	 * @param int      $block_id
7076692c8bSGreg Roach	 * @param bool     $template
71727f238cSGreg Roach	 * @param string[] $cfg
7276692c8bSGreg Roach	 *
7376692c8bSGreg Roach	 * @return string
7476692c8bSGreg Roach	 */
7576692c8bSGreg Roach	public function getBlock($block_id, $template = true, $cfg = array()) {
764b9ff166SGreg Roach		global $ctype, $WT_TREE;
778c2e8227SGreg Roach
788c2e8227SGreg Roach		switch (Filter::get('action')) {
798c2e8227SGreg Roach		case 'deletenews':
808c2e8227SGreg Roach			$news_id = Filter::get('news_id');
818c2e8227SGreg Roach			if ($news_id) {
828c2e8227SGreg Roach				Database::prepare("DELETE FROM `##news` WHERE news_id = ?")->execute(array($news_id));
838c2e8227SGreg Roach			}
848c2e8227SGreg Roach			break;
858c2e8227SGreg Roach		}
868c2e8227SGreg Roach
878c2e8227SGreg Roach		if (isset($_REQUEST['gedcom_news_archive'])) {
888c2e8227SGreg Roach			$limit = 'nolimit';
898c2e8227SGreg Roach			$flag  = '0';
908c2e8227SGreg Roach		} else {
91e2a378d3SGreg Roach			$flag = $this->getBlockSetting($block_id, 'flag', 0);
928c2e8227SGreg Roach			if ($flag === '0') {
938c2e8227SGreg Roach				$limit = 'nolimit';
948c2e8227SGreg Roach			} else {
95e2a378d3SGreg Roach				$limit = $this->getBlockSetting($block_id, 'limit', 'nolimit');
968c2e8227SGreg Roach			}
978c2e8227SGreg Roach		}
988c2e8227SGreg Roach		foreach (array('limit', 'flag') as $name) {
998c2e8227SGreg Roach			if (array_key_exists($name, $cfg)) {
1008c2e8227SGreg Roach				$$name = $cfg[$name];
1018c2e8227SGreg Roach			}
1028c2e8227SGreg Roach		}
1038c2e8227SGreg Roach		$usernews = Database::prepare(
1048c2e8227SGreg 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"
10524ec66ceSGreg Roach		)->execute(array($WT_TREE->getTreeId()))->fetchAll();
1068c2e8227SGreg Roach
1078c2e8227SGreg Roach		$id    = $this->getName() . $block_id;
1088c2e8227SGreg Roach		$class = $this->getName() . '_block';
1094b9ff166SGreg Roach		if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
1109353052eSGreg Roach			$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>';
1118c2e8227SGreg Roach		} else {
1128c2e8227SGreg Roach			$title = '';
1138c2e8227SGreg Roach		}
1148c2e8227SGreg Roach		$title .= $this->getTitle();
1158c2e8227SGreg Roach
1168c2e8227SGreg Roach		$content = '';
1178c2e8227SGreg Roach		if (count($usernews) == 0) {
1188c2e8227SGreg Roach			$content .= I18N::translate('No news articles have been submitted.') . '<br>';
1198c2e8227SGreg Roach		}
1208c2e8227SGreg Roach		$c = 0;
1218c2e8227SGreg Roach		foreach ($usernews as $news) {
1228c2e8227SGreg Roach			if ($limit == 'count') {
1238c2e8227SGreg Roach				if ($c >= $flag) {
1248c2e8227SGreg Roach					break;
1258c2e8227SGreg Roach				}
1268c2e8227SGreg Roach				$c++;
1278c2e8227SGreg Roach			}
1288c2e8227SGreg Roach			if ($limit == 'date') {
1298c2e8227SGreg Roach				if ((int) ((WT_TIMESTAMP - $news->updated) / 86400) > $flag) {
1308c2e8227SGreg Roach					break;
1318c2e8227SGreg Roach				}
1328c2e8227SGreg Roach			}
1338c2e8227SGreg Roach			$content .= '<div class="news_box" id="article' . $news->news_id . '">';
1348c2e8227SGreg Roach			$content .= '<div class="news_title">' . Filter::escapeHtml($news->subject) . '</div>';
1353d7a8a4cSGreg Roach			$content .= '<div class="news_date">' . FunctionsDate::formatTimestamp($news->updated) . '</div>';
1368c2e8227SGreg Roach			if ($news->body == strip_tags($news->body)) {
1378c2e8227SGreg Roach				$news->body = nl2br($news->body, false);
1388c2e8227SGreg Roach			}
1398c2e8227SGreg Roach			$content .= $news->body;
1408c2e8227SGreg Roach			// Print Admin options for this News item
1414b9ff166SGreg Roach			if (Auth::isManager($WT_TREE)) {
142727fa558SGreg 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 . '&amp;ged=' . $WT_TREE->getNameHtml() . '" onclick="return confirm(\'' . I18N::translate('Are you sure you want to delete “%s”?', Filter::escapeHtml($news->subject)) . "');\">" . I18N::translate('Delete') . '</a><br>';
1438c2e8227SGreg Roach			}
1448c2e8227SGreg Roach			$content .= '</div>';
1458c2e8227SGreg Roach		}
1468c2e8227SGreg Roach		$printedAddLink = false;
1474b9ff166SGreg Roach		if (Auth::isManager($WT_TREE)) {
14824ec66ceSGreg 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>";
1498c2e8227SGreg Roach			$printedAddLink = true;
1508c2e8227SGreg Roach		}
1518c2e8227SGreg Roach		if ($limit == 'date' || $limit == 'count') {
1528c2e8227SGreg Roach			if ($printedAddLink) {
1538c2e8227SGreg Roach				$content .= '&nbsp;&nbsp;|&nbsp;&nbsp;';
1548c2e8227SGreg Roach			}
155a3dfb74cSGreg Roach			$content .= '<a href="index.php?gedcom_news_archive=yes&amp;ctype=' . $ctype . '&amp;ged=' . $WT_TREE->getNameHtml() . '">' . I18N::translate('View archive') . "</a>";
1563d7a8a4cSGreg Roach			$content .= FunctionsPrint::helpLink('gedcom_news_archive') . '<br>';
1578c2e8227SGreg Roach		}
1588c2e8227SGreg Roach
1598c2e8227SGreg Roach		if ($template) {
1608c2e8227SGreg Roach			return Theme::theme()->formatBlock($id, $title, $class, $content);
1618c2e8227SGreg Roach		} else {
1628c2e8227SGreg Roach			return $content;
1638c2e8227SGreg Roach		}
1648c2e8227SGreg Roach	}
1658c2e8227SGreg Roach
1668c2e8227SGreg Roach	/** {@inheritdoc} */
1678c2e8227SGreg Roach	public function loadAjax() {
1688c2e8227SGreg Roach		return false;
1698c2e8227SGreg Roach	}
1708c2e8227SGreg Roach
1718c2e8227SGreg Roach	/** {@inheritdoc} */
1728c2e8227SGreg Roach	public function isUserBlock() {
1738c2e8227SGreg Roach		return false;
1748c2e8227SGreg Roach	}
1758c2e8227SGreg Roach
1768c2e8227SGreg Roach	/** {@inheritdoc} */
1778c2e8227SGreg Roach	public function isGedcomBlock() {
1788c2e8227SGreg Roach		return true;
1798c2e8227SGreg Roach	}
1808c2e8227SGreg Roach
18176692c8bSGreg Roach	/**
18276692c8bSGreg Roach	 * An HTML form to edit block settings
18376692c8bSGreg Roach	 *
18476692c8bSGreg Roach	 * @param int $block_id
18576692c8bSGreg Roach	 */
1868c2e8227SGreg Roach	public function configureBlock($block_id) {
1878c2e8227SGreg Roach		if (Filter::postBool('save') && Filter::checkCsrf()) {
188e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'limit', Filter::post('limit'));
189e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'flag', Filter::post('flag'));
1908c2e8227SGreg Roach		}
1918c2e8227SGreg Roach
192e2a378d3SGreg Roach		$limit = $this->getBlockSetting($block_id, 'limit', 'nolimit');
193e2a378d3SGreg Roach		$flag  = $this->getBlockSetting($block_id, 'flag', 0);
1948c2e8227SGreg Roach
1958c2e8227SGreg Roach		echo
1968c2e8227SGreg Roach			'<tr><td class="descriptionbox wrap width33">',
197e284b8e1SGreg Roach			/* I18N: Limit display by [age/number] */ I18N::translate('Limit display by'),
1988c2e8227SGreg Roach			'</td><td class="optionbox"><select name="limit"><option value="nolimit" ',
1998c2e8227SGreg Roach			($limit == 'nolimit' ? 'selected' : '') . ">",
2008c2e8227SGreg Roach			I18N::translate('No limit') . "</option>",
2018c2e8227SGreg Roach			'<option value="date" ' . ($limit == 'date' ? 'selected' : '') . ">" . I18N::translate('Age of item') . "</option>",
2028c2e8227SGreg Roach			'<option value="count" ' . ($limit == 'count' ? 'selected' : '') . ">" . I18N::translate('Number of items') . "</option>",
2038c2e8227SGreg Roach			'</select></td></tr>';
2048c2e8227SGreg Roach
2058c2e8227SGreg Roach		echo '<tr><td class="descriptionbox wrap width33">';
206e284b8e1SGreg Roach		echo I18N::translate('Limit');
2078c2e8227SGreg Roach		echo '</td><td class="optionbox"><input type="text" name="flag" size="4" maxlength="4" value="' . $flag . '"></td></tr>';
2088c2e8227SGreg Roach	}
2098c2e8227SGreg Roach}
210