xref: /webtrees/app/Module/FamilyTreeNewsModule.php (revision 225e381f36d59b49c8cdac0060465fa5af2fc308)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 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;
238c2e8227SGreg Roach
248c2e8227SGreg Roach/**
258c2e8227SGreg Roach * Class FamilyTreeNewsModule
268c2e8227SGreg Roach */
27e2a378d3SGreg Roachclass FamilyTreeNewsModule extends AbstractModule implements ModuleBlockInterface {
287fafef87SGreg Roach	// How to update the database schema for this module
297fafef87SGreg Roach	const SCHEMA_TARGET_VERSION   = 3;
307fafef87SGreg Roach	const SCHEMA_SETTING_NAME     = 'NB_SCHEMA_VERSION';
317fafef87SGreg Roach	const SCHEMA_MIGRATION_PREFIX = '\Fisharebest\Webtrees\Module\FamilyTreeNews\Schema';
327fafef87SGreg Roach
3376692c8bSGreg Roach	/**
3476692c8bSGreg Roach	 * Create a new module.
3576692c8bSGreg Roach	 *
3676692c8bSGreg Roach	 * @param string $directory Where is this module installed
3776692c8bSGreg Roach	 */
383bfb94b0SGreg Roach	public function __construct($directory) {
393bfb94b0SGreg Roach		parent::__construct($directory);
403bfb94b0SGreg Roach
413bfb94b0SGreg Roach		// Create/update the database tables.
427fafef87SGreg Roach		Database::updateSchema(self::SCHEMA_MIGRATION_PREFIX, self::SCHEMA_SETTING_NAME, self::SCHEMA_TARGET_VERSION);
433bfb94b0SGreg Roach	}
443bfb94b0SGreg Roach
4576692c8bSGreg Roach	/**
4676692c8bSGreg Roach	 * How should this module be labelled on tabs, menus, etc.?
4776692c8bSGreg Roach	 *
4876692c8bSGreg Roach	 * @return string
4976692c8bSGreg Roach	 */
508c2e8227SGreg Roach	public function getTitle() {
518c2e8227SGreg Roach		return /* I18N: Name of a module */ I18N::translate('News');
528c2e8227SGreg Roach	}
538c2e8227SGreg Roach
5476692c8bSGreg Roach	/**
5576692c8bSGreg Roach	 * A sentence describing what this module does.
5676692c8bSGreg Roach	 *
5776692c8bSGreg Roach	 * @return string
5876692c8bSGreg Roach	 */
598c2e8227SGreg Roach	public function getDescription() {
60d004f62cSGreg Roach		return /* I18N: Description of the “News” module */ I18N::translate('Family news and site announcements.');
618c2e8227SGreg Roach	}
628c2e8227SGreg Roach
6376692c8bSGreg Roach	/**
6476692c8bSGreg Roach	 * Generate the HTML content of this block.
6576692c8bSGreg Roach	 *
6676692c8bSGreg Roach	 * @param int      $block_id
6776692c8bSGreg Roach	 * @param bool     $template
68727f238cSGreg Roach	 * @param string[] $cfg
6976692c8bSGreg Roach	 *
7076692c8bSGreg Roach	 * @return string
7176692c8bSGreg Roach	 */
72a9430be8SGreg Roach	public function getBlock($block_id, $template = true, $cfg = []): string {
734b9ff166SGreg Roach		global $ctype, $WT_TREE;
748c2e8227SGreg Roach
75d004f62cSGreg Roach		$more_news = Filter::getInteger('more_news');
76d004f62cSGreg Roach		$limit     = 5 * (1 + $more_news);
77d004f62cSGreg Roach
78d004f62cSGreg Roach		$articles = Database::prepare(
7945d06cf3SGreg 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"
8013abd6f3SGreg Roach		)->execute([
8145d06cf3SGreg Roach			'offset'  => WT_TIMESTAMP_OFFSET,
82d004f62cSGreg Roach			'tree_id' => $WT_TREE->getTreeId(),
83d004f62cSGreg Roach			'limit'   => $limit,
8413abd6f3SGreg Roach		])->fetchAll();
85d004f62cSGreg Roach
86d004f62cSGreg Roach		$count = Database::prepare(
87d004f62cSGreg Roach			"SELECT SQL_CACHE COUNT(*) FROM `##news` WHERE gedcom_id = :tree_id"
8813abd6f3SGreg Roach		)->execute([
89d004f62cSGreg Roach			'tree_id' => $WT_TREE->getTreeId(),
9013abd6f3SGreg Roach		])->fetchOne();
918c2e8227SGreg Roach
928c2e8227SGreg Roach		$id      = $this->getName() . $block_id;
938c2e8227SGreg Roach		$class   = $this->getName() . '_block';
94d004f62cSGreg Roach		$title   = $this->getTitle();
958c2e8227SGreg Roach		$content = '';
96d004f62cSGreg Roach
97d004f62cSGreg Roach		if (empty($articles)) {
98d004f62cSGreg Roach			$content .= I18N::translate('No news articles have been submitted.');
998c2e8227SGreg Roach		}
100d004f62cSGreg Roach
101d004f62cSGreg Roach		foreach ($articles as $article) {
102d004f62cSGreg Roach			$content .= '<div class="news_box">';
103d53324c9SGreg Roach			$content .= '<div class="news_title">' . e($article->subject) . '</div>';
104d004f62cSGreg Roach			$content .= '<div class="news_date">' . FunctionsDate::formatTimestamp($article->updated) . '</div>';
105d004f62cSGreg Roach			if ($article->body == strip_tags($article->body)) {
106d004f62cSGreg Roach				$article->body = nl2br($article->body, false);
1078c2e8227SGreg Roach			}
108d004f62cSGreg Roach			$content .= $article->body;
1094b9ff166SGreg Roach			if (Auth::isManager($WT_TREE)) {
110d004f62cSGreg Roach				$content .= '<hr>';
11115d603e7SGreg Roach				$content .= '<a href="editnews.php?news_id=' . $article->news_id . '&amp;ctype=gedcom&amp;ged=' . $WT_TREE->getNameHtml() . '">' . I18N::translate('Edit') . '</a>';
112d004f62cSGreg Roach				$content .= ' | ';
113d53324c9SGreg 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”?', e($article->subject)) . "');\">" . I18N::translate('Delete') . '</a><br>';
1148c2e8227SGreg Roach			}
1158c2e8227SGreg Roach			$content .= '</div>';
1168c2e8227SGreg Roach		}
117d004f62cSGreg Roach
1184b9ff166SGreg Roach		if (Auth::isManager($WT_TREE)) {
11915d603e7SGreg Roach			$content .= '<p><a href="editnews.php?ctype=gedcom&amp;ged=' . $WT_TREE->getNameUrl() . '">' . I18N::translate('Add a news article') . '</a></p>';
1208c2e8227SGreg Roach		}
121d004f62cSGreg Roach
122d004f62cSGreg Roach		if ($count > $limit) {
123d004f62cSGreg Roach			if (Auth::isManager($WT_TREE)) {
124d004f62cSGreg Roach				$content .= ' | ';
1258c2e8227SGreg Roach			}
126564ae2d7SGreg 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>';
1278c2e8227SGreg Roach		}
1288c2e8227SGreg Roach
1298c2e8227SGreg Roach		if ($template) {
130*225e381fSGreg Roach			return view('blocks/template', [
1319c6524dcSGreg Roach				'block'      => str_replace('_', '-', $this->getName()),
1329c6524dcSGreg Roach				'id'         => $block_id,
1339c6524dcSGreg Roach				'config_url' => '',
1349c6524dcSGreg Roach				'title'      => $this->getTitle(),
1359c6524dcSGreg Roach				'content'    => $content,
1369c6524dcSGreg Roach			]);
1378c2e8227SGreg Roach		} else {
1388c2e8227SGreg Roach			return $content;
1398c2e8227SGreg Roach		}
1408c2e8227SGreg Roach	}
1418c2e8227SGreg Roach
1428c2e8227SGreg Roach	/** {@inheritdoc} */
143a9430be8SGreg Roach	public function loadAjax(): bool {
1448c2e8227SGreg Roach		return false;
1458c2e8227SGreg Roach	}
1468c2e8227SGreg Roach
1478c2e8227SGreg Roach	/** {@inheritdoc} */
148a9430be8SGreg Roach	public function isUserBlock(): bool {
1498c2e8227SGreg Roach		return false;
1508c2e8227SGreg Roach	}
1518c2e8227SGreg Roach
1528c2e8227SGreg Roach	/** {@inheritdoc} */
153a9430be8SGreg Roach	public function isGedcomBlock(): bool {
1548c2e8227SGreg Roach		return true;
1558c2e8227SGreg Roach	}
1568c2e8227SGreg Roach
15776692c8bSGreg Roach	/**
15876692c8bSGreg Roach	 * An HTML form to edit block settings
15976692c8bSGreg Roach	 *
16076692c8bSGreg Roach	 * @param int $block_id
161a9430be8SGreg Roach	 *
162a9430be8SGreg Roach	 * @return void
16376692c8bSGreg Roach	 */
164be9a728cSGreg Roach	public function configureBlock($block_id) {
1658c2e8227SGreg Roach	}
1668c2e8227SGreg Roach}
167