xref: /webtrees/app/Module/FamilyTreeNewsModule.php (revision 9797fe2e6d6eb3d34559dcf546f47c58bc50777b)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2016 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;
23use Fisharebest\Webtrees\Theme;
24
25/**
26 * Class FamilyTreeNewsModule
27 */
28class FamilyTreeNewsModule extends AbstractModule implements ModuleBlockInterface {
29	// How to update the database schema for this module
30	const SCHEMA_TARGET_VERSION   = 3;
31	const SCHEMA_SETTING_NAME     = 'NB_SCHEMA_VERSION';
32	const SCHEMA_MIGRATION_PREFIX = '\Fisharebest\Webtrees\Module\FamilyTreeNews\Schema';
33
34	/**
35	 * Create a new module.
36	 *
37	 * @param string $directory Where is this module installed
38	 */
39	public function __construct($directory) {
40		parent::__construct($directory);
41
42		// Create/update the database tables.
43		// NOTE: if we want to set any module-settings, we'll need to move this.
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 = array()) {
75		global $ctype, $WT_TREE;
76
77		switch (Filter::get('action')) {
78		case 'deletenews':
79			$news_id = Filter::get('news_id');
80			if ($news_id) {
81				Database::prepare("DELETE FROM `##news` WHERE news_id = ?")->execute(array($news_id));
82			}
83			break;
84		}
85
86		$more_news = Filter::getInteger('more_news');
87		$limit     = 5 * (1 + $more_news);
88
89		$articles = Database::prepare(
90			"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"
91		)->execute(array(
92			'offset'  => WT_TIMESTAMP_OFFSET,
93			'tree_id' => $WT_TREE->getTreeId(),
94			'limit'   => $limit,
95		))->fetchAll();
96
97		$count = Database::prepare(
98			"SELECT SQL_CACHE COUNT(*) FROM `##news` WHERE gedcom_id = :tree_id"
99		)->execute(array(
100			'tree_id' => $WT_TREE->getTreeId(),
101		))->fetchOne();
102
103		$id      = $this->getName() . $block_id;
104		$class   = $this->getName() . '_block';
105		$title   = $this->getTitle();
106		$content = '';
107
108		if (empty($articles)) {
109			$content .= I18N::translate('No news articles have been submitted.');
110		}
111
112		foreach ($articles as $article) {
113			$content .= '<div class="news_box">';
114			$content .= '<div class="news_title">' . Filter::escapeHtml($article->subject) . '</div>';
115			$content .= '<div class="news_date">' . FunctionsDate::formatTimestamp($article->updated) . '</div>';
116			if ($article->body == strip_tags($article->body)) {
117				$article->body = nl2br($article->body, false);
118			}
119			$content .= $article->body;
120			if (Auth::isManager($WT_TREE)) {
121				$content .= '<hr>';
122				$content .= '<a href="#" onclick="window.open(\'editnews.php?news_id=\'+' . $article->news_id . ', \'_blank\', news_window_specs); return false;">' . I18N::translate('Edit') . '</a>';
123				$content .= ' | ';
124				$content .= '<a href="index.php?action=deletenews&amp;news_id=' . $article->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($article->subject)) . "');\">" . I18N::translate('Delete') . '</a><br>';
125			}
126			$content .= '</div>';
127		}
128
129		if (Auth::isManager($WT_TREE)) {
130			$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>';
131		}
132
133		if ($count > $limit) {
134			if (Auth::isManager($WT_TREE)) {
135				$content .= ' | ';
136			}
137			$content .= '<a href="#" onclick="jQuery(\'#' . $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>";
138		}
139
140		if ($template) {
141			return Theme::theme()->formatBlock($id, $title, $class, $content);
142		} else {
143			return $content;
144		}
145	}
146
147	/** {@inheritdoc} */
148	public function loadAjax() {
149		return false;
150	}
151
152	/** {@inheritdoc} */
153	public function isUserBlock() {
154		return false;
155	}
156
157	/** {@inheritdoc} */
158	public function isGedcomBlock() {
159		return true;
160	}
161
162	/**
163	 * An HTML form to edit block settings
164	 *
165	 * @param int $block_id
166	 */
167	public function configureBlock($block_id) {
168	}
169}
170