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