xref: /webtrees/app/Module/FamilyTreeNewsModule.php (revision d2681c37325a35ab01be82034f4afd3b58010fb8)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 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\I18N;
23
24/**
25 * Class FamilyTreeNewsModule
26 */
27class FamilyTreeNewsModule extends AbstractModule implements ModuleBlockInterface {
28	// How to update the database schema for this module
29	const SCHEMA_TARGET_VERSION   = 3;
30	const SCHEMA_SETTING_NAME     = 'NB_SCHEMA_VERSION';
31	const SCHEMA_MIGRATION_PREFIX = '\Fisharebest\Webtrees\Module\FamilyTreeNews\Schema';
32
33	/**
34	 * Create a new module.
35	 *
36	 * @param string $directory Where is this module installed
37	 */
38	public function __construct($directory) {
39		parent::__construct($directory);
40
41		// Create/update the database tables.
42		Database::updateSchema(self::SCHEMA_MIGRATION_PREFIX, self::SCHEMA_SETTING_NAME, self::SCHEMA_TARGET_VERSION);
43	}
44
45	/**
46	 * How should this module be labelled on tabs, menus, etc.?
47	 *
48	 * @return string
49	 */
50	public function getTitle() {
51		return /* I18N: Name of a module */ I18N::translate('News');
52	}
53
54	/**
55	 * A sentence describing what this module does.
56	 *
57	 * @return string
58	 */
59	public function getDescription() {
60		return /* I18N: Description of the “News” module */ I18N::translate('Family news and site announcements.');
61	}
62
63	/**
64	 * Generate the HTML content of this block.
65	 *
66	 * @param int      $block_id
67	 * @param bool     $template
68	 * @param string[] $cfg
69	 *
70	 * @return string
71	 */
72	public function getBlock($block_id, $template = true, $cfg = []): string {
73		global $ctype, $WT_TREE;
74
75		$more_news = Filter::getInteger('more_news');
76		$limit     = 5 * (1 + $more_news);
77
78		$articles = Database::prepare(
79			"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"
80		)->execute([
81			'offset'  => WT_TIMESTAMP_OFFSET,
82			'tree_id' => $WT_TREE->getTreeId(),
83			'limit'   => $limit,
84		])->fetchAll();
85
86		$count = Database::prepare(
87			"SELECT SQL_CACHE COUNT(*) FROM `##news` WHERE gedcom_id = :tree_id"
88		)->execute([
89			'tree_id' => $WT_TREE->getTreeId(),
90		])->fetchOne();
91
92		$id      = $this->getName() . $block_id;
93		$class   = $this->getName() . '_block';
94		$title   = $this->getTitle();
95		$content = '';
96
97		if (empty($articles)) {
98			$content .= I18N::translate('No news articles have been submitted.');
99		}
100
101		foreach ($articles as $article) {
102			$content .= '<div class="news_box">';
103			$content .= '<div class="news_title">' . e($article->subject) . '</div>';
104			$content .= '<div class="news_date">' . FunctionsDate::formatTimestamp($article->updated) . '</div>';
105			if ($article->body == strip_tags($article->body)) {
106				$article->body = nl2br($article->body, false);
107			}
108			$content .= $article->body;
109			if (Auth::isManager($WT_TREE)) {
110				$content .= '<hr>';
111				$content .= '<a href="editnews.php?news_id=' . $article->news_id . '&amp;ctype=gedcom&amp;ged=' . $WT_TREE->getNameHtml() . '">' . I18N::translate('Edit') . '</a>';
112				$content .= ' | ';
113				$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>';
114			}
115			$content .= '</div>';
116		}
117
118		if (Auth::isManager($WT_TREE)) {
119			$content .= '<p><a href="editnews.php?ctype=gedcom&amp;ged=' . $WT_TREE->getNameUrl() . '">' . I18N::translate('Add a news article') . '</a></p>';
120		}
121
122		if ($count > $limit) {
123			if (Auth::isManager($WT_TREE)) {
124				$content .= ' | ';
125			}
126			$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>';
127		}
128
129		if ($template) {
130			return view('blocks/template', [
131				'block'      => str_replace('_', '-', $this->getName()),
132				'id'         => $block_id,
133				'config_url' => '',
134				'title'      => $this->getTitle(),
135				'content'    => $content,
136			]);
137		} else {
138			return $content;
139		}
140	}
141
142	/** {@inheritdoc} */
143	public function loadAjax(): bool {
144		return false;
145	}
146
147	/** {@inheritdoc} */
148	public function isUserBlock(): bool {
149		return false;
150	}
151
152	/** {@inheritdoc} */
153	public function isGedcomBlock(): bool {
154		return true;
155	}
156
157	/**
158	 * An HTML form to edit block settings
159	 *
160	 * @param int $block_id
161	 *
162	 * @return void
163	 */
164	public function configureBlock($block_id) {
165	}
166}
167