xref: /webtrees/app/Module/FamilyTreeNewsModule.php (revision 15d603e7c7c15d20f055d3d9c38d6b133453c5be)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
46bdf7674SGreg Roach * Copyright (C) 2017 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;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Theme;
248c2e8227SGreg Roach
258c2e8227SGreg Roach/**
268c2e8227SGreg Roach * Class FamilyTreeNewsModule
278c2e8227SGreg Roach */
28e2a378d3SGreg Roachclass FamilyTreeNewsModule extends AbstractModule implements ModuleBlockInterface {
297fafef87SGreg Roach	// How to update the database schema for this module
307fafef87SGreg Roach	const SCHEMA_TARGET_VERSION   = 3;
317fafef87SGreg Roach	const SCHEMA_SETTING_NAME     = 'NB_SCHEMA_VERSION';
327fafef87SGreg Roach	const SCHEMA_MIGRATION_PREFIX = '\Fisharebest\Webtrees\Module\FamilyTreeNews\Schema';
337fafef87SGreg Roach
3476692c8bSGreg Roach	/**
3576692c8bSGreg Roach	 * Create a new module.
3676692c8bSGreg Roach	 *
3776692c8bSGreg Roach	 * @param string $directory Where is this module installed
3876692c8bSGreg Roach	 */
393bfb94b0SGreg Roach	public function __construct($directory) {
403bfb94b0SGreg Roach		parent::__construct($directory);
413bfb94b0SGreg Roach
423bfb94b0SGreg Roach		// Create/update the database tables.
437fafef87SGreg Roach		Database::updateSchema(self::SCHEMA_MIGRATION_PREFIX, self::SCHEMA_SETTING_NAME, self::SCHEMA_TARGET_VERSION);
443bfb94b0SGreg Roach	}
453bfb94b0SGreg Roach
4676692c8bSGreg Roach	/**
4776692c8bSGreg Roach	 * How should this module be labelled on tabs, menus, etc.?
4876692c8bSGreg Roach	 *
4976692c8bSGreg Roach	 * @return string
5076692c8bSGreg Roach	 */
518c2e8227SGreg Roach	public function getTitle() {
528c2e8227SGreg Roach		return /* I18N: Name of a module */ I18N::translate('News');
538c2e8227SGreg Roach	}
548c2e8227SGreg Roach
5576692c8bSGreg Roach	/**
5676692c8bSGreg Roach	 * A sentence describing what this module does.
5776692c8bSGreg Roach	 *
5876692c8bSGreg Roach	 * @return string
5976692c8bSGreg Roach	 */
608c2e8227SGreg Roach	public function getDescription() {
61d004f62cSGreg Roach		return /* I18N: Description of the “News” module */ I18N::translate('Family news and site announcements.');
628c2e8227SGreg Roach	}
638c2e8227SGreg Roach
6476692c8bSGreg Roach	/**
6576692c8bSGreg Roach	 * Generate the HTML content of this block.
6676692c8bSGreg Roach	 *
6776692c8bSGreg Roach	 * @param int      $block_id
6876692c8bSGreg Roach	 * @param bool     $template
69727f238cSGreg Roach	 * @param string[] $cfg
7076692c8bSGreg Roach	 *
7176692c8bSGreg Roach	 * @return string
7276692c8bSGreg Roach	 */
7313abd6f3SGreg Roach	public function getBlock($block_id, $template = true, $cfg = []) {
744b9ff166SGreg Roach		global $ctype, $WT_TREE;
758c2e8227SGreg Roach
76d004f62cSGreg Roach		$more_news = Filter::getInteger('more_news');
77d004f62cSGreg Roach		$limit     = 5 * (1 + $more_news);
78d004f62cSGreg Roach
79d004f62cSGreg Roach		$articles = Database::prepare(
8045d06cf3SGreg Roach			"SELECT SQL_CACHE news_id, user_id, gedcom_id, UNIX_TIMESTAMP(updated) + :offset AS updated, subject, body FROM `##news` WHERE gedcom_id = :tree_id ORDER BY updated DESC LIMIT :limit"
8113abd6f3SGreg Roach		)->execute([
8245d06cf3SGreg Roach			'offset'  => WT_TIMESTAMP_OFFSET,
83d004f62cSGreg Roach			'tree_id' => $WT_TREE->getTreeId(),
84d004f62cSGreg Roach			'limit'   => $limit,
8513abd6f3SGreg Roach		])->fetchAll();
86d004f62cSGreg Roach
87d004f62cSGreg Roach		$count = Database::prepare(
88d004f62cSGreg Roach			"SELECT SQL_CACHE COUNT(*) FROM `##news` WHERE gedcom_id = :tree_id"
8913abd6f3SGreg Roach		)->execute([
90d004f62cSGreg Roach			'tree_id' => $WT_TREE->getTreeId(),
9113abd6f3SGreg Roach		])->fetchOne();
928c2e8227SGreg Roach
938c2e8227SGreg Roach		$id      = $this->getName() . $block_id;
948c2e8227SGreg Roach		$class   = $this->getName() . '_block';
95d004f62cSGreg Roach		$title   = $this->getTitle();
968c2e8227SGreg Roach		$content = '';
97d004f62cSGreg Roach
98d004f62cSGreg Roach		if (empty($articles)) {
99d004f62cSGreg Roach			$content .= I18N::translate('No news articles have been submitted.');
1008c2e8227SGreg Roach		}
101d004f62cSGreg Roach
102d004f62cSGreg Roach		foreach ($articles as $article) {
103d004f62cSGreg Roach			$content .= '<div class="news_box">';
104d004f62cSGreg Roach			$content .= '<div class="news_title">' . Filter::escapeHtml($article->subject) . '</div>';
105d004f62cSGreg Roach			$content .= '<div class="news_date">' . FunctionsDate::formatTimestamp($article->updated) . '</div>';
106d004f62cSGreg Roach			if ($article->body == strip_tags($article->body)) {
107d004f62cSGreg Roach				$article->body = nl2br($article->body, false);
1088c2e8227SGreg Roach			}
109d004f62cSGreg Roach			$content .= $article->body;
1104b9ff166SGreg Roach			if (Auth::isManager($WT_TREE)) {
111d004f62cSGreg Roach				$content .= '<hr>';
112*15d603e7SGreg Roach				$content .= '<a href="editnews.php?news_id=' . $article->news_id . '&amp;ctype=gedcom&amp;ged=' . $WT_TREE->getNameHtml() . '">' . I18N::translate('Edit') . '</a>';
113d004f62cSGreg Roach				$content .= ' | ';
114*15d603e7SGreg Roach				$content .= '<a href="editnews.php?action=delete&amp;news_id=' . $article->news_id . '&amp;ctype=gedcom&amp;ged=' . $WT_TREE->getNameHtml() . '" onclick="return confirm(\'' . I18N::translate('Are you sure you want to delete “%s”?', Filter::escapeHtml($article->subject)) . "');\">" . I18N::translate('Delete') . '</a><br>';
1158c2e8227SGreg Roach			}
1168c2e8227SGreg Roach			$content .= '</div>';
1178c2e8227SGreg Roach		}
118d004f62cSGreg Roach
1194b9ff166SGreg Roach		if (Auth::isManager($WT_TREE)) {
120*15d603e7SGreg Roach			$content .= '<p><a href="editnews.php?ctype=gedcom&amp;ged=' . $WT_TREE->getNameUrl() . '">' . I18N::translate('Add a news article') . '</a></p>';
1218c2e8227SGreg Roach		}
122d004f62cSGreg Roach
123d004f62cSGreg Roach		if ($count > $limit) {
124d004f62cSGreg Roach			if (Auth::isManager($WT_TREE)) {
125d004f62cSGreg Roach				$content .= ' | ';
1268c2e8227SGreg Roach			}
127564ae2d7SGreg Roach			$content .= '<a href="#" onclick="$(\'#' . $id . '\').load(\'index.php?ctype=gedcom&amp;ged=' . $WT_TREE->getNameUrl() . '&amp;block_id=' . $block_id . '&amp;action=ajax&amp;more_news=' . ($more_news + 1) . '\'); return false;">' . I18N::translate('More news articles') . '</a>';
1288c2e8227SGreg Roach		}
1298c2e8227SGreg Roach
1308c2e8227SGreg Roach		if ($template) {
1318c2e8227SGreg Roach			return Theme::theme()->formatBlock($id, $title, $class, $content);
1328c2e8227SGreg Roach		} else {
1338c2e8227SGreg Roach			return $content;
1348c2e8227SGreg Roach		}
1358c2e8227SGreg Roach	}
1368c2e8227SGreg Roach
1378c2e8227SGreg Roach	/** {@inheritdoc} */
1388c2e8227SGreg Roach	public function loadAjax() {
1398c2e8227SGreg Roach		return false;
1408c2e8227SGreg Roach	}
1418c2e8227SGreg Roach
1428c2e8227SGreg Roach	/** {@inheritdoc} */
1438c2e8227SGreg Roach	public function isUserBlock() {
1448c2e8227SGreg Roach		return false;
1458c2e8227SGreg Roach	}
1468c2e8227SGreg Roach
1478c2e8227SGreg Roach	/** {@inheritdoc} */
1488c2e8227SGreg Roach	public function isGedcomBlock() {
1498c2e8227SGreg Roach		return true;
1508c2e8227SGreg Roach	}
1518c2e8227SGreg Roach
15276692c8bSGreg Roach	/**
15376692c8bSGreg Roach	 * An HTML form to edit block settings
15476692c8bSGreg Roach	 *
15576692c8bSGreg Roach	 * @param int $block_id
15676692c8bSGreg Roach	 */
1578c2e8227SGreg Roach	public function configureBlock($block_id) {
1588c2e8227SGreg Roach	}
1598c2e8227SGreg Roach}
160